:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #17202b;
  --muted: #5b6676;
  --line: #dde2e9;
  --accent: #0f766e;
  --accent-soft: #d5f0ec;
  --accent-text: #ffffff;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --bad: #b91c1c;
  --bad-soft: #fee2e2;
  --warn: #a16207;
  --warn-soft: #fef3c7;
  --u1: #2563eb;
  --u2: #7c3aed;
  --u3: #0f766e;
  --u4: #c2410c;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 4px 16px rgba(16, 24, 40, .06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1217;
    --surface: #161b22;
    --surface-2: #1f2630;
    --text: #e6eaf0;
    --muted: #9aa5b4;
    --line: #2a323d;
    --accent: #2dd4bf;
    --accent-soft: #123a36;
    --accent-text: #06201d;
    --ok: #4ade80;
    --ok-soft: #12321f;
    --bad: #f87171;
    --bad-soft: #3a1717;
    --warn: #facc15;
    --warn-soft: #3a3010;
    --u1: #60a5fa;
    --u2: #a78bfa;
    --u3: #2dd4bf;
    --u4: #fb923c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0e1217;
  --surface: #161b22;
  --surface-2: #1f2630;
  --text: #e6eaf0;
  --muted: #9aa5b4;
  --line: #2a323d;
  --accent: #2dd4bf;
  --accent-soft: #123a36;
  --accent-text: #06201d;
  --ok: #4ade80;
  --ok-soft: #12321f;
  --bad: #f87171;
  --bad-soft: #3a1717;
  --warn: #facc15;
  --warn-soft: #3a3010;
  --u1: #60a5fa;
  --u2: #a78bfa;
  --u3: #2dd4bf;
  --u4: #fb923c;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px 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.55 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a { color: var(--accent); }

button { font: inherit; color: inherit; }

h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; }
h1 { font-size: clamp(1.6rem, 4vw, 2.2rem); letter-spacing: -.01em; }
h2 { font-size: 1.3rem; margin-top: 1.8em; }
h3 { font-size: 1.05rem; }
p { margin: 0 0 1em; }

/* ---------- Barra superior ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--text);
  white-space: nowrap;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 800;
  font-size: .85rem;
}

.brand-text { font-size: .95rem; }

.nav {
  display: flex;
  gap: 2px;
  overflow-x: auto;
  flex: 1;
  scrollbar-width: none;
}
.nav::-webkit-scrollbar { display: none; }

.nav a {
  padding: 7px 11px;
  border-radius: 9px;
  color: var(--muted);
  text-decoration: none;
  font-size: .92rem;
  white-space: nowrap;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.theme-btn {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 9px;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  cursor: pointer;
  flex-shrink: 0;
}

.current, .menu-btn { display: none; }

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; gap: 10px; padding: 8px 16px; }
  .brand-text { display: none; }
  .current { display: block; flex: 1; font-weight: 700; font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .menu-btn {
    display: grid; place-items: center;
    width: 40px; height: 40px;
    border: 1px solid var(--line); border-radius: 10px;
    background: var(--surface); cursor: pointer;
    order: 3;
  }
  .theme-btn { width: 40px; height: 40px; order: 2; }
  .nav {
    display: none;
    order: 4;
    flex-basis: 100%;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    padding: 4px 0 8px;
    overflow: visible;
  }
  .topbar.open .nav { display: grid; }
  .nav a { padding: 12px; font-size: .98rem; background: var(--surface-2); text-align: center; border-radius: 10px; }
  .nav a:last-child:nth-child(odd) { grid-column: 1 / -1; }
}

/* ---------- Layout ---------- */
.view {
  width: 100%;
  max-width: 980px;
  margin: 0 auto;
  padding: 28px 16px 48px;
  flex: 1;
  outline: none;
}

.foot {
  text-align: center;
  color: var(--muted);
  font-size: .82rem;
  padding: 18px 16px 28px;
}

.lead { color: var(--muted); font-size: 1.05rem; max-width: 62ch; }
.muted { color: var(--muted); }
.small { font-size: .88rem; }

.grid { display: grid; gap: 14px; }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}

.stat .num { font-size: 1.9rem; font-weight: 750; line-height: 1.1; }
.stat .lbl { color: var(--muted); font-size: .88rem; }

.action-card { display: flex; flex-direction: column; gap: 8px; }
.action-card p { color: var(--muted); font-size: .93rem; flex: 1; margin: 0; }

/* ---------- Botones ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  cursor: pointer;
  font-weight: 600;
  text-decoration: none;
  color: var(--text);
  transition: transform .05s, background .15s, border-color .15s;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .opt:focus-visible, .chip:focus-visible, .fc:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; }
.btn.sm { padding: 6px 11px; font-size: .88rem; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }

kbd {
  font: 600 .72rem/1 ui-monospace, SFMono-Regular, Consolas, monospace;
  padding: 3px 6px;
  border-radius: 5px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  background: var(--surface-2);
  color: var(--muted);
}

/* ---------- Chips y etiquetas ---------- */
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  cursor: pointer;
  font-size: .9rem;
  user-select: none;
}
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }

.tag {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: .76rem;
  font-weight: 650;
  background: var(--surface-2);
  color: var(--muted);
}
.tag.u1 { color: var(--u1); background: color-mix(in srgb, var(--u1) 14%, transparent); }
.tag.u2 { color: var(--u2); background: color-mix(in srgb, var(--u2) 14%, transparent); }
.tag.u3 { color: var(--u3); background: color-mix(in srgb, var(--u3) 14%, transparent); }
.tag.u4 { color: var(--u4); background: color-mix(in srgb, var(--u4) 14%, transparent); }

/* ---------- Barras de progreso ---------- */
.bar { height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar > span { display: block; height: 100%; border-radius: 999px; background: var(--accent); transition: width .4s; }
.bar.u1 > span { background: var(--u1); }
.bar.u2 > span { background: var(--u2); }
.bar.u3 > span { background: var(--u3); }
.bar.u4 > span { background: var(--u4); }

.unit-row { display: grid; grid-template-columns: 1fr auto; gap: 6px 14px; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--line); }
.unit-row:last-child { border-bottom: 0; }
.unit-row .bar { grid-column: 1 / -1; }
.unit-row .pct { font-weight: 700; }

/* ---------- Cuenta regresiva ---------- */
.countdown { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; }
.countdown input {
  font: inherit;
  padding: 8px 10px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
}
.advice { border-left: 4px solid var(--accent); background: var(--accent-soft); padding: 12px 14px; border-radius: 0 10px 10px 0; margin-top: 14px; }

/* ---------- Técnicas ---------- */
.tech { display: flex; flex-direction: column; gap: 6px; }
.tech .lvl { font-size: .76rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; }
.lvl.alta { color: var(--ok); }
.lvl.media { color: var(--warn); }
.lvl.baja { color: var(--bad); }
.tech .how { font-size: .9rem; background: var(--surface-2); padding: 8px 10px; border-radius: 8px; }
.tech .src { font-size: .78rem; color: var(--muted); }

.steps { counter-reset: s; list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.steps li { counter-increment: s; position: relative; padding: 12px 14px 12px 52px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }
.steps li::before {
  content: counter(s);
  position: absolute; left: 14px; top: 12px;
  width: 26px; height: 26px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--accent); color: var(--accent-text); font-weight: 700; font-size: .85rem;
}

/* ---------- Tablas ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
table { border-collapse: collapse; width: 100%; font-size: .93rem; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { background: var(--surface-2); font-size: .82rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
tr:last-child td { border-bottom: 0; }
td:first-child { font-weight: 650; white-space: nowrap; }

.mnemo { border-left: 4px solid var(--warn); background: var(--warn-soft); padding: 10px 14px; border-radius: 0 10px 10px 0; }
.mnemo strong { display: block; }

.trap { padding: 10px 0; border-bottom: 1px dashed var(--line); }
.trap:last-child { border-bottom: 0; }
.trap s { color: var(--bad); text-decoration-thickness: 1.5px; }
.trap .fix { color: var(--ok); font-weight: 600; }

.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0 18px; }

/* ---------- Pregunta ---------- */
.q-head { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.q-head .count { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; font-size: .9rem; }
.q-head .timer { font-weight: 700; font-variant-numeric: tabular-nums; }
.q-head .timer.low { color: var(--bad); }

.q-card { padding: 26px; }
.q-kind { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 700; margin-bottom: 8px; }
.q-text { font-size: clamp(1.1rem, 2.6vw, 1.35rem); font-weight: 600; margin-bottom: 20px; line-height: 1.4; }

.opts { display: grid; gap: 10px; }
.opts.two { grid-template-columns: 1fr 1fr; }
.opt {
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
  padding: 13px 15px;
  border-radius: 12px;
  border: 1.5px solid var(--line);
  background: var(--surface);
  cursor: pointer;
  font-size: 1rem;
}
.opt:hover:not([disabled]) { border-color: var(--accent); background: var(--surface-2); }
.opt .k { flex-shrink: 0; }
.opt.correct { border-color: var(--ok); background: var(--ok-soft); }
.opt.wrong { border-color: var(--bad); background: var(--bad-soft); }
.opt.chosen { border-color: var(--accent); background: var(--accent-soft); }
.opt[disabled] { cursor: default; }
.opts.two .opt { justify-content: center; font-weight: 700; }

.fill-line { font-size: clamp(1.1rem, 2.6vw, 1.3rem); line-height: 2.1; font-weight: 500; }
.fill-input {
  font: inherit;
  font-weight: 650;
  min-width: 8ch;
  width: 14ch;
  max-width: 100%;
  padding: 2px 8px;
  border: 0;
  border-bottom: 2.5px solid var(--accent);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 6px 6px 0 0;
  outline: none;
}
.fill-input.correct { border-color: var(--ok); background: var(--ok-soft); }
.fill-input.wrong { border-color: var(--bad); background: var(--bad-soft); }

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-top: 18px; }
.guess { display: inline-flex; gap: 8px; align-items: center; color: var(--muted); font-size: .92rem; cursor: pointer; user-select: none; }
.guess input { width: 18px; height: 18px; accent-color: var(--accent); }

.feedback { margin-top: 18px; padding: 14px 16px; border-radius: 12px; border: 1px solid; }
.feedback.ok { background: var(--ok-soft); border-color: var(--ok); }
.feedback.bad { background: var(--bad-soft); border-color: var(--bad); }
.feedback .title { font-weight: 750; margin-bottom: 4px; }
.feedback.ok .title { color: var(--ok); }
.feedback.bad .title { color: var(--bad); }
.feedback p { margin: 4px 0 0; }
.why { margin-top: 8px; font-size: .9rem; color: var(--muted); }

.tag.rep { color: var(--warn); background: var(--warn-soft); }

.variants { margin-top: 12px; padding: 12px 14px; border-radius: 12px; background: var(--surface-2); border: 1px solid var(--line); }
.variants p { margin: 4px 0; }
.variants ul { margin: 6px 0 0; padding-left: 18px; display: grid; gap: 6px; font-size: .92rem; }

.hint-line { margin-top: 10px; color: var(--muted); font-size: .9rem; }

/* ---------- Fichas ---------- */
.fc-wrap { perspective: 1200px; }
.fc {
  position: relative;
  min-height: 260px;
  cursor: pointer;
  transform-style: preserve-3d;
  transition: transform .45s cubic-bezier(.2, .7, .2, 1);
  border-radius: var(--radius);
}
.fc.flipped { transform: rotateY(180deg); }
.fc-face {
  position: absolute; inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.fc-face.back { transform: rotateY(180deg); background: var(--surface-2); }
.fc-face .big { font-size: clamp(1.2rem, 3vw, 1.6rem); font-weight: 700; }
.fc-face .body { font-size: 1.08rem; max-width: 52ch; }
.fc-face .tip { position: absolute; bottom: 12px; font-size: .82rem; color: var(--muted); }
.fc-face .tag { position: absolute; top: 14px; left: 14px; }
@media (prefers-reduced-motion: reduce) { .fc { transition: none; } }

.rate { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 16px; }
.rate .btn { flex-direction: column; gap: 2px; padding: 12px 8px; }
.rate .btn small { font-weight: 500; color: var(--muted); font-size: .78rem; }
.rate .r-bad { border-color: var(--bad); }
.rate .r-mid { border-color: var(--warn); }
.rate .r-ok { border-color: var(--ok); }

/* ---------- Resultados ---------- */
.score-big { font-size: 3rem; font-weight: 800; line-height: 1; }
.review-item { padding: 12px 0; border-bottom: 1px solid var(--line); display: grid; gap: 4px; }
.review-item:last-child { border-bottom: 0; }
.review-item .line { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.mark { font-weight: 800; width: 1.2em; flex-shrink: 0; }
.mark.ok { color: var(--ok); }
.mark.bad { color: var(--bad); }
.ans-bad { color: var(--bad); }
.ans-ok { color: var(--ok); font-weight: 600; }

.empty { text-align: center; padding: 36px 16px; color: var(--muted); }

.form-row { display: grid; gap: 8px; margin-bottom: 18px; }
.form-row > label, .form-row > .label { font-weight: 650; font-size: .93rem; }

.danger-zone { margin-top: 32px; padding-top: 18px; border-top: 1px solid var(--line); }

/* ---------- Celular ---------- */
@media (hover: none) and (pointer: coarse) {
  kbd, .kbd-hint { display: none; }
}

@media (max-width: 600px) {
  .view { padding: 18px 16px calc(32px + env(safe-area-inset-bottom)); }
  h2 { margin-top: 1.4em; }
  .lead { font-size: 1rem; }
  .card { padding: 14px; }

  .grid-4 { grid-template-columns: 1fr 1fr; gap: 10px; }
  .stat .num { font-size: 1.5rem; }
  .stat .lbl { font-size: .8rem; }
  .grid-3, .grid-2 { grid-template-columns: 1fr; }

  .countdown { gap: 8px; }
  .countdown label { flex-basis: 100%; }
  .countdown input { flex: 1; min-height: 44px; font-size: 16px; }

  .unit-row { grid-template-columns: 1fr auto; }
  .unit-row .btn { min-height: 38px; }

  .btn { min-height: 46px; }
  .btn.sm { min-height: 36px; }
  .btn-row > .btn, .toolbar > .btn { flex: 1 1 auto; }
  .chip { padding: 9px 13px; }
  .tabs { display: grid; grid-template-columns: repeat(4, 1fr); }
  .tabs .chip { justify-content: center; padding: 9px 4px; font-size: .85rem; }

  .q-head { margin-bottom: 10px; }
  .q-card { padding: 16px; }
  .q-text { margin-bottom: 14px; }
  .opt { padding: 13px 12px; min-height: 52px; }
  .fill-line { line-height: 2.3; }
  .fill-input { font-size: 16px; width: 100%; display: block; margin: 6px 0; }

  /* El botón para seguir queda siempre a mano después de responder. */
  .next-bar {
    position: sticky;
    bottom: 0;
    z-index: 5;
    margin: 14px -16px -16px;
    padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
    background: color-mix(in srgb, var(--surface) 94%, transparent);
    backdrop-filter: blur(6px);
    border-top: 1px solid var(--line);
    border-radius: 0 0 var(--radius) var(--radius);
  }
  .next-bar .btn.primary { flex: 1; }

  .fc { min-height: 300px; }
  .fc-face { padding: 40px 18px 36px; }
  .rate { gap: 8px; }
  .rate .btn { padding: 10px 4px; font-size: .9rem; }
  .rate .btn small { display: none; }

  .score-big { font-size: 2.4rem; }

  /* Tablas como tarjetas apiladas */
  .table-wrap { border: 0; background: transparent; overflow: visible; }
  .table-wrap table, .table-wrap tbody, .table-wrap tr, .table-wrap td { display: block; width: 100%; }
  .table-wrap thead { display: none; }
  .table-wrap tr { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 8px; }
  .table-wrap td { border: 0; padding: 3px 0; white-space: normal; }
  .table-wrap td:first-child { font-size: 1.02rem; color: var(--accent); padding-bottom: 6px; }
  .table-wrap td[data-label]:not(:first-child)::before { content: attr(data-label) ": "; font-weight: 650; color: var(--muted); font-size: .85rem; }
}

/* ---------- Retroalimentación para aprender ---------- */
.feedback.retry { background: var(--warn-soft); border-color: var(--warn); }
.feedback.retry .title { color: var(--warn); }
.feedback.learn { background: var(--accent-soft); border-color: var(--accent); }
.feedback.learn .title { color: var(--accent); }
.opt.tried { opacity: .55; text-decoration: line-through; text-decoration-thickness: 1.5px; border-style: dashed; }
.fill-input.tried { border-color: var(--warn); background: var(--warn-soft); }
.fill-input.learn { border-color: var(--accent); background: var(--accent-soft); }
.why-box { margin-top: 12px; padding: 12px 14px; border-radius: 12px; background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--accent); }
.why-box p { margin: 6px 0 0; }
.why-box .full { font-style: italic; }
#qTools { justify-content: space-between; }

.summary { margin: 4px 0 16px; padding: 10px 14px; border-radius: 10px; background: var(--surface-2); font-size: .95rem; }
.summary strong { color: var(--accent); }
.form-row > .small { margin-top: -2px; }
