/* ============================================================
   RESET & BASE
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --text-muted: #64748b;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --gold: #f59e0b;
  --silver: #94a3b8;
  --bronze: #b45309;
  --gold-bg: #fef3c7;
  --silver-bg: #f1f5f9;
  --bronze-bg: #fef3c7;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.12), 0 1px 2px rgba(0,0,0,.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,.1);
}

body {
  font-family: system-ui, -apple-system, sans-serif;
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
}

/* ============================================================
   BUTTONS
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius);
  font-size: .9rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background .15s, opacity .15s;
  white-space: nowrap;
}
.btn:active { opacity: .85; }

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-hover); }

.btn-outline {
  background: transparent;
  border: 1.5px solid var(--border);
  color: var(--text);
}
.btn-outline:hover { background: var(--border); }

.btn-sm { padding: 5px 12px; font-size: .8rem; }
.btn-lg { padding: 12px 28px; font-size: 1rem; width: 100%; justify-content: center; }

.btn-icon {
  width: 32px;
  height: 32px;
  padding: 0;
  border: none;
  border-radius: 6px;
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background .15s;
  background: var(--border);
  color: var(--text);
  flex-shrink: 0;
}
.btn-icon:hover { background: #cbd5e1; }
.delete-team:hover, .delete-round:hover { background: #fee2e2; color: var(--danger); }

/* ============================================================
   FORMS
   ============================================================ */
.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group label { font-size: .85rem; font-weight: 600; color: var(--text-muted); }

input[type="text"],
input[type="number"] {
  padding: 8px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: .95rem;
  color: var(--text);
  background: #fff;
  outline: none;
  transition: border-color .15s;
  width: 100%;
}
input[type="text"]:focus,
input[type="number"]:focus { border-color: var(--primary); }

.add-form {
  display: flex;
  gap: 6px;
  margin-bottom: 8px;
}
.add-form input { flex: 1; min-width: 0; }

/* ============================================================
   HOME PAGE
   ============================================================ */
.page-home {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.home-container {
  width: 100%;
  max-width: 480px;
  padding: 24px;
}

.home-card {
  background: var(--surface);
  border-radius: 16px;
  padding: 40px 36px;
  box-shadow: var(--shadow-md);
  text-align: center;
}

.home-card h1 {
  font-size: 1.8rem;
  margin-bottom: 8px;
}

.home-card .subtitle {
  color: var(--text-muted);
  margin-bottom: 32px;
}

.create-form { text-align: left; display: flex; flex-direction: column; gap: 16px; }

.home-superadmin {
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
  text-align: center;
}

/* ============================================================
   ADMIN NAV
   ============================================================ */
.admin-nav {
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 10;
}

.nav-title {
  font-weight: 700;
  font-size: 1.05rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-links { display: flex; gap: 8px; flex-shrink: 0; }

.nav-title-group {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  flex: 1;
  overflow: hidden;
}

.nav-emoji { flex-shrink: 0; }

.nav-bt-name {
  font-weight: 700;
  font-size: 1.05rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-bt-input {
  font-weight: 700;
  font-size: 1.05rem;
  padding: 4px 8px;
  border: 1.5px solid var(--primary);
  border-radius: 6px;
  background: #fff;
  outline: none;
  min-width: 200px;
  max-width: 400px;
}

.edit-bt-name {
  width: 24px;
  height: 24px;
  font-size: .8rem;
  background: transparent;
  flex-shrink: 0;
  opacity: .5;
}
.edit-bt-name:hover { opacity: 1; background: var(--border); }

.hidden { display: none !important; }

/* ============================================================
   ADMIN LAYOUT
   ============================================================ */
.admin-layout {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  padding: 16px;
  height: calc(100vh - 56px);
  overflow: hidden;
}

.setup-panel,
.standings-panel {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-header {
  font-size: .8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-muted);
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--bg);
}

.standings-panel .standings-list {
  padding: 10px 12px;
  overflow-y: auto;
  flex: 1;
}

/* Setup tabs */
.setup-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.setup-tab-btn {
  flex: 1;
  padding: 8px;
  border: none;
  background: transparent;
  font-size: .85rem;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 3px solid transparent;
  margin-bottom: -1px;
  transition: color .15s, border-color .15s;
}
.setup-tab-btn:hover { color: var(--primary); }
.setup-tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); }

.setup-tab-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  overflow-y: auto;
}

.item-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.item-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 6px;
  background: var(--bg);
  font-size: .9rem;
}

.item-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty-hint {
  color: var(--text-muted);
  font-size: .85rem;
  font-style: italic;
  padding: 4px 0;
}

.inline-edit-input {
  flex: 1;
  min-width: 0;
  padding: 2px 6px;
  border: 1.5px solid var(--primary);
  border-radius: 4px;
  font-size: .9rem;
  background: #fff;
  outline: none;
  width: 100%;
}

/* ============================================================
   SCORING PANEL
   ============================================================ */
.scoring-panel {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.scoring-panel .panel-header {
  flex-shrink: 0;
}

.empty-state {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  padding: 40px;
  text-align: center;
}

.round-tabs {
  display: flex;
  gap: 4px;
  padding: 12px 16px 0;
  flex-wrap: wrap;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0;
}

.tab-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 6px 6px 0 0;
  background: transparent;
  font-size: .85rem;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 3px solid transparent;
  transition: color .15s, border-color .15s;
  margin-bottom: -1px;
}

.tab-btn:hover { color: var(--primary); }
.tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); background: #eef2ff; }

.round-scores {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.round-scores.hidden { display: none; }

.score-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--bg);
  transition: background .15s;
}
.score-row:hover { background: #e2e8f0; }

.score-team-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
}

.score-controls {
  display: flex;
  align-items: center;
  gap: 4px;
}

.score-input {
  width: 44px;
  text-align: center;
  font-weight: 700;
  font-size: 1rem;
  padding: 6px 2px;
  border-radius: 6px;
}
/* hide number input spinners */
.score-input::-webkit-inner-spin-button,
.score-input::-webkit-outer-spin-button { -webkit-appearance: none; }
.score-input { -moz-appearance: textfield; }

.score-total {
  min-width: 50px;
  text-align: right;
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--primary);
}

/* ============================================================
   STANDINGS PANEL
   ============================================================ */
.standings-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.standing-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: .85rem;
}

.standing-item.rank-1 { background: #fef3c7; font-weight: 700; }
.standing-item.rank-2 { background: #f1f5f9; font-weight: 600; }
.standing-item.rank-3 { background: #fff7ed; font-weight: 600; }
.standing-item.rank-other { background: var(--bg); }

.standing-rank {
  font-weight: 800;
  font-size: .8rem;
  color: var(--text-muted);
  min-width: 18px;
}
.standing-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.standing-total { font-weight: 800; font-size: .95rem; }

/* ============================================================
   DISPLAY PAGE
   ============================================================ */
.page-display {
  background: #f0f2f5;
  color: #1e293b;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.display-header {
  padding: 12px 24px;
  text-align: center;
  border-bottom: 1px solid #cbd5e1;
  flex-shrink: 0;
}

.display-header h1 {
  font-size: clamp(1.2rem, 2.5vw, 2rem);
  font-weight: 800;
  letter-spacing: .02em;
  color: #1e293b;
}

.scoreboard {
  flex: 1;
  display: flex;
  gap: 12px;
  padding: 12px;
  overflow: hidden;
}

.scoreboard-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.scoreboard-col--full {
  max-width: 700px;
  margin: 0 auto;
}

.team-card {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  border-radius: 8px;
  transition: opacity .3s;
  min-height: 0;
}

/* Rank-specific sizing via flex-grow */
.team-card.rank-1 { flex: 1.5; }
.team-card.rank-2 { flex: 1.3; }
.team-card.rank-3 { flex: 1.15; }
.team-card.rank-other { flex: 1; }

/* Colors */
.team-card.rank-1 {
  background: linear-gradient(135deg, #b45309, #f59e0b);
  color: #1a1000;
}
.team-card.rank-2 {
  background: linear-gradient(135deg, #475569, #94a3b8);
  color: #0f172a;
}
.team-card.rank-3 {
  background: linear-gradient(135deg, #78350f, #b45309);
  color: #fef3c7;
}
.team-card.rank-other {
  background: #ffffff;
  color: #1e293b;
  border: 1px solid #e2e8f0;
}

.card-rank {
  font-size: clamp(.7rem, 1.2vw, 1.1rem);
  font-weight: 900;
  opacity: .7;
  min-width: 28px;
  flex-shrink: 0;
}
.team-card.rank-1 .card-rank,
.team-card.rank-2 .card-rank,
.team-card.rank-3 .card-rank { opacity: 1; }

.card-name {
  flex: 1;
  font-size: clamp(1rem, 1.8vw, 1.6rem);
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.team-card.rank-1 .card-name { font-size: clamp(1.1rem, 2.2vw, 2rem); }
.team-card.rank-2 .card-name { font-size: clamp(1.05rem, 2vw, 1.8rem); }
.team-card.rank-3 .card-name { font-size: clamp(1rem, 1.9vw, 1.7rem); }

.card-total {
  font-size: clamp(.85rem, 1.6vw, 1.4rem);
  font-weight: 900;
  flex-shrink: 0;
}

.team-card.rank-1 .card-total { font-size: clamp(.95rem, 2vw, 1.7rem); }
.team-card.rank-2 .card-total { font-size: clamp(.9rem, 1.8vw, 1.55rem); }
.team-card.rank-3 .card-total { font-size: clamp(.88rem, 1.7vw, 1.45rem); }

/* Flash animation on score change */
@keyframes flash-update {
  0%   { filter: brightness(1.4); }
  100% { filter: brightness(1); }
}
.team-card.updated { animation: flash-update .6s ease; }

.display-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #94a3b8;
  font-size: 1.4rem;
  font-style: italic;
}

/* ============================================================
   HISTORY PAGE
   ============================================================ */
.page-history { background: var(--bg); min-height: 100vh; }

.history-container {
  max-width: 900px;
  margin: 0 auto;
  padding: 32px 20px;
}

.history-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}

.history-toolbar h2 {
  font-size: 1.4rem;
  margin: 0;
}

.history-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.history-table th,
.history-table td {
  padding: 12px 16px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: .9rem;
}

.history-table th {
  background: var(--bg);
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  font-size: .75rem;
  letter-spacing: .05em;
}

.history-table tr:last-child td { border-bottom: none; }
.history-table tr:hover td { background: #f8fafc; }

.history-actions { display: flex; gap: 6px; }

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 900px) {
  .admin-layout {
    grid-template-columns: 1fr;
    height: auto;
    overflow: visible;
  }

  .setup-panel,
  .standings-panel,
  .scoring-panel {
    height: auto;
    max-height: none;
  }

  .round-scores {
    max-height: 60vh;
  }
}

@media (max-width: 600px) {
  .scoreboard { flex-direction: column; }
  .scoreboard-col { flex: none; }
  .team-card { flex: none !important; min-height: 44px; }
  .page-display { overflow-y: auto; }
}
