:root {
  --felt-dark: #10503f;
  --felt: #19795b;
  --felt-light: #2a9470;
  --wood-dark: #62381f;
  --wood: #a96b3c;
  --wood-light: #d79a5f;
  --cream: #fff8e8;
  --ink: #26362f;
  --muted: #6e756f;
  --coral: #ef695f;
  --coral-dark: #b83d3e;
  --blue: #3d8ed0;
  --blue-dark: #24649b;
  --gold: #f2bf4f;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  color: var(--ink);
  font-family: "Avenir Next", "PingFang SC", "Microsoft YaHei", sans-serif;
  background:
    radial-gradient(circle at 18% 12%, rgb(255 255 255 / 40%) 0 2px, transparent 3px),
    radial-gradient(circle at 82% 74%, rgb(255 255 255 / 24%) 0 2px, transparent 3px),
    linear-gradient(145deg, #f7d976 0%, #f4a95b 48%, #df765a 100%);
  background-size: 40px 40px, 54px 54px, auto;
}

button {
  font: inherit;
}

.game-shell {
  width: min(1080px, calc(100% - 32px));
  margin: 0 auto;
  padding: 28px 0 36px;
}

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.eyebrow {
  margin: 0 0 3px;
  color: #6c3a2c;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.2em;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  font-family: "STKaiti", "KaiTi", "Avenir Next", sans-serif;
  line-height: 1;
}

h1 {
  color: #4a2a22;
  font-size: clamp(38px, 6vw, 62px);
  letter-spacing: 0.08em;
  text-shadow: 0 2px 0 rgb(255 255 255 / 55%);
}

.restart-button,
.sound-button,
.history-button,
.play-again-button {
  border: 0;
  border-radius: 999px;
  color: #fffdf5;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 6px 0 #763f29, 0 10px 18px rgb(84 42 30 / 24%);
  transition: transform 140ms ease, box-shadow 140ms ease;
}

.sound-button {
  padding: 12px 16px;
  border: 2px solid rgb(82 52 33 / 14%);
  color: #60443a;
  background: rgb(255 248 232 / 92%);
  box-shadow: 0 5px 0 #c79062, 0 9px 16px rgb(84 42 30 / 18%);
}

.history-button {
  padding: 12px 16px;
  color: #60443a;
  background: #f8ca59;
  box-shadow: 0 5px 0 #b77926, 0 9px 16px rgb(84 42 30 / 18%);
}

.sound-button[aria-pressed="false"] {
  color: #8b807b;
  background: #eee7db;
}

.restart-button {
  padding: 12px 20px;
  background: #965436;
}

.restart-button:hover,
.play-again-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 0 #763f29, 0 13px 20px rgb(84 42 30 / 24%);
}

.restart-button:active,
.play-again-button:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #763f29;
}

.restart-button:focus-visible,
.sound-button:focus-visible,
.history-button:focus-visible,
.play-again-button:focus-visible,
.tile:focus-visible {
  outline: 4px solid #fff;
  outline-offset: 3px;
}

.play-area {
  display: grid;
  grid-template-columns: 190px minmax(0, 1fr);
  align-items: start;
  gap: 18px;
}

.scoreboard {
  min-width: 0;
}

.players {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 10px;
  margin-bottom: 10px;
}

.player-card,
.round-status {
  border: 2px solid rgb(82 52 33 / 14%);
  border-radius: 18px;
  background: rgb(255 248 232 / 92%);
  box-shadow: 0 8px 18px rgb(93 51 34 / 16%);
}

.player-card {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-width: 0;
  min-height: 68px;
  padding: 13px 16px;
  overflow: hidden;
  transition: transform 180ms ease, box-shadow 180ms ease;
}

.player-card::before {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 8px;
  content: "";
  background: var(--player-color);
}

.player-card::before {
  left: 0;
}

.player-card.is-active {
  transform: translateX(5px);
  box-shadow: -5px 0 0 var(--player-color), 0 10px 22px rgb(93 51 34 / 24%);
}

.player-label {
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-card strong {
  flex: 0 0 auto;
  margin-left: 8px;
  color: var(--player-color);
  font-size: 20px;
}

.round-status {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10px;
  text-align: center;
  min-height: 70px;
}

.round-status p {
  margin: 2px 0;
  font-weight: 900;
  overflow-wrap: anywhere;
}

.round-status > span:last-child {
  color: var(--muted);
  font-size: 13px;
}

.turn-marker {
  color: var(--gold);
  font-size: 14px;
  text-shadow: 0 1px 0 #9d6b16;
}

.table-frame {
  position: relative;
  padding: clamp(14px, 2.5vw, 28px);
  border: 8px solid var(--wood);
  border-radius: 30px;
  background:
    linear-gradient(90deg, rgb(255 255 255 / 14%), transparent 18% 82%, rgb(71 31 13 / 13%)),
    repeating-linear-gradient(6deg, var(--wood-light) 0 2px, var(--wood) 3px 12px, #b77744 13px 18px);
  box-shadow:
    inset 0 0 0 3px rgb(255 230 180 / 35%),
    inset 0 0 0 8px rgb(83 39 17 / 22%),
    0 13px 0 var(--wood-dark),
    0 24px 34px rgb(94 46 28 / 30%);
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--board-columns, 6), minmax(0, 1fr));
  gap: clamp(8px, 1.5vw, 16px);
  padding: clamp(13px, 2vw, 24px);
  border-radius: 17px;
  background:
    radial-gradient(circle at 18% 30%, rgb(255 255 255 / 6%) 0 1px, transparent 2px),
    linear-gradient(145deg, var(--felt-light), var(--felt) 45%, var(--felt-dark));
  background-size: 16px 16px, auto;
  box-shadow: inset 0 4px 14px rgb(3 54 39 / 45%);
}

.tile {
  min-width: 0;
  aspect-ratio: 1;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  perspective: 800px;
}

.tile-inner {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  transform-style: preserve-3d;
  transition: transform 420ms cubic-bezier(0.2, 0.75, 0.25, 1.15), opacity 280ms ease;
}

.tile.is-flipped .tile-inner,
.tile.is-matched .tile-inner {
  transform: rotateY(180deg);
}

.tile.is-matched .tile-inner {
  opacity: 0;
  transform: rotateY(180deg) scale(0.72) translateY(-12px);
}

.tile-face {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
  border-radius: 50%;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.tile-back {
  border: 3px solid #fff1c2;
  color: #fff4c8;
  font-size: clamp(20px, 3.5vw, 40px);
  background: #df544f center / cover no-repeat;
  background-image: url("assets/tile-back.png");
  background-image: image-set(
    url("assets/tile-back.webp") type("image/webp"),
    url("assets/tile-back.png") type("image/png")
  );
  box-shadow: inset 0 0 0 4px #cf4145, 0 5px 0 #6e2732, 0 8px 12px rgb(3 44 33 / 32%);
}

.tile-back::after {
  content: none;
}

.tile-front {
  transform: rotateY(180deg);
  border: 3px solid #f4dfaf;
  background: linear-gradient(150deg, #fffdf4, #f4e4bd);
  box-shadow: inset 0 0 0 4px #fff8de, 0 5px 0 #a8834d, 0 8px 12px rgb(3 44 33 / 28%);
}

.tile-symbol {
  display: block;
  width: 60%;
  aspect-ratio: 0.75;
  background-image: url("assets/tile-icons.png");
  background-image: image-set(
    url("assets/tile-icons.webp") type("image/webp"),
    url("assets/tile-icons.png") type("image/png")
  );
  background-repeat: no-repeat;
  background-position: var(--sprite-x) var(--sprite-y);
  background-size: 600% 300%;
  filter: drop-shadow(0 3px 0 rgb(94 64 35 / 15%));
}

.game-tip {
  margin: 27px auto 0;
  color: #5d3529;
  font-size: 14px;
  font-weight: 700;
  text-align: center;
}

.modal {
  position: fixed;
  z-index: 10;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
}

.modal[hidden] {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgb(28 33 27 / 68%);
  backdrop-filter: blur(5px);
}

.result-card,
.settings-card,
.history-card {
  position: relative;
  width: min(420px, 100%);
  padding: 42px 28px 30px;
  border: 6px solid var(--wood);
  border-radius: 28px;
  background: var(--cream);
  box-shadow: 0 16px 0 var(--wood-dark), 0 28px 55px rgb(0 0 0 / 30%);
  text-align: center;
  animation: result-arrive 380ms cubic-bezier(0.2, 0.85, 0.25, 1.2) both;
}

.history-card {
  width: min(560px, 100%);
  max-height: calc(100vh - 48px);
  padding: 28px;
  overflow: hidden;
  text-align: left;
}

.history-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 20px;
}

.history-heading h2 {
  margin-top: 8px;
  color: #4b2b23;
  font-size: 36px;
}

.close-history-button {
  flex: 0 0 auto;
  width: 42px;
  height: 42px;
  padding: 0 0 4px;
  border: 0;
  border-radius: 50%;
  color: #fffdf5;
  font-size: 30px;
  line-height: 1;
  background: var(--coral);
  box-shadow: 0 4px 0 var(--coral-dark);
  cursor: pointer;
}

.history-list {
  max-height: min(52vh, 460px);
  padding-right: 4px;
  overflow-y: auto;
}

.history-item {
  padding: 16px 18px;
  border: 2px solid #ead6aa;
  border-radius: 16px;
  background: #fffdf6;
}

.history-item + .history-item {
  margin-top: 10px;
}

.history-item-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
}

.history-item-heading strong {
  color: var(--felt);
  font-size: 18px;
}

.history-item-heading time {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.history-item p {
  margin: 9px 0 0;
  color: #60443a;
  font-size: 14px;
  font-weight: 700;
  line-height: 1.6;
}

.history-empty {
  padding: 38px 20px;
  border: 2px dashed #dfc99d;
  border-radius: 18px;
  color: var(--muted);
  text-align: center;
  background: #fffdf6;
}

.history-empty span,
.history-empty strong {
  display: block;
}

.history-empty span {
  margin-bottom: 10px;
  font-size: 44px;
}

.history-empty strong {
  color: #60443a;
  font-size: 18px;
}

.history-empty p {
  margin: 6px 0 0;
  font-size: 14px;
}

.clear-history-button {
  width: 100%;
  margin-top: 18px;
  padding: 12px 18px;
  border: 2px solid #d9bca0;
  border-radius: 999px;
  color: #8b493e;
  font-weight: 800;
  background: #fff5e7;
  cursor: pointer;
}

.clear-history-button:disabled {
  opacity: 0.45;
  cursor: default;
}

.close-history-button:focus-visible,
.clear-history-button:focus-visible {
  outline: 4px solid var(--gold);
  outline-offset: 3px;
}

.settings-card {
  position: relative;
  width: min(430px, 100%);
  max-height: calc(100vh - 48px);
  padding: 0 30px 30px;
  overflow-y: auto;
  border: 6px solid var(--wood);
  border-radius: 28px;
  background: var(--cream);
  box-shadow: 0 16px 0 var(--wood-dark), 0 28px 55px rgb(0 0 0 / 30%);
}

.setup-cover-pic {
  display: contents;
}

.setup-cover {
  display: block;
  width: calc(100% + 60px);
  height: 150px;
  margin: 0 -30px 24px;
  border-bottom: 5px solid var(--wood-light);
  border-radius: 21px 21px 0 0;
  object-fit: cover;
  object-position: center 53%;
}

.settings-card h2 {
  margin: 8px 0 26px;
  color: #4b2b23;
  font-size: 36px;
}

.option-settings {
  min-width: 0;
  padding: 0;
  border: 0;
  margin-bottom: 18px;
}

.option-settings legend {
  width: 100%;
  margin-bottom: 8px;
  color: #60443a;
  font-size: 14px;
  font-weight: 900;
}

.player-name-field input {
  width: 100%;
  min-height: 48px;
  padding: 0 14px;
  border: 2px solid #dfc99d;
  border-radius: 12px;
  color: var(--ink);
  font: inherit;
  font-weight: 700;
  background: #fffdf6;
}

.player-name-field input:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: 2px;
}

.option-buttons {
  display: grid;
  gap: 8px;
}

.player-count-options {
  grid-template-columns: repeat(5, minmax(0, 1fr));
}

.tile-count-options {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.option-button {
  position: relative;
  min-width: 0;
  min-height: 48px;
  padding: 8px 5px;
  border: 2px solid #dfc99d;
  border-radius: 14px;
  color: #60443a;
  font-weight: 900;
  line-height: 1.1;
  background: #fffdf6;
  box-shadow: 0 3px 0 #d2b77e;
  cursor: pointer;
  transition: transform 140ms ease, border-color 140ms ease, background 140ms ease, box-shadow 140ms ease;
}

.option-button small {
  display: block;
  margin-top: 3px;
  color: #8a6855;
  font-size: 10px;
}

.option-button[aria-pressed="true"] {
  border-color: #e1a92c;
  color: #fff;
  background: var(--felt);
  box-shadow: 0 4px 0 var(--felt-dark), 0 7px 12px rgb(20 77 59 / 18%);
  transform: translateY(-1px);
}

.option-button[aria-pressed="true"] small {
  color: #fff4bd;
}

.option-button:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: 3px;
}

.player-name-settings {
  padding: 0;
  border: 0;
  margin: 0 0 18px;
}

.player-name-settings legend {
  width: 100%;
  margin-bottom: 8px;
  color: #60443a;
  font-size: 14px;
  font-weight: 900;
}

.player-name-fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.player-name-field {
  display: block;
}

.player-name-field span {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

.player-name-field input {
  width: 100%;
  min-width: 0;
  min-height: 44px;
  padding: 0 12px;
}

.result-crown {
  display: block;
  margin-bottom: 12px;
  color: var(--gold);
  font-size: 64px;
  line-height: 1;
  text-shadow: 0 4px 0 #a76a20;
}

.result-card h2 {
  margin-top: 8px;
  color: #4b2b23;
  font-size: 36px;
}

.result-score {
  margin: 18px 0 24px;
  color: var(--muted);
  font-size: 22px;
  font-weight: 900;
}

.play-again-button {
  width: 100%;
  padding: 14px 20px;
  background: var(--felt);
  box-shadow: 0 6px 0 var(--felt-dark), 0 10px 18px rgb(20 77 59 / 22%);
}

@keyframes result-arrive {
  from {
    opacity: 0;
    transform: translateY(28px) scale(0.9);
  }
}

@media (max-width: 680px) {
  .game-shell {
    width: 100%;
    padding:
      max(14px, env(safe-area-inset-top))
      max(8px, env(safe-area-inset-right))
      calc(28px + env(safe-area-inset-bottom))
      max(8px, env(safe-area-inset-left));
  }

  .game-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 10px;
    margin-bottom: 14px;
  }

  .game-header h1 {
    font-size: clamp(36px, 12vw, 48px);
  }

  .header-actions {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    width: 100%;
    max-width: none;
    gap: 6px;
  }

  .restart-button,
  .history-button,
  .sound-button {
    min-width: 0;
    min-height: 42px;
    padding: 8px 4px;
    font-size: 12px;
    line-height: 1.15;
    white-space: nowrap;
    box-shadow: 0 4px 0 #763f29, 0 7px 12px rgb(84 42 30 / 18%);
  }

  .history-button {
    box-shadow: 0 4px 0 #b77926, 0 7px 12px rgb(84 42 30 / 18%);
  }

  .sound-button {
    box-shadow: 0 4px 0 #c79062, 0 7px 12px rgb(84 42 30 / 18%);
  }

  .play-area {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 12px;
  }

  .scoreboard {
    position: sticky;
    z-index: 5;
    top: env(safe-area-inset-top, 0px);
    grid-row: 1;
    grid-column: 1;
    padding: 7px;
    border: 2px solid rgb(82 52 33 / 20%);
    border-radius: 15px;
    background: #fff3d7;
    box-shadow: 0 7px 18px rgb(93 51 34 / 25%);
  }

  .table-frame {
    grid-row: 2;
    grid-column: 1;
    width: 100%;
  }

  .scoreboard,
  .table-frame,
  .board {
    min-width: 0;
  }

  .players {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 5px;
    margin-bottom: 5px;
  }

  .player-card {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 3px;
    min-height: 38px;
    padding: 5px 6px 5px 10px;
    border-width: 1px;
    border-radius: 9px;
    box-shadow: none;
  }

  .player-card::before {
    width: 5px;
  }

  .player-card.is-active {
    transform: translateY(-1px);
    box-shadow: 0 0 0 2px var(--player-color), 0 4px 8px rgb(93 51 34 / 20%);
  }

  .player-label {
    display: block;
    min-width: 0;
    overflow: hidden;
    font-size: 11px;
    letter-spacing: 0;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .player-card strong {
    display: block;
    margin: 0;
    font-size: 14px;
    white-space: nowrap;
  }

  .round-status {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 5px;
    width: 100%;
    min-height: 30px;
    padding: 5px 8px;
    border-width: 1px;
    border-radius: 9px;
    font-size: 11px;
    box-shadow: none;
    text-align: left;
  }

  .round-status p {
    min-width: 0;
    max-width: 100%;
    margin: 0;
    overflow: hidden;
    line-height: 1.25;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .round-status > span:last-child {
    font-size: 11px;
    line-height: 1.3;
    white-space: nowrap;
  }

  .turn-marker {
    font-size: 11px;
  }

  .table-frame {
    padding: clamp(5px, 1.8vw, 8px);
    border-width: clamp(4px, 1.5vw, 6px);
    border-radius: 18px;
    box-shadow:
      inset 0 0 0 2px rgb(255 230 180 / 35%),
      inset 0 0 0 5px rgb(83 39 17 / 22%),
      0 7px 0 var(--wood-dark),
      0 13px 20px rgb(94 46 28 / 28%);
  }

  .board {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: clamp(3px, 1.5vw, 7px);
    padding: clamp(5px, 1.8vw, 8px);
    border-radius: 12px;
  }

  .tile-back,
  .tile-front {
    border-width: 2px;
  }

  .game-tip {
    max-width: 320px;
    margin-top: 18px;
    padding: 0 8px;
    font-size: 12px;
    line-height: 1.6;
  }

  .modal {
    place-items: center;
    padding:
      max(12px, env(safe-area-inset-top))
      max(10px, env(safe-area-inset-right))
      max(20px, env(safe-area-inset-bottom))
      max(10px, env(safe-area-inset-left));
    overflow-y: auto;
  }

  .result-card,
  .settings-card,
  .history-card {
    max-height: calc(100dvh - max(12px, env(safe-area-inset-top)) - max(20px, env(safe-area-inset-bottom)));
    border-width: 5px;
    border-radius: 22px;
  }

  .result-card {
    padding: 28px 18px 24px;
    overflow-y: auto;
  }

  .settings-card {
    padding: 0 18px 22px;
  }

  .history-card {
    display: flex;
    flex-direction: column;
    padding: 20px 16px;
  }

  .setup-cover {
    width: calc(100% + 36px);
    height: 112px;
    margin: 0 -18px 18px;
    border-radius: 17px 17px 0 0;
  }

  .settings-card h2,
  .result-card h2,
  .history-heading h2 {
    font-size: 30px;
  }

  .settings-card h2 {
    margin-bottom: 18px;
  }

  .option-settings {
    margin-bottom: 14px;
  }

  .player-name-field input {
    min-height: 44px;
  }

  .option-button {
    min-height: 46px;
    font-size: 14px;
  }

  .player-name-fields {
    gap: 8px;
  }

  .history-heading {
    gap: 12px;
    margin-bottom: 14px;
  }

  .close-history-button {
    width: 44px;
    height: 44px;
  }

  .history-list {
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
  }

  .history-item {
    padding: 13px 14px;
  }

  .history-item-heading {
    display: block;
  }

  .history-item-heading time {
    display: block;
    margin-top: 4px;
  }

  .clear-history-button,
  .play-again-button {
    min-height: 46px;
  }
}

@media (max-width: 350px) {
  .game-shell {
    padding-right: max(6px, env(safe-area-inset-right));
    padding-left: max(6px, env(safe-area-inset-left));
  }

  .player-card {
    grid-template-columns: minmax(0, 1fr);
    gap: 1px;
    padding: 4px 4px 4px 9px;
  }

  .player-card strong {
    font-size: 12px;
  }

  .board {
    gap: 3px;
    padding: 5px;
  }

  .player-name-fields {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
