:root {
  --bg: #f5f2ec;
  --surface: #ffffff;
  --surface-2: #faf8f4;
  --ink: #1f2328;
  --ink-2: #5b616b;
  --ink-3: #8b9099;
  --line: #e7e2d9;
  --accent: #3b4cca;
  --accent-ink: #ffffff;
  --done: #2f9e6e;
  --u1: #c99a06;
  --u2: #e26d2c;
  --u3: #d23c3c;
  --soon: #7b52c7;
  --week: #1d8a8f;
  --shadow: 0 1px 2px rgba(31, 35, 40, .05), 0 4px 14px rgba(31, 35, 40, .05);
  --radius: 14px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --display: "Fraunces", Georgia, "Times New Roman", serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121418;
    --surface: #1b1e24;
    --surface-2: #20242b;
    --ink: #eceef2;
    --ink-2: #a5abb6;
    --ink-3: #737a86;
    --line: #2b3038;
    --accent: #8c9bff;
    --accent-ink: #11131a;
    --done: #4cc38a;
    --u1: #e0b52e;
    --u2: #f08a4b;
    --u3: #f06363;
    --soon: #b79cf5;
    --week: #55c2c6;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 18px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
  .chip.area, .filters button[aria-pressed="true"] { color: color-mix(in srgb, var(--c) 70%, white); }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
/* Mai scorrere di lato. clip e non hidden: hidden sul body rompe lo sticky di .quick. */
html, body { overflow-x: clip; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

.app {
  max-width: 720px;
  margin: 0 auto;
  padding: max(20px, env(safe-area-inset-top)) 16px 80px;
}

/* ---------- Testata ---------- */
.hero {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 0 20px;
}
.greet { margin: 0; color: var(--ink-2); font-size: 14px; }
.hero h1 {
  margin: 2px 0 6px;
  font: 600 clamp(28px, 7vw, 40px)/1.05 var(--display);
  letter-spacing: -.01em;
}
.hero h1::first-letter { text-transform: uppercase; }
.load { margin: 0; font-size: 14px; color: var(--ink-2); display: flex; align-items: center; gap: 8px; }
.load .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--done); flex: none; }
.load.l2 .dot { background: var(--u1); }
.load.l3 .dot { background: var(--u2); }
.load.l4 .dot { background: var(--u3); }

.ring { position: relative; width: 76px; height: 76px; flex: none; }
.ring svg { transform: rotate(-90deg); }
.ring .track { stroke: var(--line); }
.ring .bar { stroke: var(--done); transition: stroke-dashoffset .6s cubic-bezier(.2, .8, .2, 1); }
.ring .num {
  position: absolute; inset: 0;
  display: grid; place-content: center; text-align: center;
  font: 600 18px/1 var(--display);
}
.ring .num small { display: block; font: 500 10px/1.4 var(--font); color: var(--ink-3); letter-spacing: .04em; text-transform: uppercase; }

/* ---------- Inserimento rapido ---------- */
.quick {
  position: sticky; top: 8px; z-index: 5;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 6px 6px 6px 14px;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  column-gap: 8px;
}
.quick:focus-within { border-color: color-mix(in srgb, var(--accent) 55%, var(--line)); }
.quick input {
  border: 0; outline: 0; background: transparent;
  padding: 10px 0; min-width: 0; font-size: 16px;
}
.quick input::placeholder { color: var(--ink-3); }
.add {
  width: 40px; height: 40px; border: 0; border-radius: 10px;
  background: var(--accent); color: var(--accent-ink);
  font-size: 24px; line-height: 1;
  display: grid; place-content: center;
}
.add:active { transform: scale(.96); }
.preview { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 6px; }
.preview:not(:empty) { padding: 2px 0 6px; }

.syntax { margin: 10px 2px 0; color: var(--ink-2); font-size: 13px; }
.syntax summary { cursor: pointer; width: max-content; list-style: none; color: var(--ink-3); }
.syntax summary::-webkit-details-marker { display: none; }
.syntax summary::before { content: "?"; display: inline-grid; place-content: center; width: 16px; height: 16px; margin-right: 6px; border-radius: 50%; border: 1px solid var(--ink-3); font-size: 10px; font-weight: 600; }
.syntax dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 10px 0 0; padding: 12px 14px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 12px; }
.syntax dt { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--ink); white-space: nowrap; }
.syntax dd { margin: 0; }

/* ---------- Banner esempi ---------- */
.banner {
  margin: 14px 0 0; padding: 10px 14px;
  border-radius: 12px; font-size: 13px; color: var(--ink-2);
  background: color-mix(in srgb, var(--accent) 8%, transparent);
  display: flex; gap: 10px; align-items: center; justify-content: space-between; flex-wrap: wrap;
}
.link { background: none; border: 0; padding: 0; color: var(--accent); font-weight: 500; }

/* ---------- Tab e filtri ---------- */
.tabs {
  display: flex; gap: 4px; margin: 22px 0 10px;
  padding: 4px; border-radius: 12px;
  background: color-mix(in srgb, var(--ink) 6%, transparent);
}
.tabs button {
  flex: 1; border: 0; background: none; padding: 8px 6px; border-radius: 9px;
  color: var(--ink-2); font-weight: 500; font-size: 14px;
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.tabs .count { color: var(--ink-3); font-weight: 400; margin-left: 4px; }

.filters { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0 8px; scrollbar-width: none; }
.filters::-webkit-scrollbar { display: none; }
.filters button {
  flex: none; border: 1px solid var(--line); background: var(--surface);
  border-radius: 999px; padding: 4px 11px; font-size: 13px; color: var(--ink-2);
}
.filters button[aria-pressed="true"] { border-color: var(--c, var(--ink)); color: var(--ink); background: color-mix(in srgb, var(--c, var(--ink)) 12%, var(--surface)); }

/* ---------- Sezioni ---------- */
.section { margin: 18px 0 0; }
.section > h2, .section > summary {
  display: flex; align-items: baseline; gap: 8px;
  margin: 0 2px 8px; font: 600 13px/1.2 var(--font);
  letter-spacing: .06em; text-transform: uppercase; color: var(--ink-3);
}
.section > summary { cursor: pointer; list-style: none; }
.section > summary::-webkit-details-marker { display: none; }
.section > summary::after { content: "›"; transition: transform .2s; font-size: 16px; line-height: .8; }
.section[open] > summary::after { transform: rotate(90deg); }
.section h2 .n, .section summary .n { font-weight: 500; letter-spacing: 0; }
.section.overdue > h2 { color: var(--u3); }

.tasks { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }

/* ---------- Card impegno ---------- */
.task {
  position: relative;
  display: grid; grid-template-columns: auto 1fr auto; gap: 12px; align-items: start;
  padding: 12px 10px 12px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: opacity .35s, transform .35s;
}
.task::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px;
  background: transparent;
}
.task.u1::before { background: var(--u1); }
.task.u2::before { background: var(--u2); }
.task.u3::before { background: var(--u3); }
.task.new { animation: pop .5s cubic-bezier(.2, .8, .2, 1); }
@keyframes pop { from { transform: translateY(-6px) scale(.98); opacity: 0; } }

.check {
  width: 24px; height: 24px; margin-top: 1px;
  border-radius: 50%; border: 2px solid var(--ink-3);
  background: transparent; padding: 0;
  display: grid; place-content: center;
  transition: background .2s, border-color .2s;
}
.task.u2 .check { border-color: var(--u2); }
.task.u3 .check { border-color: var(--u3); }
.check svg { width: 14px; height: 14px; opacity: 0; transition: opacity .15s; }
.check:hover svg { opacity: .35; }
.task.is-done .check, .task.completing .check { background: var(--done); border-color: var(--done); color: #fff; }
.task.is-done .check svg, .task.completing .check svg { opacity: 1; }
.task.completing { opacity: .45; transform: scale(.985); }
.task.completing .title, .task.is-done .title { text-decoration: line-through; text-decoration-color: var(--ink-3); color: var(--ink-2); }

.body { min-width: 0; cursor: pointer; }
.title { font-weight: 500; overflow-wrap: anywhere; }
.notes { margin-top: 2px; font-size: 13px; color: var(--ink-2); white-space: pre-line; }
.meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; align-items: center; }
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 12px; line-height: 1; padding: 4px 8px; border-radius: 999px;
  color: var(--ink-2);
  background: color-mix(in srgb, var(--ink) 6%, transparent);
  white-space: nowrap;
}
.chip.area { color: var(--c); background: color-mix(in srgb, var(--c) 13%, transparent); font-weight: 500; }
/* Persona: bordo colorato, per non confonderla con un ambito (fondo pieno). */
.chip.person { gap: 0; color: var(--c); background: none; box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--c) 45%, transparent); font-weight: 500; }
.chip.person::before { content: "@"; opacity: .6; }
.chip.when.today { color: var(--accent); background: color-mix(in srgb, var(--accent) 13%, transparent); font-weight: 600; }
.chip.when.tomorrow { color: var(--soon); background: color-mix(in srgb, var(--soon) 13%, transparent); font-weight: 500; }
.chip.when.week { color: var(--week); background: color-mix(in srgb, var(--week) 12%, transparent); }
.chip.when.late { color: var(--u3); background: color-mix(in srgb, var(--u3) 12%, transparent); font-weight: 500; }
.chip.urg { font-weight: 600; }
.chip.urg.u1 { color: var(--u1); background: color-mix(in srgb, var(--u1) 14%, transparent); }
.chip.urg.u2 { color: var(--u2); background: color-mix(in srgb, var(--u2) 14%, transparent); }
.chip.urg.u3 { color: var(--u3); background: color-mix(in srgb, var(--u3) 14%, transparent); }
/* Qui e non in cima al file: dopo le regole sopra, altrimenti a parità di peso vincono loro. */
@media (prefers-color-scheme: dark) {
  .chip.area, .chip.person { color: color-mix(in srgb, var(--c) 60%, white); }
}
.chip.stale { color: var(--u1); background: transparent; border: 1px dashed color-mix(in srgb, var(--u1) 55%, transparent); }
.stamps { margin-top: 6px; font-size: 11px; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.stamps b { font-weight: 500; color: var(--ink-2); }

.more {
  border: 0; background: none; color: var(--ink-3);
  width: 32px; height: 32px; border-radius: 8px; margin: -4px -2px 0 0;
  font-size: 18px; line-height: 1;
}
.more:hover { background: color-mix(in srgb, var(--ink) 6%, transparent); color: var(--ink); }

.empty {
  text-align: center; color: var(--ink-3); padding: 40px 16px;
  border: 1px dashed var(--line); border-radius: var(--radius); margin-top: 18px;
}
.empty strong { display: block; color: var(--ink-2); font: 600 20px/1.3 var(--display); margin-bottom: 4px; }

/* ---------- Footer ---------- */
.foot { margin-top: 40px; display: flex; flex-wrap: wrap; gap: 8px 18px; justify-content: center; font-size: 13px; }
.foot .link { color: var(--ink-3); font-weight: 400; }
.foot .link:hover { color: var(--ink); }
.sync-status { flex-basis: 100%; margin: 0 0 4px; text-align: center; color: var(--ink-3); font-size: 12px; }
.sync-status:empty { display: none; }
.sync-status .link { font-size: 12px; }
.sync-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--done); margin-right: 6px; vertical-align: 1px; }
.sync-dot.busy { background: var(--accent); animation: blink 1s infinite alternate; }
.sync-dot.off { background: var(--u1); }
@keyframes blink { to { opacity: .3; } }

/* ---------- Accesso ---------- */
.login .ed { justify-items: start; }
.login .ed > label, .login .actions { justify-self: stretch; }
.login img { border-radius: 14px; }
.login .hint { margin: -6px 0 0; color: var(--ink-2); font-size: 14px; }
.login .error { margin: -4px 0 0; color: var(--u3); font-size: 13px; min-height: 1em; }
.login .error:empty { display: none; }

/* ---------- Editor ---------- */
dialog {
  width: min(520px, calc(100vw - 24px)); max-width: calc(100vw - 24px);
  overflow-x: hidden; /* niente scroll di lato nemmeno se un campo di iOS sporge */
  border: 1px solid var(--line); border-radius: 18px;
  background: var(--surface); color: var(--ink);
  padding: 0; box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(15, 17, 22, .45); backdrop-filter: blur(2px); }
.ed { padding: 20px; display: grid; gap: 14px; }
.ed h2 { margin: 0; font: 600 22px/1.2 var(--display); }
.ed label { display: grid; gap: 4px; min-width: 0; font-size: 12px; color: var(--ink-2); font-weight: 500; }
.ed input, .ed textarea {
  width: 100%; min-width: 0; max-width: 100%; padding: 9px 11px; border-radius: 10px;
  /* 16px: sotto, iOS ingrandisce al focus e nella PWA lo zoom resta. min-width: 0 per data e ora. */
  border: 1px solid var(--line); background: var(--surface-2); font-size: 16px;
}
.ed input:focus, .ed textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 40%, transparent); outline-offset: 0; border-color: var(--accent); }
/* minmax(0, …): le colonne non si allargano per i campi data e ora di iOS. */
.ed .row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }
.ed input[type="date"], .ed input[type="time"] { -webkit-appearance: none; appearance: none; display: block; min-height: 42px; text-align: left; }
.ed input::-webkit-date-and-time-value { text-align: left; }
.seg { position: relative; border: 0; padding: 0; margin: 0; }
.seg legend { font-size: 12px; color: var(--ink-2); font-weight: 500; padding: 0; margin-bottom: 4px; }
.seg .opts { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; padding: 3px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--line); }
/* Radio nascosti: senza misure proprie ereditano width e padding da .ed input e sporgono. */
.seg input { position: absolute; width: 1px; height: 1px; padding: 0; border: 0; opacity: 0; pointer-events: none; }
.seg label { text-align: center; padding: 7px 2px; border-radius: 7px; cursor: pointer; font-size: 12.5px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.seg input:checked + label { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.seg input:checked + label.u1 { color: var(--u1); }
.seg input:checked + label.u2 { color: var(--u2); }
.seg input:checked + label.u3 { color: var(--u3); }
.seg input:focus-visible + label { outline: 2px solid var(--accent); }
.snooze { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 12px; color: var(--ink-2); }
.snooze button, .btn {
  border: 1px solid var(--line); background: var(--surface-2);
  border-radius: 999px; padding: 5px 11px; font-size: 13px;
}
.ed .stamps { margin: 0; }
.actions { display: flex; gap: 8px; align-items: center; }
.actions .spacer { flex: 1; }
.btn { border-radius: 10px; padding: 8px 14px; font-weight: 500; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--u3); background: none; border-color: transparent; padding-left: 4px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom));
  transform: translate(-50%, 140%);
  background: var(--ink); color: var(--bg);
  padding: 10px 10px 10px 16px; border-radius: 12px;
  display: flex; gap: 14px; align-items: center;
  font-size: 14px; box-shadow: 0 10px 30px rgba(0, 0, 0, .25);
  transition: transform .3s cubic-bezier(.2, .8, .2, 1), visibility 0s .3s;
  max-width: calc(100vw - 32px); z-index: 20;
  visibility: hidden;
}
.toast.show { transform: translate(-50%, 0); visibility: visible; transition-delay: 0s; }
.toast button { border: 0; background: color-mix(in srgb, var(--bg) 18%, transparent); color: var(--bg); border-radius: 8px; padding: 6px 10px; font-weight: 600; }
.toast button:empty { display: none; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}
