:root {
  --bg: #f7f5f2;
  --surface: #ffffff;
  --surface-2: #f1eee9;
  --ink: #1c1917;
  --ink-2: #57534e;
  --ink-3: #8a837c;
  --line: #e4dfd8;
  --accent: #c2410c;
  --accent-ink: #ffffff;
  --accent-soft: #fff1e8;
  --ok: #15803d;
  --ok-soft: #e8f6ec;
  --bad: #b91c1c;
  --bad-soft: #fdecec;
  --warn: #a16207;
  --warn-soft: #fdf6e3;
  --info: #1d4ed8;
  --info-soft: #eaf0ff;
  --shadow: 0 1px 2px rgba(28,25,23,.05), 0 4px 16px rgba(28,25,23,.05);
  --radius: 14px;
  --serif: 'Source Serif 4', Georgia, serif;
  --sans: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141210; --surface: #1d1a17; --surface-2: #26221e; --ink: #f2efeb; --ink-2: #c4bdb4; --ink-3: #8f877e;
    --line: #36302a; --accent: #fb923c; --accent-ink: #1c1917; --accent-soft: #3a2415;
    --ok: #4ade80; --ok-soft: #13291b; --bad: #f87171; --bad-soft: #351717; --warn: #facc15; --warn-soft: #2f2810;
    --info: #93b4ff; --info-soft: #18213a; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141210; --surface: #1d1a17; --surface-2: #26221e; --ink: #f2efeb; --ink-2: #c4bdb4; --ink-3: #8f877e;
  --line: #36302a; --accent: #fb923c; --accent-ink: #1c1917; --accent-soft: #3a2415;
  --ok: #4ade80; --ok-soft: #13291b; --bad: #f87171; --bad-soft: #351717; --warn: #facc15; --warn-soft: #2f2810;
  --info: #93b4ff; --info-soft: #18213a; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font-family: var(--sans); font-size: 16px; line-height: 1.55; -webkit-font-smoothing: antialiased; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; letter-spacing: -.01em; }
h1 { font-size: 1.75rem; }
h2 { font-size: 1.3rem; }
h3 { font-size: 1.05rem; }
p { margin: 0 0 .8em; }
.boot { display: grid; place-items: center; min-height: 100vh; color: var(--ink-3); }

/* Layout */
.topbar { position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.topbar-in { max-width: 1180px; margin: 0 auto; display: flex; align-items: center; gap: 16px; padding: 10px 16px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; text-decoration: none; color: var(--ink); white-space: nowrap; }
.brand-mark { width: 32px; height: 32px; border-radius: 9px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-size: .85rem; font-weight: 700; }
.nav { display: flex; gap: 2px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.nav::-webkit-scrollbar { display: none; }
.nav a { text-decoration: none; color: var(--ink-2); padding: 7px 11px; border-radius: 8px; font-size: .92rem; font-weight: 500; white-space: nowrap; }
.nav a:hover { background: var(--surface-2); color: var(--ink); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.top-actions { display: flex; gap: 6px; align-items: center; }
main { max-width: 1180px; margin: 0 auto; padding: 28px 16px 80px; }

/* Components */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.stack > * + * { margin-top: 16px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.muted { color: var(--ink-2); }
.small { font-size: .875rem; }
.tiny { font-size: .78rem; color: var(--ink-3); }
.eyebrow { font-size: .75rem; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-3); font-weight: 600; margin-bottom: 6px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; border: 1px solid var(--line); background: var(--surface); padding: 9px 16px; border-radius: 10px; cursor: pointer; font-weight: 500; text-decoration: none; color: var(--ink); transition: background .15s, border-color .15s, transform .05s; }
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { padding: 6px 11px; font-size: .875rem; border-radius: 8px; }
.btn.danger { color: var(--bad); }

.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 999px; font-size: .78rem; font-weight: 600; background: var(--surface-2); color: var(--ink-2); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }

.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--surface); padding: 5px 12px; border-radius: 999px; font-size: .85rem; cursor: pointer; }
.chip.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }

.input, textarea.input, select.input { width: 100%; border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 10px 12px; outline: none; }
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
label.field { display: block; font-size: .875rem; font-weight: 600; margin-bottom: 14px; }
label.field .input { margin-top: 6px; font-weight: 400; }

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: 100%; max-width: 400px; }
.login .brand-mark { width: 52px; height: 52px; font-size: 1.2rem; border-radius: 14px; margin-bottom: 16px; }
.err { color: var(--bad); font-size: .9rem; min-height: 1.4em; }

/* Hero */
.hero { display: grid; grid-template-columns: 1.4fr 1fr; gap: 20px; align-items: stretch; margin-bottom: 24px; }
@media (max-width: 820px) { .hero { grid-template-columns: 1fr; } }
.hero h1 { font-family: var(--serif); font-size: 2.1rem; font-weight: 600; }
.stat { font-size: 1.9rem; font-weight: 700; letter-spacing: -.02em; }
.area-card { display: flex; flex-direction: column; gap: 8px; text-decoration: none; color: inherit; transition: border-color .15s, transform .15s; }
.area-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.area-card .weight { font-size: 1.6rem; font-weight: 700; color: var(--accent); }
.meter { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; position: relative; }
.meter > i { display: block; height: 100%; background: var(--accent); border-radius: 99px; }
.meter > b { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--ink-2); }

/* List items */
.item { display: flex; gap: 14px; align-items: center; padding: 14px 16px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); text-decoration: none; color: inherit; transition: border-color .15s; }
.item:hover { border-color: var(--accent); }
.item .num { width: 36px; height: 36px; border-radius: 10px; background: var(--surface-2); display: grid; place-items: center; font-weight: 700; color: var(--ink-2); flex-shrink: 0; }
.item .t { font-weight: 600; }

/* Reading */
.reading { display: grid; grid-template-columns: 1.1fr 1fr; gap: 20px; align-items: start; }
@media (max-width: 900px) { .reading { grid-template-columns: 1fr; } }
.reading .text { position: sticky; top: 76px; max-height: calc(100vh - 100px); overflow: auto; }
@media (max-width: 900px) { .reading .text { position: static; max-height: none; } }
.prose { font-family: var(--serif); font-size: 1.08rem; line-height: 1.7; }
.prose p { margin-bottom: 1em; }
.source { white-space: pre-wrap; font-family: var(--serif); background: var(--surface-2); border-radius: 10px; padding: 16px; font-size: 1rem; }

/* Questions */
.q { padding: 16px 0; border-bottom: 1px solid var(--line); }
.q:last-child { border-bottom: 0; }
.q-title { font-weight: 600; margin-bottom: 10px; display: flex; gap: 10px; }
.q-title .n { color: var(--accent); flex-shrink: 0; }
.opts { display: grid; gap: 7px; }
.opt { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; cursor: pointer; background: var(--surface); transition: border-color .12s, background .12s; }
.opt:hover { border-color: var(--ink-3); }
.opt input { margin-top: 4px; accent-color: var(--accent); }
.opt .l { font-weight: 700; color: var(--ink-3); width: 1.1em; }
.opt.sel { border-color: var(--accent); background: var(--accent-soft); }
.opt.right { border-color: var(--ok); background: var(--ok-soft); }
.opt.wrong { border-color: var(--bad); background: var(--bad-soft); }
.locked .opt { cursor: default; }
.why { margin-top: 10px; padding: 10px 12px; border-left: 3px solid var(--info); background: var(--info-soft); border-radius: 0 8px 8px 0; font-size: .92rem; }

/* Cloze */
.cloze { font-family: var(--serif); font-size: 1.08rem; line-height: 2.1; }
.cloze p { margin-bottom: 1em; }
.gap { display: inline-flex; align-items: center; gap: 4px; vertical-align: baseline; }
.gap .gn { font-family: var(--sans); font-size: .7rem; font-weight: 700; background: var(--ink); color: var(--bg); border-radius: 5px; padding: 0 5px; line-height: 1.5; }
.gap select { font-family: var(--sans); font-size: .9rem; border: 1px solid var(--line); background: var(--surface); border-radius: 7px; padding: 2px 6px; max-width: 200px; }
.gap select.right { border-color: var(--ok); background: var(--ok-soft); }
.gap select.wrong { border-color: var(--bad); background: var(--bad-soft); }

/* Score */
.score { display: flex; align-items: center; gap: 18px; }
.ring { --p: 0; width: 92px; height: 92px; border-radius: 50%; background: conic-gradient(var(--c, var(--accent)) calc(var(--p) * 1%), var(--surface-2) 0); display: grid; place-items: center; flex-shrink: 0; }
.ring > span { width: 72px; height: 72px; border-radius: 50%; background: var(--surface); display: grid; place-items: center; font-weight: 700; font-size: 1.3rem; }

/* Writer */
.writer { display: grid; grid-template-columns: 1fr 320px; gap: 20px; align-items: start; }
@media (max-width: 960px) { .writer { grid-template-columns: 1fr; } }
.writer textarea { min-height: 420px; resize: vertical; font-family: var(--serif); font-size: 1.08rem; line-height: 1.7; padding: 18px; }
.wc { font-variant-numeric: tabular-nums; font-weight: 600; }
.wc.ok { color: var(--ok); } .wc.low { color: var(--warn); } .wc.high { color: var(--bad); }
.timer { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 1.1rem; }
.timer.big { font-size: 3rem; letter-spacing: -.02em; }
.timer.low { color: var(--bad); }
.hits { display: grid; gap: 6px; }
.hit { display: flex; justify-content: space-between; gap: 8px; font-size: .9rem; padding: 6px 10px; border-radius: 8px; background: var(--bad-soft); }
.hit s { color: var(--bad); }
.hit b { color: var(--ok); font-weight: 600; }
mark.bb { background: var(--bad-soft); color: var(--bad); border-bottom: 2px solid var(--bad); border-radius: 2px; }
.preview { font-family: var(--serif); white-space: pre-wrap; line-height: 1.7; padding: 16px; border: 1px dashed var(--line); border-radius: 10px; max-height: 300px; overflow: auto; }
.conn-group { margin-bottom: 12px; }
.conn-group .chips .chip { cursor: copy; }

/* Rubric */
.rub { display: grid; gap: 12px; }
.rub-row { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; }
.rub-row .d { font-size: .82rem; color: var(--ink-2); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 5px 10px; cursor: pointer; font-weight: 600; font-size: .85rem; border-left: 1px solid var(--line); }
.seg button:first-child { border-left: 0; }
.seg button.on { background: var(--accent); color: var(--accent-ink); }

/* Tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 20px; overflow-x: auto; }
.tabs button { border: 0; background: none; padding: 10px 14px; cursor: pointer; font-weight: 600; color: var(--ink-3); border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs button.on { color: var(--accent); border-bottom-color: var(--accent); }
.tabs button .badge { margin-left: 6px; }

/* Tables */
table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); font-weight: 600; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; }

/* Flashcard */
.flash { min-height: 220px; display: grid; place-items: center; text-align: center; cursor: pointer; user-select: none; }
.flash .w { font-family: var(--serif); font-size: 2.2rem; font-weight: 600; }
.flash .r { font-size: 1.3rem; color: var(--ok); font-weight: 600; }

/* Exam bar */
.exambar { position: sticky; top: 57px; z-index: 10; background: var(--ink); color: var(--bg); border-radius: 12px; padding: 10px 16px; display: flex; align-items: center; gap: 14px; margin-bottom: 18px; flex-wrap: wrap; }
.exambar .timer { font-size: 1.3rem; }
.exambar .btn { background: var(--bg); color: var(--ink); border-color: transparent; }

.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--bad); display: inline-block; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

.chart { width: 100%; height: 180px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 10px; font-weight: 500; z-index: 100; box-shadow: 0 8px 24px rgba(0,0,0,.2); }
.back { display: inline-flex; gap: 6px; align-items: center; text-decoration: none; color: var(--ink-2); font-size: .9rem; margin-bottom: 14px; }
.back:hover { color: var(--accent); }
.hide-sm { }
@media (max-width: 640px) {
  .hide-sm { display: none; }
  h1 { font-size: 1.45rem; }
  .hero h1 { font-size: 1.7rem; }
  .card { padding: 16px; }
  .topbar-in { gap: 8px; }
}

/* Selector d'examen i propostes */
.exam-switch { flex-shrink: 0; }
.exam-switch button { padding: 5px 10px; font-size: .78rem; letter-spacing: .02em; }
.opt-card { display: flex; gap: 12px; align-items: flex-start; cursor: pointer; transition: border-color .15s; }
.opt-card:hover { border-color: var(--ink-3); }
.opt-card:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.opt-card input { margin-top: 5px; accent-color: var(--accent); }
details > summary { list-style-position: inside; }
@media (max-width: 640px) {
  .topbar-in { flex-wrap: wrap; }
  .nav { order: 3; flex-basis: 100%; }
  .exambar { top: 96px; }
}
#phTabs { flex-wrap: wrap; }
#phTabs button { padding: 8px 10px; }

/* Correcció automàtica */
.ai-loading { display: flex; gap: 14px; align-items: center; }
.spinner { width: 26px; height: 26px; border-radius: 50%; border: 3px solid var(--surface-2); border-top-color: var(--accent); animation: spin .9s linear infinite; flex-shrink: 0; }
@keyframes spin { to { transform: rotate(360deg); } }
.crit .meter { margin: 6px 0; }
.crit .pts { font-variant-numeric: tabular-nums; font-weight: 700; }
mark.err { background: var(--bad-soft); color: inherit; border-bottom: 2px solid var(--bad); border-radius: 2px; padding: 0 1px; cursor: help; }
mark.err sup { color: var(--bad); font-family: var(--sans); font-size: .65em; font-weight: 700; margin-left: 1px; }
/* Sessió oral */
.steps { display: flex; gap: 6px; flex-wrap: wrap; }
.steps span { font-size: .8rem; font-weight: 600; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); color: var(--ink-3); }
.steps span.on { background: var(--accent); color: var(--accent-ink); }
.steps span.done { color: var(--ok); }
.btn.mic { font-size: 1.05rem; padding: 12px 22px; }
.btn.mic.rec { background: var(--bad); border-color: var(--bad); color: #fff; }
.chat { display: flex; flex-direction: column; gap: 8px; max-height: 380px; overflow: auto; padding: 12px; margin: 12px 0; background: var(--surface-2); border-radius: 12px; min-height: 120px; }
.bubble { max-width: 85%; padding: 8px 12px; border-radius: 14px; line-height: 1.45; font-size: .95rem; }
.bubble.user { align-self: flex-end; background: var(--accent-soft); border: 1px solid var(--accent); }
.bubble.ai { align-self: flex-start; background: var(--surface); border: 1px solid var(--line); }
.typing { display: inline-flex; gap: 4px; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--ink-3); animation: pulse 1s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
