/* Isogrid. The canvas fills the screen; controls float over it.
   Phone first: tools along the bottom, the grid-level slider on the right
   edge. From 768 px wide (and tall enough), the slider is a bar along the top. */

:root {
  --bg: #f7f5f0;
  --paper: #f7f5f0; /* the drawing surface */
  --grid: #2b2d3a;  /* gutter lines, drawn on the paper */
  --panel: rgba(255, 255, 255, 0.92);
  --ink: #1d1e22;
  --muted: #5d606b;
  --line: #2b2d3a;
  --accent: #d9480f;
  --accent-ink: #ffffff;
  --border: rgba(0, 0, 0, 0.14);
  --pressed: #ffe3d3;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.14);
  --btn: 48px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1d22;
    /* The paper stays light in dark mode, dimmed, so black and dark colors
       still show (docs/decisions.md). The controls go dark. */
    --paper: #d8d5cd;
    --grid: #2b2d3a;
    --panel: rgba(38, 41, 48, 0.94);
    --ink: #eceef2;
    --muted: #a9adb8;
    --line: #dfe3ee;
    --accent: #ff8a4c;
    --accent-ink: #1b1d22;
    --border: rgba(255, 255, 255, 0.16);
    --pressed: #5a3423;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--ink);
  font: 16px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
body { overscroll-behavior: none; -webkit-user-select: none; user-select: none; }

#stage { position: fixed; inset: 0; width: 100%; height: 100%; touch-action: none; display: block; outline: none; }
#stage:focus-visible { outline: 3px solid var(--accent); outline-offset: -3px; }
body.gray #stage { filter: grayscale(1); }

[hidden] { display: none !important; }

.btn {
  min-width: var(--btn); min-height: var(--btn);
  display: inline-flex; align-items: center; justify-content: center; gap: 2px;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--panel); color: var(--ink); padding: 0 6px;
  font: inherit; font-weight: 600; cursor: pointer; touch-action: manipulation;
}
.btn.wide { padding: 0 14px; }
.btn svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn[aria-pressed="true"], .btn[aria-checked="true"] { background: var(--pressed); border-color: var(--accent); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn:focus-visible, .swatch:focus-visible, .notch:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.btn.danger { color: #c92a2a; }
@media (prefers-color-scheme: dark) { .btn.danger { color: #ff8787; } }

.cluster { position: fixed; display: flex; gap: 6px; z-index: 3; }
.topleft { top: calc(8px + env(safe-area-inset-top)); left: calc(8px + env(safe-area-inset-left)); }
.topright { top: calc(8px + env(safe-area-inset-top)); right: calc(8px + env(safe-area-inset-right)); }
.cluster .btn { box-shadow: var(--shadow); }

/* ---- Grid-level slider: vertical on the right edge (phone) ---- */
.levels {
  position: fixed; z-index: 3; right: calc(6px + env(safe-area-inset-right)); top: 50%; transform: translateY(-55%);
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 4px; box-shadow: var(--shadow);
}
.notches { display: flex; flex-direction: column; gap: 2px; }
.notch {
  width: 44px; height: 44px; border: 0; border-radius: 10px; background: transparent; color: var(--ink);
  display: flex; align-items: center; justify-content: center; cursor: pointer; position: relative; padding: 0;
  touch-action: none;
}
.notch svg { overflow: visible; }
.notch svg path { fill: currentColor; opacity: 0.55; }
.notch[aria-checked="true"] { background: var(--accent); color: var(--accent-ink); }
.notch[aria-checked="true"] svg path { opacity: 1; }
.notch.active::after { /* the level in use when following zoom */
  content: ""; position: absolute; left: 2px; top: 50%; width: 5px; height: 5px; margin-top: -2.5px; border-radius: 50%; background: var(--accent);
}
.notch[aria-checked="true"].active::after { background: var(--accent-ink); }
.lock { width: 44px; min-width: 44px; }
.lock .shackle { transition: transform 0.15s; }
.lock[aria-pressed="true"] .shackle { transform: none; }
.lock[aria-pressed="false"] .shackle { transform: translate(5px, -1px) rotate(18deg); transform-origin: 16px 11px; }
.readout {
  position: fixed; z-index: 3; right: calc(60px + env(safe-area-inset-right)); top: 50%; transform: translateY(-55%);
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 2px 8px; font-size: 13px; color: var(--muted);
  pointer-events: none; opacity: 0; transition: opacity 0.3s;
}
.readout.show { opacity: 1; }

/* ---- Bottom: palette and tools ---- */
.bottom {
  position: fixed; z-index: 3; left: 0; right: 0; bottom: 0;
  padding: 6px calc(6px + env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) calc(6px + env(safe-area-inset-left));
  display: flex; flex-direction: column; gap: 6px; pointer-events: none;
}
.bottom > * { pointer-events: auto; }
.palette {
  display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding: 4px; margin: 0 auto; max-width: 100%;
  background: var(--panel); border: 1px solid var(--border); border-radius: 16px; box-shadow: var(--shadow);
}
.palette::-webkit-scrollbar { display: none; }
.swatch {
  flex: 0 0 44px; width: 44px; height: 44px; border-radius: 50%; border: 2px solid var(--border); padding: 0; cursor: pointer;
  position: relative; overflow: hidden; background: var(--c);
}
.swatch .tones { position: absolute; inset: 0; display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }
.swatch .tones i:first-child { grid-column: 1 / 3; }
.swatch[aria-checked="true"] { border: 3px solid var(--ink); box-shadow: 0 0 0 2px var(--bg) inset; }
.swatch.tool { background: var(--panel); color: var(--ink); display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 20px; }
.swatch.tool svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; }
.swatch.tool[aria-pressed="true"] { background: var(--pressed); border-color: var(--accent); }
.swatch input[type="color"] { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; }

.tools {
  display: flex; gap: 4px; justify-content: space-between; margin: 0 auto; width: 100%; max-width: 440px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 4px; box-shadow: var(--shadow);
}
.tools .btn { flex: 1 1 0; min-width: 44px; border-color: transparent; background: transparent; }
.tools .btn[aria-pressed="true"] { background: var(--pressed); border-color: var(--accent); }
.tools .opts { border-color: var(--border); }

/* ---- Selection bar and handle ---- */
.selbar {
  /* Centered with auto margins, not translate: a translated fixed box shrinks to half the screen and wraps. */
  position: fixed; z-index: 4; left: 0; right: 0; margin: 0 auto; width: max-content; bottom: calc(132px + env(safe-area-inset-bottom));
  display: flex; flex-wrap: wrap; justify-content: center; gap: 4px; max-width: calc(100vw - 16px);
  background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 4px; box-shadow: var(--shadow);
}
.selbar .btn span { font-size: 20px; font-weight: 700; }
.chip { min-width: 52px; min-height: 44px; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; color: var(--muted); }
.handle {
  position: fixed; z-index: 4; width: 44px; height: 44px; margin: -22px 0 0 -22px; border-radius: 50%;
  background: var(--accent); border: 3px solid var(--bg); box-shadow: var(--shadow); touch-action: none; cursor: ns-resize;
}
.handle::after { content: "⇕"; color: var(--accent-ink); font-weight: 800; position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; }
.handle-track {
  position: absolute; left: 52px; top: 50%; display: none; flex-direction: column; gap: 3px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 6px 8px; transform: translateY(-50%);
  font-size: 12px; color: var(--ink); white-space: nowrap;
}
.handle.dragging .handle-track { display: flex; }
.handle-track .tick { display: flex; align-items: center; gap: 6px; opacity: 0.5; }
.handle-track .tick.on { opacity: 1; font-weight: 800; color: var(--accent); }
.handle-track .tick.off { text-decoration: line-through; opacity: 0.25; }
.handle-track .tick.now::after { content: "◀"; font-size: 9px; }

/* ---- Popover (brush shapes, line styles, marquee shapes) ---- */
.popover {
  position: fixed; z-index: 5; left: 0; right: 0; margin: 0 auto; width: max-content; bottom: calc(132px + env(safe-area-inset-bottom));
  background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 8px; box-shadow: var(--shadow);
  display: grid; grid-template-columns: repeat(4, minmax(64px, 1fr)); gap: 6px; max-width: calc(100vw - 16px);
}
.popover .btn { flex-direction: column; min-height: 64px; font-size: 12px; font-weight: 600; padding: 4px; }
.popover .btn svg { width: 28px; height: 28px; }
.popover h4 { grid-column: 1 / -1; margin: 2px 4px; font-size: 14px; color: var(--muted); }

.toast {
  position: fixed; z-index: 6; left: 50%; top: calc(64px + env(safe-area-inset-top)); transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 10px; max-width: calc(100vw - 32px);
  font-size: 15px; opacity: 0; pointer-events: none; transition: opacity 0.25s;
}
.toast.show { opacity: 1; }
.replay-bar { position: fixed; z-index: 6; left: 50%; top: calc(64px + env(safe-area-inset-top)); transform: translateX(-50%);
  display: flex; gap: 10px; align-items: center; background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 6px 6px 6px 14px; box-shadow: var(--shadow); }

/* ---- Menu ---- */
dialog#menu {
  border: 1px solid var(--border); border-radius: 18px; padding: 0; background: var(--bg); color: var(--ink);
  width: min(560px, calc(100vw - 16px)); max-height: calc(100dvh - 24px); box-shadow: var(--shadow);
}
dialog#menu::backdrop { background: rgba(0, 0, 0, 0.35); }
.menu { padding: 12px 16px 20px; user-select: text; -webkit-user-select: text; }
.menu header { display: flex; justify-content: space-between; align-items: center; position: sticky; top: 0; background: var(--bg); }
.menu h2 { margin: 0; font-size: 22px; }
.menu h3 { margin: 18px 0 6px; font-size: 17px; }
.menu fieldset { border: 1px solid var(--border); border-radius: 12px; margin: 8px 0; padding: 6px 10px 10px; }
.menu legend { font-weight: 600; padding: 0 4px; }
.menu label { display: flex; align-items: center; gap: 10px; min-height: 44px; }
.menu input[type="radio"], .menu input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--accent); flex: none; }
.menu select { min-height: 44px; font: inherit; border-radius: 8px; padding: 0 8px; }
.menu input[type="range"] { flex: 1; min-height: 44px; accent-color: var(--accent); }
.menu .row { display: flex; gap: 8px; align-items: center; }
.menu .wrap { flex-wrap: wrap; }
.menu .note { color: var(--muted); font-size: 14px; margin: 6px 0; }
.menu fieldset .btn { margin-top: 6px; }

/* Phones: the level slider owns the right edge, so panels sit left of it. */
@media (max-width: 767px), (max-height: 559px) {
  .selbar, .popover { margin: 0 auto 0 8px; max-width: calc(100vw - 84px); }
  .popover { width: calc(100vw - 84px); grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); }
}

/* ---- Tablet and up: the slider becomes a bar along the top ---- */
@media (min-width: 768px) and (min-height: 560px) {
  .levels { top: calc(8px + env(safe-area-inset-top)); right: auto; left: 50%; transform: translateX(-50%); flex-direction: row; }
  .notches { flex-direction: row; }
  .notch { width: 52px; }
  .notch.active::after { left: 50%; top: auto; bottom: 2px; margin: 0 0 0 -2.5px; }
  .readout { top: calc(72px + env(safe-area-inset-top)); right: auto; left: 50%; transform: translateX(-50%); }
  .bottom { flex-direction: row; align-items: flex-end; justify-content: center; gap: 10px; }
  .palette { margin: 0; flex: 0 1 auto; }
  .tools { margin: 0; width: auto; flex: 0 0 auto; }
  .tools .btn { flex: 0 0 52px; }
  .selbar, .popover { bottom: calc(80px + env(safe-area-inset-bottom)); margin: 0 auto; max-width: calc(100vw - 16px); }
  .popover { width: max-content; grid-template-columns: repeat(4, minmax(64px, 1fr)); }
}
/* Short landscape phones: horizontal slider too, tucked under the top clusters' row. */
@media (max-height: 559px) {
  .levels { top: auto; bottom: calc(118px + env(safe-area-inset-bottom)); right: calc(6px + env(safe-area-inset-right)); transform: none; flex-direction: row; }
  .notches { flex-direction: row; }
  .readout { top: auto; bottom: calc(176px + env(safe-area-inset-bottom)); transform: none; right: 8px; }
}

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