/* CMA Part 2 study app: tokens, layout, lesson typography, tools.
   Mobile first (375px). Light is default, dark follows the system unless set. */

/* ---------- Tokens ---------- */
:root {
  --ink: #0F1624;
  --navy: #00214A;
  --deep: #08375F;
  --teal: #1A7A96;
  --paper: #FFF9F8;
  --correct: #2E9E6A;
  --wrong: #C8553D;
  --gold: #E0B44C;

  --bg: var(--paper);
  --surface: #FFFFFF;
  --surface-2: #F2EEEC;
  --text: var(--ink);
  --muted: #5A6374;
  --line: rgba(15, 22, 36, 0.12);
  --accent: var(--teal);

  --hl-yellow: #FFE58A;
  --hl-green: #B9E8CF;
  --hl-blue: #B8D8F5;
  --hl-pink: #F7C6D9;
  --hl-orange: #FFC89A;

  --radius: 12px;
  --shadow: 0 8px 28px rgba(15, 22, 36, 0.16);
  --sans: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --serif: 'Source Serif 4', Georgia, 'Times New Roman', serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: var(--ink);
    --surface: var(--navy);
    --surface-2: var(--deep);
    --text: var(--paper);
    --muted: #A9B3C4;
    --line: rgba(255, 249, 248, 0.14);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: var(--ink);
  --surface: var(--navy);
  --surface-2: var(--deep);
  --text: var(--paper);
  --muted: #A9B3C4;
  --line: rgba(255, 249, 248, 0.14);
  color-scheme: dark;
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  padding-bottom: 104px;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, textarea:focus-visible, a:focus-visible {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 56px;
  padding: 8px 12px 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { min-width: 0; display: flex; flex-direction: column; line-height: 1.2; }
.brand-title { font-weight: 700; font-size: 15px; }
.section-label {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 62vw;
}
.top-actions { display: flex; gap: 2px; flex-shrink: 0; }
.icon-btn {
  width: 42px;
  height: 42px;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: transparent;
  display: inline-grid;
  place-items: center;
  color: var(--text);
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 20px; height: 20px; }

/* ---------- Lesson ---------- */
#lesson-wrap {
  position: relative;
  display: flow-root;
}
#lesson {
  max-width: 68ch;
  margin: 0 auto;
  padding: 18px 16px 48px;
  font-family: var(--serif);
  font-size: 17px;
  line-height: 1.65;
  color: var(--text);
}
#lesson p { margin: 0 0 1em; }
#lesson h2 {
  font-family: var(--sans);
  font-size: 1.45rem;
  line-height: 1.25;
  font-weight: 700;
  color: var(--accent);
  margin: 2.2em 0 0.6em;
}
#lesson h3 {
  font-family: var(--sans);
  font-size: 1.1rem;
  font-weight: 600;
  margin: 1.6em 0 0.5em;
}
#lesson table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--sans);
  font-size: 0.92em;
  margin: 1em 0;
}
#lesson th, #lesson td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}
#lesson th { background: var(--surface-2); font-weight: 600; }
#lesson .table-wrap { overflow-x: auto; }
#lesson .formula {
  font-family: var(--sans);
  font-size: 0.95em;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin: 1.2em 0;
  overflow-x: auto;
}
#lesson .example {
  background: var(--surface);
  border-left: 3px solid var(--teal);
  border-radius: 0 var(--radius) var(--radius) 0;
  padding: 12px 16px;
  margin: 1.4em 0;
  font-family: var(--sans);
  font-size: 0.95em;
}
#lesson .example-title { font-weight: 600; color: var(--accent); margin-bottom: 4px; }
#lesson .callout {
  border-radius: var(--radius);
  padding: 12px 16px;
  margin: 1.4em 0;
  font-family: var(--sans);
  font-size: 0.95em;
}
#lesson .callout.takeaway {
  background: rgba(46, 158, 106, 0.12);
  border-left: 4px solid var(--correct);
}
#lesson .callout.trap {
  background: rgba(200, 85, 61, 0.12);
  border-left: 4px solid var(--wrong);
}
#lesson input, #lesson select, #lesson textarea {
  font: inherit;
  font-family: var(--sans);
  min-height: 40px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}
#lesson .flash {
  border-radius: 6px;
  animation: flashbg 1.6s ease-out;
}
@keyframes flashbg {
  from { background: rgba(224, 180, 76, 0.4); }
  to { background: transparent; }
}

/* Highlights */
mark.hl {
  color: var(--ink);
  background: transparent;
  border-radius: 3px;
  padding: 0 0.05em;
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
}
mark.hl-yellow { background: var(--hl-yellow); }
mark.hl-green { background: var(--hl-green); }
mark.hl-blue { background: var(--hl-blue); }
mark.hl-pink { background: var(--hl-pink); }
mark.hl-orange { background: var(--hl-orange); }
mark.hl.has-note { box-shadow: inset 0 -2px 0 var(--teal); }
body[data-mode="h"] #lesson ::selection { background: rgba(26, 122, 150, 0.3); }

/* Bookmark button on h2 */
.bm-btn {
  float: right;
  width: 36px;
  height: 36px;
  margin: -6px -6px 0 8px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  display: inline-grid;
  place-items: center;
}
.bm-btn svg { width: 20px; height: 20px; }
.bm-btn.on { color: var(--gold); }
.bm-btn.on path { fill: currentColor; }

/* Ink canvas overlay */
#ink {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 5;
  pointer-events: none;
  touch-action: none;
}
body[data-mode="f"] #ink,
body[data-mode="e"] #ink { pointer-events: auto; cursor: crosshair; }
body[data-mode="e"] #ink { cursor: cell; }

/* Quiz arena (game.js) */
#arena {
  max-width: 68ch;
  margin: 0 auto;
  padding: 0 16px 48px;
}

/* ---------- Dock ---------- */
.dock {
  position: fixed;
  left: 50%;
  bottom: calc(14px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 4px;
  max-width: calc(100vw - 16px);
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  box-shadow: var(--shadow);
}
.dock-btn {
  height: 44px;
  min-width: 44px;
  padding: 0 10px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
}
.dock-btn:hover { background: var(--surface-2); }
.dock-btn.active { background: var(--teal); color: var(--paper); }
.dock-btn svg { width: 20px; height: 20px; flex-shrink: 0; }
.dk { font-weight: 700; }
.dl { font-size: 12px; font-weight: 500; }
.dock-sep { width: 1px; height: 26px; margin: 0 4px; background: var(--line); }
@media (max-width: 519px) {
  .dl { display: none; }
  .dock-btn { min-width: 42px; padding: 0 8px; }
}

/* ---------- Ink bar ---------- */
.ink-bar {
  position: fixed;
  left: 50%;
  bottom: calc(76px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: calc(100vw - 16px);
  padding: 6px 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  box-shadow: var(--shadow);
  white-space: nowrap;
}
.ink-colors { display: flex; gap: 6px; align-items: center; }
.swatch {
  width: 26px;
  height: 26px;
  padding: 0;
  border: 2px solid transparent;
  border-radius: 50%;
  background: var(--c);
  flex-shrink: 0;
}
.swatch[aria-pressed="true"] { outline: 2px solid var(--text); outline-offset: 2px; }
.chip {
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: transparent;
  font-size: 13px;
  color: var(--text);
}
.chip[aria-pressed="true"] { background: var(--teal); border-color: var(--teal); color: var(--paper); }
body[data-mode="e"] .ink-f { display: none; }

/* ---------- Popover ---------- */
.pop {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 50;
  width: min(300px, calc(100vw - 16px));
  padding: 12px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  font-family: var(--sans);
}
.pop-swatches {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-bottom: 10px;
}
.pop-swatches .swatch { width: 32px; height: 32px; }
#pop-note {
  display: block;
  width: 100%;
  min-height: 56px;
  resize: vertical;
  font: inherit;
  font-size: 14px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
}
.pop-actions {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-top: 10px;
}
.btn {
  height: 38px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
}
.btn-primary { background: var(--teal); border-color: var(--teal); color: var(--paper); }
.btn-danger { color: var(--wrong); }

/* ---------- Sheets (notes, bookmarks) ---------- */
.scrim {
  position: fixed;
  inset: 0;
  z-index: 35;
  background: rgba(15, 22, 36, 0.35);
}
.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  max-height: 72vh;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  color: var(--text);
  border-radius: 18px 18px 0 0;
  box-shadow: var(--shadow);
  transform: translateY(105%);
  transition: transform 0.25s ease;
  padding-bottom: env(safe-area-inset-bottom);
}
.sheet.open { transform: none; }
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 8px 8px 16px;
  border-bottom: 1px solid var(--line);
}
.sheet-head h3 { margin: 0; font-size: 16px; font-weight: 700; }
.sheet-body {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 4px 8px 20px;
}
.list { list-style: none; margin: 0; padding: 0; }
.item {
  display: flex;
  align-items: stretch;
  border-bottom: 1px solid var(--line);
}
.item-jump {
  flex: 1;
  min-width: 0;
  padding: 12px 8px;
  border: 0;
  background: transparent;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.item-ctx {
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.item-note { font-size: 15px; line-height: 1.4; }
.item-meta { font-size: 11px; color: var(--muted); }
.item-del {
  padding: 0 14px;
  border: 0;
  background: transparent;
  font-size: 22px;
  color: var(--muted);
}
.empty { padding: 20px 12px; color: var(--muted); font-size: 14px; line-height: 1.5; }

/* ---------- Small screens and iPad ---------- */
@media (min-width: 720px) {
  #lesson { font-size: 18px; padding: 28px 24px 64px; }
  .topbar { padding-left: 24px; padding-right: 20px; }
  .sheet { left: 50%; right: auto; width: min(560px, 100vw); transform: translate(-50%, 105%); border-radius: 18px 18px 0 0; }
  .sheet.open { transform: translate(-50%, 0); }
}
