:root {
  --bg: #f6f4ee;
  --surface: #ffffff;
  --ink: #1b2330;
  --muted: #667085;
  --line: #ddd8cc;
  --accent: #1d3557;
  --accent-ink: #ffffff;
  --good: #1f8a4c;
  --good-bg: #e4f4ea;
  --bad: #c0392b;
  --bad-bg: #fbe9e7;
  --warn: #b7791f;
  --warn-bg: #fdf3dc;
  --radius: 10px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161d;
    --surface: #1b212b;
    --ink: #e8eaf0;
    --muted: #98a2b3;
    --line: #2f3846;
    --accent: #8ab4f8;
    --accent-ink: #0d1520;
    --good: #5fd08c;
    --good-bg: #173524;
    --bad: #f28b82;
    --bad-bg: #3d1f1c;
    --warn: #f2c66d;
    --warn-bg: #3a2e12;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}

h1 { font-size: 1.6rem; margin: 0; line-height: 1.2; }
h2 { font-size: 1.15rem; margin: 0 0 .4rem; line-height: 1.3; }
p { margin: 0 0 .6rem; }
a { color: var(--accent); }
small { font-weight: 400; color: var(--muted); }
.muted { color: var(--muted); font-size: .92rem; }
.bad { color: var(--bad); }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .7rem max(1rem, env(safe-area-inset-left));
  padding-top: calc(.7rem + env(safe-area-inset-top));
  background: var(--accent); color: var(--accent-ink);
}
.topbar a { color: inherit; text-decoration: none; }
.brand { font-weight: 700; letter-spacing: .02em; font-size: 1.1rem; }
.topbar nav { display: flex; gap: .3rem; }
.topbar nav a, .topbar nav .logout { padding: .3rem .7rem; border-radius: 99px; font-size: .95rem; opacity: .8; }
.topbar .logout { border: 0; background: none; color: inherit; font-family: inherit; cursor: pointer; }
.topbar nav a.active { background: color-mix(in srgb, var(--accent-ink) 18%, transparent); opacity: 1; }
body.exam-mode .topbar { display: none; }

main { max-width: 820px; margin: 0 auto; padding: 1.2rem 1rem 4rem; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin: .4rem 0 1rem; }
.back { display: inline-block; text-decoration: none; font-size: .92rem; margin-bottom: .2rem; }

/* ---- controlli */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .3rem;
  min-height: 42px; padding: .45rem .95rem;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); color: var(--ink);
  font: inherit; font-weight: 600; font-size: .95rem; text-decoration: none; cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); }
.btn.small { min-height: 34px; padding: .2rem .65rem; font-size: .85rem; }
.btn.big { min-height: 52px; font-size: 1.05rem; }
.btn[disabled], .btn.disabled { opacity: .45; pointer-events: none; }
.icon { border: 0; background: none; color: var(--muted); font-size: 1rem; cursor: pointer; min-width: 40px; min-height: 40px; border-radius: 8px; }
.icon:hover { color: var(--bad); }

input, textarea, select {
  width: 100%; padding: .55rem .7rem;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); color: var(--ink); font: inherit;
}
input[type=radio] { width: 22px; height: 22px; flex: none; accent-color: var(--good); padding: 0; }
input[type=file] { padding: .8rem; border-style: dashed; }
textarea { resize: vertical; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.field { display: block; margin-bottom: .9rem; }
.field > span { display: block; font-weight: 600; font-size: .92rem; margin-bottom: .3rem; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem .2rem; margin: 0 0 .9rem; display: grid; gap: 0 1rem; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); }
legend { font-weight: 700; padding: 0 .4rem; }

.toolbar { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: .9rem; }
.toolbar.center { justify-content: center; margin: 1rem 0 0; }
.inline-form { display: flex; gap: .5rem; margin-top: 1.2rem; }
.inline-form input { flex: 1; min-width: 0; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }
.panel textarea { margin-bottom: .7rem; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .9rem; }
.sticky-panel { position: sticky; top: .5rem; z-index: 2; box-shadow: 0 6px 18px rgb(0 0 0 / .08); }
.sticky-panel .toolbar { margin: .4rem 0 0; }

.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; border: 1px dashed var(--line); border-radius: var(--radius); list-style: none; }

.banner { display: block; padding: .8rem 1rem; margin-bottom: 1rem; border-radius: var(--radius); background: var(--warn-bg); color: var(--ink); border: 1px solid var(--warn); text-decoration: none; }
.banner span { float: right; font-weight: 700; color: var(--warn); }

/* ---- raccolte */
.cards { list-style: none; margin: 0; padding: 0; display: grid; gap: .7rem; }
.card { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .7rem; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1rem; }
.card-main { flex: 1 1 200px; text-decoration: none; color: inherit; }
.card-main h2, .card-main p { margin: 0; }
.card-actions { display: flex; gap: .5rem; }

/* ---- domande */
.questions { list-style: none; margin: 1rem 0 0; padding: 0; display: grid; gap: .7rem; }
.question { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1rem; border-left-width: 4px; }
.question.invalid { border-left-color: var(--bad); }
.question-head { display: flex; align-items: flex-start; justify-content: space-between; gap: .8rem; }
.question-text { font-weight: 600; margin: 0 0 .5rem; white-space: pre-wrap; overflow-wrap: anywhere; }
.question-actions { display: flex; gap: .4rem; flex: none; }
.qn { color: var(--muted); font-weight: 400; }
.answers { list-style: none; margin: 0; padding: 0; display: grid; gap: .25rem; }
.answers li { display: flex; gap: .55rem; align-items: baseline; padding: .3rem .5rem; border-radius: 6px; overflow-wrap: anywhere; }
.answers .mark { flex: none; width: 1.4rem; text-align: center; font-weight: 700; color: var(--muted); font-size: .85rem; }
.answers li.correct { background: var(--good-bg); }
.answers li.correct .mark { color: var(--good); font-size: 1rem; }
.answers li.wrong { background: var(--bad-bg); }
.answers li.wrong .mark { color: var(--bad); }
.yours { color: var(--muted); font-size: .85rem; white-space: nowrap; }
.preview .answers li { cursor: pointer; }
.preview .answers li:hover { outline: 1px dashed var(--good); }
.explanation { margin: .6rem 0 0; padding: .5rem .7rem; border-left: 3px solid var(--line); color: var(--muted); font-size: .92rem; white-space: pre-wrap; }
.problem { margin: .5rem 0 0; color: var(--bad); font-size: .9rem; font-weight: 600; }

.tag { display: inline-block; margin-left: .4rem; padding: .05rem .5rem; border-radius: 99px; font-size: .75rem; font-weight: 700; vertical-align: middle; white-space: nowrap; }
.tag.right { background: var(--good-bg); color: var(--good); }
.tag.wrong { background: var(--bad-bg); color: var(--bad); }
.tag.blank { background: var(--warn-bg); color: var(--warn); }
.tag.cat { background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent); font-weight: 600; }
.review-right { border-left-color: var(--good); }
.review-wrong { border-left-color: var(--bad); }
.review-blank { border-left-color: var(--warn); }

/* ---- editor */
dialog { width: min(680px, 100vw - 1rem); max-height: calc(100dvh - 1rem); border: 1px solid var(--line); border-radius: 14px; padding: 1.1rem; background: var(--surface); color: var(--ink); }
dialog::backdrop { background: rgb(0 0 0 / .45); }
.answer-row { display: flex; align-items: center; gap: .5rem; margin-bottom: .45rem; }
.answer-row textarea { resize: none; overflow: hidden; min-height: 42px; }
.answer-row:has(input:checked) textarea { border-color: var(--good); background: var(--good-bg); }
.form-error { color: var(--bad); font-weight: 600; }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: .5rem; }

/* ---- prova */
.exam-bar { position: sticky; top: 0; z-index: 3; display: flex; align-items: center; justify-content: space-between; gap: .8rem; margin: -1.2rem -1rem .6rem; padding: .6rem 1rem; padding-top: calc(.6rem + env(safe-area-inset-top)); background: var(--bg); border-bottom: 1px solid var(--line); }
.timer { font: 700 1.5rem/1 ui-monospace, Menlo, Consolas, monospace; font-variant-numeric: tabular-nums; }
.timer.warning { color: var(--bad); }
.qnav { display: flex; gap: .35rem; overflow-x: auto; padding: .3rem 0 .6rem; scrollbar-width: thin; }
.qnav button { flex: none; min-width: 38px; height: 38px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--muted); font: inherit; font-size: .88rem; cursor: pointer; position: relative; }
.qnav button.answered { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.qnav button.current { outline: 2px solid var(--ink); outline-offset: 1px; }
.qnav button.flagged::after { content: ""; position: absolute; top: -3px; right: -3px; width: 10px; height: 10px; border-radius: 50%; background: var(--warn); border: 2px solid var(--bg); }
.exam-question { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem; }
.exam-question h2 { font-size: 1.2rem; margin: .2rem 0 1rem; white-space: pre-wrap; overflow-wrap: anywhere; }
.options { display: grid; gap: .55rem; }
.option { display: flex; align-items: flex-start; gap: .7rem; width: 100%; min-height: 54px; padding: .75rem .85rem; text-align: left; border: 2px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--ink); font: inherit; cursor: pointer; overflow-wrap: anywhere; }
.option .mark { flex: none; width: 1.7rem; height: 1.7rem; display: grid; place-items: center; border-radius: 50%; border: 1px solid var(--line); font-weight: 700; font-size: .85rem; color: var(--muted); }
.option.selected { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--surface)); }
.option.selected .mark { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.exam-foot { display: grid; grid-template-columns: 1fr auto 1fr; gap: .5rem; margin-top: .9rem; }
.exam-foot .flagged { border-color: var(--warn); color: var(--warn); }

.qnav button.section-start { margin-left: .7rem; }

/* ---- quote per categoria */
.quota-row { display: flex; align-items: center; gap: .6rem; padding: .35rem 0; border-bottom: 1px solid var(--line); }
.quota-name { flex: 1; min-width: 0; font-weight: 600; }
.quota-name small { display: block; }
.quota-row input { width: 5.5rem; flex: none; text-align: center; }
.quota-row.short .quota-name small { color: var(--warn); font-weight: 600; }
.icon-space { width: 40px; flex: none; }
#quota-total { margin: .7rem 0 0; }
#quotas + .btn { margin-top: .6rem; }

/* ---- risultato */
.breakdown { width: 100%; max-width: 460px; margin: 1rem auto 0; border-collapse: collapse; font-size: .92rem; }
.breakdown th, .breakdown td { padding: .35rem .5rem; border-bottom: 1px solid var(--line); text-align: center; }
.breakdown th:first-child { text-align: left; }
.breakdown thead th { color: var(--muted); font-weight: 600; font-size: .8rem; }
.result { text-align: center; background: var(--surface); border: 1px solid var(--line); border-top: 6px solid var(--bad); border-radius: var(--radius); padding: 1.4rem 1rem; margin-bottom: 1.4rem; }
.result.passed { border-top-color: var(--good); }
.verdict { font-size: 1.5rem; font-weight: 800; margin: .2rem 0; color: var(--bad); }
.result.passed .verdict { color: var(--good); }
.score { font-size: 1.3rem; margin: .2rem 0 .5rem; }
.score strong { font-size: 2.6rem; line-height: 1; }
.score span { color: var(--muted); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: .5rem; max-width: 460px; margin: .9rem auto 0; }
.stats div { padding: .5rem; border-radius: 8px; font-size: .88rem; }
.stats strong { display: block; font-size: 1.4rem; }
.stats .right { background: var(--good-bg); color: var(--good); }
.stats .wrong { background: var(--bad-bg); color: var(--bad); }
.stats .blank { background: var(--warn-bg); color: var(--warn); }
.chips { display: flex; flex-wrap: wrap; gap: .4rem; }
.chips button { border: 1px solid var(--line); background: var(--surface); color: var(--ink); border-radius: 99px; padding: .35rem .85rem; font: inherit; font-size: .9rem; cursor: pointer; }
.chips button.active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* ---- storico */
.history { list-style: none; margin: 0; padding: 0; display: grid; gap: .5rem; }
.history li { display: flex; align-items: center; background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--warn); border-radius: var(--radius); }
.history li.passed { border-left-color: var(--good); }
.history li.failed { border-left-color: var(--bad); }
.history a { flex: 1; min-width: 0; display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .4rem 1rem; padding: .7rem .9rem; color: inherit; text-decoration: none; }
.h-main { display: grid; }
.h-score { display: flex; align-items: center; }

#toast { position: fixed; left: 50%; bottom: calc(1.2rem + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 10; max-width: calc(100vw - 2rem); padding: .65rem 1rem; border-radius: 8px; background: var(--ink); color: var(--bg); font-weight: 600; box-shadow: 0 6px 20px rgb(0 0 0 / .25); }
#toast.error { background: var(--bad); color: #fff; }

@media (max-width: 560px) {
  h1 { font-size: 1.35rem; }
  .question-head { flex-direction: column; gap: .2rem; }
  .question-actions { align-self: flex-end; order: 2; }
  .inline-form { flex-direction: column; }
  .card-actions { width: 100%; }
  .card-actions .btn { flex: 1; }
  .exam-foot .btn { padding: .45rem .5rem; font-size: .88rem; }
  .dialog-actions .btn { flex: 1; }
}
