/* ─── Reset & Base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

:root {
  --bg:          #1a1a2e;
  --surface:     #16213e;
  --surface2:    #0f3460;
  --accent:      #e94560;
  --accent-dark: #c73652;
  --text:        #eee;
  --text-dim:    #aaa;
  --border:      #2a2a4e;
  --green:       #27ae60;
  --radius:      10px;

  /* Card colors */
  --c-neg2:   #4a0080;
  --c-neg1:   #1a4fa8;
  --c-zero:   #1565c0;
  --c-low:    #2e7d32;
  --c-mid:    #f9a825;
  --c-high:   #e53935;
}

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ─── Lobby ────────────────────────────────────────────────────────────────── */
.lobby-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
}

.lobby-container {
  width: 100%;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.logo-mark {
  filter: drop-shadow(0 0 18px rgba(233,69,96,0.45));
}

.logo {
  font-size: 3.5rem;
  font-weight: 900;
  letter-spacing: 0.15em;
  color: var(--accent);
  text-shadow: 0 0 30px rgba(233,69,96,0.5);
}

.subtitle { color: var(--text-dim); font-size: 0.9rem; letter-spacing: 0.1em; }

.lobby-footer {
  margin-top: 20px;
  text-align: center;
  color: var(--text-dim);
  font-size: 0.72rem;
  letter-spacing: 0.03em;
  opacity: 0.7;
}

.game-footer {
  position: fixed;
  bottom: 2px;
  left: 0;
  right: 0;
  text-align: center;
  color: var(--text-dim);
  font-size: 0.6rem;
  letter-spacing: 0.02em;
  opacity: 0.4;
  pointer-events: none;
  z-index: 1;
}

.card-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group label { font-size: 0.85rem; color: var(--text-dim); font-weight: 600; }

input[type="text"] {
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 1rem;
  padding: 12px 14px;
  outline: none;
  transition: border-color 0.2s;
  width: 100%;
}
input[type="text"]:focus { border-color: var(--accent); }

.btn-row { display: flex; gap: 10px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  padding: 13px 20px;
  transition: background 0.15s, opacity 0.15s, transform 0.1s;
  min-height: 44px;
  min-width: 44px;
  flex: 1;
  user-select: none;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.btn-primary  { background: var(--surface2); color: var(--text); }
.btn-primary:hover:not(:disabled)  { background: #1a4a80; }
.btn-accent   { background: var(--accent); color: #fff; }
.btn-accent:hover:not(:disabled)   { background: var(--accent-dark); }
.btn-ghost    { background: transparent; color: var(--text-dim); border: 1.5px solid var(--border); }
.btn-ghost:hover:not(:disabled)    { border-color: var(--text-dim); color: var(--text); }
.btn-sm { padding: 9px 14px; font-size: 0.85rem; }

.error-msg {
  color: var(--accent);
  font-size: 0.85rem;
  background: rgba(233,69,96,0.12);
  border: 1px solid rgba(233,69,96,0.3);
  border-radius: 6px;
  padding: 10px 12px;
}

.hint { color: var(--text-dim); font-size: 0.8rem; text-align: center; }

.room-header {
  display: flex;
  gap: 6px;
  align-items: center;
  font-size: 0.9rem;
}
.room-label { color: var(--text-dim); }
.room-name-display { color: var(--accent); font-weight: 700; }

.player-list { display: flex; flex-direction: column; gap: 8px; }

.player-item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.95rem;
}
.player-item.disconnected { opacity: 0.45; }

.player-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.player-dot.online  { background: var(--green); box-shadow: 0 0 6px var(--green); }
.player-dot.offline { background: var(--text-dim); }

.you-badge {
  margin-left: auto;
  background: var(--accent);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 20px;
}

.crown-badge {
  font-size: 0.85rem;
  margin-left: 4px;
}

/* Bot difficulty badges */
.diff-badge {
  font-size: 0.65rem;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 20px;
  letter-spacing: 0.04em;
}
.diff-easy   { background: #2e7d32; color: #fff; }
.diff-medium { background: #f9a825; color: #222; }
.diff-hard   { background: #e53935; color: #fff; }

.player-dot.bot-dot {
  width: auto;
  height: auto;
  background: none;
  font-size: 0.85rem;
  box-shadow: none;
}

.btn-remove-bot {
  margin-left: auto;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 6px;
  font-size: 0.75rem;
  padding: 3px 8px;
  cursor: pointer;
  min-height: 28px;
  min-width: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s, color 0.15s;
}
.btn-remove-bot:hover { border-color: var(--accent); color: var(--accent); }

/* Bot controls section */
.bot-controls {
  border-top: 1px solid var(--border);
  padding-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.bot-controls-label {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.bot-add-row {
  display: flex;
  gap: 8px;
  align-items: center;
}
.bot-select {
  flex: 1;
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.88rem;
  padding: 10px 12px;
  outline: none;
  cursor: pointer;
  min-height: 44px;
}
.bot-select:focus { border-color: var(--accent); }
.bot-hint { color: var(--text-dim); font-size: 0.75rem; }

/* Bot badge in game */
.bot-label {
  font-size: 0.65rem;
  background: var(--surface2);
  color: var(--text-dim);
  border-radius: 4px;
  padding: 1px 5px;
  font-weight: 700;
}

/* ─── Game Layout ──────────────────────────────────────────────────────────── */
.game-page {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}

.game-layout {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

.game-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
  min-height: 48px;
  flex-wrap: wrap;
}

.header-room  { color: var(--accent); font-weight: 700; font-size: 0.9rem; }
.header-status { color: var(--text-dim); font-size: 0.82rem; flex: 1; }
.header-turn  {
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 600;
  background: rgba(233,69,96,0.15);
  border: 1px solid rgba(233,69,96,0.3);
  border-radius: 20px;
  padding: 4px 12px;
  white-space: nowrap;
}

/* Center area */
.game-center {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
}

/* Opponents */
.opponents-area {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  width: 100%;
}

.opponent-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  min-width: 120px;
  max-width: 200px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.opponent-card.active-turn {
  border-color: var(--accent);
  box-shadow: 0 0 10px rgba(233,69,96,0.3);
}
.opponent-card.disconnected { opacity: 0.4; }

.opponent-name {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text-dim);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.opponent-score-badge {
  background: var(--surface2);
  border-radius: 4px;
  padding: 1px 6px;
  font-size: 0.7rem;
  color: var(--text);
}

.mini-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 2px;
}

.mini-card {
  width: 22px;
  height: 28px;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.55rem;
  font-weight: 700;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,0.5);
}

/* Piles */
.piles-area {
  display: flex;
  gap: 24px;
  align-items: flex-start;
  justify-content: center;
  flex-shrink: 0;
}

.pile-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.pile-label {
  font-size: 0.7rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.pile-card {
  width: 70px;
  height: 95px;
  border-radius: 8px;
  cursor: pointer;
  position: relative;
  user-select: none;
}

/* Drawn card */
.drawn-card-area {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  background: rgba(233,69,96,0.08);
  border-top: 1px dashed rgba(233,69,96,0.4);
  border-bottom: 1px dashed rgba(233,69,96,0.4);
  flex-shrink: 0;
}
.drawn-card-area .pile-label { flex-shrink: 0; }
.drawn-card-area #drawnCardDisplay { width: 46px; height: 62px; flex-shrink: 0; }
.drawn-card-area #drawnCardValue { font-size: 1.1rem; }
.drawn-card-actions { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; flex: 1; }
.drawn-card-actions .hint { display: none; }
.drawn-card-actions .btn { flex: none; }

/* ─── Cards ────────────────────────────────────────────────────────────────── */
.card {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  position: relative;
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
  transition: transform 0.15s, box-shadow 0.15s;
  user-select: none;
}

.card-value {
  font-weight: 900;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0,0,0,0.6);
  line-height: 1;
}

/* Card color by value */
.card[data-val="-2"]  { background: var(--c-neg2); }
.card[data-val="-1"]  { background: var(--c-neg1); }
.card[data-val="0"]   { background: var(--c-zero); }
.card[data-val="1"],
.card[data-val="2"],
.card[data-val="3"],
.card[data-val="4"]   { background: var(--c-low); }
.card[data-val="5"],
.card[data-val="6"],
.card[data-val="7"],
.card[data-val="8"]   { background: var(--c-mid); color: #222; }
.card[data-val="5"] .card-value,
.card[data-val="6"] .card-value,
.card[data-val="7"] .card-value,
.card[data-val="8"] .card-value { color: #222; text-shadow: none; }
.card[data-val="9"],
.card[data-val="10"],
.card[data-val="11"],
.card[data-val="12"]  { background: var(--c-high); }

.card-back {
  background: var(--surface2);
  cursor: pointer;
}
.card-back-inner {
  width: 80%;
  height: 80%;
  border: 2px solid rgba(255,255,255,0.15);
  border-radius: 6px;
  background: repeating-linear-gradient(
    45deg,
    transparent,
    transparent 4px,
    rgba(255,255,255,0.04) 4px,
    rgba(255,255,255,0.04) 8px
  );
}

.pile-count {
  position: absolute;
  bottom: 6px;
  right: 8px;
  font-size: 0.7rem;
  font-weight: 700;
  color: rgba(255,255,255,0.7);
}

/* Clickable highlight */
.card.clickable {
  cursor: pointer;
  animation: pulse-glow 1.5s ease-in-out infinite;
}
.card.clickable:hover, .card.clickable:active {
  transform: translateY(-3px) scale(1.04);
  box-shadow: 0 8px 20px rgba(233,69,96,0.5);
}

@keyframes pulse-glow {
  0%, 100% { box-shadow: 0 4px 12px rgba(0,0,0,0.4), 0 0 0 0 rgba(233,69,96,0.4); }
  50%       { box-shadow: 0 4px 12px rgba(0,0,0,0.4), 0 0 0 6px rgba(233,69,96,0); }
}

.card.removed-card {
  opacity: 0;
  pointer-events: none;
}

/* Action hint */
.action-hint {
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 8px 16px;
  text-align: center;
  font-size: 0.85rem;
  color: var(--text);
  min-height: 38px;
  flex-shrink: 0;
}
.action-hint.my-turn { color: var(--accent); font-weight: 600; }

/* Own area */
.own-area {
  background: var(--surface);
  border-top: 2px solid var(--border);
  padding: 10px 12px 14px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.own-label {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.85rem;
  font-weight: 700;
}

.own-score {
  color: var(--text-dim);
  font-size: 0.8rem;
  font-weight: 600;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 6px;
  width: 100%;
  max-width: 360px;
}

.own-card {
  aspect-ratio: 3/4;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.3rem;
  font-weight: 900;
  min-height: 60px;
}

/* ─── Overlays ─────────────────────────────────────────────────────────────── */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-y: auto;
  z-index: 200;
  padding: 20px;
  backdrop-filter: blur(4px);
}

.overlay-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px 24px;
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  align-items: center;
  text-align: center;
  flex-shrink: 0;
}
.overlay-box.wide { max-width: 480px; }
.overlay-box h2 { color: var(--accent); font-size: 1.4rem; }

.score-table {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.score-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.9rem;
}
.score-row.winner { border: 1px solid var(--accent); }

.score-name { flex: 1; text-align: left; font-weight: 600; }
.score-round { width: 70px; text-align: right; }
.score-total { width: 70px; text-align: right; color: var(--text-dim); font-size: 0.8rem; }
.score-doubled { color: var(--accent); font-size: 0.72rem; }

.reveal-status {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent);
}

.reveal-grid {
  width: 100%;
  max-width: 340px;
  grid-template-rows: repeat(3, auto);
}

.winner-text {
  color: #ffd700;
  font-size: 1.1rem;
  font-weight: 700;
}

/* ─── Floating results button ───────────────────────────────────────────────── */
.floating-results {
  position: fixed;
  bottom: 20px;
  right: 16px;
  z-index: 150;
}

/* ─── Toast ────────────────────────────────────────────────────────────────── */
.toast-container {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  width: 90%;
  max-width: 400px;
}

.toast {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 16px;
  font-size: 0.85rem;
  color: var(--text);
  text-align: center;
  animation: toast-in 0.3s ease;
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(-10px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ─── Spinner ──────────────────────────────────────────────────────────────── */
.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ─── Responsive ───────────────────────────────────────────────────────────── */

/* Phone portrait: compact layout */
@media (max-width: 767px) {
  .opponents-area {
    flex-direction: row;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
    justify-content: flex-start;
  }

  .opponent-card {
    flex-shrink: 0;
    min-width: 110px;
  }

  .piles-area { gap: 18px; }

  .pile-card { width: 60px; height: 80px; }

  .mini-card { width: 18px; height: 23px; font-size: 0.48rem; }

  .own-card { font-size: 1.1rem; min-height: 52px; }

  .card-grid { gap: 4px; max-width: 300px; }
}

/* Tablet: bigger cards, side-by-side */
@media (min-width: 768px) and (max-width: 1023px) {
  .own-card { min-height: 78px; font-size: 1.5rem; }
  .card-grid { max-width: 420px; gap: 7px; }

  .opponents-area {
    flex-direction: row;
    flex-wrap: wrap;
  }
  .mini-card { width: 26px; height: 33px; font-size: 0.62rem; }
}

/* Desktop: table layout */
@media (min-width: 1024px) {
  .game-layout {
    flex-direction: column;
  }

  .game-center {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-start;
    justify-content: center;
  }

  .opponents-area {
    flex-direction: row;
    flex-wrap: wrap;
    gap: 12px;
  }

  .opponent-card { min-width: 140px; }

  .own-card { min-height: 90px; font-size: 1.7rem; }
  .card-grid { max-width: 480px; gap: 8px; }

  .mini-card { width: 28px; height: 36px; font-size: 0.65rem; }
  .pile-card { width: 80px; height: 108px; }
}

/* Landscape phone: similar to tablet */
@media (max-height: 500px) and (orientation: landscape) {
  .own-area { padding: 6px 12px 8px; }
  .own-card { min-height: 44px; font-size: 0.95rem; }
  .card-grid { gap: 3px; }
  .game-center { padding: 6px 12px; gap: 8px; }
  .game-header { padding: 5px 12px; min-height: 38px; }
  .action-hint { padding: 5px 12px; min-height: 30px; font-size: 0.78rem; }
  .drawn-card-area { padding: 4px 12px; gap: 8px; }
  .drawn-card-area #drawnCardDisplay { width: 34px; height: 46px; }
  .drawn-card-area #drawnCardValue { font-size: 0.9rem; }
}
