:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --ink: #1a1a1a;
  --muted: #6b7280;
  --line: #e2e4e8;
  --accent: #2d5bff;
  --accent-ink: #ffffff;
  --ok-bg: #e7f6ec; --ok-ink: #1c7a3e;
  --err-bg: #fdeaea; --err-ink: #b42318;
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 15px; line-height: 1.5;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--panel); border-bottom: 1px solid var(--line);
  padding: 10px 20px;
}
.topbar .brand { font-weight: 700; color: var(--ink); }
.topbar .links { display: flex; align-items: center; gap: 18px; }
.linklike { background: none; border: none; color: var(--accent); cursor: pointer; font: inherit; padding: 0; }

.wrap { max-width: 900px; margin: 24px auto; padding: 0 20px; }
.wrap.wide { max-width: 1280px; }

.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
h1 { font-size: 22px; margin: 0; }

.muted { color: var(--muted); }
.small { font-size: 13px; }

/* bottoni */
.btn, button.primary, .primary.btn {
  display: inline-block; padding: 7px 12px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  cursor: pointer; font: inherit; text-decoration: none;
}
.btn:hover { background: #f0f1f4; text-decoration: none; }
.primary, button.primary {
  background: var(--accent); color: var(--accent-ink); border-color: var(--accent);
}
.primary:hover { background: #1e46d6; }
.danger { color: var(--err-ink); border-color: #f2c4c0; }
.inline { display: inline; }

/* tabelle */
table.grid { width: 100%; border-collapse: collapse; background: var(--panel);
  border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.grid th, .grid td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.grid th { background: #fafbfc; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.grid tr:last-child td { border-bottom: none; }
.actions { white-space: nowrap; }
.actions .btn, .actions form { margin-left: 4px; }

/* form */
label { display: block; margin: 14px 0; font-weight: 600; }
label input[type=text], label input[type=email], label input[type=password], label select, input, select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px;
  font: inherit; margin-top: 5px; background: #fff; font-weight: 400;
}
textarea { resize: vertical; line-height: 1.5; }
:root[data-theme="dark"] textarea, :root[data-theme="auto"] textarea { background: var(--panel); color: var(--ink); }
.form-actions { margin-top: 20px; display: flex; gap: 10px; align-items: center; }

.fields-box { border: 1px solid var(--line); border-radius: 10px; padding: 8px 16px 16px; margin: 18px 0; background: var(--panel); }
.fields-box legend { font-weight: 700; padding: 0 6px; }
.field-row { display: flex; align-items: center; gap: 12px; margin: 8px 0; font-weight: 400; }
.field-name { min-width: 140px; font-family: ui-monospace, Menlo, monospace; font-size: 13px; color: var(--accent); }
.field-row input { margin-top: 0; }

/* editor */
#editor { background: #fff; border-radius: 0 0 8px 8px; min-height: 260px; }
.ql-toolbar.ql-snow, .ql-container.ql-snow { border-color: var(--line); }
.ql-container.ql-snow { border-radius: 0 0 8px 8px; font-size: 15px; }
.ql-toolbar.ql-snow { border-radius: 8px 8px 0 0; }

/* login */
.login-box { max-width: 360px; margin: 8vh auto; background: var(--panel);
  border: 1px solid var(--line); border-radius: 14px; padding: 32px; text-align: center; }
.login-box h1 { margin: 0 0 2px; }
.login-box form { text-align: left; margin-top: 18px; }

/* alert / badge */
.alert { padding: 10px 14px; border-radius: 8px; margin: 12px 0; font-weight: 600; }
.alert.ok { background: var(--ok-bg); color: var(--ok-ink); }
.alert.error { background: var(--err-bg); color: var(--err-ink); }
.badge { padding: 3px 9px; border-radius: 20px; font-size: 12px; font-weight: 700; }
.badge.ok { background: var(--ok-bg); color: var(--ok-ink); }
.badge.err { background: var(--err-bg); color: var(--err-ink); }

/* anteprima invio */
.preview-block { border: 1px solid var(--line); border-radius: 10px; margin: 18px 0; overflow: hidden; background: var(--panel); }
.preview-head { background: #fafbfc; padding: 8px 14px; font-size: 13px; color: var(--muted); border-bottom: 1px solid var(--line); text-transform: uppercase; letter-spacing: .03em; }
.preview-subject { padding: 10px 14px; border-bottom: 1px solid var(--line); }
/* Il corpo dell'anteprima resta chiaro: rappresenta l'email reale, sempre su bianco. */
.preview-body { padding: 14px; background: #ffffff; color: #1a1a1a; }
/* Paragrafi compatti come nell'editor e nell'email inviata (niente a capo doppi). */
.preview-body p { margin: 0; padding: 0; }
.preview-body ul, .preview-body ol { margin: 0 0 0 1.2em; padding: 0; }

.filters { display: flex; gap: 10px; margin-bottom: 16px; }
.filters select { width: auto; margin-top: 0; }

/* ============================ TEMA SCURO ============================ */
/* Valori scuri riusati sia in automatico (media query) sia forzati. */
:root[data-theme="dark"] {
  --bg: #0f1115;
  --panel: #171a21;
  --ink: #e6e8ec;
  --muted: #9aa3af;
  --line: #2a2f3a;
  --accent: #5b7cff;
  --accent-ink: #ffffff;
  --ok-bg: #12331f; --ok-ink: #62d18a;
  --err-bg: #3a1a18; --err-ink: #f0857a;
}
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #0f1115;
    --panel: #171a21;
    --ink: #e6e8ec;
    --muted: #9aa3af;
    --line: #2a2f3a;
    --accent: #5b7cff;
    --accent-ink: #ffffff;
    --ok-bg: #12331f; --ok-ink: #62d18a;
    --err-bg: #3a1a18; --err-ink: #f0857a;
  }
}
/* In scuro gli input/tabelle usano il pannello, non bianco fisso. */
:root[data-theme="dark"] input,
:root[data-theme="dark"] select,
:root[data-theme="auto"] input,
:root[data-theme="auto"] select { background: var(--panel); color: var(--ink); }
.btn:hover { background: color-mix(in srgb, var(--line) 45%, var(--panel)); }
.grid th { background: color-mix(in srgb, var(--line) 30%, var(--panel)); }
.preview-head, .preview-subject { background: color-mix(in srgb, var(--line) 25%, var(--panel)); }

/* Quill in scuro: editor, toolbar e icone leggibili. */
:root[data-theme="dark"] #editor,
:root[data-theme="dark"] .ql-toolbar.ql-snow { background: var(--panel); }
:root[data-theme="dark"] .ql-editor { color: var(--ink); }
:root[data-theme="dark"] .ql-editor.ql-blank::before { color: var(--muted); }
:root[data-theme="dark"] .ql-snow .ql-stroke { stroke: var(--ink); }
:root[data-theme="dark"] .ql-snow .ql-fill { fill: var(--ink); }
:root[data-theme="dark"] .ql-snow .ql-picker { color: var(--ink); }
:root[data-theme="dark"] .ql-snow .ql-picker-options { background: var(--panel); }
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] #editor,
  :root[data-theme="auto"] .ql-toolbar.ql-snow { background: var(--panel); }
  :root[data-theme="auto"] .ql-editor { color: var(--ink); }
  :root[data-theme="auto"] .ql-editor.ql-blank::before { color: var(--muted); }
  :root[data-theme="auto"] .ql-snow .ql-stroke { stroke: var(--ink); }
  :root[data-theme="auto"] .ql-snow .ql-fill { fill: var(--ink); }
  :root[data-theme="auto"] .ql-snow .ql-picker { color: var(--ink); }
  :root[data-theme="auto"] .ql-snow .ql-picker-options { background: var(--panel); }
}

/* Selettore tema, fisso in basso a destra. */
.theme-switch {
  position: fixed; bottom: 16px; right: 16px; display: flex; gap: 2px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
  padding: 3px; box-shadow: 0 2px 10px rgba(0,0,0,.15); z-index: 50;
}
.theme-switch button {
  border: none; background: none; cursor: pointer; width: 32px; height: 32px;
  border-radius: 999px; font-size: 15px; line-height: 1; color: var(--muted); padding: 0;
}
.theme-switch button:hover { background: color-mix(in srgb, var(--line) 40%, var(--panel)); }
.theme-switch button.active { background: var(--accent); color: var(--accent-ink); }

/* ===== Pagina di attesa invio (annullamento entro 30s) ===== */
.pending-box {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 28px; text-align: center; max-width: 560px; margin: 10px auto;
}
.countdown {
  font-size: 64px; font-weight: 800; line-height: 1; color: var(--accent);
  font-variant-numeric: tabular-nums; margin: 6px 0 4px;
}
.pending-box.firing .countdown { color: var(--muted); }
.pending-msg { font-size: 16px; margin: 6px 0 14px; }
.pending-actions { display: flex; gap: 10px; justify-content: center; margin-top: 18px; flex-wrap: wrap; }
.btn.big { padding: 11px 20px; font-size: 16px; font-weight: 700; }
.btn.danger { color: var(--err-ink); border-color: var(--err-ink); }
.btn.danger:hover { background: var(--err-bg); }
.pending-result { font-weight: 700; margin-top: 16px; padding: 10px 14px; border-radius: 8px; }
.pending-result.ok { background: var(--ok-bg); color: var(--ok-ink); }
.pending-result.err { background: var(--err-bg); color: var(--err-ink); }
.badge.cancel { background: color-mix(in srgb, var(--line) 40%, var(--panel)); color: var(--muted); }
