:root {
  --bg-deep: #08080c;
  --bg: #0f0f14;
  --surface: rgba(22, 22, 30, 0.72);
  --surface-solid: #16161e;
  --surface-raised: rgba(32, 32, 42, 0.9);
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.14);
  --gold: #c9a962;
  --gold-soft: rgba(201, 169, 98, 0.15);
  --gold-glow: rgba(201, 169, 98, 0.35);
  --accent: #d4af6a;
  --text: #f4f2ed;
  --text-soft: #a8a6b0;
  --text-muted: #6b6978;
  --light-square: #e8e4d9;
  --dark-square: #7a8f6e;
  --board-frame: #2a2418;
  --highlight: rgba(201, 169, 98, 0.42);
  --selected: rgba(120, 180, 255, 0.45);
  --check: rgba(220, 70, 70, 0.55);
  --danger: #c45c5c;
  --ok: #5cb88a;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
  --shadow-board: 0 24px 64px rgba(0, 0, 0, 0.55), 0 0 0 1px rgba(255, 255, 255, 0.06);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

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

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: 'Outfit', system-ui, -apple-system, sans-serif;
  background: var(--bg-deep);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse 80% 50% at 50% -20%, rgba(201, 169, 98, 0.12), transparent),
    radial-gradient(ellipse 60% 40% at 100% 100%, rgba(80, 90, 120, 0.08), transparent),
    linear-gradient(180deg, var(--bg-deep) 0%, var(--bg) 100%);
  pointer-events: none;
  z-index: -1;
}

/* ── Header ── */
.header {
  position: sticky;
  top: 0;
  z-index: 100;
  padding: calc(0.85rem + var(--safe-top)) 1rem 0.85rem;
  background: rgba(8, 8, 12, 0.82);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
}

.header-inner {
  max-width: 1280px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem 1.25rem;
}

.brand h1 {
  margin: 0;
  font-family: 'Cormorant Garamond', Georgia, serif;
  font-size: clamp(1.35rem, 4vw, 1.75rem);
  font-weight: 600;
  letter-spacing: 0.02em;
  background: linear-gradient(135deg, var(--text) 0%, var(--gold) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.tagline {
  margin: 0.15rem 0 0;
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 400;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.header-status {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.65rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 500;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  color: var(--text-soft);
}

.status-pill::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
}

.status-pill.badge-ok::before { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.status-pill.badge-error::before { background: var(--danger); }
.status-pill.badge-pending::before { background: var(--gold); animation: pulse 1.5s ease infinite; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.auth-bar {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.65rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
  margin-top: 0.25rem;
}

.auth-guest, .auth-user {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  flex-wrap: wrap;
  justify-content: center;
}

.guest-badge {
  background: var(--gold-soft);
  color: var(--gold);
  padding: 0.2rem 0.65rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  border: 1px solid rgba(201, 169, 98, 0.25);
}

.guest-note, .guest-note-block {
  color: var(--text-muted);
  font-size: 0.82rem;
}

.auth-login-note {
  font-size: 0.72rem;
  color: var(--text-muted);
  font-style: italic;
}

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

.user-avatar {
  border-radius: 50%;
  border: 2px solid var(--gold);
  box-shadow: 0 0 12px var(--gold-glow);
}

/* ── Layout ── */
.layout {
  display: grid;
  grid-template-areas:
    "board"
    "controls"
    "side";
  gap: 1rem;
  max-width: 1280px;
  margin: 0 auto;
  padding: 1rem;
  padding-bottom: calc(1rem + var(--safe-bottom));
}

.board-panel { grid-area: board; }
.control-panel { grid-area: controls; }
.side-panel { grid-area: side; }

@media (min-width: 768px) {
  .layout {
    grid-template-columns: minmax(260px, 300px) 1fr;
    grid-template-areas:
      "controls board"
      "side board";
    gap: 1.25rem;
    padding: 1.25rem;
  }
}

@media (min-width: 1100px) {
  .layout {
    grid-template-columns: minmax(260px, 290px) minmax(360px, 1fr) minmax(220px, 260px);
    grid-template-areas: "controls board side";
    align-items: start;
  }
}

/* ── Panels ── */
.panel {
  background: var(--surface);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: var(--radius);
  padding: 1.15rem;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.panel h2 {
  margin: 0 0 0.85rem;
  font-family: 'Cormorant Garamond', Georgia, serif;
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--gold);
  letter-spacing: 0.03em;
}

.board-panel {
  padding: 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.board-panel h2 {
  align-self: flex-start;
  width: 100%;
}

.board-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  align-self: stretch;
  width: 100%;
  margin-bottom: 0.85rem;
}

.board-panel-head h2 {
  margin: 0;
  width: auto;
}

.board-panel-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.board-view,
.board-quality {
  margin: 0 !important;
  flex-shrink: 0;
}

.board-view select,
.board-quality select {
  min-height: 36px;
  padding: 0.35rem 0.65rem;
  font-size: 0.78rem;
  width: auto;
  min-width: 8rem;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
}

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

.hidden { display: none !important; }

/* ── Controls ── */
.mode-menu {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.setup { margin-top: 0.25rem; }

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 0.85rem;
  font-size: 0.82rem;
  color: var(--text-soft);
  font-weight: 500;
}

.field input,
.field select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.85rem;
  font-size: 1rem;
  font-family: inherit;
  min-height: 44px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.field input:focus,
.field select:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 0 0 3px var(--gold-soft);
}

.btn {
  background: linear-gradient(135deg, var(--gold) 0%, #a88b4a 100%);
  color: #1a1510;
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.7rem 1.1rem;
  min-height: 44px;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 600;
  font-family: inherit;
  width: 100%;
  transition: transform 0.15s, box-shadow 0.15s, filter 0.15s;
  box-shadow: 0 4px 16px rgba(201, 169, 98, 0.25);
}

.btn:hover { filter: brightness(1.08); transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }

.btn-mode { letter-spacing: 0.02em; }

.btn-secondary {
  background: var(--surface-raised);
  color: var(--text);
  border: 1px solid var(--border-strong);
  box-shadow: none;
}

.btn-secondary:hover { background: rgba(255, 255, 255, 0.06); filter: none; }

.btn-danger {
  background: linear-gradient(135deg, #c45c5c 0%, #9a3f3f 100%);
  color: #fff;
  box-shadow: 0 4px 16px rgba(196, 92, 92, 0.25);
}

.btn-sm {
  width: auto;
  padding: 0.4rem 0.9rem;
  min-height: 36px;
  font-size: 0.82rem;
}

.btn-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.btn-row .btn:last-child:nth-child(odd) {
  grid-column: 1 / -1;
}

@media (min-width: 400px) {
  .btn-row {
    grid-template-columns: repeat(3, 1fr);
  }
  .btn-row .btn:last-child:nth-child(odd) {
    grid-column: auto;
  }
}

.divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1.1rem 0;
}

dl { margin: 0 0 0.75rem; }
dt { color: var(--text-muted); font-size: 0.75rem; margin-top: 0.55rem; text-transform: uppercase; letter-spacing: 0.06em; }
dd { margin: 0.1rem 0 0; font-size: 0.9rem; }

.game-info dd { font-weight: 500; color: var(--text); }

.game-info code {
  font-size: 0.78rem;
  color: var(--text-muted);
  word-break: break-all;
}

.badge {
  display: inline-block;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
}
.badge-pending { background: rgba(201, 169, 98, 0.2); color: var(--gold); }
.badge-ok { background: rgba(92, 184, 138, 0.2); color: var(--ok); }
.badge-error { background: rgba(196, 92, 92, 0.2); color: var(--danger); }

.elo-desc {
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 0.85rem;
  margin-bottom: 0.85rem;
  font-size: 0.84rem;
  line-height: 1.55;
  color: var(--text-soft);
  border: 1px solid var(--border);
}

.elo-ref { color: var(--gold); font-size: 0.78rem; opacity: 0.85; }

.saved-yes { color: var(--ok); }
.saved-guest, .saved-no { color: var(--text-muted); }

.match-status {
  color: var(--text-soft);
  font-size: 0.88rem;
  margin: 0.65rem 0 0;
  min-height: 1.25rem;
}

/* ── Board ── */
.board-wrap {
  width: 100%;
  max-width: min(100%, 600px);
  margin: 0 auto;
}

@media (min-width: 768px) {
  .board-wrap {
    max-width: min(100%, 680px);
  }
}

@media (min-width: 1100px) {
  .board-wrap {
    max-width: min(100%, 760px);
  }
}

.board-frame {
  padding: clamp(6px, 1.2vw, 10px);
  background: linear-gradient(145deg, #3d3428 0%, var(--board-frame) 50%, #1f1a12 100%);
  border-radius: 12px;
  box-shadow: var(--shadow-board);
  position: relative;
}

.board-frame::before {
  content: '';
  position: absolute;
  inset: 4px;
  border-radius: 8px;
  border: 1px solid rgba(201, 169, 98, 0.12);
  pointer-events: none;
}

.board-inner {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr);
  gap: clamp(6px, 1.5vw, 10px);
  align-items: stretch;
  position: relative;
  z-index: 1;
}

/* ── Eval bar ── */
.eval-column {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  min-width: 28px;
  align-self: stretch;
}

.eval-bar {
  position: relative;
  flex: 1;
  width: 100%;
  min-height: 0;
  height: 100%;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid rgba(201, 169, 98, 0.28);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.08),
    0 2px 8px rgba(0, 0, 0, 0.35);
  display: flex;
  flex-direction: column;
  background: #1a1814;
}

.eval-zone {
  width: 100%;
  transition: height 0.45s cubic-bezier(0.4, 0, 0.2, 1);
  flex-shrink: 0;
}

.eval-zone-white {
  background: linear-gradient(180deg, #f5f0e6 0%, #d4cbb8 100%);
  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.08);
}

.eval-zone-black {
  background: linear-gradient(180deg, #3a3630 0%, #12100e 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06);
}

.eval-text {
  font-size: 0.62rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  padding: 2px 4px;
  border-radius: 4px;
  min-width: 2rem;
  text-align: center;
  color: var(--text-muted);
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--border);
}

.eval-text.ahead-white {
  color: #e8e0d0;
  background: rgba(245, 240, 230, 0.12);
  border-color: rgba(245, 240, 230, 0.2);
}

.eval-text.ahead-black {
  color: #b0a898;
  background: rgba(0, 0, 0, 0.35);
  border-color: rgba(255, 255, 255, 0.08);
}

.eval-text.equal {
  color: var(--gold);
  opacity: 0.85;
}

@media (max-width: 380px) {
  .board-inner {
    grid-template-columns: 22px minmax(0, 1fr);
    gap: 5px;
  }
  .eval-column { min-width: 22px; }
  .eval-text { font-size: 0.55rem; min-width: 1.75rem; }
}

.board-grid,
.board-ranks,
.board-files,
.board-main { display: none; }

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  aspect-ratio: 1;
  width: 100%;
  border-radius: 4px;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.board-2d {
  background: var(--dark-square);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.2);
}

.board-2d .square {
  border: none;
  padding: 0;
  margin: 0;
  font: inherit;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
}

.board-3d-host,
.board-3d {
  display: block;
  position: relative;
  aspect-ratio: 1;
  width: 100%;
  min-height: 240px;
  border-radius: 4px;
  overflow: hidden;
  touch-action: none;
  background: radial-gradient(ellipse at 50% 80%, rgba(0, 0, 0, 0.35), transparent 70%);
}

.board-3d-host canvas,
.board-3d canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

.board-loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(8, 8, 12, 0.72);
  color: var(--gold);
  font-size: 0.85rem;
  font-weight: 500;
  z-index: 2;
  pointer-events: none;
}

.board-loading.hidden {
  display: none;
}

.square {
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  aspect-ratio: 1;
}

.square.light {
  background: var(--light-square);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.15);
}

.square.dark {
  background: var(--dark-square);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}

.square.locked { cursor: default; }

.square .coord {
  position: absolute;
  font-size: 0.55rem;
  font-weight: 700;
  line-height: 1;
  pointer-events: none;
  opacity: 0.55;
  z-index: 1;
}

.square.light .coord { color: var(--dark-square); }
.square.dark .coord { color: var(--light-square); }

.coord-rank { top: 2px; left: 3px; }
.coord-file { bottom: 2px; right: 3px; }

.piece {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 88%;
  height: 88%;
  position: relative;
  z-index: 2;
  transition: transform 0.12s ease;
  pointer-events: none;
}

.piece svg {
  width: 100%;
  height: 100%;
  display: block;
}

.piece-white svg {
  fill: #fff;
  stroke: #1a1a1a;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.35));
}

.piece-black svg {
  fill: #1c1c1c;
  stroke: #888;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.5));
}

.square:not(.locked):active .piece {
  transform: scale(1.08);
}

.square.selected::after {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--selected);
  pointer-events: none;
  z-index: 0;
}

.square.highlight::after {
  content: '';
  position: absolute;
  width: 34%;
  height: 34%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: var(--highlight);
  box-shadow: 0 0 12px var(--gold-glow);
  pointer-events: none;
  z-index: 1;
}

.square.highlight:has(.piece)::after {
  width: 88%;
  height: 88%;
  border-radius: 50%;
  background: transparent;
  border: 4px solid var(--highlight);
  box-shadow: inset 0 0 8px var(--gold-glow);
}

.square.in-check::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(circle, var(--check) 0%, transparent 70%);
  pointer-events: none;
  z-index: 0;
  animation: check-pulse 1.2s ease infinite;
}

@keyframes check-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.65; }
}

.turn-info {
  text-align: center;
  margin: 1rem 0 0.25rem;
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--text-soft);
}

.game-status {
  text-align: center;
  color: var(--gold);
  font-weight: 500;
  min-height: 1.35rem;
  font-size: 0.95rem;
}

/* ── Promotion modal ── */
.promo-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.72);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 1rem;
  padding-bottom: calc(1rem + var(--safe-bottom));
}

.promotion-dialog {
  background: var(--surface-solid);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 1.25rem;
  text-align: center;
  box-shadow: var(--shadow-board);
  max-width: 340px;
  width: 100%;
}

.promotion-dialog p {
  margin: 0 0 0.75rem;
  font-weight: 500;
  color: var(--text-soft);
}

.promo-btns {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
}

.promo-btns button {
  aspect-ratio: 1;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 0.5rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 56px;
  transition: border-color 0.15s, background 0.15s, transform 0.15s;
}

.promo-btns button:hover {
  border-color: var(--gold);
  background: var(--gold-soft);
  transform: scale(1.05);
}

.promo-btns button svg {
  width: 80%;
  height: 80%;
  fill: #fff;
  stroke: #1a1a1a;
}

.promo-btns button.promo-dark svg {
  fill: #1c1c1c;
  stroke: #999;
}

.promo-cancel { margin-top: 0.85rem; width: auto; min-width: 120px; }

/* ── Move list & history ── */
.history-section {
  margin-bottom: 1rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 180px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.history-list li {
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.8rem;
  line-height: 1.45;
}

.history-empty { color: var(--text-muted); }

.hist-date { color: var(--text-muted); margin-right: 0.35rem; }
.hist-mode { color: var(--text); }
.hist-result { color: var(--gold); margin-left: 0.35rem; }

.move-list {
  margin: 0;
  padding: 0;
  list-style: none;
  max-height: min(50vh, 420px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: grid;
  grid-template-columns: 2.5rem 1fr 1fr;
  gap: 0.15rem 0.5rem;
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}

.move-list li {
  display: grid;
  grid-template-columns: 2.5rem 1fr 1fr;
  gap: 0 0.5rem;
  padding: 0.15rem 0;
  border-bottom: 1px solid transparent;
}

.move-list li:hover {
  border-bottom-color: var(--border);
}

.move-num {
  color: var(--text-muted);
  text-align: right;
  padding: 0.2rem 0;
}

.move-list .mv-w,
.move-list .mv-b {
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
  color: var(--text-soft);
}

.move-list .mv-w:hover,
.move-list .mv-b:hover {
  background: var(--gold-soft);
  color: var(--text);
}

/* Desktop: hide duplicate status in sidebar when in header */
@media (max-width: 767px) {
  .status-section { display: block; }
  .header-status { display: none; }
}

@media (min-width: 768px) {
  .status-section { display: none; }
  .header-status { display: flex; }
}

/* Touch improvements */
@media (hover: none) and (pointer: coarse) {
  .square { min-height: 0; }
  .btn { min-height: 48px; }
}
