:root {
  color-scheme: dark;
  --page: #111522;
  --panel: #1b2132;
  --panel-2: #242b3f;
  --line: #3b4560;
  --text: #f5f7ff;
  --muted: #b7bfd5;
  --accent: #8ba8ff;
  --danger: #ff879d;
  --board-size: min(88vw, 52vh, 510px);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 280px;
  min-height: 100vh;
  background: linear-gradient(155deg, #171b2b, var(--page) 60%);
  color: var(--text);
  overflow-x: clip;
  transition: background 600ms ease;
}

.moon-scene { position: fixed; inset: 0; z-index: -1; overflow: hidden; pointer-events: none; }
.moon { position: absolute; top: 8%; right: 9%; width: clamp(90px, 18vw, 180px); aspect-ratio: 1; border-radius: 50%; background: radial-gradient(circle at 35% 35%, #fffde6, #a9bfff 68%, transparent 70%); opacity: .25; filter: drop-shadow(0 0 35px rgb(164 191 255 / .35)); transition: opacity 700ms, transform 700ms; }
.stars { position: absolute; inset: 0; opacity: .25; background-image: radial-gradient(circle, #e9eeff 0 1px, transparent 1.5px); background-size: 67px 71px; transition: opacity 700ms; }
.stage-rising .moon { opacity: .42; transform: translateY(-3vh); }
.stage-rising .stars { opacity: .4; }
.stage-full .moon { opacity: .68; transform: translateY(-6vh) scale(1.08); }
.stage-full .stars { opacity: .62; }
.is-game-over .game-shell { filter: brightness(.72); }

button { font: inherit; }

.game-shell {
  width: min(100% - 24px, 760px);
  margin: 0 auto;
  padding: 18px 0 28px;
}

.game-header, .section-heading, .dialog-actions, .settings-row, .header-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.header-actions { justify-content: flex-end; }

h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(1.55rem, 5vw, 2.2rem); letter-spacing: .02em; }
h2 { margin-bottom: 8px; font-size: 1.05rem; }
.eyebrow { margin-bottom: 3px; color: var(--accent); font-size: .72rem; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; }
#build-label { margin-left: .45em; color: #c7d4ff; letter-spacing: .04em; }
.subtitle { margin: 2px 0 0; color: var(--muted); font-size: .86rem; }

.score-panel {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin: 14px 0 9px;
}

.score-panel div {
  padding: 9px 6px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  text-align: center;
}

.score-panel span { display: block; color: var(--muted); font-size: .7rem; }
.score-panel strong { display: block; margin-top: 2px; font-size: 1.25rem; font-variant-numeric: tabular-nums; }

.message {
  min-height: 1.35em;
  margin: 0 0 8px;
  color: var(--muted);
  font-size: .83rem;
  text-align: center;
}
.message.is-error { color: #ffadba; }
.message.is-success { color: #98e3bc; }
.message.is-resonance { color: #fff4a8; text-shadow: 0 0 12px rgb(255 230 125 / .55); }

.board-wrap { display: grid; place-items: center; }
.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, minmax(0, 1fr));
  width: var(--board-size);
  height: var(--board-size);
  aspect-ratio: 1;
  gap: 3px;
  padding: 5px;
  border: 1px solid #4d5876;
  border-radius: 12px;
  background: #0e1220;
  box-shadow: 0 14px 38px rgb(0 0 0 / .26);
  touch-action: none;
  contain: layout paint;
}

.cell {
  min-width: 0;
  padding: 0;
  border: 1px solid #303a54;
  border-radius: 5px;
  background: #1e2639;
  cursor: pointer;
  transition: transform 90ms ease, background 90ms ease, box-shadow 90ms ease;
}
.cell.occupied { border-color: rgb(255 255 255 / .18); box-shadow: inset 0 1px 0 rgb(255 255 255 / .25); }
.cell.preview-valid { box-shadow: inset 0 0 0 3px #d8e4ff, inset 0 0 10px rgb(139 168 255 / .38); }
.cell.preview-invalid {
  background-image: repeating-linear-gradient(135deg, rgb(255 73 108 / .32) 0 5px, transparent 5px 10px);
  box-shadow: inset 0 0 0 3px #ff6684, inset 0 0 12px rgb(255 64 99 / .35);
}
.cell.drag-visual { border-color: rgb(255 255 255 / .28); }
.cell.drag-visual.color-blue { background: rgb(94 141 244 / .62); }
.cell.drag-visual.color-yellow { background: rgb(231 189 82 / .62); }
.cell.drag-visual.color-purple { background: rgb(155 114 223 / .62); }
.cell.drag-visual.color-pink { background: rgb(233 120 155 / .62); }
.cell.drag-visual.color-cyan { background: rgb(82 195 196 / .62); }
.cell.cleared { animation: clear-flash 260ms ease; }
.cell.placed { animation: place-pop 180ms ease-out; }

.color-blue { background: #5e8df4; }
.color-yellow { background: #e7bd52; }
.color-purple { background: #9b72df; }
.color-pink { background: #e9789b; }
.color-cyan { background: #52c3c4; }

@keyframes clear-flash { 50% { transform: scale(.82); filter: brightness(1.7); } }
@keyframes place-pop { 45% { transform: scale(.84); filter: brightness(1.35); } }
@keyframes dialog-rise { from { opacity: 0; transform: translateY(12px) scale(.98); } }

.piece-section { position: relative; margin-top: var(--piece-gap, 12px); }
.section-heading h2 { margin: 0; }
.section-heading span { color: var(--muted); font-size: .72rem; }
.pieces { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-top: 7px; }

.piece-card {
  display: grid;
  place-items: center;
  min-height: 82px;
  padding: 7px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--panel);
  color: var(--text);
  cursor: grab;
  touch-action: none;
  user-select: none;
}
.piece-card:hover { border-color: #7182ad; }
.piece-card.selected { border-color: var(--accent); box-shadow: 0 0 0 2px rgb(139 168 255 / .28); }
.piece-card.used {
  border-style: dashed;
  opacity: .46;
  cursor: default;
  pointer-events: none;
}
.piece-card:active { cursor: grabbing; }

.mini-piece {
  display: grid;
  gap: 3px;
  width: fit-content;
}
.mini-cell {
  width: clamp(15px, 5vw, 24px);
  aspect-ratio: 1;
  border-radius: 4px;
}
.mini-cell.empty { background: transparent; }
.piece-label { margin-top: 5px; color: var(--muted); font-size: .66rem; }

.rules {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--panel);
  color: var(--muted);
  font-size: .78rem;
  line-height: 1.55;
}
.rules h2 { margin-bottom: 3px; color: var(--text); }
.rules p { margin-bottom: 0; }

.primary-button, .secondary-button, .toggle-button, .icon-button {
  min-height: 42px;
  padding: 8px 15px;
  border-radius: 9px;
  cursor: pointer;
}
.primary-button { border: 0; background: var(--accent); color: #101525; font-weight: 800; }
.secondary-button { border: 1px solid var(--line); background: var(--panel-2); color: var(--text); }
.toggle-button, .icon-button { border: 1px solid var(--line); background: #20283b; color: var(--text); cursor: pointer; }
.icon-button { min-width: 40px; padding: 6px; border-radius: 50%; }
.settings-row { margin: 14px 0; }
.settings-row > * { flex: 1; }
.text-button { padding: 8px; border: 0; background: transparent; color: var(--accent); text-decoration: underline; cursor: pointer; }

.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgb(7 9 16 / .78);
}
.overlay[hidden], .drag-ghost[hidden] { display: none; }
.dialog {
  width: min(100%, 420px);
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 15px;
  background: #1b2132;
  box-shadow: 0 22px 65px rgb(0 0 0 / .45);
  text-align: center;
  animation: dialog-rise 180ms ease-out;
}
.home-dialog { width: min(100%, 440px); padding: 34px 24px; background: rgb(27 33 50 / .9); backdrop-filter: blur(12px); }
.home-dialog h1 { font-size: clamp(2rem, 10vw, 3.4rem); }
.home-overlay { background: radial-gradient(circle at 70% 18%, rgb(99 121 188 / .25), rgb(7 9 16 / .86) 55%); }
.tutorial-visual { display: grid; place-items: center; width: 86px; aspect-ratio: 1; margin: 12px auto; border: 1px solid var(--accent); border-radius: 50%; color: var(--accent); font-weight: 800; box-shadow: 0 0 24px rgb(139 168 255 / .2); }
.help-list { padding-left: 1.3rem; color: var(--muted); text-align: left; line-height: 1.55; }
.result-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 7px; margin: 14px 0; }
.result-grid div { padding: 8px; border-radius: 9px; background: var(--panel-2); color: var(--muted); font-size: .75rem; }
.result-grid strong { display: block; margin-top: 3px; color: var(--text); font-size: 1.15rem; }
.dialog p { color: var(--muted); }
.dialog-actions > * { flex: 1; }

.drag-ghost {
  position: fixed;
  z-index: 30;
  pointer-events: none;
  opacity: .92;
  filter: drop-shadow(0 8px 12px rgb(0 0 0 / .35));
  will-change: transform;
}
.drag-ghost .mini-piece { gap: var(--ghost-gap, 3px); }
.drag-ghost .mini-cell {
  width: var(--ghost-cell-width, 38px);
  height: var(--ghost-cell-height, 38px);
  aspect-ratio: auto;
}

@media (max-width: 370px) {
  :root { --board-size: min(calc(100vw - 24px), calc(100dvh - 285px), 320px); }
  .game-shell { width: min(100% - 12px, 760px); padding-top: 9px; }
  .game-header { align-items: flex-start; }
  .secondary-button { min-height: 38px; padding: 6px 10px; }
  .score-panel { margin-top: 8px; }
  .piece-card { min-height: 70px; padding: 4px; }
  .rules { margin-top: 8px; padding: 7px 9px; }
}

@media (max-width: 480px), (max-height: 700px) {
  :root { --board-size: min(calc(100vw - 24px), calc(100dvh - 285px), 320px); }
  html, body { max-width: 100%; }
  .game-shell {
    width: min(100% - 12px, 760px);
    min-height: 100dvh;
    padding: 6px 0;
  }
  .game-header { align-items: center; }
  .game-header h1 { font-size: clamp(1.25rem, 6vw, 1.55rem); }
  .eyebrow { margin-bottom: 1px; font-size: .62rem; }
  .subtitle { display: none; }
  .secondary-button { min-height: 34px; padding: 4px 9px; }
  .score-panel { gap: 5px; margin: 5px 0 4px; }
  .score-panel div { padding: 4px 3px; border-radius: 8px; }
  .score-panel span { font-size: .62rem; }
  .score-panel strong { margin-top: 0; font-size: 1rem; }
  .message { min-height: 1em; margin-bottom: 4px; font-size: .7rem; }
  .piece-section { margin-top: var(--piece-gap, 6px); }
  .piece-section::before { content: "初月 · 月光旅程"; position: absolute; left: 50%; top: calc(var(--piece-gap, 6px) / -2); transform: translate(-50%, -50%); color: rgb(183 191 213 / .7); font-size: .68rem; letter-spacing: .12em; white-space: nowrap; }
  .stage-rising .piece-section::before { content: "月升 · 连消点亮花园"; color: #c8d6ff; }
  .stage-full .piece-section::before { content: "满月 · 花园正在共鸣"; color: #fff1ad; text-shadow: 0 0 10px rgb(255 232 143 / .4); }
  .section-heading h2 { margin: 0; font-size: .86rem; }
  .section-heading span { font-size: .62rem; }
  .pieces { gap: 5px; margin-top: 3px; }
  .piece-card { min-width: 0; min-height: 64px; padding: 3px; }
  .piece-label { display: none; }
  .piece-card.used .piece-label { display: block; margin: 0; }
  .rules { display: none; }
  .header-actions { gap: 5px; }
  .icon-button { min-width: 34px; min-height: 34px; padding: 3px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

@media (min-width: 900px) and (max-height: 820px) {
  :root { --board-size: min(48vw, 58vh, 470px); }
  .game-shell { display: grid; grid-template-columns: minmax(410px, 1fr) 250px; column-gap: 24px; width: min(100% - 36px, 900px); }
  .game-header, .score-panel, .message, .board-wrap { grid-column: 1; }
  .piece-section { grid-column: 2; grid-row: 1 / span 3; margin-top: 0; }
  .pieces { grid-template-columns: 1fr; }
  .rules { grid-column: 2; grid-row: 4 / span 2; align-self: end; }
}
