:root {
  --bg: #15171c;
  --bg-elev: #1e2128;
  --bg-card: #242830;
  --border: #353a45;
  --text: #e8eaf0;
  --muted: #9aa1b1;
  --accent: #e0b35a;
  --accent-hover: #ecc56f;
  --accent-ink: #1a1405;
  --accent-soft: rgba(224, 179, 90, 0.14);
  --hover: rgba(255, 255, 255, 0.06);
  --btn-hover: #2d323c;
  --danger: #e36b6b;
  --success: #6ccf8e;
  --radius: 12px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  --overlay: rgba(0, 0, 0, 0.55);
  --board-bg: #1a1d24;
  --board-border: #2c313c;
  color-scheme: dark;
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

/* Light theme: explicit choice, or system preference when nothing is chosen. */
:root[data-theme="light"],
:root:not([data-theme="dark"]):not([data-theme="light"]) {
  --light-bg: #f3f4f8;
  --light-bg-elev: #ffffff;
  --light-bg-card: #ffffff;
  --light-border: #d8dce6;
  --light-text: #1b1f2b;
  --light-muted: #5f6678;
  --light-accent: #c9962b;
  --light-accent-hover: #d9a63a;
  --light-accent-ink: #1a1405;
  --light-accent-soft: rgba(201, 150, 43, 0.14);
  --light-hover: rgba(0, 0, 0, 0.05);
  --light-btn-hover: #eceef3;
  --light-danger: #c9414b;
  --light-success: #2e9e5b;
  --light-shadow: 0 10px 30px rgba(20, 24, 40, 0.14);
  --light-overlay: rgba(20, 24, 40, 0.4);
  --light-board-bg: #ffffff;
  --light-board-border: #d8dce6;
}
:root[data-theme="light"] {
  --bg: var(--light-bg); --bg-elev: var(--light-bg-elev); --bg-card: var(--light-bg-card); --border: var(--light-border);
  --text: var(--light-text); --muted: var(--light-muted); --accent: var(--light-accent); --accent-hover: var(--light-accent-hover);
  --accent-ink: var(--light-accent-ink); --accent-soft: var(--light-accent-soft); --hover: var(--light-hover); --btn-hover: var(--light-btn-hover);
  --danger: var(--light-danger); --success: var(--light-success); --shadow: var(--light-shadow); --overlay: var(--light-overlay);
  --board-bg: var(--light-board-bg); --board-border: var(--light-board-border);
  color-scheme: light;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]):not([data-theme="light"]) {
    --bg: var(--light-bg); --bg-elev: var(--light-bg-elev); --bg-card: var(--light-bg-card); --border: var(--light-border);
    --text: var(--light-text); --muted: var(--light-muted); --accent: var(--light-accent); --accent-hover: var(--light-accent-hover);
    --accent-ink: var(--light-accent-ink); --accent-soft: var(--light-accent-soft); --hover: var(--light-hover); --btn-hover: var(--light-btn-hover);
    --danger: var(--light-danger); --success: var(--light-success); --shadow: var(--light-shadow); --overlay: var(--light-overlay);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
a { color: var(--accent); }
h1, h2, h3 { margin: 0 0 0.5em; line-height: 1.2; }
h1 { font-size: 1.75rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; margin-top: 1.25em; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error { color: var(--danger); }

/* ---- brand link & docked controls ---- */
.brand { display: inline-block; font-weight: 700; color: var(--text); text-decoration: none; font-size: 1rem; margin-bottom: 0.4rem; }
.brand:hover { color: var(--accent); }
.dock { position: fixed; bottom: 1rem; z-index: 20; }
.dock-left { left: 1rem; }
.dock-right { right: 1rem; }
.auth-area { display: flex; align-items: center; gap: 0.6rem; background: var(--bg-elev); border: 1px solid var(--border); border-radius: 10px; padding: 0.3rem 0.3rem 0.3rem 0.75rem; box-shadow: var(--shadow); }
.auth-area:has(> .btn:only-child) { padding: 0; border: 0; background: none; box-shadow: none; }
.username { font-weight: 600; }
.theme-toggle { background: var(--bg-elev); border-color: var(--border); box-shadow: var(--shadow); }
/* ---- buttons / forms ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
  padding: 0.45rem 0.9rem; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg-card); color: var(--text); font: inherit; font-weight: 600; cursor: pointer;
  text-decoration: none; transition: background 0.15s, transform 0.05s, opacity 0.15s;
}
.btn:hover { background: var(--btn-hover); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: transparent; }
.theme-toggle { padding: 0.35rem 0.55rem; font-size: 1.05rem; }
.auth-area .btn-primary { box-shadow: var(--shadow); }
.btn-danger { background: var(--danger); color: #fff; border-color: transparent; }
.btn .count {
  background: rgba(0,0,0,0.25); border-radius: 999px; padding: 0 0.45rem; font-size: 0.8rem; min-width: 1.4rem;
}
.btn-primary .count { background: rgba(0,0,0,0.15); }

.form { display: flex; flex-direction: column; gap: 0.9rem; min-width: 300px; }
.form label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.9rem; color: var(--muted); }
.form input, .select {
  font: inherit; padding: 0.5rem 0.65rem; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text);
}
.form input:focus, .select:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
.form-error { color: var(--danger); min-height: 1.2em; font-size: 0.9rem; }
.form-actions { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.link { font-size: 0.9rem; }

/* ---- layout ---- */
.view { flex: 1; width: 100%; max-width: 1200px; margin: 0 auto; padding: 1rem 1.25rem 4.5rem; }
.view-game { max-width: none; padding: 0.25rem 0.25rem 0.25rem; }
.card { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem; }

/* ---- lobby ---- */
.game-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 1.25rem; }
.game-card {
  display: flex; flex-direction: column; gap: 0.75rem; text-decoration: none; color: inherit;
  background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.25rem;
  transition: transform 0.15s, border-color 0.15s;
}
.game-card:hover { transform: translateY(-2px); border-color: var(--accent); }
.game-icon { font-size: 2.5rem; line-height: 1; }
.game-card-body p { margin: 0; color: var(--muted); }
.game-card-actions { display: flex; gap: 0.5rem; margin-top: auto; }

/* ---- stats ---- */
.tabs { display: flex; gap: 0.5rem; margin-bottom: 1.25rem; flex-wrap: wrap; }
.tab { padding: 0.4rem 0.8rem; border-radius: 8px; text-decoration: none; color: var(--muted); border: 1px solid var(--border); }
.tab.active { color: var(--accent); border-color: var(--accent); }
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 1.25rem; align-items: start; }
.stat-row { display: flex; flex-wrap: wrap; gap: 1rem; margin: 0.5rem 0 1rem; }
.stat { background: var(--bg); border-radius: 10px; padding: 0.7rem 1rem; min-width: 120px; }
.stat-value { font-size: 1.4rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-label { color: var(--muted); font-size: 0.8rem; }
.stats-table { width: 100%; border-collapse: collapse; font-size: 0.95rem; font-variant-numeric: tabular-nums; }
.stats-table th { text-align: left; color: var(--muted); font-weight: 500; font-size: 0.8rem; padding: 0.3rem 0.5rem; border-bottom: 1px solid var(--border); }
.stats-table td { padding: 0.4rem 0.5rem; border-bottom: 1px solid var(--border); }

/* ---- modal / toast ---- */
.modal-overlay {
  position: fixed; inset: 0; background: var(--overlay); display: flex; align-items: center; justify-content: center;
  z-index: 100; padding: 1rem; backdrop-filter: blur(2px);
}
.modal {
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.5rem; box-shadow: var(--shadow); max-width: 480px; width: 100%;
}
.modal h2 { margin-top: 0; }
.modal-actions { display: flex; gap: 0.5rem; margin-top: 1.25rem; flex-wrap: wrap; }
.toast-root { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 0.5rem; z-index: 200; }
.toast {
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 10px; padding: 0.6rem 1rem;
  box-shadow: var(--shadow); opacity: 0; transform: translateY(8px); transition: opacity 0.25s, transform 0.25s;
}
.toast.show { opacity: 1; transform: none; }
.toast-success { border-color: var(--success); }
.toast-error { border-color: var(--danger); }

@media (max-width: 640px) {
  .view { padding: 0.75rem 0.75rem 4.5rem; }
  .form { min-width: 0; }
}
