/* ── Variables ─────────────────────────────────────────────────────────────── */
:root {
  --bg:       #0f1117;
  --surface:  #1a1d2e;
  --surface2: #232640;
  --border:   #2e3255;
  --text:     #e8eaf6;
  --muted:    #7b80a8;
  --accent:   #8b5cf6;
  --accent2:  #a78bfa;

  --blue:   #3a7bd5;
  --yellow: #f5c518;
  --red:    #e84040;
  --green:  #3dc87a;
}
body.light {
  --bg:       #eef0f7;
  --surface:  #ffffff;
  --surface2: #f0f2fb;
  --border:   #dde0ef;
  --text:     #1a1b2e;
  --muted:    #6b6d8e;
  --accent:   #6d28d9;
  --accent2:  #7c3aed;
}

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

body {
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  background-image:
    radial-gradient(circle at 20% 20%, rgba(139,92,246,0.06) 0%, transparent 50%),
    radial-gradient(circle at 80% 80%, rgba(167,139,250,0.04) 0%, transparent 50%);
  transition: background 0.3s, color 0.3s;
}
button { font-family: inherit; cursor: pointer; border: none; outline: none; }
input  { font-family: inherit; outline: none; }

/* ── Screens ────────────────────────────────────────────────────────────────── */
.screen { display: none; }
.screen.active { display: flex; flex-direction: column; align-items: center; }

/* ── Top bar ────────────────────────────────────────────────────────────────── */
#top-bar {
  position: fixed; top: 0; right: 0;
  display: flex; gap: 8px; align-items: center;
  padding: 10px 16px; z-index: 100;
}
#theme-toggle {
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 10px; font-size: 16px;
  color: var(--text); cursor: pointer;
}

/* ── Lobby ──────────────────────────────────────────────────────────────────── */
#screen-lobby {
  min-height: 100vh;
  justify-content: center;
  padding: 80px 16px 40px;
}
.lobby-inner {
  width: 100%; max-width: 420px;
  display: flex; flex-direction: column; gap: 16px;
}
.lobby-title { text-align: center; margin-bottom: 8px; }
.lobby-title h1 { font-size: 2rem; font-weight: 700; }
.lobby-title p  { color: var(--muted); margin-top: 4px; }

.lobby-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  display: flex; flex-direction: column; gap: 12px;
}
.lobby-card h3 { font-size: 0.85rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }

.name-input, .room-code-input {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  color: var(--text);
  font-size: 1rem;
  width: 100%;
}
.name-input:focus, .room-code-input:focus { border-color: var(--accent2); }

.btn-primary {
  background: var(--accent); color: #fff;
  border-radius: 8px; padding: 11px 20px;
  font-size: 0.95rem; font-weight: 600;
  transition: opacity .15s;
}
.btn-primary:hover { opacity: .85; }
.btn-secondary {
  background: var(--surface2); color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 20px;
  font-size: 0.95rem; font-weight: 500;
  transition: border-color .15s;
}
.btn-secondary:hover { border-color: var(--accent2); }

.room-code-row { display: flex; gap: 8px; }
.room-code-input { flex: 1; text-transform: uppercase; letter-spacing: .1em; font-weight: 600; }
.btn-join {
  background: var(--surface2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 16px; font-size: 0.9rem; font-weight: 600;
}
.btn-join:hover { border-color: var(--accent2); }

#lobby-error {
  color: var(--red); font-size: 0.9rem; text-align: center; min-height: 20px;
}

/* ── Waiting ────────────────────────────────────────────────────────────────── */
#screen-waiting {
  min-height: 100vh; justify-content: center; padding: 16px;
}
.waiting-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 40px;
  display: flex; flex-direction: column; align-items: center; gap: 16px;
  max-width: 360px; width: 100%; text-align: center;
}
.spinner {
  width: 40px; height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent2);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#waiting-title  { font-size: 1.4rem; font-weight: 700; }
#waiting-sub    { color: var(--muted); }
.room-code-display {
  font-size: 2rem; font-weight: 800; letter-spacing: .25em;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 24px;
}
.room-share-hint { color: var(--muted); font-size: 0.85rem; }

/* ── Game screen ────────────────────────────────────────────────────────────── */
#screen-game {
  min-height: 100vh;
  padding: 12px;
  flex-direction: row !important;
  align-items: flex-start !important;
  justify-content: center;
  gap: 16px;
  flex-wrap: wrap;
}

/* ── Board area ─────────────────────────────────────────────────────────────── */
.board-area {
  display: flex; flex-direction: column; align-items: center; gap: 10px;
}
#status-bar {
  font-size: 1rem; font-weight: 600; color: var(--text);
  min-height: 28px;
}
#board-canvas {
  display: block;
  border: 2px solid var(--border);
  border-radius: 4px;
  cursor: crosshair;
}

/* ── Side panel ─────────────────────────────────────────────────────────────── */
.side-panel {
  width: 220px;
  display: flex; flex-direction: column; gap: 10px;
  padding-top: 38px;
}

/* Players list */
.players-panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 12px;
  display: flex; flex-direction: column; gap: 6px;
}
.player-row {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px; border-radius: 8px;
  font-size: 0.9rem;
}
.player-row.active-turn { background: var(--surface2); }
.player-dot {
  width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0;
}
.player-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player-score { color: var(--muted); font-size: 0.8rem; }
.player-blocked { opacity: 0.45; text-decoration: line-through; }

/* Controls */
.controls-panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 12px;
  display: flex; flex-direction: column; gap: 8px;
}
.controls-panel h4 { font-size: 0.8rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.ctrl-row { display: flex; gap: 6px; }
.ctrl-btn {
  flex: 1; background: var(--surface2); color: var(--text);
  border: 1px solid var(--border); border-radius: 7px;
  padding: 7px 8px; font-size: 0.8rem; font-weight: 600;
  transition: border-color .15s;
}
.ctrl-btn:hover { border-color: var(--accent2); }
.ctrl-btn.danger { color: var(--red); border-color: rgba(248,113,113,.25); }
.ctrl-btn.danger:hover { border-color: var(--red); background: rgba(248,113,113,.08); }

/* Piece tray */
.tray-panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 12px;
  display: flex; flex-direction: column; gap: 8px;
}
.tray-panel h4 { font-size: 0.8rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
#piece-tray {
  display: flex; flex-wrap: wrap; gap: 4px;
}
.piece-btn {
  background: var(--surface2); border: 2px solid var(--border);
  border-radius: 6px; padding: 3px; cursor: pointer;
  transition: border-color .15s;
  line-height: 0;
}
.piece-btn:hover { border-color: var(--accent2); }
.piece-btn.selected { border-color: #fff; }
.piece-btn canvas { display: block; }

/* ── Result overlay ─────────────────────────────────────────────────────────── */
#blokus-result {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.65);
  z-index: 200;
  align-items: center; justify-content: center;
}
#blokus-result.visible { display: flex; }
.result-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 36px 40px;
  text-align: center; max-width: 360px; width: 90%;
  display: flex; flex-direction: column; gap: 16px;
}
.result-title { font-size: 1.6rem; font-weight: 800; }
.result-scores { display: flex; flex-direction: column; gap: 6px; }
.result-row {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px; border-radius: 8px;
  background: var(--surface2); font-size: 0.9rem;
}
.result-row.winner { border: 1px solid var(--accent2); }
.result-color { width: 14px; height: 14px; border-radius: 3px; flex-shrink: 0; }
.result-name { flex: 1; text-align: left; }
.result-score { font-weight: 700; }
.result-btns { display: flex; gap: 10px; justify-content: center; }

/* ── Responsive ─────────────────────────────────────────────────────────────── */
@media (max-width: 700px) {
  #screen-game { flex-direction: column !important; align-items: center !important; }
  .side-panel { width: 100%; max-width: 600px; padding-top: 0; }
  #board-canvas { max-width: 100%; height: auto; }
}
