/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f8fafc;
  --bg2: #f1f5f9;
  --bg3: #e2e8f0;
  --card: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --text2: #475569;
  --text3: #94a3b8;
  --primary: #16a34a;
  --primary-hover: #15803d;
  --primary-fg: #ffffff;
  --danger: #dc2626;
  --score-color: #0f172a;
  --player-active: #16a34a;
  --shadow: 0 4px 24px rgba(0,0,0,.08);
  --shadow-lg: 0 8px 40px rgba(0,0,0,.14);
  --radius: 16px;
  --radius-sm: 10px;
  --transition: 200ms cubic-bezier(.4,0,.2,1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --bg2: #1e293b;
    --bg3: #334155;
    --card: #1e293b;
    --border: #334155;
    --text: #f1f5f9;
    --text2: #94a3b8;
    --text3: #475569;
    --primary: #22c55e;
    --primary-hover: #16a34a;
    --primary-fg: #0f172a;
    --danger: #f87171;
    --score-color: #f1f5f9;
    --shadow: 0 4px 24px rgba(0,0,0,.4);
    --shadow-lg: 0 8px 40px rgba(0,0,0,.6);
  }
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* ── Views ── */
.view {
  position: fixed;
  inset: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition);
}
.view.active {
  opacity: 1;
  pointer-events: all;
}
.view.hidden { display: none; }

/* ── HOME ── */
.home-container {
  max-width: 480px;
  margin: 0 auto;
  padding: 48px 20px 40px;
  display: flex;
  flex-direction: column;
  gap: 32px;
}

.logo {
  text-align: center;
  padding-top: env(safe-area-inset-top, 0px);
}
.logo-icon { font-size: 56px; display: block; margin-bottom: 8px; }
.logo h1 { font-size: 2rem; font-weight: 800; letter-spacing: -.5px; }
.logo-sub { color: var(--text2); font-size: .95rem; margin-top: 4px; }

.section-title {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: .06em;
  margin-bottom: 12px;
}

.history-list { display: flex; flex-direction: column; gap: 8px; }

.empty-state { color: var(--text3); font-size: .9rem; text-align: center; padding: 24px 0; }

.history-item {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  transition: background var(--transition), transform var(--transition);
}
.history-item:hover { background: var(--bg2); transform: translateY(-1px); }
.history-item:active { transform: scale(.98); }

.hi-names { font-weight: 600; font-size: .95rem; }
.hi-score { font-size: 1.1rem; font-weight: 800; font-variant-numeric: tabular-nums; color: var(--primary); }
.hi-date { font-size: .75rem; color: var(--text3); margin-top: 2px; }
.hi-status {
  font-size: .7rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 99px;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.hi-status.active { background: rgba(34,197,94,.15); color: var(--primary); }
.hi-status.completed { background: var(--bg3); color: var(--text3); }

/* ── BUTTONS ── */
.btn-primary {
  background: var(--primary);
  color: var(--primary-fg);
  border: none;
  border-radius: var(--radius-sm);
  padding: 14px 28px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: background var(--transition), transform var(--transition), box-shadow var(--transition);
  width: 100%;
}
.btn-primary.btn-lg { padding: 18px 28px; font-size: 1.1rem; border-radius: var(--radius); }
.btn-primary.btn-sm { width: auto; padding: 10px 16px; font-size: .85rem; border-radius: 8px; }
.btn-primary:hover { background: var(--primary-hover); box-shadow: 0 4px 16px rgba(22,163,74,.3); }
.btn-primary:active { transform: scale(.97); }

.btn-ghost {
  background: transparent;
  color: var(--text2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 28px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition), transform var(--transition);
  width: 100%;
}
.btn-ghost.btn-sm { width: auto; padding: 10px 16px; font-size: .85rem; border-radius: 8px; }
.btn-ghost:hover { background: var(--bg2); }
.btn-ghost:active { transform: scale(.97); }

.icon-btn {
  background: var(--bg2);
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background var(--transition);
  flex-shrink: 0;
}
.icon-btn svg { width: 18px; height: 18px; color: var(--text2); stroke: var(--text2); }
.icon-btn:hover { background: var(--bg3); }
.icon-btn:active { transform: scale(.92); }

/* ── MODAL ── */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.modal.hidden { display: none; }

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.5);
  backdrop-filter: blur(4px);
}

.modal-card {
  position: relative;
  background: var(--card);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 28px 24px calc(28px + env(safe-area-inset-bottom, 0px));
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  animation: slide-up 280ms cubic-bezier(.4,0,.2,1);
}

@media (min-width: 480px) {
  .modal { align-items: center; }
  .modal-card {
    border-radius: var(--radius);
    max-width: 400px;
    animation: pop-in 220ms cubic-bezier(.4,0,.2,1);
  }
}

@keyframes slide-up {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}
@keyframes pop-in {
  from { transform: scale(.92); opacity: 0; }
  to   { transform: scale(1); opacity: 1; }
}

.modal-card h2 { font-size: 1.3rem; font-weight: 800; }

.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group label { font-size: .85rem; font-weight: 600; color: var(--text2); }
.form-group input {
  background: var(--bg2);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 13px 14px;
  font-size: 1rem;
  color: var(--text);
  outline: none;
  transition: border-color var(--transition);
  width: 100%;
}
.form-group input:focus { border-color: var(--primary); }

.modal-actions { display: flex; gap: 10px; }
.modal-actions .btn-ghost { flex: 1; }
.modal-actions .btn-primary { flex: 2; }

/* ── MATCH VIEW ── */
#view-match { display: flex; flex-direction: column; background: var(--bg); }
#view-match.hidden { display: none; }

.match-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(env(safe-area-inset-top, 12px) + 12px) 16px 12px;
  flex-shrink: 0;
}

.match-meta { display: flex; align-items: center; gap: 8px; }

.match-status-badge {
  font-size: .75rem;
  font-weight: 700;
  padding: 4px 12px;
  border-radius: 99px;
  background: rgba(34,197,94,.15);
  color: var(--primary);
  text-transform: uppercase;
  letter-spacing: .05em;
}
.match-status-badge.finished {
  background: var(--bg3);
  color: var(--text3);
}

.serve-indicator {
  text-align: center;
  padding: 4px 16px 8px;
  font-size: .85rem;
  font-weight: 600;
  color: var(--text2);
  flex-shrink: 0;
}

/* ── SCORE AREA ── */
.score-area {
  flex: 1;
  display: flex;
  align-items: stretch;
  gap: 0;
  padding: 8px 12px;
  min-height: 0;
}

.player-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 16px 8px;
  border-radius: var(--radius);
  transition: background var(--transition);
}
.player-panel.leading { background: rgba(34,197,94,.06); }

.player-name {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text2);
  text-align: center;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.score-display {
  font-size: clamp(5rem, 18vw, 9rem);
  font-weight: 900;
  line-height: 1;
  color: var(--score-color);
  font-variant-numeric: tabular-nums;
  transition: transform 120ms cubic-bezier(.34,1.56,.64,1);
}
.score-display.bump { transform: scale(1.18); }

.score-controls {
  display: flex;
  gap: 10px;
}

.score-btn {
  border: none;
  border-radius: 50%;
  width: 52px;
  height: 52px;
  font-size: 1.6rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform var(--transition), box-shadow var(--transition), background var(--transition);
  line-height: 1;
}
.score-btn:active { transform: scale(.88); }
.score-btn:disabled { opacity: .3; cursor: not-allowed; }

.score-btn-plus {
  background: var(--primary);
  color: var(--primary-fg);
  width: 64px;
  height: 64px;
  font-size: 2rem;
  box-shadow: 0 4px 16px rgba(22,163,74,.35);
}
.score-btn-plus:hover:not(:disabled) { box-shadow: 0 6px 24px rgba(22,163,74,.5); }

.score-btn-minus {
  background: var(--bg3);
  color: var(--text2);
}
.score-btn-minus:hover:not(:disabled) { background: var(--border); }

.score-divider {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
  flex-shrink: 0;
}
.divider-dash {
  color: var(--text3);
  font-size: 1.5rem;
  font-weight: 300;
}

/* ── MATCH FOOTER ── */
.match-footer {
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px));
  display: flex;
  justify-content: center;
  flex-shrink: 0;
}
.match-footer .btn-ghost {
  width: auto;
  padding: 10px 24px;
  font-size: .85rem;
  color: var(--text3);
  border-color: transparent;
}
.match-footer .btn-ghost:hover { border-color: var(--border); color: var(--text2); }

/* ── SHARE MODAL ── */
.share-hint { font-size: .9rem; color: var(--text2); }

.qrcode-container {
  display: flex;
  justify-content: center;
  padding: 16px;
  background: white;
  border-radius: var(--radius-sm);
}
.qrcode-container canvas, .qrcode-container img { border-radius: 4px; }

.share-link-row {
  display: flex;
  gap: 8px;
}
.share-link-row input {
  flex: 1;
  background: var(--bg2);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: .8rem;
  color: var(--text2);
  outline: none;
  min-width: 0;
}

/* ── VICTORY OVERLAY ── */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fade-in 400ms ease;
}
.overlay.hidden { display: none; }

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.victory-content {
  text-align: center;
  padding: 32px 24px;
  max-width: 400px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  animation: pop-in 500ms cubic-bezier(.34,1.56,.64,1);
}

.victory-emoji {
  font-size: 80px;
  animation: trophy-bounce 1s cubic-bezier(.34,1.56,.64,1);
}
@keyframes trophy-bounce {
  0%   { transform: scale(0) rotate(-20deg); }
  60%  { transform: scale(1.2) rotate(5deg); }
  100% { transform: scale(1) rotate(0deg); }
}

.victory-title {
  font-size: 2.5rem;
  font-weight: 900;
  letter-spacing: -.5px;
}

.victory-winner {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--primary);
}

.victory-scores {
  font-size: 3.5rem;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  letter-spacing: .05em;
  color: var(--text2);
}

.victory-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 280px;
}

/* ── Landscape layout ── */
@media (orientation: landscape) and (max-height: 600px) {
  .score-display { font-size: clamp(4rem, 14vh, 7rem); }
  .score-btn-plus { width: 56px; height: 56px; font-size: 1.7rem; }
  .score-btn-minus { width: 44px; height: 44px; }
  .match-topbar { padding-top: 8px; padding-bottom: 8px; }
  .serve-indicator { padding: 2px 16px 4px; }
}

/* ── Utility ── */
.copy-success { color: var(--primary) !important; }
