:root {
  --bg: #12141a;
  --surface: #1a1d26;
  --surface-2: #222634;
  --line: #2c3140;
  --text: #e8eaf0;
  --muted: #949cb0;
  --accent: #5b8cff;
  --danger: #f2637e;
  --ok: #37c07a;
  --p1: #f2637e;
  --p2: #f0a13c;
  --p3: #5b8cff;
  --p4: #6d7488;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

/* Deve stare prima delle regole con display:*, altrimenti l'attributo hidden
   viene sovrascritto (la regola [hidden] del browser perde contro qualsiasi
   regola dell'autore) e schermate come .gate restano visibili. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 17px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: contain;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }

/* Quale delle due schermate esiste al primo disegno lo decide la classe
   "authed" messa dallo script inline nell'head: nessun lampeggio. */
#app { display: none; }
html.authed #app { display: block; }
html.authed #gate { display: none; }

/* ---------- gate ---------- */
.gate {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.gate-card {
  width: min(380px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 28px 24px;
  display: grid;
  gap: 14px;
}
.gate-card h1 { font-size: 28px; }
.gate-card p { margin: 0; color: var(--muted); font-size: 15px; }
.gate-card input, .gate-card button {
  width: 100%;
  padding: 14px 16px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface-2);
}
.gate-card button { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.error { color: var(--danger); font-size: 14px; margin: 0; }

/* ---------- layout ---------- */
#app {
  max-width: 720px;
  margin: 0 auto;
  /* Spazio in fondo perche' la barra fissa non copra l'ultima voce. */
  padding: calc(var(--safe-top) + 12px) 16px 250px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 12px;
}
.topbar h1 { font-size: 22px; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.badge {
  background: var(--surface-2);
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 999px;
  padding: 2px 11px;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.icon-btn {
  width: 40px; height: 40px;
  border-radius: 12px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  font-size: 18px;
}

/* ---------- barra di cattura, fissa in basso ---------- */
.dock {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 30;
  /* In colonna: la voce e' la funzione principale, quindi si prende tutta
     la larghezza in fondo, dove sta il pollice. Il testo resta sopra, defilato. */
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  /* Niente safe-area qui: su iPhone lasciava una fascia vuota alta un dito
     sotto la barra di scrittura. Un margine fisso piccolo la incolla al bordo;
     l'indicatore Home passa sopra il campo senza dare fastidio. */
  padding: 14px 14px 12px;
  background: var(--bg);
  border-top: 1px solid var(--line);
  max-width: 720px;
  margin: 0 auto;
}
.dock textarea {
  width: 100%;
  min-height: 46px;
  max-height: 26vh;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 26px;
  resize: none;
  outline: none;
  /* Mai sotto 16px: iOS ingrandisce da solo la pagina se il testo e' piu' piccolo. */
  font-size: 16.5px;
  line-height: 1.35;
  padding: 15px 18px;
  /* Il campo cresce da solo con il testo: la barra di scorrimento non serve
     e su Windows disegna pure le frecce. Lo scorrimento resta possibile. */
  scrollbar-width: none;
}
.dock textarea::-webkit-scrollbar { width: 0; height: 0; }
.dock textarea::placeholder { color: var(--muted); }

.mic-big {
  width: 124px;
  height: 124px;
  border-radius: 50%;
  flex: none;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  box-shadow: 0 10px 30px rgba(91, 140, 255, .42);
  transition: transform .12s ease, background .12s ease, box-shadow .12s ease;
  /* Il gesto e' nostro: niente scroll, niente selezione, niente menu contestuale. */
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.mic-big svg { pointer-events: none; }
.mic-big:active { transform: scale(.97); }
.mic-label {
  font-size: 13px;
  color: var(--muted);
  text-align: center;
  line-height: 1;
}
.dock.recording .mic-label { color: var(--danger); font-weight: 600; }
.dock.cancelling .mic-label { color: var(--muted); }

/* Il pulsante cambia mestiere: microfono se vuoto, invio se c'e' del testo. */
.dock .ic-send { display: none; }
.dock.has-text .ic-mic { display: none; }
.dock.has-text .ic-send { display: block; }

.dock.recording .mic-big {
  background: var(--danger);
  transform: scale(1.06);
  box-shadow: 0 12px 40px rgba(242, 99, 126, .6);
}
.dock.cancelling .mic-big {
  background: var(--muted);
  transform: scale(1);
  box-shadow: none;
}
/* Mentre si registra il campo di testo si toglie di mezzo. */
.dock.recording textarea { opacity: .35; pointer-events: none; }
.dock.sending .mic-big { opacity: .55; }

/* ---------- pannello di registrazione ---------- */
.rec-panel {
  position: fixed;
  left: 12px; right: 12px;
  bottom: 232px;
  z-index: 31;
  max-width: 696px;
  margin: 0 auto;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 14px 16px;
  box-shadow: 0 14px 44px rgba(0, 0, 0, .55);
}
.rec-head {
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: 13.5px;
  color: var(--muted);
  margin-bottom: 8px;
}
.rec-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--danger);
  flex: none;
  animation: pulse 1.1s ease-in-out infinite;
}
.rec-time { font-variant-numeric: tabular-nums; color: var(--danger); font-weight: 600; }
.rec-hint { margin-left: auto; text-align: right; }
.rec-panel.cancelling { border-color: var(--danger); }
.rec-panel.cancelling .rec-hint { color: var(--danger); font-weight: 600; }
.rec-panel.cancelling .rec-text { text-decoration: line-through; opacity: .5; }
.rec-text {
  font-size: 17px;
  line-height: 1.4;
  max-height: 30vh;
  overflow-y: auto;
  word-break: break-word;
}
@keyframes pulse { 0%,100% { opacity: 1; transform: scale(1); } 50% { opacity: .35; transform: scale(1.35); } }

.primary { background: var(--accent); color: #fff; }
.ghost { background: var(--surface-2); border: 1px solid var(--line); }
.danger { background: transparent; border: 1px solid var(--danger); color: var(--danger); }
.link { background: none; color: var(--muted); text-decoration: underline; padding: 8px; }
button:disabled { opacity: .5; cursor: default; }

.hint { color: var(--muted); font-size: 13.5px; margin: 10px 2px 0; }
.hint:empty { display: none; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }

/* ---------- chips ---------- */
.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 12px;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none;
  padding: 7px 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 14.5px;
  white-space: nowrap;
}
.chip .n { opacity: .65; margin-left: 5px; font-variant-numeric: tabular-nums; }
.chip[aria-pressed="true"] {
  color: var(--text);
  border-color: var(--dot, var(--accent));
  background: color-mix(in srgb, var(--dot, var(--accent)) 16%, var(--surface));
}

/* ---------- lista ---------- */
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 4px solid var(--pc, var(--p3));
  border-radius: var(--radius);
  padding: 12px 12px 12px 13px;
}
.item.p1 { --pc: var(--p1); }
.item.p2 { --pc: var(--p2); }
.item.p3 { --pc: var(--p3); }
.item.p4 { --pc: var(--p4); }
.item.nagging { border-color: color-mix(in srgb, var(--p2) 55%, var(--line)); }
.nag { color: var(--p2); }
.item.is-done { opacity: .5; }
.item.is-done .title { text-decoration: line-through; }

.check {
  flex: none;
  width: 26px; height: 26px;
  margin-top: 1px;
  border-radius: 50%;
  border: 2px solid var(--line);
  color: transparent;
  font-size: 14px;
  line-height: 1;
  display: grid; place-items: center;
}
.check:hover { border-color: var(--ok); }
.item.is-done .check { border-color: var(--ok); background: var(--ok); color: #10231a; }

.body { flex: 1; min-width: 0; }
.title { font-size: 16.5px; word-break: break-word; }
.meta {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  margin-top: 5px;
  font-size: 13px;
  color: var(--muted);
}
.cat { display: inline-flex; align-items: center; gap: 5px; }
.cat::before {
  content: "";
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--dot, var(--accent));
}
.due.overdue { color: var(--danger); }
.due.soon { color: var(--p2); }
.edit {
  flex: none;
  width: 32px; height: 32px;
  border-radius: 9px;
  color: var(--muted);
  font-size: 19px;
  line-height: 1;
}
.edit:hover { background: var(--surface-2); color: var(--text); }

.offline-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  background: color-mix(in srgb, var(--p2) 14%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--p2) 45%, var(--line));
  color: var(--text);
  border-radius: 12px;
  padding: 10px 13px;
  margin-bottom: 12px;
  font-size: 14.5px;
}
.offline-bar button {
  margin-left: auto;
  flex: none;
  padding: 6px 12px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-size: 14px;
}

.item.pending {
  --pc: var(--p2);
  border-style: dashed;
  opacity: .85;
}
.item.pending .clock {
  flex: none;
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  font-size: 15px;
}

.empty { text-align: center; color: var(--muted); padding: 40px 20px; }
.footer { text-align: center; margin-top: 18px; }

/* ---------- dialog ---------- */
dialog {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 18px;
  padding: 0;
  width: min(520px, calc(100vw - 24px));
  max-height: 88dvh;
}
dialog::backdrop { background: rgba(0, 0, 0, .6); }
dialog form, .settings-body {
  padding: 20px;
  display: grid;
  gap: 14px;
  overflow-y: auto;
  max-height: 88dvh;
}
dialog h2 { font-size: 20px; }
dialog h3 { font-size: 15px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
dialog label { display: grid; gap: 6px; font-size: 13.5px; color: var(--muted); }
dialog input, dialog select, dialog textarea {
  padding: 11px 13px;
  border-radius: 11px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-size: 16px;
  width: 100%;
}
dialog textarea { resize: vertical; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.dialog-actions { display: flex; align-items: center; gap: 9px; padding-top: 4px; }
.dialog-actions button { padding: 11px 18px; border-radius: 11px; font-weight: 600; }
.spacer { flex: 1; }
.settings-body section { display: grid; gap: 10px; }
.row { display: flex; gap: 9px; align-items: center; }
.row button { padding: 11px 16px; border-radius: 11px; font-weight: 600; }
.row input[type="color"] { width: 46px; padding: 3px; }

.choice-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.choice {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  width: 100%;
  text-align: left;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-left: 4px solid var(--pc, var(--p3));
  border-radius: 12px;
  padding: 12px 14px;
}
.choice:hover { border-color: var(--accent); }
.choice.p1 { --pc: var(--p1); }
.choice.p2 { --pc: var(--p2); }
.choice.p3 { --pc: var(--p3); }
.choice.p4 { --pc: var(--p4); }
.choice .t { font-size: 16px; }
.choice .m { font-size: 13px; color: var(--muted); }

.cat-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.cat-list li {
  display: flex; align-items: center; gap: 9px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 11px;
  font-size: 15px;
}
.cat-list .swatch { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.cat-list .n { color: var(--muted); font-size: 13px; }
.cat-list .del { margin-left: auto; color: var(--muted); font-size: 18px; padding: 0 6px; }
.cat-list .del:hover { color: var(--danger); }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-bottom) + 20px);
  transform: translateX(-50%);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 11px 18px;
  font-size: 15px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .45);
  z-index: 50;
  max-width: calc(100vw - 32px);
}
.toast.err { border-color: var(--danger); color: var(--danger); }

@media (max-width: 420px) {
  .grid2 { grid-template-columns: 1fr; }
}
