:root {
  --bg: #0f172a;
  --bg-2: #1e293b;
  --bg-3: #334155;
  --fg: #f1f5f9;
  --fg-dim: #94a3b8;
  --accent: #38bdf8;
  --team-a: #0ea5e9;
  --team-b: #f59e0b;
  --danger: #ef4444;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

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

html,
body {
  height: 100%;
  height: 100dvh;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", system-ui, sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

#app {
  height: 100%;
  position: relative;
  overflow: hidden;
}

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
}

.hidden {
  display: none !important;
}

/* ---------- Match view ---------- */
#match-view {
  flex-direction: row;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

.team {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
  padding: 1rem;
  border: none;
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
  position: relative;
  transition: background 100ms ease;
}

.team-a {
  background: linear-gradient(180deg,
    color-mix(in srgb, var(--team-a) 28%, var(--bg)) 0%,
    var(--bg) 70%);
}

.team-b {
  background: linear-gradient(180deg,
    color-mix(in srgb, var(--team-b) 28%, var(--bg)) 0%,
    var(--bg) 70%);
}

.team:active {
  filter: brightness(1.25);
}

.team-name {
  font-size: 0.9rem;
  color: var(--fg-dim);
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-weight: 600;
}

.prev-sets {
  font-size: 1.1rem;
  color: var(--fg-dim);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.15em;
  min-height: 1.4em;
}

.games {
  font-size: 2.2rem;
  color: var(--fg);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.points {
  font-size: clamp(4rem, 26vmin, 11rem);
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.04em;
  line-height: 0.9;
  margin-top: 0.5rem;
}

.badges {
  position: absolute;
  top: calc(var(--safe-top) + 1rem);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 0.5rem;
  z-index: 5;
  pointer-events: none;
}

.badge {
  background: var(--bg-2);
  color: var(--fg);
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

.controls {
  position: absolute;
  bottom: calc(var(--safe-bottom) + 1rem);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 0.75rem;
  z-index: 6;
}

.controls button {
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  background: var(--bg-2);
  color: var(--fg);
  border: 1px solid rgba(255, 255, 255, 0.12);
  font-size: 1.4rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

.controls button:active {
  background: var(--bg-3);
}

/* ---------- Settings + History views ---------- */
#settings-view,
#history-view {
  background: var(--bg);
  padding: var(--safe-top) 1.25rem var(--safe-bottom);
  overflow-y: auto;
}

#settings-view header,
#history-view header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 0;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 2;
}

#settings-view header h1,
#history-view header h1 {
  font-size: 1.1rem;
  font-weight: 600;
}

#settings-view header button,
#history-view header button {
  background: transparent;
  border: none;
  color: var(--fg);
  font-size: 1.8rem;
  cursor: pointer;
  padding: 0 0.5rem;
}

#settings-view h2 {
  margin: 1.5rem 0 0.75rem;
  font-size: 0.8rem;
  color: var(--fg-dim);
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-weight: 600;
}

#settings-view label {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

#settings-view label > span {
  font-size: 0.95rem;
}

#settings-view label.checkbox {
  flex-direction: row;
  align-items: flex-start;
  gap: 0.75rem;
  background: var(--bg-2);
  padding: 1rem;
  border-radius: 0.75rem;
}

#settings-view label.checkbox span small {
  display: block;
  color: var(--fg-dim);
  font-size: 0.8rem;
  margin-top: 0.25rem;
}

#settings-view input[type="text"],
#settings-view select {
  background: var(--bg-2);
  color: var(--fg);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 0.5rem;
  padding: 0.85rem 0.9rem;
  font-size: 1rem;
  font-family: inherit;
}

#settings-view input[type="text"]:focus,
#settings-view select:focus {
  outline: 2px solid var(--accent);
  border-color: transparent;
}

#settings-view input[type="checkbox"] {
  width: 1.2rem;
  height: 1.2rem;
  accent-color: var(--accent);
  margin-top: 0.15rem;
  flex-shrink: 0;
}

.actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin: 2rem 0 1rem;
}

.actions button {
  background: var(--bg-2);
  color: var(--fg);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 0.6rem;
  padding: 1rem;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

.actions button.danger {
  background: var(--danger);
  border-color: transparent;
}

#history-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding-bottom: 2rem;
}

#history-list li {
  background: var(--bg-2);
  border-radius: 0.75rem;
  padding: 0.9rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

#history-list .row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

#history-list .row .name {
  font-weight: 600;
}

#history-list .row .name.winner {
  color: var(--accent);
}

#history-list .row .score {
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.08em;
  color: var(--fg-dim);
}

#history-list .meta {
  font-size: 0.8rem;
  color: var(--fg-dim);
  margin-top: 0.4rem;
}

.empty {
  text-align: center;
  color: var(--fg-dim);
  padding: 2rem 1rem;
}

.hint {
  color: var(--fg-dim);
  font-size: 0.85rem;
  line-height: 1.5;
  margin-bottom: 0.75rem;
}

#watch-test {
  background: var(--bg-2);
  color: var(--fg);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 0.6rem;
  padding: 1rem;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
}

#watch-test-status {
  margin-top: 0.5rem;
  font-size: 0.85rem;
  min-height: 1em;
}

#watch-test-status.ok { color: #4ade80; }
#watch-test-status.err { color: var(--danger); }

/* ---------- Share modal ---------- */
.share-url-row {
  display: flex;
  gap: 0.5rem;
  margin: 0.5rem 0;
}

.share-url-row input {
  flex: 1;
  background: var(--bg);
  color: var(--fg);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 0.5rem;
  padding: 0.7rem 0.75rem;
  font-size: 0.85rem;
  font-family: ui-monospace, SFMono-Regular, monospace;
}

.share-url-row button {
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: 0.5rem;
  padding: 0.7rem 1rem;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
}

#share-code {
  font-family: ui-monospace, SFMono-Regular, monospace;
  letter-spacing: 0.15em;
  font-size: 1.4em;
  color: var(--accent);
}

.card-actions button.danger {
  background: var(--danger);
  color: white;
}

.pairing {
  background: var(--bg);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 0.5rem;
  padding: 0.75rem 1rem;
  margin: 0.5rem 0;
  text-align: left;
}

.pairing summary {
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--fg-dim);
}

.pairing summary:hover {
  color: var(--fg);
}

.pairing[open] summary {
  margin-bottom: 0.5rem;
}

/* ---------- Spectator mode ---------- */
body.spectator .team {
  cursor: default;
  pointer-events: none;
}

body.spectator .controls #undo-btn {
  display: none;
}

#spectator-badge {
  background: var(--accent);
  color: var(--bg);
  border-color: transparent;
}

#status-badge {
  background: var(--bg-2);
}

#status-badge.disconnected {
  background: var(--danger);
  color: white;
  border-color: transparent;
}

#status-badge.connected {
  display: none !important;
}

/* ---------- Winner overlay ---------- */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  padding: 1rem;
}

.card {
  background: var(--bg-2);
  border-radius: 1.2rem;
  padding: 2rem 1.5rem;
  max-width: 400px;
  width: 100%;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.card .eyebrow {
  font-size: 0.75rem;
  color: var(--fg-dim);
  text-transform: uppercase;
  letter-spacing: 0.15em;
  font-weight: 600;
}

.card h1 {
  font-size: 1.8rem;
  font-weight: 800;
  line-height: 1.1;
}

.card h1 span {
  color: var(--accent);
}

.card .summary {
  font-size: 1.1rem;
  color: var(--fg-dim);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.05em;
}

.card .stats {
  font-size: 0.85rem;
  color: var(--fg-dim);
  line-height: 1.4;
}

.card-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.card-actions button {
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: 0.6rem;
  padding: 0.9rem;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}

.card-actions button.ghost {
  background: transparent;
  color: var(--fg);
  border: 1px solid rgba(255, 255, 255, 0.12);
}

/* ---------- Landscape tweaks ---------- */
@media (orientation: portrait) and (max-aspect-ratio: 1/2) {
  .points {
    font-size: clamp(3rem, 18vmin, 8rem);
  }
}

@media (orientation: landscape) and (min-aspect-ratio: 5/3) {
  .points {
    font-size: clamp(5rem, 24vmin, 13rem);
  }
}
