:root {
  --bg: #0f172a;
  --bg-elevated: #1a2338;
  --card: #1e293b;
  --text: #f1f5f9;
  --text-dim: #94a3b8;
  --blue: #2563eb;
  --red: #dc2626;
  --green: #16a34a;
  --gold: #f59e0b;
  --radius: 16px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  height: 100%;
  overscroll-behavior-y: contain;
}

#app {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.hidden { display: none !important; }

/* ---------- LOGIN ---------- */
#view-login {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  padding-top: calc(24px + var(--safe-top));
  background: radial-gradient(circle at top, #1e293b, #0f172a 65%);
}

.login-card {
  width: 100%;
  max-width: 380px;
}

.brand { text-align: center; margin-bottom: 28px; }
.brand-flag { font-size: 42px; }
.brand h1 { margin: 8px 0 4px; font-size: 26px; letter-spacing: 0.5px; }
.tagline { color: var(--text-dim); font-size: 14px; margin: 0; }

.team-select {
  display: flex;
  gap: 12px;
  margin-bottom: 20px;
}

.team-btn {
  flex: 1;
  padding: 16px 8px;
  border-radius: var(--radius);
  border: 2px solid transparent;
  background: var(--card);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
}

.team-btn .dot {
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--text-dim);
}

.team-btn.team-blue .dot { background: var(--blue); }
.team-btn.team-red .dot { background: var(--red); }

.team-btn.selected { border-color: currentColor; }
.team-btn.team-blue.selected { color: var(--blue); box-shadow: 0 0 0 1px var(--blue) inset; }
.team-btn.team-red.selected { color: var(--red); box-shadow: 0 0 0 1px var(--red) inset; }

#login-form { display: flex; flex-direction: column; gap: 12px; }

input[type="password"], input[type="text"] {
  width: 100%;
  padding: 14px 16px;
  border-radius: 12px;
  border: 1px solid #334155;
  background: var(--card);
  color: var(--text);
  font-size: 16px;
}

.primary-btn {
  padding: 14px;
  border: none;
  border-radius: 12px;
  background: var(--blue);
  color: white;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}
.primary-btn:disabled { opacity: 0.5; }

.secondary-btn {
  padding: 12px;
  border: 1px solid #334155;
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.error-msg { color: #f87171; font-size: 13px; min-height: 16px; margin: 0; }

.admin-select { text-align: center; margin-top: 18px; }
.admin-link-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 13px;
  text-decoration: underline;
  cursor: pointer;
  padding: 6px;
}
.admin-link-btn.selected { color: var(--gold); font-weight: 700; }

/* ---------- APP SHELL ---------- */
#view-app { flex: 1; display: flex; flex-direction: column; }

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  padding-top: calc(14px + var(--safe-top));
  background: var(--bg-elevated);
  position: sticky; top: 0; z-index: 10;
}

.header-team { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; }
.header-team .dot { width: 14px; height: 14px; border-radius: 50%; }

.icon-btn {
  background: transparent; border: none; color: var(--text-dim);
  font-size: 20px; cursor: pointer; padding: 4px 8px;
}

.score-strip {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  background: var(--bg-elevated);
}

.score-chip {
  flex: 1;
  border-radius: 12px;
  padding: 10px 12px;
  background: var(--card);
  border-left: 4px solid var(--text-dim);
}
.score-chip .name { font-size: 12px; color: var(--text-dim); }
.score-chip .pts { font-size: 20px; font-weight: 800; }

.tabs {
  display: flex;
  background: var(--bg-elevated);
  border-bottom: 1px solid #263349;
  position: sticky;
  top: 68px;
  z-index: 9;
}
.tab-btn {
  flex: 1;
  padding: 12px 8px;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 14px;
  font-weight: 600;
  border-bottom: 3px solid transparent;
  cursor: pointer;
}
.tab-btn.active { color: var(--text); border-color: var(--blue); }

.tab-panel { display: none; padding: 16px; padding-bottom: calc(24px + var(--safe-bottom)); flex: 1; }
.tab-panel.active { display: block; }

.progress-line {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 12px;
}

/* ---------- TURN BANNER ---------- */
.turn-banner {
  background: linear-gradient(135deg, #2563eb, #1d4ed8);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 14px;
}
.turn-banner .turn-emoji { font-size: 22px; }
.turn-banner .turn-sub { display: block; font-weight: 400; font-size: 12px; opacity: 0.85; margin-top: 2px; }

/* ---------- ADMIN PLAYERS ---------- */
.player-team-block { margin-bottom: 18px; }
.player-team-title { font-weight: 800; font-size: 14px; margin-bottom: 8px; display: flex; align-items: center; gap: 8px; }
.player-row {
  display: flex; align-items: center; gap: 10px;
  background: var(--card); border-radius: 12px; padding: 10px 14px; margin-bottom: 8px;
}
.player-row.is-turn { border: 1px solid var(--gold); background: linear-gradient(180deg, #f59e0b22, var(--card)); }
.player-name { flex: 1; font-size: 14px; }
.player-turn-badge { font-size: 11px; color: var(--gold); font-weight: 700; }
.player-set-turn-btn {
  padding: 5px 10px; border-radius: 8px; border: 1px solid #334155; background: transparent;
  color: var(--text-dim); font-size: 11px; cursor: pointer;
}
.player-delete-btn {
  padding: 5px 10px; border-radius: 8px; border: none; background: transparent;
  color: var(--red); font-size: 16px; cursor: pointer; line-height: 1;
}
.player-empty { color: var(--text-dim); font-size: 13px; padding: 4px 2px 14px; }

/* ---------- TIER GROUPS ---------- */
.tier-group { margin-bottom: 22px; }

.tier-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  padding: 0 2px;
}
.tier-header-name { font-weight: 800; font-size: 15px; }
.tier-header-count { font-size: 12px; color: var(--text-dim); }

.tier-locked-banner {
  background: var(--card);
  border: 1px dashed #334155;
  border-radius: var(--radius);
  padding: 16px;
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
  margin-bottom: 4px;
}
.tier-locked-banner b { color: var(--text); display: block; margin-bottom: 4px; font-size: 14px; }
.tier-lock-bar-bg { height: 6px; border-radius: 999px; background: #0f172a; overflow: hidden; margin-top: 10px; }
.tier-lock-bar-fill { height: 100%; border-radius: 999px; background: var(--gold); }

.challenge-card.locked {
  opacity: 0.55;
  filter: grayscale(0.4);
}
.challenge-card.locked .challenge-title::after {
  content: ' 🔒';
}

/* ---------- CHALLENGES ---------- */
.challenge-list { display: flex; flex-direction: column; gap: 12px; }

.challenge-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px 16px;
  display: flex;
  gap: 12px;
  align-items: center;
  border: 1px solid #263349;
}

.challenge-card.done { border-color: var(--green); background: linear-gradient(180deg, #16351f33, var(--card)); }

.challenge-thumb {
  width: 56px; height: 56px; border-radius: 10px;
  object-fit: cover; flex-shrink: 0; background: #0f172a;
  cursor: pointer;
}

.thumb-wrap { position: relative; flex-shrink: 0; width: 56px; height: 56px; }
.thumb-wrap .challenge-thumb { width: 100%; height: 100%; }
.play-badge {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  pointer-events: none;
  font-size: 18px;
  color: white;
  text-shadow: 0 1px 4px rgba(0,0,0,0.8);
  background: rgba(0,0,0,0.15);
  border-radius: 10px;
}

.challenge-check {
  width: 56px; height: 56px; border-radius: 10px;
  flex-shrink: 0; display: flex; align-items: center; justify-content: center;
  background: #0f172a; font-size: 24px; color: var(--text-dim);
  border: 2px dashed #334155;
}

.challenge-body { flex: 1; min-width: 0; }
.challenge-cat { font-size: 11px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.4px; }
.challenge-title { font-weight: 700; font-size: 15px; margin: 2px 0; }
.challenge-desc { font-size: 13px; color: var(--text-dim); line-height: 1.35; }
.challenge-meta { display: flex; gap: 8px; margin-top: 6px; align-items: center; flex-wrap: wrap; }

.points-badge {
  display: inline-block; padding: 3px 9px; border-radius: 999px;
  background: var(--gold); color: #1c1400; font-weight: 800; font-size: 12px;
}
.opponent-badge {
  font-size: 11px; color: var(--text-dim);
}

.challenge-action {
  flex-shrink: 0;
}
.mark-btn {
  padding: 8px 14px;
  border-radius: 10px;
  border: none;
  background: var(--blue);
  color: white;
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
}
.done-label {
  font-size: 12px; font-weight: 700; color: var(--green);
  display: flex; align-items: center; gap: 4px;
}

/* ---------- LEADERBOARD ---------- */
.leaderboard-list { display: flex; flex-direction: column; gap: 14px; }
.lb-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 18px;
  border-left: 6px solid var(--text-dim);
}
.lb-top { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.lb-name { font-weight: 800; font-size: 17px; }
.lb-points { font-size: 28px; font-weight: 900; }
.lb-sub { font-size: 12px; color: var(--text-dim); margin-bottom: 10px; }
.lb-bar-bg { height: 8px; border-radius: 999px; background: #0f172a; overflow: hidden; }
.lb-bar-fill { height: 100%; border-radius: 999px; }

.lb-winner-banner {
  text-align: center;
  padding: 14px;
  border-radius: var(--radius);
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: #1c1400;
  font-weight: 800;
  margin-bottom: 16px;
}

/* ---------- GALLERY ---------- */
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.gallery-item {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  aspect-ratio: 1;
  background: var(--card);
  cursor: pointer;
}
.gallery-item img, .gallery-item video { width: 100%; height: 100%; object-fit: cover; display: block; }
.gallery-item .play-badge { font-size: 28px; }
.gallery-caption {
  position: absolute; left: 0; right: 0; bottom: 0;
  background: linear-gradient(0deg, rgba(0,0,0,0.85), transparent);
  padding: 20px 8px 6px;
  font-size: 11px;
  color: white;
}
.gallery-caption b { display: block; font-size: 12px; }
.empty-state { color: var(--text-dim); text-align: center; padding: 40px 10px; font-size: 14px; }

/* ---------- ADMIN ---------- */
.admin-tab-btn {
  flex: 1;
  padding: 12px 8px;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 14px;
  font-weight: 600;
  border-bottom: 3px solid transparent;
  cursor: pointer;
}
.admin-tab-btn.active { color: var(--text); border-color: var(--gold); }

.admin-add-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 18px;
}
.admin-add-card h3 { margin: 0 0 12px; font-size: 15px; }
#admin-add-form { display: flex; flex-direction: column; gap: 10px; }
#admin-add-form textarea {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid #334155;
  background: #0f172a;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  resize: vertical;
}
#admin-add-form input[type=text], #admin-add-form input[type=number],
#edit-form input[type=text], #edit-form input[type=number], #edit-form textarea {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid #334155;
  background: #0f172a;
  color: var(--text);
  font-size: 14px;
}
#edit-form textarea { font-family: inherit; resize: vertical; }

select {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid #334155;
  background: #0f172a;
  color: var(--text);
  font-size: 14px;
  appearance: none;
  -webkit-appearance: none;
}
.admin-add-row { display: flex; gap: 10px; }
.admin-add-row input { flex: 1; }

.pending-photo {
  width: 72px; height: 72px; border-radius: 10px; object-fit: cover;
  flex-shrink: 0; cursor: pointer;
}
.review-actions { display: flex; flex-direction: column; gap: 6px; }
.approve-btn, .reject-btn, .delete-challenge-btn {
  padding: 8px 12px;
  border-radius: 10px;
  border: none;
  font-weight: 700;
  font-size: 12px;
  cursor: pointer;
}
.approve-btn { background: var(--green); color: white; }
.reject-btn { background: var(--red); color: white; }
.delete-challenge-btn { background: transparent; border: 1px solid var(--red); color: var(--red); }
.edit-challenge-btn { background: var(--blue); color: white; }
.challenge-action { display: flex; flex-direction: column; gap: 6px; }

.tier-admin-card { background: var(--card); border-radius: var(--radius); padding: 16px; }
.tier-admin-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.tier-admin-name { font-weight: 800; font-size: 15px; }
.tier-admin-sub { font-size: 12px; color: var(--text-dim); margin-bottom: 12px; }
.tier-admin-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; flex-wrap: wrap; }
.tier-admin-row label { font-size: 12px; color: var(--text-dim); }
.tier-admin-row input[type=number] {
  width: 80px; padding: 8px 10px; border-radius: 8px; border: 1px solid #334155;
  background: #0f172a; color: var(--text); font-size: 13px;
}
.tier-save-btn {
  padding: 6px 12px; border-radius: 8px; border: none; background: var(--blue);
  color: white; font-size: 12px; font-weight: 700; cursor: pointer;
}

.toggle-switch {
  position: relative; display: inline-block; width: 42px; height: 24px; flex-shrink: 0;
}
.toggle-switch input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; cursor: pointer; inset: 0; background: #334155; border-radius: 999px; transition: 0.15s;
}
.toggle-slider::before {
  content: ''; position: absolute; width: 18px; height: 18px; left: 3px; top: 3px;
  background: white; border-radius: 50%; transition: 0.15s;
}
.toggle-switch input:checked + .toggle-slider { background: var(--green); }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(18px); }

.team-progress-row { display: flex; gap: 10px; margin-top: 4px; flex-wrap: wrap; }
.team-progress-chip { font-size: 11px; padding: 3px 9px; border-radius: 999px; background: #0f172a; }

.status-pill {
  display: inline-block; padding: 3px 9px; border-radius: 999px;
  font-weight: 700; font-size: 11px;
}
.status-pill.pending { background: #f59e0b33; color: #f59e0b; }
.status-pill.approved { background: #16a34a33; color: #4ade80; }

/* ---------- MODAL ---------- */
.modal {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,0.65);
  display: flex; align-items: flex-end; justify-content: center;
}
.modal-card {
  background: var(--bg-elevated);
  width: 100%;
  max-width: 480px;
  border-radius: 20px 20px 0 0;
  padding: 20px;
  padding-bottom: calc(20px + var(--safe-bottom));
  max-height: 90vh;
  overflow-y: auto;
}
.modal-card h3 { margin: 0 0 6px; }
.modal-desc { color: var(--text-dim); font-size: 14px; margin: 0 0 8px; }
.modal-points { margin: 0 0 14px; }

.upload-box {
  display: flex; align-items: center; justify-content: center;
  height: 200px;
  border-radius: 14px;
  border: 2px dashed #334155;
  background: #0f172a;
  overflow: hidden;
  cursor: pointer;
  margin-bottom: 6px;
}
.upload-box input[type=file] { display: none; }
.upload-box img, .upload-box video { width: 100%; height: 100%; object-fit: cover; }
#upload-placeholder { color: var(--text-dim); font-size: 14px; text-align: center; padding: 0 20px; }

.modal-actions { display: flex; gap: 10px; margin-top: 14px; }
.modal-actions button { flex: 1; }

.photo-modal-card { padding: 12px; max-width: 480px; }
.photo-modal-card img, .photo-modal-card video { width: 100%; border-radius: 12px; margin-bottom: 10px; max-height: 70vh; }

.toast {
  position: fixed;
  bottom: calc(20px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: #0f172a;
  border: 1px solid #334155;
  color: var(--text);
  padding: 12px 18px;
  border-radius: 12px;
  font-size: 14px;
  z-index: 200;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}
