/* ---------- Apple HIG-inspired theme tokens ---------- */
:root {
  /* Light (default) */
  --bg: #f5f5f7;
  --bg-elevated: #ffffff;
  --text: #1d1d1f;
  --text-secondary: #6e6e73;
  --fill: rgba(0, 0, 0, 0.05);
  --fill-strong: rgba(0, 0, 0, 0.08);
  --separator: rgba(0, 0, 0, 0.08);
  --glass-bg: rgba(255, 255, 255, 0.72);
  --glass-border: rgba(0, 0, 0, 0.06);
  --shadow-soft: rgba(0, 0, 0, 0.08);
  --accent: #0071e3;
  --accent-soft: rgba(0, 113, 227, 0.12);
  --ring-track: rgba(0, 0, 0, 0.08);
  --spotlight-dim: rgba(0, 0, 0, 0.22);
  --grid-line: rgba(0, 113, 227, 0.1);
  --glow-1: rgba(0, 113, 227, 0.14);
  --glow-2: rgba(175, 82, 222, 0.1);
  --title-glow: rgba(0, 113, 227, 0.35);
  --ease: cubic-bezier(0.25, 0.1, 0.25, 1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --bg-elevated: #1c1c1e;
    --text: #f5f5f7;
    --text-secondary: #86868b;
    --fill: rgba(255, 255, 255, 0.08);
    --fill-strong: rgba(255, 255, 255, 0.14);
    --separator: rgba(255, 255, 255, 0.1);
    --glass-bg: rgba(28, 28, 30, 0.72);
    --glass-border: rgba(255, 255, 255, 0.08);
    --shadow-soft: rgba(0, 0, 0, 0.4);
    --accent: #2997ff;
    --accent-soft: rgba(41, 151, 255, 0.16);
    --ring-track: rgba(255, 255, 255, 0.14);
    --spotlight-dim: rgba(0, 0, 0, 0.6);
    --grid-line: rgba(41, 151, 255, 0.16);
    --glow-1: rgba(41, 151, 255, 0.2);
    --glow-2: rgba(191, 90, 242, 0.16);
    --title-glow: rgba(41, 151, 255, 0.55);
  }
}

/* ---------- Reset & base ---------- */
* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  transition: background 0.4s var(--ease), color 0.4s var(--ease);
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: max(14px, env(safe-area-inset-top)) 18px 14px;
  z-index: 20;
  position: relative;
  background: var(--glass-bg);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  box-shadow: 0 1px 0 var(--separator);
}

.title {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.01em;
  background: linear-gradient(90deg, var(--accent), #af52de);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 12px var(--title-glow));
}

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--accent-soft);
  background: var(--fill);
  color: var(--text);
  font-size: 19px;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  box-shadow: 0 0 0 rgba(0, 113, 227, 0);
  transition: transform 0.3s var(--ease), background 0.25s var(--ease), box-shadow 0.3s var(--ease);
}

.icon-btn:active {
  background: var(--fill-strong);
  box-shadow: 0 0 16px var(--accent-soft);
  transform: scale(0.94);
}

.icon-btn.active {
  background: var(--accent-soft);
  color: var(--accent);
  box-shadow: 0 0 18px var(--accent-soft);
  transform: rotate(90deg);
}

.icon-btn.spin {
  animation: spin 0.5s var(--ease);
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(-360deg); }
}

/* ---------- Touch surface ---------- */
.surface {
  position: relative;
  flex: 1;
  overflow: hidden;
  background:
    radial-gradient(circle at 15% 15%, var(--glow-1), transparent 45%),
    radial-gradient(circle at 85% 25%, var(--glow-2), transparent 45%),
    repeating-linear-gradient(0deg, var(--grid-line) 0 1px, transparent 1px 42px),
    repeating-linear-gradient(90deg, var(--grid-line) 0 1px, transparent 1px 42px),
    var(--bg);
  touch-action: none;
  transition: background 0.4s var(--ease);
}

.instructions {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 0 40px;
  pointer-events: none;
  opacity: 1;
  filter: blur(0px);
  transform: scale(1) translateY(0);
  transition:
    opacity 0.4s var(--ease),
    transform 0.4s var(--ease),
    filter 0.4s var(--ease);
}

.instructions.hidden {
  opacity: 0;
  filter: blur(6px);
  transform: scale(0.94) translateY(-10px);
  transition-duration: 0.3s;
}

.instructions-emoji {
  font-size: 56px;
  margin-bottom: 20px;
  filter: drop-shadow(0 0 20px var(--title-glow));
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

.instructions-title {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin: 0 0 8px;
  color: var(--text);
}

.instructions-sub {
  font-size: 16px;
  font-weight: 400;
  line-height: 1.5;
  margin: 0;
  color: var(--text-secondary);
  max-width: 300px;
}

.player-count {
  position: absolute;
  top: 20px;
  left: 50%;
  transform: translate(-50%, -8px);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--glass-bg);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  padding: 8px 16px;
  border-radius: 980px;
  box-shadow: 0 4px 24px var(--shadow-soft);
  opacity: 0;
  transition:
    opacity 0.3s var(--ease),
    transform 0.3s var(--ease),
    background 0.25s var(--ease),
    color 0.25s var(--ease);
  pointer-events: none;
  z-index: 5;
}

.player-count.visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.player-count.ready {
  background: var(--accent-soft);
  color: var(--accent);
  box-shadow: 0 4px 24px var(--shadow-soft), 0 0 20px var(--accent-soft);
}

/* Soft spotlight that dims everything except the winning finger */
.spotlight {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 8;
  opacity: 0;
  transition: opacity 0.5s var(--ease);
  background: radial-gradient(
    circle at var(--wx, 50%) var(--wy, 50%),
    transparent var(--wr1, 100px),
    var(--spotlight-dim) var(--wr2, 300px)
  );
}

.spotlight.visible {
  opacity: 1;
}

.result-banner {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.85);
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.01em;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  z-index: 15;
  color: #fff;
  text-shadow: 0 2px 16px rgba(0, 0, 0, 0.35), 0 0 30px var(--title-glow);
  transition: opacity 0.35s var(--ease), transform 0.4s var(--ease);
}

.result-banner.visible {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

/* ---------- Finger dots ---------- */
.finger-dot {
  position: absolute;
  width: 108px;
  height: 108px;
  margin-left: -54px;
  margin-top: -54px;
  pointer-events: none;
  z-index: 10;
  opacity: 1;
  transform: scale(1);
  will-change: transform, opacity;
  transition: transform 0.4s var(--ease), opacity 0.25s var(--ease);
}

/* Initial state right after a finger touches down; removed on the next
   animation frame so the browser animates into the normal state above. */
.finger-dot.dot-enter {
  opacity: 0;
  transform: scale(0.4);
}

.dot-core {
  position: absolute;
  inset: 16px;
  border-radius: 50%;
  background: var(--c);
  box-shadow: 0 0 26px 4px var(--c-glow), 0 8px 20px rgba(0, 0, 0, 0.18), 0 0 0 1px rgba(255, 255, 255, 0.15) inset;
  transform: scale(1);
  transition: transform 0.25s var(--ease), box-shadow 0.25s var(--ease);
}

/* Subtle "tension build" squeeze synced to the countdown ring, plus a
   breathing glow pulse that shows the selection is actively running. */
.dot-core.counting {
  transition: transform linear, box-shadow 0.25s var(--ease);
  transform: scale(0.88);
  animation: chargePulse 0.8s ease-in-out infinite;
}

.dot-core.counting.urgent {
  animation: chargePulse 0.36s ease-in-out infinite;
}

@keyframes chargePulse {
  0%, 100% { box-shadow: 0 0 26px 4px var(--c-glow), 0 8px 20px rgba(0, 0, 0, 0.18), 0 0 0 1px rgba(255, 255, 255, 0.15) inset; }
  50% { box-shadow: 0 0 48px 14px var(--c-glow), 0 8px 20px rgba(0, 0, 0, 0.18), 0 0 0 1px rgba(255, 255, 255, 0.3) inset; }
}

.dot-label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 700;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
}

.ring {
  position: absolute;
  inset: 0;
  transform: rotate(-90deg);
}

.ring circle {
  fill: none;
  stroke-width: 4;
}

.ring .ring-bg {
  stroke: var(--ring-track);
}

.ring .ring-progress {
  stroke: var(--c);
  stroke-linecap: round;
  stroke-dasharray: 251.2; /* 2 * PI * 40 */
  stroke-dashoffset: 0;
  filter: drop-shadow(0 0 5px var(--c));
}

.ring .ring-progress.counting {
  transition: stroke-dashoffset linear;
  stroke-dashoffset: 251.2;
}

/* Winner */
.finger-dot.selected .dot-core {
  animation: winnerPulse 1s ease-in-out infinite;
  box-shadow: 0 0 50px 14px var(--c-glow), 0 12px 30px rgba(0, 0, 0, 0.2), 0 0 0 1px rgba(255, 255, 255, 0.25) inset;
}

.finger-dot.selected {
  z-index: 12;
}

.finger-dot.selected::after {
  content: "";
  position: absolute;
  inset: 16px;
  border-radius: 50%;
  border: 2px solid var(--c);
  animation: ripple 1.6s var(--ease) infinite;
}

@keyframes winnerPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.12); }
}

@keyframes ripple {
  0% { transform: scale(1); opacity: 0.5; }
  100% { transform: scale(1.9); opacity: 0; }
}

/* Losers */
.finger-dot.not-selected {
  opacity: 0;
  transform: scale(0.5);
  transition: opacity 0.45s var(--ease), transform 0.45s var(--ease);
}

/* Groups mode: every dot stays, just gets re-colored + labeled per team */
.finger-dot.grouped .dot-core {
  animation: groupPop 0.5s var(--ease);
}

@keyframes groupPop {
  0% { transform: scale(0.7); }
  60% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

/* Full-screen flash used as a haptic substitute where Vibration API is
   unavailable or blocked (most notably iOS Safari). */
.flash-overlay {
  position: fixed;
  inset: 0;
  background: radial-gradient(circle at 50% 50%, rgba(0, 113, 227, 0.4), rgba(175, 82, 222, 0.2));
  pointer-events: none;
  z-index: 999;
  opacity: 0;
}

.flash-overlay.flash {
  animation: flashFade 0.3s ease-out;
}

@keyframes flashFade {
  0% { opacity: 1; }
  100% { opacity: 0; }
}

/* Particle burst */
.particle {
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 14;
  transition: transform 0.8s var(--ease), opacity 0.8s ease;
}

/* ---------- Settings modal ---------- */
.overlay[hidden] {
  display: none;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  touch-action: none;
  opacity: 0;
  transition: opacity 0.3s var(--ease);
}

.overlay.open {
  opacity: 1;
}

.modal {
  width: min(380px, 88vw);
  max-height: 84vh;
  overflow-y: auto;
  background: var(--glass-bg);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border: 1px solid var(--glass-border);
  border-radius: 24px;
  padding: 30px 28px 24px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.25);
  opacity: 0;
  transform: scale(0.94) translateY(10px);
  transition: transform 0.35s var(--ease), opacity 0.3s var(--ease);
}

.overlay.open .modal {
  opacity: 1;
  transform: scale(1) translateY(0);
}

.modal h2 {
  margin: 0 0 24px;
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}

.field[hidden] {
  display: none;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 22px;
  font-size: 15px;
  color: var(--text);
}

.field input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.select-field {
  width: 100%;
  background: var(--fill);
  border: none;
  color: var(--text);
  font-size: 15px;
  padding: 12px 14px;
  border-radius: 12px;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='9'%3E%3Cpath d='M1 1l6 6 6-6' stroke='%236e6e73' stroke-width='2' fill='none' fill-rule='evenodd' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
}

.hint {
  margin: -12px 0 22px;
  font-size: 13px;
  line-height: 1.5;
  color: var(--text-secondary);
}

.checkbox-field {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

.checkbox-field input[type="checkbox"] {
  width: 22px;
  height: 22px;
  accent-color: var(--accent);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 8px;
}

.primary-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 12px 28px;
  border-radius: 980px;
  font-size: 16px;
  font-weight: 600;
  transition: transform 0.25s var(--ease), opacity 0.25s var(--ease);
}

.primary-btn:active {
  transform: scale(0.97);
  opacity: 0.85;
}
