/* ============================================================================
   Neural Lab — Interactive Machine Learning Playground
   Monochrome base + semantic red (Class A / +w) & orange (Class B / -w) accents
   Dark (#0D0D10) & Light (#FBFBFD) synced via /theme.js
   ============================================================================ */

:root {
  --bg: #0d0d10;
  --surface: #141419;
  --card: #18181f;
  --card-hover: #21212b;
  --header-bg: rgba(13, 13, 16, 0.82);
  --border: rgba(255, 255, 255, 0.10);
  --border-strong: rgba(255, 255, 255, 0.20);
  --heading: #f5f5f7;
  --text: #d1d1d6;
  --muted: #8e8e93;
  --inv-bg: #f5f5f7;
  --inv-fg: #0d0d10;
  --accent-red: #ff453a;
  --accent-orange: #ff9f0a;
  --chip-bg: rgba(255, 255, 255, 0.07);
  --radius: 14px;
}

:root[data-theme="light"] {
  --bg: #fbfbfd;
  --surface: #f2f2f6;
  --card: #ffffff;
  --card-hover: #f5f5f7;
  --header-bg: rgba(251, 251, 253, 0.82);
  --border: rgba(0, 0, 0, 0.09);
  --border-strong: rgba(0, 0, 0, 0.18);
  --heading: #1d1d1f;
  --text: #424245;
  --muted: #6e6e73;
  --inv-bg: #1d1d1f;
  --inv-fg: #fbfbfd;
  --accent-red: #d92d20;
  --accent-orange: #ea580c;
  --chip-bg: rgba(0, 0, 0, 0.055);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Inter", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
  transition: background-color 0.2s ease, color 0.2s ease;
}

a { color: inherit; text-decoration: none; }
button, input { font-family: inherit; color: inherit; touch-action: manipulation; }
strong { color: var(--heading); }
code {
  font-family: "SF Mono", "JetBrains Mono", ui-monospace, monospace;
  font-size: 0.88em;
  padding: 0.1em 0.35em;
  border-radius: 5px;
  background: var(--chip-bg);
  color: var(--heading);
}
.mono { font-family: "SF Mono", "JetBrains Mono", ui-monospace, monospace; font-variant-numeric: tabular-nums; }

:focus-visible { outline: 2px solid var(--accent-orange); outline-offset: 2px; border-radius: 4px; }

/* FULL-BLEED STICKY TOPBAR */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  min-height: 50px;
  padding: max(0.6rem, env(safe-area-inset-top, 0px)) max(clamp(1rem, 4vw, 2.5rem), env(safe-area-inset-right, 0px)) 0.6rem max(clamp(1rem, 4vw, 2.5rem), env(safe-area-inset-left, 0px));
  border-bottom: 1px solid var(--border);
  background: var(--header-bg);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  position: sticky;
  top: 0;
  z-index: 30;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 0.7rem; }
.back-link { font-size: 0.8rem; color: var(--muted); transition: color 0.15s; white-space: nowrap; }
.back-link:hover { color: var(--heading); }
.sep { color: var(--border-strong); user-select: none; }
.logo-wrap { display: flex; align-items: center; gap: 0.5rem; color: var(--heading); }
.logo-gem {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--inv-fg);
  background: var(--inv-bg);
}
.tag {
  font-size: 0.68rem;
  letter-spacing: 0.04em;
  color: var(--muted);
  padding: 0.18rem 0.48rem;
  border-radius: 6px;
  background: var(--chip-bg);
}

.icon-btn, .theme-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  height: 32px;
  padding: 0 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  font-size: 0.76rem;
  color: var(--text);
  cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s, color 0.15s;
}
.theme-btn { width: 32px; padding: 0; }
.icon-btn:hover, .theme-btn:hover { background: var(--chip-bg); border-color: var(--border-strong); color: var(--heading); }
.theme-btn svg { width: 15px; height: 15px; }
.theme-btn .icon-sun { display: block; }
.theme-btn .icon-moon { display: none; }
:root[data-theme="light"] .theme-btn .icon-sun { display: none; }
:root[data-theme="light"] .theme-btn .icon-moon { display: block; }

/* WORKBENCH LAYOUT */
.workbench {
  width: 100%;
  max-width: 1120px;
  margin: 0 auto;
  padding: clamp(1rem, 2.6vw, 1.75rem) clamp(1rem, 3vw, 2rem) 2.5rem;
  display: grid;
  grid-template-columns: minmax(0, 1.08fr) minmax(320px, 0.92fr);
  gap: 1.25rem;
  align-items: start;
}

/* LEFT COLUMN: DECISION PLANE */
.plane-col {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.plane-head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.6rem;
}

.dataset-bar, .brush-bar {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.22rem;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.seg-btn, .brush-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.38rem;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 500;
  padding: 0.36rem 0.68rem;
  border-radius: 7px;
  cursor: pointer;
  transition: background-color 0.14s, color 0.14s;
}
.seg-btn:hover, .brush-btn:hover { color: var(--heading); }
.seg-btn.is-active, .brush-btn.is-active {
  background: var(--card);
  color: var(--heading);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}
:root[data-theme="light"] .seg-btn.is-active,
:root[data-theme="light"] .brush-btn.is-active {
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

.swatch {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}
.swatch-a { background: var(--accent-red); }
.swatch-b { background: var(--accent-orange); }

.canvas-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border-strong);
  background: var(--card);
}

#planeCanvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

.hover-readout {
  position: absolute;
  left: 10px;
  bottom: 10px;
  padding: 0.28rem 0.55rem;
  border-radius: 7px;
  font-size: 0.74rem;
  background: var(--header-bg);
  color: var(--heading);
  border: 1px solid var(--border);
  backdrop-filter: blur(10px);
  pointer-events: none;
}

.transport-bar {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.transport-main {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  min-width: 112px;
  height: 38px;
  padding: 0 1rem;
  border-radius: 10px;
  border: 1px solid transparent;
  background: var(--inv-bg);
  color: var(--inv-fg);
  font-size: 0.86rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.14s, transform 0.1s;
}
.btn-primary:hover { opacity: 0.9; }
.btn-primary:active { transform: scale(0.98); }
.btn-primary.is-running {
  background: var(--accent-red);
  color: #ffffff;
}

.btn-secondary {
  height: 38px;
  padding: 0 0.85rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.8rem;
  cursor: pointer;
  transition: background-color 0.14s, border-color 0.14s, color 0.14s;
}
.btn-secondary:hover { background: var(--card-hover); border-color: var(--border-strong); color: var(--heading); }

.plane-hint {
  font-size: 0.72rem;
  color: var(--muted);
}

/* RIGHT COLUMN: INSPECTOR PANELS */
.inspector-col {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.panel {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.05rem;
}

/* TELEMETRY & SPARKLINE */
.metrics-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.65rem;
  margin-bottom: 0.75rem;
}
.metric {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}
.metric-label {
  font-size: 0.74rem;
  color: var(--muted);
}
.metric-val {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--heading);
  letter-spacing: -0.02em;
}

.spark-wrap {
  width: 100%;
  height: 68px;
  border-radius: 9px;
  background: var(--surface);
  border: 1px solid var(--border);
  overflow: hidden;
}
#lossCanvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* NETWORK TOPOLOGY */
.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.65rem;
}
.panel-title-wrap {
  display: flex;
  align-items: baseline;
  gap: 0.55rem;
}
.panel-title-wrap h2 {
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--heading);
}
.arch-summary {
  font-size: 0.75rem;
  color: var(--muted);
}
.layer-actions {
  display: flex;
  gap: 0.35rem;
}
.mini-btn {
  height: 27px;
  padding: 0 0.55rem;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.72rem;
  cursor: pointer;
  transition: background-color 0.14s, border-color 0.14s;
}
.mini-btn:hover:not(:disabled) { background: var(--card-hover); border-color: var(--border-strong); color: var(--heading); }
.mini-btn:disabled { opacity: 0.38; cursor: not-allowed; }

.net-svg {
  display: block;
  width: 100%;
  height: 210px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.neuron-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  margin-top: 0.65rem;
}
.layer-stepper {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.2rem 0.42rem;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 0.73rem;
}
.layer-stepper span { color: var(--muted); }
.step-btn {
  width: 22px;
  height: 22px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--heading);
  font-size: 0.8rem;
  line-height: 1;
  cursor: pointer;
}
.step-btn:hover:not(:disabled) { border-color: var(--border-strong); }
.step-btn:disabled { opacity: 0.35; cursor: not-allowed; }

.weight-legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.9rem;
  margin-top: 0.65rem;
  font-size: 0.7rem;
  color: var(--muted);
}
.weight-legend span { display: inline-flex; align-items: center; gap: 0.35rem; }
.line-swatch {
  display: inline-block;
  width: 14px;
  height: 2.5px;
  border-radius: 2px;
}
.line-swatch.pos { background: var(--accent-red); }
.line-swatch.neg { background: var(--accent-orange); }

/* HYPERPARAMETERS */
.params-panel {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}
.param-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
}
.param-label {
  font-size: 0.82rem;
  color: var(--text);
  white-space: nowrap;
}
.muted-val { color: var(--muted); margin-left: 0.25rem; font-size: 0.76rem; }

.pill-group {
  display: inline-flex;
  gap: 0.2rem;
  padding: 0.2rem;
  border-radius: 9px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.pill-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.74rem;
  padding: 0.28rem 0.55rem;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.14s, color 0.14s;
}
.pill-btn:hover { color: var(--heading); }
.pill-btn.is-active {
  background: var(--inv-bg);
  color: var(--inv-fg);
  font-weight: 600;
}

input[type="range"] {
  flex: 1;
  max-width: 190px;
  accent-color: var(--accent-orange);
  cursor: pointer;
}

/* MODAL */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.58);
  backdrop-filter: blur(6px);
  display: grid;
  place-items: center;
  padding: 1rem;
}
.modal-backdrop[hidden] { display: none; }
.modal-card {
  width: 100%;
  max-width: 480px;
  background: var(--card);
  border: 1px solid var(--border-strong);
  border-radius: 16px;
  padding: 1.25rem 1.35rem;
}
.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.85rem;
}
.modal-head h2 { font-size: 1rem; color: var(--heading); }
.modal-body {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  font-size: 0.88rem;
  line-height: 1.55;
  color: var(--text);
}
.guide-list {
  padding-left: 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.guide-tip { font-size: 0.83rem; color: var(--muted); }

/* RESPONSIVE */
@media (max-width: 860px) {
  .workbench {
    grid-template-columns: 1fr;
    max-width: 620px;
  }
  .tag { display: none; }
}

@media (max-width: 480px) {
  .topbar { padding-left: 0.85rem; padding-right: 0.85rem; }
  .back-link { font-size: 0.74rem; }
  .param-row { flex-wrap: wrap; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; }
}
