:root {
  color-scheme: light dark;
  --bg: #f4f4f1;
  --card: #ffffff;
  --text: #191919;
  --muted: #666666;
  --line: #e3e3de;
  --accent: #1f6feb;
  --accent-ink: #ffffff;
  --ok: #0a7d24;
  --warn: #9a6400;
  --bad: #c62828;
  --series-1: #257de0;
  --series-2: #f47230;
  --grid: #e8e8e4;
  --radius: 14px;
  --tab-h: 64px;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111111;
    --card: #1c1c1c;
    --text: #efefef;
    --muted: #9c9c9c;
    --line: #2e2e2e;
    --accent: #4c8ff5;
    --ok: #3fbf5a;
    --warn: #e0a43a;
    --bad: #ef6b6b;
    --series-1: #338ced;
    --series-2: #e76624;
    --grid: #2a2a2a;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font-size: 16px; line-height: 1.45; -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }

.app { max-width: 680px; margin: 0 auto; padding: calc(env(safe-area-inset-top) + 12px) 16px calc(var(--tab-h) + env(safe-area-inset-bottom) + 24px); }
.hidden { display: none !important; }

header.top { display: flex; align-items: baseline; justify-content: space-between; margin: 4px 2px 14px; }
header.top h1 { font-size: 28px; margin: 0; letter-spacing: -0.02em; }
header.top .sub { color: var(--muted); font-size: 14px; }

.banner { background: #fff4d6; color: #5a4100; border-radius: 10px; padding: 10px 12px; font-size: 14px; margin-bottom: 12px; }
@media (prefers-color-scheme: dark) { .banner { background: #3a2f12; color: #f3d98f; } }

.card { background: var(--card); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; border: 1px solid var(--line); }
.card h2 { font-size: 18px; margin: 0 0 6px; }
.card h3 { font-size: 16px; margin: 0 0 4px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spread { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

.stat { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.stat .k { font-size: 12px; color: var(--muted); }
.stat .v { font-size: 22px; font-weight: 650; letter-spacing: -0.01em; }
.stat .u { font-size: 13px; color: var(--muted); font-weight: 400; }

.pill { display: inline-block; white-space: nowrap; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--bg); border: 1px solid var(--line); color: var(--muted); }
.pill.ok { color: var(--ok); border-color: currentColor; }
.pill.warn { color: var(--warn); border-color: currentColor; }
.pill.bad { color: var(--bad); border-color: currentColor; }

.btn { appearance: none; border: 1px solid var(--line); background: var(--card); border-radius: 12px; padding: 10px 14px; font-weight: 600; cursor: pointer; min-height: 44px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--bad); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
.linkbtn { background: none; border: 0; color: var(--accent); padding: 0; cursor: pointer; font-weight: 600; min-height: 32px; }

input[type=text], input[type=email], input[type=number], input[type=date], select, textarea {
  border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 9px 10px; min-height: 42px; width: 100%;
}
textarea { min-height: 70px; }
label.field { display: block; font-size: 13px; color: var(--muted); margin-bottom: 8px; }
label.field > * { margin-top: 4px; }

/* Esercizi */
.ex .name { font-weight: 650; }
.ex .presc { margin: 2px 0 8px; }
.ex details { margin: 6px 0 10px; }
.ex summary { cursor: pointer; color: var(--accent); font-weight: 600; font-size: 14px; }
.ex ul { margin: 6px 0; padding-left: 20px; }
.ex li { margin: 2px 0; }
.set { display: grid; grid-template-columns: 34px 1fr 1fr 1fr 48px; gap: 6px; align-items: end; padding: 8px 0; border-top: 1px solid var(--line); }
.set .n { font-weight: 700; color: var(--muted); align-self: center; text-align: center; }
.set label { font-size: 11px; color: var(--muted); }
.set input, .set select { min-height: 40px; padding: 6px; text-align: center; }
.set .done { min-height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); font-size: 20px; cursor: pointer; }
.set .done.on { background: var(--ok); border-color: var(--ok); color: #fff; }
.set .pain { grid-column: 2 / -1; font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.flag { margin-top: 8px; font-size: 14px; padding: 8px 10px; border-radius: 10px; background: var(--bg); }
.flag.bad { color: var(--bad); }

/* Tag */
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.tag { border: 1px solid var(--line); background: var(--bg); border-radius: 999px; padding: 4px 10px; font-size: 13px; cursor: pointer; min-height: 32px; }
.tag.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* Liste */
.list { list-style: none; padding: 0; margin: 0; }
.list li { padding: 10px 0; border-top: 1px solid var(--line); }
.list li:first-child { border-top: 0; }

/* Tab bar */
nav.tabs { position: fixed; left: 0; right: 0; bottom: 0; height: calc(var(--tab-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--card) 92%, transparent); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line); display: flex; justify-content: center; z-index: 10; }
nav.tabs a { flex: 1; max-width: 170px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; text-decoration: none; color: var(--muted); font-size: 11px; font-weight: 600; }
nav.tabs a svg { width: 24px; height: 24px; }
nav.tabs a.active { color: var(--accent); }

/* Grafici */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .axis text { font-size: 11px; fill: var(--muted); }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .tip { position: absolute; pointer-events: none; background: var(--text); color: var(--bg); font-size: 12px; padding: 6px 8px; border-radius: 8px; white-space: nowrap; transform: translate(-50%, -110%); }
.legend { display: flex; gap: 14px; font-size: 13px; color: var(--muted); margin: 4px 0 6px; flex-wrap: wrap; }
.legend .sw { display: inline-block; width: 12px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 6px; }
.legend .sw.s1 { background: var(--series-1); }
.legend .sw.s2 { background: var(--series-2); }
.legend .sw.dot { width: 8px; height: 8px; border-radius: 50%; }
.empty { color: var(--muted); font-size: 14px; padding: 12px 0; }
table.t { width: 100%; border-collapse: collapse; font-size: 14px; }
table.t th, table.t td { text-align: left; padding: 6px 4px; border-top: 1px solid var(--line); }
table.t th { color: var(--muted); font-weight: 600; font-size: 12px; }
table.t td.num, table.t th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }

/* Login */
.login { max-width: 420px; margin: 12vh auto 0; text-align: center; }
.login h1 { font-size: 30px; margin-bottom: 4px; }
.login .card { text-align: left; }

.toast { position: fixed; left: 50%; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 16px); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 12px; font-size: 14px; z-index: 20; max-width: 90vw; }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; word-break: break-all; }
.secret { background: var(--bg); border: 1px dashed var(--line); padding: 10px; border-radius: 10px; }
