:root {
  --bg: #111;
  --fg: #eee;
  --muted: #888;
  --accent: #5fa8ff;
  --ok: #4caf50;
  --warn: #f0ad4e;
  --err: #e03c3c;
  --cell-empty: #222;
  --cell-border: #333;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  background: var(--bg); color: var(--fg);
  font: 14px/1.4 system-ui, -apple-system, sans-serif;
  margin: 0; padding: 0;
  display: flex; flex-direction: column; min-height: 100vh;
}

.app-header {
  position: sticky; top: 0; z-index: 10;
  background: var(--bg); border-bottom: 1px solid var(--cell-border);
  padding: .6rem 1rem;
  display: flex; align-items: stretch; gap: .75rem;
}
/* Sized to roughly span both header-lines. object-fit keeps aspect so
   a squarish favicon renders square even if the source isn't exactly
   pixel-square. */
.app-logo {
  height: 2.75rem; width: 2.75rem;
  object-fit: contain;
  flex-shrink: 0;
  align-self: center;
}
.header-stack {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column; justify-content: center;
}
.header-line {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem;
}
.header-line + .header-line { margin-top: .25rem; }
.title-block { display: flex; align-items: center; gap: .6rem; }
h1 { margin: 0; font-size: 1.1rem; font-weight: 600; }
.paused-badge {
  display: inline-block;
  padding: 2px 8px; border-radius: 3px;
  background: var(--warn); color: #000;
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.5px;
}
/* The explicit display above otherwise overrides the UA stylesheet's
   [hidden] { display: none } — re-assert it with specificity. */
.paused-badge[hidden] { display: none; }
.status, .plot-meta { margin: 0; color: var(--muted); font-size: 12px; }

.view-toggle { display: flex; gap: 2px; }
.view-btn {
  background: #1a1a1a; color: var(--fg); border: 1px solid var(--cell-border);
  padding: .35rem .75rem; font-size: 12px; cursor: pointer;
}
.view-btn:first-child { border-radius: 4px 0 0 4px; }
.view-btn:last-child  { border-radius: 0 4px 4px 0; border-left: 0; }
.view-btn:hover { background: #222; }
.view-btn.active {
  background: var(--accent); color: #000; border-color: var(--accent); font-weight: 600;
}
.view-btn.aged { color: var(--muted); }
.view-btn.aged.active { color: #000; }
.view-btn[hidden] { display: none; }

/* Sub-view overlay: floats just below the (sticky) header at top-right,
   so grid-fit / camera-rotation controls don't steal a whole header row.
   position:absolute + top:100% anchors to the header's bottom edge and
   inherits the sticky scroll behavior for free. */
#subview-overlay {
  position: absolute; top: 100%; right: 1rem;
  margin-top: .5rem; z-index: 15;
  display: flex; gap: .4rem;
  pointer-events: none;
}
#subview-overlay[hidden] { display: none; }
.subview-toggle { display: flex; gap: 2px; pointer-events: auto; }
.subview-toggle[hidden] { display: none; }
.view-btn-icon {
  font-size: 16px; line-height: 1;
  padding: .3rem .55rem;
  min-width: 2rem;
}

#toast-container {
  position: fixed; bottom: 1rem; right: 1rem; z-index: 1000;
  display: flex; flex-direction: column; gap: .5rem; align-items: flex-end;
  pointer-events: none;
}
.toast {
  background: #1a1a1a; color: var(--fg);
  padding: .7rem 1rem; border-radius: 4px;
  border: 1px solid var(--cell-border); max-width: 360px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.5);
  opacity: 0; transform: translateY(10px);
  transition: opacity .25s ease, transform .25s ease;
  pointer-events: auto; font-size: 13px;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.hide { opacity: 0; transform: translateY(10px); }
.toast-ok  { border-color: var(--ok); }
.toast-err { border-color: var(--err); }

.upload {
  padding: 1rem; background: #1a1a1a; border-radius: 6px; margin-bottom: 1.5rem;
  max-width: 600px;
}
.upload.disabled { color: var(--muted); }
.upload label { display: block; margin-bottom: .5rem; }
.upload input[type=file] { display: block; margin-top: .5rem; }
.upload button {
  background: var(--accent); color: #000; border: 0; padding: .5rem 1rem;
  border-radius: 4px; cursor: pointer; font-weight: 600;
}

/* Grid-wrap fills the remaining viewport below the sticky header. Uses
   margin:auto on the grid (not align-items:center) so a grid taller than
   the wrapper anchors to the top and scrolls correctly — centering with
   align-items would push the overflow equally both ways and make the top
   unreachable. */
.grid-wrap {
  flex: 1; min-height: 0;
  display: flex;
  padding: 1rem;
  overflow: auto;
}
/* Explicit `display: flex` above would otherwise override the UA's
   [hidden] { display: none } — re-assert it so toggling to camera view
   actually hides the grid instead of leaving it sharing the flex row. */
.grid-wrap[hidden] { display: none; }
.grid-wrap > .grid { margin: auto; }
.grid {
  /* --paper is set inline from config.yml (paper.color). Grid background and
     empty-cell backgrounds inherit it so the preview matches the physical paper.
     No gap/padding — cells butt directly against each other to match the
     actual plot (where adjacent tiles touch with no inter-cell margin). */
  display: grid; gap: 0; background: var(--paper, #fff); padding: 0;
  border-radius: 4px;
}
/* Fit-width: grid fills the viewport width; height follows aspect-ratio and
   can overflow (body scrolls). Fit-height: grid fills remaining vertical
   space; width follows aspect-ratio, centered horizontally. */
.grid.fit-width  { width: 100%; height: auto; }
.grid.fit-height { height: 100%; width: auto; max-width: 100%; }
.cell {
  position: relative; background: var(--paper, #fff);
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
  border: 0; color: inherit; font: inherit; padding: 0;
}
button.cell { cursor: pointer; }
button.cell:hover { background: color-mix(in srgb, var(--paper, #fff) 92%, black); }
button.cell:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.cell .empty-label {
  color: color-mix(in srgb, var(--paper, #fff) 40%, black);
}
.hint { color: var(--muted); margin: 0 0 1rem; }
/* Always fill the cell; object-fit handles the aspect-preserving scale.
   Using width/height (not max-width) so small-intrinsic-size SVGs — which
   our rewrite outputs with width/height = cellMm — scale UP to the cell
   on screen instead of rendering at their tiny natural size. */
.cell img { width: 100%; height: 100%; object-fit: contain; display: block; }
.cell .empty-label { color: #444; font-size: 1.5rem; font-weight: 600; }
.cell .badge {
  position: absolute; bottom: 2px; left: 2px;
  background: rgba(0,0,0,0.7); color: #fff;
  font-size: 10px; padding: 2px 5px; border-radius: 2px;
}
/* Status is shown via the corner badge, not a cell outline — outlines
   would overlay the SVG and make the preview inaccurate to the plot.
   Opacity is non-obtrusive and useful for non-terminal / skipped cells. */
.cell.status-pending   { opacity: 0.55; }
.cell.status-queued    { opacity: 0.85; }
.cell.status-cancelled { opacity: 0.45; }

/* ---------- camera view ----------
   Fills the remaining viewport like .grid-wrap; contains the JPEG uploaded
   by plotopia-camera. The image uses object-fit:contain so any resolution
   fits the window without cropping. The meta line overlays the bottom-left
   with the snapshot age so viewers can see the feed is live. */
.camera-view {
  flex: 1; min-height: 0; position: relative;
  display: flex; align-items: center; justify-content: center;
  padding: 1rem; background: #000;
  overflow: hidden;
}
.camera-view[hidden] { display: none; }
.camera-img {
  /* Fill the available area, scaling UP if the viewport is larger than
     the snapshot's native resolution. object-fit: contain preserves
     aspect ratio so the image stays fully on-screen (letterbox/pillarbox
     on mismatched aspect, never cropped). */
  width: 100%; height: 100%;
  object-fit: contain; display: block;
}
/* Rotated 90° CW: swap the image's pre-rotate box to match the
   container's height/width. After rotate(90deg), the visual bounds
   realign with the container. --cam-w / --cam-h are set by a JS
   ResizeObserver on .camera-view. */
.camera-view.rotated .camera-img {
  width: var(--cam-h, 100%);
  height: var(--cam-w, 100%);
  transform: rotate(90deg);
}
.camera-meta {
  position: absolute; bottom: 1rem; left: 1rem; margin: 0;
  background: rgba(0,0,0,0.7); color: var(--muted);
  font-size: 11px; padding: 3px 7px; border-radius: 3px;
}

/* ---------- archive view ----------
   Scrollable card grid of completed grids, newest first. Each card shows
   the final camera snapshot (when available) and its timestamp. */
.archive-view {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column;
  padding: 1rem; overflow: auto;
}
.archive-view[hidden] { display: none; }
.archive-status {
  margin: 0 0 .75rem; color: var(--muted); font-size: 12px;
}
.archive-cards {
  display: grid; gap: 1rem;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
}
.archive-card {
  background: #1a1a1a; border: 1px solid var(--cell-border); border-radius: 6px;
  overflow: hidden; display: flex; flex-direction: column;
}
.archive-thumb {
  width: 100%; aspect-ratio: 4 / 3;
  display: block; background: #000;
  position: relative; overflow: hidden;
}
.archive-thumb-img {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover; display: block;
}
.archive-thumb-empty {
  display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 12px;
}
/* Rotated cards: flip the thumb to portrait aspect, then oversize the
   image's pre-rotate box so rotate(90deg) lands it exactly inside the
   portrait frame (4:3 pre-rotate → 3:4 visual). */
.archive-cards.rotated .archive-thumb { aspect-ratio: 3 / 4; }
.archive-cards.rotated .archive-thumb-img {
  inset: 50% auto auto 50%;
  width: calc(100% * 4 / 3);
  height: calc(100% * 3 / 4);
  transform: translate(-50%, -50%) rotate(90deg);
}
.archive-stamp {
  margin: 0; padding: .5rem .75rem;
  color: var(--fg); font-size: 12px; font-variant-numeric: tabular-nums;
  border-top: 1px solid var(--cell-border);
}
.archive-card-clickable { cursor: zoom-in; }
.archive-card-clickable:hover { border-color: var(--accent); }

/* ---------- archive lightbox ----------
   Fullscreen overlay for clicked archive cards. Image scales to fit
   the viewport (letterboxed on mismatched aspect), click-anywhere or
   Esc to dismiss. */
.lightbox {
  position: fixed; inset: 0; z-index: 800;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, 0.92);
}
.lightbox[hidden] { display: none; }
.lightbox-img {
  /* Fill the viewport in whichever axis hits first, letterboxing the
     other. width/height 100% (not max-*) also scales UP if the source
     is smaller than the viewport. */
  width: 100%; height: 100%;
  object-fit: contain; display: block;
}
/* Rotated lightbox: the overlay is viewport-sized (position:fixed inset:0),
   so vh/vw map 1:1 to the lightbox box. Swapping them pre-rotate lets
   rotate(90deg) land the image back flush with the viewport. */
.lightbox.rotated .lightbox-img {
  width: 100vh; height: 100vw;
  transform: rotate(90deg);
}
.lightbox-caption {
  position: absolute; bottom: 1rem; left: 50%; transform: translateX(-50%);
  margin: 0; padding: 4px 10px; border-radius: 3px;
  background: rgba(0, 0, 0, 0.7); color: var(--fg);
  font-size: 12px; font-variant-numeric: tabular-nums;
}
.lightbox-close {
  position: absolute; top: 1rem; right: 1rem;
  background: transparent; border: 0; color: var(--fg);
  font-size: 2rem; line-height: 1; cursor: pointer;
  padding: .2rem .6rem;
}
.lightbox-close:hover { color: var(--accent); }
.lightbox-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  background: rgba(0, 0, 0, 0.5); color: var(--fg); border: 0;
  font-size: 2.5rem; line-height: 1; cursor: pointer;
  padding: .4rem .8rem; border-radius: 4px;
}
.lightbox-nav:hover { background: rgba(0, 0, 0, 0.85); color: var(--accent); }
.lightbox-nav[hidden] { display: none; }
.lightbox-prev { left: 1rem; }
.lightbox-next { right: 1rem; }

footer { color: var(--muted); margin-top: 2rem; font-size: 12px; }

/* ---------- convert-dialog modal ---------- */
.modal[hidden] { display: none; }
.modal {
  position: fixed; inset: 0; z-index: 500;
  display: flex; align-items: center; justify-content: center;
}
.modal-backdrop {
  position: absolute; inset: 0; background: rgba(0,0,0,0.65);
}
.modal-panel {
  position: relative;
  background: #181818; border: 1px solid var(--cell-border); border-radius: 6px;
  width: min(1000px, 95vw); max-height: 95vh;
  display: flex; flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: .75rem 1rem; border-bottom: 1px solid var(--cell-border);
}
.modal-header h2 { margin: 0; font-size: 1rem; font-weight: 600; }
.btn-close {
  background: transparent; border: 0; color: var(--muted); font-size: 1.5rem;
  line-height: 1; cursor: pointer; padding: 0 .4rem;
}
.btn-close:hover { color: var(--fg); }

.modal-body {
  display: grid; grid-template-columns: 260px 1fr; gap: 1rem;
  padding: 1rem; overflow: hidden; min-height: 0;
}
@media (max-width: 720px) {
  .modal-body { grid-template-columns: 1fr; }
}
/* Simple variant for the SVG preview dialog — no algorithm sidebar,
   just preview + status stacked vertically. */
.modal-body-simple {
  display: flex; flex-direction: column; gap: .5rem;
}
.controls { overflow-y: auto; padding-right: .5rem; }
.algo-desc { color: var(--muted); font-size: 12px; margin: .25rem 0 .75rem; }
.param {
  display: grid; grid-template-columns: 1fr auto; grid-template-rows: auto auto;
  gap: .25rem .5rem; margin-bottom: .75rem;
}
.param-name { font-size: 12px; color: var(--muted); }
.param-value { font-size: 12px; color: var(--fg); text-align: right; font-variant-numeric: tabular-nums; }
.param input[type=range], .param select, .param input[type=checkbox] {
  grid-column: 1 / -1;
}
.param input[type=checkbox] { justify-self: start; width: auto; }
.param select, .param input[type=range] { width: 100%; }

.preview-wrap {
  display: flex; flex-direction: column; gap: .5rem; min-height: 0;
}
.preview {
  flex: 1; min-height: 320px;
  background: #fff; border-radius: 4px; overflow: auto;
  display: flex; align-items: center; justify-content: center;
}
.preview svg { max-width: 100%; max-height: 100%; }
.preview-wrap .status { margin: 0; color: var(--muted); font-size: 12px; }

.preview-loading {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: .75rem; color: #666; font-size: 13px;
}
.preview-loading p { margin: 0; }
.spinner {
  width: 32px; height: 32px;
  border: 3px solid #ddd; border-top-color: var(--accent);
  border-radius: 50%;
  /* transform animations run on the compositor, so this keeps spinning
     even while the algorithm blocks the main thread. */
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.modal-footer {
  display: flex; justify-content: flex-end; gap: .5rem;
  padding: .75rem 1rem; border-top: 1px solid var(--cell-border);
}
.modal-footer button {
  background: #2a2a2a; color: var(--fg); border: 1px solid var(--cell-border);
  padding: .5rem 1rem; border-radius: 4px; cursor: pointer; font-size: 13px;
}
.modal-footer button.primary {
  background: var(--accent); color: #000; border-color: var(--accent); font-weight: 600;
}
.modal-footer button:hover { filter: brightness(1.1); }
