:root {
  --bg-hi: #24504c;
  --bg: #173b39;
  --bg-lo: #0e2423;
  --ink: #2b2118;
  --cream: #fff7e6;
  --tile-top: #fffaf0;
  --tile: #f8ecd2;
  --edge: #d9c08e;
  --edge-lo: #a98a55;
  --accent: #f07a3c;
  --gold: #f3c35a;
  --shelf: #7b4a2b;
  --shelf-hi: #96603a;
  --shelf-lo: #4d2c17;
  --danger: #ff6a5c;
  --text: #fdf3dc;
  --muted: #b9d1cb;
  --font: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", "PingFang HK", "Microsoft JhengHei", sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg-lo);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

body {
  background:
    radial-gradient(circle at 1px 1px, rgba(255, 247, 230, 0.06) 1px, transparent 1.5px) 0 0 / 22px 22px,
    radial-gradient(120% 80% at 50% 0%, var(--bg-hi) 0%, var(--bg) 45%, var(--bg-lo) 100%);
  overflow: hidden;
}

button { font: inherit; color: inherit; touch-action: manipulation; }

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 8px) calc(env(safe-area-inset-right) + 16px) calc(env(safe-area-inset-bottom) + 12px) calc(env(safe-area-inset-left) + 16px);
}

.top {
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  gap: 8px;
  min-height: 56px;
}

.heading { text-align: center; line-height: 1.1; }
.heading h1 { margin: 0; font-size: 1.35rem; font-weight: 800; letter-spacing: 0.02em; color: var(--cream); }
.heading p { margin: 2px 0 0; font-size: 0.8rem; font-weight: 600; color: var(--muted); min-height: 1em; }

.round-btn {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 247, 230, 0.1);
  display: grid;
  place-items: center;
  cursor: pointer;
}
.round-btn svg, .act svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.round-btn .spk { fill: currentColor; }
.round-btn .mute { display: none; }
.round-btn[aria-pressed="false"] .wave { display: none; }
.round-btn[aria-pressed="false"] .mute { display: inline; }
.round-btn:active { transform: scale(0.94); }

.board {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  margin: 6px 0 10px;
  isolation: isolate;
  touch-action: manipulation;
}

.tile {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--size, 56px);
  height: var(--size, 56px);
  padding: 0;
  border: 1.5px solid rgba(43, 33, 24, 0.55);
  border-radius: calc(var(--size, 56px) * 0.22);
  background: linear-gradient(170deg, var(--tile-top) 0%, var(--tile) 70%);
  box-shadow:
    0 calc(var(--size, 56px) * 0.065) 0 var(--edge),
    0 calc(var(--size, 56px) * 0.065 + 1.5px) 0 var(--edge-lo),
    0 calc(var(--size, 56px) * 0.12) calc(var(--size, 56px) * 0.16) rgba(0, 0, 0, 0.38);
  cursor: pointer;
  transition: filter 0.22s ease, transform 0.22s cubic-bezier(.3,.7,.4,1);
  transform: var(--at, none);
  will-change: transform;
}
.tile svg { display: block; width: 80%; height: 80%; margin: 10%; pointer-events: none; }
.board .tile.locked { filter: brightness(0.52) saturate(0.55); cursor: default; }
.board .tile:not(.locked):active { filter: brightness(1.06); }

.tile.appear { animation: appear 0.28s cubic-bezier(.3,1.4,.5,1); }
.tile.nudge { animation: nudge 0.3s ease; }
.tile.flip { animation: flip 0.42s ease-in-out; }
.tile.clearing { animation: clearing 0.34s ease-in forwards; z-index: 3; }

.flyer {
  position: fixed;
  z-index: 60;
  pointer-events: none;
  transform-origin: 0 0;
  transition: none;
}

.shelf {
  position: relative;
  padding: 10px 10px 14px;
  border-radius: 20px;
  background: linear-gradient(180deg, var(--shelf-hi) 0%, var(--shelf) 55%, var(--shelf-lo) 100%);
  box-shadow: inset 0 2px 0 rgba(255, 220, 180, 0.3), 0 10px 22px rgba(0, 0, 0, 0.45);
  transition: box-shadow 0.3s ease;
}
.shelf.warn { animation: warn 1.1s ease-in-out infinite; }
.shelf.full { box-shadow: 0 0 0 3px var(--danger), 0 10px 22px rgba(0, 0, 0, 0.45); }

.tray {
  position: relative;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}
.slot {
  aspect-ratio: 1;
  border-radius: 22%;
  background: rgba(30, 14, 4, 0.42);
  box-shadow: inset 0 3px 6px rgba(0, 0, 0, 0.45), 0 1px 0 rgba(255, 220, 180, 0.18);
}
.tray-layer { position: absolute; inset: 0; pointer-events: none; isolation: isolate; }
.tray-layer .tile { cursor: default; transition: transform 0.2s cubic-bezier(.3,.7,.4,1); }
.tray-layer .tile.waiting { visibility: hidden; }

.actions {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-top: 12px;
}
.act {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 52px;
  padding: 0 8px;
  border: 0;
  border-radius: 16px;
  background: var(--cream);
  color: var(--ink);
  font-weight: 800;
  font-size: 0.95rem;
  box-shadow: 0 4px 0 var(--edge-lo), 0 8px 14px rgba(0, 0, 0, 0.3);
  cursor: pointer;
}
.act:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--edge-lo), 0 4px 8px rgba(0, 0, 0, 0.3); }
.act:disabled { opacity: 0.45; cursor: default; transform: none; }
.act .count {
  position: absolute;
  top: -7px;
  right: -4px;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 11px;
  background: var(--accent);
  color: var(--ink);
  font-size: 0.78rem;
  line-height: 22px;
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.25);
}
.label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.overlay {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(8, 22, 21, 0.62);
  -webkit-backdrop-filter: blur(5px);
  backdrop-filter: blur(5px);
  animation: fade 0.2s ease;
}
.overlay[hidden] { display: none; }
.card {
  width: min(100%, 380px);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  padding: 24px 20px 20px;
  border-radius: 26px;
  background: var(--cream);
  color: var(--ink);
  text-align: center;
  box-shadow: 0 6px 0 var(--edge), 0 24px 50px rgba(0, 0, 0, 0.5);
  animation: rise 0.32s cubic-bezier(.3,1.3,.5,1);
}
.card h2 { margin: 6px 0 6px; font-size: 1.6rem; font-weight: 900; }
.card p { margin: 0 0 16px; font-size: 1rem; line-height: 1.45; color: #5b4a3a; }
.card ol { margin: 0 0 18px; padding: 0 0 0 1.3em; text-align: left; line-height: 1.45; color: #4b3b2c; }
.card ol li { margin: 0 0 8px; }
.card .art { display: flex; justify-content: center; gap: 6px; margin: 0 0 6px; }
.card .art .tile { position: relative; --size: 52px; }
.card .stack { display: grid; gap: 10px; }
.card .row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 48px; padding: 0 4px; border-bottom: 1px solid rgba(43, 33, 24, 0.12); font-weight: 700; }
.card .row:last-of-type { border-bottom: 0; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.chip { min-height: 40px; padding: 0 12px; border: 2px solid rgba(43, 33, 24, 0.2); border-radius: 999px; background: transparent; font-weight: 700; cursor: pointer; }
.chip[aria-pressed="true"] { border-color: var(--ink); background: var(--ink); color: var(--cream); }
.btn { min-height: 52px; padding: 0 20px; border: 0; border-radius: 16px; font-weight: 800; font-size: 1.05rem; cursor: pointer; }
.btn.primary { background: var(--accent); color: var(--ink); box-shadow: 0 4px 0 #b9541f; }
.btn.secondary { background: transparent; color: var(--ink); border: 2px solid rgba(43, 33, 24, 0.25); }
.btn:active { transform: translateY(2px); }
.small { font-size: 0.85rem; color: #7a6652; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 150px);
  z-index: 90;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 999px;
  background: rgba(14, 36, 35, 0.92);
  color: var(--cream);
  font-weight: 700;
  font-size: 0.9rem;
  opacity: 0;
  transform: translate(-50%, 10px);
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.spark {
  position: fixed;
  z-index: 70;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  pointer-events: none;
}
.petal { position: fixed; z-index: 85; top: -40px; width: 30px; height: 30px; pointer-events: none; }

.noscript { padding: 24px; text-align: center; }

@keyframes appear { from { transform: var(--at) scale(0.6); opacity: 0; } to { transform: var(--at) scale(1); opacity: 1; } }
@keyframes nudge { 0%, 100% { transform: var(--at); } 25% { transform: var(--at) translateX(-4px) rotate(-3deg); } 75% { transform: var(--at) translateX(4px) rotate(3deg); } }
@keyframes flip { 0%, 100% { transform: var(--at) scaleX(1); } 50% { transform: var(--at) scaleX(0); } }
@keyframes clearing { 0% { transform: var(--at) scale(1); } 40% { transform: var(--at) translateY(-6px) scale(1.15); filter: brightness(1.25); } 100% { transform: var(--at) translateY(-14px) scale(0.2); opacity: 0; } }
@keyframes warn { 0%, 100% { box-shadow: inset 0 2px 0 rgba(255, 220, 180, 0.3), 0 10px 22px rgba(0, 0, 0, 0.45); } 50% { box-shadow: inset 0 2px 0 rgba(255, 220, 180, 0.3), 0 0 0 3px rgba(255, 106, 92, 0.75), 0 10px 22px rgba(0, 0, 0, 0.45); } }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(24px) scale(0.96); opacity: 0; } }

@media (min-height: 760px) {
  .act { min-height: 56px; }
}

@media (prefers-reduced-motion: reduce) {
  .tile, .tray-layer .tile { transition: none; }
  .tile.appear, .tile.nudge, .tile.flip, .shelf.warn, .overlay, .card { animation: none; }
}
