:root {
  --bg: #f3f4f7;
  --card: #ffffff;
  --text: #191c24;
  --muted: #5f6573;
  --line: #dde0e7;
  --accent: #3b5bdb;
  --accent-soft: #e6ebfb;
  --on-accent: #ffffff;
  --radius: 14px;
  --box-l: 62%;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111318; --card: #1a1d24; --text: #e9ebf0; --muted: #9aa0ad; --line: #2c303a;
    --accent: #7c96ff; --accent-soft: #1e2540; --on-accent: #0b0d14;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111318; --card: #1a1d24; --text: #e9ebf0; --muted: #9aa0ad; --line: #2c303a;
  --accent: #7c96ff; --accent-soft: #1e2540; --on-accent: #0b0d14;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.wrap { max-width: 560px; margin: 0 auto; padding: 40px 16px 32px; }

header { margin-bottom: 20px; }
h1 { margin: 0; font-size: 2rem; letter-spacing: -0.02em; line-height: 1.1; }
h1 span { color: var(--accent); }
.tagline { margin: 8px 0 0; color: var(--muted); }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 14px;
  box-shadow: 0 1px 2px rgb(0 0 0 / 4%), 0 8px 24px rgb(0 0 0 / 5%);
}
.label { font-size: 0.75rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }

/* stats */
.bar { display: grid; grid-template-columns: 1.4fr 1fr 1fr 1fr; gap: 8px; margin-bottom: 16px; }
.stat { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.stat strong { font-size: 1.35rem; font-variant-numeric: tabular-nums; line-height: 1.3; }
.level-nav { display: flex; align-items: center; gap: 6px; }
.icon-btn {
  font: inherit; font-size: 1.3rem; line-height: 1; width: 28px; height: 28px;
  display: grid; place-items: center; padding: 0 0 3px;
  color: var(--text); background: transparent; border: 1px solid var(--line); border-radius: 8px; cursor: pointer;
}
.icon-btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.icon-btn:disabled { opacity: .35; cursor: not-allowed; }

/* board */
.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  aspect-ratio: 1;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--line);
  container-type: inline-size;
  user-select: none;
  -webkit-user-select: none;
}
.sq { position: relative; background: var(--card); }
.sq.dark { background: color-mix(in srgb, var(--text) 7%, var(--card)); }
.sq .coord {
  position: absolute; font-size: 2.2cqi; font-weight: 600; color: var(--muted); opacity: .7; pointer-events: none;
}
.sq .coord.file { right: 0.6cqi; bottom: 0.2cqi; }
.sq .coord.rank { left: 0.6cqi; top: 0.2cqi; }
.sq.target { cursor: pointer; }
.sq.target::after {
  content: ""; position: absolute; inset: 36%;
  border-radius: 50%; background: var(--accent); opacity: .55;
}
.sq.target:hover::after { opacity: .9; }

.box {
  --h: 220;
  position: absolute; left: 0; top: 0;
  width: 12.5%; height: 12.5%;
  padding: 0.9cqi;
  transition: transform 170ms ease-out;
  touch-action: none;
  cursor: grab;
  z-index: 1;
  outline: none;
}
.box span {
  display: grid; place-items: center; width: 100%; height: 100%;
  border-radius: 1.6cqi;
  font-weight: 700; font-size: 5cqi; font-variant-numeric: tabular-nums; letter-spacing: -0.02em;
  background: hsl(var(--h) 75% var(--box-l));
  color: #14161b;
  box-shadow: inset 0 -0.6cqi 0 rgb(0 0 0 / 18%), 0 0.4cqi 1cqi rgb(0 0 0 / 18%);
  transition: box-shadow 120ms, background 200ms;
}
.box.soft span {
  background: hsl(var(--h) 75% var(--box-l) / 20%);
  color: var(--text);
  box-shadow: inset 0 0 0 0.6cqi hsl(var(--h) 75% var(--box-l));
}
.box.long span { font-size: 3.9cqi; }
.box.xlong span { font-size: 3.1cqi; }
.box.selected { z-index: 3; }
.box.selected span, .box:focus-visible span {
  box-shadow: 0 0 0 0.7cqi var(--card), 0 0 0 1.3cqi var(--accent);
}
.box.moving { z-index: 4; }
.box.merge-target { cursor: pointer; }
.box.merge-target span { outline: 0.6cqi dashed var(--accent); outline-offset: 0.3cqi; }
.box.pop span { animation: pop 260ms ease-out; }
.box.trim span { animation: trim 300ms ease-out; }
.box.nudge span { animation: nudge 220ms ease-in-out; }
@keyframes pop { 40% { transform: scale(1.18); } }
@keyframes trim {
  0% { transform: scale(0.6); filter: brightness(1.5); }
  60% { transform: scale(1.12); }
}
@keyframes nudge { 25% { transform: translateX(-6%); } 75% { transform: translateX(6%); } }

.status { margin: 14px 0 0; min-height: 1.5em; text-align: center; color: var(--muted); font-variant-numeric: tabular-nums; }
.status.win { color: var(--accent); font-weight: 600; }

.row { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.actions { margin-top: 14px; }
.btn {
  font: inherit; font-weight: 600;
  border-radius: 10px; padding: 10px 16px;
  border: 1px solid transparent; cursor: pointer; white-space: nowrap;
}
.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--line); }
.btn.ghost:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn:focus-visible, .icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.rules h2 { margin: 0 0 8px; font-size: 1.05rem; }
.rules ul { margin: 0; padding-left: 20px; color: var(--muted); }
.rules li + li { margin-top: 4px; }
.rules strong { color: var(--text); }

@media (prefers-reduced-motion: reduce) {
  .box { transition: none; }
  .box.pop span, .box.nudge span, .box.trim span { animation: none; }
}

/* embedded in another page */
.embedded body { background: transparent; }
.embedded .wrap { max-width: none; padding: 0; }
.embedded header { display: none; }
.embedded .rules { margin-bottom: 4px; }
