:root {
  color-scheme: dark;
  --night: #0b2430;
  --night-deep: #061820;
  --rail: #173f4c;
  --rail-light: #315d68;
  --steel: #7d9aa2;
  --mist: #bfd0d1;
  --chalk: #f3f1e8;
  --chalk-dim: #dfe5df;
  --ink: #10252c;
  --x: #d9533f;
  --x-dark: #923626;
  --o: #c47a00;
  --o-dark: #744600;
  --focus: #f6ca65;
  --danger: #f0816f;
  --radius-small: 8px;
  --radius-medium: 14px;
  --motion-fast: 160ms;
  --motion-state: 220ms;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --z-skip: 30;
  --z-settings: 20;
  --z-win-line: 5;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--night);
}

body {
  min-width: 320px;
  min-height: 100vh;
  min-height: 100svh;
  margin: 0;
  background: var(--night);
  color: var(--chalk);
  font-family:
    "Noto Sans TC",
    "Noto Sans",
    "Microsoft JhengHei UI",
    "Microsoft JhengHei",
    "Yu Gothic",
    "Malgun Gothic",
    "Leelawadee UI",
    system-ui,
    -apple-system,
    sans-serif;
  text-rendering: optimizeLegibility;
}

button,
select,
summary,
a {
  font: inherit;
  touch-action: manipulation;
}

.control-icon,
.restart-icon {
  width: 1.25rem;
  height: 1.25rem;
  flex: 0 0 auto;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

button,
summary,
select {
  min-height: 44px;
}

.fa-help-btn {
  min-width: 44px;
  min-height: 44px;
}

button {
  color: inherit;
}

button,
summary {
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

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

.skip-link {
  position: fixed;
  top: 12px;
  left: 50%;
  z-index: var(--z-skip);
  min-height: 44px;
  padding: 11px 18px;
  border-radius: 999px;
  background: var(--focus);
  color: var(--ink);
  font-weight: 800;
  text-decoration: none;
  transform: translate(-50%, -160%);
  transition: transform var(--motion-fast) var(--ease-out);
}

.skip-link:focus {
  transform: translate(-50%, 0);
}

.app-shell {
  width: min(1180px, 100%);
  min-height: 100vh;
  min-height: 100svh;
  margin: 0 auto;
  padding: 18px clamp(16px, 3vw, 36px) 22px;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: clamp(16px, 2.8vh, 30px);
}

.topbar {
  position: relative;
  z-index: var(--z-settings);
  min-height: 56px;
  display: grid;
  grid-template-columns: minmax(120px, 1fr) auto minmax(120px, 1fr);
  align-items: center;
  gap: 16px;
}

.back-link {
  justify-self: start;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px 8px 4px;
  color: var(--mist);
  font-size: 0.92rem;
  font-weight: 720;
  text-decoration: none;
  transition:
    color var(--motion-fast) ease,
    transform var(--motion-fast) var(--ease-out);
}

.back-link > span[aria-hidden="true"] {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border-radius: 50%;
  background: var(--rail);
  color: var(--chalk);
  font-size: 1.15rem;
}

.back-link:hover {
  color: var(--chalk);
  transform: translateX(-2px);
}

.brand-lockup {
  justify-self: center;
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
}

.route-emblem {
  position: relative;
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(3, 8px);
  align-items: center;
  justify-content: center;
  gap: 3px;
}

.route-emblem::before {
  content: "";
  position: absolute;
  left: 5px;
  right: 5px;
  top: 50%;
  height: 4px;
  border-radius: 999px;
  background: var(--rail-light);
  transform: translateY(-50%);
}

.route-emblem i {
  position: relative;
  width: 8px;
  height: 8px;
  border: 2px solid var(--night);
  border-radius: 50%;
  background: var(--steel);
}

.route-emblem i:first-child {
  background: var(--x);
}

.route-emblem i:last-child {
  background: var(--o);
}

.route-label {
  display: block;
  margin-bottom: 1px;
  color: var(--steel);
  font-family: "Bahnschrift SemiCondensed", "DIN Alternate", "Arial Narrow", sans-serif;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  color: var(--chalk);
  font-size: 1.34rem;
  font-weight: 850;
  letter-spacing: -0.025em;
  line-height: 1.12;
  text-wrap: balance;
}

.settings {
  position: relative;
  justify-self: end;
}

.settings > summary {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: var(--radius-small);
  background: var(--rail);
  color: var(--chalk);
  font-size: 0.9rem;
  font-weight: 750;
  cursor: pointer;
  list-style: none;
}

.settings > summary::-webkit-details-marker {
  display: none;
}

.settings > summary:hover {
  background: var(--rail-light);
}

.settings-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: min(286px, calc(100vw - 32px));
  padding: 16px;
  border: 2px solid var(--rail-light);
  border-radius: var(--radius-medium);
  background: var(--night-deep);
  box-shadow: 0 8px 0 rgb(0 0 0 / 18%);
}

.settings-panel label {
  display: block;
  margin-bottom: 7px;
  color: var(--mist);
  font-size: 0.82rem;
  font-weight: 750;
}

.settings-panel select {
  width: 100%;
  padding: 9px 38px 9px 11px;
  border: 2px solid var(--rail-light);
  border-radius: var(--radius-small);
  background: var(--night);
  color: var(--chalk);
  font-size: 1rem;
}

.version-row {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--rail);
  display: flex;
  justify-content: space-between;
  color: var(--steel);
  font-family: "Bahnschrift SemiCondensed", "DIN Alternate", "Arial Narrow", monospace;
  font-size: 0.76rem;
}

.game-main {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(270px, 320px);
  align-items: center;
  gap: clamp(24px, 5vw, 72px);
}

.board-deck {
  min-width: 0;
  display: grid;
  justify-items: center;
  gap: 14px;
}

.board-heading {
  width: min(100%, 540px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
}

.turn-signal {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
}

.mini-token {
  position: relative;
  width: 46px;
  height: 46px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--rail);
}

.turn-signal[data-player="X"] .mini-token::before,
.turn-signal[data-player="X"] .mini-token::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 25px;
  height: 6px;
  border-radius: 999px;
  background: var(--x);
}

.turn-signal[data-player="X"] .mini-token::before {
  transform: translate(-50%, -50%) rotate(45deg);
}

.turn-signal[data-player="X"] .mini-token::after {
  transform: translate(-50%, -50%) rotate(-45deg);
}

.turn-signal[data-player="O"] .mini-token::before {
  content: "";
  position: absolute;
  inset: 10px;
  border: 6px solid var(--o);
  border-radius: 50%;
}

.turn-signal > p {
  min-width: 0;
}

.turn-signal p > span {
  display: block;
  margin-bottom: 2px;
  color: var(--steel);
  font-size: 0.75rem;
  font-weight: 720;
}

.turn-signal strong {
  display: block;
  overflow-wrap: anywhere;
  color: var(--chalk);
  font-size: 1.25rem;
  line-height: 1.18;
}

.turn-signal.is-result .mini-token {
  background: var(--chalk);
}

.turn-signal.is-result strong {
  color: var(--focus);
}

.controls-hint {
  max-width: 31ch;
  color: var(--mist);
  font-size: 0.78rem;
  line-height: 1.5;
  text-align: right;
  text-wrap: pretty;
}

.board-frame {
  width: min(100%, 540px);
  aspect-ratio: 1;
  padding: clamp(9px, 1.4vw, 14px);
  border: 4px solid var(--steel);
  border-radius: var(--radius-medium);
  background: var(--rail);
  box-shadow: 0 8px 0 var(--night-deep);
}

.board {
  position: relative;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  gap: clamp(4px, 0.8vw, 7px);
  overflow: hidden;
  border-radius: var(--radius-small);
  background: var(--rail-light);
}

.cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  padding: 0;
  overflow: hidden;
  border: 0;
  border-radius: 3px;
  background: var(--chalk);
  cursor: pointer;
  transition:
    background-color var(--motion-fast) ease,
    transform var(--motion-fast) var(--ease-out);
}

.cell:hover:not([aria-disabled="true"]) {
  background: var(--chalk-dim);
}

.cell:active:not([aria-disabled="true"]) {
  transform: scale(0.975);
}

.cell[aria-disabled="true"] {
  cursor: default;
}

.cell.is-winner {
  background: var(--chalk);
}

.cell.is-invalid {
  animation: reject-move var(--motion-fast) ease-out;
}

.mark {
  position: absolute;
  inset: 0;
  display: block;
  transform: scale(1);
  transform-origin: center;
}

.cell.is-new .mark {
  animation: place-token var(--motion-fast) var(--ease-out);
}

.cell[data-mark="X"] .mark::before,
.cell[data-mark="X"] .mark::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 58%;
  height: clamp(10px, 2.4vw, 19px);
  border-radius: 999px;
  background: var(--x);
  box-shadow: 0 4px 0 var(--x-dark);
}

.cell[data-mark="X"] .mark::before {
  transform: translate(-50%, -50%) rotate(45deg);
}

.cell[data-mark="X"] .mark::after {
  transform: translate(-50%, -50%) rotate(-45deg);
}

.cell[data-mark="O"] .mark::before {
  content: "";
  position: absolute;
  inset: 21%;
  border: clamp(10px, 2.5vw, 19px) solid var(--o);
  border-radius: 50%;
  box-shadow:
    inset 0 -4px 0 var(--o-dark),
    0 4px 0 var(--o-dark);
}

.board:not([aria-busy="true"])[data-next="X"] .cell[data-mark=""]:hover .mark::before,
.board:not([aria-busy="true"])[data-next="X"] .cell[data-mark=""]:hover .mark::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 48%;
  height: clamp(7px, 1.5vw, 11px);
  border-radius: 999px;
  background: rgb(217 83 63 / 22%);
}

.board:not([aria-busy="true"])[data-next="X"] .cell[data-mark=""]:hover .mark::before {
  transform: translate(-50%, -50%) rotate(45deg);
}

.board:not([aria-busy="true"])[data-next="X"] .cell[data-mark=""]:hover .mark::after {
  transform: translate(-50%, -50%) rotate(-45deg);
}

.board:not([aria-busy="true"])[data-next="O"] .cell[data-mark=""]:hover .mark::before {
  content: "";
  position: absolute;
  inset: 27%;
  border: clamp(7px, 1.5vw, 11px) solid rgb(242 181 68 / 28%);
  border-radius: 50%;
}

.win-line {
  --line-left: 16.6667%;
  --line-top: 16.6667%;
  --line-width: 66.6667%;
  --line-angle: 0deg;
  position: absolute;
  top: var(--line-top);
  left: var(--line-left);
  z-index: var(--z-win-line);
  width: var(--line-width);
  height: clamp(7px, 1.3vw, 11px);
  border-radius: 999px;
  background: var(--focus);
  box-shadow: 0 3px 0 rgb(82 55 8 / 45%);
  pointer-events: none;
  opacity: 0;
  transform: translateY(-50%) rotate(var(--line-angle)) scaleX(0);
  transform-origin: left center;
  transition:
    transform var(--motion-state) var(--ease-out),
    opacity 1ms linear;
}

.win-line[data-visible="true"] {
  opacity: 0.94;
  transform: translateY(-50%) rotate(var(--line-angle)) scaleX(1);
}

.app-subtitle {
  max-width: 46ch;
  color: var(--steel);
  font-size: 0.82rem;
  line-height: 1.45;
  text-align: center;
  text-wrap: pretty;
}

.control-deck {
  min-width: 0;
  align-self: center;
  display: grid;
  gap: 20px;
  padding: 4px 0;
}

.control-section,
.score-section,
.round-actions {
  padding-bottom: 20px;
  border-bottom: 1px solid var(--rail-light);
}

.control-section h2,
.score-heading h2 {
  margin: 0 0 9px;
  color: var(--mist);
  font-size: 0.76rem;
  font-weight: 780;
}

.segmented-control {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  border-radius: 11px;
  background: var(--rail);
}

.segmented-control button,
.difficulty-control button {
  min-width: 0;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--mist);
  font-size: 0.82rem;
  font-weight: 760;
  cursor: pointer;
  transition:
    background-color var(--motion-fast) ease,
    color var(--motion-fast) ease,
    transform var(--motion-fast) var(--ease-out);
}

.segmented-control button:hover,
.difficulty-control button:hover {
  color: var(--chalk);
}

.segmented-control button[aria-pressed="true"] {
  background: var(--chalk);
  color: var(--ink);
}

.difficulty-control {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 7px;
}

.difficulty-control button {
  border: 2px solid var(--rail-light);
}

.difficulty-control button[aria-pressed="true"] {
  border-color: var(--focus);
  background: var(--focus);
  color: var(--ink);
}

.score-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.score-heading > span {
  color: var(--steel);
  font-family: "Bahnschrift SemiCondensed", "DIN Alternate", "Arial Narrow", sans-serif;
  font-size: 0.75rem;
  font-weight: 700;
}

.score-strip {
  margin: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

.score-strip > div {
  min-width: 0;
  padding: 4px 12px;
  text-align: center;
}

.score-strip > div + div {
  border-left: 1px solid var(--rail-light);
}

.score-strip dt {
  overflow-wrap: break-word;
  color: var(--steel);
  font-size: 0.75rem;
  font-weight: 720;
  line-height: 1.3;
}

.score-strip dd {
  margin: 3px 0 0;
  color: var(--chalk);
  font-family: "Bahnschrift SemiCondensed", "DIN Alternate", "Arial Narrow", monospace;
  font-size: 2.3rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.score-x dd {
  color: var(--danger);
}

.score-o dd {
  color: var(--o);
}

.score-value-changed {
  animation: score-tick var(--motion-state) var(--ease-out);
}

.round-actions {
  padding-bottom: 0;
  border-bottom: 0;
}

.restart-button {
  width: 100%;
  min-height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  border: 0;
  border-radius: 10px;
  background: var(--o);
  color: var(--ink);
  font-weight: 850;
  cursor: pointer;
  box-shadow: 0 4px 0 var(--o-dark);
  transition:
    background-color var(--motion-fast) ease,
    transform var(--motion-fast) var(--ease-out),
    box-shadow var(--motion-fast) var(--ease-out);
}

.restart-button:hover {
  background: var(--focus);
}

.restart-button:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 var(--o-dark);
}

.round-actions p {
  margin-top: 10px;
  color: var(--steel);
  font-size: 0.75rem;
  line-height: 1.45;
  text-align: center;
  text-wrap: pretty;
}

:focus-visible {
  outline: 4px solid var(--focus);
  outline-offset: 3px;
}

.cell:focus-visible {
  z-index: calc(var(--z-win-line) + 1);
  outline-color: var(--night);
  outline-offset: -5px;
}

@keyframes place-token {
  0% {
    opacity: 0.25;
    transform: scale(0.72);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes reject-move {
  0%,
  100% {
    transform: translateX(0);
  }
  35% {
    transform: translateX(-4px);
  }
  70% {
    transform: translateX(4px);
  }
}

@keyframes score-tick {
  0% {
    transform: translateY(0);
  }
  45% {
    transform: translateY(-5px);
  }
  100% {
    transform: translateY(0);
  }
}

@media (max-width: 760px) {
  .app-shell {
    padding: 12px 14px 20px;
    gap: 14px;
  }

  .topbar {
    grid-template-columns: minmax(44px, 1fr) auto minmax(44px, 1fr);
    gap: 7px;
  }

  .back-link {
    width: 44px;
    padding: 5px;
  }

  .back-link > span:not([aria-hidden="true"]) {
    display: none;
  }

  .settings > summary {
    width: 44px;
    padding: 7px;
    justify-content: center;
  }

  .settings > summary > span:last-child {
    display: none;
  }

  .route-emblem {
    width: 34px;
    height: 38px;
  }

  h1 {
    font-size: 1.16rem;
  }

  .game-main {
    grid-template-columns: minmax(0, 1fr);
    gap: 18px;
  }

  .board-heading,
  .board-frame {
    width: min(100%, 420px);
  }

  .board-frame {
    max-height: min(52vh, 420px);
  }

  .control-deck {
    width: min(100%, 420px);
    margin: 0 auto;
    grid-template-columns: 1fr 1fr;
    align-items: start;
    gap: 14px 16px;
  }

  .control-section,
  .score-section,
  .round-actions {
    padding-bottom: 14px;
  }

  .score-section,
  .round-actions {
    grid-column: 1 / -1;
  }

  .difficulty-section {
    grid-column: 2;
  }

  .control-section:first-child {
    grid-column: 1;
  }

  .score-strip > div {
    padding-inline: 8px;
  }

  .score-strip dd {
    font-size: 1.9rem;
  }
}

@media (max-width: 470px) {
  .fa-help-btn {
    top: 76px !important;
    right: 14px !important;
    bottom: auto !important;
  }

  .route-label {
    display: none;
  }

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

  .brand-lockup {
    gap: 5px;
  }

  .route-emblem {
    width: 30px;
  }

  .board-heading {
    justify-content: center;
  }

  .board-frame {
    width: min(100%, 356px);
    max-height: none;
  }

  .app-subtitle {
    font-size: 0.76rem;
  }

  .control-deck {
    grid-template-columns: 1fr;
  }

  .control-section,
  .difficulty-section,
  .score-section,
  .round-actions {
    grid-column: 1;
  }
}

@media (min-width: 520px) and (max-height: 540px) {
  .fa-help-btn {
    top: 62px !important;
    right: auto !important;
    bottom: auto !important;
    left: 14px !important;
  }

  .app-shell {
    min-height: 100vh;
    min-height: 100svh;
    padding: 8px 14px 10px;
    gap: 5px;
  }

  .topbar {
    min-height: 44px;
  }

  .route-label,
  .app-subtitle,
  .round-actions p {
    display: none;
  }

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

  .route-emblem {
    width: 34px;
    height: 34px;
  }

  .game-main {
    grid-template-columns: minmax(220px, 1fr) minmax(210px, 286px);
    gap: clamp(14px, 3vw, 32px);
  }

  .board-deck {
    gap: 7px;
  }

  .board-heading,
  .board-frame {
    width: min(calc(100svh - 120px), 48vw, 330px);
  }

  .board-frame {
    max-height: none;
  }

  .board-heading {
    justify-content: center;
  }

  .mini-token {
    width: 38px;
    height: 38px;
  }

  .turn-signal strong {
    font-size: 1rem;
  }

  .board-frame {
    border-width: 3px;
    padding: 7px;
    box-shadow: 0 5px 0 var(--night-deep);
  }

  .control-deck {
    width: 100%;
    grid-template-columns: minmax(0, 1fr);
    gap: 9px;
  }

  .control-section,
  .difficulty-section,
  .score-section,
  .round-actions {
    grid-column: 1;
  }

  .control-section,
  .score-section {
    padding-bottom: 9px;
  }

  .control-section h2,
  .score-heading h2 {
    margin-bottom: 5px;
  }

  .segmented-control button,
  .difficulty-control button {
    min-height: 44px;
  }

  .score-strip dd {
    font-size: 1.55rem;
  }

  .restart-button {
    min-height: 44px;
  }
}

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