:root {
  --bg: #f3f1ec;
  --surface: #ffffff;
  --surface-2: #f8f7f4;
  --ink: #1f2328;
  --muted: #6b6f76;
  --line: #d9d6cf;
  --accent: #e0453a;
  --accent-ink: #ffffff;
  --ok: #2f8a4c;
  --warn: #a86a00;
  --board-ink: #2a2a2a;
  --crane: #8a8f98;
  --slot: #fbfaf7;
  --none: #a6a9ae;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 2px 8px rgb(0 0 0 / 0.05);
  --rainbow: linear-gradient(135deg, hsl(0 90% 70%), hsl(60 90% 70%), hsl(120 90% 70%), hsl(180 90% 70%), hsl(240 90% 72%), hsl(300 90% 72%));
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16181c;
    --surface: #1f2227;
    --surface-2: #262a30;
    --ink: #e8e6e1;
    --muted: #9aa0a8;
    --line: #353a41;
    --accent: #ff6a5c;
    --accent-ink: #1a0b09;
    --ok: #5cc27c;
    --warn: #e0a640;
    --board-ink: #0c0d0f;
    --crane: #9aa1ab;
    --slot: #2a2e35;
    --none: #6d7178;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}
h1, h2, h3 { line-height: 1.2; margin: 0; }
a { color: inherit; }
code { font-size: 0.92em; background: var(--surface-2); padding: 0 4px; border-radius: 4px; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------------- buttons */
button, .button {
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
button:hover:not(:disabled), .button:hover { border-color: var(--muted); }
button:disabled { opacity: 0.45; cursor: default; }
button:focus-visible, .button:focus-within, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.primary:hover:not(:disabled) { filter: brightness(1.06); border-color: var(--accent); }
.quiet { background: transparent; }
.danger { color: var(--accent); }
.small { padding: 2px 8px; font-size: 13px; }
button.link { background: none; border: 0; padding: 0; color: var(--muted); text-decoration: underline; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row.tight { gap: 6px; }
.hint { color: var(--muted); font-size: 13px; margin: 6px 0; }
.error, .page-error { color: var(--accent); }
.page-error { max-width: 640px; margin: 40px auto; padding: 0 16px; }

input, textarea, select {
  font: inherit;
  color: var(--ink);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 8px;
  width: 100%;
}
textarea { resize: vertical; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 13px; }
label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
label.button, label.toggle { display: inline-flex; color: var(--ink); font-size: inherit; }
label.toggle { gap: 6px; align-items: center; cursor: pointer; }
label.toggle input { width: auto; }

/* ----------------------------------------------------------------- topbar */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.topbar h1 { font-size: 18px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .back { text-decoration: none; color: var(--muted); }
.topbar .actions { display: flex; gap: 6px; flex-wrap: wrap; }
.dirty { color: var(--muted); font-weight: 400; font-size: 14px; }

/* ------------------------------------------------------------------- menu */
.hero { max-width: 1100px; margin: 0 auto; padding: 40px 16px 8px; }
.hero h1 { font-size: 40px; letter-spacing: -0.02em; }
.hero p { color: var(--muted); font-size: 17px; margin: 8px 0 16px; }
.menu { max-width: 1100px; margin: 0 auto; padding: 8px 16px 48px; display: grid; gap: 32px; }
.menu h2 { font-size: 20px; margin-bottom: 12px; }
.section-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.section-head h2 { margin: 0; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.card-main { text-decoration: none; padding: 12px; display: grid; gap: 8px; flex: 1; }
.card-main:hover h3 { color: var(--accent); }
.card h3 { font-size: 16px; display: flex; gap: 8px; align-items: baseline; }
.card .num { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 14px; }
.card .meta { margin: 0; color: var(--muted); font-size: 13px; display: flex; justify-content: space-between; }
.card .best { color: var(--ok); font-weight: 600; }
.card .best.star { color: var(--warn); }
.card-actions { display: flex; gap: 12px; padding: 8px 12px; border-top: 1px solid var(--line); font-size: 13px; }
.card-actions a { color: var(--muted); }
.thumbs { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 4px; background: var(--surface-2); border-radius: 6px; padding: 6px; color: var(--muted); }
.thumbs canvas { width: 100%; height: 64px; display: block; }

/* ------------------------------------------------------------------- play */
.play { max-width: 1200px; margin: 0 auto; padding: 12px 16px 40px; display: grid; gap: 12px; }
.description { margin: 0; color: var(--muted); }
.stage { display: grid; grid-template-columns: 3fr 1fr; gap: 12px; }
.board-wrap, .goal {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-width: 0;
}
#board { width: 100%; height: clamp(220px, 46vh, 520px); display: block; }
.goal { padding: 8px; display: flex; flex-direction: column; }
.goal h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
#goal { width: 100%; flex: 1; min-height: 120px; display: block; }
.banner {
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 5px solid var(--muted);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 10px 14px;
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: space-between;
}
.banner.win { border-left-color: var(--ok); }
.banner.fail { border-left-color: var(--accent); }
.banner p { margin: 0; flex: 1 1 240px; }
.banner-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.controls .spacer { flex: 1; }
.speeds { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.speeds button { border: 0; border-radius: 0; padding: 6px 10px; min-width: 38px; justify-content: center; }
.speeds button + button { border-left: 1px solid var(--line); }
.speeds button.on { background: var(--ink); color: var(--surface); }
.score { color: var(--muted); font-variant-numeric: tabular-nums; }
.score b { color: var(--ink); }

/* ---------------------------------------------------------------- program */
.program {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px;
  display: grid;
  gap: 6px;
  overflow-x: auto;
}
.line { display: flex; align-items: center; gap: 8px; }
.line-name { width: 24px; flex: none; color: var(--muted); font-size: 13px; font-weight: 600; }
.slots { display: flex; gap: 4px; }
.slot, .block {
  --size: 40px;
  width: var(--size);
  height: var(--size);
  flex: none;
  padding: 0;
  justify-content: center;
  border-radius: 7px;
  font-size: 18px;
  font-weight: 600;
  position: relative;
  color: #111;
}
.slot { background: var(--slot); border: 1px dashed var(--line); color: var(--ink); }
.slot.cond-color, .block.cond-color { background: var(--cond); color: #111; border-style: solid; }
.slot.cond-any, .block.cond-any { background: var(--rainbow); color: #111; border-style: solid; }
.slot.cond-none, .block.cond-none { background: var(--none); color: #111; border-style: solid; }
.slot.has-close { margin-right: calc(var(--close-count) * 6px + 4px); }
.slot.selected { outline: 3px solid var(--accent); outline-offset: 1px; }
.slot.active { outline: 3px solid var(--ok); outline-offset: 1px; transform: translateY(-2px); }
.slot .close {
  position: absolute;
  right: -7px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 16px;
  color: var(--accent);
  pointer-events: none;
  z-index: 1;
  letter-spacing: -2px;
}
.program.locked .slot { cursor: default; }

.palette { display: flex; flex-wrap: wrap; gap: 12px 20px; }
.palette .group { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.palette .group-name { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin-right: 4px; }
.block { background: var(--surface); border: 1px solid var(--line); color: var(--ink); }
.block.erase { color: var(--accent); }
.palette.locked { opacity: 0.5; }

/* ----------------------------------------------------------------- dialog */
dialog {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--ink);
  padding: 20px 24px;
  max-width: min(640px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  box-shadow: 0 12px 48px rgb(0 0 0 / 0.3);
}
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
dialog .close { position: absolute; right: 12px; top: 12px; border: 0; background: none; font-size: 18px; color: var(--muted); }
dialog h2 { margin-bottom: 8px; padding-right: 32px; }
dialog h3 { font-size: 15px; margin: 16px 0 4px; }
dialog p { margin: 6px 0; }
dialog.io { width: 600px; }
dialog.io textarea { margin: 8px 0; }
.help ul { padding-left: 18px; margin: 6px 0; }
.help li { margin: 4px 0; }
.chip {
  display: inline-block;
  min-width: 24px;
  padding: 0 5px;
  border: 1px solid var(--line);
  border-radius: 5px;
  text-align: center;
  font-weight: 600;
  background: var(--surface-2);
}
.chip.cond-any { background: var(--rainbow); }
.chip.cond-none { background: var(--none); }
.keys { border-collapse: collapse; font-size: 14px; }
.keys td { padding: 3px 12px 3px 0; vertical-align: top; }
kbd {
  display: inline-block;
  font: 12px ui-monospace, Menlo, monospace;
  padding: 1px 5px;
  margin-right: 2px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--surface-2);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  background: var(--ink);
  color: var(--surface);
  padding: 8px 16px;
  border-radius: 8px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  max-width: calc(100vw - 32px);
  z-index: 10;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ----------------------------------------------------------------- editor */
.editor {
  max-width: 1400px;
  margin: 0 auto;
  padding: 12px 16px 40px;
  display: grid;
  grid-template-columns: 340px 1fr;
  grid-template-areas: "details boards" "checks boards" "json boards";
  grid-template-rows: auto auto 1fr;
  gap: 12px;
  align-items: start;
}
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  display: grid;
  gap: 10px;
  min-width: 0;
}
.panel h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.details { grid-area: details; }
.boards { grid-area: boards; }
.checks { grid-area: checks; }
.json { grid-area: json; }
.json summary { cursor: pointer; }
.json summary h2 { display: inline; }
.json textarea { margin-top: 6px; }
label .hint { margin: 0; font-size: 12px; }
.details .row { display: grid; grid-template-columns: repeat(4, 1fr); }
.functions .label { font-size: 13px; color: var(--muted); }
.function-sizes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin: 4px 0 8px; }
.function-sizes label { grid-template-columns: auto 1fr; align-items: center; color: var(--ink); font-weight: 600; }
.tools-allowed { border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px 10px; margin: 0; }
.tools-allowed legend { font-size: 13px; color: var(--muted); padding: 0 4px; }
.tool-toggles { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 10px; }
.tool-toggles label { display: flex; align-items: center; gap: 6px; color: var(--ink); }
.tool-toggles input { width: auto; }
.tools { display: flex; flex-wrap: wrap; gap: 6px; }
.tool.on { outline: 3px solid var(--accent); outline-offset: 1px; }
.swatch { width: 34px; height: 34px; padding: 0; background: var(--cond); border-color: rgb(0 0 0 / 0.25); }
.board-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.board-pair figure { margin: 0; display: grid; gap: 6px; }
.board-pair figcaption { display: flex; gap: 6px; align-items: center; font-weight: 600; }
.board-pair figcaption button:first-of-type { margin-left: auto; }
.edit-canvas {
  width: 100%;
  height: clamp(240px, 50vh, 560px);
  display: block;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: crosshair;
  touch-action: none;
}
.text-edit summary { cursor: pointer; color: var(--muted); }
.checks ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; font-size: 14px; }
.checks .err { color: var(--accent); }
.checks .warn { color: var(--warn); }
.checks .ok { color: var(--ok); }

/* ----------------------------------------------------------------- narrow */
@media (max-width: 900px) {
  .editor { grid-template-columns: 1fr; grid-template-areas: "boards" "checks" "details" "json"; }
}
@media (max-width: 700px) {
  .stage { grid-template-columns: 1fr; }
  #goal { height: 120px; flex: none; }
  .slot, .block { --size: 34px; font-size: 16px; }
  .board-pair { grid-template-columns: 1fr; }
  .edit-canvas { height: 240px; }
  .hero h1 { font-size: 32px; }
}
