/* ===== Variables de tema ===== */
:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eef1f7;
  --text: #0f172a;
  --text-muted: #5b6472;
  --border: #e2e7f0;
  --primary: #2563eb;
  --primary-ink: #ffffff;
  --accent: #7c3aed;
  --ok: #16a34a;
  --bad: #dc2626;
  --warn: #d97706;
  --shadow: 0 1px 3px rgba(15,23,42,.08);
  --shadow-lg: 0 8px 24px rgba(15,23,42,.14);
  --radius: 14px;
  --font-size: 16px;
}
[data-theme="dark"] {
  --bg: #0b1220;
  --surface: #111a2c;
  --surface-2: #182236;
  --text: #e6ebf4;
  --text-muted: #94a3b8;
  --border: #22304a;
  --primary: #3b82f6;
  --primary-ink: #ffffff;
  --accent: #a78bfa;
  --ok: #22c55e;
  --bad: #f87171;
  --warn: #fbbf24;
  --shadow: 0 1px 3px rgba(0,0,0,.4);
  --shadow-lg: 0 8px 24px rgba(0,0,0,.5);
}

/* ===== Base ===== */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { font-size: var(--font-size); }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  padding-bottom: 76px;
  padding-top: 56px;
  -webkit-font-smoothing: antialiased;
}
h1,h2,h3,h4 { margin: 0 0 .4rem; line-height: 1.25; }
p { margin: 0 0 .7rem; }
a { color: var(--primary); }
button { font-family: inherit; }
.muted { color: var(--text-muted); }
.hidden { display: none !important; }
[hidden] { display: none !important; }

/* ===== App bar ===== */
.appbar {
  position: fixed; top: 0; left: 0; right: 0; height: 56px;
  display: flex; align-items: center; gap: 8px;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 50;
}
.appbar-title { font-size: 1.05rem; font-weight: 700; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn {
  width: 40px; height: 40px; border: none; border-radius: 12px;
  background: transparent; color: var(--text); font-size: 1.2rem;
  cursor: pointer; display: grid; place-items: center;
}
.icon-btn:active { background: var(--surface-2); }

/* ===== Main / views ===== */
.main { padding: 16px; max-width: 720px; margin: 0 auto; }
.view { animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.view-head { margin-bottom: 14px; }
.view-head h2 { font-size: 1.25rem; }

/* ===== Hero / stats ===== */
.hero { padding: 8px 2px 16px; }
.hero h2 { font-size: 1.4rem; }
.stat-strip { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.stat-pill {
  flex: 1; min-width: 90px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 12px; text-align: center; box-shadow: var(--shadow);
}
.stat-pill .n { font-size: 1.3rem; font-weight: 800; color: var(--primary); }
.stat-pill .l { font-size: .72rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .03em; }

/* ===== Listas ===== */
.module-list, .chapter-list, .section-list, .search-results { display: flex; flex-direction: column; gap: 10px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); cursor: pointer; text-align: left; width: 100%;
  display: block; color: inherit;
}
.card:active { transform: scale(.99); }
.card .card-title { font-weight: 700; font-size: 1rem; }
.card .card-sub { color: var(--text-muted); font-size: .86rem; margin-top: 2px; }
.card .badge {
  display: inline-block; font-size: .68rem; padding: 2px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-muted); margin-left: 6px; vertical-align: middle;
}
.badge.recap { background: var(--accent); color: #fff; }
.badge.done { background: var(--ok); color: #fff; }
.card-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

/* ===== Sección detalle ===== */
.section-title { font-size: 1.2rem; font-weight: 800; margin-bottom: 4px; }
.section-num { color: var(--primary); font-weight: 700; }
.section-content { margin: 14px 0; }
.section-content h4, .section-content strong { display: block; margin-top: 12px; }
.section-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 16px 0; }
.btn {
  display: inline-flex; align-items: center; gap: 6px; justify-content: center;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 12px; padding: 12px 16px; font-size: .92rem; font-weight: 600;
  cursor: pointer; min-height: 44px; box-shadow: var(--shadow);
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.small { padding: 8px 12px; min-height: 38px; font-size: .84rem; }
.btn.full { width: 100%; }

/* ===== Flashcards ===== */
.flashcard {
  perspective: 1000px; cursor: pointer; user-select: none;
  width: 100%; min-height: 260px; margin: 8px 0 20px;
}
.flashcard-inner {
  position: relative; width: 100%; min-height: 260px; transition: transform .5s;
  transform-style: preserve-3d; display: flex;
}
.flashcard.flipped .flashcard-inner { transform: rotateY(180deg); }
.flashcard-face {
  position: absolute; inset: 0; backface-visibility: hidden;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 26px 22px; display: flex; flex-direction: column; justify-content: center;
  box-shadow: var(--shadow-lg); overflow-y: auto;
}
.flashcard-face.back { transform: rotateY(180deg); background: var(--surface-2); }
.flashcard-face .fc-label { font-size: .7rem; text-transform: uppercase; letter-spacing: .06em; color: var(--text-muted); margin-bottom: 10px; }
.flashcard-face .fc-text { font-size: 1.15rem; font-weight: 600; }
.flashcard-face.back .fc-text { font-weight: 500; }
.fc-actions { display: flex; gap: 8px; margin-top: 12px; }
.fc-actions .btn { flex: 1; }
.fc-progress { text-align: center; color: var(--text-muted); font-size: .85rem; margin: 4px 0; }

/* ===== Quiz ===== */
.quiz-q { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; box-shadow: var(--shadow); }
.quiz-q .q-text { font-weight: 600; margin-bottom: 10px; }
.quiz-opt {
  display: block; width: 100%; text-align: left; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px;
  margin-bottom: 8px; cursor: pointer; color: var(--text); font-size: .92rem; min-height: 44px;
}
.quiz-opt.correct { background: #dcfce7; border-color: var(--ok); color: #14532d; }
.quiz-opt.wrong { background: #fee2e2; border-color: var(--bad); color: #7f1d1d; }
[data-theme="dark"] .quiz-opt.correct { background: #052e16; color: #86efac; }
[data-theme="dark"] .quiz-opt.wrong { background: #3f0a0a; color: #fca5a5; }
.quiz-opt:disabled { cursor: default; opacity: 1; }
.explain { font-size: .85rem; color: var(--text-muted); background: var(--surface-2); border-radius: 10px; padding: 10px 12px; margin-top: 10px; }
.explain b { color: var(--text); }

/* ===== Simulacro ===== */
.exam-timer { font-size: 1.1rem; font-weight: 800; color: var(--warn); text-align: center; margin: 8px 0; }
.exam-timer.low { color: var(--bad); }

/* ===== Busqueda ===== */
.searchbar { margin-bottom: 14px; }
.searchbar input {
  width: 100%; min-height: 48px; border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 16px; font-size: 1rem; background: var(--surface); color: var(--text);
}
.searchbar input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.search-result .card-title mark { background: #fde68a; color: #78350f; border-radius: 3px; padding: 0 2px; }
[data-theme="dark"] .search-result .card-title mark { background: #713f12; color: #fde68a; }

/* ===== Bottom nav ===== */
.bottomnav {
  position: fixed; bottom: 0; left: 0; right: 0; height: 64px;
  display: flex; background: var(--surface); border-top: 1px solid var(--border);
  z-index: 50; padding-bottom: env(safe-area-inset-bottom);
}
.nav-btn {
  flex: 1; border: none; background: transparent; color: var(--text-muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: .68rem; cursor: pointer; padding: 6px 0;
}
.nav-btn .nav-ico { font-size: 1.3rem; line-height: 1; }
.nav-btn.active { color: var(--primary); font-weight: 700; }

/* ===== Notas / favoritos ===== */
textarea.note {
  width: 100%; min-height: 110px; border: 1px solid var(--border); border-radius: 12px;
  padding: 12px; font-size: .95rem; background: var(--surface); color: var(--text); font-family: inherit;
  resize: vertical;
}
.star { color: var(--warn); font-size: 1.3rem; }

/* ===== Ajustes ===== */
.setting-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; margin-bottom: 10px; box-shadow: var(--shadow);
}
.setting-row label { font-weight: 600; }
.setting-row input[type="range"] { width: 120px; }
.seg { display: flex; gap: 6px; }
.seg button {
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  border-radius: 10px; padding: 8px 12px; cursor: pointer; font-size: .85rem; font-weight: 600;
}
.seg button.on { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }

/* ===== Toast ===== */
#toast {
  position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px;
  font-size: .88rem; font-weight: 600; box-shadow: var(--shadow-lg); z-index: 99;
  opacity: 0; pointer-events: none; transition: opacity .2s;
}
#toast.show { opacity: 1; }

/* ===== Dashboard / progreso ===== */
.xpbar, .goalbar {
  position: relative; background: var(--surface-2); border-radius: 999px;
  height: 26px; margin-bottom: 8px; overflow: hidden;
}
.xpbar-fill { height: 100%; background: linear-gradient(90deg, var(--primary), var(--accent)); border-radius: 999px; transition: width .5s cubic-bezier(.16,1,.3,1); }
.xpbar-label, .goalbar-label {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-size: .72rem; font-weight: 700; color: var(--text); text-shadow: 0 1px 2px var(--bg);
}
.goalbar-fill { height: 100%; background: var(--ok); border-radius: 999px; transition: width .5s cubic-bezier(.16,1,.3,1); }
.home-h { font-size: 1rem; margin: 20px 0 10px; }
.card-pct { font-weight: 800; color: var(--primary); font-size: .95rem; }
.card-pct.low { color: var(--bad); }
.bar { height: 6px; background: var(--surface-2); border-radius: 999px; margin-top: 8px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--primary); border-radius: 999px; transition: width .5s cubic-bezier(.16,1,.3,1); }
.resume { border-left: 3px solid var(--primary); }
.small-card { padding: 10px 14px; }

/* ===== Gráfico de actividad ===== */
.chart { display: flex; align-items: flex-end; gap: 6px; height: 100px; margin-top: 12px; }
.chart-col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; height: 100%; justify-content: flex-end; }
.chart-bar { width: 100%; max-width: 28px; background: var(--primary); border-radius: 6px 6px 2px 2px; min-height: 2px; }
.chart-label { font-size: .62rem; color: var(--text-muted); }

/* ===== Logros ===== */
.ach-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.ach-grid .card { text-align: center; padding: 16px 10px; cursor: default; }
.ach-locked { opacity: .55; }
.ach-earned { border-color: var(--primary); }
.ach-icon { font-size: 1.8rem; }
.ach-name { font-weight: 700; font-size: .9rem; margin-top: 4px; }
.ach-desc { color: var(--text-muted); font-size: .75rem; margin-top: 2px; }

/* ===== Flashcards v2 ===== */
.fc-mode-toggle { display: flex; gap: 6px; margin-bottom: 8px; }
.fc-mode-toggle button {
  flex: 1; border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  border-radius: 10px; padding: 8px; cursor: pointer; font-size: .85rem; font-weight: 600;
}
.fc-mode-toggle button.seg-on { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
#fcTypeInput { width: 100%; }

/* ===== Pantalla de bloqueo ===== */
.lockscreen {
  position: fixed; inset: 0; z-index: 200; background: var(--bg);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.lockbox {
  background: var(--surface); border: 1px solid var(--border); border-radius: 18px;
  padding: 30px 24px; width: 100%; max-width: 340px; text-align: center; box-shadow: var(--shadow-lg);
}
.lock-ico { font-size: 2.6rem; }
.lockbox h2 { margin: 8px 0 4px; }
.lockbox input {
  width: 100%; text-align: center; font-size: 1.6rem; letter-spacing: .4em;
  border: 1px solid var(--border); border-radius: 12px; padding: 12px;
  background: var(--surface-2); color: var(--text); margin: 16px 0;
}
.lockbox input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }

/* ===== Banner de recordatorio ===== */
.reminder-banner {
  position: fixed; top: 60px; left: 12px; right: 12px; z-index: 90;
  display: flex; align-items: center; gap: 10px;
  background: var(--primary); color: var(--primary-ink);
  border-radius: 14px; padding: 10px 14px; box-shadow: var(--shadow-lg);
  font-weight: 600; font-size: .9rem;
}
.reminder-banner .btn.small { background: rgba(255,255,255,.2); color: var(--primary-ink); border: none; }
.reminder-banner .icon-btn { color: var(--primary-ink); }

/* ===== Desafío / batalla ===== */
.challenge-timer { font-size: 1.4rem; font-weight: 800; text-align: center; color: var(--primary); margin: 8px 0; }
.challenge-timer.low { color: var(--bad); }
