* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0b1220;
  --panel: #11192a;
  --line: #1e293b;
  --text: #e8eef6;
  --muted: #94a3b8;
  --accent: #3b82f6;
  --c0: rgb(59, 130, 246);
  --c1: rgb(249, 115, 22);
  --train: #c4b5fd;
  --test: #2dd4bf;
}

html, body {
  height: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

/* Sized for the site's iframe: about 960 x 860 on desktop. */
#app {
  display: grid;
  height: 100%;
  grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr);
  grid-template-rows: auto auto minmax(0, 1fr) auto auto;
  grid-template-areas:
    "header header"
    "lessons lessons"
    "plot tree"
    "loss controls"
    "footer footer";
}

header {
  grid-area: header;
  padding: 0.7rem 1rem 0.6rem;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
header h1 { font-size: 1.3rem; letter-spacing: -0.01em; }
header p { color: var(--muted); font-size: 0.8rem; margin-top: 0.15rem; }

/* ---- Walkthrough ---- */
#lessons {
  grid-area: lessons;
  padding: 0.55rem 1rem 0.6rem;
  border-bottom: 1px solid var(--line);
  background: #0e1626;
}
.lesson-buttons { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.lesson-buttons button {
  padding: 0.3rem 0.65rem;
  font-size: 0.78rem;
  border-radius: 999px;
  min-height: 30px;
}
.lesson-buttons button[aria-pressed="true"] {
  background: #2563eb; /* darker than --accent so white text passes 4.5:1 */
  border-color: #2563eb;
  color: #fff;
}
#lesson-text {
  margin-top: 0.45rem;
  font-size: 0.84rem;
  line-height: 1.45;
  color: #cbd5e1;
  min-height: 2.9em;
}
#lesson-text b { color: var(--text); }

/* ---- Panels ---- */
.panel {
  min-width: 0;
  min-height: 0;
  padding: 0.6rem 0.8rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  border-bottom: 1px solid var(--line);
}
#plot-panel { grid-area: plot; border-right: 1px solid var(--line); }
#tree-panel { grid-area: tree; }
#loss-panel { grid-area: loss; border-right: 1px solid var(--line); }
#controls { grid-area: controls; }

.panel-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 0.2rem 0.8rem; }
.panel h2 { font-size: 0.88rem; }
.panel-head .sub { font-size: 0.75rem; color: var(--muted); }

.legend { list-style: none; display: flex; flex-wrap: wrap; gap: 0.15rem 0.7rem; font-size: 0.75rem; color: var(--muted); }
.legend li { display: flex; align-items: center; gap: 0.3rem; }
.sw { display: inline-block; width: 9px; height: 9px; border-radius: 50%; }
.sw.c0 { background: var(--c0); }
.sw.c1 { background: var(--c1); }
.sw.ring { border: 1.5px solid var(--muted); }
.sw.cut { width: 16px; height: 0; border-radius: 0; border-top: 2px dashed #f1f5f9; }
.sw.ln-train { width: 16px; height: 0; border-radius: 0; border-top: 2px solid var(--train); }
.sw.ln-test { width: 16px; height: 0; border-radius: 0; border-top: 2px dashed var(--test); }

#plot, #tree {
  display: block;
  width: 100%;
  flex: 1 1 0;
  min-height: 0;
}
#plot { border-radius: 6px; touch-action: none; cursor: crosshair; }
#loss { display: block; width: 100%; height: auto; }

/* ---- Plot marks ---- */
.cut { stroke: #f1f5f9; stroke-width: 1.5; stroke-dasharray: 5 4; opacity: 0.85; }
.cut.active { stroke-width: 3.5; stroke-dasharray: none; opacity: 1; }
.region { stroke: #fff; stroke-width: 1.5; }
.train-pt { stroke: #0b1220; stroke-width: 0.8; }
.train-pt.inside { stroke: #fff; stroke-width: 1.6; }
.test-pt { fill: none; stroke-width: 1.4; }
.axis { fill: var(--muted); font-size: 11px; }

/* ---- Tree ---- */
.edge { stroke: #475569; stroke-width: 1.5; }
.tnode { cursor: pointer; outline: none; }
.tnode .split { fill: #1e293b; stroke: #64748b; stroke-width: 1.2; }
.tnode .leaf { stroke: #0b1220; stroke-width: 1; }
.tnode.active .split, .tnode.active .leaf, .tnode:focus-visible .split, .tnode:focus-visible .leaf { stroke: #fff; stroke-width: 2.2; }
.tlabel { fill: var(--text); font-size: 11px; font-weight: 650; pointer-events: none; font-variant-numeric: tabular-nums; }

#tree { min-height: 150px; }

/* Scrolls rather than squeezing the tree when the frame is narrow. */
#detail {
  flex: 0 1 auto;
  min-height: 7em;
  overflow-y: auto;
  padding: 0.55rem 0.7rem;
  background: #0e1626;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 0.8rem;
  line-height: 1.45;
  color: #cbd5e1;
}
#detail h3 { font-size: 0.82rem; color: var(--text); margin-bottom: 0.25rem; }
#detail p + p { margin-top: 0.3rem; }
#detail .math { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.76rem; color: var(--text); white-space: pre-wrap; }
#detail b { color: #fff; }
#detail .hint { color: var(--muted); font-style: italic; }
.k0 { color: var(--c0); font-weight: 650; }
.k1 { color: var(--c1); font-weight: 650; }

/* ---- Loss ---- */
.gridline { stroke: var(--line); stroke-width: 1; }
.tick { fill: var(--muted); font-size: 10px; }
.ln-train, .ln-test { fill: none; stroke-width: 2; }
.ln-train { stroke: var(--train); }
.ln-test { stroke: var(--test); stroke-dasharray: 5 3; }
.best { fill: none; stroke: var(--test); stroke-width: 2; }
.best-label { fill: var(--test); }
.n-marker { stroke: #f1f5f9; stroke-width: 1; stroke-dasharray: 2 3; }
.dot-train { fill: var(--train); }
.dot-test { fill: var(--test); }
#stats { font-size: 0.78rem; color: var(--muted); font-variant-numeric: tabular-nums; }
#stats b { color: var(--text); }

/* ---- Controls ---- */
#controls {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  align-content: start;
  gap: 0.55rem 0.9rem;
  background: var(--panel);
}
#controls .wide { grid-column: 1 / -1; }
.control { display: flex; flex-direction: column; gap: 0.2rem; }
.control label { display: flex; justify-content: space-between; gap: 0.4rem; font-size: 0.76rem; color: var(--muted); }
.control label span { color: var(--text); font-variant-numeric: tabular-nums; }
.control input[type=range] { width: 100%; accent-color: var(--accent); }
.button-row { display: grid; grid-template-columns: 0.6fr 1.4fr 1fr; gap: 0.4rem; }
.note { font-size: 0.75rem; color: var(--muted); }

button {
  padding: 0.4rem 0.7rem;
  font: inherit;
  font-size: 0.8rem;
  background: #1e293b;
  color: var(--text);
  border: 1px solid #334155;
  border-radius: 6px;
  cursor: pointer;
  min-height: 34px;
  transition: background 0.15s, border-color 0.15s;
}
button:hover { background: #334155; border-color: var(--accent); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

footer {
  grid-area: footer;
  padding: 0.45rem 1rem;
  background: var(--panel);
  border-top: 1px solid var(--line);
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
}

/* ---- Narrow frames (phones): one column that grows to its content ---- */
@media (max-width: 701px) {
  html, body { height: auto; }
  #app {
    height: auto;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: none;
    grid-template-areas: "header" "lessons" "plot" "controls" "tree" "loss" "footer";
  }
  .panel { border-right: none !important; }
  #plot, #tree { flex: none; height: auto; }
  #plot { max-height: 380px; }
  #tree { max-height: 240px; }
  #tree { min-height: 0; }
  #detail { flex: none; min-height: 12.5em; overflow: visible; }
  #lesson-text { min-height: 0; }
}

/* SVG text is in viewBox units, so it shrinks with the frame: enlarge it on
   phones to keep it readable. */
@media (max-width: 480px) {
  .tick { font-size: 17px; }
  .axis { font-size: 17px; }
  .tlabel { font-size: 13px; }
}

/* The narrowest two-column frames (702–900px) also shrink the charts. */
@media (min-width: 702px) and (max-width: 900px) {
  .tick, .axis { font-size: 13.5px; }
  .tlabel { font-size: 12.5px; }
}
