/* ============ Base ============ */
* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

:root {
  --bg: #0f172a;
  --bg2: #1e293b;
  --card: #1e293b;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --primary: #6366f1;
  --primary-hi: #818cf8;
  --ok: #22c55e;
  --pass: #f59e0b;
  --danger: #ef4444;
  --radius: 18px;
}

html, body {
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

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

.hidden { display: none !important; }
.ok-color { color: var(--ok); }
.pass-color { color: var(--pass); }

/* ============ Screens ============ */
.screen {
  display: none;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.screen.active { display: flex; flex-direction: column; }

/* ============ Home ============ */
#screen-home { padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom)); }

.home-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 6px;
}
.logo { font-size: 1.5rem; letter-spacing: -0.5px; }
.logo span {
  background: linear-gradient(90deg, #818cf8, #ec4899);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.header-btns { display: flex; gap: 8px; }

.icon-btn {
  background: var(--card); border: none; color: var(--text);
  width: 42px; height: 42px; border-radius: 12px; font-size: 1.2rem;
  display: flex; align-items: center; justify-content: center;
}
.icon-btn:active { transform: scale(0.93); }

.tagline { color: var(--muted); font-size: 0.9rem; margin-bottom: 14px; }

.deck-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
  overflow-y: auto;
  flex: 1;
  padding-bottom: 12px;
  align-content: start;
}

.deck-card {
  background: var(--card);
  border: 2px solid transparent;
  border-radius: var(--radius);
  padding: 14px 12px;
  text-align: left;
  color: var(--text);
  position: relative;
  transition: transform 0.1s, border-color 0.15s, background 0.15s;
  min-height: 92px;
  display: flex; flex-direction: column; justify-content: space-between; gap: 6px;
}
.deck-card:active { transform: scale(0.96); }
.deck-card .deck-emoji { font-size: 1.7rem; line-height: 1; }
.deck-card .deck-name { font-weight: 700; font-size: 0.95rem; }
.deck-card .deck-count { color: var(--muted); font-size: 0.75rem; }
.deck-card.selected {
  border-color: var(--deck-color, var(--primary));
  background: color-mix(in srgb, var(--deck-color, var(--primary)) 18%, var(--card));
}
.deck-card.selected::after {
  content: "✓";
  position: absolute; top: 8px; right: 10px;
  color: var(--deck-color, var(--primary));
  font-weight: 800; font-size: 1rem;
}
.deck-card .deck-edit {
  position: absolute; bottom: 8px; right: 8px;
  background: none; border: none; font-size: 0.95rem; opacity: 0.7;
  padding: 4px;
}

.home-footer {
  display: flex; gap: 10px; padding-top: 12px;
  border-top: 1px solid #33415555;
}
.home-footer .btn-primary { flex: 1; }

/* ============ Buttons ============ */
.btn {
  border: none; border-radius: 14px;
  padding: 14px 22px;
  font-size: 1rem; font-weight: 700;
  color: var(--text);
}
.btn:active { transform: scale(0.97); }
.btn-primary {
  background: linear-gradient(135deg, var(--primary), #8b5cf6);
  box-shadow: 0 4px 20px #6366f166;
}
.btn-primary:disabled { opacity: 0.35; box-shadow: none; }
.btn-ghost { background: var(--card); }
.btn-big { font-size: 1.3rem; padding: 18px 40px; }
.btn-danger-text { color: var(--danger); }

/* ============ Pre-round ============ */
#screen-preround { align-items: center; justify-content: center; text-align: center; position: relative; }
.back-btn { position: absolute; top: calc(14px + env(safe-area-inset-top)); left: 14px; }
.preround-content { display: flex; flex-direction: column; align-items: center; gap: 14px; padding: 20px; }
.preround-team {
  font-size: 1.1rem; font-weight: 800; color: var(--primary-hi);
  background: var(--card); padding: 8px 18px; border-radius: 999px;
}
.preround-team:empty { display: none; }
.preround-decks { color: var(--muted); font-size: 0.9rem; }
.preround-content h2 { font-size: 1.8rem; }
.preround-sub { color: var(--muted); line-height: 1.6; }
.tilt-warning { color: var(--pass); font-size: 0.85rem; max-width: 320px; }

/* ============ Countdown ============ */
#screen-countdown { align-items: center; justify-content: center; }
.countdown-number {
  font-size: 9rem; font-weight: 900;
  animation: pop 0.9s ease infinite;
}
@keyframes pop {
  0% { transform: scale(0.6); opacity: 0.4; }
  35% { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

/* ============ Play ============ */
#screen-play {
  align-items: center; justify-content: center;
  position: relative; overflow: hidden;
  background: var(--bg);
}
.play-top {
  position: absolute; top: 0; left: 0; right: 0;
  padding: env(safe-area-inset-top) 0 0;
}
.timer-bar { height: 8px; background: #334155; width: 100%; }
.timer-fill {
  height: 100%; width: 100%;
  background: var(--primary-hi);
  transition: width 0.25s linear, background 0.3s;
}
.timer-fill.low { background: var(--danger); }
.play-meta {
  display: flex; justify-content: space-between;
  padding: 8px 18px; font-size: 1.3rem; font-weight: 800;
}
.play-timer { color: var(--muted); font-variant-numeric: tabular-nums; }
.play-score { color: var(--ok); }

.abort-btn-inline {
  background: none; border: none; color: var(--muted);
  font-size: 1.2rem; font-weight: 700;
  padding: 0 14px 0 0; line-height: 1;
  opacity: 0.75;
}
.abort-btn-inline:active { transform: scale(0.9); }
.play-meta .play-timer { margin-right: auto; }

.play-word {
  font-size: clamp(2.2rem, 11vmin, 5.5rem);
  font-weight: 900;
  text-align: center;
  padding: 0 28px;
  line-height: 1.15;
  max-width: 100%;
  overflow-wrap: break-word;
  hyphens: auto;
}

.tap-controls {
  position: absolute; bottom: 0; left: 0; right: 0;
  display: flex; gap: 10px;
  padding: 0 14px calc(14px + env(safe-area-inset-bottom));
}
.tap-btn {
  flex: 1; border: none; border-radius: 16px;
  padding: 20px; font-size: 1.2rem; font-weight: 800; color: #fff;
}
.tap-btn:active { transform: scale(0.97); }
.tap-correct { background: var(--ok); }
.tap-pass { background: var(--pass); }

.feedback-overlay {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: clamp(2.5rem, 12vmin, 6rem); font-weight: 900; color: #fff;
  opacity: 0; pointer-events: none;
  transition: opacity 0.12s;
}
.feedback-overlay.show { opacity: 1; }
.feedback-overlay.ok { background: var(--ok); }
.feedback-overlay.pass { background: var(--pass); }

/* ============ Results ============ */
#screen-results { padding: calc(20px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom)); }
.results-inner {
  max-width: 520px; margin: 0 auto; width: 100%;
  display: flex; flex-direction: column; flex: 1;
}
#results-title { text-align: center; font-size: 1.6rem; }
.results-score {
  text-align: center; font-size: 2.8rem; font-weight: 900;
  color: var(--ok); margin: 6px 0 2px;
}
.results-hint { text-align: center; color: var(--muted); font-size: 0.8rem; margin-bottom: 12px; }
.results-list {
  list-style: none; overflow-y: auto; flex: 1;
  display: flex; flex-direction: column; gap: 6px;
  padding-bottom: 10px;
}
.results-list li {
  background: var(--card);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex; justify-content: space-between; align-items: center;
  font-weight: 600;
  border-left: 4px solid var(--pass);
}
.results-list li.ok { border-left-color: var(--ok); }
.results-list li .mark { font-size: 1.1rem; }
.results-list li.ok .mark { color: var(--ok); }
.results-list li:not(.ok) .mark { color: var(--pass); }

.mini-scoreboard {
  display: flex; gap: 8px; justify-content: center; flex-wrap: wrap;
  margin: 10px 0;
}
.mini-scoreboard .team-chip {
  background: var(--card); border-radius: 999px;
  padding: 6px 14px; font-size: 0.9rem; font-weight: 700;
}
.mini-scoreboard .team-chip b { color: var(--primary-hi); }

.results-btns { display: flex; gap: 10px; padding-top: 8px; }
.results-btns .btn-primary { flex: 1; }

/* ============ Modals ============ */
.modal {
  position: fixed; inset: 0; z-index: 50;
  background: #000a;
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  backdrop-filter: blur(4px);
}
.modal-card {
  background: var(--bg2);
  border-radius: 20px;
  padding: 22px;
  width: 100%; max-width: 440px;
  max-height: 88dvh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 10px;
}
.modal-card h2 { font-size: 1.3rem; margin-bottom: 4px; }

.setting-label {
  font-size: 0.8rem; font-weight: 700; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.5px; margin-top: 6px;
}

.seg { display: flex; background: var(--bg); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button {
  flex: 1; border: none; background: none; color: var(--muted);
  padding: 10px 0; border-radius: 9px; font-weight: 700; font-size: 0.95rem;
}
.seg button.active { background: var(--primary); color: #fff; }

.team-names { display: flex; flex-direction: column; gap: 8px; }

.toggle-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 0; font-weight: 600;
}
.toggle {
  width: 52px; height: 30px; border-radius: 999px; border: none;
  background: #334155; position: relative; transition: background 0.15s;
}
.toggle::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 24px; height: 24px; border-radius: 50%; background: #fff;
  transition: transform 0.15s;
}
.toggle.on { background: var(--ok); }
.toggle.on::after { transform: translateX(22px); }

.input {
  background: var(--bg); border: 1px solid #334155; border-radius: 12px;
  color: var(--text); padding: 12px 14px; font-size: 1rem; font-family: inherit;
  width: 100%;
}
.input:focus { outline: 2px solid var(--primary); border-color: transparent; }
.input-emoji { width: 90px; font-size: 1.4rem; text-align: center; }
.textarea { resize: vertical; min-height: 120px; user-select: text; -webkit-user-select: text; }
.input { user-select: text; -webkit-user-select: text; }

.modal-btns { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; flex-wrap: wrap; }

.modal-card-sm { max-width: 340px; text-align: center; }
.confirm-text { color: var(--muted); font-size: 0.95rem; }
.modal-card-sm .modal-btns { justify-content: center; }
.modal-card-sm .modal-btns .btn { flex: 1; }

.btn-sm { padding: 10px 16px; font-size: 0.9rem; }

.tilt-readout {
  background: var(--bg); border-radius: 12px; padding: 12px 14px;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 0.82rem; line-height: 1.7; color: var(--muted);
  white-space: pre-wrap;
}
.tilt-readout b { color: var(--text); }
.tilt-readout .good { color: var(--ok); font-weight: 700; }
.tilt-readout .bad { color: var(--danger); font-weight: 700; }
.tilt-readout .warn { color: var(--pass); font-weight: 700; }
.tilt-bar {
  height: 10px; background: #334155; border-radius: 999px;
  position: relative; margin: 8px 0 4px; overflow: hidden;
}
.tilt-bar i {
  position: absolute; top: 0; bottom: 0; width: 4px; margin-left: -2px;
  background: var(--primary-hi); border-radius: 2px;
  transition: left 0.06s linear;
}
.tilt-bar u {
  position: absolute; top: 0; bottom: 0; width: 1px; left: 50%;
  background: #64748b; text-decoration: none;
}

.scoreboard-content { display: flex; flex-direction: column; gap: 8px; }
.scoreboard-row {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--bg); border-radius: 12px; padding: 12px 16px; font-weight: 700;
}
.scoreboard-row .pts { color: var(--primary-hi); font-size: 1.2rem; }
.scoreboard-row.leader { border: 1px solid var(--ok); }
.scoreboard-empty { color: var(--muted); text-align: center; padding: 12px 0; }

/* ============ Toast ============ */
.toast {
  position: fixed; z-index: 80;
  left: 50%; transform: translateX(-50%) translateY(20px);
  bottom: calc(96px + env(safe-area-inset-bottom));
  background: #0f172aee; border: 1px solid #475569;
  color: var(--text); font-size: 0.9rem; font-weight: 600;
  padding: 12px 18px; border-radius: 999px;
  max-width: 90vw; text-align: center;
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ============ Landscape play tweaks ============ */
@media (orientation: landscape) {
  .play-word { font-size: clamp(2.5rem, 14vmin, 7rem); }
}
