/* All styling for the Animator app. The app's Gleam code attaches class names
   and sets --side-width inline; browser_ffi.mjs sets inline styles on the side
   panel only while it's being dragged or settling. */

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { width: 100%; height: 100%; overflow: hidden; }
button, select, input { font: inherit; }

/* ── Design tokens ──────────────────────────────────────────────────────── */

:root {
  /* Colours */
  --app-bg: #1a1a2e;
  --panel-bg: #0f0f1e;
  --header-bg: #1a1a3e;
  --editor-bg: #0d0d1a;
  --stage-bg: #0a0a14;
  --text: #d4c5a0;
  --text-muted: #b8a888;
  --heading: #8a8aca;
  --line: #3a3a5e;
  --control-bg: #2a2a4a;
  --control-border: #4a4a8a;
  --accent: #7ab8ff;

  /* Motion */
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --slide: 280ms; /* the panel sliding; browser_ffi.mjs waits SETTLE_MS for it */
  --quick: 150ms; /* hovers and small fades */
  --unfold: 240ms; /* sections opening and closing */

  /* Spacing */
  --panel-pad: 14px; /* the side panel's inner left and right padding */
}

/* ── Layout: side panel and animation ───────────────────────────────────── */

.app {
  /* --side-width is the width the user dragged to (set inline by the app); keep
     it usable whatever the window size. 260px and 70vw match min_width in
     side_panel.gleam and MAX_FRACTION in browser_ffi.mjs. */
  --side-actual: clamp(260px, var(--side-width, 420px), 70vw);
  color-scheme: dark;
  display: flex;
  overflow: hidden;
  width: 100vw;
  height: 100vh;
  background: var(--app-bg);
  color: var(--text);
  font-family: system-ui, sans-serif;
  font-size: 14px;
}

/* Left column: the side panel (drawing, code, built-ins). */
.code-col {
  width: var(--side-actual);
  flex-shrink: 0;
  margin-left: 0;
  transition: margin-left var(--slide) var(--ease-out), width var(--slide) var(--ease-out),
    opacity var(--quick) ease-out, visibility 0s;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--line);
  background: var(--panel-bg);
  /* The side tab hangs off its right edge, over the animation. */
  position: relative;
  z-index: 5;
}

/* The side panel stays in the DOM when collapsed, so CodeMirror keeps its
   state. It slides off to the left, and the animation area widens to fill the
   space as it goes, so the drawing glides across rather than jumping. It
   becomes invisible only once it has finished sliding. */
.app.code-hidden .code-col {
  margin-left: calc(-1 * var(--side-actual));
  visibility: hidden;
  transition: margin-left var(--slide) var(--ease-out), visibility 0s linear var(--slide);
}

/* While the seam or the side tab is being dragged, the panel follows the
   pointer directly (no easing), nothing gets text-selected, and the resize
   cursor stays on wherever the pointer wanders. */
.app.side-dragging .code-col { transition: none; }
.app.side-dragging,
.app.side-dragging * { cursor: col-resize !important; user-select: none; }
/* Dragged past the minimum: releasing now will collapse the panel. */
.app.side-collapsing .code-col { opacity: 0.55; }

/* The seam between the panel and the animation (a "sash", as VS Code calls
   it): a 10px grab area centred on the border, which lights up with a thin
   accent line after a short hover, or at once while dragging. */
.sash {
  position: absolute;
  top: 0;
  bottom: 0;
  right: -5px;
  width: 10px;
  z-index: 8;
  cursor: col-resize;
  touch-action: none;
  user-select: none;
}
.sash::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 4px;
  width: 3px;
  background: var(--accent);
  opacity: 0;
  transition: opacity var(--quick) ease-out 0s;
}
.sash:hover::after { opacity: 1; transition-delay: var(--slide); }
.sash:focus-visible { outline: none; }
.sash:focus-visible::after,
.app.side-dragging .sash::after { opacity: 1; transition-delay: 0s; }
.app.side-collapsing .sash::after { background: #ff8a9a; }
/* With the panel shut, the seam sits on the window's left edge, and the whole
   strip (top to bottom) can be dragged to pull the panel back out. */
.app.code-hidden .sash { visibility: visible; right: -8px; width: 8px; }
.app.code-hidden .sash::after { left: 0; }
/* A brief glow when the width clicks onto a snap point. */
.sash.snapped::after { animation: snap-flash 260ms ease-out; }
@keyframes snap-flash {
  0% { background: #ffffff; box-shadow: 0 0 10px 2px var(--accent); }
}

.icon { width: 16px; height: 16px; flex-shrink: 0; }

/* A small square button, like the Code section's fullscreen button. */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 36px;
  background: var(--control-bg);
  color: var(--text);
  border: 1px solid var(--control-border);
  border-radius: 4px;
  cursor: pointer;
  transition: background-color var(--quick) ease-out;
}
.icon-btn:hover { background: var(--line); }
.icon-btn.small { width: 30px; height: 26px; }

/* ── Side panel ─────────────────────────────────────────────────────────── */

.side-header {
  display: flex;
  align-items: center;
  padding: 10px var(--panel-pad);
  background: var(--header-bg);
  border-bottom: 1px solid var(--line);
}
.side-title {
  color: #e8d8b0;
  font-family: Georgia, serif;
  font-size: 16px;
  letter-spacing: 1px;
}
/* The panel's one scroller: the editor grows to fit the code rather than
   scrolling inside it, so there's never a scrollbar within a scrollbar. */
.side-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
}

/* Collapsible sections: Drawing, Variables, Code, Built-ins reference. They
   grow and shrink to their height instead of snapping, where the browser
   supports animating <details> (Chrome and Edge 131+). */
.section {
  border-bottom: 1px solid var(--line);
  interpolate-size: allow-keywords;
}
.section::details-content {
  block-size: 0;
  overflow: hidden;
  transition: block-size var(--unfold) var(--ease-out),
    content-visibility var(--unfold) allow-discrete;
}
.section[open]::details-content { block-size: auto; }
.section-summary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px var(--panel-pad);
  color: var(--heading);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  cursor: pointer;
  list-style: none;
  user-select: none;
  /* Section headings stay at the top of the panel while you scroll through
     their section, so you always know where you are. */
  position: sticky;
  top: 0;
  z-index: 3;
  background: var(--panel-bg);
}
.section-summary::-webkit-details-marker { display: none; }
.section-title { flex: 1; }
.section-summary:hover { color: #b0b0ea; }
.section-caret { transition: transform var(--unfold) var(--ease-out); }
.section:not([open]) .section-caret { transform: rotate(-90deg); }
.section-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 0 var(--panel-pad) var(--panel-pad);
}

.drawing-select,
.template-select {
  height: 36px;
  padding: 0 8px;
  background: var(--control-bg);
  color: var(--text);
  border: 1px solid var(--control-border);
  border-radius: 4px;
  cursor: pointer;
}
/* The Templates menu reads as a secondary action under the drawing menu. */
.template-select { height: 30px; color: var(--text-muted); font-size: 13px; }

.slider-row {
  display: flex;
  align-items: center;
  gap: 10px;
}
.slider-row label { width: 72px; color: var(--text-muted); }
/* The Step limit drop-down, under the Iterations slider, with its hint. */
.step-select {
  flex: 1;
  padding: 4px 8px;
  background: var(--control-bg);
  color: var(--text);
  border: 1px solid var(--control-border);
  border-radius: 6px;
  font-size: 13px;
}
.step-hint {
  margin: 4px 0 0 82px;
  color: var(--text-muted);
  font-size: 11px;
}
.slider-row input { flex: 1; }
input[type=range] { accent-color: var(--accent); cursor: pointer; }
/* A slider variable's row: its name (shortened if long), the slider, the
   value, and the button that animates it. */
.variable-row label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 13px;
}

/* Live numbers: holding Alt over a number in the code outlines it, with a
   hint, and the cursor shows it can be dragged sideways. */
.live-number {
  position: fixed;
  z-index: 100;
  border: 1px solid var(--accent);
  border-radius: 3px;
  background: rgba(122, 184, 255, 0.15);
  pointer-events: none;
}
.live-number::after {
  content: attr(data-hint);
  position: absolute;
  left: 0;
  bottom: calc(100% + 4px);
  padding: 2px 6px;
  background: var(--header-bg);
  border: 1px solid var(--control-border);
  border-radius: 4px;
  color: var(--text);
  font: 11px system-ui, sans-serif;
  white-space: nowrap;
}
html[data-live-number] .cm-content { cursor: ew-resize; }
.readout {
  min-width: 32px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* The provided editor panel (editor.view_panel) inside the Code section: only
   its editor shows; the side panel supplies the heading, the hide control and
   the Built-ins section. (The panel itself must stay: Gradescope uses it.) */
.code-panel > .code-header,
.code-panel > .reference { display: none; }

/* CodeMirror mount node. It grows with the code; a short program still gets
   most of the panel to type in. It spans the section's full width. */
.code-editor {
  /* The least room the editor takes, however short the code. */
  --editor-min-height: max(240px, calc(100vh - 300px));
  margin: 0 calc(-1 * var(--panel-pad));
  display: flex;
  background: var(--editor-bg);
}
.code-editor .cm-editor { width: 100%; }
/* REQUIRED BY THE GRADESCOPE AUTOGRADER, DON'T REMOVE: CodeMirror's own text
   area (and its line-number gutter) must fill the whole editor box, as
   CodeMirror recommends for an editor that grows with its code. Otherwise,
   with short code, the space below the text belongs to the editor but not to
   its text area: clicking there doesn't focus the editor, so whatever is typed
   next goes nowhere. The autograder clicks the middle of the editor before
   each edit, and without this rule it lost every edit after the first (Q9 and
   Q11 failed). The audit's "Click middle of …" checks guard this. */
.code-editor .cm-content,
.code-editor .cm-gutter { min-height: var(--editor-min-height); }

/* Why the code doesn't draw, under the editor. */
.error {
  animation: fade-up 200ms var(--ease-out);
  padding: 10px 12px;
  background: #3a1620;
  color: #ffb4be;
  border: 1px solid #7a2a3a;
  border-radius: 4px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 13px;
  white-space: pre-wrap;
  word-break: break-word;
}

/* The built-ins reference. */
.reference-group { margin-top: 4px; }
.reference-group + .reference-group { margin-top: 10px; }
.reference-group-title {
  color: var(--heading);
  font-family: monospace;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 4px;
}
.reference-row {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 10px;
  padding: 1px 0;
}
.reference-sig {
  color: #89ddff;
  font-family: monospace;
  font-size: 12px;
  white-space: pre-wrap;
}
.reference-desc { color: #8a9aaa; font-size: 11px; }

/* ── Animation area (right column) ──────────────────────────────────────── */

.canvas-area {
  flex: 1;
  min-width: 0;
  position: relative;
  display: flex;
  background: var(--stage-bg);
}

/* The canvas fills the whole stage beside the code (or the screen, in
   fullscreen), so panning and zooming out reveal everything a drawing
   reaches. canvas.mjs fits the drawing's square world, unstretched, into the
   part above the dock: --dock-space is how much room the dock takes. */
.canvas-stage {
  flex: 1;
  min-width: 0;
  display: flex;
}
.canvas {
  --dock-space: 112px;
  display: block;
  width: 100%;
  height: 100%;
}
/* While the code is broken, the last drawing that worked stays up, faded, so a
   typo doesn't make the drawing vanish. */
.canvas {
  transition: opacity 300ms var(--ease-out);
  /* Drag to pan, scroll or pinch to zoom (explorer_ffi.mjs). */
  cursor: grab;
  touch-action: none;
}
.canvas[data-panning] { cursor: grabbing; }
.canvas.stale { opacity: 0.3; }

/* The one tab that hides and shows the panel, vertically centred on the seam.
   It hangs just past the panel's right border, so it travels with the panel's
   edge and ends up on the window's left edge when the panel is shut, where it
   stays visible. Only its arrow turns. */
.side-tab {
  position: absolute;
  top: calc(50% - 90px); /* half its height */
  left: calc(100% + 1px);
  z-index: 10;
  width: 44px;
  height: 180px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: var(--header-bg);
  color: var(--text);
  border: 1px solid var(--control-border);
  border-left: none;
  border-radius: 0 10px 10px 0;
  cursor: pointer;
  user-select: none;
  touch-action: none;
  transition: background-color var(--quick) ease-out;
}
.side-tab:hover { background: var(--control-bg); }
.app.code-hidden .side-tab { visibility: visible; }
.edge-label {
  writing-mode: vertical-rl;
  font-size: 13px;
  letter-spacing: 0.04em;
}
.edge-chevron { display: inline-flex; transition: transform var(--slide) var(--ease-out); }
.app.code-hidden .edge-chevron { transform: rotate(180deg); }

/* The playback controls floating at the bottom centre of the animation. */
.dock {
  position: absolute;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  padding: 8px var(--panel-pad);
  background: rgba(26, 26, 46, 0.92);
  border: 1px solid var(--line);
  border-radius: 24px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  white-space: nowrap;
}
/* Tucked away: it slides down out of sight (staying in the page, so its
   controls keep their state), and the Controls tab below brings it back. */
.dock {
  transition: transform var(--slide) var(--ease-out), opacity var(--slide) var(--ease-out);
}
.dock.tucked {
  transform: translate(-50%, calc(100% + 40px));
  opacity: 0;
  pointer-events: none;
}
/* The hide chevron, on the dock's top edge. */
.dock-hide {
  position: absolute;
  top: -11px;
  left: 50%;
  transform: translateX(-50%);
  width: 30px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  background: rgba(26, 26, 46, 0.95);
  border: 1px solid var(--line);
  border-radius: 11px;
  color: var(--text-muted);
  cursor: pointer;
}
.dock-hide:hover { color: var(--text); background: var(--control-bg); }
.dock-hide svg, .dock-tab svg { width: 14px; height: 14px; }
.dock-tab {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translate(-50%, 12px);
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  background: rgba(26, 26, 46, 0.92);
  border: 1px solid var(--line);
  border-radius: 16px;
  color: var(--text-muted);
  font: 12px system-ui, sans-serif;
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: transform var(--slide) var(--ease-out), opacity var(--slide) var(--ease-out);
}
.dock-tab.shown { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.dock-tab:hover { color: var(--text); background: var(--control-bg); }

/* The variables floating over the top left of the canvas while the side
   panel's Variables section is out of sight. It clears the panel's edge tab on
   the left and stops above the dock. */
.variables-card {
  position: absolute;
  top: 14px;
  left: 60px;
  z-index: 11;
  width: 300px;
  max-width: calc(100% - 80px);
  max-height: calc(100% - var(--dock-space, 112px) - 40px);
  overflow: auto;
  background: rgba(26, 26, 46, 0.82);
  backdrop-filter: blur(6px);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  animation: fade-up 200ms var(--ease-out) both;
}
.card-header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: none;
  border: none;
  color: var(--text-muted);
  font: 600 11px system-ui, sans-serif;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
}
.card-header:hover { color: var(--text); }
.card-header svg { width: 14px; height: 14px; }
.card-body { display: flex; flex-direction: column; gap: 6px; padding: 0 12px 10px; }
.variables-card .variable-row label { width: 70px; }
.variables-card .variable-row input[type=range] { min-width: 0; }
.dock-row { display: flex; align-items: center; gap: 10px; }
.dock-row input[type=range] { width: 140px; }
/* The timeline across the top of the dock, with the time on its right. */
.timeline-row { display: flex; align-items: center; gap: 10px; padding: 0 4px; }
.timeline { flex: 1; min-width: 0; }
.timeline:disabled { opacity: 0.4; cursor: default; }
.time-readout { min-width: 92px; text-align: right; color: var(--text-muted); font-size: 12px; }
.dock-label { margin-left: 4px; color: var(--text-muted); }
.speed-readout { min-width: 40px; text-align: left; }

.round-btn {
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: var(--control-bg);
  color: var(--text);
  border: 1px solid var(--control-border);
  border-radius: 50%;
  cursor: pointer;
  transition: background-color var(--quick) ease-out, transform var(--quick) ease-out;
}
.round-btn:hover { background: var(--line); }
.round-btn:active { transform: scale(0.94); }
.play-btn {
  width: 48px;
  height: 48px;
  background: var(--accent);
  color: var(--stage-bg);
  border: none;
}
.play-btn:hover { background: #a0ccff; }
.play-btn .icon { width: 18px; height: 18px; }

/* Zoom out, the zoom level (click to reset), zoom in. */
.zoom-controls { display: flex; align-items: center; gap: 4px; }
.round-btn.small { width: 32px; height: 32px; }
.zoom-readout {
  min-width: 52px;
  height: 32px;
  padding: 0 6px;
  background: none;
  color: var(--text);
  border: 1px solid transparent;
  border-radius: 16px;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  transition: border-color var(--quick) ease-out;
}
.zoom-readout:hover { border-color: var(--control-border); }

/* ── Fullscreen ─────────────────────────────────────────────────────────── */

/* The Code section fullscreen: the editor fills the screen and scrolls inside
   itself (it's the only scroller then), with any error still showing under it.
   58px is the section heading; 70px more makes room for an error. */
.section:fullscreen {
  display: flex;
  flex-direction: column;
  background: var(--panel-bg);
  overflow: auto;
}
.section:fullscreen::details-content { block-size: auto; content-visibility: visible; }
.section:fullscreen .section-body { position: relative; }
.section:fullscreen .code-editor {
  --editor-min-height: 100%;
  height: calc(100vh - 58px);
}
.section:fullscreen:has(.error) .code-editor { height: calc(100vh - 128px); }
.section:fullscreen .code-editor .cm-editor { height: 100%; }
.section:fullscreen .error { max-height: 56px; overflow: auto; }
.section:fullscreen .fullscreen-hint { top: 8px; z-index: 20; }

.fullscreen-hint {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  padding: 6px var(--panel-pad);
  background: rgba(26, 26, 46, 0.85);
  border: 1px solid var(--line);
  border-radius: 16px;
  color: var(--text-muted);
  font-size: 12px;
  pointer-events: none;
  animation: hint-in 250ms var(--ease-out) both;
}

/* ── Motion ─────────────────────────────────────────────────────────────── */

@keyframes fade-up {
  from { opacity: 0; transform: translateY(4px); }
}
@keyframes hint-in {
  from { opacity: 0; transform: translate(-50%, -8px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after, .section::details-content {
    animation-duration: 1ms !important;
    animation-delay: 0s !important;
    transition-duration: 1ms !important;
    transition-delay: 0s !important;
  }
}
