:root {
  color-scheme: dark;
  --bg: #07070b;
  --stone: #181820;
  --stone-light: #2d2a36;
  --ink: #f4e8d2;
  --muted: #b5a994;
  --gold: #e2b867;
  --crimson: #b91f3a;
  --crimson-bright: #ff365d;
  --violet: #322045;
  --blue: #284a78;
  --cell: clamp(64px, 18vw, 116px);
  --gap: clamp(8px, 2.2vw, 14px);
  --board-size: calc((var(--cell) * 4) + (var(--gap) * 5));
  font-family: "Palatino Linotype", "Noto Serif TC", "Songti TC", Georgia, serif;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(5, 4, 8, 0.32), rgba(5, 4, 8, 0.82)),
    url("assets/castle-hall-bg.webp") center / cover fixed,
    var(--bg);
  overflow-x: hidden;
}

body::before {
  position: fixed;
  inset: 0;
  pointer-events: none;
  content: "";
  background:
    repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.035) 0 1px, transparent 1px 4px),
    radial-gradient(circle at 50% 52%, rgba(188, 42, 63, 0.16), transparent 32rem);
  mix-blend-mode: screen;
  opacity: 0.45;
}

button {
  font: inherit;
}

button:focus-visible,
.board:focus-visible {
  outline: 3px solid #9bc8ff;
  outline-offset: 4px;
}

[hidden] {
  display: none !important;
}

.game-shell {
  position: relative;
  z-index: 1;
  width: min(1180px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: clamp(16px, 4vw, 34px);
  display: flex;
  flex-direction: column;
  gap: clamp(16px, 3vw, 26px);
}

.hud {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 16px;
}

.kicker {
  margin: 0 0 6px;
  color: var(--gold);
  font-size: 0.78rem;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(2rem, 7vw, 4.8rem);
  line-height: 0.94;
  text-shadow: 0 4px 0 #16060b, 0 0 28px rgba(255, 54, 93, 0.34);
}

.meters {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.meter,
.hint,
.codex-panel,
.controls,
.board-frame {
  border: 2px solid #4a3941;
  background: linear-gradient(180deg, rgba(28, 27, 34, 0.92), rgba(10, 10, 14, 0.92));
  box-shadow:
    inset 0 0 0 2px rgba(236, 186, 100, 0.12),
    0 18px 44px rgba(0, 0, 0, 0.42);
}

.meter {
  min-width: 98px;
  padding: 10px 14px;
  text-align: right;
}

.meter span,
.hint span {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
}

.meter strong {
  display: block;
  color: #fff6d8;
  font-size: 1.6rem;
  line-height: 1.1;
}

.game-stage {
  display: grid;
  grid-template-columns: minmax(150px, 220px) minmax(0, auto) minmax(132px, 180px);
  align-items: center;
  justify-content: center;
  gap: clamp(14px, 3vw, 28px);
}

.codex-panel,
.controls {
  align-self: stretch;
  padding: 16px;
}

.codex-panel h2 {
  margin-bottom: 12px;
  color: #fff1c4;
  font-size: 1.1rem;
}

.legend {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.legend li {
  display: grid;
  grid-template-columns: 30px 1fr auto;
  align-items: center;
  gap: 8px;
  min-width: 0;
  color: var(--muted);
  font-size: 0.84rem;
}

.legend .mini-icon {
  width: 30px;
  height: 30px;
  background-image: url("assets/tiles-sprite.webp");
  background-repeat: no-repeat;
  background-size: 400% 300%;
  image-rendering: auto;
}

.legend strong {
  color: #fff0c8;
  font-size: 0.8rem;
}

.legend span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.board-wrap {
  width: min(var(--board-size), calc(100vw - 32px));
}

.board-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  padding: var(--gap);
  border-color: #5b454d;
}

.board-frame::before,
.board-frame::after {
  position: absolute;
  z-index: 2;
  width: 28px;
  height: 28px;
  content: "";
  border: 2px solid rgba(226, 184, 103, 0.7);
}

.board-frame::before {
  top: 8px;
  left: 8px;
  border-right: 0;
  border-bottom: 0;
}

.board-frame::after {
  right: 8px;
  bottom: 8px;
  border-top: 0;
  border-left: 0;
}

.board {
  position: relative;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--gap);
  padding: 0;
  touch-action: none;
  user-select: none;
}

.grid-cell {
  border: 1px solid rgba(226, 184, 103, 0.24);
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.05), transparent),
    #111219;
  box-shadow: inset 0 0 18px rgba(0, 0, 0, 0.55);
}

.tile {
  position: absolute;
  width: calc((100% - (var(--gap) * 3)) / 4);
  height: calc((100% - (var(--gap) * 3)) / 4);
  display: grid;
  place-items: center;
  border: 2px solid var(--tile-border, #463a43);
  background:
    radial-gradient(circle at 50% 34%, rgba(255, 255, 255, 0.08), transparent 32%),
    linear-gradient(180deg, var(--tile-top, #2a2530), var(--tile-bottom, #141319));
  box-shadow:
    inset 0 0 0 2px rgba(255, 255, 255, 0.04),
    0 10px 22px rgba(0, 0, 0, 0.42),
    0 0 20px var(--tile-glow, rgba(185, 31, 58, 0));
  transform: translate(calc(var(--x) * (100% + var(--gap))), calc(var(--y) * (100% + var(--gap))));
  transition: transform 125ms ease, filter 125ms ease;
}

.tile.new {
  animation: materialize 180ms ease-out both;
}

.tile.merged {
  animation: merge-pulse 210ms ease-out both;
}

.tile-icon {
  position: absolute;
  z-index: 1;
  top: 53%;
  left: 50%;
  width: 72%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  background-image: url("assets/tiles-sprite.webp");
  background-repeat: no-repeat;
  background-size: 400% 300%;
  filter: drop-shadow(0 7px 5px rgba(0, 0, 0, 0.55));
  pointer-events: none;
}

.tile-value {
  position: absolute;
  z-index: 2;
  right: 7px;
  bottom: 5px;
  min-width: 2ch;
  padding: 1px 4px 2px;
  color: #fff4cf;
  font: 700 clamp(0.68rem, 2.2vw, 0.92rem) ui-monospace, SFMono-Regular, Menlo, monospace;
  text-align: right;
  background: rgba(6, 5, 8, 0.62);
  border: 1px solid rgba(226, 184, 103, 0.22);
}

.tile-name {
  position: absolute;
  z-index: 2;
  top: 5px;
  left: 7px;
  right: 7px;
  color: rgba(255, 243, 208, 0.88);
  font-size: clamp(0.58rem, 1.9vw, 0.74rem);
  line-height: 1.05;
  text-align: center;
  text-shadow: 0 1px 2px #000;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status {
  min-height: 28px;
  margin-top: 10px;
  color: #fff1c7;
  text-align: center;
  text-shadow: 0 2px 4px #000;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.icon-button,
.dialog-actions button {
  min-height: 46px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #fff4d6;
  cursor: pointer;
  border: 2px solid #6b4b55;
  background: linear-gradient(180deg, #35202b, #140d13);
  box-shadow: inset 0 0 0 1px rgba(255, 225, 165, 0.18), 0 8px 18px rgba(0, 0, 0, 0.32);
}

.icon-button:hover,
.dialog-actions button:hover {
  border-color: var(--gold);
  filter: brightness(1.1);
}

.sprite-icon.restart {
  width: 28px;
  height: 28px;
  background-image: url("assets/tiles-sprite.webp");
  background-repeat: no-repeat;
  background-size: 400% 300%;
  background-position: 100% 100%;
}

.audio-mark {
  width: 16px;
  height: 16px;
  border: 2px solid var(--gold);
  border-left-width: 7px;
  clip-path: polygon(0 24%, 46% 24%, 100% 0, 100% 100%, 46% 76%, 0 76%);
}

.hint {
  padding: 10px;
}

.hint strong {
  display: block;
  color: #fff1c4;
  font-size: 0.94rem;
}

.result-dialog {
  width: min(420px, calc(100vw - 30px));
  color: var(--ink);
  border: 2px solid #75515b;
  background:
    linear-gradient(180deg, rgba(42, 24, 35, 0.97), rgba(9, 8, 12, 0.98));
  box-shadow: 0 20px 90px rgba(0, 0, 0, 0.72);
}

.result-dialog::backdrop {
  background: rgba(2, 2, 5, 0.72);
}

.dialog-content {
  padding: 22px;
  text-align: center;
}

.dialog-content h2 {
  margin-bottom: 8px;
  color: #fff1c4;
  font-size: 2rem;
}

.dialog-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 18px;
}

.dialog-actions button {
  padding: 8px 14px;
}

@keyframes materialize {
  from {
    opacity: 0;
    transform: translate(calc(var(--x) * (100% + var(--gap))), calc(var(--y) * (100% + var(--gap)))) scale(0.72);
    filter: brightness(1.8);
  }
  to {
    opacity: 1;
    transform: translate(calc(var(--x) * (100% + var(--gap))), calc(var(--y) * (100% + var(--gap)))) scale(1);
    filter: brightness(1);
  }
}

@keyframes merge-pulse {
  0% {
    transform: translate(calc(var(--x) * (100% + var(--gap))), calc(var(--y) * (100% + var(--gap)))) scale(1);
  }
  55% {
    transform: translate(calc(var(--x) * (100% + var(--gap))), calc(var(--y) * (100% + var(--gap)))) scale(1.1);
    filter: brightness(1.5);
  }
  100% {
    transform: translate(calc(var(--x) * (100% + var(--gap))), calc(var(--y) * (100% + var(--gap)))) scale(1);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
  }

  .tile {
    transition: none;
  }

  .tile.new,
  .tile.merged {
    animation: none;
  }
}

@media (max-width: 900px) {
  .hud {
    align-items: start;
    flex-direction: column;
  }

  .meters {
    justify-content: flex-start;
  }

  .game-stage {
    grid-template-columns: 1fr;
  }

  .codex-panel {
    order: 3;
  }

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

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

@media (max-width: 540px) {
  .game-shell {
    padding: 14px;
  }

  .meters {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .meter {
    min-width: 0;
  }

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

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

  .tile-name {
    display: none;
  }

  .tile-icon {
    top: 50%;
    width: 80%;
  }
}
