/* ==========================================================
   木紋五子棋 — wood-grain visual theme
   ========================================================== */

:root {
  --walnut-900: #1c120b;
  --walnut-800: #2a1a10;
  --walnut-700: #3a2416;
  --walnut-600: #4e311d;
  --walnut-500: #6a4427;
  --kaya-300: #e7c48a;
  --kaya-200: #f0d6a6;
  --kaya-100: #f8ead0;
  --brass-500: #b88a3e;
  --brass-400: #d2a656;
  --brass-300: #e9c97f;
  --ink: #2b1a0e;
  --paper: #f4e6c8;
  --muted: #c9ad85;
  --red: #c8392b;
  --win: #e7c35a;
  --lose: #e0705f;
  --draw: #b9c2b0;
  --radius: 12px;
  --board-size: min(90vw, 78vh, 640px);
  --font-cjk: "Noto Serif TC", "Source Han Serif TC", "Noto Serif CJK TC",
    "Songti TC", "PMingLiU", "MingLiU", "STSong", "Georgia", serif;
}

*,
*::before,
*::after { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  min-height: 100vh;
  font-family: var(--font-cjk);
  color: var(--paper);
  background-color: var(--walnut-800);
  background-image:
    radial-gradient(ellipse at 50% 0%, rgba(255, 200, 140, 0.10), transparent 60%),
    radial-gradient(ellipse at 50% 120%, rgba(0, 0, 0, 0.55), transparent 60%),
    repeating-linear-gradient(92deg,
      rgba(255, 255, 255, 0.018) 0px, rgba(255, 255, 255, 0.018) 2px,
      transparent 2px, transparent 7px),
    repeating-linear-gradient(88deg,
      rgba(0, 0, 0, 0.10) 0px, rgba(0, 0, 0, 0.10) 1px,
      transparent 1px, transparent 13px),
    repeating-linear-gradient(91deg,
      rgba(120, 70, 35, 0.16) 0px, transparent 40px, rgba(0, 0, 0, 0.12) 85px, transparent 130px),
    linear-gradient(180deg, #3a2415 0%, #2a190f 55%, #1d110a 100%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: 1080px;
  margin: 0 auto;
  padding: 20px 16px 28px;
}

/* ---------- Header ---------- */
.app-header {
  text-align: center;
  margin-bottom: 18px;
}

.title {
  display: inline-flex;
  align-items: center;
  gap: 14px;
  margin: 0;
  font-size: clamp(1.7rem, 4.2vw, 2.5rem);
  font-weight: 700;
  letter-spacing: 0.22em;
  color: var(--kaya-200);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.55), 0 0 18px rgba(233, 201, 127, 0.18);
}

.title-text { padding-left: 0.22em; }

.title-seal {
  display: inline-grid;
  place-items: center;
  width: 1.45em;
  height: 1.45em;
  font-size: 0.72em;
  letter-spacing: 0;
  color: #fbe9d0;
  background: linear-gradient(145deg, #c9442f, #8f2418);
  border-radius: 6px;
  box-shadow: inset 0 0 0 2px rgba(255, 220, 190, 0.35), 0 3px 8px rgba(0, 0, 0, 0.45);
  transform: rotate(-4deg);
  text-shadow: none;
}

.subtitle {
  margin: 8px 0 0;
  font-size: 0.92rem;
  letter-spacing: 0.2em;
  color: var(--muted);
  opacity: 0.85;
}

/* ---------- Layout ---------- */
.layout {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: 28px;
}

.board-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  flex: 0 0 auto;
}

/* Thick wooden 棋盤 frame */
.board-frame {
  position: relative;
  padding: clamp(8px, 1.8vw, 16px);
  border-radius: 10px;
  background:
    repeating-linear-gradient(90deg,
      rgba(0, 0, 0, 0.10) 0px, transparent 3px, rgba(255, 220, 170, 0.05) 6px, transparent 11px),
    linear-gradient(160deg, #8a5a32 0%, #6b4223 40%, #55331b 70%, #6e4526 100%);
  box-shadow:
    inset 0 2px 0 rgba(255, 225, 180, 0.35),
    inset 0 -3px 0 rgba(0, 0, 0, 0.45),
    inset 2px 0 0 rgba(255, 225, 180, 0.12),
    inset -2px 0 0 rgba(0, 0, 0, 0.3),
    0 2px 0 #2e1b0e,
    0 6px 0 #26160b,
    0 10px 0 #1e1108,
    0 22px 40px rgba(0, 0, 0, 0.65),
    0 40px 70px rgba(0, 0, 0, 0.35);
}

.board-frame::after {
  /* inner recessed lip around the playing surface */
  content: "";
  position: absolute;
  inset: clamp(8px, 1.8vw, 16px);
  border-radius: 3px;
  pointer-events: none;
  box-shadow:
    inset 0 0 0 1px rgba(40, 20, 8, 0.55),
    inset 0 2px 6px rgba(0, 0, 0, 0.35);
}

#board {
  display: block;
  width: var(--board-size);
  height: var(--board-size);
  aspect-ratio: 1 / 1;
  border-radius: 3px;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  background: #dcb479; /* fallback before the renderer paints */
}

/* ---------- Status line ---------- */
.board-info {
  width: var(--board-size);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  border-radius: 10px;
  background: linear-gradient(180deg, rgba(20, 11, 5, 0.55), rgba(20, 11, 5, 0.35));
  box-shadow: inset 0 1px 0 rgba(255, 220, 170, 0.08), inset 0 0 0 1px rgba(255, 220, 170, 0.07);
}

.status {
  font-size: 1.08rem;
  letter-spacing: 0.08em;
  color: var(--kaya-200);
  min-height: 1.5em;
  display: inline-flex;
  align-items: center;
  transition: color 0.3s ease, text-shadow 0.3s ease;
}

.status.thinking { color: var(--brass-300); }

.status.thinking::after {
  content: "";
  display: inline-block;
  width: 26px;
  height: 8px;
  margin-left: 10px;
  background:
    radial-gradient(circle, currentColor 3px, transparent 3.5px) 0 50% / 9px 8px no-repeat,
    radial-gradient(circle, currentColor 3px, transparent 3.5px) 9px 50% / 9px 8px no-repeat,
    radial-gradient(circle, currentColor 3px, transparent 3.5px) 18px 50% / 9px 8px no-repeat;
  animation: thinking-dots 1.2s ease-in-out infinite;
}

@keyframes thinking-dots {
  0%   { opacity: 0.25; transform: translateY(0); }
  30%  { opacity: 1;    transform: translateY(-2px); }
  60%  { opacity: 0.55; transform: translateY(0); }
  100% { opacity: 0.25; transform: translateY(0); }
}

.status.win {
  color: var(--win);
  text-shadow: 0 0 12px rgba(231, 195, 90, 0.45);
  font-weight: 700;
}
.status.lose { color: var(--lose); font-weight: 700; }
.status.draw { color: var(--draw); font-weight: 700; }

.move-info {
  flex: 0 0 auto;
  font-size: 0.92rem;
  color: var(--muted);
  letter-spacing: 0.06em;
}

#move-count {
  display: inline-block;
  min-width: 2ch;
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--kaya-200);
  font-weight: 700;
}

/* ---------- Side panel ---------- */
.side-panel {
  flex: 0 1 300px;
  min-width: 260px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.panel {
  position: relative;
  padding: 16px 18px 18px;
  border-radius: var(--radius);
  color: var(--ink);
  background:
    repeating-linear-gradient(178deg,
      rgba(120, 72, 30, 0.07) 0px, transparent 2px, rgba(120, 72, 30, 0.04) 5px, transparent 9px),
    linear-gradient(170deg, #ecd3a4 0%, #e0bd85 55%, #d4ab70 100%);
  box-shadow:
    inset 0 1px 0 rgba(255, 245, 225, 0.65),
    inset 0 -2px 0 rgba(110, 64, 28, 0.35),
    0 3px 0 #6b4324,
    0 4px 0 #4a2c16,
    0 12px 22px rgba(0, 0, 0, 0.45);
}

.panel-title {
  margin: 0 0 12px;
  padding-bottom: 8px;
  font-size: 1.05rem;
  letter-spacing: 0.3em;
  color: #5a3417;
  border-bottom: 1px solid rgba(90, 52, 23, 0.25);
  box-shadow: 0 1px 0 rgba(255, 245, 225, 0.5);
  display: flex;
  align-items: center;
  gap: 8px;
}

.panel-title::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #d65a45, #8f2418);
  box-shadow: 0 0 0 2px rgba(143, 36, 24, 0.18);
}

.field + .field { margin-top: 14px; }

.field-label {
  display: block;
  margin-bottom: 7px;
  font-size: 0.85rem;
  letter-spacing: 0.15em;
  color: #6e4524;
}

/* Radios as wooden toggle tiles */
.radio-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.radio { position: relative; cursor: pointer; }

.radio input {
  position: absolute;
  opacity: 0;
  inset: 0;
  margin: 0;
  cursor: pointer;
}

.radio-face {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 6px;
  font-size: 0.88rem;
  border-radius: 8px;
  color: #4a2a12;
  background: linear-gradient(180deg, rgba(255, 248, 232, 0.55), rgba(200, 160, 105, 0.35));
  box-shadow: inset 0 0 0 1px rgba(110, 64, 28, 0.28), 0 1px 0 rgba(255, 245, 225, 0.5);
  transition: background 0.2s ease, box-shadow 0.2s ease, color 0.2s ease, transform 0.1s ease;
  white-space: nowrap;
}

.radio:hover .radio-face {
  box-shadow: inset 0 0 0 1px rgba(110, 64, 28, 0.5), 0 1px 0 rgba(255, 245, 225, 0.5);
}

.radio input:checked + .radio-face {
  color: #fbecd2;
  background: linear-gradient(180deg, #7a4a26, #57321a);
  box-shadow:
    inset 0 2px 4px rgba(0, 0, 0, 0.45),
    inset 0 0 0 1px rgba(30, 15, 5, 0.6),
    0 1px 0 rgba(255, 245, 225, 0.5);
}

.radio input:focus-visible + .radio-face {
  outline: 2px solid var(--brass-400);
  outline-offset: 2px;
}

.radio input:disabled + .radio-face { opacity: 0.55; cursor: not-allowed; }

.mini-stone {
  display: inline-block;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  flex: 0 0 auto;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}
.mini-stone.black { background: radial-gradient(circle at 35% 30%, #6a6a6a, #111 60%, #000); }
.mini-stone.white { background: radial-gradient(circle at 35% 30%, #fff, #ece7dc 60%, #bdb4a4); }

/* Select */
.select-wrap { position: relative; }

.select-wrap::after {
  content: "";
  position: absolute;
  right: 14px;
  top: 50%;
  width: 7px;
  height: 7px;
  border-right: 2px solid #5a3417;
  border-bottom: 2px solid #5a3417;
  transform: translateY(-70%) rotate(45deg);
  pointer-events: none;
}

select {
  appearance: none;
  -webkit-appearance: none;
  width: 100%;
  padding: 9px 36px 9px 12px;
  font: inherit;
  font-size: 0.95rem;
  letter-spacing: 0.1em;
  color: #3b220f;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  background: linear-gradient(180deg, rgba(255, 248, 232, 0.7), rgba(215, 178, 125, 0.55));
  box-shadow: inset 0 0 0 1px rgba(110, 64, 28, 0.35), inset 0 1px 2px rgba(255, 255, 255, 0.4);
  transition: box-shadow 0.2s ease;
}

select:hover { box-shadow: inset 0 0 0 1px rgba(110, 64, 28, 0.6); }
select:focus-visible { outline: 2px solid var(--brass-400); outline-offset: 2px; }
select:disabled { opacity: 0.55; cursor: not-allowed; }
select option { color: #2b1a0e; background: #f3e2c2; }

/* ---------- Buttons (wood + brass) ---------- */
.btn-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.btn {
  position: relative;
  padding: 10px 14px;
  font: inherit;
  font-size: 0.95rem;
  letter-spacing: 0.14em;
  color: #fbecd2;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  background:
    repeating-linear-gradient(90deg,
      rgba(0, 0, 0, 0.07) 0px, transparent 2px, rgba(255, 220, 170, 0.05) 5px, transparent 8px),
    linear-gradient(180deg, #84522c 0%, #6a4022 55%, #573319 100%);
  box-shadow:
    inset 0 1px 0 rgba(255, 220, 170, 0.35),
    inset 0 0 0 1px rgba(30, 14, 4, 0.55),
    0 3px 0 #3a210f,
    0 5px 8px rgba(0, 0, 0, 0.35);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.45);
  transition: transform 0.08s ease, box-shadow 0.12s ease, filter 0.15s ease, opacity 0.2s ease;
  white-space: nowrap;
}

.btn:hover { filter: brightness(1.1); }

.btn:active {
  transform: translateY(2px);
  box-shadow:
    inset 0 1px 0 rgba(255, 220, 170, 0.25),
    inset 0 0 0 1px rgba(30, 14, 4, 0.55),
    0 1px 0 #3a210f,
    0 2px 4px rgba(0, 0, 0, 0.35);
}

.btn:focus-visible { outline: 2px solid var(--brass-300); outline-offset: 3px; }

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  filter: saturate(0.6);
  transform: none;
}

.btn-primary {
  color: #2e1a07;
  text-shadow: 0 1px 0 rgba(255, 240, 200, 0.5);
  background: linear-gradient(180deg, #f0d391 0%, #d4a650 45%, #b5863a 100%);
  box-shadow:
    inset 0 1px 0 rgba(255, 250, 225, 0.75),
    inset 0 0 0 1px rgba(100, 62, 14, 0.65),
    0 3px 0 #7a5518,
    0 5px 8px rgba(0, 0, 0, 0.35);
}

.btn-primary:active {
  box-shadow:
    inset 0 1px 0 rgba(255, 250, 225, 0.5),
    inset 0 0 0 1px rgba(100, 62, 14, 0.65),
    0 1px 0 #7a5518,
    0 2px 4px rgba(0, 0, 0, 0.35);
}

.btn-wide { grid-column: 1 / -1; }

#btn-sound.off {
  color: #d8c3a2;
  background: linear-gradient(180deg, #5d4636 0%, #4a372a 55%, #3c2d22 100%);
  filter: saturate(0.5);
}

.btn-small {
  padding: 7px 12px;
  font-size: 0.82rem;
}

.btn-ghost {
  display: block;
  margin: 14px 0 0 auto;
  color: #6b3f1d;
  text-shadow: none;
  background: transparent;
  box-shadow: inset 0 0 0 1px rgba(107, 63, 29, 0.45);
}

.btn-ghost:hover { background: rgba(107, 63, 29, 0.1); filter: none; }
.btn-ghost:active { transform: translateY(1px); box-shadow: inset 0 0 0 1px rgba(107, 63, 29, 0.6); }

/* ---------- Stats ---------- */
.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 9px 4px 8px;
  border-radius: 8px;
  background: rgba(255, 248, 232, 0.42);
  box-shadow: inset 0 0 0 1px rgba(110, 64, 28, 0.22), inset 0 -2px 0 rgba(110, 64, 28, 0.12);
}

.stat-label {
  font-size: 0.78rem;
  letter-spacing: 0.15em;
  color: #7a4f2c;
}

.stat-value {
  font-size: 1.35rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
  color: #3b220f;
}

.stat-win .stat-value { color: #8a5a10; }
.stat-lose .stat-value { color: #9c2f22; }
.stat-draw .stat-value { color: #4f5a48; }
.stat-rate .stat-value { color: #3b220f; font-size: 1.15rem; line-height: 1.4; }

/* ---------- Footer ---------- */
.app-footer {
  margin-top: 26px;
  text-align: center;
  font-size: 0.8rem;
  letter-spacing: 0.18em;
  color: var(--muted);
  opacity: 0.6;
}

/* ---------- Result overlay ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 16px;
  background: radial-gradient(ellipse at center, rgba(20, 10, 4, 0.55), rgba(8, 4, 1, 0.78));
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.35s ease, visibility 0s linear 0.35s;
}

.overlay.show {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transition: opacity 0.35s ease, visibility 0s linear 0s;
}

.result-card {
  position: relative;
  width: min(360px, 100%);
  padding: 30px 28px 26px;
  text-align: center;
  color: var(--ink);
  border-radius: 14px;
  background:
    repeating-linear-gradient(178deg,
      rgba(120, 72, 30, 0.07) 0px, transparent 2px, rgba(120, 72, 30, 0.04) 5px, transparent 9px),
    linear-gradient(170deg, #f2dcb2 0%, #e2c08a 60%, #d3a96d 100%);
  box-shadow:
    inset 0 0 0 1px rgba(255, 245, 225, 0.6),
    inset 0 0 0 6px rgba(110, 64, 28, 0.12),
    inset 0 0 0 7px rgba(110, 64, 28, 0.35),
    0 4px 0 #6b4324,
    0 7px 0 #3e240f,
    0 30px 60px rgba(0, 0, 0, 0.6);
  transform: translateY(16px) scale(0.9);
  opacity: 0;
  transition: transform 0.45s cubic-bezier(0.2, 1.3, 0.4, 1), opacity 0.3s ease;
}

.overlay.show .result-card {
  transform: translateY(0) scale(1);
  opacity: 1;
  transition-delay: 0.08s;
}

.result-ornament {
  width: 54px;
  height: 54px;
  margin: -58px auto 12px;
  border-radius: 50%;
  background: radial-gradient(circle at 34% 30%, #6c6c6c 0%, #1a1a1a 45%, #000 100%);
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.5), inset 0 -3px 6px rgba(255, 255, 255, 0.06);
}

.result-ornament::after {
  content: "";
  display: block;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  transform: translate(30px, 4px) scale(0.82);
  background: radial-gradient(circle at 34% 30%, #ffffff 0%, #efe9dd 50%, #bfb5a3 100%);
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.4);
}

.result-title {
  margin: 4px 0 10px;
  font-size: 1.8rem;
  letter-spacing: 0.3em;
  padding-left: 0.3em;
  color: #5a2a12;
  text-shadow: 0 1px 0 rgba(255, 245, 225, 0.7);
}

.result-text {
  margin: 0 0 22px;
  font-size: 1rem;
  line-height: 1.7;
  color: #5b3a1e;
  letter-spacing: 0.06em;
}

.result-card .btn { min-width: 150px; }

/* ---------- Responsive ---------- */
@media (max-width: 1000px) {
  .layout {
    flex-direction: column;
    align-items: center;
    gap: 22px;
  }

  .side-panel {
    flex: 0 0 auto;
    width: 100%;
    max-width: calc(var(--board-size) + 32px);
    min-width: 0;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  }
}

@media (max-width: 520px) {
  :root { --board-size: min(calc(100vw - 52px), 78vh); }
  .app { padding: 14px 10px 22px; }
  .board-info { padding: 8px 12px; }
  .status { font-size: 0.98rem; }
  .subtitle { font-size: 0.8rem; letter-spacing: 0.1em; }
  .side-panel { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  .status.thinking::after { animation: none; }
  .overlay, .result-card { transition: none; }
}
