/* Rolling Shapes. The view fills the window, with a card of controls in each
   corner. In a tight window each card folds to its name. */
:root {
  color-scheme: light dark;
  --bg: light-dark(#f8f6f0, #14151a);
  --panel: light-dark(rgba(255, 253, 248, 0.94), rgba(28, 29, 35, 0.94));
  --text: light-dark(#46443d, #c8c4b9);
  --h1: light-dark(#1a1a1a, #f5f3ec);
  --link: light-dark(#1552a1, #7aa6e0);
  --rule: light-dark(#ddd8cc, #3a3a40);
  --ring: light-dark(rgba(0, 0, 0, 0.16), rgba(255, 255, 255, 0.18));
  --muted: light-dark(#7c786d, #918c80);
  --accent: light-dark(#1552a1, #7aa6e0);
  --shadow: light-dark(0 2px 10px rgba(40, 30, 10, 0.12), 0 2px 12px rgba(0, 0, 0, 0.5));
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); }
body {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  color: var(--text);
  font: 14px/1.35 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
}

.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 5px max(12px, env(safe-area-inset-right)) 5px max(12px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--rule);
}
.back { color: var(--link); text-decoration: none; font-size: 13px; white-space: nowrap; }
.back:hover { text-decoration: underline; }
h1 { margin: 0; font-size: 16px; font-weight: 600; color: var(--h1); white-space: nowrap; }
.topbar .icon { justify-self: end; }

button, select {
  font: inherit;
  color: var(--h1);
  background: transparent;
  border: 1px solid var(--ring);
  border-radius: 7px;
  padding: 4px 9px;
  cursor: pointer;
}
button:hover:not(:disabled), select:hover { border-color: var(--muted); }
button:disabled { opacity: 0.45; cursor: default; }
select { background: var(--panel); min-width: 0; width: 100%; }
button:focus-visible, select:focus-visible, input:focus-visible, a:focus-visible, canvas:focus-visible {
  outline: 2px solid var(--link);
  outline-offset: 1px;
}
.icon { display: inline-flex; padding: 3px; }
.icon svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.stage { position: relative; min-height: 0; overflow: hidden; }
.stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#shapes { touch-action: none; cursor: crosshair; outline-offset: -2px; }
#shapes.on-pen { cursor: grab; }
#shapes.dragging { cursor: grabbing; }

.card {
  position: absolute;
  background: var(--panel);
  border: 1px solid var(--rule);
  border-radius: 10px;
  box-shadow: var(--shadow);
  max-height: calc(100% - 20px);
  display: flex;
  flex-direction: column;
}
.tl { top: 10px; left: max(10px, env(safe-area-inset-left)); width: 212px; }
.tr { top: 10px; right: max(10px, env(safe-area-inset-right)); width: 236px; }
.bl { bottom: 10px; left: max(10px, env(safe-area-inset-left)); width: 212px; }
.br { bottom: 10px; right: max(10px, env(safe-area-inset-right)); width: 236px; }
.card .fold { display: none; }
.card .body { padding: 9px 10px; display: flex; flex-direction: column; gap: 7px; overflow-y: auto; }
.card h2 { margin: 3px 0 -2px; font-size: 11px; font-weight: 600; letter-spacing: 0.03em; color: var(--muted); text-transform: uppercase; }
.card h2:first-child { margin-top: 0; }

.field { display: flex; flex-direction: column; gap: 2px; }
.field > span { font-size: 12px; color: var(--muted); }
.check { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.check input { margin: 0; width: 15px; height: 15px; accent-color: var(--accent); flex: none; }
.row { display: flex; gap: 5px; flex-wrap: wrap; }
.row button { flex: 1 1 auto; white-space: nowrap; }

.seg { display: grid; grid-template-columns: 1fr 1fr; }
.seg button { border-radius: 0; }
.seg button:first-child { border-radius: 7px 0 0 7px; border-right: 0; }
.seg button:last-child { border-radius: 0 7px 7px 0; }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--bg); border-color: var(--accent); }

.slider { display: grid; grid-template-columns: 1fr auto; gap: 0 6px; font-size: 12px; margin-bottom: 3px; }
.slider > span { color: var(--text); }
.slider output { font-variant-numeric: tabular-nums; color: var(--h1); }
.slider input { grid-column: 1 / 3; width: 100%; margin: 1px 0; accent-color: var(--accent); }

#close { font-weight: 600; }
#close.busy { color: var(--muted); }

.theme-toggle { display: inline-flex; align-items: center; gap: 6px; align-self: flex-start; }
.theme-toggle svg { width: 16px; height: 16px; }
:root[data-theme="light"] .theme-toggle .sun, :root:not([data-theme]) .theme-toggle .sun { display: none; }
:root[data-theme="dark"] .theme-toggle .moon { display: none; }

.transport {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px 8px;
  padding: 6px max(10px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  border-top: 1px solid var(--rule);
}
.transport button { white-space: nowrap; }
.transport .main { font-weight: 600; min-width: 62px; background: var(--accent); color: var(--bg); border-color: var(--accent); }
.speed { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.speed input { width: 110px; margin: 0; accent-color: var(--accent); }
.readout { display: flex; align-items: center; gap: 8px; margin-left: auto; min-width: 0; font-variant-numeric: tabular-nums; }
.readout strong { color: var(--h1); font-weight: 600; }
.readout strong:empty { display: none; }
progress { width: 90px; height: 7px; }

/* A tight window: every card folds to its name, one opens at a time. */
.compact .card { width: auto; max-width: calc(100% - 20px); }
.compact .card .fold { display: block; border: 0; padding: 5px 11px; font-weight: 600; font-size: 13px; }
.compact .card .body { display: none; width: min(246px, 100vw - 40px); max-height: calc(100% - 34px); }
.compact .card.open { z-index: 2; max-height: calc(100% - 14px); }
.compact .card.open .body { display: flex; flex: 1 1 auto; border-top: 1px solid var(--rule); }
.compact .card .fold::after { content: " ▾"; color: var(--muted); }
.compact .card.open .fold::after { content: " ▴"; }

@media (pointer: coarse) {
  button, select { padding: 7px 10px; }
  .slider input { margin: 5px 0; }
  .speed input { width: 90px; }
}
/* A phone held upright. */
@media (max-width: 640px) {
  .back-long { display: none; }
  .topbar { padding-top: 3px; padding-bottom: 3px; }
  #all { order: 1; }
  .readout { order: 5; flex: 1 0 100%; margin-left: 0; }
}
/* A short window: a phone held sideways. */
@media (max-height: 460px) {
  .topbar { padding-top: 2px; padding-bottom: 2px; }
  h1 { font-size: 14px; }
  .transport { padding-top: 3px; padding-bottom: max(3px, env(safe-area-inset-bottom)); flex-wrap: nowrap; font-size: 12px; }
  .transport button { padding: 4px 8px; }
  .speed span { display: none; }
  .speed input { width: 64px; }
  .long { display: none; }
}
