:root {
  --bg: #dfe8dc;
  --bg2: #cfdccb;
  --ink: #243028;
  --muted: #5c6b60;
  --paper: #fbf6ea;
  --tile: #fff8ea;
  --tile-ink: #1e2a22;
  --tile-edge: #e4d6b8;
  --empty: #b7c6b3;
  --empty-in: #9aab97;
  --accent: #2f6f5e;
  --accent-2: #e09a3e;
  --good: #3d8b62;
  --card: rgba(251, 246, 234, 0.92);
  --shadow: 0 8px 20px rgba(36, 48, 40, 0.12);
  --radius: 16px;
  --cell: 28px;
  --pad: 8px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background:
    radial-gradient(1200px 600px at 50% -10%, #eef4e8 0%, transparent 55%),
    linear-gradient(180deg, var(--bg) 0%, var(--bg2) 100%);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  font-size: 17px;
}

body {
  max-width: 480px;
  margin: 0 auto;
  padding: 0;
}

.page {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  height: 100svh;
  width: 100%;
  padding: 6px 10px calc(8px + env(safe-area-inset-bottom, 0px));
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex: 0 0 auto;
}

.back, .icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  padding: 0 12px;
  border: 0;
  border-radius: 14px;
  background: var(--card);
  color: var(--ink);
  text-decoration: none;
  font-size: 15px;
  font-weight: 650;
  box-shadow: var(--shadow);
  cursor: pointer;
}

.icon-btn { min-width: 40px; padding: 0 10px; }

h1 {
  margin: 0;
  font-size: 18px;
  letter-spacing: 0.02em;
  text-align: center;
  flex: 1;
}

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
  margin: 6px 0 4px;
  flex: 0 0 auto;
}

.stat {
  background: var(--card);
  border-radius: 12px;
  padding: 5px 4px 6px;
  text-align: center;
  box-shadow: var(--shadow);
}

.stat b {
  display: block;
  font-size: 18px;
  line-height: 1.1;
  color: var(--accent);
}

.stat span {
  display: block;
  margin-top: 1px;
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
}

.hint {
  display: none;
}

.stage {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board-wrap {
  position: relative;
  background: #6f8a72;
  border-radius: 18px;
  padding: var(--pad);
  margin: 0 auto;
  box-shadow: var(--shadow), inset 0 1px 0 rgba(255,255,255,.18);
  touch-action: manipulation;
  user-select: none;
  overflow: hidden;
}

.board {
  display: grid;
  gap: 2px;
}

.cell {
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  margin: 0;
  padding: 0;
  border: 0;
  appearance: none;
  -webkit-appearance: none;
  border-radius: 7px;
  font-weight: 800;
  font-size: calc(var(--cell) * 0.52);
  line-height: 1;
  color: var(--tile-ink);
  background: linear-gradient(180deg, #fffdf6 0%, var(--tile) 100%);
  box-shadow: 0 1px 0 var(--tile-edge), 0 2px 0 #cbb992;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  text-transform: uppercase;
  overflow: hidden;
}

.cell.empty {
  background: linear-gradient(180deg, var(--empty-in), var(--empty));
  box-shadow: inset 0 2px 5px rgba(36,48,40,.28);
  color: transparent;
  cursor: default;
}

.cell.selected {
  background: linear-gradient(180deg, #ffe7b0, #f3c56a);
  box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--accent-2);
  transform: scale(1.04);
  z-index: 2;
}

.cell.hot {
  background: linear-gradient(180deg, #fff3c4, #f0d36a);
  animation: pulse .7s ease infinite alternate;
}

.cell.gone {
  animation: merge .55s ease forwards;
}

@keyframes pulse {
  from { transform: scale(1); }
  to { transform: scale(1.05); }
}

@keyframes merge {
  0% { transform: scale(1); opacity: 1; }
  55% { transform: scale(1.18); background: #ffe28a; }
  100% { transform: scale(0.2); opacity: 0; }
}

.toast {
  position: absolute;
  left: 50%;
  top: 42%;
  transform: translate(-50%, -50%) scale(.9);
  background: rgba(36, 48, 40, .92);
  color: #fff8ea;
  padding: 12px 16px;
  border-radius: 16px;
  font-weight: 800;
  font-size: 22px;
  opacity: 0;
  pointer-events: none;
  transition: .25s ease;
  white-space: nowrap;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
  flex: 0 0 auto;
}

.btn {
  min-height: 46px;
  border: 0;
  border-radius: 16px;
  font-size: 16px;
  font-weight: 750;
  cursor: pointer;
  box-shadow: var(--shadow);
}

.btn.primary {
  background: var(--accent);
  color: #f7fff8;
}

.btn.ghost {
  background: var(--card);
  color: var(--ink);
}

.words-left {
  margin: 6px 0 0;
  font-size: 13px;
  color: var(--muted);
  text-align: center;
  flex: 0 0 auto;
  min-height: 18px;
}

.bank {
  margin-top: 6px;
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  justify-content: center;
  align-content: flex-start;
  height: 160px;
  min-height: 160px;
  max-height: 160px;
  overflow: auto;
  flex: 0 0 auto;
}

.chip {
  background: var(--card);
  color: var(--ink);
  border-radius: 999px;
  padding: 4px 9px;
  font-size: 13px;
  font-weight: 750;
  letter-spacing: 0.03em;
  box-shadow: 0 2px 8px rgba(36,48,40,.08);
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(28, 36, 30, .45);
  display: none;
  align-items: flex-end;
  justify-content: center;
  z-index: 20;
  padding: 12px;
}

.overlay.show { display: flex; }

.sheet {
  width: min(480px, 100%);
  background: var(--paper);
  border-radius: 22px 22px 18px 18px;
  padding: 18px 16px 20px;
  box-shadow: var(--shadow);
  max-height: min(86dvh, 720px);
  overflow: auto;
}

.sheet h2 { margin: 0 0 10px; font-size: 22px; }
.sheet p { margin: 0 0 12px; color: var(--muted); font-size: 16px; line-height: 1.4; }
.sheet label {
  display: block;
  margin: 12px 0 6px;
  font-weight: 700;
  font-size: 15px;
}

.sheet select {
  width: 100%;
  min-height: 48px;
  border-radius: 14px;
  border: 1px solid #d7cdb6;
  background: #fff;
  font-size: 17px;
  padding: 0 12px;
  color: var(--ink);
}

.help ol { margin: 0 0 14px 18px; padding: 0; color: var(--ink); }
.help li { margin: 0 0 8px; line-height: 1.35; }

.win { text-align: center; }
.win .big { font-size: 40px; margin-bottom: 6px; }
