:root {
  --bg: #05070c;
  --panel: #0d121c;
  --panel-2: #121a28;
  --border: #1e2a3d;
  --text: #f4f7fb;
  --muted: #8b9bb4;
  --home: #3b82f6;
  --home-glow: rgba(59, 130, 246, 0.35);
  --away: #ef4444;
  --away-glow: rgba(239, 68, 68, 0.35);
  --accent: #fbbf24;
  --accent-dim: #b45309;
  --ok: #22c55e;
  --warn: #f59e0b;
  --danger: #dc2626;
  --score-size: clamp(4.5rem, 18vw, 14rem);
  --clock-size: clamp(2.8rem, 10vw, 7rem);
  --shot-size: clamp(2rem, 6vw, 4.5rem);
  --name-size: clamp(1.1rem, 3.5vw, 2.6rem);
  --radius: 12px;
  --font: "SF Pro Display", "Segoe UI", system-ui, -apple-system, sans-serif;
  --mono: "SF Mono", "Menlo", "Consolas", ui-monospace, monospace;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
  user-select: none;
  -webkit-user-select: none;
}

body.mode-display {
  overflow: hidden;
}

#app {
  min-height: 100%;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background:
    radial-gradient(ellipse at 20% 0%, rgba(59, 130, 246, 0.08), transparent 50%),
    radial-gradient(ellipse at 80% 0%, rgba(239, 68, 68, 0.08), transparent 50%),
    var(--bg);
}

/* ── Top bar ── */
.top-bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
}

.room-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  justify-self: start;
}

.room-label, .meta-label {
  font-size: 0.65rem;
  letter-spacing: 0.14em;
  color: var(--muted);
  font-weight: 700;
}

.room-code {
  font-family: var(--mono);
  font-weight: 800;
  font-size: 1rem;
  letter-spacing: 0.12em;
  color: var(--accent);
  background: var(--panel);
  padding: 0.2rem 0.55rem;
  border-radius: 6px;
  border: 1px solid var(--border);
}

.conn {
  font-size: 0.85rem;
  line-height: 1;
}
.conn.connected { color: var(--ok); }
.conn.disconnected { color: var(--danger); animation: blink 1.2s infinite; }

@keyframes blink {
  50% { opacity: 0.35; }
}

.game-title {
  justify-self: center;
  font-size: clamp(1rem, 2.5vw, 1.75rem);
  font-weight: 800;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--text);
  padding: 0.15rem 0.75rem;
  border-radius: 6px;
  outline: none;
  min-width: 4ch;
  text-align: center;
}

.game-title:focus, [contenteditable]:focus {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  background: rgba(251, 191, 36, 0.08);
}

.top-actions {
  justify-self: end;
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.btn-link, .btn-icon, .controls button {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  font-weight: 700;
  font-size: 0.8rem;
  padding: 0.45rem 0.7rem;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.btn-icon {
  padding: 0.45rem 0.65rem;
  font-size: 1rem;
}

.btn-link:active, .btn-icon:active, .controls button:active {
  transform: scale(0.96);
}

.controls button:hover, .btn-link:hover, .btn-icon:hover {
  border-color: var(--accent);
  background: #1a2436;
}

.controls button.danger {
  border-color: #7f1d1d;
  color: #fecaca;
  background: #1a0a0a;
}

/* ── Scoreboard ── */
.scoreboard {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: clamp(0.5rem, 2vw, 1.5rem);
  align-items: center;
  padding: clamp(0.5rem, 2vw, 1.5rem);
  min-height: 0;
}

.team {
  position: relative;
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: clamp(0.75rem, 2vw, 1.5rem);
  text-align: center;
  min-width: 0;
}

.team.home {
  box-shadow: inset 0 0 0 1px rgba(59, 130, 246, 0.25), 0 0 40px var(--home-glow);
}
.team.away {
  box-shadow: inset 0 0 0 1px rgba(239, 68, 68, 0.25), 0 0 40px var(--away-glow);
}

.team-name {
  font-size: var(--name-size);
  font-weight: 900;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  line-height: 1.1;
  margin-bottom: 0.25rem;
  outline: none;
  word-break: break-word;
}

.team.home .team-name { color: #93c5fd; }
.team.away .team-name { color: #fca5a5; }

.score {
  font-family: var(--mono);
  font-size: var(--score-size);
  font-weight: 800;
  line-height: 0.95;
  letter-spacing: -0.02em;
  outline: none;
  color: #fff;
  text-shadow: 0 0 30px rgba(255, 255, 255, 0.15);
  padding: 0.1em 0;
}

.team-meta {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: clamp(0.75rem, 3vw, 1.75rem);
  margin-top: 0.75rem;
  flex-wrap: wrap;
}

.meta-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
}

.meta-value {
  font-family: var(--mono);
  font-size: clamp(1.25rem, 3.5vw, 2.25rem);
  font-weight: 800;
  outline: none;
  min-width: 1.5ch;
}

.bonus {
  min-height: 1.4em;
  min-width: 4ch;
  font-size: clamp(0.75rem, 1.8vw, 1.1rem);
  font-weight: 900;
  letter-spacing: 0.12em;
  color: var(--accent);
  outline: none;
  align-self: center;
}

.poss-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  font-size: clamp(1.5rem, 4vw, 2.5rem);
  color: var(--accent);
  opacity: 0.15;
  transition: opacity 0.2s, text-shadow 0.2s;
  pointer-events: none;
}
.home-poss { right: -0.15em; }
.away-poss { left: -0.15em; }
.poss-arrow.active {
  opacity: 1;
  text-shadow: 0 0 18px var(--accent);
}

/* Center column */
.center {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(0.6rem, 2vw, 1.25rem);
  padding: 0.5rem;
  min-width: clamp(140px, 22vw, 280px);
}

.period-wrap, .shot-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
}

.period {
  font-family: var(--mono);
  font-size: clamp(1.5rem, 4vw, 3rem);
  font-weight: 800;
  outline: none;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.15rem 0.75rem;
  min-width: 2ch;
  text-align: center;
}

.clock-wrap {
  position: relative;
  background: #02040a;
  border: 2px solid #2a3a55;
  border-radius: 14px;
  padding: clamp(0.5rem, 1.5vw, 1rem) clamp(0.75rem, 2vw, 1.5rem);
  box-shadow: inset 0 0 40px rgba(0, 0, 0, 0.8), 0 0 20px rgba(251, 191, 36, 0.1);
}

.game-clock {
  font-family: var(--mono);
  font-size: var(--clock-size);
  font-weight: 800;
  letter-spacing: 0.04em;
  color: var(--accent);
  outline: none;
  text-align: center;
  min-width: 5ch;
  line-height: 1;
}

.game-clock.low { color: #f87171; animation: pulse 1s infinite; }
.shot-clock.low { color: #f87171; animation: pulse 0.6s infinite; }

@keyframes pulse {
  50% { opacity: 0.55; }
}

.run-badge {
  position: absolute;
  top: 0.35rem;
  right: 0.5rem;
  font-size: 0.7rem;
  color: var(--ok);
}
.run-badge.hidden { display: none; }

.shot-clock {
  font-family: var(--mono);
  font-size: var(--shot-size);
  font-weight: 800;
  color: #67e8f9;
  outline: none;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.2rem 0.6rem;
  min-width: 2.5ch;
  text-align: center;
  line-height: 1;
}

.shot-wrap.disabled {
  opacity: 0.35;
}

/* Notes */
.notes-bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.4rem 1rem;
  border-top: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.35);
}

.notes {
  flex: 1;
  font-size: clamp(0.85rem, 1.5vw, 1.1rem);
  color: var(--muted);
  outline: none;
  min-height: 1.4em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.notes:empty::before {
  content: attr(data-placeholder);
  opacity: 0.5;
}

.notes:focus {
  white-space: normal;
  overflow: visible;
  color: var(--text);
}

/* Controls */
.controls {
  border-top: 1px solid var(--border);
  background: linear-gradient(180deg, #0a0f18, #070b12);
  padding: 0.75rem;
}

.ctrl-grid {
  display: grid;
  grid-template-columns: 1fr 1.2fr 1fr;
  gap: 0.75rem;
  max-width: 1200px;
  margin: 0 auto;
}

.ctrl-team, .ctrl-center {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem;
}

.ctrl-team h3, .ctrl-center h3 {
  margin: 0 0 0.45rem;
  font-size: 0.75rem;
  letter-spacing: 0.14em;
  color: var(--muted);
  text-align: center;
}

.ctrl-team:first-child h3 { color: #93c5fd; }
.ctrl-team:last-child h3 { color: #fca5a5; }

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  justify-content: center;
  margin-bottom: 0.35rem;
}

.controls button {
  flex: 1 1 auto;
  min-width: 3.2rem;
  padding: 0.55rem 0.5rem;
  font-size: 0.85rem;
  touch-action: manipulation;
}

/* Display mode */
body.mode-display .controls,
body.mode-display .top-actions,
body.mode-display .notes-bar {
  display: none !important;
}

body.mode-display .top-bar {
  grid-template-columns: auto 1fr auto;
  padding: 0.4rem 1.25rem;
}

body.mode-display {
  --score-size: clamp(6rem, 22vw, 18rem);
  --clock-size: clamp(3.5rem, 12vw, 9rem);
  --shot-size: clamp(2.5rem, 7vw, 5.5rem);
  --name-size: clamp(1.4rem, 4.5vw, 3.2rem);
}

body.mode-display [contenteditable] {
  pointer-events: none;
  caret-color: transparent;
}

body.mode-display .scoreboard {
  padding: 1rem 1.5rem 1.5rem;
}

/* Phone / narrow */
@media (max-width: 720px) {
  .scoreboard {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto auto;
  }
  .center {
    grid-column: 1 / -1;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-around;
    min-width: 0;
    order: -1;
  }
  .ctrl-grid {
    grid-template-columns: 1fr;
  }
  .top-bar {
    grid-template-columns: 1fr;
    justify-items: center;
    gap: 0.35rem;
  }
  .room-meta, .top-actions {
    justify-self: center;
  }
  .poss-arrow {
    display: none;
  }
  .team.home::after,
  .team.away::after {
    content: none;
  }
  body:not(.mode-display) .team.home.has-poss {
    outline: 2px solid var(--accent);
  }
  body:not(.mode-display) .team.away.has-poss {
    outline: 2px solid var(--accent);
  }
}

/* Landscape phone / tablet control */
@media (max-height: 500px) and (orientation: landscape) {
  body:not(.mode-display) .scoreboard {
    padding: 0.35rem;
  }
  body:not(.mode-display) {
    --score-size: clamp(2.5rem, 12vh, 5rem);
    --clock-size: clamp(1.8rem, 10vh, 3.5rem);
    --shot-size: clamp(1.4rem, 8vh, 2.5rem);
    --name-size: clamp(0.9rem, 4vh, 1.4rem);
  }
  .controls { padding: 0.4rem; }
  .controls button { padding: 0.4rem 0.4rem; font-size: 0.75rem; }
}

/* Apple TV / large landscape display */
@media (min-width: 1000px) and (min-aspect-ratio: 16/10) {
  body.mode-display .scoreboard {
    padding: 1.5rem 2.5rem 2rem;
  }
}

.toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  background: #1e293b;
  color: #fff;
  padding: 0.65rem 1.1rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  font-size: 0.9rem;
  font-weight: 600;
  z-index: 100;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  pointer-events: none;
}
.toast.hidden { display: none; }

[contenteditable] {
  -webkit-user-select: text;
  user-select: text;
  cursor: text;
}
