/* Shell, module map, and the shared furniture every module reuses. */

*, *::before, *::after { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  /* Column layout so an injected site chrome bar sits above the app and the
     app takes the height that is left. See PLAN.md, deployment notes. */
  display: flex;
  flex-direction: column;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: var(--fs-base);
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

#app {
  flex: 1 1 auto;
  min-height: 0;
  height: auto;
  display: flex;
  flex-direction: column;
}

h1, h2, h3, h4 { line-height: 1.25; margin: 0 0 var(--sp-3); font-weight: 650; }
h1 { font-size: var(--fs-2xl); }
h2 { font-size: var(--fs-xl); }
h3 { font-size: var(--fs-lg); }
p { margin: 0 0 var(--sp-3); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.math { font-family: var(--font-math); font-style: italic; }
.math-up { font-family: var(--font-math); font-style: normal; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap;
}

/* ------------------------------------------------------------- header */

.app-header {
  flex: none;
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 0 var(--sp-4);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.app-header .brand {
  font-weight: 700;
  font-size: var(--fs-lg);
  letter-spacing: -0.01em;
  margin-right: auto;
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  background: none;
  border: 0;
  color: inherit;
  cursor: pointer;
  padding: var(--sp-2);
  border-radius: var(--radius-sm);
}
.app-header .brand:hover { background: var(--surface-sunk); }
.app-header .brand .chapter-tag {
  font-size: var(--fs-xs);
  font-weight: 600;
  color: var(--ink-faint);
  background: var(--surface-sunk);
  padding: 2px var(--sp-2);
  border-radius: 999px;
}

.btn {
  font: inherit;
  font-size: var(--fs-sm);
  font-weight: 600;
  padding: var(--sp-2) var(--sp-4);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}
.btn:hover { background: var(--surface-sunk); }
.btn[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: #17498190; background: #174981; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-ghost { border-color: transparent; background: none; }
.btn-ghost:hover { background: var(--surface-sunk); }

/* --------------------------------------------------------- module map */

.module-map {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: var(--sp-6) var(--sp-5) var(--sp-7);
}
.map-inner { max-width: 1100px; margin: 0 auto; }

.map-intro { max-width: 62ch; margin-bottom: var(--sp-6); color: var(--ink-soft); }
.map-intro h1 { color: var(--ink); margin-bottom: var(--sp-2); }

.chapter-heading {
  display: flex; align-items: baseline; gap: var(--sp-3);
  margin: var(--sp-6) 0 var(--sp-4);
  padding-bottom: var(--sp-2);
  border-bottom: 2px solid var(--border);
}
.chapter-heading h2 { margin: 0; }
.chapter-heading .pages { font-size: var(--fs-sm); color: var(--ink-faint); }

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: var(--sp-4);
}

.module-card {
  text-align: left;
  font: inherit;
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  padding: var(--sp-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  cursor: pointer;
  position: relative;
}
.module-card:hover { border-color: var(--accent); transform: translateY(-1px); }
.module-card .card-top { display: flex; align-items: center; gap: var(--sp-2); }
.module-card .sect {
  font-size: var(--fs-xs); font-weight: 700; letter-spacing: 0.03em;
  color: var(--accent); background: var(--accent-soft);
  padding: 2px var(--sp-2); border-radius: 999px;
}
.module-card .star { margin-left: auto; color: var(--highlight); font-size: var(--fs-sm); }
.module-card h3 { margin: 0; font-size: var(--fs-lg); }
.module-card p { margin: 0; font-size: var(--fs-sm); color: var(--ink-soft); }

.progress-bar {
  height: 5px; border-radius: 999px; background: var(--surface-sunk);
  overflow: hidden; margin-top: var(--sp-2);
}
.progress-bar span { display: block; height: 100%; background: var(--ok); border-radius: 999px; }

/* ------------------------------------------------- module shell layout */

.module-view {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.module-bar {
  flex: none;
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.module-bar h2 { margin: 0; font-size: var(--fs-lg); }
.module-bar .spacer { margin-left: auto; }
.module-bar .cite { font-size: var(--fs-xs); color: var(--ink-faint); }

.module-body {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr var(--panel-width);
}
.module-body.no-panel { grid-template-columns: 1fr; }

.stage {
  min-width: 0; min-height: 0;
  display: flex; flex-direction: column;
  padding: var(--sp-4);
  overflow: auto;
}
.stage svg { width: 100%; height: auto; flex: 1 1 auto; min-height: 0; display: block; }

.panel {
  min-height: 0;
  overflow-y: auto;
  padding: var(--sp-4);
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex; flex-direction: column; gap: var(--sp-4);
}

.panel-block { }
.panel-block h3 { font-size: var(--fs-base); margin-bottom: var(--sp-2); }

.prompt {
  background: var(--accent-soft);
  border-left: 3px solid var(--accent);
  padding: var(--sp-3);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-size: var(--fs-sm);
}

.statement-box {
  background: #f3f8e8;
  border: 1px solid #cfe0a8;
  border-radius: var(--radius-sm);
  padding: var(--sp-3);
  font-size: var(--fs-sm);
}
.statement-box strong { display: block; margin-bottom: var(--sp-1); }

/* Syllabus scope warnings — flagged, not buried. */
.scope-note {
  background: var(--nearly-soft);
  border-left: 3px solid var(--nearly);
  padding: var(--sp-3);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-size: var(--fs-sm);
}
.scope-note strong { color: var(--nearly); }

/* Scaffolding that Present mode strips away. */
:root[data-mode="present"] .learn-only { display: none !important; }
:root[data-mode="learn"] .present-only { display: none !important; }

/* ----------------------------------------------- geometry stage styles */

.stage svg { --stroke: var(--figure-stroke); }
.figure-fill { fill: var(--figure-fill); stroke: var(--figure-stroke); stroke-width: var(--geo-stroke); stroke-linejoin: round; }
.figure-fill-2 { fill: var(--figure-fill-2); stroke: var(--figure-stroke-2); stroke-width: var(--geo-stroke); stroke-linejoin: round; }
.figure-line { fill: none; stroke: var(--figure-stroke); stroke-width: var(--geo-stroke); stroke-linecap: round; }
.construction-line { fill: none; stroke: var(--ink-faint); stroke-width: var(--geo-stroke-thin); stroke-dasharray: 5 4; }
.compass-arc { fill: none; stroke: var(--highlight); stroke-width: var(--geo-annotation-stroke); }

.ticks line, .angle-arc path, .right-angle, .parallel-marks polyline {
  stroke: var(--figure-stroke);
  stroke-width: var(--geo-annotation-stroke);
  fill: none;
}
.mark-a line, .mark-a path, .mark-a polyline { stroke: var(--accent); }
.mark-b line, .mark-b path, .mark-b polyline { stroke: var(--highlight); }

.vertex-label { font-family: var(--font-math); font-style: italic; font-size: var(--geo-label-size); fill: var(--ink); }
.side-label, .angle-label, .dimension-label {
  font-family: var(--font-sans); font-size: var(--geo-small-label-size); fill: var(--ink-soft);
}
.dimension line, .dimension polyline { stroke: var(--ink-faint); stroke-width: var(--geo-stroke-thin); fill: none; }

.grid-minor { stroke: #dce6ee; stroke-width: 1; }
.grid-major { stroke: #b9cad8; stroke-width: 1; }

/* Unit squares and unit cubes tiling a figure — the "count why" layer. */
.unit-line { stroke: var(--figure-stroke); stroke-width: 1; opacity: 0.45; }
.solid-fill { fill: var(--figure-fill); stroke: var(--figure-stroke); stroke-width: var(--geo-stroke); stroke-linejoin: round; }
.solid-fill-2 { fill: var(--figure-fill-2); stroke: var(--figure-stroke-2); stroke-width: var(--geo-stroke); stroke-linejoin: round; }
.face-top { filter: brightness(1.06); }
.face-left { filter: brightness(0.88); }
.face-right { filter: brightness(0.97); }

.ratio-table { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
.ratio-table td { padding: var(--sp-2) var(--sp-1); border-bottom: 1px solid var(--border); }
.ratio-table td:first-child { font-family: var(--font-math); color: var(--ink-soft); white-space: nowrap; }
.ratio-table td:last-child { font-weight: 650; text-align: right; }

.handle {
  fill: var(--surface); stroke: var(--accent); stroke-width: 2.5;
  cursor: grab;
}
.handle:hover, .handle.dragging { fill: var(--accent-soft); }
.handle.dragging { cursor: grabbing; }
.handle-hit { fill: transparent; stroke: none; cursor: grab; }

/* -------------------------------------------------------- answer field */

.answer-field { display: flex; flex-direction: column; gap: var(--sp-2); }
.af-row { display: flex; align-items: center; gap: var(--sp-2); flex-wrap: wrap; }
.af-label { font-family: var(--font-math); font-style: italic; font-size: var(--fs-lg); }
.af-input {
  font: inherit; font-family: var(--font-math); font-size: var(--fs-lg);
  padding: var(--sp-2) var(--sp-3);
  border: 2px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--ink);
  width: 9ch; min-width: 0; flex: 1 1 9ch;
}
.af-input:focus { border-color: var(--accent); outline: none; }
.af-input:read-only { background: var(--ok-soft); border-color: var(--ok); }
.af-unit-hint { font-size: var(--fs-base); color: var(--ink-soft); }
.af-submit {
  font: inherit; font-size: var(--fs-sm); font-weight: 600;
  padding: var(--sp-2) var(--sp-4);
  border: 1px solid var(--accent); border-radius: var(--radius-sm);
  background: var(--accent); color: #fff; cursor: pointer;
}
.af-submit:disabled { background: var(--ok); border-color: var(--ok); opacity: 1; cursor: default; }

/* Live typeset preview: stacked fractions and mixed numbers. */
.af-preview { min-height: 2.2em; display: flex; align-items: center; gap: var(--sp-2); }
.af-preview-value {
  display: inline-flex; align-items: center; gap: 0.25em;
  font-family: var(--font-math); font-size: var(--fs-xl); color: var(--ink);
}
.af-sign { margin-right: 0.05em; }
.af-frac { display: inline-flex; flex-direction: column; align-items: center; line-height: 1; }
.af-num, .af-den { font-size: 0.62em; padding: 0 0.25em; }
.af-bar { display: block; width: 100%; height: 1.5px; background: currentColor; margin: 0.1em 0; }
.af-unit { font-family: var(--font-sans); font-size: 0.62em; color: var(--ink-soft); margin-left: 0.15em; }
.af-approx { font-size: var(--fs-sm); color: var(--ink-faint); }

.af-keypad { display: flex; gap: var(--sp-2); }
.af-key {
  font: inherit; font-size: var(--fs-sm); font-family: var(--font-math);
  min-width: 3.2em; padding: var(--sp-1) var(--sp-2);
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface-sunk); cursor: pointer;
}
.af-key:hover { background: var(--surface); border-color: var(--accent); }

.af-feedback { font-size: var(--fs-sm); min-height: 1.4em; }
.af-feedback[data-tone="ok"] { color: var(--ok); font-weight: 600; }
.af-feedback[data-tone="nearly"] { color: var(--nearly); }
.af-feedback[data-tone="no"] { color: var(--no); }
.af-feedback[data-tone="shown"] { color: var(--ink-soft); font-style: italic; }

/* ------------------------------------------------ drag-and-drop pieces */

.draggable-item {
  padding: var(--sp-2) var(--sp-3);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: var(--fs-sm);
  cursor: grab;
  touch-action: none;
  user-select: none;
}
.draggable-item.is-dragging { opacity: 0.35; }
.drag-ghost { box-shadow: var(--shadow-lg); transform: rotate(-1deg); opacity: 0.95; }
.drop-slot {
  min-height: 2.6em;
  border: 2px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-sunk);
  display: flex; align-items: center;
  padding: var(--sp-1);
}
.drop-slot.slot-hover { border-color: var(--accent); background: var(--accent-soft); }
.drop-slot:has(.draggable-item) { border-style: solid; background: var(--surface); }

/* ------------------------------------------------------------ tablets */

@media (max-width: 860px) {
  .module-body { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto; }
  .panel { border-left: 0; border-top: 1px solid var(--border); max-height: 45vh; }
}
