:root {
  --bg: #f2f2f7;
  --card: #ffffff;
  --card-2: #f7f7fa;
  --text: #1c1c1e;
  --muted: #6c6c70;
  --line: #e2e2e8;
  --accent: #0a66d8;
  --accent-text: #ffffff;
  --accent-soft: #e5efff;
  --ok: #1f8a3c;
  --ok-soft: #e3f5e8;
  --bad: #c9302c;
  --bad-soft: #fde8e7;
  --warn: #b26a00;
  --warn-soft: #fff2dc;
  --hard: #b26a00;
  --easy: #0a7d8c;
  --radius: 16px;
  --tabbar-h: 60px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1e;
    --card-2: #2a2a2d;
    --text: #f2f2f7;
    --muted: #9a9aa0;
    --line: #38383c;
    --accent: #3d8bff;
    --accent-soft: #10284d;
    --ok: #3cc463;
    --ok-soft: #12301b;
    --bad: #ff6159;
    --bad-soft: #3a1513;
    --warn: #ffb340;
    --warn-soft: #3a2a0d;
    --hard: #ffb340;
    --easy: #4fd1e0;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 17px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  min-height: 100vh;
  min-height: 100dvh;
}
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); }

#view {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(var(--safe-t) + 16px) 16px calc(var(--tabbar-h) + var(--safe-b) + 24px);
}

/* Barra in basso */
#tabbar {
  position: fixed; left: 0; right: 0; bottom: 0;
  display: flex; justify-content: space-around;
  padding-bottom: var(--safe-b);
  background: color-mix(in srgb, var(--card) 88%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--line);
  z-index: 10;
}
#tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  height: var(--tabbar-h); gap: 2px;
  color: var(--muted); text-decoration: none; font-size: 11px; font-weight: 500;
}
#tabbar svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
#tabbar a.active { color: var(--accent); }

/* Tipografia */
h1 { font-size: 32px; line-height: 1.15; margin: 8px 0 4px; letter-spacing: -0.02em; }
h2 { font-size: 21px; margin: 24px 0 10px; letter-spacing: -0.01em; }
h3 { font-size: 17px; margin: 0 0 4px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.center { text-align: center; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.icon-btn {
  width: 44px; height: 44px; border-radius: 50%; border: none;
  background: var(--card); display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer; flex: none;
}
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* Card */
.card {
  background: var(--card); border-radius: var(--radius); padding: 16px;
  margin: 12px 0;
}
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 16px 0; }
.stat { background: var(--card); border-radius: var(--radius); padding: 14px 10px; text-align: center; }
.stat b { display: block; font-size: 28px; line-height: 1.1; }
.stat span { font-size: 13px; color: var(--muted); }

/* Pulsanti */
.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; min-height: 54px; padding: 14px 18px;
  border: none; border-radius: 14px; cursor: pointer;
  background: var(--accent); color: var(--accent-text);
  font-size: 18px; font-weight: 600;
}
.btn:active { transform: scale(0.98); }
.btn[disabled] { opacity: 0.45; }
.btn.secondary { background: var(--card); color: var(--accent); }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.danger { background: var(--bad-soft); color: var(--bad); }
.btn + .btn { margin-top: 10px; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; margin: 0 !important; }

/* Lista blocchi */
.blocks { list-style: none; padding: 0; margin: 0; background: var(--card); border-radius: var(--radius); overflow: hidden; }
.blocks li { display: flex; align-items: center; gap: 14px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.blocks li:last-child { border-bottom: none; }
.blocks .ico { font-size: 24px; width: 32px; text-align: center; }
.blocks .txt { flex: 1; min-width: 0; }
.blocks .txt small { display: block; color: var(--muted); font-size: 14px; }
.blocks li.soon { opacity: 0.55; }
.check { width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--line); display: inline-flex; align-items: center; justify-content: center; font-size: 15px; flex: none; }
.check.done { background: var(--ok); border-color: var(--ok); color: #fff; }
.badge { font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 99px; background: var(--card-2); color: var(--muted); white-space: nowrap; }
.badge.nuova { background: var(--accent-soft); color: var(--accent); }
.badge.studio { background: var(--warn-soft); color: var(--warn); }
.badge.imparata { background: var(--ok-soft); color: var(--ok); }

.banner { border-radius: var(--radius); padding: 14px 16px; margin: 12px 0; background: var(--ok-soft); color: var(--ok); font-weight: 600; }
.banner.info { background: var(--accent-soft); color: var(--accent); }

/* Percorso */
.phase { margin-top: 20px; }
.phase-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.phase-head .badge { flex: none; }
.month { margin: 10px 0 14px; }
.month-title { font-size: 14px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; margin: 8px 4px; }
.units { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.unit {
  background: var(--card); border-radius: 14px; padding: 12px; border: 2px solid transparent;
  text-align: left; cursor: pointer; min-height: 92px; display: flex; flex-direction: column; gap: 4px;
}
.unit .n { font-size: 13px; color: var(--muted); font-weight: 600; display: flex; justify-content: space-between; }
.unit .t { font-size: 15px; font-weight: 600; line-height: 1.25; }
.unit.attuale { border-color: var(--accent); }
.unit.completata .n { color: var(--ok); }
.unit.bloccata { opacity: 0.55; }

/* Mazzo */
.search { width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); font-size: 17px; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 12px 0 4px; scrollbar-width: none; }
.chip { border: none; background: var(--card); padding: 9px 14px; border-radius: 99px; font-size: 15px; font-weight: 500; cursor: pointer; white-space: nowrap; }
.chip.on { background: var(--accent); color: var(--accent-text); }
.list { list-style: none; padding: 0; margin: 0; background: var(--card); border-radius: var(--radius); overflow: hidden; }
.list li { padding: 12px 14px; border-bottom: 1px solid var(--line); }
.list li:last-child { border-bottom: none; }
.list .line { display: flex; align-items: center; gap: 10px; }
.list .line .txt { flex: 1; min-width: 0; }
.list .en { font-weight: 600; }
.list .it { color: var(--muted); font-size: 15px; }
.list .ex { margin-top: 8px; font-size: 15px; }
.say { width: 40px; height: 40px; border-radius: 50%; border: none; background: var(--accent-soft); color: var(--accent); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; flex: none; }
.say svg { width: 20px; height: 20px; fill: currentColor; }
.say.big { width: 56px; height: 56px; }
.say.big svg { width: 28px; height: 28px; }

/* Progresso */
.bar { height: 12px; border-radius: 99px; background: var(--card-2); overflow: hidden; margin: 8px 0; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 99px; }

/* Overlay sessione */
#overlay {
  position: fixed; inset: 0; z-index: 20; background: var(--bg);
  overflow-y: auto; -webkit-overflow-scrolling: touch;
}
.sess { max-width: 640px; margin: 0 auto; min-height: 100%; display: flex; flex-direction: column;
  padding: calc(var(--safe-t) + 10px) 16px calc(var(--safe-b) + 20px); }
.sess-top { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.sess-top .title { flex: 1; font-weight: 600; text-align: center; }
.sess-top .icon-btn { background: var(--card); }
.sess-body { flex: 1; display: flex; flex-direction: column; }
.sess-foot { padding-top: 16px; }
.progress-dots { height: 6px; border-radius: 99px; background: var(--card-2); overflow: hidden; margin-bottom: 10px; }
.progress-dots > i { display: block; height: 100%; background: var(--accent); transition: width .25s; }

/* Flashcard */
.flash { background: var(--card); border-radius: 22px; padding: 28px 20px; text-align: center; flex: 1; display: flex; flex-direction: column; justify-content: center; gap: 14px; min-height: 300px; }
.flash .front { font-size: 34px; font-weight: 700; line-height: 1.2; word-break: break-word; }
.flash .back { border-top: 1px solid var(--line); padding-top: 16px; }
.flash .tr { font-size: 24px; font-weight: 600; }
.flash .exen { font-size: 18px; margin-top: 12px; display: flex; align-items: center; justify-content: center; gap: 10px; text-align: left; }
.flash .exit { color: var(--muted); font-size: 16px; margin-top: 4px; }
.grades { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.grade { border: none; border-radius: 14px; min-height: 64px; padding: 8px 4px; cursor: pointer; font-weight: 600; font-size: 15px; background: var(--card); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; }
.grade small { font-weight: 500; font-size: 12px; color: var(--muted); }
.grade.g1 { color: var(--bad); }
.grade.g2 { color: var(--hard); }
.grade.g3 { color: var(--ok); }
.grade.g4 { color: var(--easy); }

/* Lezione */
.lesson h2 { margin-top: 8px; }
.lesson p { margin: 0 0 12px; }
.examples { list-style: none; padding: 0; margin: 0; }
.examples li { display: flex; gap: 10px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.examples li:last-child { border-bottom: none; }
.examples .txt { flex: 1; }
.examples .it { color: var(--muted); font-size: 15px; }
table.gram { width: 100%; border-collapse: collapse; font-size: 16px; }
table.gram td, table.gram th { padding: 8px 6px; border-bottom: 1px solid var(--line); text-align: left; }
table.gram th { color: var(--muted); font-weight: 600; font-size: 14px; }
.tip { background: var(--warn-soft); color: var(--text); border-radius: 12px; padding: 12px 14px; font-size: 15px; margin: 12px 0; }

.ex-kind { font-size: 13px; font-weight: 700; color: var(--accent); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 6px; }
.ex-q { font-size: 22px; font-weight: 600; margin: 4px 0 6px; line-height: 1.3; }
.ex-hint { color: var(--muted); font-style: italic; margin-bottom: 12px; }
.answer-input { width: 100%; padding: 14px; border-radius: 14px; border: 2px solid var(--line); background: var(--card); font-size: 20px; }
.answer-input:focus { outline: none; border-color: var(--accent); }
.options { display: flex; flex-direction: column; gap: 10px; margin-top: 8px; }
.option { border: 2px solid var(--line); background: var(--card); border-radius: 14px; min-height: 56px; padding: 12px 16px; text-align: left; font-size: 18px; cursor: pointer; }
.option.right { border-color: var(--ok); background: var(--ok-soft); }
.option.wrong { border-color: var(--bad); background: var(--bad-soft); }
.tiles { display: flex; flex-wrap: wrap; gap: 8px; min-height: 60px; padding: 10px; border-radius: 14px; }
.tiles.answer { background: var(--card); border: 2px dashed var(--line); margin-bottom: 14px; }
.tile { border: none; background: var(--card); border-radius: 10px; padding: 10px 14px; font-size: 18px; cursor: pointer; box-shadow: 0 1px 0 var(--line); }
.tiles.answer .tile { background: var(--accent-soft); color: var(--accent); }
.tile.used { visibility: hidden; }
.feedback { border-radius: 14px; padding: 14px 16px; margin-top: 14px; font-size: 17px; }
.feedback.ok { background: var(--ok-soft); color: var(--ok); }
.feedback.ko { background: var(--bad-soft); color: var(--bad); }
.feedback .sol { color: var(--text); margin-top: 6px; display: flex; gap: 10px; align-items: center; }

.big-emoji { font-size: 64px; text-align: center; margin: 24px 0 8px; }

/* Impostazioni */
.setting { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.setting:last-child { border-bottom: none; }
.seg { display: inline-flex; background: var(--card-2); border-radius: 10px; padding: 3px; }
.seg button { border: none; background: transparent; padding: 8px 12px; border-radius: 8px; font-size: 15px; cursor: pointer; }
.seg button.on { background: var(--card); box-shadow: 0 1px 3px rgba(0,0,0,.15); font-weight: 600; }

/* Foglio dettagli */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 30; display: flex; align-items: flex-end; justify-content: center; }
.sheet { background: var(--bg); width: 100%; max-width: 640px; max-height: 85vh; overflow-y: auto; border-radius: 20px 20px 0 0; padding: 20px 16px calc(var(--safe-b) + 20px); }

#toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px); background: var(--text); color: var(--bg); padding: 12px 18px; border-radius: 12px; z-index: 40; font-size: 15px; max-width: 90vw; text-align: center; }
.empty { text-align: center; color: var(--muted); padding: 32px 16px; }
