:root {
  color-scheme: light dark;
  --bg: #f6f7f5;
  --surface: #ffffff;
  --surface-2: #eceeea;
  --text: #15191a;
  --muted: #4a524f;
  --border: #dde0da;
  --border-strong: #7a8278;
  --accent: #0b6b5f;
  --on-accent: #ffffff;
  --error: #b3261e;
  --stage: #ffffff;
  --checker-a: #e4e7e2;
  --checker-b: #f6f7f5;
  --radius: 8px;
  --radius-card: 12px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121614;
    --surface: #1a1f1c;
    --surface-2: #232924;
    --text: #e7ece8;
    --muted: #a9b3ad;
    --border: #2e3530;
    --border-strong: #7f8a83;
    --accent: #5bc4ae;
    --on-accent: #0b1f1a;
    --error: #ff8a80;
    --stage: #0d100e;
    --checker-a: #1c211e;
    --checker-b: #121614;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121614;
  --surface: #1a1f1c;
  --surface-2: #232924;
  --text: #e7ece8;
  --muted: #a9b3ad;
  --border: #2e3530;
  --border-strong: #7f8a83;
  --accent: #5bc4ae;
  --on-accent: #0b1f1a;
  --error: #ff8a80;
  --stage: #0d100e;
  --checker-a: #1c211e;
  --checker-b: #121614;
}

:root[data-theme="light"] {
  color-scheme: light;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { scroll-padding-top: 300px; }
@media (max-height: 500px) { html { scroll-padding-top: 0; } }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--font);
  overflow-wrap: anywhere;
}

h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 24px; }
h2 { font-size: 20px; }
h3 { font-size: 16px; }
p { margin: 0; }
code, pre, .mono { font-family: var(--mono); font-size: 14px; }
.muted { color: var(--muted); font-weight: 400; }
.help { color: var(--muted); font-size: 14px; max-width: 70ch; }
.note { color: var(--muted); font-size: 14px; padding: 8px 0; }
.row { display: flex; flex-wrap: wrap; gap: 8px; }

.site-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0 16px;
  padding: 8px 16px 16px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 14px;
}
.site-footer a { display: inline-flex; align-items: center; min-height: 44px; color: var(--accent); }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  min-height: 56px;
  padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.file-name { flex: 1 1 160px; color: var(--muted); font-size: 14px; }
.bar-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 8px 16px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  transition: background 150ms ease-out, border-color 150ms ease-out;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }

.theme { display: inline-flex; border: 1px solid var(--border-strong); border-radius: var(--radius); overflow: hidden; }
.theme button {
  min-height: 44px;
  min-width: 44px;
  padding: 0 12px;
  border: 0;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
.theme button[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }

.layout {
  display: grid;
  gap: 16px;
  padding: 16px;
  max-width: 1280px;
  margin: 0 auto;
  grid-template-columns: minmax(0, 1fr);
}
.stage-column { display: contents; }


.stage {
  position: sticky;
  top: 0;
  z-index: 2;
  display: grid;
  place-items: center;
  height: clamp(200px, 40vh, 280px);
  background: var(--stage);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-card);
  overflow: hidden;
}
.stage[data-backdrop="checker"] {
  background:
    conic-gradient(var(--checker-a) 25%, var(--checker-b) 0 50%, var(--checker-a) 0 75%, var(--checker-b) 0) 0 0 / 24px 24px;
}
.stage[data-backdrop="light"] { background: #ffffff; }
.stage[data-backdrop="dark"] { background: #0d100e; }
.stage > * { grid-area: 1 / 1; }
.stage canvas { width: 100%; height: 100%; display: block; }

.empty, .error, .loading { padding: 24px; text-align: center; display: grid; gap: 12px; justify-items: center; max-width: 520px; }
.empty h2 { font-size: 20px; }
.empty p { color: var(--muted); }
.stage.is-dragging { border-color: var(--accent); border-style: dashed; border-width: 2px; }
.stage.is-empty { border-style: dashed; border-width: 2px; background: var(--surface); }
.error { background: var(--surface); border: 2px solid var(--error); border-radius: var(--radius-card); text-align: left; justify-items: stretch; }
.error h2 { color: var(--error); }
.error pre { margin: 8px 0 0; white-space: pre-wrap; max-height: 160px; overflow: auto; }
.error summary { min-height: 44px; display: flex; align-items: center; cursor: pointer; }

.loading { grid-auto-flow: column; color: var(--muted); }
.spinner {
  width: 20px;
  height: 20px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 800ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.playbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; }

.field { display: grid; gap: 4px; margin-top: 12px; }
.field > span { font-size: 14px; color: var(--muted); }
.field.inline { display: flex; align-items: center; gap: 8px; margin: 0; }
select, input[type="text"], input[type="number"], input.hex {
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  width: 100%;
}
.field.inline select { width: auto; }
select:disabled { opacity: 0.6; }

.cards { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 8px; grid-template-columns: 1fr; }
.card {
  display: grid;
  gap: 4px;
  width: 100%;
  min-height: 64px;
  padding: 12px;
  text-align: left;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-card);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  transition: background 150ms ease-out;
}
.card:hover { background: var(--surface-2); }
.card[aria-current="true"] { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.card .title { font-weight: 600; }
.card .desc { color: var(--muted); font-size: 14px; }
.pill { justify-self: start; padding: 0 8px; border-radius: 999px; background: var(--surface-2); font: 12px/20px var(--mono); }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-card); padding: 16px; min-width: 0; }
.panel details { border-top: 1px solid var(--border); padding: 12px 0; }
.panel details:first-of-type { border-top: 0; padding-top: 0; }
.panel summary { cursor: pointer; min-height: 44px; display: flex; align-items: center; }
.panel summary h2 { font-size: 20px; }

.control { display: grid; gap: 4px; padding: 12px 0; border-top: 1px solid var(--border); }
.control:first-child { border-top: 0; }
.control-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 8px; }
.switch-row { display: flex; align-items: center; justify-content: space-between; min-height: 44px; gap: 12px; cursor: pointer; }
.switch-row input { width: 44px; height: 28px; accent-color: var(--accent); }
.color-row { display: grid; grid-template-columns: 44px 1fr; gap: 8px; }
.color-row input[type="color"] { width: 44px; height: 44px; padding: 0; border: 1px solid var(--border-strong); border-radius: var(--radius); background: var(--surface); }
.group-title { margin: 12px 0 0; padding-top: 12px; border-top: 1px solid var(--border); }
.status-text { font-size: 14px; color: var(--muted); min-height: 21px; }

.drop-veil {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--accent) 80%, transparent);
  color: var(--on-accent);
  font-size: 24px;
  pointer-events: none;
}
.drop-veil[hidden] { display: none; }

@media (min-width: 768px) {
  .bar, .layout { padding-left: 24px; padding-right: 24px; }
  .stage-column { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
  .stage { position: static; height: auto; aspect-ratio: 4 / 3; max-height: 56vh; }
  .cards { grid-template-columns: repeat(2, 1fr); }
  html { scroll-padding-top: 0; }
}

@media (min-width: 1280px) {
  .bar, .layout { padding-left: 32px; padding-right: 32px; }
  .layout {
    grid-template-columns: minmax(640px, 1fr) 360px;
    grid-template-areas: "stage panel" "examples panel";
    align-items: start;
  }
  .panel { position: sticky; top: 16px; max-height: calc(100vh - 32px); overflow: auto; }
  .stage { aspect-ratio: auto; height: min(56vh, 640px); max-height: none; }
  .cards { grid-template-columns: repeat(3, 1fr); }
  .stage-column { grid-area: stage; }
  .panel { grid-area: panel; }
  .examples { grid-area: examples; }
}

@media (max-height: 500px) {
  .stage { position: static; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
  .spinner { animation: none; border-top-color: var(--border); }
}

@media (forced-colors: active) {
  .btn, select, input, .card, .stage, .panel { border-color: CanvasText; }
  .btn-primary, .theme button[aria-pressed="true"] { background: Highlight; color: HighlightText; }
  .card[aria-current="true"] { outline: 3px solid Highlight; }
}
