:root {
  --bg: #1e2128;
  --panel: #2a2e38;
  --text: #e8e9ec;
  --muted: #9aa0ab;
  --accent: #d4a24e;
  --light-sq: #eeeed2;
  --dark-sq: #769656;
  --border: #3a3f4b;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

header { padding: 16px 20px; text-align: center; }

h1 { font-size: 24px; font-weight: 700; color: var(--accent); }

main { max-width: 900px; margin: 0 auto; padding: 16px; }

.panel { background: var(--panel); border-radius: 12px; padding: 20px; }

.hidden { display: none; }

h2 { font-size: 18px; margin-bottom: 12px; color: var(--accent); }

.setup-row { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }

.setup-row label { min-width: 90px; color: var(--muted); }

select, button {
  font-size: 16px;
  padding: 10px 16px;
  border-radius: 8px;
  border: 1px solid var(--muted);
  background: var(--bg);
  color: var(--text);
  min-height: 44px;
}

button { background: var(--accent); color: #1a1a1a; font-weight: 600; border: none; cursor: pointer; }

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

#status {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--accent);
  min-height: 28px;
}

.connection-status {
  font-size: 14px;
  color: var(--muted);
  min-height: 20px;
  margin-bottom: 8px;
}

.player-names {
  font-weight: 600;
  color: var(--text);
  margin-bottom: 12px;
}

input[type="text"] {
  font-size: 16px;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--muted);
  background: var(--bg);
  color: var(--text);
  min-height: 44px;
  flex: 1;
}

.game-layout { display: flex; gap: 24px; align-items: flex-start; flex-wrap: wrap; }

.board {
  --sq: min(11vw, 64px);
  display: grid;
  grid-template-columns: repeat(8, var(--sq));
  grid-template-rows: repeat(8, var(--sq));
  border: 3px solid var(--muted);
  border-radius: 4px;
  max-width: 100%;
  touch-action: manipulation;
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
}

.square.light { background: var(--light-sq); }
.square.dark { background: var(--dark-sq); }
.square.selected { outline: 3px solid var(--accent); outline-offset: -3px; }
.square.legal { cursor: pointer; }

.square.legal::after {
  content: '';
  width: 30%;
  height: 30%;
  border-radius: 50%;
  background: rgba(212, 162, 78, 0.5);
  position: absolute;
}

.piece { font-size: calc(var(--sq, 64px) * 0.75); line-height: 1; z-index: 1; }

.white-piece { color: #fff; text-shadow: 0 1px 3px rgba(0,0,0,0.7); }
.black-piece { color: #111; text-shadow: 0 1px 2px rgba(255,255,255,0.3); }

.side { flex: 1; min-width: 220px; display: flex; flex-direction: column; gap: 12px; }

.timers { display: flex; gap: 12px; }

.timer {
  flex: 1;
  background: var(--bg);
  border-radius: 8px;
  padding: 8px 12px;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  text-align: center;
}

.captured { font-size: 20px; color: var(--muted); min-height: 28px; }

#move-list {
  background: var(--bg);
  border-radius: 8px;
  padding: 12px 12px 12px 32px;
  max-height: 260px;
  overflow-y: auto;
  font-family: ui-monospace, monospace;
  font-size: 14px;
}

#move-list li.current { color: var(--accent); font-weight: 700; }

.turn-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 8px;
  background: var(--panel);
  width: fit-content;
  margin-bottom: 8px;
  min-height: 24px;
}
.turn-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid var(--border);
}
.turn-dot.w { background: #ffffff; border-color: #999; }
.turn-dot.b { background: #1a1a1a; border-color: #555; }
.turn-check { color: #e0b45a; }

.timer.active {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.material-adv { color: var(--accent); font-size: 14px; margin-left: 8px; }

.controls { display: flex; gap: 12px; }

.menu-title { font-size: 28px; font-weight: 700; color: var(--accent); text-align: center; }
.menu-subtitle { color: var(--muted); font-size: 18px; text-align: center; margin: 4px 0 20px; }
#main-menu .menu-buttons { display: flex; flex-direction: column; gap: 8px; }
.menu-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 16px;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  min-height: 44px;
  border: none;
  cursor: pointer;
}
.menu-btn.primary { background: var(--accent); color: #1a1a1a; }
.menu-btn.secondary { background: var(--panel); color: var(--text); border: 1px solid var(--border); }
.menu-btn.accent { background: var(--panel); color: var(--accent); border: 1px solid var(--accent); }
.menu-divider { border: none; border-top: 1px solid var(--border); margin: 16px 0; }
.menu-footer { color: var(--muted); font-size: 14px; text-align: center; }

/* Board themes (5 styles) */
body.theme-classic { --light-sq: #eeeed2; --dark-sq: #769656; }
body.theme-ocean { --light-sq: #d6eaf8; --dark-sq: #2e86c1; }
body.theme-wood { --light-sq: #f0d9b5; --dark-sq: #b58863; }
body.theme-midnight { --light-sq: #9fa8b8; --dark-sq: #3a4256; }
body.theme-slate { --light-sq: #c8cdd6; --dark-sq: #5d6b7e; }

/* Piece designs */
body.pieces-classic .piece { font-family: system-ui, -apple-system, 'Segoe UI', sans-serif; }
body.pieces-serif .piece {
  font-family: Georgia, 'Times New Roman', serif;
  font-weight: 600;
}
body.pieces-serif .white-piece { color: #f5f1e6; text-shadow: 0 2px 4px rgba(0,0,0,0.8); }
body.pieces-serif .black-piece { color: #2b2b2b; text-shadow: 0 1px 3px rgba(255,255,255,0.35); }
body.pieces-outline .piece { font-family: system-ui, sans-serif; }
body.pieces-outline .white-piece {
  color: #ffffff;
  -webkit-text-stroke: 2px #333;
  text-shadow: none;
}
body.pieces-outline .black-piece {
  color: #1a1a1a;
  -webkit-text-stroke: 2px #f0f0f0;
  text-shadow: none;
}

/* Light / dark UI palettes */
body.ui-dark { --bg: #1e2128; --panel: #2a2e38; --text: #e8e9ec; --muted: #9aa0ab; --border: #3a3f4b; }
body.ui-light { --bg: #f4f4ef; --panel: #ffffff; --text: #24262b; --muted: #6b7280; --border: #d8dbe0; }
body.ui-light .menu-btn.secondary { background: #f0f0ea; }
body.ui-light .menu-btn.primary, body.ui-light button { color: #1a1a1a; }
body.ui-light #move-list, body.ui-light .timer, body.ui-light .ach-card, body.ui-light select, body.ui-light button { background: #ffffff; }
body.ui-light #move-list, body.ui-light .ach-card { background: #fafaf7; }
body.ui-light button { background: var(--accent); }

/* New sections */
.section-info { color: var(--muted); margin-bottom: 12px; min-height: 24px; }

.puzzle-feedback { min-height: 28px; margin: 12px 0; font-weight: 600; }
.puzzle-feedback.good { color: #7bc47b; }
.puzzle-feedback.bad { color: #e07a7a; }

.ach-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; margin-bottom: 16px; }
.ach-card { background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; }
.ach-card.unlocked { border-color: var(--accent); }
.ach-card .ach-name { font-weight: 600; color: var(--accent); }
.ach-card.locked .ach-name { color: var(--muted); }
.ach-card .ach-desc { color: var(--muted); font-size: 14px; }
.ach-card .ach-date { font-size: 12px; color: var(--muted); }

#analysis-board, #puzzle-board { margin-bottom: 16px; }

.square.last-move { box-shadow: inset 0 0 0 3px rgba(212, 162, 78, 0.55); }

#toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--accent);
  color: #1a1a1a;
  padding: 12px 20px;
  border-radius: 10px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease-out;
  z-index: 50;
}
#toast.show { opacity: 1; }

.stats-table { width: 100%; border-collapse: collapse; margin-bottom: 12px; }
.stats-table td { padding: 8px 12px; border-bottom: 1px solid var(--border); }
.stats-table td:first-child { color: var(--muted); }
.recent-results { color: var(--muted); font-size: 14px; }

/* Intermediate squeeze: two-column game layout stays but tightens */
@media (max-width: 900px) {
  main { padding: 12px; }
  .game-layout { gap: 16px; }
  .board { --sq: min(10.5vw, 64px); }
}

/* Small phones: stack and free horizontal room.
   Board width = 8 * --sq + 6px border. With main padding 12px and
   panel padding 14px: 8 * ((100vw - 24px - 28px - 6px - 32px) / 8) + 6px
   = 100vw - 78px < 100vw, so it never overflows, even at 320px. */
/* Replays & Share */
.replay-layout { display: flex; gap: 20px; align-items: flex-start; flex-wrap: wrap; }
.replay-side { flex: 1; min-width: 260px; display: flex; flex-direction: column; gap: 12px; }
.replay-list { display: flex; flex-direction: column; gap: 8px; max-height: 320px; overflow-y: auto; }
.replay-row {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.replay-info { min-width: 0; }
.replay-title { font-size: 14px; font-weight: 600; }
.replay-code { font-family: ui-monospace, monospace; font-size: 12px; color: var(--muted); word-break: break-all; }
.replay-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.replay-actions button { font-size: 13px; padding: 6px 10px; min-height: 36px; }
.replay-import { display: flex; gap: 8px; }
.replay-import input { flex: 1; font-size: 14px; padding: 10px; border-radius: 8px; border: 1px solid var(--muted); background: var(--bg); color: var(--text); min-height: 44px; }
.replay-error { color: #e07a7a; }
.replay-list .section-info { padding: 4px 0; }

@media (max-width: 640px) {
  .panel { padding: 14px; }
  .game-layout { flex-direction: column; align-items: center; }
  .board {
    --sq: calc((100vw - 24px - 28px - 6px - 32px) / 8);
    grid-template-columns: repeat(8, var(--sq));
    grid-template-rows: repeat(8, var(--sq));
  }
  .side { width: 100%; }
  .setup-row { flex-wrap: wrap; }
  .setup-row label { min-width: 0; }
  .controls { flex-wrap: wrap; }
  .ach-grid { grid-template-columns: 1fr; }
  .stats-table td { padding: 6px 8px; }
  #toast {
    width: max-content;
    max-width: calc(100vw - 32px);
    white-space: normal;
    text-align: center;
  }
}

/* ---------- Tournament ---------- */
.tour-rounds { display: flex; flex-direction: column; gap: 14px; margin: 12px 0; }
.tour-round-title { font-size: 13px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin-bottom: 6px; }
.tour-matches { display: flex; flex-direction: column; gap: 8px; }
.tour-match { display: flex; align-items: center; gap: 10px; background: var(--bg); border: 1px solid var(--muted); border-radius: 8px; padding: 8px 12px; flex-wrap: wrap; }
.tour-slot { flex: 1; min-width: 100px; font-weight: 600; }
.tour-slot.winner { color: var(--accent); }
.tour-slot.loser { color: var(--muted); text-decoration: line-through; }
.tour-slot.pending { color: var(--muted); font-style: italic; font-weight: 400; }
.tour-vs { color: var(--muted); font-size: 12px; }
.tour-badge { font-size: 11px; padding: 2px 8px; border-radius: 10px; border: 1px solid var(--muted); color: var(--muted); }
.tour-badge.live { color: var(--accent); border-color: var(--accent); }
.tour-badge.done { color: var(--text); border-color: var(--text); }
.tour-winner { font-size: 28px; font-weight: 700; color: var(--accent); margin: 12px 0; text-align: center; }
body.ui-light .tour-match { background: #fafaf7; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
