:root {
  --tile-w: 52px;
  --tile-h: 64px;
  --layer-lift: 38px;
  --tile-radius: 7px;

  --ink: #f4ece0;
  --ink-dim: #b9a98f;
  --bg: #1c1714;
  --panel: #2a2018;
  --accent: #f0c060;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue",
    "PingFang SC", "Noto Sans CJK SC", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  overscroll-behavior: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
}

/* ---------------- top bar ---------------- */
#topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid #3a2d22;
  flex: 0 0 auto;
}
#topbar .title {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.5px;
  margin-right: auto;
  white-space: nowrap;
}
#topbar .diff-control {
  display: inline-flex;
  background: #1c1410;
  border: 1px solid #4a3a2c;
  border-radius: 7px;
  overflow: hidden;
}
#topbar .diff-control button {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--ink-dim);
  font-size: 11px;
  font-weight: 600;
  padding: 6px 9px;
  cursor: pointer;
  touch-action: manipulation;
}
#topbar .diff-control button.active {
  background: var(--accent);
  color: #2a2018;
}
#lang-control select {
  appearance: none;
  background: #1c1410;
  border: 1px solid #4a3a2c;
  border-radius: 6px;
  color: var(--ink);
  font-size: 11px;
  font-weight: 600;
  padding: 6px 8px;
  cursor: pointer;
  touch-action: manipulation;
}
#topbar .controls { display: flex; gap: 6px; flex-wrap: nowrap; }
#topbar button {
  appearance: none;
  border: 1px solid #4a3a2c;
  background: #3a2d22;
  color: var(--ink);
  font-size: 12px;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  touch-action: manipulation;
}
#topbar button:active { background: #4a3a2c; }
#topbar button:disabled { opacity: 0.35; cursor: default; }
#topbar .status {
  font-size: 12px;
  color: var(--ink-dim);
  min-width: 56px;
  text-align: right;
}
#topbar .status #status-text { color: var(--accent); font-weight: 700; }

/* ---------------- controls bar (bottom) ---------------- */
#controls-bar {
  flex: 0 0 auto;
  background: var(--panel);
  border-top: 1px solid #3a2d22;
  padding: 10px 12px calc(env(safe-area-inset-bottom, 0px) + 10px);
  display: flex;
  justify-content: center;
}
#controls-bar .controls {
  display: flex;
  gap: 14px;
}
#controls-bar .icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 10px;
  border: 1px solid #4a3a2c;
  background: #3a2d22;
  color: var(--ink);
  cursor: pointer;
  touch-action: manipulation;
}
#controls-bar .icon-btn:active { background: #4a3a2c; }
#controls-bar .icon-btn:disabled { opacity: 0.35; cursor: default; }
#controls-bar .icon-btn svg { display: block; pointer-events: none; }

/* ---------------- stage ---------------- */
#stage {
  flex: 1 1 auto;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(ellipse at 50% 35%, #2a2118 0%, #15110e 75%);
}
#scaler {
  position: relative;
  transform-origin: center center;
}
#board {
  position: relative;
  width: calc(var(--tile-w) * 6);
  height: calc(var(--tile-h) * 8 + var(--layer-lift) * 4);
}
/* ---------------- tiles ---------------- */
.tile {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--tile-w);
  height: var(--tile-h);
  border-radius: var(--tile-radius);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 4px;
  line-height: 1;
  cursor: default;
  will-change: transform;
  text-shadow: none;
  /* body background set per-tile by JS (parchment graded by layer depth) */
  box-shadow:
    inset 0 -3px 0 rgba(0,0,0,0.10),
    inset 0 2px 0 rgba(255,255,255,0.35),
    0 3px 5px rgba(0,0,0,0.45);
  border: 1px solid rgba(60,42,24,0.35);
  transition: transform 140ms ease, filter 140ms ease, box-shadow 140ms ease;
}
.tile .face-glyph {
  display: block;
  transform: translateY(-7px);
  font-weight: 700;
  line-height: 1;
  -webkit-background-clip: text;
  background-clip: text;
}

.tile.playable {
  cursor: pointer;
  border-color: rgba(240, 192, 96, 0.55);
  box-shadow:
    inset 0 -3px 0 rgba(0,0,0,0.10),
    inset 0 2px 0 rgba(255,255,255,0.35),
    0 3px 5px rgba(0,0,0,0.45),
    0 0 0 1px rgba(240,192,96,0.25);
}
.tile.playable:hover { filter: brightness(1.06); }
.tile.playable:active { transform: translate3d(var(--x), var(--y), 0) scale(0.94); }

.tile.hint { animation: hintPulse 0.9s ease-in-out 3; }

@keyframes hintPulse {
  0%, 100% {
    box-shadow:
      inset 0 -3px 0 rgba(0,0,0,0.10),
      inset 0 2px 0 rgba(255,255,255,0.35),
      0 3px 5px rgba(0,0,0,0.45),
      0 0 0 2px rgba(255,170,60,0.55);
  }
  50% {
    box-shadow:
      inset 0 -3px 0 rgba(0,0,0,0.10),
      inset 0 2px 0 rgba(255,255,255,0.35),
      0 3px 5px rgba(0,0,0,0.45),
      0 0 0 3px rgba(255,210,74,0.95),
      0 0 14px rgba(255,210,74,0.7);
  }
}
@keyframes flyToTray {
  to { transform: scale(0.6); opacity: 0; }
}
.tile.leaving {
  animation: leaveTile 220ms ease forwards;
  pointer-events: none;
}
@keyframes leaveTile {
  to { translate: 0 14px; scale: 0.55; opacity: 0; }
}
.tile.shake { animation: shake 230ms; }
@keyframes shake {
  0%,100% { translate: 0 0; }
  20%     { translate: -3px 0; }
  40%     { translate: 3px 0; }
  60%     { translate: -2px 0; }
  80%     { translate: 2px 0; }
}
.slot .tray-tile.pop { animation: popIn 220ms ease; }
@keyframes popIn {
  from { scale: 0.4; opacity: 0; }
  to   { scale: 1; opacity: 1; }
}
@keyframes vanishPair {
  0%   { transform: scale(1); opacity: 1; }
  40%  { transform: scale(1.35); opacity: 1; filter: drop-shadow(0 0 14px #fff6b0); }
  100% { transform: scale(0); opacity: 0; }
}

/* ---------------- tray (top) ---------------- */
#tray-wrap {
  flex: 0 0 auto;
  background: var(--panel);
  border-bottom: 1px solid #3a2d22;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
.tray-label {
  font-size: 11px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--ink-dim);
}
.tray {
  display: flex;
  gap: 10px;
}
.slot {
  width: 56px;
  height: 64px;
  border-radius: 8px;
  border: 2px dashed #4a3a2c;
  background: rgba(0,0,0,0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.slot.filled {
  border-style: solid;
  border-color: transparent;
  background: linear-gradient(180deg, #e8d8b6 0%, #c8b48a 100%);
  box-shadow: 0 3px 5px rgba(0,0,0,0.45);
}
.slot .tray-tile {
  font-size: 28px;
  font-weight: 700;
  -webkit-background-clip: text;
  background-clip: text;
}
.slot .tray-tile.vanishing { animation: vanishPair 280ms ease forwards; }

/* ---------------- overlay ---------------- */
#overlay {
  position: fixed;
  inset: 0;
  background: rgba(10,8,6,0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 99;
  animation: fadeIn 160ms ease;
}
#overlay.hidden { display: none; }
.overlay-card {
  background: var(--panel);
  border: 1px solid #4a3a2c;
  border-radius: 12px;
  padding: 22px 26px;
  text-align: center;
  max-width: 320px;
  box-shadow: 0 12px 32px rgba(0,0,0,0.6);
}
.overlay-card h2 { margin: 0 0 8px; font-size: 24px; color: var(--accent); }
.overlay-card p  { margin: 0 0 16px; color: var(--ink-dim); font-size: 13px; }
.overlay-card button {
  appearance: none;
  border: none;
  background: var(--accent);
  color: #2a2018;
  font-weight: 700;
  font-size: 14px;
  padding: 10px 18px;
  border-radius: 8px;
  cursor: pointer;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ---------------- responsive ---------------- */
@media (max-width: 480px) {
  #topbar { flex-wrap: wrap; gap: 6px; }
  #topbar .title { font-size: 13px; flex: 1 1 100%; margin-right: 0; }
  #topbar .diff-control button { padding: 7px 8px; font-size: 11px; }
  #topbar .status { font-size: 11px; flex: 1 0 auto; text-align: right; min-width: 0; }
  #controls-bar .icon-btn { width: 48px; height: 48px; }
  #controls-bar .icon-btn svg { width: 22px; height: 22px; }
}