:root {
  --bg: #12121c;
  --bg2: #1a1a2e;
  --card: #21213a;
  --text: #eaeaf2;
  --muted: #9a9ab8;
  --accent: #e94560;
  --accent2: #4f9dff;
  --ok: #4fff8f;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: linear-gradient(160deg, var(--bg) 0%, var(--bg2) 100%);
  color: var(--text);
  min-height: 100vh;
  /* keep content clear of a side notch when installed in landscape */
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  /* top inset clears the iOS status bar / Dynamic Island in standalone mode */
  padding: calc(16px + env(safe-area-inset-top)) 24px 16px;
  border-bottom: 1px solid #2e2e4d;
}
header h1 { margin: 0; font-size: 1.5rem; letter-spacing: 1px; display: flex; align-items: center; gap: 10px; }
header h1 span { color: var(--accent); }
header h1 .logo { height: 38px; width: auto; display: block; }
.gate-logo { width: 140px; height: auto; margin: 0 auto 8px; display: block; }

.header-right { display: flex; align-items: center; gap: 12px; }

.pill {
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 0.8rem;
  border: 1px solid currentColor;
}
.pill.online { color: var(--ok); }
.pill.offline { color: var(--muted); }

#auth { display: flex; align-items: center; gap: 10px; }
.auth-name { font-size: 0.85rem; color: var(--text); }
.auth-name .role { color: var(--ok); }
.auth-name .role.guest { color: var(--muted); }
.auth-btn {
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 0.85rem;
  border: 1px solid #5865F2;
  color: #fff;
  background: #5865F2;
}
.auth-btn.ghost { background: none; border-color: var(--muted); color: var(--muted); }

nav#tabs {
  display: flex;
  gap: 4px;
  padding: 12px 24px 0;
  flex-wrap: wrap;
}
nav#tabs button {
  background: none;
  border: none;
  color: var(--muted);
  padding: 10px 18px;
  font-size: 1rem;
  cursor: pointer;
  border-bottom: 2px solid transparent;
}
nav#tabs button.active { color: var(--text); border-bottom-color: var(--accent); }
nav#tabs button:hover { color: var(--text); }

main { padding: 24px; max-width: 1100px; margin: 0 auto; }
.tab { display: none; }
.tab.active { display: block; }

h2 { font-size: 1.1rem; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }

.card {
  background: var(--card);
  border: 1px solid #2e2e4d;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 16px;
}

/* scoreboard */
.score-cards { display: flex; gap: 16px; flex-wrap: wrap; margin-bottom: 24px; }
.score-card {
  flex: 1;
  min-width: 220px;
  background: var(--card);
  border-radius: 12px;
  padding: 24px;
  text-align: center;
  border-top: 4px solid var(--accent);
}
.score-card .points { font-size: 3.5rem; font-weight: 800; line-height: 1; }
.score-card .name { font-size: 1.2rem; margin-bottom: 8px; }
.score-card .wins { color: var(--muted); font-size: 0.9rem; margin-top: 6px; }
.score-card.leader { box-shadow: 0 0 24px rgba(233, 69, 96, 0.25); }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid #2e2e4d; }
th { color: var(--muted); font-size: 0.8rem; text-transform: uppercase; }
td .del { background: none; border: none; color: var(--muted); cursor: pointer; }
td .del:hover { color: var(--accent); }

/* forms */
.form label { display: block; margin-bottom: 14px; color: var(--muted); font-size: 0.9rem; }
.form select, .form input, .form textarea {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 10px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid #2e2e4d;
  border-radius: 8px;
  font-size: 1rem;
}

/* Custom dropdown chevron with breathing room from the right edge (all selects). */
select {
  -webkit-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%239a9ab8' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") !important;
  background-repeat: no-repeat !important;
  background-position: right 12px center !important;
  padding-right: 34px !important;
}
button {
  background: #2e2e4d;
  color: var(--text);
  border: none;
  border-radius: 8px;
  padding: 10px 18px;
  font-size: 1rem;
  cursor: pointer;
}
button:hover { filter: brightness(1.2); }
button.primary { background: var(--accent); font-weight: 700; }
button.big { font-size: 1.3rem; padding: 14px 40px; }
button:disabled { opacity: 0.5; cursor: not-allowed; }

/* picker */
.picker-layout { display: flex; gap: 24px; flex-wrap: wrap; align-items: flex-start; }
.wheel-wrap { text-align: center; position: relative; }
.wheel-pointer { font-size: 2rem; color: var(--accent); line-height: 1; margin-bottom: -8px; }
#wheel { max-width: 100%; height: auto; }
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* install (Add to Home Screen) banner */
.install-banner {
  display: flex; align-items: center; justify-content: center; gap: 12px;
  background: var(--card); border-bottom: 1px solid #2e2e4d;
  padding: 8px 16px; font-size: 0.9rem; color: var(--text);
}
.install-banner.hidden { display: none; }
.install-go { background: var(--accent); color: #fff; border: none; border-radius: 8px; padding: 6px 14px; font-weight: 600; cursor: pointer; }
.install-x { background: none; border: none; color: var(--muted); font-size: 1rem; cursor: pointer; line-height: 1; }
@media (max-width: 640px) { .install-banner { font-size: 0.82rem; gap: 8px; flex-wrap: wrap; } }
#spin-btn { display: block; margin: 16px auto 0; }
#spin-result { font-size: 1.6rem; font-weight: 800; margin-top: 12px; min-height: 2rem; color: var(--ok); }
.picker-layout .card { flex: 1; min-width: 260px; }

/* discord */
.row-buttons { display: flex; gap: 12px; flex-wrap: wrap; }
#discord-log {
  background: var(--bg);
  border-radius: 8px;
  padding: 12px;
  color: var(--muted);
  font-size: 0.85rem;
  min-height: 40px;
  white-space: pre-wrap;
}

/* admin */
.admin-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; }
.admin-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.admin-row input, .admin-row select {
  background: var(--bg); color: var(--text);
  border: 1px solid #2e2e4d; border-radius: 6px; padding: 6px 8px;
}
.admin-row .grow { flex: 1; }
.admin-row.inactive { opacity: 0.5; }
.admin-row .w-num { width: 60px; }
.inline-form { display: flex; gap: 8px; margin-top: 12px; }
.inline-form input { flex: 1; background: var(--bg); color: var(--text); border: 1px solid #2e2e4d; border-radius: 6px; padding: 8px; }
.inline-form input[type="number"] { flex: 0 0 70px; }
.hint { color: var(--muted); font-size: 0.8rem; }

/* players tab */
.player-form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 14px;
  align-items: end;
  margin-bottom: 16px;
}
.player-form-grid .check-inline {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
}
.player-form-grid .check-inline input { width: auto; margin-top: 0; }
#players-table td { vertical-align: middle; }
#players-table select { background: var(--bg); color: var(--text); border: 1px solid #2e2e4d; border-radius: 6px; padding: 5px 6px; }
#players-table input[type="checkbox"] { width: 18px; height: 18px; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 0.8rem; color: var(--muted); }
.warn { font-family: var(--font, inherit); font-size: 0.78rem; color: #f5a623; white-space: nowrap; }

/* trophies / achievements */
.trophies { display: flex; flex-wrap: wrap; gap: 16px; margin: 4px 0 8px; }
.trophy-team { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.tt-name { font-weight: 600; font-size: 0.9rem; }
.trophy {
  background: var(--card); border: 1px solid #2e2e4d; border-radius: 999px;
  padding: 4px 12px; font-size: 0.82rem;
}
.trophy em { color: var(--muted); font-style: normal; }

/* stats leaderboard */
#stats-table td:nth-child(1) { color: var(--muted); width: 36px; }
#stats-table td:nth-child(4) { white-space: nowrap; }

/* celebrate flash when a new result lands */
@keyframes celebrate {
  0% { transform: scale(1); filter: brightness(1); }
  35% { transform: scale(1.03); filter: brightness(1.6); }
  100% { transform: scale(1); filter: brightness(1); }
}
.score-cards.celebrate { animation: celebrate 0.6s ease; }

/* toast */
#toast {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--card);
  border: 1px solid var(--accent);
  padding: 12px 24px;
  border-radius: 8px;
  transition: transform 0.25s;
  z-index: 10;
}
#toast.show { transform: translateX(-50%) translateY(0); }

/* access gate */
.gate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, var(--bg) 0%, var(--bg2) 100%);
}
.gate.hidden { display: none; }
.gate-box {
  text-align: center;
  max-width: 420px;
  padding: 40px;
}
.gate-box h1 { font-size: 2rem; letter-spacing: 1px; margin: 0 0 16px; }
.gate-box h1 span { color: var(--accent); }
.gate-box p { color: var(--muted); font-size: 1.05rem; line-height: 1.6; margin-bottom: 24px; }
#gate-actions { display: flex; flex-direction: column; gap: 12px; align-items: center; }

/* hide admin-only UI from non-admin (viewer) players */
body:not(.is-admin) .admin-only { display: none !important; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

/* screenshot → lineup */
button.ghost {
  background: none; border: 1px solid var(--muted); color: var(--text);
  border-radius: 8px; padding: 8px 14px; margin-top: 8px;
}
button.link { background: none; border: none; cursor: pointer; font-size: 1rem; padding: 2px 4px; }
.lineup-table { width: 100%; border-collapse: collapse; margin: 10px 0; font-size: 0.85rem; }
.lineup-table th, .lineup-table td { text-align: left; padding: 6px 8px; border-bottom: 1px solid #2e2e4d; }
.lineup-table th { color: var(--muted); font-weight: 600; }
.lineup-table select { background: var(--bg); color: var(--text); border: 1px solid #2e2e4d; border-radius: 6px; padding: 4px 6px; }
#lineup-review { margin: 12px 0; }

/* match detail modal */
.modal {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0, 0, 0, 0.6);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.modal-box {
  background: var(--card); border: 1px solid #2e2e4d; border-radius: 12px;
  padding: 24px; max-width: 900px; max-height: 85vh; overflow: auto; position: relative;
}
.modal-close { position: absolute; top: 12px; right: 12px; background: none; border: none; color: var(--muted); font-size: 1.1rem; cursor: pointer; }
.modal .shot { max-width: 100%; border-radius: 8px; margin-bottom: 16px; display: block; }

/* duels & ladder */
#duel-form select { background: var(--bg); color: var(--text); border: 1px solid #2e2e4d; border-radius: 6px; padding: 8px; }
.duel-sides { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.duel-side { flex: 1; min-width: 160px; display: flex; flex-direction: column; gap: 8px; }
.duel-side h3 { margin: 0 0 2px; font-size: 0.95rem; }
.duel-vs { color: var(--muted); font-weight: 700; padding-top: 24px; }
.duel-card {
  background: var(--card); border: 1px solid #2e2e4d; border-radius: 10px;
  padding: 12px 14px; margin-bottom: 10px;
}
.duel-card.done { opacity: 0.85; }
.duel-card.mine { border-color: var(--accent); border-left: 3px solid var(--accent); }
#duels-mine:not(:empty) { margin-bottom: 8px; }
.duel-head { margin-bottom: 6px; }
.duel-desc { font-size: 0.85rem; color: var(--muted); margin-bottom: 8px; }
.duel-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.duel-actions button { border-radius: 8px; padding: 6px 12px; }
.badge {
  display: inline-block; background: var(--accent); color: #fff;
  font-size: 0.72rem; font-weight: 700; padding: 2px 8px; border-radius: 999px; margin-right: 4px;
}
.ladder-pick { display: inline-flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.ladder-pick select { background: var(--bg); color: var(--text); border: 1px solid #2e2e4d; border-radius: 6px; padding: 6px 8px; }
#ladder-table { width: 100%; border-collapse: collapse; }
#ladder-table th, #ladder-table td { text-align: left; padding: 8px; border-bottom: 1px solid #2e2e4d; }
#ladder-table th { color: var(--muted); font-weight: 600; }
#ladder-table td:nth-child(1) { color: var(--muted); width: 36px; }
#ladder-table td:nth-child(3) { font-weight: 600; }

/* ---- mobile / small screens ---- */
@media (max-width: 640px) {
  header { padding: calc(12px + env(safe-area-inset-top)) 16px 12px; flex-wrap: wrap; gap: 8px; }
  header h1 { font-size: 1.2rem; }
  .header-right { gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
  .pill, .auth-btn, .auth-name { font-size: 0.78rem; }

  /* tab bar becomes a horizontal swipe strip instead of wrapping to many rows */
  nav#tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 8px 12px 0;
    gap: 2px;
  }
  nav#tabs::-webkit-scrollbar { display: none; }
  nav#tabs { scrollbar-width: none; }
  nav#tabs button { white-space: nowrap; padding: 8px 12px; font-size: 0.92rem; }

  main { padding: 16px 12px; }
  .card { padding: 16px; overflow-x: auto; }
  h2 { font-size: 1rem; }

  /* score cards stack and shrink */
  .score-card { min-width: 0; flex-basis: 100%; padding: 18px; }
  .score-card .points { font-size: 2.6rem; }
  .score-card .name { font-size: 1.05rem; }

  /* tables scroll sideways rather than crushing */
  .table-scroll, #lineup-review, #match-detail-body { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  th, td { padding: 6px 8px; font-size: 0.85rem; }

  /* admin panels and duel sides go single-column */
  .admin-grid { grid-template-columns: 1fr; }
  .duel-sides { flex-direction: column; align-items: stretch; }
  .duel-vs { padding-top: 0; text-align: center; }
  .duel-side { min-width: 0; }

  /* modal fills the small screen */
  .modal { padding: 12px; }
  .modal-box { padding: 16px; max-height: 92vh; width: 100%; }
}
