:root {
  color-scheme: light;
  --ink: #1f2933;
  --muted: #6b7280;
  --paper: #f7f2e7;
  --panel: #ffffff;
  --line: #6f4f26;
  --grid-line-width: 2px;
  --board-border: #5f4624;
  --accent: #1d7a6f;
  --accent-dark: #145a52;
  --danger: #9f2d2d;
  --shadow: 0 24px 70px rgba(28, 36, 46, 0.16);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(135deg, rgba(29, 122, 111, 0.16), transparent 34%),
    linear-gradient(315deg, rgba(159, 45, 45, 0.12), transparent 36%),
    #eef2f0;
}

body.theme-forest {
  --ink: #17231c;
  --muted: #516256;
  --paper: #e4ead8;
  --panel: #fbfdf8;
  --line: #3f5f32;
  --board-border: #31472f;
  --accent: #2e6f4e;
  --accent-dark: #1f5038;
  --danger: #9a3333;
  background:
    linear-gradient(135deg, rgba(46, 111, 78, 0.2), transparent 34%),
    linear-gradient(315deg, rgba(147, 173, 101, 0.18), transparent 36%),
    #edf3e9;
}

body.theme-midnight {
  --ink: #eef2ff;
  --muted: #b8c1d8;
  --paper: #d3d8e8;
  --panel: #172033;
  --line: #2f3f5f;
  --board-border: #d8dee9;
  --accent: #4f8fba;
  --accent-dark: #79aee0;
  --danger: #ff8f8f;
  --shadow: 0 24px 70px rgba(4, 9, 20, 0.42);
  background:
    linear-gradient(135deg, rgba(79, 143, 186, 0.22), transparent 34%),
    linear-gradient(315deg, rgba(255, 143, 143, 0.15), transparent 36%),
    #0d1423;
}

body.high-contrast {
  --ink: #050505;
  --muted: #1f2933;
  --paper: #fff1ad;
  --panel: #ffffff;
  --line: #050505;
  --grid-line-width: 3px;
  --board-border: #050505;
  --accent: #004f46;
  --accent-dark: #003832;
  --danger: #8b0000;
  --shadow: 0 0 0 3px #050505;
  background: #ffffff;
}

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

button {
  min-height: 42px;
  border: 1px solid rgba(31, 41, 51, 0.14);
  border-radius: 8px;
  padding: 0 16px;
  color: #ffffff;
  background: var(--accent);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

button:hover {
  background: var(--accent-dark);
}

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

button[aria-disabled="true"] {
  cursor: not-allowed;
}

.app-shell {
  width: min(1180px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 32px 0;
}

.game-surface {
  border: 1px solid rgba(31, 41, 51, 0.1);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.78);
  box-shadow: var(--shadow);
  overflow: hidden;
}

body.theme-midnight .game-surface {
  border-color: rgba(216, 222, 233, 0.2);
  background: rgba(15, 23, 42, 0.88);
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 22px 24px;
  border-bottom: 1px solid rgba(31, 41, 51, 0.1);
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent-dark);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(1.8rem, 4vw, 3rem);
  line-height: 1;
}

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

.play-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 24px;
  padding: 24px;
}

.board-wrap {
  --board-size: min(70vw, 700px);
  --coord-size: 24px;
  display: grid;
  gap: 10px;
  place-items: center;
  min-width: 0;
}

.board-frame {
  display: grid;
  grid-template-columns: var(--coord-size) var(--board-size) var(--coord-size);
  grid-template-rows: var(--coord-size) var(--board-size) var(--coord-size);
  gap: 4px;
  align-items: stretch;
  justify-items: stretch;
}

.coord-strip {
  display: grid;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
  line-height: 1;
  user-select: none;
}

.coord-strip span {
  display: grid;
  min-width: 0;
  min-height: 0;
  place-items: center;
}

.coord-strip-top,
.coord-strip-bottom {
  grid-column: 2;
  grid-template-columns: repeat(15, minmax(0, 1fr));
}

.coord-strip-top {
  grid-row: 1;
}

.coord-strip-bottom {
  grid-row: 3;
}

.coord-strip-left,
.coord-strip-right {
  grid-row: 2;
  grid-template-rows: repeat(15, minmax(0, 1fr));
}

.coord-strip-left {
  grid-column: 1;
}

.coord-strip-right {
  grid-column: 3;
}

.board {
  grid-column: 2;
  grid-row: 2;
  display: grid;
  width: var(--board-size);
  height: var(--board-size);
  max-width: 100%;
  aspect-ratio: 1;
  grid-template-columns: repeat(15, 1fr);
  grid-template-rows: repeat(15, 1fr);
  border: 4px solid var(--board-border);
  border-radius: 6px;
  background:
    linear-gradient(
      to right,
      transparent calc(50% - var(--grid-line-width) / 2),
      var(--line) calc(50% - var(--grid-line-width) / 2),
      var(--line) calc(50% + var(--grid-line-width) / 2),
      transparent calc(50% + var(--grid-line-width) / 2)
    ),
    linear-gradient(
      to bottom,
      transparent calc(50% - var(--grid-line-width) / 2),
      var(--line) calc(50% - var(--grid-line-width) / 2),
      var(--line) calc(50% + var(--grid-line-width) / 2),
      transparent calc(50% + var(--grid-line-width) / 2)
    ),
    var(--paper);
  background-size: calc(100% / 15) calc(100% / 15);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.35);
}

.selected-cell-status {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 38px;
  border: 1px solid rgba(31, 41, 51, 0.1);
  border-radius: 8px;
  padding: 6px 14px;
  background: var(--panel);
}

.selected-cell-status strong {
  min-width: 3ch;
  color: var(--accent-dark);
  font-size: 1.05rem;
  font-variant-numeric: tabular-nums;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  min-height: 0;
  border: 0;
  border-radius: 0;
  padding: 0;
  background: transparent;
}

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

.cell:hover,
.cell:focus-visible {
  outline: 2px solid rgba(29, 122, 111, 0.55);
  outline-offset: -2px;
  background: rgba(29, 122, 111, 0.12);
}

.cell::after {
  content: "";
  width: 74%;
  height: 74%;
  border-radius: 50%;
  transform: scale(0);
  transition: transform 140ms ease;
}

.cell.black::after {
  transform: scale(1);
  background:
    radial-gradient(circle at 34% 30%, #5b6471 0 8%, transparent 9%),
    radial-gradient(circle at 50% 52%, #111827, #030712);
  box-shadow: 0 8px 14px rgba(0, 0, 0, 0.35);
}

.cell.white::after {
  transform: scale(1);
  background:
    radial-gradient(circle at 34% 30%, #ffffff 0 9%, transparent 10%),
    radial-gradient(circle at 50% 52%, #f8fafc, #cbd5e1);
  box-shadow:
    0 8px 14px rgba(0, 0, 0, 0.18),
    inset 0 0 0 1px rgba(31, 41, 51, 0.2);
}

.move-number {
  position: absolute;
  z-index: 2;
  display: grid;
  width: 74%;
  height: 74%;
  place-items: center;
  border-radius: 50%;
  font-size: clamp(0.55rem, 1.4vw, 0.92rem);
  font-weight: 900;
  line-height: 1;
  pointer-events: none;
}

.cell.black .move-number {
  color: #ffffff;
  text-shadow: 0 1px 2px #000000;
}

.cell.white .move-number {
  color: #111827;
  text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
}

.cell.last-move::before {
  content: "";
  position: absolute;
  z-index: 1;
  width: 28%;
  height: 28%;
  border: 2px solid #facc15;
  border-radius: 50%;
  background: rgba(250, 204, 21, 0.28);
  pointer-events: none;
}

.cell.hint::before {
  content: "";
  position: absolute;
  z-index: 1;
  width: 42%;
  height: 42%;
  border: 3px dashed var(--accent-dark);
  border-radius: 50%;
  background: rgba(29, 122, 111, 0.16);
  pointer-events: none;
}

.cell.win {
  background: rgba(159, 45, 45, 0.16);
}

body.high-contrast .game-surface,
body.high-contrast .status-block,
body.high-contrast .score-grid,
body.high-contrast .history-box,
body.high-contrast .recent-match,
body.high-contrast .share-output,
body.high-contrast .setup-panel,
body.high-contrast .selected-cell-status {
  border-color: #050505;
}

body.high-contrast .game-surface {
  background: var(--panel);
}

body.high-contrast .cell:hover,
body.high-contrast .cell:focus-visible {
  outline: 4px solid #004f46;
  outline-offset: -4px;
  background: rgba(0, 79, 70, 0.18);
}

body.high-contrast .cell.black::after {
  background: #000000;
  box-shadow: 0 0 0 2px #000000;
}

body.high-contrast .cell.white::after {
  background: #ffffff;
  box-shadow:
    0 0 0 2px #000000,
    inset 0 0 0 1px #ffffff;
}

body.high-contrast .cell.black .move-number {
  color: #ffffff;
  text-shadow: none;
}

body.high-contrast .cell.white .move-number {
  color: #000000;
  text-shadow: none;
}

body.high-contrast .cell.last-move::before {
  border-color: #000000;
  background: #ffff00;
}

body.high-contrast .cell.hint::before {
  border-color: #004f46;
  background: #ffffff;
}

body.high-contrast .cell.win {
  background: #ffd4d4;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.status-block,
.score-grid,
.history-box,
.recent-match,
.share-output,
.setup-panel {
  border: 1px solid rgba(31, 41, 51, 0.1);
  border-radius: 8px;
  background: var(--panel);
}

.status-block {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
}

.label {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
  text-transform: uppercase;
}

.setup-panel {
  display: grid;
  gap: 14px;
  padding: 16px;
}

.mode-row,
.language-field,
.theme-field,
.name-field,
.difficulty-field,
.computer-side-field,
.opening-field {
  display: grid;
  gap: 8px;
}

.segmented-control {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 4px;
  border: 1px solid rgba(31, 41, 51, 0.12);
  border-radius: 8px;
  padding: 4px;
  background: #f1f5f4;
}

.segmented-control label {
  min-width: 0;
}

.segmented-control input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.segmented-control span {
  display: grid;
  min-height: 34px;
  place-items: center;
  border-radius: 6px;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 800;
  cursor: pointer;
}

.segmented-control input:checked + span {
  color: #ffffff;
  background: var(--accent);
}

.segmented-control input:focus-visible + span {
  outline: 2px solid rgba(29, 122, 111, 0.55);
  outline-offset: 2px;
}

.name-field input,
.language-field select,
.theme-field select,
.difficulty-field select,
.computer-side-field select,
.opening-field select {
  width: 100%;
  min-height: 40px;
  border: 1px solid rgba(31, 41, 51, 0.14);
  border-radius: 8px;
  padding: 0 12px;
  color: var(--ink);
  background: #ffffff;
  font: inherit;
}

body.theme-midnight .name-field input,
body.theme-midnight .language-field select,
body.theme-midnight .theme-field select,
body.theme-midnight .difficulty-field select,
body.theme-midnight .computer-side-field select,
body.theme-midnight .opening-field select {
  background: #0f172a;
  border-color: rgba(216, 222, 233, 0.28);
}

.language-field select,
.theme-field select,
.difficulty-field select,
.computer-side-field select,
.opening-field select {
  cursor: pointer;
}

.language-field select:disabled,
.theme-field select:disabled,
.difficulty-field select:disabled,
.computer-side-field select:disabled,
.opening-field select:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

.name-field input:focus,
.language-field select:focus,
.theme-field select:focus,
.difficulty-field select:focus,
.computer-side-field select:focus,
.opening-field select:focus {
  outline: 2px solid rgba(29, 122, 111, 0.55);
  outline-offset: 2px;
}

.contrast-toggle,
.timer-toggle,
.move-numbers-toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  font-weight: 800;
}

.contrast-toggle input,
.timer-toggle input,
.move-numbers-toggle input {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
}

#elapsed-time-label {
  font-variant-numeric: tabular-nums;
}

.score-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  overflow: hidden;
}

.score-grid > div {
  display: grid;
  gap: 8px;
  padding: 16px;
}

.score-grid > div + div {
  border-left: 1px solid rgba(31, 41, 51, 0.1);
}

.stone-sample {
  width: 22px;
  height: 22px;
  border-radius: 50%;
}

.stone-sample.black {
  background: #111827;
}

.stone-sample.white {
  border: 1px solid rgba(31, 41, 51, 0.2);
  background: #f8fafc;
}

.history-box {
  min-height: 260px;
  overflow: hidden;
}

.history-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid rgba(31, 41, 51, 0.1);
}

.history-head > div {
  display: flex;
  align-items: baseline;
  gap: 10px;
}

.history-head button {
  min-height: 34px;
  padding: 0 12px;
  white-space: nowrap;
}

#move-history {
  max-height: 340px;
  margin: 0;
  padding: 12px 16px 12px 36px;
  overflow: auto;
}

#move-history li {
  padding: 3px 0;
  color: var(--muted);
}

.history-move {
  width: 100%;
  min-height: 32px;
  border: 0;
  border-radius: 6px;
  padding: 4px 8px;
  color: var(--muted);
  background: transparent;
  font: inherit;
  font-weight: 700;
  text-align: left;
  touch-action: manipulation;
}

.history-move:hover,
.history-move:focus-visible {
  color: #ffffff;
  background: var(--accent);
  outline: 2px solid rgba(29, 122, 111, 0.55);
  outline-offset: 1px;
}

body.high-contrast .history-move:hover,
body.high-contrast .history-move:focus-visible {
  color: Canvas;
  background: CanvasText;
  outline: 3px solid #004f46;
}

.recent-match {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px;
}

.recent-match > div {
  display: grid;
  gap: 6px;
}

.recent-match time {
  color: var(--muted);
  font-size: 0.88rem;
}

.recent-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.recent-match button {
  min-height: 36px;
  padding: 0 12px;
}

.share-output {
  display: grid;
  gap: 10px;
  padding: 14px 16px 16px;
}

.share-output[hidden] {
  display: none;
}

.share-output-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.share-output button {
  min-height: 34px;
  padding: 0 12px;
}

.share-output textarea {
  width: 100%;
  min-height: 150px;
  resize: vertical;
  border: 1px solid rgba(31, 41, 51, 0.14);
  border-radius: 8px;
  padding: 10px;
  color: var(--ink);
  background: #ffffff;
  font: 0.82rem ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  line-height: 1.45;
  white-space: pre;
}

body.theme-midnight .share-output textarea {
  border-color: rgba(216, 222, 233, 0.28);
  background: #0f172a;
}

@media (prefers-contrast: more), (forced-colors: active) {
  body:not(.standard-contrast) {
    --ink: CanvasText;
    --muted: CanvasText;
    --paper: Canvas;
    --panel: Canvas;
    --line: CanvasText;
    --board-border: CanvasText;
    --accent: Highlight;
    --accent-dark: Highlight;
    --danger: Mark;
    background: Canvas;
  }
}

@media (prefers-reduced-motion: reduce) {
  .cell::after {
    transition: none;
  }
}

@media (max-width: 900px) {
  .play-layout {
    grid-template-columns: 1fr;
  }

  .board-wrap {
    --board-size: min(calc(92vw - 64px), 660px);
  }

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

  .history-box {
    grid-column: 1 / -1;
  }

  .setup-panel {
    grid-column: 1 / -1;
  }

  .recent-match {
    grid-column: 1 / -1;
  }

  .share-output {
    grid-column: 1 / -1;
  }
}

@media (max-width: 560px) {
  .app-shell {
    width: min(100vw - 18px, 520px);
    padding: 9px 0;
  }

  .top-bar {
    align-items: flex-start;
    flex-direction: column;
    padding: 18px;
  }

  .actions {
    width: 100%;
  }

  .actions button {
    flex: 1 1 calc(50% - 6px);
    padding: 0 10px;
  }

  .play-layout {
    gap: 16px;
    padding: 14px;
  }

  .board-wrap {
    --board-size: calc(100vw - 86px);
    --coord-size: 20px;
  }

  .coord-strip {
    font-size: 0.68rem;
  }

  .side-panel {
    grid-template-columns: 1fr;
  }
}
