/* ── base ── */
/* HTML hidden attribute must win over any display: flex/grid below */
[hidden] { display: none !important; }

:root {
  --bg: #0e0e10;
  --panel: #18181b;
  --panel-2: #1f1f24;
  --border: #2a2a30;
  --text: #e8e8ec;
  --text-dim: #9a9aa3;
  --accent: #66c2ff;
  --warn: #ffb84d;
  --error: #ff6b6b;
  --ok: #74e792;
}
* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 14px;
  line-height: 1.5;
  height: 100vh;
  overflow: hidden;
}

/* ── topbar ── */
.topbar {
  padding: 10px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.topbar h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}
.topbar .subtitle {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--text-dim);
}
/* Hidden on desktop; shown only inside the mobile media query below. */
.mobile-credits { display: none; }

/* ── layout: left half inputs, right half = top graphics + bottom plot ── */
.layout {
  display: grid;
  /* Compact input panel: ~60% of the previous 40% target, with a generous
     min so the banner + collapse button always fit. The extra width frees
     up the graphics + plot panes. */
  grid-template-columns: minmax(300px, 24%) 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 1px;
  background: var(--border);
  height: calc(100vh - 56px);
}
.inputs {
  grid-row: 1 / 3;
  grid-column: 1;
  background: var(--panel);
  padding: 16px;
  overflow-y: auto;
}
.pane {
  position: relative;
  background: var(--panel);
  padding: 12px;
  overflow: hidden;
}
.graphics { grid-row: 1; grid-column: 2; }
.plot     { grid-row: 2; grid-column: 2; }

/* ── quick-start banner ── */
.panel-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(102, 194, 255, 0.08);
  border: 1px solid rgba(102, 194, 255, 0.25);
  border-radius: 4px;
  padding: 8px 10px;
  margin: 0 0 12px;
  font-size: 12px;
  color: var(--text);
}
.panel-banner > span { flex: 1; }
.panel-banner em { color: var(--accent); font-style: normal; }
.collapse-toggle {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 4px 8px;
  font-size: 11px;
  cursor: pointer;
  white-space: nowrap;
}
.collapse-toggle:hover { filter: brightness(1.15); }

/* ── input groups (collapsible <details>) ── */
.group {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  margin: 0 0 12px;
  background: var(--panel-2);
}
details.group:not([open]) { padding-bottom: 10px; }
details.group > summary {
  cursor: pointer;
  list-style: none;
  font-weight: 600;
  color: var(--accent);
  user-select: none;
  padding: 0;
  margin: 0;
}
details.group[open] > summary { margin-bottom: 10px; }
details.group > summary::-webkit-details-marker { display: none; }
details.group > summary::before {
  content: '▾';
  display: inline-block;
  width: 28px;
  font-size: 25px;
  line-height: 1;
  vertical-align: middle;
  color: var(--text-dim);
}
details.group:not([open]) > summary::before { content: '▸'; }
.group.placeholder { opacity: 0.4; }
.row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 10px;
}
.row:last-child { margin-bottom: 0; }
.row label {
  font-size: 12px;
  color: var(--text-dim);
  font-weight: 500;
}
.row input[type="text"],
.row input[type="number"],
.row input[type="file"],
.row select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 6px 8px;
  font-size: 13px;
  font-family: inherit;
}
.row input[type="file"] { padding: 4px; }
.slider-pair, .seed-pair {
  display: flex;
  gap: 8px;
  align-items: center;
}
.slider-pair input[type="range"] { flex: 1; }
.slider-pair input[type="number"] { width: 70px; }
.seed-pair input[type="number"] { flex: 1; }
.seed-pair button, .simulate {
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: 4px;
  padding: 6px 12px;
  font-weight: 600;
  cursor: pointer;
  font-size: 13px;
}
.seed-pair button:hover, .simulate:hover { filter: brightness(1.1); }
.simulate {
  width: 100%;
  padding: 10px;
  font-size: 14px;
  margin-top: 8px;
}
.simulate:disabled {
  background: var(--border);
  color: var(--text-dim);
  cursor: not-allowed;
}
.sim-hint {
  display: block;
  margin-top: 6px;
  text-align: center;
  font-size: 11px;
}
.sim-hint:empty { display: none; }

/* ── export buttons ── */
.export-btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 8px 10px;
  font-weight: 500;
  cursor: pointer;
  font-size: 13px;
  width: 100%;
  font-family: inherit;
}
.export-btn:hover:not(:disabled) {
  background: var(--border);
  filter: brightness(1.1);
}
.export-btn:disabled {
  color: var(--text-dim);
  cursor: not-allowed;
  opacity: 0.6;
}
.export-row {
  display: flex;
  gap: 8px;
}
.export-row .export-btn { flex: 1; }

/* Randomize: accent-outlined secondary button, sits just above Simulate. */
.randomize-btn {
  margin-top: 6px;
  border-color: var(--accent);
  color: var(--accent);
}
.randomize-btn:disabled {
  border-color: var(--border);
  color: var(--text-dim);
}

/* Echo block groups the "Pac-Men are…" / "Start is…" confirmations. */
.echo-block:empty { display: none; }
.hint {
  color: var(--text-dim);
  font-size: 11px;
}

/* ── echo (e.g. "Pac Man Nodes Are: ...") ── */
.echo {
  margin-top: 8px;
  padding: 6px 8px;
  background: var(--bg);
  border-radius: 3px;
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  color: var(--text);
  word-break: break-all;
}
.echo:empty { display: none; }
.echo-adv { border-left: 3px solid #fc6255; }
.echo-start { border-left: 3px solid #83c167; }
.echo-label { color: var(--text-dim); margin-right: 6px; }

/* ── graphics pane ── */
.pane-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
  gap: 12px;
}
.pane-titles h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}
.pane-titles h3 {
  margin: 2px 0 0;
  font-size: 12px;
  font-weight: 400;
  color: var(--text-dim);
  min-height: 18px;
}
.preview-badge {
  font-size: 11px;
  color: var(--warn);
  background: rgba(255, 184, 77, 0.12);
  border: 1px solid rgba(255, 184, 77, 0.35);
  border-radius: 4px;
  padding: 4px 8px;
  cursor: help;
}
.info-icon { margin-left: 4px; opacity: 0.7; }

#canvas {
  background: #000;
  display: block;
  width: 100%;
  height: calc(100% - 70px);
  border-radius: 4px;
}

/* ── plot pane ── */
.plot h2 {
  margin: 0 0 8px;
  font-size: 14px;
  font-weight: 600;
}
.plot-area {
  width: 100%;
  height: calc(100% - 28px);
  background: #000;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.placeholder-text {
  color: var(--text-dim);
  font-size: 12px;
}

/* ── playback HUD (single-line variant of Manim's t/walkers group) ── */
.hud {
  position: absolute;
  top: 62px;
  left: 22px;
  color: #ffffff;
  font-family: "SF Mono", Menlo, monospace;
  pointer-events: none;
  z-index: 1;
  text-shadow: 0 0 4px #000, 0 0 4px #000;
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 13px;
  white-space: nowrap;
}
.hud-sep { color: #9aa0a6; }

/* ── playback control bar (below canvas) ── */
.player-bar {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
  background: rgba(20, 20, 24, 0.85);
  border: 1px solid var(--border);
  border-radius: 4px;
  z-index: 2;
  backdrop-filter: blur(4px);
}
.player-bar button {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 3px;
  width: 28px;
  height: 26px;
  cursor: pointer;
  font-size: 13px;
  padding: 0;
}
.player-bar button:hover { filter: brightness(1.2); }
.player-speed-label {
  color: var(--text-dim);
  font-size: 11px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.player-speed-label select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 3px 4px;
  font-size: 12px;
}
/* Larger 14px hit-area wrapping a thin 4px visible track, so clicking or
   dragging anywhere near the bar reliably registers. */
.player-progress {
  flex: 1;
  height: 14px;
  display: flex;
  align-items: center;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}
.player-progress-track {
  width: 100%;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
  pointer-events: none;
}
.player-progress-track:hover { height: 5px; }
.player-progress-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
}
.player-time {
  color: var(--text-dim);
  font-size: 11px;
  font-family: "SF Mono", Menlo, monospace;
  min-width: 90px;
  text-align: right;
}

/* ── overlay (hard errors) ── */
.overlay {
  position: absolute;
  inset: 12px;
  top: 70px;
  background: rgba(14, 14, 16, 0.94);
  border: 1px solid var(--error);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.overlay-content {
  text-align: center;
  padding: 20px;
}
.overlay-content strong {
  color: var(--error);
  display: block;
  margin-bottom: 8px;
  font-size: 16px;
}

/* ── warnings (soft) ── */
.warnings {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 12px;
}
.warning {
  background: rgba(255, 184, 77, 0.12);
  border: 1px solid rgba(255, 184, 77, 0.35);
  color: var(--warn);
  padding: 6px 10px;
  border-radius: 4px;
  font-size: 12px;
  margin-top: 4px;
}

/* ───────────────── Mobile (shared) ─────────────────
   Compact-UI rules that apply on both portrait and landscape phones.
   Triggered by EITHER a narrow viewport (portrait) OR a short viewport
   (landscape phones — wider than 768 px but only ~390 px tall). Desktops
   and tablets have > 500 px of height, so they never match the second
   clause, and they're > 768 px wide so they don't match the first. */
@media (max-width: 768px), (orientation: landscape) and (max-height: 500px) {

  /* Topbar: shorter, hide the long subtitle, show the website credits. */
  html, body { height: 100dvh; }
  body { display: flex; flex-direction: column; }
  .topbar { padding: 5px 12px; flex: 0 0 auto; }
  .topbar h1 { font-size: 13px; line-height: 1.2; }
  .topbar .subtitle { display: none; }
  .mobile-credits {
    display: block;
    margin: 1px 0 0;
    font-size: 10px;
    line-height: 1.2;
    color: var(--text-dim);
  }

  .graphics, .plot, .inputs { min-height: 0; }
  .graphics, .plot { overflow: hidden; }

  /* Mobile feature exclusions. */
  .row:has(#upload)       { display: none; }
  .row:has(#export-zip)   { display: none; }
  .row:has(#export-graph) { display: none; }
  #export-plot            { display: none; }
  /* CSV button stretches to fill the row once PNG is gone. */
  .export-row { display: block; }

  /* Inputs: sticky Simulate, scrollable details, banner hidden. */
  .panel-banner { display: none; }
  .inputs {
    padding: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    display: flex;
    flex-direction: column;
  }
  /* Top action area: Randomize sits just above the sticky Simulate, then the
     hint, the "Pac-Men / start" echoes, and finally the collapsed sections. */
  #randomize {
    order: 0;
    margin: 0 8px 6px;
    padding: 12px;
    font-size: 15px;
    touch-action: manipulation;
  }
  #simulate {
    order: 1;
    position: sticky;
    top: 0;
    z-index: 5;
    border-radius: 0;
    margin: 0;
    padding: 14px;
    font-size: 16px;
    touch-action: manipulation;
  }
  .sim-hint     { order: 2; padding: 4px 12px; margin: 0; background: var(--panel); }
  .echo-block   { order: 3; margin: 0 8px; }
  details.group { order: 4; margin: 8px 8px 0; }
  details.group:last-of-type { margin-bottom: 8px; }

  /* Graphics-pane header: tighter and smaller. */
  .preview-badge { display: none; }
  .pane-titles h2 { font-size: 12px; }
  .pane-titles h3 { font-size: 10px; }
  .pane-header { margin-bottom: 4px; }
  .pane { padding: 8px; }

  /* HUD: small enough for the compact pane. Position is layout-specific
     (overridden below in portrait/landscape blocks). */
  .hud { font-size: 10px; gap: 5px; }

  /* Player bar: bigger touch targets, scrubable track that's chunky enough
     to tap reliably with a fingertip. */
  .player-bar {
    padding: 6px 8px;
    gap: 6px;
    bottom: 6px;
    left: 6px;
    right: 6px;
  }
  .player-bar button {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
  .player-speed-label { font-size: 10px; gap: 4px; }
  .player-speed-label select { padding: 4px 6px; font-size: 12px; }
  .player-time { font-size: 10px; min-width: 64px; }
  .player-progress-track { height: 5px; }

  /* Scrubable surfaces: disable default touch behaviour (pan/zoom) so our
     pointermove handler can drive seek instead of the browser scrolling. */
  .player-progress, .plot-area { touch-action: none; }

  /* Input rows: comfortable fingertip target size. */
  .row label { font-size: 12px; }
  .row input, .row select { font-size: 14px; padding: 8px; }
  .seed-pair button { padding: 8px 12px; font-size: 13px; }
  .export-btn { padding: 10px; font-size: 14px; }
}

/* ───────────────── Phone portrait — vertical stack ─────────────────
   The user's familiar layout: graphics on top, plot under it, input
   panel scrolls in the remainder. */
@media (max-width: 768px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: 34dvh 34dvh minmax(0, 1fr);
    flex: 1 1 0;
    min-height: 0;
    height: auto;
    overflow: hidden;
  }
  .graphics { grid-row: 1; grid-column: 1; }
  .plot     { grid-row: 2; grid-column: 1; }
  .inputs   { grid-row: 3; grid-column: 1; }

  /* HUD sits below the two-line pane header. */
  .hud { top: 50px; left: 14px; }
}

/* ───────────────── Phone landscape — 3 columns side-by-side ─────────────────
   Inputs left, graphics middle, plot right. Each pane gets the full
   viewport height (minus the compact topbar). No vertical-stacking is
   sensible here — the viewport is short and wide. */
@media (orientation: landscape) and (max-height: 500px) {
  .layout {
    grid-template-columns: minmax(200px, 26%) 1fr 1fr;
    grid-template-rows: 1fr;
    flex: 1 1 0;
    min-height: 0;
    height: auto;
    overflow: hidden;
  }
  .inputs   { grid-row: 1; grid-column: 1; }
  .graphics { grid-row: 1; grid-column: 2; }
  .plot     { grid-row: 1; grid-column: 3; }

  /* Topbar packs title and credits on one line so it stays a single thin
     row (~26 px). With limited height every pixel saved here is a pixel
     each pane gets. */
  .topbar {
    display: flex;
    align-items: baseline;
    gap: 12px;
    padding: 4px 12px;
  }
  .topbar h1 {
    flex: 1 1 auto; min-width: 0;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    font-size: 12px;
  }
  .mobile-credits { flex: 0 0 auto; margin: 0; font-size: 10px; }

  /* Sticky Simulate is slightly more compact than portrait. */
  #simulate { padding: 10px; font-size: 14px; }
  .sim-hint     { padding: 3px 8px; font-size: 10px; }
  details.group { margin: 6px 6px 0; }

  /* HUD: graphics pane is now tall, so the HUD goes near the top just
     under the small pane header. 44 px was riding on top of the subtitle
     line in landscape — bumped to 70 px so it clears even a wrapped
     two-line subheading. */
  .hud { top: 70px; left: 10px; }
}
