/* ==========================================================================
   Tokens
   ========================================================================== */
:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1a1d23;
  --text-muted: #5b6270;
  --border: #e2e5ea;
  --brand: #1e3a5f;
  --brand-2: #2c5282;
  --danger: #c0392b;
  --danger-bg: #fdecec;
  --ok: #2f9e44;
  --ok-bg: #e8f7ec;
  --radius: 8px;
  --shadow: 0 1px 6px rgba(20, 25, 35, 0.08);
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151a;
    --surface: #1a1e26;
    --text: #eef0f3;
    --text-muted: #9aa2b1;
    --border: #2b3038;
    --brand: #7fb0e0;
    --brand-2: #a9cdf0;
    --danger: #ff6b5e;
    --danger-bg: #2c1615;
    --ok: #57d17b;
    --ok-bg: #16281c;
    --shadow: 0 1px 8px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

h1, h2, h3 { line-height: 1.25; }
code {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 0.9em;
}

/* ==========================================================================
   Login
   ========================================================================== */
.login-view { min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.login-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 28px; width: 320px;
  display: flex; flex-direction: column; gap: var(--space-3);
}
.login-card h1 { font-size: 1.2rem; margin: 0 0 var(--space-1); }
.login-card label { display: flex; flex-direction: column; gap: var(--space-1); font-size: 0.85rem; color: var(--text-muted); }
.login-card input { padding: 9px 10px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); color: var(--text); font-size: 0.95rem; }
.login-card button { margin-top: var(--space-1); background: var(--brand); color: #fff; border: none; padding: 10px; border-radius: 6px; cursor: pointer; font-weight: 600; }
.error-text { color: var(--danger); font-size: 0.85rem; margin: 0; }

/* ==========================================================================
   Shell: header + tabs
   ========================================================================== */
.admin-header { background: var(--surface); border-bottom: 1px solid var(--border); }
.admin-header-inner { max-width: 1100px; margin: 0 auto; padding: 14px 24px; display: flex; justify-content: space-between; align-items: center; }
.tabs { max-width: 1100px; margin: 0 auto; padding: 0 24px; display: flex; gap: 4px; overflow-x: auto; }
.tab-btn { background: none; border: none; padding: 10px 16px; cursor: pointer; color: var(--text-muted); font-size: 0.9rem; border-bottom: 2px solid transparent; white-space: nowrap; }
.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--brand); border-bottom-color: var(--brand); font-weight: 600; }

.admin-main { max-width: 1100px; margin: 0 auto; padding: var(--space-5) 24px 60px; }

/* ==========================================================================
   Generic building blocks
   ========================================================================== */
.panel-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: var(--space-4); flex-wrap: wrap; gap: var(--space-3); }
.panel-header h2 { margin: 0; font-size: 1.15rem; }
.panel-header input[type="search"] { padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text); min-width: 220px; }

.section-subtitle { margin: 0 0 var(--space-2); font-size: 1rem; }
.settings-section-title { margin-top: var(--space-6); padding-top: var(--space-5); border-top: 1px solid var(--border); }

.hint { color: var(--text-muted); font-size: 0.88rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; margin: 0 0 var(--space-4); }
.hint-compact { padding: 0; border: none; background: none; margin: 0 0 var(--space-3); }
.muted { color: var(--text-muted); font-size: 0.85rem; }

.form-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; margin: 0 0 var(--space-4); }
.form-card summary { cursor: pointer; font-weight: 600; }

/* Form a griglia 2 colonne, usata per broker/contatti/composer */
.grid-form { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-3) var(--space-4); margin-top: var(--space-4); }
.grid-form .full { grid-column: 1 / -1; }

/* Form impilata a colonna singola, usata in Impostazioni */
.stacked-form { display: flex; flex-direction: column; gap: var(--space-3); max-width: 480px; }

.grid-form label, .stacked-form label { display: flex; flex-direction: column; gap: var(--space-1); font-size: 0.85rem; color: var(--text-muted); }
.grid-form input, .grid-form select, .grid-form textarea,
.stacked-form input, .stacked-form select, .stacked-form textarea {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--bg); color: var(--text); font-size: 0.92rem; font-family: inherit;
}
.grid-form input:focus, .grid-form select:focus, .grid-form textarea:focus,
.stacked-form input:focus, .stacked-form select:focus, .stacked-form textarea:focus {
  outline: 2px solid var(--brand-2); outline-offset: 1px;
}

.input-with-action { display: flex; gap: var(--space-2); align-items: center; }
.input-with-action input { flex: 1; min-width: 0; }
.input-with-action .secondary-btn { padding: 8px 12px; font-size: 0.82rem; white-space: nowrap; }

.form-actions { display: flex; align-items: center; gap: var(--space-3); flex-wrap: wrap; }
.form-actions button { padding: 9px 18px; border-radius: 6px; border: none; cursor: pointer; font-weight: 600; font-size: 0.88rem; }
.form-actions button[type="submit"], .primary-btn { background: var(--brand); color: #fff; }
.secondary-btn { background: var(--bg); color: var(--text); border: 1px solid var(--border) !important; }
.link-btn { background: none; border: none; color: var(--brand-2); cursor: pointer; font-size: 0.85rem; padding: 0; }
.icon-btn { background: none; border: none; color: var(--text-muted); cursor: pointer; padding: 4px; border-radius: 6px; display: inline-flex; }
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn:hover { color: var(--text); background: var(--bg); }

/* ==========================================================================
   Tabelle
   ========================================================================== */
.table-scroll { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; font-size: 0.87rem; }
.data-table th, .data-table td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.data-table th { color: var(--text-muted); font-weight: 600; background: var(--bg); white-space: nowrap; }
.data-table tr:last-child td { border-bottom: none; }
.row-actions { display: flex; gap: var(--space-2); }
.row-actions button { background: none; border: none; color: var(--brand-2); cursor: pointer; font-size: 0.85rem; padding: 0; }
.row-actions button.danger { color: var(--danger); }
.row-link { background: none; border: none; color: var(--brand-2); cursor: pointer; font: inherit; padding: 0; text-decoration: underline; }

.table-select { padding: 5px 7px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); color: var(--text); font-size: 0.82rem; min-width: 130px; }
.send-both-btn { background: var(--brand); color: #fff; border: none; padding: 6px 14px; border-radius: 6px; cursor: pointer; font-size: 0.82rem; font-weight: 600; white-space: nowrap; }
.send-both-btn:disabled { opacity: 0.55; cursor: default; }
.send-feedback { font-size: 0.76rem; margin-top: var(--space-1); min-height: 1.1em; }

/* ==========================================================================
   Badge / stato
   ========================================================================== */
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 0.75rem; font-weight: 600; white-space: nowrap; }
.badge.fraudulent { background: var(--danger-bg); color: #8a1f1a; }
.badge.suspicious { background: #fff7e6; color: #7a5410; }
.badge.cleared { background: var(--ok-bg); color: #1f7a34; }
.badge.opted_in { background: var(--ok-bg); color: #1f7a34; }
.badge.opted_out { background: var(--danger-bg); color: #8a1f1a; }
.badge.unknown { background: #eee; color: #555; }
.badge.new { background: #e6f0fb; color: #1a4d8f; }
.badge.in_progress { background: #fff7e6; color: #7a5410; }
.badge.done { background: var(--ok-bg); color: #1f7a34; }

.assistance-description { white-space: pre-wrap; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 12px 14px; font-size: 0.9rem; line-height: 1.5; }
.assistance-files-list { list-style: none; padding: 0; margin: 0 0 var(--space-3); display: flex; flex-direction: column; gap: 6px; }
.assistance-files-list li { display: flex; justify-content: space-between; align-items: center; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 8px 12px; font-size: 0.85rem; }
.assistance-status-select { padding: 5px 7px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); color: var(--text); font-size: 0.82rem; }

.delivery-badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 0.75rem; font-weight: 600; background: #eee; color: #555; white-space: nowrap; }
.delivery-badge.delivered { background: var(--ok-bg); color: #1f7a34; }
.delivery-badge.sent { background: #e6f0fb; color: #1a4d8f; }
.delivery-badge.failed, .delivery-badge.undelivered { background: var(--danger-bg); color: #8a1f1a; }

/* ==========================================================================
   Pagine SMS / Email: due colonne (form a sinistra, anteprima a destra)
   ========================================================================== */
.compose-columns { display: grid; grid-template-columns: 3fr 2fr; gap: var(--space-5); align-items: start; margin-bottom: var(--space-5); }
/* La pagina Email dà più spazio all'anteprima, dovendo mostrare un'email intera */
.compose-columns-email { grid-template-columns: 1fr 1.2fr; }

.compose-form {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: var(--space-4) var(--space-5); display: flex; flex-direction: column; gap: var(--space-3);
}
.compose-form label { display: flex; flex-direction: column; gap: var(--space-1); font-size: 0.85rem; color: var(--text-muted); }
.compose-form input, .compose-form select, .compose-form textarea {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--bg); color: var(--text); font-size: 0.92rem; font-family: inherit;
}
.compose-form input:focus, .compose-form select:focus, .compose-form textarea:focus {
  outline: 2px solid var(--brand-2); outline-offset: 1px;
}

.compose-actions { padding-top: var(--space-2); border-top: 1px solid var(--border); }
.actions-divider { color: var(--text-muted); font-size: 0.82rem; }
.compose-actions input[type="text"] { padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); color: var(--text); font-size: 0.88rem; min-width: 200px; }
.save-status { font-size: 0.82rem; color: var(--ok); }
.template-preview { color: var(--text-muted); max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.compose-preview-col { position: sticky; top: var(--space-4); display: flex; flex-direction: column; gap: var(--space-2); }
.char-counter { font-size: 0.78rem; color: var(--text-muted); }
.preview-label { display: block; font-size: 0.78rem; color: var(--text-muted); margin-bottom: var(--space-1); }
.sms-preview-bubble {
  display: inline-block; max-width: 100%; background: #e8f3ff; color: #16324f;
  border-radius: 16px 16px 16px 4px; padding: 10px 14px; font-size: 0.9rem; line-height: 1.4;
  white-space: pre-wrap; word-break: break-word;
}
@media (prefers-color-scheme: dark) {
  .sms-preview-bubble { background: #1f3a52; color: #d8ecff; }
}

.email-preview { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--surface); }
.email-preview-header { padding: 10px 16px; background: var(--bg); border-bottom: 1px solid var(--border); font-size: 0.85rem; color: var(--text-muted); overflow-wrap: anywhere; }
.email-preview-body { min-height: 480px; max-height: 780px; overflow-y: auto; }
.email-preview-text { padding: 20px; font-size: 0.92rem; line-height: 1.6; white-space: pre-wrap; word-break: break-word; color: var(--text); }
.email-preview-html { background: #fff; }
.format-badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 0.72rem; font-weight: 600; background: #eee; color: #555; }
.format-badge.html { background: #e6f0fb; color: #1a4d8f; }

/* Tab in stile editor WordPress (Testo | HTML) sopra il textarea dell'email */
.wp-editor-field { display: flex; flex-direction: column; }
.wp-editor-hint { font-size: 0.78rem; color: var(--text-muted); margin-bottom: var(--space-2); }
.wp-editor-tabs { display: flex; gap: 2px; }
.wp-editor-tab {
  background: var(--bg); border: 1px solid var(--border); border-bottom-color: transparent;
  padding: 7px 16px; font-size: 0.82rem; font-weight: 600; color: var(--text-muted);
  border-radius: 6px 6px 0 0; cursor: pointer; position: relative; top: 1px;
}
.wp-editor-tab.active { background: var(--surface); color: var(--brand); border-bottom-color: var(--surface); }
.wp-editor-tab:hover:not(.active) { color: var(--text); }
.wp-editor-textarea { border-radius: 0 6px 6px 6px !important; }
.wp-editor-textarea.code-mode { font-family: "SFMono-Regular", Consolas, Menlo, monospace; font-size: 0.85rem !important; }

/* ==========================================================================
   Mockup telefono per l'anteprima SMS
   ========================================================================== */
.phone-mockup { display: flex; justify-content: center; }
.phone-frame {
  width: 100%; max-width: 300px; background: #1c1c1e; border-radius: 32px; padding: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}
.phone-statusbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 16px 2px; color: #fff; font-size: 0.72rem; font-weight: 600;
}
.phone-statusbar-icons { display: flex; align-items: center; gap: 5px; }
.phone-contact-bar {
  text-align: center; color: #fff; font-size: 0.82rem; font-weight: 600; padding: 4px 0 10px;
}
.phone-screen {
  background: #f4f4f6; border-radius: 20px; min-height: 200px;
  padding: 16px 12px; display: flex; flex-direction: column;
}

@media (max-width: 860px) {
  .compose-columns, .compose-columns-email { grid-template-columns: 1fr; }
  .compose-preview-col { position: static; }
}

/* ==========================================================================
   Impostazioni: due colonne + form stretta per la password
   ========================================================================== */
.tab-panel.wide { max-width: none; }
.settings-columns { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-6); align-items: start; }
.settings-column { min-width: 0; }
.narrow-form { max-width: 380px; }
.test-result { margin: var(--space-2) 0 0; font-size: 0.85rem; padding: 8px 10px; border-radius: 6px; }
.test-result.ok { background: var(--ok-bg); color: #1f7a34; }
.test-result.error { background: var(--danger-bg); color: #8a1f1a; }

@media (max-width: 760px) {
  .settings-columns { grid-template-columns: 1fr; }
  .grid-form { grid-template-columns: 1fr; }
}

/* ==========================================================================
   Overlay dettaglio contatto
   ========================================================================== */
.overlay {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; z-index: 50; overflow-y: auto;
}
.overlay-panel {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  max-width: 760px; width: 100%; padding: var(--space-5) var(--space-5) 28px;
}
.overlay-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: var(--space-3); }
.overlay-header h2 { margin: 0; font-size: 1.1rem; }
.contact-detail-info {
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 12px 14px;
  margin-bottom: var(--space-5); font-size: 0.88rem;
  display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: var(--space-2);
}
.contact-detail-info div strong { display: block; color: var(--text-muted); font-size: 0.75rem; text-transform: uppercase; margin-bottom: 2px; }
