:root {
  --w: 18;
  --h: 11;
  /* ô gốc 40x50 px -> cao = rộng * 1.25 */
  --cell: max(22px, min(calc((100vw - 24px) / var(--w)), calc((100vh - 215px) / (var(--h) * 1.25)), 48px));
  --cell-h: calc(var(--cell) * 1.25);
  --board-w: calc(var(--cell) * var(--w));
  --board-h: calc(var(--cell-h) * var(--h));
  --bg1: #06202a;
  --bg2: #0b3b46;
  --panel: #0f2f3a;
  --text: #e6f4f1;
  --muted: #8fb5b0;
  --accent: #facc15;
  --line: #ff3b3b;
}
@supports (height: 100dvh) {
  :root { --cell: max(22px, min(calc((100vw - 24px) / var(--w)), calc((100dvh - 215px) / (var(--h) * 1.25)), 48px)); }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: system-ui, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at 15% 10%, #146b5a 0, transparent 45%),
    radial-gradient(circle at 85% 90%, #0e5e7a 0, transparent 45%),
    linear-gradient(160deg, var(--bg1), var(--bg2));
  background-attachment: fixed;
  display: flex;
  flex-direction: column;
  align-items: center;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- HUD ---------- */
header {
  width: var(--board-w);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0 8px;
  gap: 8px;
}
.stat { display: flex; flex-direction: column; align-items: center; min-width: 60px; }
.stat .label { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.stat b { font-size: 20px; }

#timebar {
  width: var(--board-w);
  height: 10px;
  background: rgba(255,255,255,.1);
  border-radius: 5px;
  overflow: hidden;
  margin-bottom: 10px;
}
#timefill {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, #22c55e, #a3e635);
  transition: width .25s linear, background .3s;
}
#timefill.low {
  background: linear-gradient(90deg, #ef4444, #f97316);
  animation: blink .5s infinite alternate;
}

/* ---------- Board ---------- */
#board-wrap {
  position: relative;
  width: var(--board-w);
  height: var(--board-h);
}
#board {
  display: grid;
  grid-template-columns: repeat(var(--w), var(--cell));
  grid-template-rows: repeat(var(--h), var(--cell-h));
}
.tile { padding: 1px; }
/* mỗi ô là 1 mảnh của sprite sheet gốc (36 mảnh, nền ô đã có sẵn trong hình) */
.tile .face {
  display: block;
  width: 100%;
  height: 100%;
  position: relative;
  background-image: url("images/pieces-sprite.png");
  background-repeat: no-repeat;
  background-size: calc((var(--cell) - 2px) * 36) 100%;
  background-position: calc((var(--cell) - 2px) * -1 * var(--i, 0)) 0;
  cursor: pointer;
  transition: transform .08s, box-shadow .08s, filter .08s;
}
.tile.empty .face { visibility: hidden; }
.face .alt { display: none; }

/* dự phòng khi không tải được sprite sheet: ô màu đào + emoji */
.nosheet .face {
  background: linear-gradient(180deg, #fff9f2, #f7c29a);
  border: 1px solid #c88c62;
  border-radius: 3px;
}
.nosheet .face .alt {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  font-size: calc(var(--cell) * .6);
  line-height: 1;
}

.tile.selected .face {
  filter: brightness(.55);
  transform: scale(1.06);
  z-index: 1;
}
.tile.hint .face { animation: pulse .45s infinite alternate; }
.tile.pop .face { animation: pop .2s forwards; }

@keyframes pulse {
  from { box-shadow: 0 0 0 2px #22d3ee; }
  to   { box-shadow: 0 0 0 4px #67e8f9, 0 0 18px #22d3ee; }
}
@keyframes pop {
  0%   { transform: scale(1.08); }
  100% { transform: scale(0); opacity: 0; }
}
@keyframes blink { from { opacity: 1; } to { opacity: .5; } }

#path { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#line {
  fill: none;
  stroke: var(--line);
  stroke-width: .14;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0;
  transition: opacity .3s;
}
#line.show { opacity: 1; transition: none; }

.float {
  position: absolute;
  transform: translate(-50%, -50%);
  color: var(--accent);
  font-weight: 800;
  font-size: calc(var(--cell) * .45);
  text-shadow: 0 2px 4px #000;
  pointer-events: none;
  animation: rise .8s forwards;
}
@keyframes rise { to { transform: translate(-50%, -180%); opacity: 0; } }

/* ---------- Controls ---------- */
footer {
  width: var(--board-w);
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 12px 0;
  flex-wrap: wrap;
}
button {
  font: inherit;
  font-weight: 600;
  color: var(--text);
  background: rgba(255,255,255,.08);
  border: 1px solid rgba(255,255,255,.15);
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
  transition: background .15s, transform .08s;
}
button:hover:not(:disabled) { background: rgba(255,255,255,.16); }
button:active:not(:disabled) { transform: scale(.96); }
button:disabled { opacity: .4; cursor: default; }
button.primary {
  background: var(--accent);
  color: #1e1b4b;
  border-color: transparent;
  font-size: 18px;
  padding: 10px 28px;
}
button.primary:hover { background: #fde047; }

#toast {
  position: fixed;
  left: 50%;
  top: 14px;
  transform: translate(-50%, -30px);
  background: rgba(3,20,28,.92);
  border: 1px solid rgba(255,255,255,.15);
  padding: 8px 16px;
  border-radius: 999px;
  opacity: 0;
  transition: .25s;
  pointer-events: none;
  font-weight: 600;
  z-index: 5;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(2,12,18,.75);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.overlay.hidden { display: none; }
.panel {
  background: var(--panel);
  border-radius: 18px;
  padding: 28px 32px;
  text-align: center;
  max-width: min(92vw, 420px);
  box-shadow: 0 20px 60px rgba(0,0,0,.6);
  border: 1px solid rgba(255,255,255,.08);
}
.panel h1 { margin: 0 0 12px; font-size: 30px; }
.panel p { color: var(--muted); line-height: 1.5; margin: 0 0 20px; }
.panel p b { color: var(--text); }
