:root {
  --bg: #0c1015;
  --surface: #141a22;
  --surface-alt: #1b232e;
  --surface-hover: #212b38;
  --border: #2a3442;
  --text: #eef2f7;
  --muted: #93a1b5;
  --accent: #f59e0b;
  --accent-dim: rgba(245, 158, 11, 0.14);
  --green: #22c55e;
  --green-dim: rgba(34, 197, 94, 0.14);
  --red: #ef4444;
  --red-dim: rgba(239, 68, 68, 0.14);
  --sky: #38bdf8;
  --sky-dim: rgba(56, 189, 248, 0.14);
  --radius: 10px;
  --font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 {
  margin: 0;
  text-wrap: balance;
  line-height: 1.25;
}

p {
  margin: 0;
  text-wrap: pretty;
}

.hidden {
  display: none !important;
}

.muted {
  color: var(--muted);
  font-size: 0.9rem;
}

.mono, .num {
  font-variant-numeric: tabular-nums;
}

.mt-sm { margin-top: 0.5rem; }
.mb-sm { margin-bottom: 0.5rem; }
.mt-md { margin-top: 1rem; }
.mt-lg { margin-top: 1.75rem; }

/* Toast */
.toast-region {
  position: fixed;
  bottom: max(1rem, env(safe-area-inset-bottom));
  right: 1rem;
  z-index: 40;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: min(360px, calc(100vw - 2rem));
}

.toast {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  color: var(--text);
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}

.toast.toast-ok { border-left-color: var(--green); }
.toast.toast-err { border-left-color: var(--red); }

/* Auth Shell */
.auth-shell {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}

.auth-card {
  width: 100%;
  max-width: 440px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.75rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.auth-brand h1 {
  font-size: 1.45rem;
  margin-top: 0.6rem;
}

.auth-brand p {
  margin-top: 0.4rem;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.auth-foot {
  border-top: 1px solid var(--border);
  padding-top: 0.85rem;
  font-size: 0.82rem;
}

/* Form Controls */
.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.field label {
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--muted);
}

input, select, textarea {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
  font-size: 0.95rem;
  font-family: inherit;
}

input:focus, select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: var(--accent);
}

.inline-error {
  background: var(--red-dim);
  border: 1px solid rgba(239, 68, 68, 0.4);
  color: #fca5a5;
  padding: 0.55rem 0.75rem;
  border-radius: 8px;
  font-size: 0.85rem;
}

/* Buttons & Badges */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 0.58rem 1rem;
  font-size: 0.9rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background-color 150ms ease-out, border-color 150ms ease-out, opacity 150ms ease-out;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-full { width: 100%; }
.btn-sm { padding: 0.38rem 0.72rem; font-size: 0.82rem; }

.btn-primary {
  background: var(--accent);
  color: #11161d;
}
.btn-primary:hover:not(:disabled) { background: #fbbf24; }

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}
.btn-ghost:hover:not(:disabled) { background: var(--surface-hover); }

.btn-danger {
  background: var(--red-dim);
  border-color: rgba(239, 68, 68, 0.45);
  color: #fca5a5;
}
.btn-danger:hover:not(:disabled) { background: rgba(239, 68, 68, 0.24); }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.18rem 0.55rem;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 700;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--muted);
  white-space: nowrap;
}

.badge-accent {
  background: var(--accent-dim);
  border-color: rgba(245, 158, 11, 0.4);
  color: #fbbf24;
}

.badge-green {
  background: var(--green-dim);
  border-color: rgba(34, 197, 94, 0.4);
  color: #4ade80;
}

.badge-red {
  background: var(--red-dim);
  border-color: rgba(239, 68, 68, 0.4);
  color: #f87171;
}

.badge-sky {
  background: var(--sky-dim);
  border-color: rgba(56, 189, 248, 0.4);
  color: #7dd3fc;
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 3px;
  gap: 3px;
}

.seg-btn {
  background: transparent;
  color: var(--muted);
  border: none;
  border-radius: 6px;
  padding: 0.5rem;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
}

.seg-btn.active {
  background: var(--surface-alt);
  color: var(--text);
}

/* App Shell */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar-inner {
  max-width: 1120px;
  margin: 0 auto;
  padding: 0.7rem 1rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.brand-cluster {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.brand-title {
  font-size: 1.02rem;
}

.main-nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem;
}

.nav-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--muted);
  padding: 0.45rem 0.85rem;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}

.nav-btn:hover { color: var(--text); background: var(--surface-hover); }
.nav-btn.active {
  background: var(--accent-dim);
  border-color: rgba(245, 158, 11, 0.35);
  color: #fbbf24;
}

.user-cluster {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.user-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.88rem;
  font-weight: 600;
  padding: 0.3rem 0.6rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.container {
  max-width: 1120px;
  margin: 0 auto;
  padding: 1.25rem 1rem 3rem;
}

/* Stage Bar & Summary */
.stage-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.stage-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}

.stage-tab {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 9px;
  padding: 0.5rem 0.9rem;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
}

.stage-tab.active {
  background: var(--surface-alt);
  border-color: var(--accent);
  color: var(--text);
}

.summary-banner {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.95rem 1.1rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.9rem;
  margin-bottom: 1rem;
}

.summary-metrics {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.25rem;
  align-items: center;
}

.subtabs {
  display: inline-flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px;
  gap: 4px;
  margin-bottom: 1rem;
}

.subtab-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 0.48rem 0.95rem;
  border-radius: 7px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}

.subtab-btn.active {
  background: var(--accent);
  color: #11161d;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem;
}

/* Redondo Pick'Em Board */
.redondo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(225px, 1fr));
  gap: 1rem;
}

.bucket-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.bucket-card.active-target {
  border-color: var(--accent);
}

.bucket-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.bucket-slots {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.55rem;
}

.slot-chip {
  min-height: 46px;
  border-radius: 8px;
  border: 1px dashed var(--border);
  background: var(--bg);
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  padding: 0.4rem 0.65rem;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.slot-chip.filled {
  border-style: solid;
  border-color: var(--border);
  background: var(--surface-alt);
  color: var(--text);
}

.slot-chip.filled:hover:not(:disabled) {
  border-color: var(--red);
}

.team-pool-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 0.6rem;
}

.pool-team-btn {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 0.6rem 0.75rem;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  text-align: left;
  cursor: pointer;
}

.pool-team-btn:hover:not(:disabled) {
  background: var(--surface-hover);
  border-color: var(--accent);
}

.pool-team-btn.used {
  opacity: 0.45;
  border-style: dashed;
}

.pool-team-name {
  font-size: 0.88rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Matches Grid */
.round-group {
  margin-bottom: 1.5rem;
}

.round-group-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.matches-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(330px, 1fr));
  gap: 0.9rem;
}

.match-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

.match-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.match-teams-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 0.55rem;
}

.team-pick-btn {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 0.65rem 0.7rem;
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  cursor: pointer;
  min-width: 0;
}

.team-pick-btn:hover:not(:disabled) {
  border-color: var(--accent);
}

.team-pick-btn.selected {
  background: var(--accent-dim);
  border-color: var(--accent);
}

.team-pick-btn.official-winner {
  border-color: var(--green);
}

.team-acro {
  font-weight: 700;
  font-size: 0.98rem;
}

.team-full {
  font-size: 0.76rem;
  color: var(--muted);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.vs-box {
  text-align: center;
  font-weight: 700;
  font-size: 0.95rem;
  min-width: 58px;
}

.score-controls {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 0.7rem;
}

.preset-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.preset-btn {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 0.28rem 0.55rem;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
}

.preset-btn.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: #fbbf24;
}

.score-inputs-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr auto;
  align-items: center;
  gap: 0.45rem;
}

.score-inputs-row input {
  text-align: center;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  padding: 0.45rem;
}

.friends-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.84rem;
}

.friends-table th, .friends-table td {
  padding: 0.42rem 0.5rem;
  border-top: 1px solid var(--border);
  text-align: left;
}

.friends-table th {
  color: var(--muted);
  font-weight: 600;
  border-top: none;
}

/* Tables */
.data-table-wrap {
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}

.data-table th, .data-table td {
  padding: 0.72rem 0.65rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  white-space: nowrap;
}

.data-table th {
  color: var(--muted);
  font-size: 0.8rem;
  text-transform: uppercase;
}

.data-table tbody tr:last-child td {
  border-bottom: none;
}

.rules-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1rem;
}

.rules-list {
  margin: 0.65rem 0 0;
  padding-left: 1.15rem;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  font-size: 0.92rem;
}

.rules-list strong { color: var(--text); }

/* Admin */
.admin-stack {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.admin-form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.75rem;
  align-items: end;
}

.section-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.85rem;
}

/* Modal */
.modal-dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  padding: 0;
  width: min(520px, calc(100vw - 2rem));
  z-index: 30;
}

.modal-dialog::backdrop {
  background: rgba(0, 0, 0, 0.7);
}

.modal-card {
  padding: 1.2rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-body {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.modal-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.spacer { flex: 1; }

.warning-banner {
  background: var(--accent-dim);
  border: 1px solid rgba(245, 158, 11, 0.4);
  color: #fde68a;
  padding: 0.65rem 0.85rem;
  border-radius: 8px;
  font-size: 0.86rem;
}

.official-winner-note {
  font-size: 0.75rem;
  color: var(--muted);
}

.md1-pending-note {
  font-size: 0.78rem;
  color: #fde68a;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.88rem;
  cursor: pointer;
}

.checkbox-row input[type="checkbox"] {
  width: auto;
  margin: 0;
}

.team-logo {
  width: 24px;
  height: 24px;
  object-fit: contain;
  border-radius: 4px;
  flex-shrink: 0;
}

.team-logo-sm {
  width: 18px;
  height: 18px;
  object-fit: contain;
  border-radius: 3px;
  flex-shrink: 0;
}

.team-pick-head {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
}

.pool-team-label {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  min-width: 0;
}

.match-time-row {
  font-size: 0.78rem;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.35rem;
}

.matches-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1rem;
  padding: 0.75rem 0.9rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.matches-toolbar-left,
.matches-toolbar-right {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.filter-pills {
  display: inline-flex;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 3px;
  gap: 3px;
}

.filter-pill {
  background: transparent;
  border: none;
  color: var(--muted);
  border-radius: 6px;
  padding: 0.32rem 0.7rem;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}

.filter-pill.active {
  background: var(--surface-alt);
  color: var(--text);
}

.match-card.has-unsaved {
  border-color: rgba(245, 158, 11, 0.55);
}

.friends-table tr.is-me td {
  background: rgba(245, 158, 11, 0.08);
}

.card.is-me {
  border-color: rgba(245, 158, 11, 0.55);
}

.data-table tr.is-me td {
  background: rgba(245, 158, 11, 0.08);
}

.ranking-controls {
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.ranking-controls select {
  width: auto;
  min-width: 210px;
}

.sub-form-divider {
  border-top: 1px solid var(--border);
  padding-top: 0.85rem;
  margin-top: 0.85rem;
}

.admin-group-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem 0.75rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
}

.admin-row-active td {
  background: var(--accent-dim);
}

.admin-notice {
  padding: 0.65rem 0.85rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  font-size: 0.9rem;
}

.admin-notice-warn {
  border-color: var(--accent);
  background: var(--accent-dim);
}

.inline-hint {
  display: block;
  font-size: 0.82rem;
  color: var(--muted);
  margin-top: 0.35rem;
}

@media (max-width: 640px) {
  .topbar { position: static; }
  .topbar-inner { padding: 0.6rem 0.75rem; }
  .matches-grid { grid-template-columns: 1fr; }
  .bucket-slots { grid-template-columns: 1fr 1fr; }
  .ranking-controls { width: 100%; }
  .ranking-controls select { width: 100%; }
}
