:root {
  --bg: #1a1208;
  --bg-elevated: #2a1e12;
  --bg-panel: #23180f;
  --border: #4a3420;
  --text: #f3e6d0;
  --text-muted: #b89a78;
  --accent: #d4a056;
  --accent-hover: #e8b86a;
  --danger: #e07060;
  --success: #6dba8a;
  --glow: rgba(212, 160, 86, 0.35);
  --board-wood: #c9a06a;
  --board-wood-dark: #a07848;
  --board-frame: #5c3d22;
  --line: #3a2614;
  --hoshi: #2a1a0c;
  --stone-black: #1a1208;
  --stone-white: #f5f0e6;
  --last-move: rgba(220, 60, 40, 0.85);
  --hover: rgba(40, 100, 200, 0.35);
  --radius: 14px;
  --font: "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei",
    system-ui, sans-serif;
  --board-size: min(92vw, 560px, 70vh);
  --tap-min: 44px;
}

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

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  min-height: 100dvh;
  background: radial-gradient(ellipse at top, #2e2114 0%, var(--bg) 55%);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

body {
  display: flex;
  justify-content: center;
  padding: 16px 12px 24px;
}

.app {
  width: min(1200px, 100%);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.header {
  text-align: center;
}

.title {
  margin: 0;
  font-size: clamp(1.5rem, 4vw, 2rem);
  font-weight: 700;
  letter-spacing: 0.12em;
}

.subtitle {
  margin: 4px 0 0;
  color: var(--text-muted);
  font-size: 0.9rem;
  letter-spacing: 0.25em;
  text-transform: uppercase;
}

/* —— Desktop: side-by-side —— */
.layout {
  display: grid;
  grid-template-columns:
    minmax(180px, 240px)
    minmax(280px, var(--board-size))
    minmax(180px, 240px);
  gap: 16px;
  align-items: start;
  justify-content: center;
}

.panel {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
  min-height: 0;
  min-width: 0;
}

.player-card {
  background: var(--bg-elevated);
  border-radius: 12px;
  padding: 12px;
  border: 1px solid var(--border);
}

.player-card.you {
  border-color: rgba(91, 140, 255, 0.45);
}

.player-card.active-turn {
  box-shadow: 0 0 0 2px var(--accent), 0 0 16px var(--glow);
}

.player-name {
  font-weight: 600;
  margin-bottom: 8px;
  font-size: 0.95rem;
}

.timer-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.88rem;
  margin-top: 4px;
}

.timer-label {
  color: var(--text-muted);
}

.timer-value {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
}

.timer-value.low {
  color: var(--danger);
  animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
  50% { opacity: 0.55; }
}

.capt-row {
  margin-top: 8px;
  font-size: 0.88rem;
  color: var(--text-muted);
}

.capt-row strong {
  color: var(--text);
  font-size: 1.05rem;
}

.stat-block,
.settings {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-label {
  font-size: 0.78rem;
  color: var(--text-muted);
  letter-spacing: 0.04em;
}

.stat-value {
  font-weight: 600;
  font-size: 0.95rem;
}

.select {
  appearance: none;
  background: var(--bg-elevated)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath fill='%23b89a78' d='M0 0l6 8 6-8z'/%3E%3C/svg%3E")
    no-repeat right 10px center;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 28px 10px 12px;
  font: inherit;
  font-size: 0.9rem;
  min-height: var(--tap-min);
  width: 100%;
  cursor: pointer;
}

.select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.select:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  border-radius: 10px;
  padding: 12px 14px;
  font: inherit;
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  min-height: var(--tap-min);
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.btn:hover:not(:disabled) {
  background: #352518;
  border-color: var(--accent);
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(180deg, #d4a056, #b8843c);
  color: #1a1208;
  border-color: #e0b56a;
}

.btn-primary:hover:not(:disabled) {
  background: linear-gradient(180deg, #e8b86a, #c99448);
  border-color: #f0c878;
}

.btn-danger {
  border-color: rgba(224, 112, 96, 0.5);
  color: #ffb0a4;
}

.btn-danger:hover:not(:disabled) {
  background: rgba(224, 112, 96, 0.18);
  border-color: var(--danger);
}

.board-section {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 0;
}

.board-wrap {
  width: var(--board-size);
  max-width: 100%;
  aspect-ratio: 1;
  position: relative;
  border-radius: 8px;
  padding: 10px;
  background: linear-gradient(145deg, #6b4423, #4a2e16);
  box-shadow:
    0 12px 40px rgba(0, 0, 0, 0.45),
    inset 0 1px 0 rgba(255, 220, 160, 0.25);
}

.board {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 4px;
  cursor: pointer;
  touch-action: none;
  background: var(--board-wood);
  box-shadow: inset 0 0 30px rgba(80, 40, 10, 0.25);
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(20, 12, 6, 0.72);
  border-radius: 8px;
  z-index: 5;
  padding: 16px;
}

.overlay.hidden {
  display: none;
}

.overlay-card {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px 20px;
  max-width: 340px;
  width: 100%;
  text-align: center;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
}

.overlay-card h2 {
  margin: 0 0 8px;
  font-size: 1.35rem;
  letter-spacing: 0.08em;
}

.overlay-card p {
  margin: 0 0 16px;
  color: var(--text-muted);
  font-size: 0.95rem;
  line-height: 1.5;
  white-space: pre-wrap;
}

.overlay-score {
  text-align: left;
  background: var(--bg-elevated);
  border-radius: 10px;
  padding: 12px;
  margin-bottom: 16px;
  font-size: 0.9rem;
  line-height: 1.55;
  border: 1px solid var(--border);
}

.overlay-score.hidden {
  display: none;
}

.move-list-wrap {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
  min-height: 0;
}

.move-list {
  list-style: none;
  margin: 0;
  padding: 8px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-y: auto;
  max-height: min(360px, 42vh);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  line-height: 1.55;
}

.move-list li {
  padding: 2px 4px;
  border-radius: 4px;
}

.move-list li:nth-child(odd) {
  background: rgba(255, 255, 255, 0.03);
}

.move-list li.latest {
  background: rgba(212, 160, 86, 0.18);
  color: var(--accent-hover);
}

.hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  line-height: 1.4;
  min-height: 1.2em;
}

.help {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.82rem;
  line-height: 1.5;
}

.help p {
  margin: 0;
}

.settings.locked .select {
  pointer-events: none;
}

/* —— Tablet / narrow desktop —— */
@media (max-width: 960px) and (min-width: 769px) {
  .layout {
    grid-template-columns: minmax(160px, 200px) minmax(260px, var(--board-size));
    grid-template-areas:
      "side board"
      "log board";
  }

  .panel-side:first-of-type {
    grid-area: side;
  }

  .board-section {
    grid-area: board;
  }

  .panel-side:last-of-type {
    grid-area: log;
  }

  --board-size: min(58vw, 520px, 68vh);
}

/* —— Mobile: stacked vertical —— */
@media (max-width: 768px) {
  body {
    padding: 8px 8px 16px;
  }

  .app {
    gap: 10px;
    width: 100%;
  }

  .header {
    padding: 4px 0;
  }

  .title {
    font-size: 1.35rem;
    letter-spacing: 0.1em;
  }

  .subtitle {
    font-size: 0.75rem;
  }

  .layout {
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: stretch;
  }

  /* Board first / on top */
  .board-section {
    order: 1;
    width: 100%;
  }

  .board-wrap {
    width: min(100%, 100vw - 16px);
    max-width: 100%;
    margin: 0 auto;
    padding: 6px;
    --board-size: min(100vw - 16px, 92dvh - 220px, 520px);
    width: var(--board-size);
  }

  /* Compact controls below board */
  .panel-side:first-of-type {
    order: 2;
  }

  .panel-side:last-of-type {
    order: 3;
  }

  .panel {
    padding: 12px;
    gap: 10px;
  }

  /* Horizontal player strip on mobile */
  .panel-side:first-of-type {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  .panel-side:first-of-type > .player-card {
    grid-column: 1 / -1;
  }

  /* Hide opponent card in left panel on mobile — shown in right? 
     Better: show both players in a row via CSS reorder of name cards.
     Simpler approach: keep left panel as settings+actions, inject opp into flow. */

  .stat-block {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .settings {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .actions {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
  }

  .actions .btn {
    padding: 12px 8px;
    font-size: 0.88rem;
  }

  .move-list {
    max-height: 160px;
  }

  .help {
    font-size: 0.75rem;
    padding: 0 4px;
  }

  .overlay-card {
    max-width: min(340px, 92vw);
    padding: 18px 14px;
  }
}

/* Very small phones */
@media (max-width: 400px) {
  .settings {
    grid-template-columns: 1fr;
  }

  .actions {
    grid-template-columns: 1fr;
  }

  .stat-block {
    grid-template-columns: 1fr 1fr;
  }
}

/* Coarse pointer / touch: larger tap affordances */
@media (pointer: coarse) {
  .btn,
  .select {
    min-height: 48px;
  }

  .move-list {
    font-size: 0.9rem;
  }
}

/* Landscape phone: board beside compact controls */
@media (max-width: 900px) and (max-height: 500px) and (orientation: landscape) {
  body {
    padding: 6px;
  }

  .header {
    display: none;
  }

  .help {
    display: none;
  }

  .layout {
    display: grid;
    grid-template-columns: minmax(200px, 42vh) 1fr;
    grid-template-rows: auto auto;
    gap: 8px;
  }

  .board-section {
    order: unset;
    grid-row: 1 / 3;
    grid-column: 1;
  }

  .board-wrap {
    width: min(42vh, 42vw);
    padding: 4px;
  }

  .panel-side:first-of-type {
    order: unset;
    grid-column: 2;
    grid-row: 1;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-start;
    max-height: none;
  }

  .panel-side:first-of-type .settings {
    display: none;
  }

  .panel-side:last-of-type {
    order: unset;
    grid-column: 2;
    grid-row: 2;
  }

  .move-list {
    max-height: 28vh;
  }

  .actions {
    display: flex;
    flex-direction: row;
    flex: 1;
  }
}

/* Mobile-only / desktop-only helpers */
.mobile-only {
  display: none;
}

.players-bar {
  gap: 8px;
}

.player-card.compact {
  flex: 1;
  padding: 10px;
  min-width: 0;
}

.player-card.compact .player-name {
  font-size: 0.88rem;
  margin-bottom: 4px;
}

@media (max-width: 768px) {
  .mobile-only {
    display: flex;
  }

  /* Hide duplicate full player cards inside side panels on mobile */
  .panel-side > .player-card {
    display: none;
  }

  .players-bar {
    order: 2;
    width: 100%;
  }

  .board-section {
    order: 1;
  }

  .panel-side:first-of-type {
    order: 3;
  }

  .panel-side:last-of-type {
    order: 4;
  }
}

@media (min-width: 769px) {
  .mobile-only {
    display: none !important;
  }
}


.thinking-badge {
  position: absolute;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  z-index: 4;
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(20, 12, 6, 0.82);
  border: 1px solid var(--accent);
  color: var(--accent);
  font-size: 0.9rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  pointer-events: none;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  white-space: nowrap;
}

.thinking-badge.hidden {
  display: none;
}
