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

  :root {
    --bg: #0c0c10;
    --surface: #13131a;
    --surface2: #1a1a24;
    --border: #2a2a3d;
    --gold: #c9a84c;
    --gold2: #e8c97a;
    --gold-dim: #6b5420;
    --cream: #f0e6cc;
    --muted: #6b6b88;
    --white-sq: #e8d5b0;
    --black-sq: #7a5c38;
    --white-sq-hi: #f5e87a;
    --black-sq-hi: #c8a820;
    --white-sq-move: #a8d8a0;
    --black-sq-move: #5a9850;
    --white-sq-swap: #a0c0f0;
    --black-sq-swap: #4878c8;
    --check-sq: #e84040;
    --danger: #e84040;
    --success: #50c878;
    --radius: 4px;
    --board-size: min(90vw, 90vh, 560px);
  }

  html, body {
    height: 100%;
    background: var(--bg);
    color: var(--cream);
    font-family: 'Crimson Pro', serif;
    font-size: 16px;
    overflow-x: hidden;
  }

  /* ── Noise texture overlay ─────────────────────────────── */
  body::before {
    content: '';
    position: fixed;
    inset: 0;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)' opacity='0.04'/%3E%3C/svg%3E");
    pointer-events: none;
    z-index: 0;
  }

  /* ── Screens ───────────────────────────────────────────── */
  .screen { display: none; position: relative; z-index: 1; }
  .screen.active { display: flex; }

  /* ── Landing ───────────────────────────────────────────── */
  #landing {
    min-height: 100vh;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2rem;
    padding: 2rem;
  }

  .logo-wrap {
    text-align: center;
    animation: fadeDown 0.8s ease both;
  }
  .logo-wrap h1 {
    font-family: 'Cinzel', serif;
    font-size: clamp(2.4rem, 8vw, 4.5rem);
    font-weight: 900;
    letter-spacing: 0.06em;
    background: linear-gradient(135deg, var(--gold) 0%, var(--gold2) 50%, var(--gold) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    line-height: 1;
  }
  .logo-wrap .tagline {
    font-style: italic;
    color: var(--muted);
    font-size: 1.1rem;
    margin-top: 0.5rem;
    letter-spacing: 0.04em;
  }

  .deco-line {
    display: flex;
    align-items: center;
    gap: 1rem;
    width: 100%;
    max-width: 340px;
    margin: 0 auto;
  }
  .deco-line::before, .deco-line::after {
    content: '';
    flex: 1;
    height: 1px;
    background: linear-gradient(to right, transparent, var(--gold-dim));
  }
  .deco-line::after {
    background: linear-gradient(to left, transparent, var(--gold-dim));
  }
  .deco-line span { font-size: 1.2rem; color: var(--gold-dim); }

  .menu-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 2rem;
    width: 100%;
    max-width: 380px;
    animation: fadeUp 0.8s 0.2s ease both;
  }

  .menu-title {
    font-family: 'Cinzel', serif;
    font-size: 0.85rem;
    letter-spacing: 0.15em;
    color: var(--gold);
    text-transform: uppercase;
    margin-bottom: 1.2rem;
  }

  .btn {
    display: block;
    width: 100%;
    padding: 0.75rem 1.5rem;
    border: 1px solid var(--gold-dim);
    background: transparent;
    color: var(--gold2);
    font-family: 'Cinzel', serif;
    font-size: 0.9rem;
    letter-spacing: 0.1em;
    cursor: pointer;
    border-radius: var(--radius);
    transition: all 0.2s;
    text-align: center;
  }
  .btn:hover {
    background: rgba(201, 168, 76, 0.12);
    border-color: var(--gold);
    transform: translateY(-1px);
    box-shadow: 0 4px 20px rgba(201, 168, 76, 0.15);
  }
  .btn:active { transform: translateY(0); }
  .btn.primary {
    background: linear-gradient(135deg, #7a5420, #c9a84c);
    border-color: var(--gold);
    color: #0c0c10;
    font-weight: 700;
  }
  .btn.primary:hover {
    background: linear-gradient(135deg, #8a6030, #e8c97a);
    box-shadow: 0 4px 24px rgba(201, 168, 76, 0.35);
  }
  .btn.danger {
    border-color: #5a2020;
    color: #e88080;
  }
  .btn.danger:hover { background: rgba(232, 64, 64, 0.1); border-color: var(--danger); }
  .btn.sm { width: auto; font-size: 0.78rem; padding: 0.4rem 0.9rem; }

  .input-field {
    width: 100%;
    padding: 0.7rem 1rem;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--cream);
    font-family: 'Crimson Pro', serif;
    font-size: 1.1rem;
    letter-spacing: 0.08em;
    outline: none;
    transition: border-color 0.2s;
    text-transform: uppercase;
  }
  .input-field:focus { border-color: var(--gold); }
  .input-field::placeholder { color: var(--muted); text-transform: none; letter-spacing: 0; }

  .form-group { margin-bottom: 1rem; }
  .form-label {
    display: block;
    font-family: 'Cinzel', serif;
    font-size: 0.72rem;
    letter-spacing: 0.12em;
    color: var(--muted);
    text-transform: uppercase;
    margin-bottom: 0.4rem;
  }

  .divider { height: 1px; background: var(--border); margin: 1.2rem 0; }

  .bot-levels {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.5rem;
  }
  .level-btn {
    flex: 1;
    padding: 0.5rem;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--muted);
    font-family: 'Cinzel', serif;
    font-size: 0.75rem;
    letter-spacing: 0.08em;
    cursor: pointer;
    transition: all 0.2s;
    text-align: center;
  }
  .level-btn.active {
    border-color: var(--gold);
    color: var(--gold2);
    background: rgba(201, 168, 76, 0.1);
  }

  .room-code-display {
    background: var(--surface2);
    border: 1px solid var(--gold-dim);
    border-radius: var(--radius);
    padding: 1rem;
    text-align: center;
    margin-bottom: 1rem;
  }
  .room-code-display .code {
    font-family: 'Cinzel', serif;
    font-size: 2rem;
    font-weight: 700;
    letter-spacing: 0.3em;
    color: var(--gold2);
  }
  .room-code-display .hint {
    font-size: 0.85rem;
    color: var(--muted);
    margin-top: 0.3rem;
    font-style: italic;
  }

  .wait-pulse {
    display: inline-block;
    width: 8px; height: 8px;
    background: var(--gold);
    border-radius: 50%;
    margin-right: 0.5rem;
    animation: pulse 1.4s ease infinite;
  }

  /* ── Game Screen ──────────────────────────────────────── */
  #game {
    min-height: 100vh;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding: 1rem;
    gap: 0.75rem;
  }

  .game-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    max-width: calc(var(--board-size) + 240px);
  }
  .game-title {
    font-family: 'Cinzel', serif;
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--gold);
    letter-spacing: 0.08em;
  }

  .game-layout {
    display: flex;
    gap: 1.25rem;
    align-items: flex-start;
    width: 100%;
    max-width: calc(var(--board-size) + 260px);
    flex-wrap: wrap;
    justify-content: center;
  }

  /* ── Board ─────────────────────────────────────────────── */
  .board-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.3rem;
  }

  .player-bar {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    width: var(--board-size);
    padding: 0.5rem 0.6rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
  }
  .player-bar .color-dot {
    width: 14px; height: 14px;
    border-radius: 50%;
    flex-shrink: 0;
    border: 2px solid var(--border);
  }
  .color-dot.white { background: #f0e6cc; }
  .color-dot.black { background: #1a1a24; border-color: #555; }
  .player-bar .name { font-family: 'Cinzel', serif; font-size: 0.85rem; color: var(--cream); flex: 1; }
  .player-bar .turn-indicator {
    font-size: 0.75rem;
    color: var(--gold);
    font-style: italic;
    opacity: 0;
    transition: opacity 0.3s;
  }
  .player-bar.active .turn-indicator { opacity: 1; }
  .player-bar.active { border-color: var(--gold-dim); }
  .player-bar.in-check { border-color: var(--danger); }
  .player-bar.in-check .turn-indicator { color: var(--danger); }

  .board-container {
    position: relative;
    width: var(--board-size);
    height: var(--board-size);
  }

  #board {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    width: 100%;
    height: 100%;
    border: 2px solid var(--gold-dim);
    border-radius: 2px;
    overflow: hidden;
    box-shadow: 0 8px 48px rgba(0,0,0,0.6), 0 0 0 1px rgba(201,168,76,0.1);
  }

  .sq {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.12s;
    user-select: none;
  }
  .sq.light { background: var(--white-sq); }
  .sq.dark  { background: var(--black-sq); }
  .sq.selected.light { background: var(--white-sq-hi); }
  .sq.selected.dark  { background: var(--black-sq-hi); }
  .sq.move-hint.light { background: var(--white-sq-move); }
  .sq.move-hint.dark  { background: var(--black-sq-move); }
  .sq.swap-hint.light { background: var(--white-sq-swap); }
  .sq.swap-hint.dark  { background: var(--black-sq-swap); }
  .sq.last-move.light { background: #dbc86a; }
  .sq.last-move.dark  { background: #9a8430; }
  .sq.in-check.light, .sq.in-check.dark { background: var(--check-sq); }

  /* Coordinate labels */
  .sq .coord-rank {
    position: absolute;
    top: 2px; left: 3px;
    font-size: calc(var(--board-size) / 60);
    font-family: 'Cinzel', serif;
    font-weight: 600;
    opacity: 0.55;
    pointer-events: none;
    line-height: 1;
  }
  .sq .coord-file {
    position: absolute;
    bottom: 2px; right: 3px;
    font-size: calc(var(--board-size) / 60);
    font-family: 'Cinzel', serif;
    font-weight: 600;
    opacity: 0.55;
    pointer-events: none;
    line-height: 1;
  }
  .sq.light .coord-rank, .sq.light .coord-file { color: var(--black-sq); }
  .sq.dark  .coord-rank, .sq.dark  .coord-file  { color: var(--white-sq); }

  .piece {
    width: calc(var(--board-size) / 9.5);
    height: calc(var(--board-size) / 9.5);
    pointer-events: none;
    transition: transform 0.1s;
    display: block;
    filter: drop-shadow(0 1px 3px rgba(0,0,0,0.45));
  }

  /* move dot hint */
  .sq.move-hint::after {
    content: '';
    position: absolute;
    width: 28%;
    height: 28%;
    border-radius: 50%;
    background: rgba(0,0,0,0.22);
    pointer-events: none;
  }
  .sq.move-hint.capture-hint::after {
    width: 82%; height: 82%;
    background: transparent;
    border: 3px solid rgba(0,0,0,0.22);
    border-radius: 50%;
  }
  .sq.swap-hint::after {
    content: '⇄';
    position: absolute;
    font-size: calc(var(--board-size) / 22);
    color: rgba(0,0,0,0.4);
    pointer-events: none;
  }

  /* ── Sidebar ───────────────────────────────────────────── */
  .sidebar {
    width: 220px;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    flex-shrink: 0;
  }

  .panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.9rem;
  }
  .panel-title {
    font-family: 'Cinzel', serif;
    font-size: 0.72rem;
    letter-spacing: 0.14em;
    color: var(--gold);
    text-transform: uppercase;
    margin-bottom: 0.7rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border);
  }

  .status-text {
    font-size: 0.95rem;
    color: var(--cream);
    line-height: 1.5;
    font-style: italic;
  }
  .status-text.highlight { color: var(--gold2); font-style: normal; font-weight: 600; }
  .status-text.error { color: var(--danger); }

  /* Swap mode toggle */
  .swap-toggle {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.6rem 0.8rem;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    transition: all 0.2s;
    user-select: none;
  }
  .swap-toggle:hover { border-color: var(--gold-dim); }
  .swap-toggle.active {
    border-color: #4878c8;
    background: rgba(72, 120, 200, 0.12);
  }
  .swap-toggle .icon { font-size: 1.2rem; }
  .swap-toggle .label {
    font-family: 'Cinzel', serif;
    font-size: 0.8rem;
    letter-spacing: 0.08em;
    color: var(--cream);
  }
  .swap-toggle .sub {
    font-size: 0.72rem;
    color: var(--muted);
    margin-top: 0.15rem;
    font-style: italic;
  }

  .move-log {
    max-height: 160px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    font-size: 0.82rem;
  }
  .move-log::-webkit-scrollbar { width: 4px; }
  .move-log::-webkit-scrollbar-track { background: var(--surface2); }
  .move-log::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
  .move-entry {
    display: flex;
    gap: 0.4rem;
    padding: 0.2rem 0.4rem;
    border-radius: 2px;
  }
  .move-entry:nth-child(odd) { background: rgba(255,255,255,0.02); }
  .move-entry .mn { color: var(--muted); width: 20px; flex-shrink: 0; }
  .move-entry .mv { color: var(--cream); }
  .move-entry .mv.swap-move { color: #88b8f0; }
  .move-entry .mv.swap-move::before { content: '⇄ '; }

  /* ── Overlay / Modal ───────────────────────────────────── */
  .overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.75);
    backdrop-filter: blur(4px);
    z-index: 100;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.3s ease;
  }
  .overlay.show { display: flex; }
  .modal {
    background: var(--surface);
    border: 1px solid var(--gold-dim);
    border-radius: 10px;
    padding: 2.5rem;
    width: 90%;
    max-width: 380px;
    text-align: center;
    animation: scaleIn 0.3s ease both;
    position: relative;
  }
  .modal-icon { font-size: 3rem; margin-bottom: 0.5rem; }
  .modal-title {
    font-family: 'Cinzel', serif;
    font-size: 1.8rem;
    color: var(--gold2);
    margin-bottom: 0.5rem;
  }
  .modal-sub { color: var(--muted); font-style: italic; margin-bottom: 1.5rem; font-size: 1rem; }
  .modal .btn + .btn { margin-top: 0.5rem; }

  /* Promotion picker */
  #promo-overlay .modal { max-width: 340px; }
  .promo-pieces {
    display: flex;
    gap: 0.75rem;
    justify-content: center;
    margin: 1rem 0;
  }
  .promo-piece {
    width: 60px; height: 60px;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2.5rem;
    cursor: pointer;
    transition: all 0.15s;
  }
  .promo-piece:hover {
    border-color: var(--gold);
    background: rgba(201, 168, 76, 0.12);
    transform: translateY(-2px);
  }

  /* Toast */
  #toast {
    position: fixed;
    bottom: 1.5rem;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.6rem 1.2rem;
    font-family: 'Cinzel', serif;
    font-size: 0.82rem;
    letter-spacing: 0.08em;
    color: var(--cream);
    z-index: 200;
    transition: transform 0.3s ease;
    pointer-events: none;
    white-space: nowrap;
  }
  #toast.show { transform: translateX(-50%) translateY(0); }
  #toast.error { border-color: var(--danger); color: #e88080; }

  /* ── Animations ────────────────────────────────────────── */
  @keyframes fadeDown {
    from { opacity: 0; transform: translateY(-20px); }
    to   { opacity: 1; transform: translateY(0); }
  }
  @keyframes fadeUp {
    from { opacity: 0; transform: translateY(20px); }
    to   { opacity: 1; transform: translateY(0); }
  }
  @keyframes fadeIn {
    from { opacity: 0; } to { opacity: 1; }
  }
  @keyframes scaleIn {
    from { opacity: 0; transform: scale(0.9); }
    to   { opacity: 1; transform: scale(1); }
  }
  @keyframes pulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.4; transform: scale(0.8); }
  }
  @keyframes pieceCapture {
    0% { transform: scale(1); }
    50% { transform: scale(1.3); opacity: 0.5; }
    100% { transform: scale(0); opacity: 0; }
  }
  @keyframes swapGlow {
    0% { box-shadow: inset 0 0 0 0 rgba(72,120,200,0); }
    50% { box-shadow: inset 0 0 20px 4px rgba(72,120,200,0.4); }
    100% { box-shadow: inset 0 0 0 0 rgba(72,120,200,0); }
  }

  /* ── Responsive ────────────────────────────────────────── */
  @media (max-width: 680px) {
    :root { --board-size: min(94vw, 94vw); }
    .sidebar { width: 100%; flex-direction: row; flex-wrap: wrap; }
    .panel { flex: 1; min-width: 140px; }
  }