:root {
  --bg: #edf1ec;
  --ink: #17211d;
  --muted: #617069;
  --panel: rgba(255, 255, 255, 0.82);
  --line: rgba(23, 33, 29, 0.14);
  --accent: #1f7a5a;
  --accent-strong: #155f45;
  --danger: #ae3f3f;
  --gold: #c38d32;
  --tile: #fffaf0;
  --tile-edge: #d9c39b;
  --tile-shadow: rgba(43, 34, 20, 0.34);
  --tile-locked: rgba(78, 85, 82, 0.72);
  --board: #cbd7c8;
  --board-dark: #879889;
  --unit: 22px;
  --lift: 5px;
}

body.dark {
  --bg: #1f2420;
  --ink: #f2f4ed;
  --muted: #b7c1b8;
  --panel: rgba(36, 43, 38, 0.9);
  --line: rgba(242, 244, 237, 0.16);
  --accent: #61c49b;
  --accent-strong: #8bd9b6;
  --danger: #f07972;
  --gold: #d6a843;
  --tile: #fbf4e4;
  --tile-edge: #b89c64;
  --tile-shadow: rgba(0, 0, 0, 0.45);
  --tile-locked: rgba(35, 40, 37, 0.78);
  --board: #323c36;
  --board-dark: #111613;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background:
    linear-gradient(135deg, rgba(31, 122, 90, 0.1), transparent 34%),
    linear-gradient(315deg, rgba(174, 63, 63, 0.1), transparent 28%),
    var(--bg);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

button,
select {
  color: inherit;
  font: inherit;
}

button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel);
  cursor: pointer;
  font-weight: 750;
  touch-action: manipulation;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.42;
}

button:not(:disabled):active {
  transform: translateY(1px);
}

select {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel);
  font-weight: 700;
}

.app-shell {
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  gap: 10px;
  width: 100vw;
  height: 100dvh;
  padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
}

.topbar {
  display: grid;
  grid-template-columns: minmax(150px, 1fr) auto;
  gap: 12px;
  align-items: center;
}

.brand {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  display: grid;
  width: 42px;
  height: 42px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--tile);
  box-shadow: 0 5px 0 var(--tile-edge), 0 10px 22px rgba(0, 0, 0, 0.14);
  font-size: 28px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 1.4rem;
  line-height: 1.05;
}

.brand p,
.bottombar {
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 650;
}

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(66px, 1fr));
  gap: 6px;
  min-width: min(520px, 52vw);
}

.stats div,
.bottombar {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  backdrop-filter: blur(14px);
}

.stats div {
  padding: 7px 10px;
}

.stats span {
  display: block;
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 800;
  text-transform: uppercase;
}

.stats strong {
  display: block;
  min-width: 0;
  margin-top: 1px;
  overflow: hidden;
  font-size: 1rem;
  line-height: 1.15;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.controls {
  display: grid;
  grid-template-columns: 72px minmax(112px, 150px) 44px 44px 68px 86px 72px 44px;
  gap: 8px;
}

.controls button:first-child,
#hintBtn,
#shuffleBtn {
  background: var(--accent);
  color: #fff;
}

body.dark .controls button:first-child,
body.dark #hintBtn,
body.dark #shuffleBtn {
  color: #102019;
}

.play-area {
  min-height: 0;
}

.board-frame {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 260px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.08) 1px, transparent 1px),
    linear-gradient(rgba(255, 255, 255, 0.08) 1px, transparent 1px),
    radial-gradient(circle at 50% 50%, var(--board), var(--board-dark));
  background-size: 28px 28px, 28px 28px, auto;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.18);
}

.board {
  position: absolute;
  left: 50%;
  top: 50%;
  width: calc(var(--board-w) * var(--unit));
  height: calc(var(--board-h) * var(--unit));
  transform: translate(-50%, -50%);
  transform-origin: center;
}

.tile {
  position: absolute;
  left: calc(var(--x) * var(--unit) + var(--z) * var(--lift));
  top: calc(var(--y) * var(--unit) - var(--z) * var(--lift));
  z-index: var(--stack);
  display: grid;
  width: calc(2 * var(--unit));
  height: calc(2.7 * var(--unit));
  place-items: center;
  padding: 0;
  border: 1px solid rgba(89, 68, 33, 0.38);
  border-radius: 7px;
  background:
    linear-gradient(130deg, rgba(255, 255, 255, 0.92), rgba(255, 255, 255, 0.25) 44%, transparent 45%),
    var(--tile);
  box-shadow:
    calc(var(--lift) * 0.75) calc(var(--lift) * 0.9) 0 var(--tile-edge),
    calc(var(--lift) * 1.15) calc(var(--lift) * 1.3) 12px var(--tile-shadow);
  transition:
    transform 140ms ease,
    filter 140ms ease,
    opacity 180ms ease,
    box-shadow 140ms ease;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.tile.free {
  cursor: pointer;
}

.tile.locked {
  color: var(--tile-locked);
  filter: saturate(0.55) brightness(0.92);
}

.tile.selected {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
  transform: translateY(-5px);
}

.tile.hint {
  outline: 3px solid var(--gold);
  outline-offset: 2px;
}

.tile.bad {
  outline: 3px solid var(--danger);
  outline-offset: 2px;
}

.tile.removing {
  opacity: 0;
  transform: translateY(-12px) scale(0.88);
}

.tile-face {
  display: grid;
  width: 82%;
  height: 82%;
  place-items: center;
  border: 1px solid rgba(31, 122, 90, 0.16);
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.38);
}

.tile-symbol {
  font-family: "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", serif;
  font-size: calc(var(--unit) * 1.08);
  line-height: 1;
}

.tile-rank {
  position: absolute;
  right: 5px;
  bottom: 4px;
  color: rgba(23, 33, 29, 0.72);
  font-size: calc(var(--unit) * 0.38);
  font-weight: 900;
}

.tile[data-family="dragon"] .tile-symbol,
.tile[data-family="flower"] .tile-symbol,
.tile[data-family="season"] .tile-symbol {
  font-size: calc(var(--unit) * 0.92);
}

.tile[data-suit="characters"] .tile-rank,
.tile[data-family="dragon"][data-key="dragon-red"] .tile-rank {
  color: var(--danger);
}

.tile[data-suit="bamboo"] .tile-rank,
.tile[data-family="dragon"][data-key="dragon-green"] .tile-rank {
  color: var(--accent-strong);
}

.tile[data-suit="dots"] .tile-rank,
.tile[data-family="wind"] .tile-rank {
  color: #315c9f;
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 2000;
  display: grid;
  place-items: center;
  background: rgba(10, 14, 12, 0.62);
  backdrop-filter: blur(8px);
}

.overlay div {
  display: grid;
  gap: 14px;
  min-width: 180px;
  padding: 20px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 8px;
  color: #fff;
  background: rgba(23, 33, 29, 0.88);
  text-align: center;
}

.bottombar {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 7px 10px;
}

@media (max-width: 760px) {
  body {
    overflow: auto;
  }

  .app-shell {
    min-height: 100dvh;
    height: auto;
    gap: 8px;
  }

  .topbar {
    grid-template-columns: 1fr;
  }

  .stats {
    grid-template-columns: repeat(4, 1fr);
    min-width: 0;
  }

  .stats div {
    padding: 6px 7px;
  }

  .stats span {
    font-size: 0.62rem;
  }

  .stats strong {
    font-size: 0.92rem;
  }

  .controls {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .controls select {
    grid-column: span 2;
  }

  .play-area {
    height: calc(100dvh - 230px);
    min-height: 360px;
  }

  .tile {
    border-radius: 6px;
  }

  .brand-mark {
    width: 38px;
    height: 38px;
    font-size: 24px;
  }

  h1 {
    font-size: 1.22rem;
  }
}

@media (max-width: 430px) {
  .app-shell {
    padding-inline: 7px;
  }

  .controls {
    gap: 6px;
  }

  button,
  select {
    min-height: 38px;
    padding-inline: 6px;
    font-size: 0.88rem;
  }

  .bottombar {
    align-items: center;
    font-size: 0.76rem;
  }
}
