:root {
  --bg: #f4f4f1;
  --card: #ffffff;
  --text: #17171a;
  --muted: #6b6b73;
  --line: #e3e3de;
  --brand: #ffc928;
  --brand-ink: #1a1400;
  --brand-soft: #fff4cc;
  --ok: #15924a;
  --ok-soft: #dff5e8;
  --warn: #b26a00;
  --warn-soft: #fff1d6;
  --bad: #c62828;
  --bad-soft: #fde4e4;
  --info: #1b62c4;
  --info-soft: #e1ecfb;
  --wa: #25d366;
  --tg: #229ed9;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 6px 20px rgba(0,0,0,.06);
  --radius: 16px;
  --nav-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111114;
    --card: #1c1c21;
    --text: #f1f1f3;
    --muted: #9b9ba5;
    --line: #2d2d34;
    --brand-soft: #3a3110;
    --ok-soft: #12321f;
    --warn-soft: #3a2a0c;
    --bad-soft: #3d1717;
    --info-soft: #132a4a;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}
a { color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h2 { font-size: 20px; }
h3 { font-size: 16px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hidden { display: none !important; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }

/* ---------- Header ---------- */
.topbar {
  position: sticky; top: 0; z-index: 900;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--brand); color: var(--brand-ink);
}
.logo {
  width: 38px; height: 38px; border-radius: 11px; background: var(--brand-ink); color: var(--brand);
  display: grid; place-items: center; font-weight: 800; font-size: 20px; flex-shrink: 0;
}
.topbar .title { font-weight: 800; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .sub { font-size: 12px; opacity: .8; display: flex; align-items: center; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); display: inline-block; flex-shrink: 0; }
.dot.off { background: var(--bad); }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; border: 0; display: grid; place-items: center;
  background: rgba(0,0,0,.1); color: inherit; cursor: pointer; text-decoration: none; flex-shrink: 0;
}
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn.wa { background: var(--wa); color: #fff; }

/* ---------- Layout ---------- */
main { padding: 16px; padding-bottom: calc(var(--nav-h) + 24px + env(safe-area-inset-bottom)); max-width: 760px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.card + .card { margin-top: 12px; }
.section-title { display: flex; align-items: center; justify-content: space-between; margin: 4px 2px 12px; }

/* ---------- Bottom nav ---------- */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 900;
  display: flex; justify-content: space-around;
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom); height: calc(var(--nav-h) + env(safe-area-inset-bottom));
}
.bottom-nav button {
  flex: 1; border: 0; background: none; color: var(--muted); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font: inherit; font-size: 11px; font-weight: 600; position: relative;
}
.bottom-nav button svg { width: 24px; height: 24px; }
.bottom-nav button.active { color: var(--text); }
.bottom-nav button.active svg { color: var(--brand-ink); background: var(--brand); border-radius: 10px; padding: 2px 10px; width: 46px; box-sizing: border-box; height: 28px; }
.badge {
  position: absolute; top: 6px; left: calc(50% + 8px); min-width: 18px; height: 18px; border-radius: 9px;
  background: var(--bad); color: #fff; font-size: 11px; display: grid; place-items: center; padding: 0 5px;
}

@media (min-width: 1000px) {
  .bottom-nav {
    top: 0; bottom: 0; right: auto; width: 96px; height: auto; flex-direction: column; justify-content: flex-start;
    padding-top: 80px; border-top: 0; border-right: 1px solid var(--line);
  }
  .bottom-nav button { flex: 0 0 76px; }
  .with-nav main { padding-left: 112px; max-width: 1080px; }
  .with-nav .topbar { padding-left: 112px; }
  main { padding-bottom: 32px; }
}

/* ---------- Form ---------- */
label.field { display: block; }
label.field > span { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin: 0 0 5px 2px; }
input, select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--bg);
  border: 1.5px solid var(--line); border-radius: 12px; padding: 11px 12px; outline: none;
}
textarea { resize: vertical; min-height: 80px; }
input:focus, select:focus, textarea:focus { border-color: var(--brand); background: var(--card); }
input[type=checkbox] { width: auto; accent-color: var(--brand-ink); }
.check { display: flex; gap: 10px; align-items: center; cursor: pointer; padding: 6px 0; }
.check input { width: 20px; height: 20px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font: inherit; font-weight: 700; border: 0; border-radius: 12px; padding: 12px 16px; cursor: pointer;
  background: var(--bg); color: var(--text); text-decoration: none; white-space: nowrap;
  border: 1.5px solid var(--line);
}
.btn svg { width: 18px; height: 18px; }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.dark { background: var(--text); color: var(--bg); border-color: var(--text); }
.btn.ok { background: var(--ok); color: #fff; border-color: var(--ok); }
.btn.bad { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.btn.wa { background: var(--wa); color: #fff; border-color: var(--wa); }
.btn.tg { background: var(--tg); color: #fff; border-color: var(--tg); }
.btn.block { width: 100%; }
.btn.big { padding: 15px 18px; font-size: 16px; }
.btn.sm { padding: 7px 11px; font-size: 13px; border-radius: 10px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn:active:not(:disabled) { transform: scale(.98); }

.seg { display: flex; background: var(--bg); border-radius: 12px; padding: 4px; gap: 4px; border: 1.5px solid var(--line); }
.seg button {
  flex: 1; border: 0; background: none; font: inherit; font-weight: 600; padding: 8px; border-radius: 9px;
  color: var(--muted); cursor: pointer;
}
.seg button.active { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

/* ---------- Pill di stato ---------- */
.pill { display: inline-flex; align-items: center; gap: 5px; padding: 3px 10px; border-radius: 99px; font-size: 12px; font-weight: 700; white-space: nowrap; }
.st-in_attesa { background: var(--warn-soft); color: var(--warn); }
.st-accettata, .st-arrivato { background: var(--ok-soft); color: var(--ok); }
.st-in_corso { background: var(--info-soft); color: var(--info); }
.st-completata { background: var(--bg); color: var(--muted); }
.st-rifiutata, .st-annullata { background: var(--bad-soft); color: var(--bad); }
.tag-offerta { background: #ffe0d1; color: #b4400b; }
.tag-avviso { background: var(--warn-soft); color: var(--warn); }
.tag-tariffe { background: var(--ok-soft); color: var(--ok); }
.tag-info { background: var(--info-soft); color: var(--info); }
@media (prefers-color-scheme: dark) { .tag-offerta { background: #3e2010; color: #ff9a66; } }

/* ---------- Toast & modale ---------- */
#toasts { position: fixed; top: max(12px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%); z-index: 3000; display: flex; flex-direction: column; gap: 8px; width: min(92vw, 420px); pointer-events: none; }
.toast { background: var(--text); color: var(--bg); padding: 12px 16px; border-radius: 14px; box-shadow: var(--shadow); font-weight: 600; animation: fade .2s; pointer-events: auto; }
.toast.err { background: var(--bad); color: #fff; }
.toast.ok { background: var(--ok); color: #fff; }

.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 2000; display: flex; align-items: flex-end; justify-content: center; }
.modal {
  background: var(--card); width: 100%; max-width: 520px; max-height: 92vh; overflow: auto;
  border-radius: 22px 22px 0 0; padding: 20px 16px calc(20px + env(safe-area-inset-bottom)); animation: up .22s ease;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (min-width: 600px) { .modal-bg { align-items: center; } .modal { border-radius: 22px; padding: 24px; } }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.close-x { border: 0; background: var(--bg); width: 34px; height: 34px; border-radius: 50%; cursor: pointer; color: var(--text); font-size: 18px; }

.empty { text-align: center; color: var(--muted); padding: 32px 16px; }
.empty .big-ico { font-size: 44px; margin-bottom: 8px; }

.spinner { width: 18px; height: 18px; border: 2.5px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
