:root {
  --bg: #16171d;
  --surface: #1f2129;
  --surface-2: #262835;
  --border: #34374a;
  --text: #f0f1f6;
  --muted: #9497a8;
  --primary: #ff5d73;
  --primary-dark: #e14d62;
  --secondary: #5da9ff;
  --success: #4fd18b;
  --danger: #ff5d73;
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  min-height: 100%;
}

body {
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

h1, h2, h3 { margin: 0 0 0.4em; }
p { margin: 0.3em 0; }
.muted { color: var(--muted); }
.hidden { display: none !important; }

button {
  font-family: inherit;
  cursor: pointer;
}

.btn {
  border: none;
  border-radius: 999px;
  padding: 12px 20px;
  font-size: 1rem;
  font-weight: 600;
  transition: transform 0.05s ease, opacity 0.15s ease;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-small { padding: 8px 14px; font-size: 0.85rem; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: var(--secondary); color: #08182b; }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.link-button {
  background: none; border: none; color: var(--secondary);
  font-weight: 600; text-decoration: underline; padding: 0; font-size: inherit;
}

input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 1rem;
  margin-bottom: 14px;
}
input:focus { outline: 2px solid var(--secondary); }
label { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 6px; }
.room-code-input { letter-spacing: 0.3em; text-transform: uppercase; text-align: center; font-size: 1.4rem; font-weight: 700; }

.view {
  max-width: 720px;
  margin: 0 auto;
  padding: 20px 16px 40px;
  min-height: 100vh;
}

/* ---------- Portal ---------- */
.portal-header { text-align: center; margin-bottom: 24px; }
.portal-header h1 { font-size: 2rem; }
.subtitle { color: var(--muted); }
.portal-footer { text-align: center; margin-top: 24px; color: var(--muted); }

.game-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}

.game-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  text-align: left;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 150px;
}
.game-card:active { transform: scale(0.98); }
.game-card h3 { font-size: 1.1rem; }
.game-card .desc { color: var(--muted); font-size: 0.9rem; flex: 1; }
.game-card .players { font-size: 0.8rem; color: var(--secondary); font-weight: 600; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  z-index: 50;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px 20px;
  width: 100%;
  max-width: 420px;
  position: relative;
  border: 1px solid var(--border);
}
.modal-close {
  position: absolute; top: 10px; right: 14px;
  background: none; border: none; color: var(--muted);
  font-size: 1.6rem; line-height: 1;
}
.modal-actions { display: flex; gap: 10px; margin-top: 8px; flex-wrap: wrap; }
.modal-actions .btn { flex: 1; }
.form-error { color: var(--danger); min-height: 1.2em; font-size: 0.9rem; }

/* ---------- Lobby ---------- */
.lobby-header, .game-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 20px;
}
.room-code-label { font-size: 0.8rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.1em; }
.room-code-display {
  font-size: 2.4rem; font-weight: 800; letter-spacing: 0.12em;
  color: var(--primary);
}
.game-room-code { font-weight: 700; letter-spacing: 0.1em; color: var(--muted); }

.player-list-section, .lobby-options, .chat-section { margin-bottom: 22px; }
.player-list { list-style: none; padding: 0; margin: 0 0 12px; display: flex; flex-direction: column; gap: 8px; }
.player-item {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 12px;
}
.player-item .name { flex: 1; font-weight: 600; }
.tag {
  font-size: 0.7rem; padding: 2px 8px; border-radius: 999px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.05em;
}
.tag-host { background: var(--primary); color: #fff; }
.tag-bot { background: var(--secondary); color: #08182b; }
.tag-offline { background: var(--border); color: var(--muted); }
.tag-czar { background: #ffd166; color: #3a2900; }
.tag-you { background: var(--success); color: #05271a; }
.kick-btn { background: none; border: none; color: var(--danger); font-size: 0.8rem; font-weight: 700; }

.options-fields { display: flex; flex-direction: column; gap: 12px; }
.option-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
.option-row label { margin: 0; color: var(--text); font-size: 0.95rem; }
.option-row input[type="number"] { width: 80px; margin: 0; padding: 8px; }
.option-row select { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 6px; }
.option-row .pack-list { display: flex; flex-wrap: wrap; gap: 8px; }
.option-row .pack-list label { display: flex; align-items: center; gap: 4px; background: var(--surface-2); padding: 6px 10px; border-radius: 999px; font-size: 0.85rem; }
.option-row input[type="checkbox"] { width: auto; margin: 0; }

.lobby-start-row { display: flex; flex-direction: column; align-items: center; gap: 10px; margin-bottom: 22px; text-align: center; }

.chat-log {
  height: 140px; overflow-y: auto; background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px;
  font-size: 0.85rem; margin-bottom: 8px; display: flex; flex-direction: column; gap: 4px;
}
.chat-msg .who { color: var(--secondary); font-weight: 700; }
.chat-form { display: flex; gap: 8px; }
.chat-form input { margin-bottom: 0; }

/* ---------- Game (CAH) ---------- */
.black-card, .white-card {
  border-radius: 12px;
  padding: 18px;
  font-weight: 700;
  min-height: 90px;
  display: flex;
  align-items: center;
  line-height: 1.35;
}
.black-card { background: #101014; color: #fff; border: 1px solid #000; margin-bottom: 16px; font-size: 1.15rem; }
.white-card {
  background: #fff; color: #111; font-size: 1rem;
  border: 2px solid transparent; cursor: pointer;
  transition: border-color 0.1s ease, transform 0.05s ease;
}
.white-card.selected { border-color: var(--primary); transform: translateY(-3px); }
.white-card.disabled { opacity: 0.5; cursor: default; }

.hand-grid, .submissions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}

.timer-bar { height: 6px; background: var(--border); border-radius: 4px; overflow: hidden; margin-bottom: 14px; }
.timer-bar-fill { height: 100%; background: var(--secondary); transition: width 1s linear; }

.status-banner {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px 14px; margin-bottom: 16px; text-align: center;
}
.status-banner.czar { border-color: #ffd166; }

.scoreboard { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.score-row { display: flex; justify-content: space-between; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; }
.score-row .pts { font-weight: 700; color: var(--primary); }

.winner-banner {
  text-align: center; padding: 20px; background: var(--surface);
  border: 1px solid var(--success); border-radius: 12px; margin-bottom: 16px;
}
.winner-banner .trophy { font-size: 2rem; }

.toast-stack {
  position: fixed; top: 12px; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px; z-index: 100; width: calc(100% - 32px); max-width: 420px;
}
.toast {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 14px; font-size: 0.9rem;
  box-shadow: 0 4px 14px rgba(0,0,0,0.3);
}
.toast.error { border-color: var(--danger); }
.toast.success { border-color: var(--success); }

/* ---------- Game (UNO) ---------- */
.uno-turn-order { margin-bottom: 14px; }
.uno-turn-order-dir { font-size: 0.8rem; margin-bottom: 6px; text-align: center; }
.uno-turn-order-row {
  display: flex; gap: 8px; overflow-x: auto; padding: 2px;
}
.uno-turn-chip {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  flex: 0 0 auto; background: var(--surface); border: 2px solid var(--border);
  border-radius: 10px; padding: 6px 12px; font-size: 0.85rem; opacity: 0.7;
  transition: transform 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
}
.uno-turn-chip.next { opacity: 0.9; border-color: var(--secondary); }
.uno-turn-chip.current {
  opacity: 1; border-color: var(--primary); background: var(--surface-2);
  transform: translateY(-3px); box-shadow: 0 4px 14px rgba(255, 93, 115, 0.35);
}
.uno-turn-chip-label { font-weight: 700; white-space: nowrap; }
.uno-turn-chip-tag {
  font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.05em;
  color: var(--muted);
}
.uno-turn-chip.current .uno-turn-chip-tag { color: var(--primary); font-weight: 700; }
.uno-turn-chip.next .uno-turn-chip-tag { color: var(--secondary); }

.uno-table {
  display: flex; align-items: center; justify-content: center; gap: 18px;
  flex-wrap: wrap; margin-bottom: 16px; text-align: center;
}
.uno-pile { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.uno-card-back {
  width: 70px; height: 100px; border-radius: 10px;
  background: repeating-linear-gradient(135deg, #2a2c38, #2a2c38 6px, #34374a 6px, #34374a 12px);
  border: 2px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; color: var(--muted); font-size: 1.1rem;
}
.uno-color-indicator { display: flex; align-items: center; gap: 8px; }
.uno-color-dot {
  display: inline-block; width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid var(--border);
}
.uno-color-dot.red { background: #e63946; }
.uno-color-dot.yellow { background: #f4c542; }
.uno-color-dot.green { background: #2a9d5c; }
.uno-color-dot.blue { background: #3a7bd5; }

.uno-card {
  width: 70px; height: 100px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 1.1rem; color: #fff;
  border: 3px solid rgba(255,255,255,0.35);
  cursor: pointer; transition: transform 0.05s ease, border-color 0.1s ease;
  padding: 6px; text-align: center;
}
.uno-card.big { width: 90px; height: 128px; font-size: 1.3rem; }
.uno-card.red { background: #e63946; }
.uno-card.yellow { background: #dcae1f; }
.uno-card.green { background: #2a9d5c; }
.uno-card.blue { background: #3a7bd5; }
.uno-card.wild { background: linear-gradient(135deg, #e63946 25%, #dcae1f 25% 50%, #2a9d5c 50% 75%, #3a7bd5 75%); }
.uno-card.selected { border-color: #fff; transform: translateY(-4px); }
.uno-card.disabled { opacity: 0.4; cursor: default; }
.uno-card-label { word-break: break-word; }

.uno-hand { grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); justify-items: center; }

.uno-actions { display: flex; justify-content: center; margin-bottom: 14px; }

.uno-badge {
  display: inline-block; background: var(--primary); color: #fff;
  font-weight: 800; padding: 4px 12px; border-radius: 999px;
  font-size: 0.85rem; margin-bottom: 10px; letter-spacing: 0.05em;
}

.uno-color-picker { text-align: center; margin: 14px 0; }
.uno-color-picker-row { display: flex; justify-content: center; gap: 10px; }
.uno-color-btn {
  width: 44px; height: 44px; border-radius: 50%; border: 2px solid rgba(255,255,255,0.4);
}
.uno-color-btn.red { background: #e63946; }
.uno-color-btn.yellow { background: #dcae1f; }
.uno-color-btn.green { background: #2a9d5c; }
.uno-color-btn.blue { background: #3a7bd5; }

/* ---------- Game (Phase 10) ---------- */
.p10-phase-guide-toggle { display: flex; justify-content: center; margin-bottom: 10px; }
.p10-phase-guide {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px; margin-bottom: 16px; display: flex; flex-direction: column; gap: 4px;
}
.p10-phase-guide-row {
  display: flex; align-items: center; gap: 10px; padding: 6px 8px; border-radius: 8px;
}
.p10-phase-guide-row.mine { background: var(--surface-2); border: 1px solid var(--primary); }
.p10-phase-guide-num {
  flex: 0 0 auto; width: 22px; height: 22px; border-radius: 50%; background: var(--surface-2);
  display: flex; align-items: center; justify-content: center; font-size: 0.75rem; font-weight: 800; color: var(--muted);
}
.p10-phase-guide-row.mine .p10-phase-guide-num { background: var(--primary); color: #fff; }
.p10-phase-guide-label { flex: 1; font-size: 0.9rem; }
.p10-phase-guide-players { font-size: 0.75rem; text-align: right; white-space: nowrap; }

.p10-rail { margin-bottom: 14px; }
.p10-rail-row { display: flex; gap: 8px; overflow-x: auto; padding: 2px; }
.p10-chip {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  flex: 0 0 auto; background: var(--surface); border: 2px solid var(--border);
  border-radius: 10px; padding: 6px 12px; font-size: 0.8rem; opacity: 0.7;
  transition: transform 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
}
.p10-chip.next { opacity: 0.9; border-color: var(--secondary); }
.p10-chip.current {
  opacity: 1; border-color: var(--primary); background: var(--surface-2);
  transform: translateY(-3px); box-shadow: 0 4px 14px rgba(255, 93, 115, 0.35);
}
.p10-chip.skip-pending { opacity: 1; border-color: var(--danger); }
.p10-chip-label { font-weight: 700; white-space: nowrap; }
.p10-chip-phase { font-size: 0.7rem; color: var(--muted); }
.p10-chip-tag { font-size: 0.6rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.p10-chip.current .p10-chip-tag { color: var(--primary); font-weight: 700; }
.p10-chip.next .p10-chip-tag { color: var(--secondary); }
.p10-chip.skip-pending .p10-chip-tag { color: var(--danger); font-weight: 700; }

.p10-table {
  display: flex; align-items: center; justify-content: center; gap: 18px;
  flex-wrap: wrap; margin-bottom: 16px; text-align: center;
}
.p10-pile { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.p10-pile.clickable { cursor: pointer; }
.p10-card-back {
  width: 70px; height: 100px; border-radius: 10px;
  background: repeating-linear-gradient(135deg, #2a2c38, #2a2c38 6px, #34374a 6px, #34374a 12px);
  border: 2px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; color: var(--muted); font-size: 1.1rem;
}
.p10-empty-pile {
  width: 70px; height: 100px; border-radius: 10px; border: 2px dashed var(--border);
  display: flex; align-items: center; justify-content: center; text-align: center; font-size: 0.75rem; padding: 4px;
}

.p10-card {
  width: 60px; height: 86px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 1.3rem; color: #fff;
  border: 3px solid rgba(255,255,255,0.35);
  cursor: pointer; transition: transform 0.05s ease, border-color 0.1s ease;
  padding: 4px; text-align: center; position: relative;
}
.p10-card.big { width: 90px; height: 128px; font-size: 1.6rem; }
.p10-card.small { width: 44px; height: 62px; font-size: 0.95rem; }
.p10-card.red { background: #e63946; }
.p10-card.yellow { background: #dcae1f; }
.p10-card.green { background: #2a9d5c; }
.p10-card.blue { background: #3a7bd5; }
.p10-card.wild { background: linear-gradient(135deg, #e63946 25%, #dcae1f 25% 50%, #2a9d5c 50% 75%, #3a7bd5 75%); }
.p10-card.skip { background: #2a2c38; border-color: rgba(255,255,255,0.15); font-size: 1.4rem; }
.p10-card.selected { border-color: #fff; transform: translateY(-4px); }
.p10-card.disabled { opacity: 0.4; cursor: default; }
.p10-card.drawn { box-shadow: 0 0 0 3px var(--secondary), 0 4px 14px rgba(93, 169, 255, 0.5); }
.p10-card-label { word-break: break-word; }
.p10-card-badge {
  position: absolute; top: -6px; right: -6px; background: var(--primary); color: #fff;
  border-radius: 999px; width: 20px; height: 20px; font-size: 0.7rem; font-weight: 800;
  display: flex; align-items: center; justify-content: center; border: 2px solid var(--bg);
}

.p10-hand { grid-template-columns: repeat(auto-fill, minmax(60px, 1fr)); justify-items: center; }

.p10-sort-row { display: flex; align-items: center; justify-content: center; gap: 6px; margin-bottom: 10px; flex-wrap: wrap; }

.p10-controls { margin-bottom: 14px; }
.p10-mode-row { display: flex; justify-content: center; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.p10-lay-panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px; text-align: center; margin-bottom: 8px;
}
.p10-lay-panel .btn { margin-top: 8px; }
.p10-lay-warning { color: var(--danger); font-size: 0.85rem; }

.p10-phase-laid-banner {
  background: rgba(79, 209, 139, 0.15); border: 1px solid var(--success); color: var(--success);
  border-radius: 10px; padding: 8px 12px; margin-bottom: 10px; text-align: center; font-weight: 700; font-size: 0.9rem;
}

.p10-laid-wrap { margin-bottom: 16px; }
.p10-owner-block {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px; margin-bottom: 10px;
}
.p10-owner-name { font-weight: 700; margin-bottom: 6px; }
.p10-groups-row { display: flex; gap: 10px; flex-wrap: wrap; }
.p10-group-chip {
  display: flex; gap: 4px; padding: 6px; border-radius: 10px; border: 2px solid var(--border);
  background: var(--surface-2);
}
.p10-group-chip.hittable { border-color: var(--success); cursor: pointer; }
.p10-group-cards { display: flex; gap: 4px; }

@media (min-width: 640px) {
  .view { padding-top: 40px; }
}
