/* ── Variables ─────────────────────────────────────────────────────────────── */
:root {
  --bg:       #0f1117;
  --surface:  #1a1d2e;
  --surface2: #232640;
  --border:   #2e3255;
  --text:     #e8eaf6;
  --muted:    #7b80a8;
  --accent:   #8b5cf6;
  --accent2:  #a78bfa;
  --red:    #cc2222;
  --green:  #3dc87a;
  --yellow: #f5c518;
  --blue:   #3a7bd5;
}
body.light {
  --bg:       #eef0f7;
  --surface:  #ffffff;
  --surface2: #f0f2fb;
  --border:   #dde0ef;
  --text:     #1a1b2e;
  --muted:    #6b6d8e;
  --accent:   #6d28d9;
  --accent2:  #7c3aed;
  --red:    #bb1111;
}

*, *::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: background .15s;
}
.btn-secondary:hover { background: var(--border); }
.btn-join {
  background: var(--accent2); color: #fff;
  border-radius: 8px; padding: 10px 18px;
  font-size: 0.95rem; font-weight: 600;
  white-space: nowrap;
  transition: opacity .15s;
}
.btn-join:hover { opacity: .85; }
.room-code-row { display: flex; gap: 8px; }
.room-code-input { text-transform: uppercase; letter-spacing: 0.15em; font-weight: 700; }

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

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

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

/* ── Table area ─────────────────────────────────────────────────────────────── */
.table-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

/* ── Opponent rows ─────────────────────────────────────────────────────────── */
.opp-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.opp-name {
  font-size: 0.8rem; color: var(--muted); font-weight: 600;
  text-transform: uppercase; letter-spacing: .04em;
}
.opp-name.active-turn { color: var(--accent2); }

/* Middle row */
.middle-row {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 16px;
}
.opp-side {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-width: 64px;
}

/* ── Trick cross ─────────────────────────────────────────────────────────────── */
.trick-zone {
  display: grid;
  grid-template-columns: 60px 60px 60px;
  grid-template-rows: 84px 84px 84px;
  gap: 2px;
  background: rgba(139,92,246,0.05);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px;
}
.trick-slot {
  display: flex; align-items: center; justify-content: center;
}
.trick-slot.top    { grid-column: 2; grid-row: 1; }
.trick-slot.left   { grid-column: 1; grid-row: 2; }
.trick-slot.center { grid-column: 2; grid-row: 2; }
.trick-slot.right  { grid-column: 3; grid-row: 2; }
.trick-slot.bottom { grid-column: 2; grid-row: 3; }

/* ── My hand ─────────────────────────────────────────────────────────────────── */
.my-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}
.my-name {
  font-size: 0.8rem; color: var(--muted); font-weight: 600;
  text-transform: uppercase; letter-spacing: .04em;
}
.my-name.active-turn { color: var(--accent2); }

#my-hand {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  justify-content: center;
  max-width: 680px;
  min-height: 84px;
  padding: 4px 0;
}

/* ── Bid row ─────────────────────────────────────────────────────────────────── */
#bid-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  max-width: 480px;
  padding: 4px 0;
}
.bid-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
  min-width: 48px;
  transition: background .15s, border-color .15s;
}
.bid-btn:hover { background: var(--accent); border-color: var(--accent); color: #fff; }
.bid-btn.nil-btn { color: var(--accent2); border-color: var(--accent2); }
.bid-btn.nil-btn:hover { background: var(--accent2); border-color: var(--accent2); color: #fff; }

/* ── Bid badge ───────────────────────────────────────────────────────────────── */
.bid-badge {
  font-size: 0.72rem;
  color: var(--muted);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 6px;
  margin-top: 2px;
  white-space: nowrap;
}
.bid-badge.nil { color: var(--accent2); border-color: var(--accent2); }

/* ── Face-down fan hands ─────────────────────────────────────────────────────── */
.fan-hand {
  display: flex;
  position: relative;
}
.fan-hand .card {
  position: relative;
  margin-left: -20px;
}
.fan-hand .card:first-child { margin-left: 0; }

/* ── Card ────────────────────────────────────────────────────────────────────── */
.card {
  width: 52px; height: 76px; border-radius: 7px;
  border: 1.5px solid rgba(255,255,255,0.15);
  background: #fff; color: #1a1a1a;
  display: flex; flex-direction: column; justify-content: space-between;
  padding: 3px 4px; font-size: 12px; font-weight: 700;
  user-select: none; transition: transform .15s, box-shadow .15s;
  cursor: default; flex-shrink: 0;
}
.card .corner { display: flex; flex-direction: column; line-height: 1.1; }
.card .corner.bot { transform: rotate(180deg); }
.card.red { color: var(--red); }
.card.back {
  background: repeating-linear-gradient(135deg,#1a3a6a,#1a3a6a 4px,#1e2d52 4px,#1e2d52 8px);
  border-color: #2a4a8a;
}
.card.valid { cursor: pointer; }
.card.valid:hover { transform: translateY(-10px); border-color: var(--accent2); box-shadow: 0 4px 12px rgba(139,92,246,.4); }
.card.selected { transform: translateY(-18px); border-color: var(--accent);
  box-shadow: 0 0 14px rgba(139,92,246,.55); }
.card.dimmed { opacity: .4; }
.card.trick-winner { box-shadow: 0 0 10px rgba(255,220,50,.7); border-color: #ffdc32; }

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

.panel-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
}
.panel-card h4 {
  font-size: 0.75rem; color: var(--muted);
  text-transform: uppercase; letter-spacing: .06em;
  margin-bottom: 8px;
}

/* Team scores */
.team-scores-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.team-box {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  text-align: center;
}
.team-box.my-team { border-color: var(--accent2); }
.team-label {
  font-size: 0.7rem; color: var(--muted);
  text-transform: uppercase; letter-spacing: .05em;
  margin-bottom: 4px;
}
.team-score-val {
  font-size: 1.3rem; font-weight: 800; color: var(--text);
}
.team-bags {
  font-size: 0.7rem; color: var(--muted); margin-top: 2px;
}

/* Round info: bids table */
#bids-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}
#bids-table th {
  color: var(--muted); font-weight: 600;
  text-align: left; padding: 2px 4px;
  font-size: 0.72rem;
}
#bids-table td { padding: 3px 4px; border-top: 1px solid var(--border); }
#bids-table tr.me td { color: var(--accent2); font-weight: 700; }

.spades-broken-label {
  font-size: 0.8rem;
  margin-top: 8px;
  color: var(--muted);
}
.spades-broken-label.broken { color: var(--accent2); font-weight: 600; }

/* Status text */
#status-text {
  font-size: 0.9rem; color: var(--text); min-height: 1.4em;
  line-height: 1.4;
}
#last-trick-info {
  font-size: 0.8rem; color: var(--muted);
  min-height: 1.2em;
  padding: 4px 0;
}

/* Resign button */
.ctrl-btn {
  width: 100%;
  border-radius: 8px; padding: 9px 14px;
  font-size: 0.88rem; font-weight: 600;
  transition: opacity .15s;
}
.ctrl-btn.danger {
  background: rgba(232,64,64,0.15); color: var(--red);
  border: 1px solid rgba(232,64,64,0.3);
}
.ctrl-btn.danger:hover { background: rgba(232,64,64,0.25); }

/* ── Result overlay ─────────────────────────────────────────────────────────── */
#spades-result {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.72);
  z-index: 200;
  align-items: center; justify-content: center;
}
#spades-result.visible { display: flex; }
.result-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 40px 48px;
  display: flex; flex-direction: column; align-items: center; gap: 20px;
  min-width: 320px;
}
.result-title { font-size: 1.8rem; font-weight: 800; }
.result-scores { width: 100%; }
.result-scores table {
  width: 100%; border-collapse: collapse; font-size: 0.95rem;
}
.result-scores th {
  color: var(--muted); padding: 4px 8px; text-align: left; font-size: 0.8rem;
}
.result-scores td { padding: 5px 8px; border-top: 1px solid var(--border); }
.result-scores tr.winner td { color: var(--green); font-weight: 700; }
.result-scores tr.me td { color: var(--accent2); }
.result-btns { display: flex; gap: 12px; }

/* ── Responsive ─────────────────────────────────────────────────────────────── */
@media (max-width: 860px) {
  #screen-game {
    flex-direction: column;
    align-items: center;
  }
  .side-panel { width: 100%; max-width: 520px; flex-direction: row; flex-wrap: wrap; }
  .panel-card { flex: 1 1 160px; }
}
