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

:root {
  --bg: #111113;
  --surface: #1c1c20;
  --surface2: #26262c;
  --surface3: #303038;
  --border: #3a3a44;
  --text: #eaeaf0;
  --text2: #9a9ab0;
  --orange: #e8601c;
  --orange-dim: #b84a15;
  --green: #34c759;
  --red: #ff3b30;
  --blue: #4a90d9;
  --yellow: #f5a623;
  --home: #2a6dd9;
  --away: #d93a2a;
  --radius: 10px;
  --touch: 52px;
}

html, body {
  font-family: -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100%; width: 100%;
  overflow: hidden;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  font-size: 15px;
}

/* Screens */
.screen { display: none; height: 100vh; flex-direction: column; }
.screen.active { display: flex; }

/* ===== SETUP SCREEN ===== */
#setup-screen { padding: 20px; overflow-y: auto; gap: 16px; }
#setup-screen h1 { font-size: 22px; text-align: center; color: var(--orange); letter-spacing: 1px; }

.setup-section {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
}
.setup-section h2 { font-size: 14px; text-transform: uppercase; letter-spacing: 2px; color: var(--text2); margin-bottom: 12px; }

.setup-row {
  display: flex; gap: 12px; align-items: center; margin-bottom: 10px;
}
.setup-row label { font-size: 14px; color: var(--text2); min-width: 110px; }

input, select {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 16px;
  min-height: var(--touch);
  outline: none;
  flex: 1;
}
input:focus { border-color: var(--orange); }
input::placeholder { color: var(--text2); opacity: .5; }

.team-setup { display: flex; gap: 16px; }
.team-setup > div { flex: 1; }

.team-label-home { color: var(--home); font-weight: 700; }
.team-label-away { color: var(--away); font-weight: 700; }

.player-list { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.player-entry { display: flex; gap: 8px; align-items: center; }
.player-entry input:first-child { max-width: 70px; text-align: center; flex: none; }
.player-entry .remove-player {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--surface3); border: none; color: var(--red);
  font-size: 20px; cursor: pointer; flex: none;
  display: flex; align-items: center; justify-content: center;
}

.add-player-btn {
  margin-top: 8px; padding: 10px;
  background: var(--surface2); border: 1px dashed var(--border);
  border-radius: 8px; color: var(--text2); font-size: 14px;
  cursor: pointer; text-align: center; width: 100%;
}
.add-player-btn:active { background: var(--surface3); }

.start-game-btn {
  width: 100%; padding: 16px;
  background: var(--orange); border: none; border-radius: var(--radius);
  color: #fff; font-size: 18px; font-weight: 700;
  cursor: pointer; letter-spacing: 1px;
  min-height: 56px;
}
.start-game-btn:active { background: var(--orange-dim); }
.start-game-btn:disabled { opacity: .4; pointer-events: none; }

/* ===== GAME SCREEN ===== */
#game-screen { overflow: hidden; }

/* Top bar */
.game-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 12px; background: var(--surface); border-bottom: 1px solid var(--border);
  min-height: 48px; flex: none;
}
.quarter-display {
  background: var(--surface2); border-radius: 6px; padding: 4px 14px;
  font-size: 14px; font-weight: 700; letter-spacing: 1px;
}

.game-clock {
  font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums;
  letter-spacing: 2px; min-width: 100px; text-align: center;
}
.game-clock.running { color: var(--green); }
.game-clock.stopped { color: var(--red); }

.clock-controls { display: flex; gap: 6px; }
.clock-controls button {
  width: 40px; height: 40px; border-radius: 8px;
  border: none; font-size: 18px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface2); color: var(--text);
}
.clock-controls button:active { background: var(--surface3); }

.header-menu-btn {
  width: 40px; height: 40px; border-radius: 8px;
  border: none; background: var(--surface2); color: var(--text2);
  font-size: 18px; cursor: pointer;
}

/* Scoreboard */
.scoreboard {
  display: flex; align-items: center; justify-content: center;
  padding: 8px 16px; gap: 20px; flex: none;
  background: linear-gradient(180deg, var(--surface) 0%, var(--bg) 100%);
}
.score-team {
  text-align: center; flex: 1;
}
.score-team-name {
  font-size: 13px; text-transform: uppercase; letter-spacing: 2px;
  margin-bottom: 2px; font-weight: 600;
}
.score-team-name.home { color: var(--home); }
.score-team-name.away { color: var(--away); }
.score-value {
  font-size: 52px; font-weight: 800; line-height: 1;
  font-variant-numeric: tabular-nums;
}
.score-team-fouls {
  font-size: 12px; color: var(--text2); margin-top: 2px;
}
.score-divider {
  font-size: 24px; color: var(--text2); font-weight: 300;
}

/* Main area: two team panels */
.game-area {
  display: flex; flex: 1; overflow: hidden;
}
.team-panel {
  flex: 1; display: flex; flex-direction: column;
  border-right: 1px solid var(--border);
}
.team-panel:last-child { border-right: none; }

/* Action buttons row */
.action-bar {
  display: flex; gap: 4px; padding: 6px;
  flex: none; flex-wrap: wrap;
}
.action-btn {
  flex: 1; min-width: 0;
  min-height: var(--touch); padding: 6px 4px;
  border-radius: 8px; border: none;
  font-size: 13px; font-weight: 700;
  cursor: pointer; text-align: center;
  display: flex; align-items: center; justify-content: center;
  transition: transform .1s;
}
.action-btn:active { transform: scale(.95); }
.action-btn.pts2 { background: #2a5a1a; color: #8f8; }
.action-btn.pts3 { background: #1a4a6a; color: #8cf; }
.action-btn.ft { background: #5a4a1a; color: #fd8; }
.action-btn.foul { background: #5a1a1a; color: #f88; }
.action-btn.more { background: var(--surface3); color: var(--text2); }
.action-btn.timeout { background: var(--surface2); color: var(--yellow); border: 1px solid var(--yellow); font-size: 12px; }

/* Player grid */
.player-grid {
  flex: 1; overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 4px; padding: 6px;
  align-content: start;
}
.player-tile {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 8px;
  padding: 8px 4px;
  text-align: center;
  cursor: pointer;
  min-height: var(--touch);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  transition: border-color .15s, transform .1s;
}
.player-tile:active { transform: scale(.95); }
.player-tile .p-number-row { display: flex; align-items: center; justify-content: center; gap: 6px; }
.player-tile .p-number { font-size: 22px; font-weight: 800; line-height: 1; }
.player-tile .foul-badge {
  background: var(--red); color: #fff; font-size: 13px; font-weight: 800;
  min-width: 22px; height: 22px; border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  padding: 0 4px; line-height: 1;
}
.player-tile .p-name { font-size: 10px; color: var(--text2); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 90px; }
.player-tile .p-stats { font-size: 10px; color: var(--text2); margin-top: 3px; display: flex; gap: 6px; justify-content: center; }
.player-tile .p-stats .stat-pts { color: var(--green); }

.player-tile.special { border-style: dashed; }
.player-tile.selected { border-color: var(--orange); background: var(--surface2); }

/* ===== MODAL / OVERLAY ===== */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 16px;
}
.modal {
  background: var(--surface);
  border-radius: 14px;
  padding: 20px;
  max-width: 420px; width: 100%;
  max-height: 85vh; overflow-y: auto;
}
.modal h3 {
  font-size: 16px; margin-bottom: 14px;
  text-align: center; color: var(--orange);
}
.modal-actions {
  display: flex; flex-direction: column; gap: 8px;
}
.modal-btn {
  min-height: var(--touch); padding: 12px;
  border-radius: 8px; border: none;
  font-size: 15px; font-weight: 600;
  cursor: pointer; text-align: center;
  background: var(--surface2); color: var(--text);
}
.modal-btn:active { background: var(--surface3); }
.modal-btn.danger { background: #5a1a1a; color: var(--red); }
.modal-btn.success { background: #1a4a2a; color: var(--green); }
.modal-btn.cancel { background: var(--surface3); color: var(--text2); }

/* Free throw dialog */
.ft-dialog {
  text-align: center;
}
.ft-dialog .ft-info { font-size: 14px; color: var(--text2); margin-bottom: 16px; }
.ft-dialog .ft-attempt {
  font-size: 18px; font-weight: 700; margin-bottom: 12px;
}
.ft-dialog .ft-buttons { display: flex; gap: 12px; justify-content: center; margin-bottom: 12px; }
.ft-dialog .ft-btn {
  width: 100px; height: 60px; border-radius: 10px; border: none;
  font-size: 24px; font-weight: 700; cursor: pointer;
}
.ft-dialog .ft-btn.made { background: #1a5a2a; color: var(--green); }
.ft-dialog .ft-btn.missed { background: #5a1a1a; color: var(--red); }
.ft-dialog .ft-btn:active { transform: scale(.93); }
.ft-dialog .ft-results { display: flex; gap: 8px; justify-content: center; margin-top: 10px; }
.ft-dialog .ft-dot {
  width: 28px; height: 28px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; font-weight: 700;
}
.ft-dot.made { background: #1a5a2a; color: var(--green); }
.ft-dot.missed { background: #5a1a1a; color: var(--red); }
.ft-dot.pending { background: var(--surface3); color: var(--text2); }

/* FT count selector */
.ft-count-select { display: flex; gap: 10px; justify-content: center; margin-bottom: 16px; }
.ft-count-btn {
  width: 64px; height: 64px; border-radius: 12px;
  background: var(--surface2); border: 2px solid var(--border);
  color: var(--text); font-size: 28px; font-weight: 800;
  cursor: pointer;
}
.ft-count-btn:active { border-color: var(--orange); background: var(--surface3); }

/* Event log */
.event-log-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 12px; background: var(--surface);
  border-top: 1px solid var(--border);
  min-height: 40px; flex: none;
  overflow-x: auto;
}
.event-chip {
  flex: none;
  background: var(--surface2);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 12px; white-space: nowrap;
  display: flex; align-items: center; gap: 6px;
}
.event-chip .undo-btn {
  background: none; border: none; color: var(--red);
  font-size: 14px; cursor: pointer; padding: 0 2px;
}
.event-log-label { font-size: 11px; color: var(--text2); flex: none; }

/* More actions panel */
.more-panel {
  display: none;
  flex-direction: column; gap: 6px;
  padding: 6px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.more-panel.open { display: flex; }

/* Sync indicator */
.sync-indicator {
  position: fixed; bottom: 8px; right: 8px;
  width: 12px; height: 12px; border-radius: 50%;
  z-index: 50;
}
.sync-indicator.online { background: var(--green); }
.sync-indicator.offline { background: var(--red); }
.sync-indicator.syncing { background: var(--yellow); animation: pulse .8s infinite; }
@keyframes pulse { 0%,100%{ opacity: 1; } 50%{ opacity: .3; } }

/* Timeout display */
.timeout-display {
  font-size: 11px; color: var(--text2); padding: 0 6px;
  display: flex; align-items: center; gap: 4px;
}
.timeout-dot {
  width: 10px; height: 10px; border-radius: 50%;
  border: 1px solid var(--text2);
}
.timeout-dot.used { background: var(--yellow); border-color: var(--yellow); }

/* Scrollbar */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* Player select grid (numbers only, large buttons) */
.player-select-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 8px;
}
.player-select-btn {
  aspect-ratio: 1;
  min-height: 64px;
  border-radius: 10px;
  border: 2px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 26px;
  font-weight: 800;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform .1s;
}
.player-select-btn:active {
  transform: scale(.92);
  background: var(--surface3);
  border-color: var(--orange);
}

/* Foul summary popup */
.foul-summary { text-align: center; }
.foul-summary h3 { margin-bottom: 16px; }
.foul-summary-player { display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 4px; }
.foul-summary-team { display: flex; align-items: center; justify-content: center; gap: 10px; margin-top: 16px; margin-bottom: 4px; }
.foul-count-big {
  font-size: 42px; font-weight: 900; color: var(--red); line-height: 1;
}
.foul-count-big.team { color: var(--yellow); }
.foul-label { font-size: 14px; color: var(--text2); }
.foul-summary-name { font-size: 18px; font-weight: 700; }
.foul-summary-teamname { font-size: 14px; color: var(--text2); }
.foul-warning {
  margin-top: 12px; padding: 10px;
  background: #5a1a1a; border-radius: 8px;
  color: var(--red); font-weight: 700; font-size: 14px;
}
.foul-warning.team-foul-warning { background: #4a3a0a; color: var(--yellow); }

/* Timeout timer overlay */
.timeout-timer-overlay { text-align: center; }
.timeout-timer-label { font-size: 14px; text-transform: uppercase; letter-spacing: 2px; color: var(--yellow); margin-bottom: 8px; }
.timeout-timer-clock { font-size: 72px; font-weight: 900; color: var(--text); line-height: 1; font-variant-numeric: tabular-nums; }
.timeout-timer-sub { font-size: 13px; color: var(--text2); }

/* Header menu dropdown */
.header-menu {
  position: absolute; top: 50px; right: 10px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 6px;
  z-index: 90; min-width: 180px;
  display: none;
}
.header-menu.open { display: block; }
.header-menu button {
  width: 100%; text-align: left;
  padding: 12px; border: none;
  background: none; color: var(--text);
  font-size: 14px; cursor: pointer;
  border-radius: 6px;
}
.header-menu button:active { background: var(--surface2); }

/* Quarter scores row */
.quarter-scores {
  display: flex; justify-content: center; gap: 6px;
  padding: 4px 12px; flex: none;
  font-size: 11px; color: var(--text2);
  background: var(--bg);
  flex-wrap: wrap;
}
.quarter-score-item {
  background: var(--surface);
  border-radius: 4px;
  padding: 2px 8px;
  white-space: nowrap;
}
.quarter-score-item .qs-label { color: var(--text2); }
.quarter-score-item .qs-home { color: var(--home); font-weight: 700; }
.quarter-score-item .qs-away { color: var(--away); font-weight: 700; }

/* History screen */
#history-screen { padding: 16px; overflow-y: auto; }
.history-header {
  display: flex; align-items: center; gap: 16px; margin-bottom: 16px;
}
.history-header h1 { font-size: 20px; color: var(--orange); }
.history-back-btn {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 16px;
  color: var(--text); font-size: 14px; cursor: pointer;
}
.history-back-btn:active { background: var(--surface2); }

.history-list { display: flex; flex-direction: column; gap: 8px; }
.history-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  display: flex; justify-content: space-between; align-items: center;
  cursor: pointer;
  border: 1px solid var(--border);
}
.history-card:active { background: var(--surface2); }
.history-card-teams { font-size: 16px; font-weight: 700; }
.history-card-score { font-size: 20px; font-weight: 800; }
.history-card-date { font-size: 12px; color: var(--text2); }
.history-card-status { font-size: 11px; padding: 2px 8px; border-radius: 4px; }
.history-card-status.active { background: #1a4a2a; color: var(--green); }
.history-card-status.finished { background: var(--surface2); color: var(--text2); }
.history-empty { text-align: center; color: var(--text2); padding: 40px; font-size: 14px; }

.history-btn {
  width: 100%; padding: 14px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text2); font-size: 15px;
  cursor: pointer; margin-top: 8px;
}
.history-btn:active { background: var(--surface2); }

/* Roster editor */
.roster-editor { max-height: 70vh; overflow-y: auto; }
.roster-editor h3 { color: var(--orange); text-align: center; margin-bottom: 12px; }
.roster-team-section { margin-bottom: 16px; }
.roster-team-label { font-size: 13px; text-transform: uppercase; letter-spacing: 2px; margin-bottom: 8px; font-weight: 700; }
.roster-team-label.home { color: var(--home); }
.roster-team-label.away { color: var(--away); }
.roster-player-row {
  display: flex; gap: 6px; align-items: center; margin-bottom: 6px;
}
.roster-player-row input {
  min-height: 40px; font-size: 14px; padding: 6px 10px;
}
.roster-player-row input:first-child { max-width: 60px; text-align: center; flex: none; }
.roster-player-row .roster-remove {
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--surface3); border: none; color: var(--red);
  font-size: 18px; cursor: pointer; flex: none;
  display: flex; align-items: center; justify-content: center;
}
.roster-player-row .roster-remove:disabled { opacity: .3; pointer-events: none; }
.roster-add-btn {
  padding: 8px; background: var(--surface2); border: 1px dashed var(--border);
  border-radius: 6px; color: var(--text2); font-size: 13px;
  cursor: pointer; text-align: center; width: 100%; margin-top: 4px;
}
.roster-add-btn:active { background: var(--surface3); }
