:root {
  --bg: #f6f4f0;
  --surface: #ffffff;
  --surface-2: #efece6;
  --text: #1d2329;
  --muted: #5f6870;
  --line: #ddd8cf;
  --accent: #d0583a;
  --accent-ink: #ffffff;
  --accent-soft: #fbe6df;
  --teal: #1f7a72;
  --teal-soft: #dcefec;
  --ok: #217a45;
  --ok-soft: #e0f2e6;
  --ko: #b3261e;
  --ko-soft: #fbe3e1;
  --warn: #9a6400;
  --warn-soft: #fbefd6;
  --shadow: 0 1px 2px rgba(30, 25, 20, .06), 0 4px 14px rgba(30, 25, 20, .06);
  --radius: 14px;
  --tabs-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14181d; --surface: #1d232a; --surface-2: #262d35; --text: #eef0f2; --muted: #a2abb4;
    --line: #333c46; --accent: #f07a5a; --accent-ink: #1a0d08; --accent-soft: #3a2621;
    --teal: #5cc1b6; --teal-soft: #1b3533; --ok: #63c787; --ok-soft: #1a3324; --ko: #f08a82; --ko-soft: #3d201e;
    --warn: #e7b65a; --warn-soft: #3a2f18; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 14px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #14181d; --surface: #1d232a; --surface-2: #262d35; --text: #eef0f2; --muted: #a2abb4;
  --line: #333c46; --accent: #f07a5a; --accent-ink: #1a0d08; --accent-soft: #3a2621;
  --teal: #5cc1b6; --teal-soft: #1b3533; --ok: #63c787; --ok-soft: #1a3324; --ko: #f08a82; --ko-soft: #3d201e;
  --warn: #e7b65a; --warn-soft: #3a2f18; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 14px rgba(0,0,0,.25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100dvh;
}
main {
  max-width: 720px; margin: 0 auto;
  padding: calc(16px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) calc(var(--tabs-h) + 24px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
body.no-tabs main { padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
h1 { font-size: 1.6rem; line-height: 1.2; margin: 4px 0 4px; letter-spacing: -.01em; }
h2 { font-size: 1.15rem; margin: 24px 0 10px; }
h3 { font-size: 1rem; margin: 0 0 6px; }
p { margin: 0 0 10px; }
a { color: var(--teal); }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.loading { padding: 40vh 0; text-align: center; color: var(--muted); }
.center { text-align: center; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spread { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.stack > * + * { margin-top: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.card.tight { padding: 12px 14px; }
.hero { background: linear-gradient(135deg, var(--accent-soft), var(--teal-soft)); border: 0; }

button, .btn {
  font: inherit; cursor: pointer; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); padding: 12px 16px; min-height: 48px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; text-decoration: none;
  -webkit-tap-highlight-color: transparent; transition: transform .08s, background .15s, border-color .15s;
}
button:active, .btn:active { transform: scale(.98); }
button:focus-visible, .btn:focus-visible, a:focus-visible, input:focus-visible, summary:focus-visible { outline: 3px solid var(--teal); outline-offset: 2px; }
button:disabled { opacity: .5; cursor: default; }
.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.secondary { background: var(--teal); border-color: var(--teal); color: var(--surface); font-weight: 600; }
.ghost { background: transparent; }
.danger { color: var(--ko); border-color: var(--ko); background: transparent; }
.block { width: 100%; }
.big { min-height: 56px; font-size: 1.05rem; }
.link { background: none; border: 0; padding: 4px; min-height: 0; color: var(--teal); text-decoration: underline; }

.action-card {
  display: flex; align-items: center; gap: 14px; text-align: left; width: 100%;
  padding: 16px; border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow);
}
.action-card .ico { font-size: 1.8rem; width: 44px; text-align: center; flex: none; }
.action-card b { display: block; font-size: 1.05rem; }
.action-card span.sub { color: var(--muted); font-size: .9rem; }
.action-card .go { margin-left: auto; color: var(--muted); font-size: 1.4rem; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px; text-align: center; }
.stat b { display: block; font-size: 1.4rem; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat span { font-size: .8rem; color: var(--muted); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 40px; padding: 8px 14px; border-radius: 999px; font-size: .95rem;
}
.chip[aria-pressed="true"] { background: var(--teal); border-color: var(--teal); color: var(--surface); }

.tema-list { list-style: none; margin: 0; padding: 0; }
.tema-list li { border-top: 1px solid var(--line); }
.tema-list li:first-child { border-top: 0; }
.tema-list label { display: flex; gap: 12px; align-items: center; padding: 10px 2px; cursor: pointer; min-height: 48px; }
.tema-list input { width: 22px; height: 22px; accent-color: var(--teal); flex: none; }
.tema-list .n { color: var(--muted); font-size: .85rem; margin-left: auto; white-space: nowrap; }
details.bloque { margin-bottom: 10px; }
details.bloque > summary { cursor: pointer; font-weight: 600; padding: 6px 0; list-style-position: outside; }

/* Quiz */
.qtop { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.qtop .close { min-height: 40px; padding: 6px 12px; }
.progress { flex: 1; height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--teal); border-radius: 99px; transition: width .3s; }
.counter { font-variant-numeric: tabular-nums; color: var(--muted); font-size: .9rem; white-space: nowrap; }
.timer { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.timer.low { color: var(--ko); }
.run { font-weight: 700; color: var(--accent); white-space: nowrap; }
.qmeta { font-size: .8rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 6px; }
.tag { display: inline-block; padding: 1px 8px; border-radius: 99px; background: var(--warn-soft); color: var(--warn); font-weight: 600; text-transform: none; letter-spacing: 0; }
.enunciado { font-size: 1.1rem; line-height: 1.45; margin-bottom: 16px; white-space: pre-line; }
.options { display: grid; gap: 10px; }
.opt {
  justify-content: flex-start; text-align: left; width: 100%; padding: 12px 14px; gap: 12px; align-items: flex-start;
  line-height: 1.4; min-height: 56px;
}
.opt .letter {
  flex: none; width: 30px; height: 30px; border-radius: 50%; display: inline-grid; place-items: center;
  background: var(--surface-2); font-weight: 700; font-size: .9rem;
}
.opt.selected { border-color: var(--teal); background: var(--teal-soft); }
.opt.selected .letter { background: var(--teal); color: var(--surface); }
.opt.correct { border-color: var(--ok); background: var(--ok-soft); }
.opt.correct .letter { background: var(--ok); color: var(--surface); }
.opt.wrong { border-color: var(--ko); background: var(--ko-soft); }
.opt.wrong .letter { background: var(--ko); color: var(--surface); }
.opt.dim { opacity: .65; }
.opt:disabled { opacity: 1; cursor: default; }
.opt.dim:disabled { opacity: .6; }

.feedback { margin-top: 16px; border-radius: var(--radius); padding: 14px 16px; border: 1px solid var(--line); background: var(--surface); animation: rise .25s ease-out; }
.feedback.ok { border-color: var(--ok); }
.feedback.ko { border-color: var(--ko); }
.feedback .verdict { font-weight: 700; font-size: 1.1rem; margin-bottom: 6px; }
.feedback.ok .verdict { color: var(--ok); }
.feedback.ko .verdict { color: var(--ko); }
.expl { margin: 8px 0; }
.expl b { display: block; font-size: .85rem; color: var(--muted); }
.feedback details { margin-top: 8px; }
.feedback summary { cursor: pointer; color: var(--teal); font-weight: 600; padding: 6px 0; }
.source { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 10px; font-size: .85rem; color: var(--muted); flex-wrap: wrap; }
.next-bar {
  position: sticky; bottom: 0; padding: 12px 0 calc(8px + env(safe-area-inset-bottom));
  background: linear-gradient(transparent, var(--bg) 30%); margin-top: 8px;
}
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.04); } 100% { transform: scale(1); } }
.opt.correct.just { animation: pop .3s; }
@keyframes shake { 0%,100% { transform: none; } 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }
.opt.wrong.just { animation: shake .3s; }

.palette { display: grid; grid-template-columns: repeat(auto-fill, minmax(40px, 1fr)); gap: 6px; }
.palette button { min-height: 40px; padding: 0; font-size: .85rem; border-radius: 8px; }
.palette button.answered { background: var(--teal-soft); border-color: var(--teal); }
.palette button.current { outline: 2px solid var(--accent); }
.exam-nav { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 16px; }

/* Results */
.score-ring { width: 150px; height: 150px; margin: 8px auto 6px; position: relative; }
.score-ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.score-ring .val { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; }
.score-ring .val b { font-size: 2.2rem; line-height: 1; display: block; }
.score-ring .val span { font-size: .8rem; color: var(--muted); }
.result-q { border-top: 1px solid var(--line); padding: 12px 0; }
.result-q:first-child { border-top: 0; }
.result-q summary { cursor: pointer; display: flex; gap: 10px; align-items: flex-start; list-style: none; }
.result-q summary::-webkit-details-marker { display: none; }
.mark { flex: none; width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center; font-size: .8rem; font-weight: 700; color: var(--surface); }
.mark.ok { background: var(--ok); } .mark.ko { background: var(--ko); } .mark.blank { background: var(--muted); }
.result-q .body { padding: 8px 0 0 34px; }
.result-q .body p { margin: 4px 0; }

/* Progress bars */
.mastery { margin: 0; padding: 0; list-style: none; }
.mastery li { padding: 10px 0; border-top: 1px solid var(--line); }
.mastery li:first-child { border-top: 0; }
.bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin-top: 6px; }
.bar > i { display: block; height: 100%; border-radius: 99px; background: var(--teal); }
.bar.lo > i { background: var(--ko); } .bar.mid > i { background: var(--warn); } .bar.hi > i { background: var(--ok); }

.history { list-style: none; margin: 0; padding: 0; }
.history li { display: flex; justify-content: space-between; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); font-size: .95rem; }
.history li:first-child { border-top: 0; }

/* Badges */
.badges { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.badge { text-align: center; padding: 14px 10px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--line); }
.badge .emoji { font-size: 2.2rem; line-height: 1.2; display: block; }
.badge b { display: block; margin-top: 4px; }
.badge span { font-size: .82rem; color: var(--muted); display: block; }
.badge.locked .emoji { filter: grayscale(1); opacity: .35; }
.badge.locked b { color: var(--muted); }
.badge.got { border-color: var(--accent); background: linear-gradient(180deg, var(--accent-soft), var(--surface)); }

/* Tabs */
#tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
#tabs[hidden] { display: none; }
#tabs a {
  height: var(--tabs-h); display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-decoration: none; color: var(--muted); font-size: .72rem; gap: 2px; position: relative;
}
#tabs a span { font-size: 1.3rem; line-height: 1; }
#tabs a.active { color: var(--accent); font-weight: 600; }
.badge-count {
  position: absolute; top: 6px; left: calc(50% + 6px); min-width: 18px; height: 18px; border-radius: 9px;
  background: var(--accent); color: var(--accent-ink); font-size: .7rem; display: grid; place-items: center; padding: 0 4px;
}
.badge-count[hidden] { display: none; }

#banner { background: var(--warn-soft); color: var(--warn); padding: 10px 16px; text-align: center; font-size: .9rem; padding-top: calc(10px + env(safe-area-inset-top)); }

#toasts { position: fixed; left: 50%; transform: translateX(-50%); top: calc(12px + env(safe-area-inset-top)); z-index: 30; display: grid; gap: 8px; width: min(420px, calc(100% - 32px)); pointer-events: none; }
.toast { background: var(--text); color: var(--bg); padding: 12px 16px; border-radius: 12px; box-shadow: var(--shadow); display: flex; gap: 10px; align-items: center; animation: rise .3s ease-out; }
.toast .emoji { font-size: 1.6rem; }
#confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 25; }

dialog { border: 0; border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 20px; width: min(480px, calc(100% - 32px)); box-shadow: 0 10px 40px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(10, 12, 15, .5); }
dialog textarea { width: 100%; min-height: 100px; font: inherit; padding: 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); }
dialog .row { justify-content: flex-end; margin-top: 14px; }

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 6px; background: var(--surface-2); padding: 4px; border-radius: 12px; }
.seg button { border: 0; background: transparent; min-height: 42px; }
.seg button[aria-pressed="true"] { background: var(--surface); box-shadow: var(--shadow); font-weight: 600; }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 48px; cursor: pointer; }
.switch input { width: 22px; height: 22px; accent-color: var(--teal); }

.empty { text-align: center; padding: 28px 12px; }
.empty .emoji { font-size: 2.6rem; display: block; margin-bottom: 6px; }

@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
@media (min-width: 560px) { h1 { font-size: 1.9rem; } }
