:root {
  --bg: #f5f3ee;
  --stage: #faf9f6;
  --panel: #ffffff;
  --panel-2: #f3f1ec;
  --ink: #22242a;
  --ink-2: #4a4d57;
  --muted: #7d808b;
  --line: #e4e1da;
  --line-2: #d4d0c7;
  --accent: #2f6f73;
  --accent-ink: #ffffff;
  --accent-soft: #dcebea;
  --danger: #c8453b;
  --danger-soft: #f8dedb;
  --warn: #d99a1e;
  --warn-soft: #fbefd2;
  --ok: #3b8a57;
  --shadow: 0 1px 2px rgba(30, 28, 20, .06), 0 6px 24px rgba(30, 28, 20, .07);
  --radius: 14px;
  --sidebar: 340px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171b;
    --stage: #1b1c21;
    --panel: #222329;
    --panel-2: #2a2b32;
    --ink: #ecebe7;
    --ink-2: #c3c2bd;
    --muted: #8f9099;
    --line: #33343c;
    --line-2: #43444d;
    --accent: #6cc0bf;
    --accent-ink: #0f1a1a;
    --accent-soft: #23393a;
    --danger: #ff7a6e;
    --danger-soft: #45231f;
    --warn: #f0b64a;
    --warn-soft: #3e3220;
    --ok: #71c68d;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 28px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

#app {
  display: grid;
  grid-template-columns: var(--sidebar) minmax(0, 1fr);
  grid-template-rows: 56px minmax(0, 1fr);
  grid-template-areas: "top top" "side stage";
  height: 100dvh;
}

/* ---------- top bar ---------- */
.topbar {
  grid-area: top;
  min-width: 0;
  display: flex; align-items: center; gap: 14px;
  padding: 0 14px 0 18px;
  padding-top: env(safe-area-inset-top, 0px);
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; white-space: nowrap; }
.brand-mark {
  width: 22px; height: 22px; border-radius: 50%;
  background:
    radial-gradient(circle at 30% 32%, #8fb3e8 0 22%, transparent 23%),
    radial-gradient(circle at 72% 34%, #e9a17a 0 22%, transparent 23%),
    radial-gradient(circle at 50% 74%, #7cc3a4 0 22%, transparent 23%);
  box-shadow: inset 0 0 0 1.5px var(--line-2);
}
.brand-name { font-family: var(--serif); font-size: 19px; letter-spacing: .01em; }
.chips { display: flex; gap: 8px; flex: 1; justify-content: center; min-width: 0; overflow: hidden; }
.chip {
  display: inline-flex; align-items: baseline; gap: 6px;
  padding: 4px 11px; border-radius: 999px;
  background: var(--panel-2); color: var(--ink-2);
  font-size: 13px; white-space: nowrap;
  transition: background .25s, color .25s;
}
.chip b { color: var(--ink); font-variant-numeric: tabular-nums; font-weight: 600; }
.chip.bad { background: var(--danger-soft); color: var(--danger); }
.chip.bad b { color: var(--danger); }
.chip.good { background: var(--accent-soft); color: var(--accent); }
.chip.good b { color: var(--accent); }
.chip.warn { background: var(--warn-soft); }
.top-actions { display: flex; gap: 6px; }

.icon-btn {
  width: 34px; height: 34px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--panel);
  display: inline-grid; place-items: center; cursor: pointer;
  font-size: 17px; line-height: 1; color: var(--ink-2);
  transition: background .15s, border-color .15s, transform .1s;
}
.icon-btn:hover { background: var(--panel-2); border-color: var(--line-2); }
.icon-btn:active { transform: scale(.95); }
.icon-btn:disabled { opacity: .35; cursor: default; }

/* ---------- sidebar ---------- */
.sidebar {
  grid-area: side;
  overflow-y: auto;
  padding: 14px 14px 24px;
  background: var(--bg);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 12px;
}
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 14px 12px;
  display: flex; flex-direction: column; gap: 10px;
}
.card h2 {
  margin: 0; font: 600 12px/1 var(--sans);
  text-transform: uppercase; letter-spacing: .09em; color: var(--muted);
}
.card h3 { margin: 6px 0 0; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 12.5px; color: var(--ink-2); }
.field.inline { flex-direction: row; align-items: center; justify-content: space-between; gap: 10px; }
select, input[type=number], input[type=text], textarea {
  border: 1px solid var(--line-2); border-radius: 9px;
  background: var(--panel); padding: 6px 9px; min-width: 0;
}
select:focus, input:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type=number] { width: 64px; font-variant-numeric: tabular-nums; }
textarea { width: 100%; min-height: 160px; font: 12.5px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; resize: vertical; }
.params { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 8px; }
.params .field input { width: 100%; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.btn {
  border: 1px solid var(--line-2); background: var(--panel);
  border-radius: 9px; padding: 6px 12px; cursor: pointer;
  transition: background .15s, border-color .15s, transform .1s;
  white-space: nowrap;
}
.btn:hover { background: var(--panel-2); }
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { padding: 3px 8px; font-size: 12.5px; }
.stepper { display: inline-flex; align-items: center; gap: 2px; }
.stepper .btn { padding: 4px 9px; }
.stepper input { text-align: center; width: 52px; }
.toggle { display: flex; align-items: center; gap: 9px; cursor: pointer; user-select: none; }
.toggle input { accent-color: var(--accent); width: 16px; height: 16px; margin: 0; }
.note { margin: 0; color: var(--ink-2); }
.small { font-size: 12.5px; }
.muted { color: var(--muted); }
.side-foot { padding: 0 4px; }
details.adv summary { cursor: pointer; color: var(--ink-2); font-size: 12.5px; }
details.adv[open] summary { margin-bottom: 8px; }
.matrix { display: grid; grid-template-columns: auto 1fr 1fr; gap: 6px 8px; align-items: center; font-size: 12.5px; }
.matrix input { width: 100%; }

.segmented { display: grid; grid-template-columns: 1fr 1fr; background: var(--panel-2); padding: 3px; border-radius: 10px; }
.segmented button {
  border: 0; background: transparent; padding: 6px 0; border-radius: 8px; cursor: pointer; color: var(--ink-2);
  transition: background .2s, color .2s, box-shadow .2s;
}
.segmented button.on { background: var(--panel); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
body.mode-free .puzzle-only { display: none; }

.solver-status {
  display: flex; align-items: center; gap: 10px;
  background: var(--panel-2); border-radius: 10px; padding: 8px 10px;
}
.solver-text { flex: 1; font-size: 12.5px; font-variant-numeric: tabular-nums; color: var(--ink-2); }
.spinner {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--line-2); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.progress { height: 8px; border-radius: 99px; background: var(--panel-2); overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--accent); border-radius: 99px; transition: width .35s cubic-bezier(.2,.8,.2,1); }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; }
.stat { display: flex; justify-content: space-between; font-size: 13px; color: var(--ink-2); }
.stat b { color: var(--ink); font-variant-numeric: tabular-nums; }
.stat.bad b { color: var(--danger); }
.dist { display: flex; flex-direction: column; gap: 3px; }
.dist-row { display: grid; grid-template-columns: 22px 1fr 60px; gap: 8px; align-items: center; font-size: 12px; font-variant-numeric: tabular-nums; }
.dist-sw { width: 20px; height: 20px; border-radius: 50%; display: grid; place-items: center; font-size: 10.5px; font-weight: 700; }
.dist-bar { height: 6px; border-radius: 3px; background: var(--panel-2); position: relative; overflow: hidden; }
.dist-bar > i { position: absolute; inset: 0 auto 0 0; border-radius: 3px; }
.dist-bar > s { position: absolute; top: -2px; bottom: -2px; width: 1.5px; background: var(--ink-2); opacity: .5; }
.dist-row span:last-child { text-align: right; color: var(--ink-2); }

.gallery { display: flex; flex-direction: column; gap: 6px; }
.gallery-item {
  display: grid; grid-template-columns: 1fr auto auto; gap: 6px; align-items: center;
  padding: 6px 8px; border-radius: 9px; background: var(--panel-2); font-size: 12.5px;
}
.gallery-item b { font-variant-numeric: tabular-nums; }
.gallery-empty { font-size: 12.5px; color: var(--muted); }

/* ---------- stage ---------- */
.stage { grid-area: stage; position: relative; overflow: hidden; background: var(--stage); }
#canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: crosshair; display: block; }
#canvas.panning { cursor: grabbing; }
.view-controls { position: absolute; right: 14px; top: 14px; display: flex; flex-direction: column; gap: 6px; }
.view-controls .icon-btn { box-shadow: var(--shadow); }

.palette-bar {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 0 14px calc(14px + env(safe-area-inset-bottom, 0px));
  display: flex; justify-content: center; pointer-events: none;
}
.palette {
  pointer-events: auto;
  display: flex; gap: 6px; padding: 8px; max-width: 100%;
  overflow-x: auto; scrollbar-width: thin;
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border: 1px solid var(--line); border-radius: 18px; box-shadow: var(--shadow);
}
.swatch {
  flex: 0 0 auto; position: relative;
  width: 38px; height: 38px; border-radius: 50%;
  border: 0; cursor: pointer; font-weight: 700; font-size: 14px;
  display: grid; place-items: center; font-variant-numeric: tabular-nums;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.12);
  transition: transform .15s cubic-bezier(.3,1.6,.5,1), box-shadow .15s;
}
.swatch:hover { transform: translateY(-2px); }
.swatch.on { transform: scale(1.08); box-shadow: 0 0 0 2.5px var(--panel), 0 0 0 4.5px var(--ink); }
.swatch.on:hover { transform: translateY(-2px) scale(1.08); }
.swatch.dim { opacity: .35; }
.swatch .count {
  position: absolute; right: -3px; bottom: -3px; min-width: 16px; height: 16px; padding: 0 3px;
  border-radius: 8px; font-size: 9.5px; line-height: 16px; font-weight: 600;
  background: var(--panel); color: var(--ink-2); box-shadow: 0 0 0 1px var(--line-2);
}
.swatch.eraser { background: var(--panel-2); color: var(--ink-2); font-size: 16px; }
.palette .sep { width: 1px; background: var(--line); margin: 4px 2px; flex: 0 0 auto; }

.tooltip {
  position: absolute; pointer-events: none; z-index: 4;
  max-width: 300px; padding: 8px 10px; border-radius: 10px;
  background: var(--panel); border: 1px solid var(--line); box-shadow: var(--shadow);
  font-size: 12.5px; color: var(--ink-2);
}
.tooltip b { color: var(--ink); }
.tooltip .bad { color: var(--danger); }
.tooltip .good { color: var(--ok); }

.toasts {
  position: absolute; left: 50%; bottom: calc(82px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%);
  display: flex; flex-direction: column-reverse; align-items: center; gap: 8px; pointer-events: none; z-index: 6;
  width: min(560px, calc(100% - 32px));
}
.toast {
  pointer-events: auto;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px; border-radius: 12px;
  background: var(--ink); color: var(--bg); box-shadow: var(--shadow);
  font-size: 13.5px;
  animation: toast-in .35s cubic-bezier(.2,1.2,.4,1);
}
.toast.bad { background: var(--danger); color: #fff; }
.toast.good { background: var(--accent); color: var(--accent-ink); }
.toast button { border: 0; background: rgba(255,255,255,.18); color: inherit; border-radius: 8px; padding: 4px 10px; cursor: pointer; white-space: nowrap; }
.toast button:hover { background: rgba(255,255,255,.28); }
.toast.out { animation: toast-out .25s ease forwards; }
@keyframes toast-in { from { opacity: 0; transform: translateY(12px) scale(.96); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(8px); } }

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(20, 20, 24, .38);
  display: grid; place-items: center; padding: 16px;
  animation: fade .2s ease;
}
@keyframes fade { from { opacity: 0; } }
.modal {
  position: relative; width: min(620px, 100%); max-height: calc(100dvh - 32px); overflow: auto;
  background: var(--panel); border-radius: 18px; padding: 24px 24px 20px; box-shadow: var(--shadow);
  animation: pop .3s cubic-bezier(.2,1.3,.4,1);
}
@keyframes pop { from { transform: scale(.95); opacity: 0; } }
.modal h2 { margin: 0 0 12px; font: 500 24px/1.2 var(--serif); }
.modal p { margin: 0 0 10px; color: var(--ink-2); }
.modal ul { margin: 0 0 10px; padding-left: 20px; color: var(--ink-2); }
.modal li { margin: 3px 0; }
.modal-close { position: absolute; right: 14px; top: 14px; }
.modal .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; flex-wrap: wrap; }
.win-score { font: 500 64px/1 var(--serif); color: var(--accent); margin: 6px 0 4px; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; margin: 8px 0 14px; font-size: 13.5px; }
.facts b { font-variant-numeric: tabular-nums; }

#confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 30; }

.only-mobile { display: none; }

/* ---------- mobile ---------- */
@media (max-width: 820px) {
  #app { grid-template-columns: minmax(0, 1fr); grid-template-areas: "top" "stage"; }
  .only-mobile { display: inline-grid; }
  .brand-name { font-size: 16px; }
  .chips { justify-content: flex-start; }
  .chip.opt { display: none; }
  .sidebar {
    position: fixed; z-index: 15; top: 56px; bottom: 0; left: 0;
    width: min(360px, 92vw);
    transform: translateX(-102%); transition: transform .3s cubic-bezier(.2,.8,.2,1);
    box-shadow: var(--shadow);
  }
  body.drawer-open .sidebar { transform: none; }
  .swatch { width: 36px; height: 36px; }
}
@media (max-width: 480px) {
  .brand { display: none; }
  .topbar { gap: 6px; padding: 0 8px; }
  .chip { padding: 4px 8px; font-size: 12px; }
  .top-actions { gap: 4px; }
  .view-controls { right: 10px; top: 10px; }
}
