:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --ok: #15803d;
  --ok-bg: #dcfce7;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --err: #b91c1c;
  --err-bg: #fee2e2;
  --info: #6d28d9;
  --info-bg: #ede9fe;
  --row-attiva: #f3fcf6;
  --row-in_scadenza: #fffaeb;
  --row-scaduta: #fef4f4;
  --row-test: #f7f5ff;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --surface: #111a2e;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --border: #1e293b;
    --primary: #3b82f6;
    --ok: #4ade80;
    --ok-bg: #052e16;
    --warn: #fbbf24;
    --warn-bg: #3a2604;
    --err: #f87171;
    --err-bg: #3b0d0d;
    --info: #c4b5fd;
    --info-bg: #2e1065;
    --row-attiva: #0f2119;
    --row-in_scadenza: #251d0c;
    --row-scaduta: #2a1216;
    --row-test: #1c1533;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--primary); }
h1 { font-size: 1.5rem; margin: 0 0 .75rem; }
h2 { font-size: 1.2rem; margin: 1.5rem 0 .75rem; }
.muted { color: var(--muted); }
.mt { margin-top: 1rem; }
.inline { display: inline; }

.container { max-width: 1100px; margin: 0 auto; padding: 16px; padding-bottom: calc(16px + env(safe-area-inset-bottom)); }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10; flex-wrap: wrap;
}
.brand { font-weight: 700; text-decoration: none; color: var(--text); }
.nav { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.nav a { text-decoration: none; color: var(--muted); }
.nav a.active { color: var(--text); font-weight: 600; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
}
.auth-box { max-width: 380px; margin: 8vh auto 0; }

label { display: block; margin-bottom: 12px; font-size: .92rem; color: var(--muted); }
input, select, textarea {
  display: block; width: 100%; margin-top: 4px;
  padding: 10px 12px; font-size: 16px; /* 16px evita lo zoom automatico su iOS */
  color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 1px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 14px; font-size: .95rem; font-weight: 600;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); cursor: pointer; text-decoration: none;
}
.btn:disabled { opacity: .6; cursor: wait; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn-danger { color: var(--err); border-color: var(--err); }
.btn-sm { min-height: 32px; padding: 4px 10px; font-size: .85rem; }
.btn-block { width: 100%; }
button.link { background: none; border: 0; padding: 0; color: var(--muted); cursor: pointer; font: inherit; }

.flashes { margin-bottom: 12px; }
.flash { padding: 10px 12px; border-radius: 8px; margin-bottom: 8px; }
.flash-success { background: var(--ok-bg); color: var(--ok); }
.flash-error { background: var(--err-bg); color: var(--err); }
.flash-warning, .flash-info { background: var(--warn-bg); color: var(--warn); }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%); max-width: calc(100% - 32px);
  padding: 10px 16px; border-radius: 8px; background: var(--text); color: var(--bg);
  box-shadow: 0 6px 24px rgba(0,0,0,.25); z-index: 100;
}
.toast.error { background: var(--err); color: #fff; }

.small { font-size: .82rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

/* --- dashboard --- */
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.page-head h1 { margin: 0; }

.stats { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; margin-bottom: 14px; }
.stat {
  display: flex; flex-direction: column; padding: 12px 14px; text-decoration: none; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  border-left-width: 4px;
}
.stat-n { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-l { font-size: .85rem; color: var(--muted); }
.stat-attiva { border-left-color: var(--ok); }
.stat-in_scadenza { border-left-color: var(--warn); }
.stat-scaduta { border-left-color: var(--err); }
.stat-test { border-left-color: var(--info); }
.stat.on { outline: 2px solid var(--primary); outline-offset: -1px; }

.filters { display: flex; gap: 8px; margin-bottom: 14px; }
.filters select, .filters input { margin: 0; }
.filters select { width: auto; flex: 0 0 auto; }
.filters input { flex: 1 1 auto; }

.table-card { padding: 0; overflow: hidden; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 10px 12px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
.table th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
.table tbody tr:last-child td { border-bottom: 0; }
/* righe colorate per stato: barra laterale + sfondo tenue */
.riga td:first-child { border-left: 4px solid transparent; }
.riga-attiva { background: var(--row-attiva); }
.riga-attiva td:first-child { border-left-color: var(--ok); }
.riga-in_scadenza { background: var(--row-in_scadenza); }
.riga-in_scadenza td:first-child { border-left-color: var(--warn); }
.riga-scaduta { background: var(--row-scaduta); }
.riga-scaduta td:first-child { border-left-color: var(--err); }
.riga-test { background: var(--row-test); }
.riga-test td:first-child { border-left-color: var(--info); }
.riga-in_scadenza .rel { color: var(--warn); font-weight: 600; }
.riga-scaduta .rel { color: var(--err); font-weight: 600; }
.c-scad .data, .c-scad .rel { display: block; }
.c-scad .rel { color: var(--muted); }
.c-note { max-width: 240px; white-space: pre-wrap; word-break: break-word; color: var(--muted); font-size: .9rem; }
.actions { white-space: nowrap; }
.actions .btn { margin: 0 2px 4px 0; }
td.c-tg:empty::after { content: "—"; color: var(--muted); }
.empty { padding: 20px; text-align: center; margin: 0; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .78rem; font-weight: 600; white-space: nowrap; }
.badge-attiva { background: var(--ok-bg); color: var(--ok); }
.badge-in_scadenza { background: var(--warn-bg); color: var(--warn); }
.badge-scaduta { background: var(--err-bg); color: var(--err); }
.badge-test { background: var(--info-bg); color: var(--info); }
.btn-test { color: var(--info); border-color: var(--info); }
.badge-tg { background: var(--bg); color: var(--muted); border: 1px solid var(--border); }
fieldset.gruppo { border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px 0; margin: 0 0 12px; }
fieldset.gruppo legend { font-size: .9rem; color: var(--muted); padding: 0 4px; }
.badge-auto { background: #e0f2fe; color: #0369a1; }
@media (prefers-color-scheme: dark) { .badge-auto { background: #082f49; color: #7dd3fc; } }
.c-stato .badge { margin: 0 0 3px 0; }
.hint { display: block; margin-top: 4px; font-size: .78rem; color: var(--muted); }

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

.dialog {
  width: min(440px, calc(100% - 24px)); padding: 18px; border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--surface); color: var(--text);
}
.dialog::backdrop { background: rgba(0,0,0,.5); }
.dialog h2 { margin-top: 0; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
label.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
label.check input { width: auto; margin: 0; }
.form-error { background: var(--err-bg); color: var(--err); padding: 8px 10px; border-radius: 8px; }

/* --- mobile: le righe diventano card compatte sotto i 640px --- */
@media (max-width: 640px) {
  .container { padding: 10px; }
  h1 { font-size: 1.25rem; }
  .topbar { flex-wrap: nowrap; padding: 8px 10px; padding-top: calc(8px + env(safe-area-inset-top)); }
  .brand { font-size: .95rem; white-space: nowrap; }
  .nav { gap: 10px; flex-wrap: nowrap; font-size: .9rem; }
  .stats { grid-template-columns: repeat(5, 1fr); gap: 6px; margin-bottom: 10px; }
  .stat { padding: 6px 4px; align-items: center; text-align: center; border-left-width: 1px; border-top-width: 4px; }
  .stat:first-child { border-top-color: var(--muted); }
  .stat-attiva { border-top-color: var(--ok); }
  .stat-in_scadenza { border-top-color: var(--warn); }
  .stat-scaduta { border-top-color: var(--err); }
  .stat-test { border-top-color: var(--info); }
  .stat-n { font-size: 1.15rem; }
  .stat-l { font-size: .68rem; line-height: 1.1; }
  .filters { gap: 6px; margin-bottom: 10px; }
  .filters select { flex: 0 0 38%; width: 38%; }
  .filters .btn { display: none; } /* invio da tastiera */

  .table-card { background: none; border: 0; overflow: visible; }
  .table.responsive thead { display: none; }
  .table.responsive, .table.responsive tbody { display: block; width: 100%; }
  .table.responsive tr.riga {
    display: grid; grid-template-columns: 1fr auto; column-gap: 8px;
    grid-template-areas: "linea stato" "linea scad" "note tg" "azioni azioni";
    border: 1px solid var(--border); border-left: 5px solid var(--border);
    border-radius: var(--radius); padding: 8px 10px; margin-bottom: 8px;
  }
  .table.responsive tr.riga-attiva { border-left-color: var(--ok); }
  .table.responsive tr.riga-in_scadenza { border-left-color: var(--warn); }
  .table.responsive tr.riga-scaduta { border-left-color: var(--err); }
  .table.responsive tr.riga-test { border-left-color: var(--info); }
  .table.responsive td { display: block; padding: 0; border: 0; }
  .riga td:first-child { border-left: 0; }
  .c-linea { grid-area: linea; min-width: 0; }
  .c-linea strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .c-stato { grid-area: stato; text-align: right; }
  .c-scad { grid-area: scad; text-align: right; font-size: .85rem; }
  .c-scad .data { display: none; }
  .c-scad .rel { font-size: .8rem; }
  .table.responsive td.c-note { grid-area: note; max-width: none; margin-top: 4px; font-size: .82rem;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .table.responsive td.c-note:empty, .table.responsive td.c-tg:empty { display: none; }
  .c-tg { grid-area: tg; margin-top: 4px; text-align: right; }
  .table.responsive td.c-azioni { grid-area: azioni; display: flex; gap: 6px; margin-top: 8px; }
  .c-azioni .btn { flex: 1 1 0; min-height: 36px; margin: 0; padding: 4px 6px; font-size: .8rem; }
}

/* --- attività --- */
.stats-attivita { margin-bottom: 4px; }
.stats-nota { margin: 0 0 14px; }
.stat-auto { border-left-color: #0284c7; }
.griglia-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 14px; }
.griglia-2 h2, .registro h2 { margin: 0 0 10px; }
.registro .page-head { margin-bottom: 6px; }
.registro select { width: auto; margin: 0; }
.lista-registro { list-style: none; padding: 0; margin: 0; }
.lista-registro li { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 8px; padding: 8px 0 8px 10px;
  border-bottom: 1px dashed var(--border); border-left: 3px solid transparent; }
.lista-registro li.esito-errore { border-left-color: var(--err); background: var(--row-scaduta); }
.lista-registro .quando { color: var(--muted); min-width: 78px; }
.lista-registro .dettaglio { flex-basis: 100%; padding-left: 86px; }
@media (max-width: 640px) {
  .griglia-2 { grid-template-columns: 1fr; }
  .stat-auto { border-top-color: #0284c7; }
  .lista-registro .dettaglio { padding-left: 0; }
}

@media (max-width: 480px) {
  /* il titolo della pagina dice già dove sei: spazio alla navigazione */
  .brand { display: none; }
  .topbar { justify-content: center; }
  .nav { width: 100%; justify-content: space-between; }
}

/* --- impostazioni --- */
.settings-section { max-width: 560px; margin-bottom: 16px; }
.settings-section h2 { margin-top: 0; }
.settings-section.wide { max-width: 760px; }
.settings-section h3 { font-size: 1rem; margin: 16px 0 8px; }
.lista-rinnovi { list-style: none; padding: 0; margin: 0; }
.lista-rinnovi li { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; padding: 8px 0; border-bottom: 1px dashed var(--border); }
.lista-rinnovi li:last-child { border-bottom: 0; }
.status-line { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* --- pannello reseller --- */
.head-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.credits { padding: 6px 10px; border-radius: 8px; background: var(--surface); border: 1px solid var(--border); font-size: .9rem; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 0 0 12px; }
.kv dt { color: var(--muted); font-size: .85rem; }
.kv dd { margin: 0; word-break: break-all; }
.m3u-box { resize: none; }
label[hidden] { display: none; }
@media (max-width: 640px) {
  .page-head { flex-wrap: wrap; }
  .head-actions { width: 100%; justify-content: stretch; }
  .head-actions .btn { flex: 1 1 auto; }
}
details.bouquets { border: 1px solid var(--border); border-radius: 8px; margin: 0 0 12px; }
details.bouquets summary { padding: 12px; cursor: pointer; font-weight: 600; min-height: 44px; list-style-position: inside; }
details.bouquets[open] summary { border-bottom: 1px solid var(--border); }
details.bouquets > p { margin: 8px 12px; }
.bouquet-list { max-height: 45vh; overflow-y: auto; padding: 4px 12px 8px; }
.bouquet-list label.check { padding: 8px 0; margin: 0; border-bottom: 1px dashed var(--border); }
.bouquet-list label.check:last-child { border-bottom: 0; }
.bouquet-list input { width: 20px; height: 20px; flex: 0 0 auto; }
button.link { color: var(--primary); }
[hidden] { display: none !important; }
