:root {
  color-scheme: dark;
  font-family: "Avenir Next", "PingFang SC", "Noto Sans SC", "Microsoft YaHei", system-ui, sans-serif;
  --bg: #060a12;
  --panel: #101b2b;
  --ink: #f5efe3;
  --muted: #93a4b8;
  --gold: #e6b56e;
  --gold-bright: #ffd49a;
  --line: rgba(158, 184, 212, 0.15);
  --action-ink: #201306;
  --game-icon-size: 46px;
  --game-action-size: 48px;
  --game-ui-font: 15px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: stretch;
  background:
    radial-gradient(1100px 720px at 50% 4%, rgba(45, 73, 104, 0.42), transparent 68%),
    radial-gradient(900px 620px at 82% 104%, rgba(83, 55, 26, 0.24), transparent 64%),
    var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; }

[hidden] { display: none !important; }
.room-panel { margin-top: 10px; flex: none; }
.room-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.room-panel button, .room-panel input {
  min-height: 36px; border: 1px solid var(--line); border-radius: 8px;
  background: #142237; color: var(--ink); font: inherit; padding: 6px 9px; font-size: 13px;
}
.room-panel button { cursor: pointer; }
.room-panel input { min-width: 0; width: 115px; }
.room-panel input::placeholder { color: #a8b5c8; }
.room-panel #room-link { width: 100%; margin-top: 6px; }
#room-identity { font-size: 13px; margin-right: auto; }
#room-message { margin: 6px 0 0; color: #b7c6db; font-size: 12px; line-height: 1.5; }
button:disabled { opacity: .5; cursor: default; }
.room-panel :focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

#stage {
  position: relative;
  isolation: isolate;
  width: min(100vw, 760px, 80dvh);
  height: 100dvh;
  overflow: hidden;
  background:
    radial-gradient(72% 38% at 50% 0%, rgba(47, 77, 108, 0.42), transparent 74%),
    linear-gradient(180deg, #0d1828 0%, #080e18 56%, #060a11 100%);
  box-shadow: 0 0 90px rgba(0, 0, 0, 0.62);
}

#stage::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(38% 25% at 50% 11%, rgba(255, 204, 135, 0.06), transparent 74%),
    linear-gradient(90deg, rgba(255, 255, 255, 0.025), transparent 18%, transparent 82%, rgba(255, 255, 255, 0.025));
}

.shell {
  position: relative;
  z-index: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding:
    calc(var(--game-safe-top, 0px) + 12px)
    max(clamp(14px, 3.8vw, 28px), var(--game-safe-right, 0px))
    calc(var(--game-safe-bottom, 0px) + 10px)
    max(clamp(14px, 3.8vw, 28px), var(--game-safe-left, 0px));
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding-top: 4px;
}

.brand { min-width: 0; }

.eyebrow {
  margin: 0 0 5px;
  color: var(--gold);
  font-size: clamp(10px, 2.7vw, 12px);
  font-weight: 750;
  line-height: 1.2;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  color: #faf5e9;
  font-family: Georgia, "Songti SC", "Noto Serif SC", "STSong", serif;
  font-size: clamp(30px, 7.6vw, 40px);
  font-weight: 600;
  line-height: 1;
  letter-spacing: 0.055em;
  text-shadow: 0 2px 18px rgba(0, 0, 0, 0.34);
}

#settings {
  position: relative;
  z-index: 4;
  flex: none;
}

.game-settings-trigger {
  display: grid;
  place-items: center;
  width: clamp(40px, 10.5vw, 46px);
  height: clamp(40px, 10.5vw, 46px);
  padding: 0;
  color: #efc78d;
  background: linear-gradient(180deg, rgba(27, 45, 67, 0.94), rgba(13, 24, 39, 0.94));
  border: 1px solid rgba(230, 181, 110, 0.32);
  border-radius: 50%;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08), 0 8px 22px rgba(0, 0, 0, 0.28);
  cursor: pointer;
  touch-action: manipulation;
}

.game-settings-trigger:hover { border-color: rgba(230, 181, 110, 0.62); }
.game-settings-trigger:active { transform: translateY(1px); }
.game-settings-trigger svg { width: 21px; height: 21px; }

.players {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 10px;
  margin-top: clamp(12px, 2.4vh, 20px);
}

.player {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: linear-gradient(180deg, rgba(22, 39, 60, 0.78), rgba(11, 21, 35, 0.86));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.045);
  transition: border-color 180ms ease, background 180ms ease, box-shadow 180ms ease, opacity 180ms ease;
}

.player.is-active {
  border-color: rgba(230, 181, 110, 0.7);
  background: linear-gradient(180deg, rgba(62, 48, 27, 0.66), rgba(19, 27, 38, 0.9));
  box-shadow: 0 0 0 1px rgba(230, 181, 110, 0.12), 0 10px 28px rgba(0, 0, 0, 0.24), inset 0 1px 0 rgba(255, 233, 196, 0.13);
}

.player.is-winner {
  border-color: rgba(255, 212, 154, 0.9);
  background: linear-gradient(180deg, rgba(78, 57, 28, 0.74), rgba(22, 27, 35, 0.94));
  box-shadow: 0 0 22px rgba(230, 181, 110, 0.12), inset 0 1px 0 rgba(255, 241, 210, 0.18);
}

.player:not(.is-active):not(.is-winner) { opacity: 0.82; }

.swatch {
  width: 28px;
  height: 28px;
  flex: none;
  border-radius: 50%;
}

.swatch-black {
  background: radial-gradient(circle at 33% 27%, #7d8797 0%, #31353c 25%, #111317 58%, #030405 100%);
  box-shadow: 0 4px 9px rgba(0, 0, 0, 0.55), inset -2px -3px 6px rgba(0, 0, 0, 0.8), inset 2px 3px 5px rgba(160, 176, 196, 0.32);
}

.swatch-white {
  background: radial-gradient(circle at 33% 27%, #ffffff 0%, #f6f1e7 38%, #d1c8b8 72%, #a69d8e 100%);
  box-shadow: 0 4px 9px rgba(0, 0, 0, 0.4), inset -2px -3px 6px rgba(125, 115, 96, 0.28), inset 2px 3px 5px rgba(255, 255, 255, 0.9);
}

.player-copy {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.player-copy strong {
  overflow: hidden;
  color: #f4efe5;
  font-size: clamp(14px, 3.7vw, 17px);
  font-weight: 750;
  line-height: 1.2;
  letter-spacing: 0.02em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-meta {
  display: flex;
  align-items: center;
  gap: 5px;
  min-width: 0;
  color: var(--muted);
  font-size: clamp(9px, 2.5vw, 11px);
  font-weight: 600;
  line-height: 1.2;
  letter-spacing: 0.1em;
  white-space: nowrap;
}

.player.is-active .player-meta [data-player-role],
.player.is-winner .player-meta [data-player-role] { color: #e0bc86; }

.player-state {
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--gold-bright);
  font-size: clamp(9px, 2.5vw, 11px);
  font-weight: 750;
  line-height: 1;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

.player-state:empty { display: none; }

.player-state::before {
  content: "";
  width: 5px;
  height: 5px;
  flex: none;
  border-radius: 50%;
  background: currentColor;
  box-shadow: 0 0 8px currentColor;
}

.player.is-active .player-state::before { animation: turn-pulse 1.8s ease-in-out infinite; }

@keyframes turn-pulse {
  0%, 100% { opacity: 0.55; transform: scale(0.82); }
  50% { opacity: 1; transform: scale(1.18); }
}

.board-area {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  margin: clamp(4px, 1.1vh, 10px) 0;
}

#canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  outline: none;
}

.dock {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding-top: clamp(4px, 1.2vh, 10px);
}

.status { min-width: 0; }

.status-title {
  margin: 0;
  color: #f7f1e6;
  font-size: clamp(19px, 5vw, 23px);
  font-weight: 750;
  line-height: 1.22;
  letter-spacing: 0.015em;
}

.status-detail {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: clamp(11px, 2.9vw, 13px);
  font-weight: 500;
  line-height: 1.35;
}

#stage[data-status="won"] .status-title { color: var(--gold-bright); }
#stage[data-status="draw"] .status-title { color: #dce9f8; }

.restart {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  flex: none;
  min-height: clamp(46px, 12vw, 50px);
  padding: 0 clamp(14px, 4vw, 18px);
  color: var(--action-ink);
  font-size: clamp(13px, 3.4vw, 15px);
  font-weight: 800;
  letter-spacing: 0.02em;
  background: linear-gradient(180deg, #f2c67e, #dba254);
  border: 1px solid rgba(255, 232, 195, 0.62);
  border-radius: 14px;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.5);
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
}

.restart:hover { background: linear-gradient(180deg, #f8d294, #e3ab5e); }
.restart:active { transform: translateY(2px); box-shadow: 0 6px 14px rgba(0, 0, 0, 0.28), inset 0 1px 0 rgba(255, 255, 255, 0.36); }
.restart svg { flex: none; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

#error {
  position: absolute;
  z-index: 10;
  inset: auto 1rem 1rem;
  margin: 0;
  padding: 1rem;
  color: #fff;
  background: #751f35;
  border-radius: 12px;
  white-space: pre-wrap;
}

[hidden] { display: none !important; }

button:focus-visible,
select:focus-visible {
  outline: 3px solid #fff4c4;
  outline-offset: 3px;
}

.game-settings-panel {
  position: fixed;
  inset: max(var(--sc-safe-top, 0px), env(safe-area-inset-top)) max(var(--sc-safe-right, 0px), env(safe-area-inset-right)) max(var(--sc-safe-bottom, 0px), env(safe-area-inset-bottom)) max(var(--sc-safe-left, 0px), env(safe-area-inset-left));
  margin: auto;
  width: min(430px, calc(100vw - 2 * max(var(--sc-safe-left, 0px), var(--sc-safe-right, 0px)) - 24px));
  max-height: calc(100dvh - var(--sc-safe-top, 0px) - var(--sc-safe-bottom, 0px) - 24px);
  overflow: auto;
  padding: 22px;
  color: var(--ink);
  background: #122031;
  border: 1px solid rgba(230, 181, 110, 0.34);
  border-radius: 18px;
  box-shadow: 0 28px 80px rgba(0, 0, 0, 0.56);
  font-size: 16px;
}

.game-settings-panel::backdrop { background: rgba(2, 7, 13, 0.72); backdrop-filter: blur(3px); }
.game-settings-panel h2 { margin: 0 0 20px; font-size: 21px; font-weight: 750; letter-spacing: 0.02em; }

#language-control {
  display: flex;
  align-items: center;
  gap: 11px;
  color: var(--ink);
}

#language-control svg { flex: none; color: var(--gold); }
#language-selector { flex: 1; min-width: 0; }

#language-selector select {
  width: 100%;
  padding: 10px 12px;
  color: var(--ink);
  background: #1a2c42;
  border: 1px solid rgba(158, 184, 212, 0.3);
  border-radius: 9px;
}

.game-settings-close {
  display: block;
  min-height: 46px;
  margin: 22px 0 0 auto;
  padding-inline: 1.25em;
  color: var(--action-ink);
  font-weight: 800;
  background: var(--gold);
  border: 0;
  border-radius: 11px;
  cursor: pointer;
}

@media (hover: hover) {
  .game-settings-trigger:hover { background: linear-gradient(180deg, rgba(34, 56, 82, 0.98), rgba(16, 28, 44, 0.98)); }
}

@media (orientation: landscape) and (max-height: 620px) {
  body { display: block; }

  #stage {
    width: 100vw;
    max-width: none;
    height: 100dvh;
    box-shadow: none;
  }

  .shell {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(210px, 32vw);
    grid-template-rows: auto auto 1fr auto;
    grid-template-areas:
      "board header"
      "board players"
      "board gap"
      "board footer";
    column-gap: clamp(12px, 2.4vw, 22px);
    padding:
      calc(var(--game-safe-top, 0px) + 10px)
      max(clamp(12px, 2.4vw, 24px), var(--game-safe-right, 0px))
      calc(var(--game-safe-bottom, 0px) + 10px)
      max(clamp(12px, 2.4vw, 24px), var(--game-safe-left, 0px));
  }

  .topbar { grid-area: header; padding-top: 0; }
  h1 { font-size: clamp(26px, 4vw, 34px); }
  .eyebrow { letter-spacing: 0.1em; }

  .players {
    grid-area: players;
    grid-template-columns: minmax(0, 1fr);
    gap: 8px;
    margin-top: 12px;
  }

  .player { padding: 8px 10px; border-radius: 13px; }
  .swatch { width: 24px; height: 24px; }
  .player-state { font-size: 9px; }

  .board-area {
    grid-area: board;
    margin: 0;
    min-height: 0;
  }

  .dock {
    grid-area: footer;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    padding-top: 12px;
  }

  .restart { width: 100%; }
  .status-title { font-size: clamp(17px, 2.6vw, 21px); }
}

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