/* =============================================
   TongaChase — Mobile-First Responsive CSS
   Sky-Blue + White PWA Theme
   ============================================= */

/* ── Variables ── */
:root {
  --sky: #87CEEB;
  --sky-dark: #5BA3C9;
  --sky-light: #D4F1FF;
  --sky-pale: #EAF7FF;
  --white: #FFFFFF;
  --text: #2C3E50;
  --text-light: #6B8299;
  --gold: #FFD700;
  --danger: #E74C3C;
  --success: #27AE60;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ── Reset ── */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: linear-gradient(170deg, var(--white) 0%, var(--sky-pale) 40%, var(--sky-light) 100%);
  color: var(--text);
  user-select: none; -webkit-user-select: none;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: contain;
}

/* ══════════════════════════════════════════
   SCREENS — Full viewport containers
   ══════════════════════════════════════════ */
.screen {
  position: fixed; inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-top: max(16px, var(--safe-top));
  padding-bottom: max(16px, var(--safe-bottom));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.screen.active { display: flex; }

/* ══════════════════════════════════════════
   TITLE SCREEN
   ══════════════════════════════════════════ */
.title-area {
  text-align: center;
  margin-bottom: 24px;
  flex-shrink: 0;
}

/* Title letters container */
#title-text {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 2px;
  margin-bottom: 8px;
  min-height: 50px;
}
.title-letter {
  display: inline-block;
  font-size: clamp(32px, 10vw, 52px);
  font-weight: 900;
  color: var(--sky-dark);
  text-shadow: 2px 3px 0 rgba(0,0,0,0.08);
  opacity: 0;
  animation: letterDrop 0.7s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
.title-letter.gold {
  color: var(--gold);
  text-shadow: 2px 3px 0 rgba(180,140,0,0.3);
}
@keyframes letterDrop {
  0%   { opacity: 0; transform: translateY(-60px) scale(0.2) rotate(-15deg); }
  60%  { opacity: 1; transform: translateY(4px) scale(1.08) rotate(2deg); }
  80%  { transform: translateY(-2px) scale(0.97) rotate(-1deg); }
  100% { opacity: 1; transform: translateY(0) scale(1) rotate(0); }
}
#title-text.done .title-letter {
  opacity: 1;
  animation: letterPulse 3s ease-in-out infinite;
}
@keyframes letterPulse {
  0%, 100% { text-shadow: 2px 3px 0 rgba(0,0,0,0.08); transform: scale(1); }
  50% { text-shadow: 0 0 24px rgba(135,206,235,0.8), 0 0 48px rgba(255,215,0,0.3), 2px 3px 0 rgba(0,0,0,0.08); transform: scale(1.06); }
}
.subtitle {
  font-size: clamp(11px, 3vw, 14px);
  color: var(--text-light);
  letter-spacing: 2px;
  text-transform: uppercase;
  margin-top: 4px;
}

/* ── Form Area (Title Screen) ── */
.form-area {
  background: var(--white);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: 0 8px 32px rgba(135,206,235,0.25);
  width: 100%;
  max-width: 340px;
  flex-shrink: 0;
}
.form-area input[type="text"] {
  width: 100%;
  padding: 12px 14px;
  border: 2px solid var(--sky-light);
  border-radius: 10px;
  font-size: 16px;
  outline: none;
  background: var(--sky-pale);
  color: var(--text);
  transition: border-color 0.2s;
  -webkit-appearance: none;
  appearance: none;
}
.form-area input[type="text"]:focus {
  border-color: var(--sky);
}
#input-code {
  text-transform: uppercase;
  letter-spacing: 6px;
  font-weight: 700;
  text-align: center;
}
.or-divider {
  text-align: center;
  margin: 14px 0;
  color: var(--text-light);
  font-size: 13px;
  position: relative;
}
.or-divider::before, .or-divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 25%;
  height: 1px;
  background: var(--sky-light);
}
.or-divider::before { left: 5%; }
.or-divider::after { right: 5%; }
#error-msg {
  color: var(--danger);
  font-size: 13px;
  margin-top: 10px;
  min-height: 18px;
  text-align: center;
}

/* ══════════════════════════════════════════
   BUTTONS
   ══════════════════════════════════════════ */
.btn {
  padding: 13px 24px;
  border: none;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s;
  width: 100%;
  margin-top: 10px;
  letter-spacing: 0.5px;
  -webkit-appearance: none;
  appearance: none;
}
.btn-primary {
  background: var(--sky);
  color: var(--white);
  box-shadow: 0 4px 12px rgba(135,206,235,0.4);
}
.btn-primary:active {
  background: var(--sky-dark);
  transform: scale(0.97);
}
.btn-secondary {
  background: var(--white);
  color: var(--sky-dark);
  border: 2px solid var(--sky);
}
.btn-secondary:active {
  background: var(--sky-pale);
}
.btn:disabled {
  opacity: 0.5;
  pointer-events: none;
}

/* ══════════════════════════════════════════
   CHARACTER SELECT SCREEN
   ══════════════════════════════════════════ */
#screen-charselect {
  justify-content: flex-start;
  padding-top: max(12px, var(--safe-top));
}
#screen-charselect h2 {
  font-size: clamp(18px, 5vw, 22px);
  margin-bottom: 4px;
  flex-shrink: 0;
}
.cs-code {
  font-size: 14px;
  color: var(--text-light);
  margin-bottom: 10px;
  flex-shrink: 0;
}
.cs-code span {
  display: inline-block;
  background: var(--sky);
  color: var(--white);
  padding: 2px 10px;
  border-radius: 6px;
  font-weight: 700;
  letter-spacing: 3px;
  font-size: 16px;
}
.char-grid {
  flex: 1;
  overflow-y: auto;
  width: 100%;
  max-width: 420px;
  -webkit-overflow-scrolling: touch;
  padding: 4px 0 12px;
  min-height: 0; /* flex scroll fix */
}
.char-group {
  margin-bottom: 10px;
}
.group-label {
  font-size: 12px;
  color: var(--text-light);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 6px;
  font-weight: 600;
}
.char-row {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
  gap: 6px;
}
.char-card {
  background: var(--white);
  border: 2px solid var(--sky-light);
  border-radius: 10px;
  padding: 6px 4px 5px;
  text-align: center;
  cursor: pointer;
  transition: all 0.15s;
}
.char-card.selected {
  border-color: var(--sky);
  background: var(--sky-pale);
  box-shadow: 0 0 0 3px rgba(135,206,235,0.3);
}
.char-card canvas {
  display: block;
  margin: 0 auto 3px;
  image-rendering: pixelated;
  width: 48px;
  height: 48px;
}
.char-card .char-name {
  font-size: 9px;
  color: var(--text-light);
  font-weight: 600;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#btn-ready {
  max-width: 420px;
  flex-shrink: 0;
}

/* ══════════════════════════════════════════
   LOBBY SCREEN
   ══════════════════════════════════════════ */
#screen-lobby h2 {
  font-size: clamp(18px, 5vw, 22px);
  margin-bottom: 4px;
}
.lobby-code {
  font-size: 14px;
  color: var(--text-light);
  margin-bottom: 16px;
}
.lobby-code span {
  display: inline-block;
  background: var(--sky);
  color: var(--white);
  padding: 4px 16px;
  border-radius: 8px;
  letter-spacing: 4px;
  font-size: 22px;
  font-weight: 700;
}
.player-list {
  width: 100%;
  max-width: 360px;
  max-height: 50vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.player-item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--white);
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.player-item canvas {
  image-rendering: pixelated;
  border-radius: 6px;
  background: var(--sky-pale);
  flex-shrink: 0;
}
.player-item .pi-name {
  flex: 1;
  font-weight: 600;
  font-size: 15px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.player-item .pi-host {
  font-size: 11px;
  color: var(--gold);
  font-weight: 700;
  flex-shrink: 0;
}
#lobby-wait {
  font-size: 13px;
  color: var(--text-light);
  margin-top: 10px;
  text-align: center;
}
#btn-start {
  max-width: 360px;
}

/* ══════════════════════════════════════════
   GAME SCREEN (Canvas fills viewport)
   ══════════════════════════════════════════ */
#screen-game {
  padding: 0;
  background: #5BA3C9;
  overflow: hidden;
}
#game-canvas {
  position: absolute;
  inset: 0;
  width: 100% !important;
  height: 100% !important;
  display: block;
  image-rendering: pixelated;
  touch-action: none;
}

/* ── Countdown Overlay ── */
#countdown-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.5);
  z-index: 100;
}
#countdown-num {
  font-size: clamp(80px, 25vw, 140px);
  font-weight: 900;
  color: var(--white);
  text-shadow: 0 0 40px rgba(135,206,235,0.8);
  animation: countPop 0.8s ease-out;
}
@keyframes countPop {
  0% { transform: scale(2); opacity: 0; }
  50% { transform: scale(0.9); opacity: 1; }
  100% { transform: scale(1); }
}

/* ══════════════════════════════════════════
   RESULTS SCREEN
   ══════════════════════════════════════════ */
#screen-results {
  justify-content: flex-start;
  padding-top: max(20px, var(--safe-top));
}
#results-title {
  font-size: clamp(22px, 7vw, 32px);
  margin-bottom: 12px;
  color: var(--sky-dark);
  text-align: center;
  flex-shrink: 0;
}
#results-team-info {
  text-align: center;
  font-size: 15px;
  margin-bottom: 12px;
  color: var(--text-light);
  flex-shrink: 0;
}
.results-list {
  width: 100%;
  max-width: 380px;
  max-height: 50vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  margin-bottom: 12px;
}
.result-item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--white);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}
.result-item.winner {
  border: 2px solid var(--gold);
  background: #FFFDE7;
}
.result-rank {
  font-size: 18px;
  font-weight: 900;
  color: var(--sky-dark);
  width: 28px;
  text-align: center;
  flex-shrink: 0;
}
.result-info {
  flex: 1;
  min-width: 0;
}
.result-name {
  font-weight: 700;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.result-detail {
  font-size: 11px;
  color: var(--text-light);
}
.result-badge {
  font-size: 10px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 6px;
  white-space: nowrap;
  flex-shrink: 0;
}
.badge-escaped { background: #E8F5E9; color: var(--success); }
.badge-caught  { background: #FFEBEE; color: var(--danger); }
.badge-survived { background: var(--sky-pale); color: var(--sky-dark); }
.badge-chaser  { background: #FFEBEE; color: var(--danger); }
.results-buttons {
  display: flex;
  gap: 10px;
  width: 100%;
  max-width: 380px;
  flex-shrink: 0;
}
.results-buttons .btn {
  flex: 1;
}

/* ══════════════════════════════════════════
   SCROLLBAR
   ══════════════════════════════════════════ */
.char-grid::-webkit-scrollbar,
.player-list::-webkit-scrollbar,
.results-list::-webkit-scrollbar {
  width: 4px;
}
.char-grid::-webkit-scrollbar-track,
.player-list::-webkit-scrollbar-track,
.results-list::-webkit-scrollbar-track {
  background: transparent;
}
.char-grid::-webkit-scrollbar-thumb,
.player-list::-webkit-scrollbar-thumb,
.results-list::-webkit-scrollbar-thumb {
  background: var(--sky);
  border-radius: 2px;
}

/* ══════════════════════════════════════════
   RESPONSIVE — Small phones (≤360px wide)
   ══════════════════════════════════════════ */
@media (max-width: 360px) {
  .form-area { padding: 14px; }
  .form-area input[type="text"] { padding: 10px 12px; font-size: 15px; }
  .btn { padding: 11px 18px; font-size: 15px; }
  .char-row { grid-template-columns: repeat(auto-fill, minmax(60px, 1fr)); gap: 4px; }
  .char-card { padding: 4px 2px 3px; border-radius: 8px; }
  .char-card canvas { width: 40px; height: 40px; }
  .char-card .char-name { font-size: 8px; }
  .result-item { padding: 8px 10px; gap: 8px; }
}

/* ── Short phones (landscape or short viewport) ── */
@media (max-height: 580px) {
  .title-area { margin-bottom: 12px; }
  .form-area { padding: 12px; }
  .form-area input[type="text"] { padding: 10px 12px; }
  .btn { padding: 10px 18px; margin-top: 8px; }
  .or-divider { margin: 8px 0; }
  #screen-charselect { padding-top: 8px; }
  #screen-charselect h2 { margin-bottom: 2px; }
  .cs-code { margin-bottom: 6px; }
  .char-group { margin-bottom: 6px; }
}

/* ── Tablets / wider screens ── */
@media (min-width: 600px) {
  .form-area { max-width: 400px; padding: 28px; }
  .char-row { grid-template-columns: repeat(auto-fill, minmax(80px, 1fr)); }
  .char-grid { max-width: 540px; }
  .player-list { max-width: 420px; }
  .results-list { max-width: 440px; }
  .results-buttons { max-width: 440px; }
  #btn-ready { max-width: 540px; }
  #btn-start { max-width: 420px; }
}

/* ── Desktop / large screens ── */
@media (min-width: 900px) {
  .char-row { grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); gap: 10px; }
  .char-grid { max-width: 700px; }
}

/* ── Landscape orientation on mobile ── */
@media (orientation: landscape) and (max-height: 500px) {
  .screen { padding: 8px 16px; }
  #screen-title { flex-direction: row; gap: 24px; }
  .title-area { margin-bottom: 0; }
  .form-area { max-width: 300px; }
  #screen-charselect { flex-direction: column; }
  .char-row { grid-template-columns: repeat(auto-fill, minmax(56px, 1fr)); gap: 4px; }
  .char-card canvas { width: 36px; height: 36px; }
}
