:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1c2330;
  --muted: #667085;
  --line: #e3e6eb;
  --primary: #2f5bd3;
  --primary-ink: #ffffff;
  --ok: #157f3b;
  --ok-bg: #e7f6ec;
  --err: #b42318;
  --err-bg: #fdecea;
  --warn: #9a6700;
  --warn-bg: #fff4d6;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b;
    --surface: #1b2029;
    --text: #e6e9ef;
    --muted: #98a2b3;
    --line: #2c333f;
    --primary: #6b8cff;
    --primary-ink: #0d1117;
    --ok: #4ade80;
    --ok-bg: #12301f;
    --err: #ff8a80;
    --err-bg: #3a1916;
    --warn: #f5c451;
    --warn-bg: #3a2f12;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.45rem; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 1.1rem; margin: 0 0 .5rem; }
h3 { font-size: 1rem; margin: 1.2rem 0 .4rem; }
code, pre, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .9em; }
pre { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: .7rem .8rem; overflow-x: auto; }

.wrap { max-width: 1180px; margin: 0 auto; padding: 0 16px; }
.main { padding-top: 24px; padding-bottom: 48px; }

.topbar { background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.topbar-inner { display: flex; align-items: center; gap: 20px; min-height: 56px; flex-wrap: wrap; }
.brand { font-weight: 700; color: var(--text); letter-spacing: -0.01em; }
.nav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.nav a { color: var(--muted); padding: 6px 10px; border-radius: 8px; }
.nav a:hover { background: var(--bg); text-decoration: none; color: var(--text); }
.nav a.on { color: var(--text); background: var(--bg); font-weight: 600; }
.logout { margin: 0; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 18px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head form { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.empty { text-align: center; color: var(--muted); padding: 36px 18px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
  transition: background-color .15s ease, border-color .15s ease, transform .05s ease;
}
.btn:hover { text-decoration: none; border-color: var(--muted); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .6; cursor: progress; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { filter: brightness(1.06); border-color: var(--primary); }
.btn-ghost { background: transparent; }
.btn-danger { color: var(--err); }
.btn-danger:hover { border-color: var(--err); }
.btn-sm { min-height: 32px; padding: 0 10px; font-size: .85rem; }

.alert { border-radius: 8px; padding: 10px 14px; margin-bottom: 16px; border: 1px solid transparent; }
.alert ul { margin: 0; padding-left: 18px; }
.alert-ok { background: var(--ok-bg); color: var(--ok); }
.alert-err { background: var(--err-bg); color: var(--err); overflow-wrap: anywhere; }

.form { display: grid; gap: 14px; }
.form label, .filters label { display: grid; gap: 5px; font-weight: 600; font-size: .9rem; }
input, select, textarea {
  width: 100%; min-height: 38px; padding: 7px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 400;
}
textarea { resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .85rem; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 1px; border-color: var(--primary); }
.hint { font-weight: 400; font-size: .8rem; color: var(--muted); }
.cols { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.span2 { grid-column: span 2; }
fieldset { border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px 14px; margin: 0; }
legend { font-weight: 700; padding: 0 6px; font-size: .9rem; }
.form-actions { display: flex; gap: 10px; }
.side-actions { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.side-actions form { margin: 0; }
.side-actions .alert { flex-basis: 100%; margin: 0; }
.wrap-any { overflow-wrap: anywhere; }

.filters { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; }
.filters label { flex: 1 1 150px; }
.filters .grow { flex: 3 1 220px; }
.filter-actions { display: flex; gap: 8px; }

.table-wrap { padding: 0; overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; background: var(--surface); }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: var(--bg); }
.row-off td { opacity: .6; }
.actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.actions form { margin: 0; }
.msg { min-width: 260px; max-width: 460px; }
.msg summary { cursor: pointer; font-weight: 600; overflow-wrap: anywhere; }
.body { white-space: pre-wrap; overflow-wrap: anywhere; max-height: 320px; overflow-y: auto; margin: 8px 0 0; font-size: .82rem; }
.code { background: var(--warn-bg); color: var(--warn); padding: 2px 6px; border-radius: 6px; font-weight: 700; letter-spacing: .03em; }

.tag { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .78rem; font-weight: 600; background: var(--bg); border: 1px solid var(--line); white-space: nowrap; }
.tag-inbox { background: var(--ok-bg); color: var(--ok); border-color: transparent; }
.tag-spam { background: var(--err-bg); color: var(--err); border-color: transparent; }
.tag-promozioni, .tag-social, .tag-aggiornamenti, .tag-altra { background: var(--warn-bg); color: var(--warn); border-color: transparent; }
.tag-off { color: var(--muted); }

.auth { display: inline-block; font-size: .72rem; font-weight: 700; padding: 1px 6px; border-radius: 5px; margin: 0 2px 3px 0; }
.auth-ok { background: var(--ok-bg); color: var(--ok); }
.auth-ko { background: var(--err-bg); color: var(--err); }
.auth-na { background: var(--bg); color: var(--muted); }

.pager { display: flex; gap: 12px; align-items: center; justify-content: center; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.status-card { margin: 0; }
.status-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.status-head h2 { margin: 0; font-size: 1rem; flex: 1; overflow-wrap: anywhere; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; background: var(--muted); }
.dot-ok { background: var(--ok); }
.dot-err { background: var(--err); }
.dot-off { background: var(--line); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0 0 8px; font-size: .9rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

.login-box { max-width: 380px; margin: 8vh auto 0; padding: 28px; }
.login-box h1 { margin-bottom: 4px; }
.key-box { border-color: var(--primary); }
.key-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.key { background: var(--bg); padding: 8px 10px; border-radius: 8px; overflow-wrap: anywhere; flex: 1; min-width: 0; }
.inline-forms { display: flex; gap: 10px; flex-wrap: wrap; }
.inline-forms form { margin: 0; }
.doc pre { white-space: pre-wrap; overflow-wrap: anywhere; }

@media (max-width: 720px) {
  .cols { grid-template-columns: 1fr; }
  .span2 { grid-column: auto; }
  .topbar-inner { gap: 8px; padding-top: 8px; padding-bottom: 8px; }
  .nav { order: 3; flex-basis: 100%; }
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table tr { border-bottom: 1px solid var(--line); padding: 8px 0; }
  .table td { border: 0; padding: 4px 14px; }
  .table td[data-label]::before { content: attr(data-label); display: block; font-size: .72rem; text-transform: uppercase; color: var(--muted); letter-spacing: .04em; }
  .msg { max-width: none; min-width: 0; }
  .actions { justify-content: flex-start; }
  .nowrap { white-space: normal; }
}
