:root {
  --bg: #fbf6ef; --ink: #1d2340; --muted: #646a85; --card: #fff; --line: #e8e1d6;
  --accent: #ff5e3a; --accent-2: #2f7cf6; --radius: 16px;
  --surface: #fff; --surface-2: #faf8f4; --track: #f0ebe3; --hover: #efe8dd; --notice: #fff3d6;
  --bar: rgba(251,246,239,.92); --stage-a: #f4efe7; --stage-b: #e9e2d6; --shadow: rgba(29,35,64,.35);
  --font: ui-rounded, "SF Pro Rounded", "Nunito", "Segoe UI", system-ui, -apple-system, sans-serif;
  color-scheme: light;
}
/* Dark mode: the platform (or the offline stand-in) sets data-iht-theme on <html>. */
[data-iht-theme="dark"] {
  --bg: #15110d; --ink: #efe6d8; --muted: #b3a690; --card: #211a14; --line: #3a2f25;
  --accent-2: #7fb0ff;
  --surface: #241c15; --surface-2: #1c1611; --track: #3a2f25; --hover: #2f261d; --notice: #3a2c17;
  --bar: rgba(21,17,13,.92); --stage-a: #241c15; --stage-b: #120e0a; --shadow: rgba(0,0,0,.6);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
button, select, a, .swatch { touch-action: manipulation; }
body { background: var(--bg); color: var(--ink); font: 16px/1.45 var(--font); -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; }
a { color: var(--accent-2); }

.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 18px; height: 60px; color: var(--ink); }
.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; font-weight: 800; font-size: 20px; letter-spacing: -.01em; }
.logo { display: grid; grid-template-columns: 1fr 1fr; gap: 3px; width: 26px; height: 26px; transform: rotate(-8deg); }
.logo i { border-radius: 5px; }
.logo i:nth-child(1) { background: #ff5e3a; } .logo i:nth-child(2) { background: #ffc531; }
.logo i:nth-child(3) { background: #2f7cf6; } .logo i:nth-child(4) { background: #33b679; }
.top nav { display: flex; align-items: center; gap: 16px; }
.home-link { color: var(--muted); text-decoration: none; font-weight: 600; font-size: 15px; }
.home-link:hover { color: var(--ink); }
.theme-picker { font: inherit; font-size: 14px; font-weight: 600; color: var(--muted); background: transparent; border: 1.5px solid var(--line); border-radius: 999px; padding: 5px 10px; cursor: pointer; }
.theme-picker:hover { color: var(--ink); border-color: var(--ink); }
.theme-row { display: flex; gap: 8px; }
.theme-row .btn { flex: 1; padding: 9px 10px; }
.theme-row .btn.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }

/* ---- gallery ---- */
#app { max-width: 1180px; margin: 0 auto; padding: 0 18px 48px; }
.boot, .error { text-align: center; color: var(--muted); padding: 80px 0; }
.hero { text-align: center; padding: 26px 0 22px; }
.hero h1 { font-size: clamp(26px, 4vw, 40px); line-height: 1.12; margin: 0 auto 10px; max-width: 760px; letter-spacing: -.02em; }
.hero p { color: var(--muted); margin: 0 auto; max-width: 620px; }
.notice { display: inline-block; margin-top: 16px; padding: 9px 16px; background: var(--notice); border-radius: 999px; font-size: 15px; }
.link { background: none; border: 0; padding: 0; color: var(--accent-2); font-weight: 700; cursor: pointer; text-decoration: underline; }
.filters { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin: 0 0 22px; }
.chip { border: 1.5px solid var(--line); background: var(--surface); border-radius: 999px; padding: 7px 15px; font-weight: 700; font-size: 14.5px; cursor: pointer; color: var(--muted); }
.chip:hover { border-color: var(--ink); color: var(--ink); }
.chip.on { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr)); gap: 20px; }
.empty { grid-column: 1 / -1; text-align: center; color: var(--muted); padding: 40px 0; }
.card { background: var(--card); border-radius: 22px; overflow: hidden; box-shadow: 0 1px 0 var(--line), 0 12px 30px -18px var(--shadow); }
.thumb { aspect-ratio: 16/9; background: var(--track); overflow: hidden; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; filter: grayscale(.85) contrast(.9) brightness(1.12); transition: filter .5s; }
.card:hover .thumb img, .thumb.colored img { filter: none; }
.card-body { padding: 12px 14px 14px; }
.card h3 { margin: 0 0 10px; font-size: 17px; }
.levels { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; }
.level { display: grid; gap: 2px; text-align: left; padding: 8px 9px 7px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--surface); color: var(--ink); cursor: pointer; transition: transform .12s, border-color .12s, box-shadow .12s; }
.level:hover { transform: translateY(-2px); border-color: var(--ink); box-shadow: 0 6px 16px -10px rgba(29,35,64,.5); }
.level.done { border-color: #33b679; }
.lv-name { font-weight: 800; display: flex; align-items: center; gap: 5px; }
.lock { width: 14px; height: 14px; fill: var(--muted); }
.lv-meta { font-size: 11.5px; color: var(--muted); white-space: nowrap; }
.lv-name { font-size: 14.5px; }
.lv-bar { height: 5px; border-radius: 5px; background: var(--track); overflow: hidden; margin-top: 4px; }
.lv-bar i { display: block; height: 100%; background: linear-gradient(90deg, #ffc531, #ff5e3a); border-radius: 5px; }
.level.done .lv-bar i { background: #33b679; }
.lv-state { font-size: 11.5px; font-weight: 700; color: var(--muted); min-height: 1em; }
.level.locked { background: var(--surface-2); }

/* ---- play ---- */
body[data-view="play"] { overflow: hidden; overscroll-behavior: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
body[data-view="play"] .top { display: none; }
body[data-view="play"] #app { max-width: none; padding: 0; height: 100dvh; }
.play { display: grid; grid-template-rows: auto 1fr auto; height: 100%; }
.toolbar { display: flex; align-items: center; gap: 10px; padding: 8px 10px; padding-top: max(8px, env(safe-area-inset-top)); background: var(--bar); border-bottom: 1px solid var(--line); z-index: 2; }
.toolbar .title { display: grid; line-height: 1.15; min-width: 0; }
.toolbar .title strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.toolbar .title span { font-size: 13px; color: var(--muted); }
.pct { display: flex; align-items: center; gap: 8px; margin-left: auto; font-weight: 800; font-variant-numeric: tabular-nums; }
.pct-bar { width: 90px; height: 7px; border-radius: 7px; background: var(--track); overflow: hidden; }
.pct-bar i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #ffc531, #ff5e3a); transition: width .3s; }
.tools { display: flex; gap: 4px; }
.icon-btn { width: 40px; height: 40px; display: grid; place-items: center; border: 0; border-radius: 12px; background: transparent; cursor: pointer; }
.icon-btn:hover { background: var(--hover); }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: var(--ink); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn svg circle[r="1.3"] { fill: var(--ink); }
.snd-off { display: none; } body.muted .snd-on { display: none; } body.muted .snd-off { display: block; }
.stage { position: relative; min-height: 0; background: radial-gradient(circle at 50% 40%, var(--stage-a), var(--stage-b)); }
#board { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: crosshair; display: block; }
.zoom-fab { position: absolute; right: 12px; bottom: 12px; display: none; flex-direction: column; gap: 8px; z-index: 1; }
.fab { width: 46px; height: 46px; border-radius: 50%; border: 1px solid var(--line); background: var(--surface); box-shadow: 0 4px 14px -4px var(--shadow); display: grid; place-items: center; cursor: pointer; }
.fab svg { width: 22px; height: 22px; fill: none; stroke: var(--ink); stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.fab:active { transform: scale(.94); }
.menu-sound { display: none; }
.tip { position: absolute; left: 12px; right: 70px; bottom: 12px; z-index: 2; max-width: 420px;
  background: var(--ink); color: var(--bg); border-radius: 14px; padding: 10px 14px; font-size: 14px; line-height: 1.35;
  box-shadow: 0 8px 22px -8px rgba(0,0,0,.5); display: flex; gap: 10px; align-items: center; animation: fade .25s; }
.tip button { border: 0; background: transparent; color: inherit; font-weight: 800; font-size: 14px; cursor: pointer; padding: 4px 2px; }
.loading { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-weight: 600; }
.palette { display: flex; gap: 10px; overflow-x: auto; padding: 12px 14px; padding-bottom: max(12px, env(safe-area-inset-bottom)); background: var(--surface); border-top: 1px solid var(--line); scroll-snap-type: x proximity; scrollbar-width: thin; }
.swatch { position: relative; flex: 0 0 auto; width: 56px; height: 56px; border: 0; padding: 0; border-radius: 50%; background: none; cursor: pointer; scroll-snap-align: center; transition: transform .15s; }
.swatch .ring { position: absolute; inset: 0; border-radius: 50%; background: conic-gradient(var(--ink) var(--deg), var(--track) 0); }
.swatch .dot { position: absolute; inset: 4px; border-radius: 50%; background: var(--c); color: var(--fg); display: grid; place-items: center; font-weight: 800; font-size: 17px; box-shadow: inset 0 0 0 2px rgba(255,255,255,.65); }
.swatch.sel { transform: translateY(-5px) scale(1.08); }
.swatch.sel .dot { box-shadow: inset 0 0 0 3px #fff, 0 6px 14px -4px rgba(29,35,64,.55); }
.swatch.done { opacity: .45; cursor: default; }
.swatch.nudge { animation: nudge .45s; }
@keyframes nudge { 20%, 60% { transform: translateX(-4px); } 40%, 80% { transform: translateX(4px); } }

/* ---- modal ---- */
.modal-wrap { position: fixed; inset: 0; background: rgba(10,8,6,.55); display: grid; place-items: center; padding: 18px; z-index: 50; animation: fade .2s; }
.modal { background: var(--surface); color: var(--ink); border-radius: 22px; padding: 24px; width: min(520px, 100%); max-height: 92dvh; overflow: auto; box-shadow: 0 30px 60px -20px rgba(0,0,0,.4); }
.modal h2 { margin: 0 0 8px; font-size: 24px; }
.modal p { color: var(--muted); margin: 0 0 18px; }
.row { display: flex; flex-wrap: wrap; gap: 10px; }
.col { display: grid; gap: 10px; }
.btn { display: inline-flex; align-items: center; justify-content: center; padding: 11px 18px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--surface); font-weight: 700; cursor: pointer; text-decoration: none; color: var(--ink); }
.btn:hover { border-color: var(--ink); }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn.primary:hover { background: #ec4b28; }
.btn.ghost { border-color: transparent; color: var(--muted); }
.complete { position: relative; text-align: center; }
.complete img { width: 100%; border-radius: 14px; margin: 6px 0 12px; display: block; }
.complete .row { justify-content: center; }
.modal:has(.complete) { width: min(720px, 100%); }
.confetti { position: absolute; inset: -24px; pointer-events: none; overflow: hidden; }
.confetti i { position: absolute; top: -12px; left: var(--x); width: 8px; height: 13px; border-radius: 2px; background: hsl(var(--h) 85% 60%); animation: fall 2.6s calc(var(--i) * 40ms) ease-in forwards; opacity: 0; }
@keyframes fall { 0% { opacity: 1; transform: translateY(0) rotate(0); } 100% { opacity: 0; transform: translateY(560px) rotate(540deg); } }
@keyframes fade { from { opacity: 0; } }

/* ---- phones ---- */
@media (max-width: 640px) {
  /* gallery */
  .top { padding: 8px 12px; height: 52px; gap: 8px; }
  .brand { font-size: 17px; gap: 8px; white-space: nowrap; }
  .logo { width: 22px; height: 22px; gap: 2px; }
  .top nav { gap: 10px; }
  .home-link { font-size: 14px; white-space: nowrap; }
  .theme-picker { font-size: 13px; padding: 4px 6px; }
  #app { padding: 0 12px 32px; }
  .hero { padding: 10px 0 14px; }
  .hero h1 { font-size: 22px; margin-bottom: 6px; }
  .hero p { font-size: 14.5px; }
  .notice { font-size: 14px; border-radius: 14px; margin-top: 10px; }
  .filters { flex-wrap: nowrap; justify-content: flex-start; overflow-x: auto; margin: 0 -12px 16px; padding: 2px 12px; scrollbar-width: none; }
  .filters::-webkit-scrollbar { display: none; }
  .chip { flex: 0 0 auto; padding: 7px 13px; font-size: 14px; }
  .gallery { gap: 14px; }
  .card h3 { font-size: 16px; }
  .levels { gap: 6px; }
  .level { padding: 8px 8px 7px; }
  .lv-meta { font-size: 11px; }

  /* play */
  .toolbar { gap: 4px; padding: 4px 6px; padding-top: max(4px, env(safe-area-inset-top)); }
  .toolbar .title { flex: 1; }
  .toolbar .title strong { font-size: 15px; }
  .toolbar .title span, .pct-bar { display: none; }
  .pct { margin-left: 0; font-size: 15px; }
  .icon-btn { width: 40px; height: 40px; }
  .tools [data-act="zoomin"], .tools [data-act="zoomout"], .tools [data-act="fit"], .tools [data-act="sound"] { display: none; }
  .menu-sound { display: flex; }
  .zoom-fab { display: flex; }
  .palette { gap: 8px; padding: 10px 10px; padding-bottom: max(10px, env(safe-area-inset-bottom));
    -webkit-mask-image: linear-gradient(90deg, transparent 0, #000 14px, #000 calc(100% - 22px), transparent 100%);
            mask-image: linear-gradient(90deg, transparent 0, #000 14px, #000 calc(100% - 22px), transparent 100%); }
  .swatch { width: 48px; height: 48px; }
  .swatch .dot { font-size: 16px; }
  .modal { padding: 20px; border-radius: 20px; }
  .modal h2 { font-size: 21px; }
}
/* phones on their side and other short screens */
@media (max-height: 500px) {
  .tools [data-act="zoomin"], .tools [data-act="zoomout"], .tools [data-act="fit"] { display: none; }
  .toolbar { padding-top: 2px; padding-bottom: 2px; }
  .icon-btn { width: 36px; height: 36px; }
  .palette { padding-top: 6px; padding-bottom: max(6px, env(safe-area-inset-bottom)); }
  .swatch { width: 42px; height: 42px; }
  .zoom-fab { display: flex; flex-direction: row; bottom: 8px; right: 8px; }
  .fab { width: 40px; height: 40px; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
