:root {
  --bg: #18120e;
  --bg-grad-1: #1d1612;
  --bg-grad-2: #14100c;
  --card: #221a14;
  --card-edge: #2e241c;
  --cell: #2a1f17;
  --text: #f6ead8;
  --text-dim: #a89280;
  --accent: #d4a373;
  --accent-strong: #f0b87a;
  --slide: 140ms;
  --ease-out: cubic-bezier(0.22, 0.61, 0.36, 1);
  --ease-pop: cubic-bezier(0.34, 1.56, 0.64, 1);
  --radius-tile: 14px;
  --radius-card: 24px;
  --gap: 6px;
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  font-family: ui-rounded, "SF Pro Rounded", "Hiragino Maru Gothic ProN", "Inter", system-ui, -apple-system, sans-serif;
  color: var(--text);
  background:
    radial-gradient(ellipse at top, var(--bg-grad-1), var(--bg-grad-2) 60%),
    var(--bg);
  display: grid;
  place-items: center;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  overflow: hidden;
  font-synthesis: none;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.skip-link {
  position: absolute;
  top: 8px;
  left: 8px;
  padding: 8px 12px;
  background: var(--accent);
  color: #1a1410;
  border-radius: 8px;
  text-decoration: none;
  font-weight: 600;
  transform: translateY(-150%);
  transition: transform 200ms var(--ease-out);
  z-index: 10;
}
.skip-link:focus { transform: translateY(0); }

.game {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 16px;
  width: min(440px, 100vw);
  max-height: 100dvh;
}

/* ===== HUD ===== */
.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 6px;
}
.brand-mark {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.7rem;
  font-style: italic;
  color: var(--accent-strong);
  line-height: 1;
}
.brand-title {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 0;
  color: var(--text);
}

.scores {
  display: flex;
  gap: 8px;
  align-items: stretch;
}

.score-card {
  background: var(--card);
  border: 1px solid var(--card-edge);
  border-radius: 12px;
  padding: 6px 12px;
  min-width: 64px;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.1;
}
.score-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  font-weight: 600;
}
.score-value {
  font-size: 1.2rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  transition: color 200ms var(--ease-out);
}
#score.bump { color: var(--accent-strong); }

.new-game {
  background: var(--card);
  border: 1px solid var(--card-edge);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  font-size: 0.85rem;
  padding: 6px 14px;
  border-radius: 12px;
  cursor: pointer;
  transition: background 160ms var(--ease-out), border-color 160ms var(--ease-out), transform 100ms;
}
.new-game:hover { background: var(--cell); border-color: var(--accent); }
.new-game:active { transform: scale(0.96); }
.new-game:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ===== Board ===== */
.board-wrap {
  width: 100%;
}

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 5;
  max-height: calc(100dvh - 200px);
  margin: 0 auto;
  background: var(--card);
  border: 1px solid var(--card-edge);
  border-radius: var(--radius-card);
  padding: var(--gap);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(5, 1fr);
  gap: var(--gap);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.03),
    0 12px 30px rgba(0, 0, 0, 0.4);
  container-type: size;
}
.board:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; }

/* Constrain board width when viewport is short so it fits vertically */
@media (max-aspect-ratio: 3/5) {
  .board { width: 100%; }
}
@media (min-aspect-ratio: 3/5) {
  .board {
    width: auto;
    aspect-ratio: 3/5;
    height: calc(100dvh - 200px);
    max-height: 720px;
  }
}

.cell {
  background: var(--cell);
  border-radius: var(--radius-tile);
}

.tile {
  position: absolute;
  /* Tile occupies one cell's worth of board interior */
  width: calc((100% - var(--gap) * 2 - var(--gap) * 2) / 3);
  height: calc((100% - var(--gap) * 2 - var(--gap) * 4) / 5);
  /* Top-left of the first cell */
  top: var(--gap);
  left: var(--gap);
  transform: translate(
    calc(var(--c, 0) * (100% + var(--gap))),
    calc(var(--r, 0) * (100% + var(--gap)))
  );
  transition: transform var(--slide) var(--ease-out), opacity var(--slide) var(--ease-out);
  pointer-events: none;
  will-change: transform;
}

.tile-inner {
  width: 100%;
  height: 100%;
  border-radius: var(--radius-tile);
  display: grid;
  place-items: center;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  background: var(--tile-bg, var(--cell));
  color: var(--tile-fg, #fff6e6);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.18),
    inset 0 -1px 0 rgba(0, 0, 0, 0.18),
    0 2px 4px rgba(0, 0, 0, 0.25);
  transform: scale(1);
  transition: transform 220ms var(--ease-pop), background 220ms var(--ease-out), color 220ms var(--ease-out);
  font-size: clamp(1.2rem, 9cqh, 2rem);
}

.tile[data-digits="3"] .tile-inner { font-size: clamp(0.95rem, 7cqh, 1.6rem); }
.tile[data-digits="4"] .tile-inner { font-size: clamp(0.8rem, 5.6cqh, 1.3rem); }

/* Entry animation: start scaled down, scale up to 1 */
.tile.appear .tile-inner { transform: scale(0); }

/* Merge target pulse */
.tile.pop .tile-inner { animation: pop 280ms var(--ease-pop); }
@keyframes pop {
  0% { transform: scale(1); }
  45% { transform: scale(1.14); }
  100% { transform: scale(1); }
}

/* Absorbed tile fades out while sliding */
.tile.absorbing { opacity: 0; }

/* ===== Tile color ramp (Fibonacci) — warm sand → coral → wine → indigo → teal ===== */
.tile[data-value="1"]    { --tile-bg: oklch(0.72 0.06 75);   --tile-fg: #1a1410; }
.tile[data-value="2"]    { --tile-bg: oklch(0.70 0.10 60);   --tile-fg: #1a1410; }
.tile[data-value="3"]    { --tile-bg: oklch(0.66 0.14 45);   --tile-fg: #1a1410; }
.tile[data-value="5"]    { --tile-bg: oklch(0.63 0.17 35); }
.tile[data-value="8"]    { --tile-bg: oklch(0.60 0.19 25); }
.tile[data-value="13"]   { --tile-bg: oklch(0.56 0.21 15); }
.tile[data-value="21"]   { --tile-bg: oklch(0.52 0.21 5); }
.tile[data-value="34"]   { --tile-bg: oklch(0.49 0.21 355); }
.tile[data-value="55"]   { --tile-bg: oklch(0.47 0.19 335); }
.tile[data-value="89"]   { --tile-bg: oklch(0.44 0.17 315); }
.tile[data-value="144"]  { --tile-bg: oklch(0.42 0.16 290); }
.tile[data-value="233"]  { --tile-bg: oklch(0.42 0.14 260); }
.tile[data-value="377"]  { --tile-bg: oklch(0.43 0.13 230); }
.tile[data-value="610"]  { --tile-bg: oklch(0.45 0.13 200); }
.tile[data-value="987"]  { --tile-bg: oklch(0.48 0.13 170); }
.tile[data-value="1597"] { --tile-bg: oklch(0.52 0.13 145); }
.tile[data-value="2584"] { --tile-bg: oklch(0.56 0.13 130); }

/* ===== Footer ===== */
.footer { text-align: center; }
.hint {
  margin: 0;
  color: var(--text-dim);
  font-size: 0.78rem;
  line-height: 1.55;
}
kbd {
  display: inline-block;
  padding: 1px 6px;
  margin: 0 1px;
  font: inherit;
  font-size: 0.72rem;
  background: var(--card);
  border: 1px solid var(--card-edge);
  border-bottom-width: 2px;
  border-radius: 4px;
  color: var(--text);
}

/* ===== Dialog ===== */
dialog {
  border: 1px solid var(--card-edge);
  background: var(--card);
  color: var(--text);
  padding: 24px 28px;
  border-radius: 18px;
  min-width: 260px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}
dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
}
dialog h2 { margin: 0 0 12px; font-size: 1.3rem; color: var(--accent-strong); }
.dialog-row { margin: 4px 0; color: var(--text-dim); }
.dialog-row strong { color: var(--text); font-variant-numeric: tabular-nums; }
.dialog-btn {
  margin-top: 18px;
  background: var(--accent);
  color: #1a1410;
  font: inherit;
  font-weight: 700;
  font-size: 0.95rem;
  padding: 10px 22px;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: background 160ms, transform 100ms;
}
.dialog-btn:hover { background: var(--accent-strong); }
.dialog-btn:active { transform: scale(0.97); }
.dialog-btn:focus-visible { outline: 2px solid var(--accent-strong); outline-offset: 3px; }

/* ===== Utilities ===== */
.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0);
  white-space: nowrap; border: 0;
}

/* ===== Reduced motion ===== */
@media (prefers-reduced-motion: reduce) {
  .tile, .tile-inner, .skip-link, .new-game, .dialog-btn, .score-value {
    transition: none !important;
    animation: none !important;
  }
}
