/* Layout idea: a dark room with a lit mosaic. The board is the only bright thing on screen;
   the room's glow takes its two colours from the puzzle being played. */
@property --g1 { syntax: '<color>'; inherits: true; initial-value: #ef4f5f; }
@property --g2 { syntax: '<color>'; inherits: true; initial-value: #2fb8a6; }

:root {
  color-scheme: dark;
  --bg: #14111f;
  --bg-2: #1c1830;
  --well: #262239;          /* an untouched square (tools/validate.mjs measures against this) */
  --well-x: #1a1729;
  --line: #14111f;
  --rule: #6c64a0;
  --ink: #f4f1ff;
  --ink-dim: #aaa3cb;
  --glass: rgba(255, 255, 255, .06);
  --glass-hi: rgba(255, 255, 255, .11);
  --edge: rgba(255, 255, 255, .13);
  --accent: #ffb547;
  --accent-ink: #2a1a00;
  --bad: #ff5d73;
  --display: 'Bricolage Grotesque', 'Avenir Next', 'Segoe UI', system-ui, sans-serif;
  --nums: 'Barlow Condensed', 'Arial Narrow', 'Helvetica Neue', sans-serif;
  --target: 44px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { background: var(--bg); -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font: 400 16px/1.45 var(--display);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  transition: --g1 .9s ease, --g2 .9s ease;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.num { font-family: var(--nums); font-variant-numeric: tabular-nums; letter-spacing: .02em; }

/* ---- the room's glow ---- */
.ambient { position: fixed; inset: 0; overflow: hidden; pointer-events: none; z-index: 0; }
.ambient i { position: absolute; width: 90vmax; height: 90vmax; border-radius: 50%; opacity: .34; }
.ambient i:first-child { left: -38vmax; top: -42vmax; background: radial-gradient(closest-side, var(--g1), transparent); animation: drift-a 38s ease-in-out infinite alternate; }
.ambient i:last-child { right: -40vmax; bottom: -46vmax; background: radial-gradient(closest-side, var(--g2), transparent); animation: drift-b 46s ease-in-out infinite alternate; }
@keyframes drift-a { to { transform: translate(14vmax, 10vmax); } }
@keyframes drift-b { to { transform: translate(-12vmax, -9vmax); } }

/* ---- buttons ---- */
.primary, .ghost {
  min-height: var(--target);
  padding: 0 1.25rem;
  border-radius: 12px;
  font-weight: 700;
  letter-spacing: .01em;
}
.primary { background: var(--accent); color: var(--accent-ink); border: 0; box-shadow: inset 0 1px 0 rgba(255, 255, 255, .5), 0 10px 26px -12px var(--accent); }
.primary:active { transform: translateY(1px); }
.ghost { background: var(--glass); border: 1px solid var(--edge); }
.ghost:hover { background: var(--glass-hi); }
.icon-btn { width: var(--target); height: var(--target); display: grid; place-items: center; background: var(--glass); border: 1px solid var(--edge); border-radius: 12px; }
.icon-btn svg { width: 22px; height: 22px; }
.eyebrow { margin: 0; font-size: .74rem; font-weight: 700; letter-spacing: .16em; text-transform: uppercase; color: var(--accent); }

/* ---- home ---- */
.home {
  position: relative;
  z-index: 1;
  max-width: 1040px;
  margin: 0 auto;
  padding-block: calc(env(safe-area-inset-top, 0px) + 1.5rem) calc(env(safe-area-inset-bottom, 0px) + 3rem);
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  display: grid;
  gap: 2rem;
}
.masthead { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.wordmark {
  margin: 0;
  display: flex;
  align-items: center;
  gap: .04em;
  font-size: clamp(2.3rem, 10.5vw, 4.6rem);
  font-weight: 800;
  line-height: 1;
  letter-spacing: -.035em;
  font-variation-settings: 'opsz' 96;
}
.wordmark .o { display: inline-block; width: .7em; height: .7em; margin: 0 .05em; border-radius: .09em; box-shadow: inset 0 .05em 0 rgba(255, 255, 255, .35), inset 0 -.05em 0 rgba(0, 0, 0, .25); }
.wordmark .o1 { background: #ef4f5f; }
.wordmark .o2 { background: #2fb8a6; }

.hero {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: clamp(1rem, 4vw, 2rem);
  padding: clamp(1rem, 3.5vw, 1.75rem);
  background: linear-gradient(135deg, rgba(255, 255, 255, .1), rgba(255, 255, 255, .03));
  border: 1px solid var(--edge);
  border-radius: 24px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .14), 0 30px 70px -40px #000;
}
.hero canvas { width: clamp(104px, 30vw, 190px); height: auto; aspect-ratio: 1; border-radius: 12px; box-shadow: 0 0 0 2px var(--rule), 0 18px 40px -18px #000; image-rendering: pixelated; }
.hero-text { display: grid; gap: .35rem; justify-items: start; min-width: 0; }
.hero-text h2 { margin: 0; font-size: clamp(1.6rem, 6vw, 2.6rem); font-weight: 800; line-height: 1.05; letter-spacing: -.02em; text-wrap: balance; }
.hero-text p { margin: 0 0 .6rem; color: var(--ink-dim); }

.shelf { display: grid; gap: .9rem; }
.shelf + .shelf { margin-top: 2rem; }
.shelf-head { display: flex; align-items: baseline; gap: .75rem; flex-wrap: wrap; }
.shelf-head h2 { margin: 0; font-size: 1.5rem; font-weight: 800; letter-spacing: -.01em; }
.shelf-head span { color: var(--ink-dim); font-size: .92rem; }
.shelf-head .count { margin-left: auto; font-family: var(--nums); font-size: 1.05rem; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(98px, 1fr)); gap: 12px; }
@media (min-width: 700px) { .cards { grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); gap: 16px; } }
.card {
  display: grid;
  gap: .5rem;
  padding: 10px;
  text-align: left;
  background: var(--glass);
  border: 1px solid var(--edge);
  border-radius: 16px;
  min-width: 0;
  transition: transform .18s ease, background .18s ease;
}
@media (hover: hover) { .card:hover { transform: translateY(-3px); background: var(--glass-hi); } }
.card canvas { width: 100%; height: auto; aspect-ratio: 1; border-radius: 8px; image-rendering: pixelated; display: block; }
.card-line { display: flex; align-items: center; justify-content: space-between; gap: .4rem; min-height: 1.3rem; }
.card-no { font-family: var(--nums); font-size: 1rem; color: var(--ink-dim); white-space: nowrap; }
.card.solved .card-no { color: var(--accent); }
.card-name { font-size: .86rem; font-weight: 700; overflow-wrap: anywhere; line-height: 1.2; }
.dots { display: flex; gap: 2px; min-width: 0; }
.dots i { width: 8px; height: 8px; border-radius: 2.5px; flex: none; }
.card-time { font-family: var(--nums); font-size: .95rem; color: var(--ink-dim); }

/* ---- play ---- */
.play {
  position: fixed;
  inset: 0;
  z-index: 2;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
  overscroll-behavior: none;
}
.bar { display: grid; grid-template-columns: var(--target) 1fr var(--target); align-items: center; gap: .5rem; padding: 10px 12px 4px; }
.bar-title { display: flex; align-items: baseline; justify-content: center; gap: .6rem; font-size: 1.5rem; font-weight: 700; }
.bar-size { font-family: var(--display); font-size: .86rem; font-weight: 500; color: var(--ink-dim); }
.clock { justify-self: end; font-size: 1.25rem; font-weight: 600; color: var(--ink-dim); white-space: nowrap; }

.stage { position: relative; display: grid; place-items: center; min-height: 0; min-width: 0; padding: 6px 8px; overflow: hidden; }
.board { --cell: 20px; --chip: 14px; display: grid; grid-template-columns: auto auto; grid-template-rows: auto auto; }
.colclues { grid-column: 2; grid-row: 1; display: flex; align-items: stretch; }
.rowclues { grid-column: 1; grid-row: 2; display: flex; flex-direction: column; }
.cc { width: var(--cell); display: flex; flex-direction: column; justify-content: flex-end; padding-bottom: 3px; border-radius: 5px 5px 0 0; }
.rc { height: var(--cell); display: flex; justify-content: flex-end; padding-right: 3px; border-radius: 5px 0 0 5px; }
.cc.hot, .rc.hot { background: rgba(255, 255, 255, .13); }
.chip {
  display: grid;
  place-items: center;
  background: var(--c);
  color: var(--chip-ink);
  border: 1px solid transparent;
  background-clip: padding-box;
  border-radius: 4px;
  font: 700 min(calc(var(--chip) * .96), calc(var(--cell) * .74)) / 1 var(--nums);
  transition: opacity .2s ease, filter .2s ease;
}
.cc .chip { height: var(--chip); }
.rc .chip { width: var(--chip); }
.chip.done { opacity: .3; filter: saturate(.5); }

.grid {
  grid-column: 2;
  grid-row: 2;
  position: relative;
  display: grid;
  background: var(--line);
  border-radius: 5px;
  box-shadow: 0 0 0 2px var(--rule), 0 26px 60px -22px #000;
  overflow: hidden;
  touch-action: none;
  cursor: crosshair;
}
.grid::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image: linear-gradient(to right, var(--rule) 0 2px, transparent 2px), linear-gradient(to bottom, var(--rule) 0 2px, transparent 2px);
  background-size: calc(var(--cell) * 5) calc(var(--cell) * 5);
  background-position: -1.5px -1.5px;
  transition: opacity .5s ease;
}
.cell {
  width: var(--cell);
  height: var(--cell);
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: var(--well) radial-gradient(circle, rgba(255, 255, 255, .16) 0 1.1px, transparent 1.7px);
}
.cell.f { background-image: none; box-shadow: inset 0 1.5px 0 rgba(255, 255, 255, .32), inset 1.5px 0 0 rgba(255, 255, 255, .12), inset 0 -1.5px 0 rgba(0, 0, 0, .24), inset -1.5px 0 0 rgba(0, 0, 0, .12); }
.cell.x {
  background: var(--well-x) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Cpath d='M2.6 2.6l4.8 4.8M7.4 2.6L2.6 7.4' stroke='%23857db3' stroke-width='1.3' stroke-linecap='round'/%3E%3C/svg%3E") center / 100% no-repeat;
}
.cell.pop { animation: pop .16s ease-out; }
@keyframes pop { from { transform: scale(.72); } }
.cell.wrong { animation: wrong .5s ease-in-out 3; z-index: 1; position: relative; }
@keyframes wrong { 50% { box-shadow: inset 0 0 0 3px var(--bad); filter: brightness(1.35); } }
.cell.proved { animation: proved 1.2s ease-out; position: relative; z-index: 1; }
@keyframes proved { 0% { box-shadow: 0 0 0 3px #fff, 0 0 22px 6px #fff; } 100% { box-shadow: 0 0 0 0 transparent; } }

/* the finished picture: grid lines melt, marks vanish */
.board.solved .grid { cursor: default; }
.board.solved .grid::after { opacity: 0; }
.board.solved .cell { border-color: transparent; }
.board.solved .cell:not(.f) { background: var(--bg-2); }
.board.solved .cell.f { box-shadow: none; }
.board.solved .chip { opacity: .3; }
.board.celebrate .cell { animation: reveal .62s cubic-bezier(.2, .9, .3, 1.2) both; animation-delay: calc((var(--x) + var(--y)) * 26ms); }
@keyframes reveal { 0% { transform: scale(.4) rotate(-8deg); filter: brightness(2.2); } 100% { transform: none; filter: none; } }

.toast {
  position: absolute;
  left: 50%;
  top: 8px;
  transform: translate(-50%, -8px);
  max-width: calc(100% - 24px);
  padding: .5rem .9rem;
  background: #2f2a4d;
  border: 1px solid var(--edge);
  border-radius: 999px;
  font-size: .9rem;
  font-weight: 600;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
  box-shadow: 0 14px 30px -14px #000;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.panel { padding: 8px 12px 14px; display: grid; justify-items: center; }
.controls { display: grid; gap: 12px; justify-items: center; width: 100%; }
.palette { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }
.swatch {
  position: relative;
  width: 50px;
  height: 50px;
  border: 0;
  border-radius: 13px;
  background: var(--c, var(--well));
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, .32), inset 0 -3px 0 rgba(0, 0, 0, .22);
  transition: transform .14s ease, box-shadow .14s ease;
}
.swatch.cross { background: var(--well); display: grid; place-items: center; box-shadow: inset 0 0 0 1px var(--edge); }
.swatch.cross svg { width: 24px; height: 24px; }
.swatch[aria-checked="true"] { transform: translateY(-5px) scale(1.06); box-shadow: inset 0 2px 0 rgba(255, 255, 255, .32), inset 0 -3px 0 rgba(0, 0, 0, .22), 0 0 0 3px var(--bg), 0 0 0 5.5px var(--ink), 0 14px 22px -8px rgba(0, 0, 0, .8); }
.swatch.cross[aria-checked="true"] { box-shadow: inset 0 0 0 1px var(--edge), 0 0 0 3px var(--bg), 0 0 0 5.5px var(--ink), 0 14px 22px -8px rgba(0, 0, 0, .8); }
.swatch kbd { display: none; }
@media (hover: hover) and (pointer: fine) {
  .swatch kbd { display: block; position: absolute; right: 5px; bottom: 3px; font: 700 .8rem/1 var(--nums); color: var(--chip-ink, var(--ink-dim)); opacity: .7; }
}
.tools { display: flex; gap: 8px; justify-content: center; }
.tool { display: flex; align-items: center; gap: .45rem; min-height: var(--target); padding: 0 .95rem; background: var(--glass); border: 1px solid var(--edge); border-radius: 12px; font-weight: 600; font-size: .95rem; }
.tool svg { width: 20px; height: 20px; flex: none; }
.tool:hover { background: var(--glass-hi); }
.tool:disabled { opacity: .4; cursor: default; }

.result { display: grid; gap: .3rem; justify-items: center; text-align: center; padding: 4px 0 6px; animation: rise .6s .5s both ease-out; }
.result h2 { margin: 0; font-size: clamp(1.9rem, 8vw, 2.8rem); font-weight: 800; letter-spacing: -.02em; line-height: 1.05; text-wrap: balance; }
.result-meta { margin: 0 0 .7rem; color: var(--ink-dim); font-size: 1.15rem; }
.result-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
@keyframes rise { from { opacity: 0; transform: translateY(14px); } }

/* wide screens: board on the left, controls in a column on the right */
@media (min-width: 880px) and (min-aspect-ratio: 5/4) {
  .play { grid-template-columns: minmax(0, 1fr) 300px; grid-template-rows: auto minmax(0, 1fr); }
  .bar { grid-column: 1 / -1; padding: 14px 20px 4px; }
  .panel { align-content: center; padding: 16px 28px 16px 8px; }
  .controls { gap: 26px; }
  .palette { gap: 14px; }
  .swatch { width: 58px; height: 58px; }
  .tools { flex-direction: column; width: 100%; }
  .tool { justify-content: center; }
}

/* ---- dialogs ---- */
dialog { width: min(92vw, 30rem); padding: 1.5rem; color: var(--ink); background: #221e38; border: 1px solid var(--edge); border-radius: 22px; box-shadow: 0 40px 90px -30px #000; }
dialog:modal { overscroll-behavior: contain; }
dialog::backdrop { background: rgba(10, 8, 18, .72); overflow: hidden; overscroll-behavior: contain; }
dialog h1 { margin: 0 0 .9rem; font-size: 1.55rem; font-weight: 800; letter-spacing: -.01em; }
dialog p { margin: 0 0 1.2rem; color: var(--ink-dim); text-wrap: pretty; }
.how { margin: 0 0 1rem; padding-left: 1.2rem; display: grid; gap: .65rem; color: var(--ink-dim); }
.how b { color: var(--ink); font-weight: 700; }
.how-keys { font-size: .9rem; }
kbd { font: 700 .85em var(--nums); padding: .05em .4em; border: 1px solid var(--edge); border-radius: 5px; background: var(--glass); }
.dlg-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }

@media (prefers-reduced-motion: reduce) {
  .ambient i, .cell.pop, .board.celebrate .cell, .result { animation: none; }
  body, .card, .swatch { transition: none; }
}
