:root {
  --bg: #1d1f24;
  --panel: #262930;
  --panel2: #2d3139;
  --line: #3a3f49;
  --text: #e6e8ec;
  --muted: #9aa1ad;
  --accent: #5aa9ff;
  --accent2: #ffb347;
  --danger: #ff6b6b;
  --radius: 8px;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  font-size: 13px;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; background: var(--bg); color: var(--text); }
body { display: grid; grid-template-rows: auto 1fr; grid-template-columns: 340px 1fr; grid-template-areas: "head head" "side main"; height: 100vh; height: 100dvh; overflow: hidden; }   /* dvh: iOS Safari's toolbar no longer hides the zoom bar */

header { grid-area: head; display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; padding: 8px 14px; min-height: 48px; background: var(--panel); border-bottom: 1px solid var(--line); }
header h1 { font-size: 15px; margin: 0 14px 0 0; font-weight: 600; letter-spacing: .3px; white-space: nowrap; }
header h1 span { color: var(--muted); font-weight: 400; }
header .grow { flex: 1; }
header label { color: var(--muted); display: flex; align-items: center; gap: 6px; white-space: nowrap; }
/* grid type and canvas size: display:contents keeps them in the header row on wide screens; under 900 px
   they fold into their own row behind the summary button (see the media query below) */
.docSettings { display: contents; }
.docToggle { display: none; max-width: 62vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.docToggle .chev { display: inline-block; color: var(--muted); transition: transform .15s; }
.docToggle[aria-expanded="true"] .chev { transform: rotate(180deg); }

aside { grid-area: side; background: var(--panel); border-right: 1px solid var(--line); overflow-y: auto; padding: 10px 12px 40px; }
main { grid-area: main; position: relative; overflow: hidden; background: #15161a; display: flex; align-items: center; justify-content: center; }
/* touch-action none: one finger pans and two fingers pinch-zoom the canvas (app.js stage gestures) instead of scrolling or zooming the page.
   margin:auto (not align/justify-items) centres a small canvas yet lets a zoomed-in one scroll all the way to its top-left corner. */
#stageWrap { position: absolute; inset: 0; overflow: auto; display: flex; touch-action: none; }
#stage { position: relative; margin: auto; flex: none; box-shadow: 0 10px 40px rgba(0,0,0,.6); background: #fff; }
#stage.checker { background-color: #ddd; background-image: linear-gradient(45deg, #fff 25%, transparent 25%, transparent 75%, #fff 75%), linear-gradient(45deg, #fff 25%, transparent 25%, transparent 75%, #fff 75%); background-size: 20px 20px; background-position: 0 0, 10px 10px; }
#stage canvas { position: absolute; left: 0; top: 0; display: none; }
#stage svg { position: relative; display: block; }
/* nothing ever targets the grid itself, so keep it out of hit testing: a hover over 60 000 stroked paths cost
   4 ms per move, and a pen hovers all the time (handles live in their own svg#handles layer below) */
#stage svg#vectorSvg, #stage canvas { pointer-events: none; }
#stage svg#handles { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; touch-action: none; }   /* a finger on a handle drags it, never scrolls */
.handle { pointer-events: all; cursor: grab; touch-action: none; }
.handle:active { cursor: grabbing; }
.handle circle.hit { fill: rgba(255,255,255,0.001); stroke: none; }
.handle circle.ring { fill: rgba(255,255,255,.9); stroke: #1b6ac9; stroke-width: 2; }
@media (hover: hover) { .handle:hover circle.ring { stroke: #ffb347; } }
.handle circle.dot { fill: #1b6ac9; }
.handle text { font: 600 11px/1 "Segoe UI", system-ui, sans-serif; fill: #fff; paint-order: stroke; stroke: rgba(0,0,0,.65); stroke-width: 3px; pointer-events: none; }
.handle line.vis { stroke: #d33; stroke-width: 1.5; stroke-dasharray: 6 4; }
.handle line.hit { stroke: transparent; stroke-width: 14; }
.handle.hline { cursor: ns-resize; }
#status { position: absolute; left: 10px; bottom: 8px; font-size: 11px; color: var(--muted); background: rgba(0,0,0,.4); padding: 3px 8px; border-radius: 6px; pointer-events: none; }
#zoomBar { position: absolute; right: 10px; bottom: 8px; display: flex; gap: 4px; }

select, input[type=number], input[type=text], button { background: var(--panel2); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 4px 7px; font: inherit; }
input[type=number] { width: 74px; }
button { cursor: pointer; }
@media (hover: hover) { button:hover { border-color: var(--accent); } button:disabled:hover { border-color: var(--line); } }   /* hover only where it exists: no stuck highlight after a tap */
button.primary { background: var(--accent); color: #0b1b2e; border-color: var(--accent); font-weight: 600; }
button.small { padding: 2px 6px; font-size: 11px; }
button.icon { font-size: 14px; line-height: 1; padding: 2px 8px; min-width: 28px; }
button:disabled { opacity: .35; cursor: default; }
.btnPair { display: inline-flex; gap: 4px; }
input[type=range] { width: 100%; accent-color: var(--accent); }
input[type=color] { width: 28px; height: 22px; padding: 0; border: 1px solid var(--line); border-radius: 4px; background: none; }
input[type=checkbox] { accent-color: var(--accent); }

.tabs { display: flex; gap: 2px; margin: 2px 0 8px; border-bottom: 1px solid var(--line); position: sticky; top: -10px; background: var(--panel); z-index: 2; padding-top: 4px; }
.tabs button { flex: 1; background: none; border: none; border-bottom: 2px solid transparent; border-radius: 0; padding: 8px 6px; color: var(--muted); font-weight: 600; letter-spacing: .3px; }
@media (hover: hover) { .tabs button:hover { color: var(--text); } }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); }
.tabs button .dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--accent2); margin-left: 6px; vertical-align: middle; }
.tabPane { display: none; }
.tabPane.active { display: block; }

.group { margin: 10px 0 6px; }
.group h3 { font-size: 11px; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); margin: 10px 0 6px; padding-bottom: 3px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; }
.param { display: grid; grid-template-columns: 1fr 74px; grid-template-areas: "label num" "slider slider"; gap: 2px 8px; align-items: center; margin: 6px 0; }
.param label { grid-area: label; color: var(--text); }
.param .hint { display: block; color: var(--muted); font-size: 11px; }
.param input[type=number] { grid-area: num; }
.param input[type=range] { grid-area: slider; }
.param.row { grid-template-columns: 1fr auto; grid-template-areas: "label ctl"; }
.param.row > :last-child { grid-area: ctl; }
.param select { max-width: 180px; }

.layer { display: grid; grid-template-columns: 20px 1fr 56px 30px; gap: 6px; align-items: center; margin: 4px 0; }
.layer .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }   /* a label for the visibility box */
.layer input[type=number] { width: 56px; padding: 2px 4px; }

.canvasRow { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin: 6px 0; }
.canvasRow input[type=number] { width: 70px; }
.btnRow { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0; }
.note { color: var(--muted); font-size: 11px; line-height: 1.4; }
a { color: var(--accent); }

.dropzone { border: 1px dashed var(--line); border-radius: var(--radius); padding: 10px; text-align: center; color: var(--muted); margin: 6px 0; cursor: pointer; }
.dropzone.over { border-color: var(--accent); color: var(--accent); }
#srcPreview { position: relative; width: 100%; background: #111; border-radius: 6px; overflow: hidden; margin: 6px 0; cursor: crosshair; }
#srcPreview canvas { display: block; width: 100%; }
#srcPreview svg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }

/* animation: sweep tracks */
.track { border-left: 2px solid var(--line); padding: 2px 0 2px 8px; margin: 6px 0; }
.trackHead { display: flex; gap: 6px; align-items: center; margin-bottom: 4px; }
.trackHead select { flex: 1; max-width: none; }
.trackRow { display: grid; grid-template-columns: auto 1fr auto 1fr; gap: 4px 6px; align-items: center; margin: 3px 0; }
.trackRow label { color: var(--muted); font-size: 11px; }
.trackRow input[type=number] { width: 100%; min-width: 0; }
.trackRow select { grid-column: 2 / span 3; max-width: none; }

/* presets: a horizontal strip of live thumbnails */
.presetStrip { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0 6px; scrollbar-width: thin; }
.preset { flex: 0 0 auto; width: 110px; position: relative; border: 1px solid var(--line); border-radius: 6px; background: var(--panel2); overflow: hidden; cursor: pointer; }
@media (hover: hover) { .preset:hover { border-color: var(--accent); } }
.preset .thumb { width: 100%; background: #fff; display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 11px; }
.preset .thumb svg { display: block; width: 100%; height: 100%; pointer-events: none; }   /* the card takes the click; its paths need no hit testing */
.preset .pname { font-size: 10.5px; padding: 3px 5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text); }
.preset.user .pname { color: var(--accent2); }
.preset .del { position: absolute; top: 2px; right: 2px; width: 18px; height: 18px; padding: 0; border-radius: 50%; line-height: 1; font-size: 12px; background: rgba(0,0,0,.6); color: #fff; border: none; display: none; }
@media (hover: hover) { .preset:hover .del { display: block; } .preset .del:hover { background: var(--danger); } }
@media (hover: none) { .preset .del { display: block; } }   /* no hover on a touch screen: the delete button is always there */

/* the selected handle (arrow keys nudge it) */
.handle.active circle.ring { stroke: var(--accent2); stroke-width: 3; }
.handle.active line.vis { stroke: var(--accent2); }

/* help drawer */
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: 460px; max-width: 100vw; background: var(--panel); border-left: 1px solid var(--line); z-index: 30; display: flex; flex-direction: column; box-shadow: -12px 0 32px rgba(0,0,0,.45); }
.drawer[hidden] { display: none; }
.drawerHead { display: flex; align-items: center; justify-content: space-between; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.drawerHead h2 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); }
.drawerBody { overflow-y: auto; padding: 6px 18px 40px; line-height: 1.5; font-size: 12.5px; }
.drawerBody h2 { font-size: 17px; margin: 12px 0 6px; }
.drawerBody h3 { font-size: 14px; margin: 16px 0 6px; }
.drawerBody h4 { font-size: 12.5px; margin: 12px 0 4px; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; }
.drawerBody p { margin: 6px 0; }
.drawerBody p.lead { color: var(--text); font-size: 13px; border-left: 3px solid var(--accent); padding-left: 10px; }
.drawerBody ul, .drawerBody ol { padding-left: 20px; margin: 6px 0; }
.drawerBody li { margin: 3px 0; }
.drawerBody code { background: var(--panel2); padding: 1px 4px; border-radius: 3px; font-size: 11.5px; }
.drawerBody pre { background: var(--panel2); padding: 8px 10px; border-radius: 6px; overflow-x: auto; font-size: 11.5px; line-height: 1.4; }
.drawerBody table { border-collapse: collapse; margin: 6px 0; font-size: 12px; }
.drawerBody td, .drawerBody th { border: 1px solid var(--line); padding: 4px 7px; vertical-align: top; text-align: left; }
.drawerBody table.shortcuts td { border: none; padding: 2px 10px 2px 0; }
.drawerBody kbd { display: inline-block; background: var(--panel2); border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 6px; font: 11px/18px ui-monospace, Consolas, monospace; white-space: nowrap; }
.drawerBody details { margin: 10px 0; border: 1px solid var(--line); border-radius: 6px; padding: 4px 10px; }
.drawerBody summary { cursor: pointer; font-weight: 600; color: var(--muted); }
.drawerBody details[open] summary { color: var(--text); margin-bottom: 4px; }

.toast { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); background: var(--panel2); border: 1px solid var(--line); padding: 8px 14px; border-radius: 8px; box-shadow: 0 6px 24px rgba(0,0,0,.5); opacity: 0; transition: opacity .2s; pointer-events: none; }
.toast.show { opacity: 1; }

.sideToggle { display: none; }
@media (max-width: 900px) {
  body { grid-template-columns: 1fr; grid-template-rows: auto 1fr; grid-template-areas: "head" "main"; }
  header h1 span { display: none; }
  .docToggle { display: inline-block; }
  .docSettings { display: none; }
  .docSettings.open { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; order: 10; width: 100%; padding: 8px 0 2px; border-top: 1px solid var(--line); }
  /* #side only: the help drawer is an <aside> too and keeps its own place on the right */
  aside#side { position: fixed; left: 0; top: var(--headH, 48px); bottom: 0; width: 340px; max-width: 92vw; z-index: 5; transform: translateX(-100%); transition: transform .2s; box-shadow: 10px 0 30px rgba(0,0,0,.45); }
  aside#side.open { transform: none; }
  .sideToggle { display: inline-block; }
}

/* ---- touch screens (iPad and other tablets) ----
   The page never zooms or double-tap-zooms: pan-x pan-y on every scroller and on the chrome keeps
   scrolling native but hands every pinch to the canvas, which has its own (touch-action: none above). */
html, body, header, aside, main, .tabPane, .presetStrip, .drawer, .drawerBody, .drawerBody pre { touch-action: pan-x pan-y; }
/* no long-press text selection or callout on the chrome and the canvas; text stays selectable where it is text */
header, aside, main, .drawerHead { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
input, textarea, select, .drawerBody, .note, code, pre { -webkit-user-select: text; user-select: text; }

/* finger-sized controls where the primary pointer is a finger; the mouse keeps the dense layout */
@media (pointer: coarse) {
  :root { font-size: 14px; }
  select, input[type=number], input[type=text], button { min-height: 38px; padding: 6px 10px; }
  button.small { min-height: 34px; padding: 4px 10px; font-size: 12.5px; }
  button.icon { min-width: 38px; font-size: 16px; }
  input[type=checkbox] { width: 22px; height: 22px; margin: 2px 4px; }
  input[type=color] { width: 44px; height: 34px; }
  input[type=range] { height: 36px; margin: 0; }           /* the whole strip is the touch target, not just the track */
  header { gap: 8px 10px; padding: 8px 10px; }
  header label { gap: 8px; }
  .tabs button { padding: 12px 6px; font-size: 14px; }
  .param { grid-template-columns: 1fr 86px; gap: 4px 8px; margin: 10px 0; }
  .param.row { min-height: 38px; }
  .param select { max-width: 200px; }
  .layer { grid-template-columns: 30px 1fr 68px 46px; gap: 6px; margin: 6px 0; }
  .layer input[type=number] { width: 68px; padding: 4px 6px; }
  .layer .name { min-height: 34px; display: flex; align-items: center; }
  .canvasRow { gap: 8px; margin: 8px 0; }
  .canvasRow input[type=number] { width: 80px; }
  .btnRow { gap: 8px; }
  .preset { width: 128px; }
  .preset .pname { font-size: 12px; padding: 5px 6px; }
  .preset .del { display: block; width: 32px; height: 32px; min-height: 32px; font-size: 18px; top: 4px; right: 4px; }   /* min-height: the generic button rule above would stretch it */
  .presetStrip { gap: 8px; padding: 2px 0 8px; }
  #zoomBar { gap: 6px; }
  #zoomBar button.small { min-height: 42px; min-width: 46px; font-size: 14px; }
  .drawerHead button.small { min-width: 44px; }
  .drawerBody { font-size: 14px; }
  .drawerBody summary { padding: 6px 0; }
  .dropzone { padding: 16px 10px; }
  .trackHead { gap: 8px; }
}
