/* ==========================================================================
   Tokens. Colors are defined once here; viz.js reads the --heat-* and
   --point-* values to paint the canvases, so they must stay hex colors.
   ========================================================================== */
:root {
  color-scheme: light;
  --bg: #f4f5f7;
  --panel: #ffffff;
  --fg: #111827;
  --muted: #4b5563;
  --border: #d5d9e0;
  --hover: #eef1f5;
  --focus: #7c3aed;

  /* Weight sign: blue positive, orange negative */
  --pos: #2563eb;
  --neg: #ea580c;

  /* Heatmaps: orange (−1) → neutral (0) → blue (+1) */
  --heat-neg: #f4a06a;
  --heat-mid: #f7f8fa;
  --heat-pos: #6e9cf0;

  /* Data points */
  --point-pos: #1d4ed8;
  --point-neg: #c2410c;
  --point-ring: #ffffff;
  --point-test-ring: #111827;

  /* Loss chart */
  --line-train: #111827;
  --line-test: #7c3aed;

  --alert-bg: #fef2f2;
  --alert-fg: #991b1b;
  --alert-border: #fca5a5;

  --radius: 10px;
  --node: 42px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0e1015;
    --panel: #171a21;
    --fg: #e8eaee;
    --muted: #a3a9b5;
    --border: #2e333e;
    --hover: #222631;
    --focus: #c4a5ff;
    --pos: #60a5fa;
    --neg: #fb923c;
    --heat-neg: #a8501c;
    --heat-mid: #1c1f27;
    --heat-pos: #2d5cb0;
    --point-pos: #93c5fd;
    --point-neg: #fdba74;
    --point-ring: #0e1015;
    --point-test-ring: #ffffff;
    --line-train: #e8eaee;
    --line-test: #c4a5ff;
    --alert-bg: #3b1214;
    --alert-fg: #fecaca;
    --alert-border: #7f1d1d;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e1015;
  --panel: #171a21;
  --fg: #e8eaee;
  --muted: #a3a9b5;
  --border: #2e333e;
  --hover: #222631;
  --focus: #c4a5ff;
  --pos: #60a5fa;
  --neg: #fb923c;
  --heat-neg: #a8501c;
  --heat-mid: #1c1f27;
  --heat-pos: #2d5cb0;
  --point-pos: #93c5fd;
  --point-neg: #fdba74;
  --point-ring: #0e1015;
  --point-test-ring: #ffffff;
  --line-train: #e8eaee;
  --line-test: #c4a5ff;
  --alert-bg: #3b1214;
  --alert-fg: #fecaca;
  --alert-border: #7f1d1d;
}

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

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow-x: hidden;
}

h1, h2, h3 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.05rem; font-weight: 650; }
h3 { font-size: 0.95rem; font-weight: 650; }

button, select, input { font: inherit; color: inherit; }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

kbd {
  font: 600 0.8em/1 ui-monospace, SFMono-Regular, Consolas, monospace;
  padding: 2px 6px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--panel);
}

svg { display: block; }

/* ==========================================================================
   Top bar
   ========================================================================== */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 28px;
  padding: 12px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.brand h1 { font-size: 1.25rem; font-weight: 700; letter-spacing: -0.01em; }
.hint { margin: 2px 0 0; font-size: 0.85rem; color: var(--muted); }

.transport { display: flex; align-items: center; gap: 10px; }

.icon-btn, .play-btn, .theme-btn, .small-btn {
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--border);
  background: var(--panel);
  cursor: pointer;
  border-radius: 999px;
  transition: background-color 120ms, border-color 120ms, transform 80ms;
}
.icon-btn:hover, .theme-btn:hover, .small-btn:hover:not(:disabled) { background: var(--hover); }
.icon-btn:active, .play-btn:active, .small-btn:active:not(:disabled) { transform: scale(0.95); }

.icon-btn { width: 44px; height: 44px; }
.icon-btn svg, .theme-btn svg {
  width: 22px; height: 22px;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
#step svg { fill: currentColor; stroke: none; }

.play-btn {
  width: 60px; height: 60px;
  background: var(--fg);
  color: var(--bg);
  border-color: var(--fg);
}
.play-btn svg { width: 28px; height: 28px; fill: currentColor; }
.play-btn .icon-pause { display: none; }
.play-btn.playing .icon-play { display: none; }
.play-btn.playing .icon-pause { display: block; }
.play-btn:disabled { opacity: 0.35; cursor: not-allowed; }

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 28px;
  margin: 0;
}
.stats dt { font-size: 0.8rem; color: var(--muted); }
.stats dd {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  min-width: 4.5ch;
}

.theme-btn { width: 44px; height: 44px; margin-left: auto; }
.theme-btn .icon-sun { display: none; }
:root[data-theme="dark"] .theme-btn .icon-sun { display: block; }
:root[data-theme="dark"] .theme-btn .icon-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-btn .icon-sun { display: block; }
  :root:not([data-theme="light"]) .theme-btn .icon-moon { display: none; }
}

/* ==========================================================================
   Hyperparameter row
   ========================================================================== */
.hyper {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 24px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field label {
  font-size: 0.8rem;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.field output { color: var(--fg); font-weight: 600; font-variant-numeric: tabular-nums; }

select {
  min-height: 40px;
  padding: 6px 32px 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5l5 5 5-5' fill='none' stroke='%23888' stroke-width='1.6'/%3E%3C/svg%3E")
    no-repeat right 10px center / 11px;
  appearance: none;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
}
select:disabled { opacity: 0.5; cursor: not-allowed; }

.range-field { min-width: 160px; }
input[type="range"] {
  width: 100%;
  min-height: 32px;
  accent-color: var(--fg);
  cursor: pointer;
  margin: 0;
}

/* ==========================================================================
   Status + alert
   ========================================================================== */
.status-row {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 24px;
  align-items: baseline;
  padding: 12px 16px 0;
}
.status { margin: 0; font-size: 1.05rem; font-weight: 500; }
.notice { margin: 0; font-size: 0.9rem; color: var(--neg); font-weight: 600; }

.alert {
  margin: 12px 16px 0;
  padding: 12px 16px;
  border: 1px solid var(--alert-border);
  border-radius: var(--radius);
  background: var(--alert-bg);
  color: var(--alert-fg);
  font-weight: 500;
}
.alert strong { display: block; margin-bottom: 2px; }

/* ==========================================================================
   Workspace layout
   ========================================================================== */
.workspace {
  display: grid;
  gap: 16px;
  padding: 16px;
  grid-template-columns: 220px minmax(0, 1fr) 360px;
  grid-template-areas: "data net out";
  align-items: start;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  min-width: 0;
}
.data-panel { grid-area: data; display: flex; flex-direction: column; gap: 16px; }
.network-panel { grid-area: net; }
.output-panel { grid-area: out; }

.panel-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 16px; }
.panel-note { margin: 6px 0 0; font-size: 0.82rem; color: var(--muted); }

@media (max-width: 1180px) {
  .workspace {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-areas: "net net" "data out";
  }
}
@media (max-width: 720px) {
  .workspace {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "data" "net" "out";
    padding: 12px 16px;
  }
  .topbar { gap: 12px 16px; position: relative; }
  .brand { padding-right: 52px; }
  .stats { gap: 6px 18px; }
  .stats dd { font-size: 1.2rem; }
  .theme-btn { position: absolute; top: 12px; right: 16px; }
  .icon-btn { width: 40px; height: 40px; }
  .play-btn { width: 52px; height: 52px; }
  .hyper .field { flex: 1 1 140px; }
}

/* ==========================================================================
   Data panel
   ========================================================================== */
.datasets { border: 0; padding: 0; margin: 0; min-width: 0; }
.datasets legend { font-size: 0.8rem; color: var(--muted); padding: 0; margin-bottom: 6px; }
.dataset-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
@media (max-width: 1180px) {
  .dataset-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

.dataset-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 6px;
  border: 2px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  cursor: pointer;
  font-size: 0.78rem;
  color: var(--muted);
}
.dataset-btn canvas { width: 100%; aspect-ratio: 1; border-radius: 4px; display: block; }
.dataset-btn:hover { background: var(--hover); }
.dataset-btn[aria-pressed="true"] { border-color: var(--fg); color: var(--fg); font-weight: 600; }

/* ==========================================================================
   Network diagram
   ========================================================================== */
.layer-ctl { display: flex; align-items: center; gap: 10px; font-weight: 600; font-variant-numeric: tabular-nums; }
.small-btn { width: 32px; height: 32px; font-size: 1.15rem; line-height: 1; padding: 0; }
.small-btn:disabled { opacity: 0.3; cursor: not-allowed; }

.diagram-scroll {
  margin-top: 8px;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
}
.diagram { position: relative; }

.links {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
}
.link { fill: none; stroke-linecap: round; }
.link.pos { stroke: var(--pos); }
.link.neg { stroke: var(--neg); }
.link.hl { stroke-opacity: 1 !important; filter: drop-shadow(0 0 2px var(--panel)); }
.link-hit {
  fill: none;
  stroke: transparent;
  stroke-width: 10;
  pointer-events: stroke;
  cursor: help;
}

.node {
  position: absolute;
  width: var(--node);
  height: var(--node);
  padding: 0;
  border: 0;
  border-radius: 7px;
  background: var(--heat-mid);
  box-shadow: 0 0 0 1.5px var(--border);
  cursor: default;
}
.node canvas { width: 100%; height: 100%; display: block; border-radius: 7px; }
.node:hover, .node.hovered { box-shadow: 0 0 0 2.5px var(--fg); z-index: 2; }
.node:focus-visible { outline-offset: 3px; }

.node.input { cursor: pointer; }
.node.input.off { box-shadow: none; opacity: 0.4; }
.node.input.off:not(:focus-visible) { outline: 1.5px dashed var(--muted); outline-offset: 0; }
.node.input.off canvas { filter: grayscale(1); }
.node.input.off:hover { opacity: 0.75; }

.node-label {
  position: absolute;
  right: calc(100% + 8px);
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
  pointer-events: none;
}
.node.input.off .node-label { font-weight: 400; }

.col-head {
  position: absolute;
  top: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transform: translateX(-50%);
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
}
.col-head .count { font-weight: 600; color: var(--fg); min-width: 1.2ch; text-align: center; }
.col-ctl { display: flex; align-items: center; gap: 4px; }
.col-ctl .small-btn { width: 28px; height: 28px; font-size: 1rem; }

/* Formation animation: neurons pop in column by column, links draw in. */
@keyframes node-in {
  from { opacity: 0; transform: scale(0.3); }
  to { opacity: 1; transform: none; }
}
.node.forming { animation: node-in 340ms cubic-bezier(0.2, 0.9, 0.3, 1.25) both; animation-delay: var(--delay); }
.node.input.off.forming { animation-name: node-in-dim; }
@keyframes node-in-dim {
  from { opacity: 0; transform: scale(0.3); }
  to { opacity: 0.4; transform: none; }
}
.col-head.forming { animation: fade-in 300ms ease-out both; animation-delay: var(--delay); }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

@keyframes link-draw { to { stroke-dashoffset: 0; } }
.link.forming {
  stroke-dasharray: var(--len);
  stroke-dashoffset: var(--len);
  animation: link-draw 420ms ease-out forwards;
  animation-delay: var(--delay);
}

@media (prefers-reduced-motion: reduce) {
  .node.forming, .col-head.forming, .link.forming { animation: none; stroke-dasharray: none; stroke-dashoffset: 0; }
  .icon-btn, .play-btn, .small-btn { transition: none; }
}

.link-legend, .point-legend, .loss-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 16px;
  margin-top: 10px;
  font-size: 0.8rem;
  color: var(--muted);
}
.swatch { display: inline-block; width: 18px; height: 4px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }
.swatch.pos { background: var(--pos); }
.swatch.neg { background: var(--neg); }

/* ==========================================================================
   Output panel
   ========================================================================== */
.plot-caption { margin: 2px 0 10px; font-size: 0.85rem; color: var(--muted); min-height: 1.3em; }
.plot-caption strong { color: var(--fg); }

.plot-wrap {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 4px 6px;
}
.plot {
  grid-column: 2;
  grid-row: 1;
  position: relative;
  aspect-ratio: 1;
  width: 100%;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 0 0 1px var(--border);
  background: var(--heat-mid);
}
.plot canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#heat { image-rendering: auto; }

.axis { display: flex; justify-content: space-between; font-size: 0.72rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.axis-x { grid-column: 2; grid-row: 2; }
.axis-y { grid-column: 1; grid-row: 1; flex-direction: column; align-items: flex-end; }

.heat-legend { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 0.75rem; color: var(--muted); }
.heat-bar {
  flex: 1;
  height: 8px;
  border-radius: 4px;
  background: linear-gradient(to right, var(--heat-neg), var(--heat-mid), var(--heat-pos));
  box-shadow: 0 0 0 1px var(--border);
}

.checks { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 12px; }
.checks label { display: flex; align-items: center; gap: 8px; cursor: pointer; min-height: 32px; font-size: 0.9rem; }
.checks input { width: 18px; height: 18px; accent-color: var(--fg); margin: 0; }

.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: -1px; }
.dot.pos { background: var(--point-pos); }
.dot.neg { background: var(--point-neg); }
.dot.test { background: var(--muted); box-shadow: 0 0 0 2px var(--point-test-ring); }

.loss-heading { margin-top: 20px; }
.loss-chart { width: 100%; height: 120px; display: block; margin-top: 8px; }
.line { display: inline-block; width: 18px; height: 0; border-top: 2.5px solid; margin-right: 6px; vertical-align: middle; }
.line.train { border-color: var(--line-train); }
.line.test { border-color: var(--line-test); border-top-style: dashed; }

/* ==========================================================================
   Tooltip
   ========================================================================== */
.tooltip {
  position: fixed;
  z-index: 50;
  left: 0;
  top: 0;
  padding: 6px 10px;
  border-radius: 6px;
  background: var(--fg);
  color: var(--bg);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
  white-space: nowrap;
  box-shadow: 0 4px 16px rgb(0 0 0 / 0.2);
}
.tooltip .muted { opacity: 0.75; }
