/* ============================================================================
   Phosphor design system — tokens first, components second.
   Everything reads from the token layer, so theming = redefine ~10 vars.
   ============================================================================ */
:root {
  /* neutral graphite ramp (dark default) */
  --bg:        #0a0b0e;
  --s-0:       #131419;   /* deepest — inputs, wells */
  --s-1:       #191a20;   /* panel surface */
  --s-2:       #22242c;   /* raised — ghost buttons, chips */
  --s-3:       #2b2d37;   /* hover */
  --line:      rgba(255, 255, 255, 0.08);
  --line-2:    rgba(255, 255, 255, 0.14);
  --text:      #eaeaf1;
  --muted:     #8a8b98;
  --faint:     #5d5e69;
  --panel:     rgba(23, 24, 30, 0.86);
  --chrome:    rgba(16, 17, 21, 0.82);

  /* accent (user-dialable via Setup → Accent) */
  --accent:      #ff2bb0;
  --accent-weak: color-mix(in srgb, var(--accent) 16%, transparent);
  --accent-soft: color-mix(in srgb, var(--accent) 28%, transparent);
  --accent-ring: color-mix(in srgb, var(--accent) 45%, transparent);
  --on-accent:   #fff;

  /* semantic */
  --warn: #ffb86b;
  --rec:  #ff3b46;

  /* spacing (4px grid) */
  --sp-1: 4px; --sp-2: 8px; --sp-3: 12px; --sp-4: 16px; --sp-5: 20px; --sp-6: 24px;
  /* radii */
  --r-1: 6px; --r-2: 8px; --r-3: 10px; --r-4: 14px; --r-full: 999px;
  /* type */
  --fz-eyebrow: 10px; --fz-xs: 11px; --fz-sm: 12px; --fz-md: 13px; --fz-lg: 14px;
  /* controls / elevation */
  --ctrl-h: 30px;
  --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.35);
  --shadow-2: 0 12px 40px rgba(0, 0, 0, 0.5);
  --ease: 0.16s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Light theme — redefine the ramp only; every component follows automatically. */
#app[data-theme="light"] {
  --bg:     #ececef;
  --s-0:    #ffffff;
  --s-1:    #f6f6f9;
  --s-2:    #e9e9ee;
  --s-3:    #dcdce3;
  --line:   rgba(0, 0, 0, 0.10);
  --line-2: rgba(0, 0, 0, 0.16);
  --text:   #1b1b22;
  --muted:  #5e5f6b;
  --faint:  #9a9ba6;
  --panel:  rgba(250, 250, 253, 0.9);
  --chrome: rgba(247, 247, 250, 0.85);
  --on-accent: #fff;
  --warn: #b4681a;
}

/* Compact density — tighter targets for power users (Setup → Density). */
#app.compact { --ctrl-h: 26px; --sp-3: 9px; --sp-2: 6px; }

* { box-sizing: border-box; }
/* The hidden attribute always wins — author display rules (e.g. button's
   inline-flex) would otherwise resurrect elements the JS has hidden. */
[hidden] { display: none !important; }
html, body {
  margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: var(--fz-md)/1.5 ui-sans-serif, system-ui, -apple-system, sans-serif; overflow: hidden;
  -webkit-text-size-adjust: 100%; text-size-adjust: 100%;
  overscroll-behavior: none;   /* no rubber-band scroll / pull-to-refresh on touch */
}
/* Kill the 300ms tap delay + double-tap-zoom on interactive controls */
button, select, input, summary, .swatch, .layer-row, .menu-btn, .tb-btn, .cat, .dot { touch-action: manipulation; }

#stage { position: fixed; inset: 0; display: grid; place-items: center; }
#frame { position: relative; line-height: 0; touch-action: none; }
#gl { max-width: 100vw; max-height: 100vh; display: block; background: #000; }
#overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }

/* ---- Start overlay ---------------------------------------------------------- */
#start-overlay { position: fixed; inset: 0; display: grid; place-items: center; z-index: 40;
  background: radial-gradient(120% 120% at 50% 30%, var(--s-1), var(--bg) 72%); }
#start-overlay .card { text-align: center; padding: var(--sp-6) 48px; border: 1px solid var(--line);
  border-radius: var(--r-4); background: var(--panel); backdrop-filter: blur(8px); max-width: 460px; box-shadow: var(--shadow-2); }
#start-overlay h1 { margin: 0 0 var(--sp-2); font-size: 34px; letter-spacing: -0.5px; }
#start-overlay h1 span { color: var(--accent); }
#start-overlay p { color: var(--muted); margin: 0 0 22px; font-size: var(--fz-lg); }
.btn-row { display: flex; flex-direction: column; gap: var(--sp-2); }
.btn-row button { width: 100%; justify-content: flex-start; gap: 11px; padding: 14px 18px;
  font-size: 15px; border-radius: var(--r-4); text-align: left; }
.btn-row button svg { width: 19px; height: 19px; }
.btn-row button span { font-weight: 500; }
#start-overlay small { display: block; margin-top: 14px; color: var(--rec); min-height: 1em; }

/* ---- Buttons ---------------------------------------------------------------- */
button { font: inherit; color: var(--on-accent); background: var(--accent); border: 0; border-radius: var(--r-3);
  padding: 9px 16px; cursor: pointer; transition: filter var(--ease), background var(--ease), color var(--ease);
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
button:hover { filter: brightness(1.1); }
button:focus-visible { outline: 2px solid var(--accent-ring); outline-offset: 2px; }
#panel svg { width: 15px; height: 15px; flex: none; }
.seg svg { width: 17px; height: 17px; }
#hide svg { width: 18px; height: 18px; }
/* neutral (refined): chrome stays graphite; accent only on active state */
button.ghost, button.mini, #record, #snap, #export, #transport button { background: var(--s-2); color: var(--text); }
button.ghost:hover, button.mini:hover, #record:hover, #snap:hover, #export:hover { background: var(--s-3); filter: none; }
button.mini { padding: 7px 10px; font-size: var(--fz-sm); width: 100%; }
button.rec, #record.rec { background: var(--rec); color: #fff; }

/* ---- Inspector panel -------------------------------------------------------- */
#panel { position: fixed; top: 12px; right: 12px; width: 216px; max-height: calc(100vh - 24px);
  overflow-y: auto; padding: var(--sp-3); border: 1px solid var(--line); border-radius: var(--r-4);
  background: var(--panel); backdrop-filter: blur(12px); display: flex; flex-direction: column; gap: var(--sp-2);
  font-size: var(--fz-sm); scrollbar-width: thin; scrollbar-color: var(--line-2) transparent; }
#panel::-webkit-scrollbar { width: 6px; }
#panel::-webkit-scrollbar-track { background: transparent; }
#panel::-webkit-scrollbar-thumb { background: var(--line-2); border-radius: 3px; }
#panel header { display: flex; align-items: center; justify-content: space-between; }
#panel header strong { font-size: var(--fz-eyebrow); text-transform: uppercase; letter-spacing: 1.6px; color: var(--muted); font-weight: 600; }
#hide { background: transparent; color: var(--muted); padding: 0 6px; font-size: 17px; }
#hide:hover { color: var(--text); filter: none; }

.brush { display: flex; align-items: center; gap: 6px; padding: 7px 10px; border: 1px solid var(--line);
  border-radius: var(--r-2); background: var(--s-0); font-size: var(--fz-eyebrow); color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.6px; }
.brush b { margin-left: auto; font-weight: 600; font-size: var(--fz-sm); text-transform: none; letter-spacing: 0; color: var(--text); }
.brush b::before { content: ''; display: inline-block; width: 8px; height: 8px; border-radius: 3px;
  background: var(--accent); margin-right: 6px; vertical-align: middle; }

/* ---- Form controls ---------------------------------------------------------- */
label { display: flex; flex-direction: column; gap: 4px; font-size: var(--fz-xs); color: var(--muted); }
label.row { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: var(--fz-sm); }
output { color: var(--text); float: right; font-variant-numeric: tabular-nums; font-size: var(--fz-xs); }

select, input[type="range"] { width: 100%; }
select { background: var(--s-0); color: var(--text); border: 1px solid var(--line); border-radius: var(--r-2);
  padding: 0 26px 0 9px; height: var(--ctrl-h); font: inherit; font-size: var(--fz-sm); cursor: pointer;
  appearance: none; -webkit-appearance: none; transition: border-color var(--ease);
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: right 12px center, right 7px center; background-size: 5px 5px, 5px 5px; background-repeat: no-repeat; }
select:hover { border-color: var(--line-2); }
select:focus-visible { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-weak); }
input[type="range"] { accent-color: var(--accent); height: 16px; margin: 0; }
input[type="checkbox"] { accent-color: var(--accent); width: 15px; height: 15px; flex: none; }
input[type="text"] { width: 100%; background: var(--s-0); color: var(--text); border: 1px solid var(--line);
  border-radius: var(--r-2); padding: 0 9px; height: var(--ctrl-h); font: inherit; font-size: var(--fz-sm); transition: border-color var(--ease); }
input[type="text"]:focus-visible { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-weak); }
input[type="color"] { width: 28px; height: 22px; padding: 0; border: 1px solid var(--line-2); border-radius: var(--r-1); background: transparent; cursor: pointer; }
#composite { display: none; }

/* ---- Sections (former accordion groups, now flat within a tab) -------------- */
.grp { display: flex; flex-direction: column; gap: var(--sp-2); padding: 0; border: 0; }
.grp + .grp { margin-top: var(--sp-1); padding-top: var(--sp-4); border-top: 1px solid var(--line); }
.grp > summary, .grp-title { cursor: default; font-size: var(--fz-eyebrow); text-transform: uppercase; letter-spacing: 1.4px;
  color: var(--faint); font-weight: 600; list-style: none; user-select: none; margin: 0 0 2px; }
.grp > summary::-webkit-details-marker { display: none; }
.grp > summary::after { display: none; }

/* ---- Tabbed inspector ------------------------------------------------------- */
.tabrail { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 2px; padding: 3px;
  background: var(--s-0); border: 1px solid var(--line); border-radius: var(--r-3); }
.tabrail .tab { background: transparent; color: var(--muted); padding: 7px 0; border-radius: var(--r-1);
  display: flex; align-items: center; justify-content: center; gap: 6px; font-size: var(--fz-xs); font-weight: 500; }
.tabrail .tab svg { width: 15px; height: 15px; }
.tabrail .tab span { letter-spacing: 0.2px; }
.tabrail .tab:hover { color: var(--text); background: var(--s-2); filter: none; }
.tabrail .tab.on { background: var(--accent-weak); color: var(--accent); }
#app[data-theme="light"] .tabrail .tab.on { color: color-mix(in srgb, var(--accent) 78%, #000); }
.tab-body { display: flex; flex-direction: column; gap: var(--sp-3); }
.tab-panel { display: flex; flex-direction: column; gap: var(--sp-3); }
.tab-panel[hidden] { display: none; }

/* 2-col rows */
.two { display: grid; grid-template-columns: 1fr 1fr; gap: var(--sp-2); }
.two.pad { padding: 1px 2px; }
.two button, .two select { width: 100%; }
#record, #snap, #export { padding: 0; height: var(--ctrl-h); }
#full-opts, #paint-opts { display: flex; flex-direction: column; gap: var(--sp-2); }

.fps { color: var(--faint); text-align: center; font-variant-numeric: tabular-nums; font-size: var(--fz-xs); }
.note { color: var(--muted); font-size: var(--fz-xs); line-height: 1.45; }
.note.warn { color: var(--warn); }

/* ---- Segmented controls ----------------------------------------------------- */
.seg { display: flex; gap: 3px; background: var(--s-0); border: 1px solid var(--line); border-radius: var(--r-3); padding: 3px; }
.seg button { flex: 1; flex-direction: column; gap: 3px; background: transparent; color: var(--muted); padding: 7px 0; border-radius: var(--r-1); transition: background var(--ease), color var(--ease); }
.seg button span { font-size: var(--fz-eyebrow); }
.seg button.on { background: var(--accent); color: var(--on-accent); box-shadow: var(--shadow-1); }
.seg button:hover { filter: none; color: var(--text); background: var(--s-2); }
.seg button.on:hover { color: var(--on-accent); background: var(--accent); }
#frame-opts, #bg-opts { display: flex; flex-direction: column; gap: var(--sp-2); }
.subs button { color: var(--muted); padding: 6px 0; }
.subs button:hover { color: var(--text); background: var(--s-2); }
.subs button.on { background: var(--s-3); color: var(--text); }

/* accent preset swatches (Setup → Accent) */
.accent-dots { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; }
.accent-dots .dot { width: 20px; height: 20px; border-radius: var(--r-full); border: 1px solid var(--line-2); cursor: pointer; padding: 0; }
.accent-dots .dot:hover { transform: scale(1.12); filter: none; }
.accent-dots .dot.on { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent-ring); }

.hdr-btns { display: flex; align-items: center; gap: 2px; }
#help { background: transparent; color: var(--muted); padding: 0 4px; }
#help:hover { color: var(--text); filter: none; }

/* ---- Gestures dialog -------------------------------------------------------- */
#gestures-dialog { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; background: rgba(0, 0, 0, 0.5); }
#gestures-dialog[hidden] { display: none; }
.gd-card { width: 330px; max-width: 90vw; max-height: 86dvh; overflow-y: auto; background: var(--panel); border: 1px solid var(--line); border-radius: var(--r-4); padding: 18px; backdrop-filter: blur(12px); box-shadow: var(--shadow-2); }
.gd-card header { display: flex; justify-content: space-between; align-items: center; font-size: var(--fz-sm); text-transform: uppercase; letter-spacing: 1.5px; color: var(--muted); margin-bottom: 14px; }
.gd-card #help-close { background: transparent; color: var(--muted); padding: 0 6px; font-size: 14px; }
.gd-card ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 11px; }
.gd-card li { display: flex; gap: 12px; align-items: center; font-size: var(--fz-md); color: var(--text); }
.gd-card li span { font-size: 22px; line-height: 1; flex: none; width: 26px; text-align: center; }
.gd-card li em { color: var(--muted); font-style: normal; }
.gd-card small { display: block; margin-top: 15px; color: var(--muted); font-size: var(--fz-xs); line-height: 1.7; }

/* ---- Transport bar ---------------------------------------------------------- */
#transport { position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%);
  display: flex; align-items: center; gap: 10px; padding: 8px 12px; z-index: 30;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--r-4);
  backdrop-filter: blur(12px); width: min(520px, calc(100vw - 24px)); box-shadow: var(--shadow-2); }
#transport[hidden] { display: none; }
#transport button { background: var(--s-2); color: var(--text); padding: 7px 9px; border-radius: var(--r-2); }
#transport #tp-loop.on { background: var(--accent); color: var(--on-accent); }
#transport #tp-seek { flex: 1; }
#transport #tp-time { color: var(--muted); font-variant-numeric: tabular-nums; font-size: var(--fz-xs); white-space: nowrap; }
#video-export { display: flex; flex-direction: column; gap: 6px; }
#video-export[hidden] { display: none; }
#export-progress { width: 100%; height: 6px; accent-color: var(--accent); }
@media (max-width: 640px) { #transport { bottom: auto; top: 12px; } }

/* ---- Timeline / region list ------------------------------------------------- */
.tl-list { display: flex; flex-direction: column; gap: 3px; max-height: 130px; overflow-y: auto; }
.tl-row { display: flex; align-items: center; gap: 6px; font-size: var(--fz-xs); color: var(--muted);
  background: var(--s-0); border: 1px solid var(--line); border-radius: var(--r-1); padding: 5px 8px; }
.tl-row b { color: var(--text); font-weight: 500; }
.tl-chip { width: 12px; height: 12px; border-radius: 3px; flex: none; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.2); }
.tl-x { margin-left: auto; background: transparent; color: var(--muted); padding: 0 4px; font-size: 12px; }
.tl-x:hover { color: var(--rec); filter: none; }

#panel[hidden] { display: none; }
#panel.collapsed { width: auto; }
#panel.collapsed > *:not(header) { display: none; }
#panel.collapsed #hide { transform: rotate(180deg); }

/* ---- Tablet: comfortable touch targets -------------------------------------- */
@media (min-width: 641px) and (max-width: 1024px) {
  #panel { gap: var(--sp-2); padding: 12px 14px calc(14px + env(safe-area-inset-bottom, 0px)); }
  #panel.collapsed { width: auto; left: 0; right: 0; }
  #panel.collapsed #hide { transform: rotate(-90deg); }
  .seg button { padding: 10px 0; }
  button { padding: 11px 16px; }
  .mini { padding: 10px 10px; }
  label.row { padding: 3px 0; }
  select, input[type="text"], input[type="color"] { height: 40px; font-size: 15px; }
  input[type="range"] { height: 26px; }   /* fatter hit area for thumbs */
}

/* ---- Phone: compact — tighter spacing, ~36px tap targets -------------------- */
@media (max-width: 640px) {
  #app { --sp-2: 6px; --sp-3: 9px; }        /* squeeze all token-driven gaps */
  #panel.collapsed { width: auto; left: 0; right: 0; }
  #panel.collapsed #hide { transform: rotate(-90deg); }
  #app #panel { padding: 8px 12px calc(10px + env(safe-area-inset-bottom, 0px)); gap: 8px; }
  .tab-body, .tab-panel { gap: 11px; }
  .grp { gap: 6px; }
  .grp-title { margin: 0; }
  .brush { padding: 7px 10px; }
  .tabrail { gap: 3px; }
  .tabrail .tab { padding: 8px 4px; }
  button { padding: 9px 14px; }
  .mini { padding: 8px 9px; }
  .seg button { padding: 8px 0; font-size: var(--fz-xs); }
  label.row { padding: 2px 0; }
  select, input[type="text"], input[type="color"] { height: 36px; font-size: 15px; }
  input[type="range"] { height: 24px; }
  .layer-list { max-height: 120px; }
  .swatch { min-height: 38px; padding: 6px 8px; }
  #start-overlay .card { padding: 24px 18px; max-width: 92vw; }
  #start-overlay h1 { font-size: 26px; }
  .btn-row button { padding: 14px 18px; }
  #transport { width: calc(100vw - 16px); padding: 9px 12px; }
}
