:root {
  --bg: #f6f1e7;
  --surface: #fffdf8;
  --surface-2: #f0e9da;
  --ink: #2b2418;
  --ink-soft: #6b5f49;
  --line: #ddd2bb;
  --accent: #8a5a12;
  --accent-ink: #fffdf8;
  --accent-soft: #efe0bd;
  --ok: #3f6b3a;
  --warn: #9a4a12;
  --danger: #a3342a;
  --shadow: 0 1px 2px rgba(43, 36, 24, .06), 0 8px 24px rgba(43, 36, 24, .06);
  --serif: "Newsreader", Georgia, "Times New Roman", serif;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --radius: 10px;
}
:root[data-theme="dark"] {
  --bg: #15120d;
  --surface: #1e1a13;
  --surface-2: #272217;
  --ink: #efe7d4;
  --ink-soft: #b3a78c;
  --line: #3a3324;
  --accent: #d9a94a;
  --accent-ink: #1b1608;
  --accent-soft: #3a2f16;
  --ok: #8dbb86;
  --warn: #e0954f;
  --danger: #e0786d;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .3);
}

* { box-sizing: border-box; }
html { color-scheme: light dark; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
}
h1, h2, h3, h4 { font-family: var(--serif); line-height: 1.2; margin: 0 0 .5em; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Barra superior ---------- */
.topbar {
  display: flex; align-items: center; gap: 24px; flex-wrap: wrap;
  padding: 14px 28px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; margin-right: auto; }
.brand-mark {
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center;
  font-family: var(--serif); font-size: 24px; font-weight: 700; color: var(--accent-ink); background: var(--accent);
}
.brand-logo { width: 44px; height: 44px; border-radius: 12px; flex: none; display: block; }
.brand-name { font-family: var(--serif); font-weight: 700; font-size: 20px; letter-spacing: .14em; }
.brand-sub { font-size: 12px; color: var(--ink-soft); }
.line-switch { display: flex; align-items: center; gap: 10px; }
.line-label { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-soft); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: var(--surface-2); }
.seg button { border: 0; background: transparent; padding: 6px 14px; text-align: left; display: flex; flex-direction: column; line-height: 1.2; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button b { font-weight: 600; font-size: 13px; }
.seg button small { font-size: 10.5px; color: var(--ink-soft); }
.seg button[aria-checked="true"] { background: var(--accent); color: var(--accent-ink); }
.seg button[aria-checked="true"] small { color: inherit; opacity: .85; }
.icon-btn { border: 1px solid var(--line); background: var(--surface); border-radius: 8px; width: 36px; height: 36px; font-size: 16px; }

/* ---------- Navegación ---------- */
.nav { display: flex; gap: 4px; padding: 0 28px; background: var(--surface); border-bottom: 1px solid var(--line); overflow-x: auto; }
.nav button {
  border: 0; background: none; padding: 12px 16px; font-weight: 500; color: var(--ink-soft);
  border-bottom: 3px solid transparent; white-space: nowrap;
}
.nav button .n { display: inline-grid; place-items: center; width: 20px; height: 20px; border-radius: 50%; background: var(--surface-2); font-size: 11px; margin-right: 6px; }
.nav button[aria-current="page"] { color: var(--ink); border-bottom-color: var(--accent); }
.nav button[aria-current="page"] .n { background: var(--accent); color: var(--accent-ink); }

.view { padding: 24px 28px 96px; max-width: 1400px; margin: 0 auto; outline: none; }
.view-head { margin-bottom: 18px; }
.view-head h1 { font-size: 30px; margin-bottom: 4px; }
.view-head p { color: var(--ink-soft); margin: 0; max-width: 80ch; }

/* ---------- Componentes ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; }
.card + .card { margin-top: 16px; }
.btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 8px 14px; font-weight: 500;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn[disabled] { opacity: .55; cursor: wait; }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.grow { flex: 1; }
.muted { color: var(--ink-soft); }
.chip { display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.chip.ok { background: transparent; border: 1px solid var(--ok); color: var(--ok); }
.chip.warn { background: transparent; border: 1px solid var(--warn); color: var(--warn); }

label.f { display: block; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-soft); margin: 14px 0 4px; }
input[type=text], select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface);
}
textarea { min-height: 96px; resize: vertical; line-height: 1.5; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 18px; flex-wrap: wrap; }
.tabs button { border: 0; background: none; padding: 8px 14px; color: var(--ink-soft); border-bottom: 2px solid transparent; font-weight: 500; }
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }

.pills { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 16px; }
.pills button { border: 1px solid var(--line); background: var(--surface); padding: 6px 14px; border-radius: 999px; font-weight: 500; }
.pills button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* ---------- Currículo ---------- */
.split { display: grid; grid-template-columns: 340px minmax(0, 1fr); gap: 20px; align-items: start; }
.tree { position: sticky; top: 140px; max-height: calc(100vh - 160px); overflow: auto; padding: 8px; }
.tree details { border-bottom: 1px solid var(--line); }
.tree details:last-child { border-bottom: 0; }
.tree summary { cursor: pointer; padding: 9px 8px; font-weight: 600; font-size: 14px; list-style: none; display: flex; gap: 8px; align-items: baseline; }
.tree summary::-webkit-details-marker { display: none; }
.tree summary::before { content: "▸"; color: var(--accent); transition: transform .15s; }
.tree details[open] > summary::before { transform: rotate(90deg); }
.tree .area-title { font-family: var(--serif); font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--accent); padding: 12px 8px 2px; font-weight: 700; }
.tree .mod-desc { font-weight: 400; font-size: 12px; color: var(--ink-soft); padding: 0 8px 4px 24px; margin-top: -4px; }
.lesson-btn {
  display: flex; width: 100%; text-align: left; gap: 8px; align-items: baseline; border: 0; background: none;
  padding: 6px 8px 6px 24px; border-radius: 6px; font-size: 13.5px;
}
.lesson-btn:hover { background: var(--surface-2); }
.lesson-btn[aria-current="true"] { background: var(--accent-soft); color: var(--ink); font-weight: 600; }
.lesson-btn .dot { width: 8px; height: 8px; border-radius: 50%; border: 1.5px solid var(--line); flex: none; transform: translateY(1px); }
.lesson-btn .dot.on { background: var(--ok); border-color: var(--ok); }
.lesson-btn .star { color: var(--warn); font-size: 11px; }

.lesson-pane { min-height: 320px; }
.lesson-toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; padding-bottom: 14px; border-bottom: 1px solid var(--line); }
.empty { color: var(--ink-soft); text-align: center; padding: 56px 16px; font-family: var(--serif); font-size: 18px; }

.loading { display: flex; gap: 12px; align-items: center; color: var(--ink-soft); padding: 28px 8px; }
.spinner { width: 20px; height: 20px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }

/* ---------- Texto renderizado (Markdown) ---------- */
.prose { font-family: var(--serif); font-size: 18px; line-height: 1.65; max-width: 74ch; }
.prose h1 { font-size: 30px; margin: .2em 0 .6em; }
.prose h2 { font-size: 23px; margin: 1.5em 0 .5em; color: var(--accent); }
.prose h3 { font-size: 19px; margin: 1.2em 0 .4em; }
.prose p { margin: 0 0 .9em; }
.prose ul, .prose ol { padding-left: 1.4em; margin: 0 0 1em; }
.prose li { margin-bottom: .3em; }
.prose blockquote { margin: 1em 0; padding: .2em 1em; border-left: 3px solid var(--accent); color: var(--ink-soft); font-style: italic; }
.prose hr { border: 0; border-top: 1px solid var(--line); margin: 1.6em 0; }
.prose code { font-family: ui-monospace, Consolas, monospace; font-size: .85em; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
.prose table { border-collapse: collapse; margin: 1em 0; font-size: 15px; font-family: var(--sans); }
.prose th, .prose td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.prose.compact { font-size: 16px; }
.prose.compact h2 { font-size: 19px; }

.editor { font-family: ui-monospace, Consolas, monospace; font-size: 13.5px; min-height: 55vh; }
.notes-box { margin-top: 24px; padding-top: 16px; border-top: 1px dashed var(--line); }

/* ---------- Tutor / bloques ---------- */
.tutor { margin-bottom: 20px; }
.tutor-form { display: flex; gap: 10px; }
.tutor-form input { flex: 1; }
.redirect {
  display: block; width: 100%; text-align: left; margin-top: 8px; padding: 10px 12px; border: 1px solid var(--accent); border-radius: 8px; background: var(--accent-soft); color: var(--ink);
}
.redirect b { display: block; }
.redirect small { color: var(--ink-soft); }

.block { border-left: 3px solid var(--accent); padding: 4px 0 4px 14px; margin: 18px 0; }
.block h3 { font-family: var(--sans); font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--accent); margin-bottom: 6px; }
.block.alert { border-left-color: var(--warn); }
.block.alert h3 { color: var(--warn); }

/* ---------- Conferencias ---------- */
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.topic-row { display: flex; gap: 8px; margin-bottom: 8px; }
.topic-row .num { width: 64px; flex: none; display: grid; place-items: center; background: var(--surface-2); border-radius: 8px; font-size: 12px; font-weight: 600; color: var(--ink-soft); }
.list-item { display: flex; gap: 10px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.list-item:last-child { border-bottom: 0; }
.list-item .t { flex: 1; font-weight: 500; min-width: 0; }
.list-item .t small { display: block; color: var(--ink-soft); font-weight: 400; }

.finding { border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px; margin: 10px 0; background: var(--surface); }
.finding blockquote { margin: 0 0 8px; font-style: italic; color: var(--ink-soft); font-family: var(--serif); }
.finding.alta { border-left: 4px solid var(--danger); }
.finding.media { border-left: 4px solid var(--warn); }
.finding.baja { border-left: 4px solid var(--ok); }

.lex { width: 100%; border-collapse: collapse; font-size: 14px; }
.lex th, .lex td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.lex th { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-soft); }
.lex .orig { font-family: var(--serif); font-size: 18px; }

/* ---------- Panel flotante ---------- */
.fab {
  position: fixed; right: 22px; bottom: 22px; z-index: 30; display: flex; align-items: center; gap: 10px;
  background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 999px; padding: 10px 18px 10px 10px; box-shadow: var(--shadow); font-weight: 600;
}
.fab-q { width: 30px; height: 30px; border-radius: 50%; background: var(--accent-ink); color: var(--accent); display: grid; place-items: center; font-family: var(--serif); font-size: 20px; font-weight: 700; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(520px, 100vw); z-index: 40; background: var(--surface); border-left: 1px solid var(--line);
  box-shadow: var(--shadow); transform: translateX(100%); transition: transform .22s ease; display: flex; flex-direction: column; visibility: hidden;
}
.drawer.open { transform: none; visibility: visible; }
.drawer-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.drawer-head h2 { margin: 0; font-size: 21px; }
.drawer-body { padding: 16px 18px 28px; overflow: auto; flex: 1; }

.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none; z-index: 60;
  background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 8px; transition: all .2s; max-width: min(92vw, 560px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--danger); color: #fff; }

.err-box { border: 1px solid var(--danger); color: var(--danger); border-radius: 8px; padding: 12px 14px; margin: 12px 0; }

@media (max-width: 980px) {
  .split { grid-template-columns: 1fr; }
  .tree { position: static; max-height: 360px; }
  .grid2 { grid-template-columns: 1fr; }
  .topbar { padding: 12px 16px; gap: 12px; }
  .nav, .view { padding-left: 16px; padding-right: 16px; }
  .seg button small { display: none; }
  .fab-t { display: none; }
  .fab { padding: 10px; }
}
@media print {
  .topbar, .nav, .fab, .drawer, .tree, .lesson-toolbar, .toast { display: none !important; }
  .split { display: block; }
  body { background: #fff; color: #000; }
}
