:root {
  /* Initial uses the fallback; an empty toggle contributes no tokens. */
  --theme-light: initial;
  --theme-dark: ;
  color-scheme: var(--theme-light, light) var(--theme-dark, dark);
  --bg: var(--theme-light, #f5f6f3) var(--theme-dark, #141c18);
  --surface: var(--theme-light, #ffffff) var(--theme-dark, #1d2821);
  --surface-soft: var(--theme-light, #edf1ec) var(--theme-dark, #28372d);
  --text: var(--theme-light, #202c27) var(--theme-dark, #edf4ed);
  --muted: var(--theme-light, #57645c) var(--theme-dark, #b4c4b7);
  --accent: var(--theme-light, #23634c) var(--theme-dark, #a0d9b6);
  --accent-hover: var(--theme-light, #184a38) var(--theme-dark, #bceacc);
  --accent-soft: var(--theme-light, #e8f2eb) var(--theme-dark, #2a4133);
  --on-accent: var(--theme-light, #ffffff) var(--theme-dark, #14261b);
  --border: var(--theme-light, #d6ded6) var(--theme-dark, #3e5144);
  --control-border: var(--theme-light, #85958a) var(--theme-dark, #7b9482);
  --error: var(--theme-light, #ad2637) var(--theme-dark, #ffabba);
  --focus: var(--theme-light, #1269b1) var(--theme-dark, #84c9ff);
  --check-a: var(--theme-light, #e3e8e1) var(--theme-dark, #28362c);
  --check-b: var(--theme-light, #f1f4ee) var(--theme-dark, #233026);
  --grid: var(--theme-light, #1c6549) var(--theme-dark, #a0d9b6);
  --grid-halo: var(--theme-light, #ffffff) var(--theme-dark, #14261b);
  --shadow: var(--theme-light, 0 3px 16px #24372a05) var(--theme-dark, none);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --theme-light: ;
    --theme-dark: initial;
  }
}

:root[data-theme="dark"] {
  --theme-light: ;
  --theme-dark: initial;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; font-size: 14px; line-height: 1.6; }
button, input, select { font: inherit; }
button, input, select, a, .file-picker { -webkit-tap-highlight-color: transparent; }
button, .button { cursor: pointer; }
button, .button, input:not([type="checkbox"]):not([type="radio"]):not([type="file"]), select { min-height: 42px; border: 1px solid var(--control-border); border-radius: 8px; }
button, .button { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 9px 14px; background: var(--surface); color: var(--text); font-weight: 600; text-decoration: none; line-height: 1.4; }
button:hover:not(:disabled), .button:hover { background: var(--surface-soft); border-color: var(--accent); }
button:disabled, input:disabled, select:disabled { cursor: default; }
button:disabled { color: var(--muted); background: var(--surface-soft); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }
input:focus-visible, select:focus-visible { outline-offset: 2px; }
input, select { max-width: 100%; background: var(--surface); color: var(--text); accent-color: var(--accent); }
input[type="number"], select { width: 100%; padding: 8px 10px; }
input[type="checkbox"], input[type="radio"] { width: 17px; height: 17px; margin: 0; flex-shrink: 0; }
input[aria-invalid="true"] { border-color: var(--error) !important; }
fieldset { min-width: 0; padding: 0; margin: 0; border: 0; }
h1, h2, h3, p { margin: 0; }
h1 { font-size: clamp(26px, 3.4vw, 36px); line-height: 1.4; letter-spacing: -1.2px; font-weight: 750; }
h2 { font-size: 16px; font-weight: 700; letter-spacing: -0.4px; }
h3 { font-size: 13px; }
[hidden] { display: none !important; }
.site-header, main, footer { width: min(1200px, calc(100% - 32px)); margin-inline: auto; }
.site-header { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; padding-block: 22px; border-bottom: 1px solid var(--border); }
.brand { display: flex; gap: 10px; align-items: center; color: var(--text); font-size: 16px; font-weight: 750; text-decoration: none; }
.brand-arrow { padding-inline: 3px; color: var(--muted); }
.brand-mark { display: grid; grid-template-columns: repeat(2, 7px); gap: 3px; transform: rotate(-8deg); }
.brand-mark i { width: 7px; height: 7px; background: var(--accent); border-radius: 1px; }
.brand-mark i:last-child { opacity: 0.5; }
.local-badge { color: var(--muted); font-size: 11px; }
.local-badge span { color: var(--accent); font-size: 9px; margin-right: 5px; }
.intro { padding-block: 34px 25px; }
.eyebrow { margin-bottom: 8px; color: var(--accent); font-size: 12px; font-weight: 700; letter-spacing: 1px; }
.intro > p:last-child { color: var(--muted); margin-top: 10px; }
.upload-panel { padding: 18px; margin-bottom: 24px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); }
.drop-zone { display: flex; flex-direction: column; align-items: center; gap: 16px; padding: 22px 12px; border: 1px dashed var(--control-border); border-radius: 8px; background: var(--surface); text-align: center; }
.drop-zone.drag-over { border: 2px solid var(--accent); background: var(--accent-soft); }
.upload-icon { display: grid; flex-shrink: 0; place-items: center; width: 44px; height: 44px; border-radius: 12px; background: var(--accent-soft); color: var(--accent); font-size: 28px; line-height: 1; }
.upload-copy p { margin-top: 5px; color: var(--muted); font-size: 12px; }
.separator { padding-inline: 7px; }
.file-picker { position: relative; flex-shrink: 0; }
.file-picker input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; }
.file-picker:focus-within { outline: 3px solid var(--focus); outline-offset: 3px; border-radius: 8px; }
.file-status { margin-top: 10px; color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
.workspace { display: grid; gap: 20px; align-items: start; }
.editing-column { display: grid; gap: 20px; min-width: 0; }
.panel { padding: 20px; min-width: 0; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); box-shadow: var(--shadow); }
.section-heading { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 22px; }
.section-heading h2 { display: flex; align-items: center; gap: 9px; }
.step { display: inline-grid; place-items: center; width: 25px; height: 25px; border-radius: 7px; background: var(--accent-soft); color: var(--accent); font-size: 11px; font-weight: 700; }
.meta, .hint { color: var(--muted); font-size: 12px; }
.meta { font-variant-numeric: tabular-nums; }
.mode-control { display: flex; flex-wrap: wrap; gap: 18px; margin-bottom: 18px; }
.mode-control legend { float: left; margin-right: 20px; color: var(--muted); font-size: 12px; padding-top: 2px; }
.mode-control label, .checkbox-label { display: inline-flex; align-items: center; gap: 7px; cursor: pointer; }
.grid-controls, .options-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 12px; }
.field { display: flex; flex-direction: column; min-width: 0; gap: 6px; }
.field label, .background-options legend { font-size: 12px; font-weight: 600; }
.field label span { margin-left: 3px; font-weight: 400; color: var(--muted); }
.error { color: var(--error); font-size: 12px; overflow-wrap: anywhere; }
.error:empty { display: none; }
p.error:not(:empty) { margin-block: 10px; }
.checkerboard { background-color: var(--check-b); background-image: conic-gradient(var(--check-a) 25%, transparent 0 50%, var(--check-a) 0 75%, transparent 0); background-size: 16px 16px; }
.source-stage { display: grid; place-items: center; min-height: 245px; margin-top: 22px; border: 1px solid var(--border); border-radius: 8px; padding: 16px; overflow: hidden; }
canvas { display: block; max-width: 100%; image-rendering: pixelated; }
#source-preview { width: auto; height: auto; }
.empty-state { display: flex; flex-direction: column; align-items: center; gap: 18px; padding: 20px 0; text-align: center; color: var(--muted); font-size: 12px; }
.empty-grid { display: grid; grid-template-columns: repeat(2, 19px); gap: 4px; }
.empty-grid i { width: 19px; height: 19px; border: 1px solid var(--control-border); border-radius: 3px; background: var(--surface); }
.empty-grid i:last-child { transform: translate(4px, 4px); background: var(--accent-soft); border-color: var(--accent); }
.panel-note { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 6px; margin-top: 12px; color: var(--muted); font-size: 11px; }
.grid-key { display: inline-block; width: 9px; height: 9px; border: 1.5px solid var(--accent); vertical-align: middle; margin-right: 5px; }
.frame-toolbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 16px; }
.button-group { display: flex; gap: 7px; }
button.small { min-height: 34px; padding: 7px 10px; font-size: 12px; }
.thumbnail-strip { display: flex; gap: 10px; max-width: 100%; overflow-x: auto; padding: 5px 4px 12px; min-height: 104px; scrollbar-color: var(--control-border) var(--surface-soft); }
.thumbnail-strip > .hint { margin-block: auto; }
.thumbnail { display: flex; flex: 0 0 78px; flex-direction: column; gap: 7px; padding: 7px; border: 1px solid var(--control-border); font-size: 11px; font-weight: 500; }
.thumbnail[aria-pressed="true"] { border: 2px solid var(--accent); padding: 6px; background: var(--accent-soft); color: var(--accent); }
.thumbnail canvas { width: 60px; height: 54px; object-fit: contain; }
.thumbnail[aria-pressed="false"] canvas { opacity: 0.5; }
.sequence-heading { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px; margin-top: 14px; padding-top: 18px; border-top: 1px solid var(--border); }
.sequence { display: flex; flex-wrap: wrap; gap: 8px; padding: 0; margin: 12px 0 0; list-style: none; }
.sequence li { display: flex; align-items: center; gap: 5px; padding: 4px 5px 4px 9px; border: 1px solid var(--border); border-radius: 7px; color: var(--muted); font-size: 12px; }
.sequence button { min-height: 32px; min-width: 30px; padding: 4px; border: 0; background: var(--surface-soft); }
#sequence-empty { margin-top: 10px; }
.preview-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
.player-stage { display: grid; place-items: center; min-height: 210px; padding: 20px; border: 1px solid var(--border); border-radius: 8px; }
#player { width: auto; height: auto; max-height: 320px; object-fit: contain; }
.player-toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 12px; margin-bottom: 24px; }
.player-toolbar button { min-width: 90px; }
.options-grid { row-gap: 16px; }
.wide { grid-column: 1 / -1; }
.background-options { margin-top: 20px; }
.background-options legend { margin-bottom: 10px; }
.background-options .hint { margin-top: 9px; font-size: 11px; }
.checkbox-label { font-size: 13px; }
.color-field { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 12px; font-size: 12px; }
input[type="color"] { width: 64px; padding: 4px; }
.export-area { margin-top: 23px; padding-top: 20px; border-top: 1px solid var(--border); }
.export-summary { color: var(--muted); font-size: 12px; margin-bottom: 12px; }
button.primary { width: 100%; min-height: 48px; background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
button.primary:hover:not(:disabled) { background: var(--accent-hover); }
button.primary:disabled { background: var(--surface-soft); border-color: var(--border); color: var(--muted); opacity: 1; }
.primary > span { font-size: 20px; line-height: 1; }
.export-status { color: var(--accent); font-size: 12px; margin-top: 10px; }
.export-hint { margin-top: 12px; text-align: center; font-size: 11px; }
footer { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px; padding-block: 28px; margin-top: 10px; color: var(--muted); font-size: 11px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }

@media (min-width: 680px) {
  .drop-zone { flex-direction: row; text-align: left; padding: 23px; }
  .upload-copy { flex: 1; }
  .grid-controls { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .source-stage { min-height: 310px; }
  .site-header { padding-block: 25px; }
  .local-badge { font-size: 12px; }
  .intro { padding-block: 42px 30px; }
}

@media (min-width: 960px) {
  .workspace { grid-template-columns: minmax(0, 1fr) 324px; }
  .panel { padding: 24px; }
  .source-stage { min-height: 330px; }
  .upload-panel { padding: 14px; }
}
