:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --fg: #14161a;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #4f7cff;
  --accent-weak: #e8eeff;
  --target: #8b8f98;
  --success: #1f9d6b;
  --error: #e05260;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 8px 28px rgba(16, 24, 40, 0.06);
  --radius: 16px;
  color-scheme: light;
}

[data-theme='dark'] {
  --bg: #0e1014;
  --surface: #171a20;
  --fg: #f2f4f8;
  --muted: #9aa1ad;
  --border: #262a31;
  --accent: #6b95ff;
  --accent-weak: #1d2740;
  --target: #6c727d;
  --success: #34c98a;
  --error: #ff6b78;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 12px 32px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* The hidden attribute must always win over component display rules below
   (e.g. .modal-backdrop{display:grid}, .controls{display:flex}). */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  line-height: 1.5;
  transition: background 0.25s ease, color 0.25s ease;
}

/* ---- Topbar ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px clamp(16px, 4vw, 32px);
  max-width: 720px;
  margin: 0 auto;
}
.brand {
  font-weight: 650;
  font-size: 1.05rem;
  letter-spacing: 0.2px;
  background: none;
  border: none;
  color: var(--fg);
  cursor: pointer;
  padding: 0;
}
.topbar-actions { display: flex; align-items: center; gap: 8px; }

/* ---- Buttons ---- */
button { font-family: inherit; }
/* Springy easing shared by every interactive element. */
.primary-btn, .secondary-btn, .ghost-btn, .icon-btn, .seg-btn, .brand {
  transition: transform 0.24s cubic-bezier(0.34, 1.56, 0.64, 1),
              background 0.18s ease, border-color 0.18s ease, color 0.18s ease,
              filter 0.18s ease, box-shadow 0.18s ease, opacity 0.18s ease;
}
.primary-btn, .secondary-btn, .ghost-btn {
  border-radius: 10px;
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  border: 1px solid transparent;
}
.primary-btn {
  background: var(--accent);
  color: #fff;
  padding: 12px 20px;
}
.primary-btn:hover { filter: brightness(1.05); transform: translateY(-2px) scale(1.04); box-shadow: var(--shadow); }
.primary-btn:active, .secondary-btn:active, .ghost-btn:active, .icon-btn:active { transform: scale(0.92); }
.primary-btn:disabled { opacity: 0.55; cursor: default; }
.primary-btn:disabled:hover { transform: none; box-shadow: none; }
.secondary-btn {
  background: var(--surface);
  color: var(--fg);
  border-color: var(--border);
  padding: 12px 20px;
}
.secondary-btn:hover { border-color: var(--accent); transform: translateY(-2px) scale(1.04); box-shadow: var(--shadow); }
.secondary-btn.full { width: 100%; margin-top: 16px; }
.ghost-btn {
  background: transparent;
  color: var(--muted);
  padding: 8px 12px;
}
.ghost-btn:hover { color: var(--fg); transform: translateY(-1px) scale(1.05); }
.brand:hover { transform: scale(1.06); }
.icon-btn {
  width: 38px; height: 38px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  display: grid; place-items: center;
}
.icon-btn:hover { border-color: var(--accent); transform: translateY(-2px) scale(1.08) rotate(-8deg); }
.theme-icon { width: 18px; height: 18px; display: inline-block; }
.theme-icon::before { content: '☀'; font-size: 16px; }
[data-theme='dark'] .theme-icon::before { content: '☾'; }
.mute-icon { width: 18px; height: 18px; display: inline-block; }
.mute-icon::before { content: '🔊'; font-size: 15px; }
#muteToggle.is-muted .mute-icon::before { content: '🔇'; }

/* ---- Layout / views ---- */
#app {
  max-width: 720px;
  margin: 0 auto;
  padding: clamp(8px, 3vw, 24px) clamp(16px, 4vw, 32px) 64px;
}
.view { animation: fade 0.25s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(20px, 5vw, 32px);
}

/* ---- Home ---- */
.hero { text-align: center; padding-top: clamp(16px, 6vw, 48px); }
.title {
  font-size: clamp(2.6rem, 9vw, 4rem);
  margin: 0 0 8px;
  letter-spacing: -1.5px;
  font-weight: 700;
}
.tagline { color: var(--muted); margin: 0 auto 28px; max-width: 34ch; }
.card .primary-btn { width: 100%; }

/* Two equal mode cards, side by side — neither is the "default". */
.mode-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: clamp(12px, 3vw, 18px);
  text-align: left;
}
.mode-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(18px, 4vw, 26px);
  cursor: pointer;
  font-family: inherit;
  color: var(--fg);
  transition: transform 0.24s cubic-bezier(0.34, 1.56, 0.64, 1),
              border-color 0.18s ease, box-shadow 0.18s ease;
}
.mode-card:hover {
  transform: translateY(-4px) scale(1.02);
  border-color: var(--accent);
  box-shadow: 0 8px 32px rgba(16, 24, 40, 0.12);
}
.mode-card:active { transform: scale(0.98); }
.mode-icon { font-size: 1.8rem; line-height: 1; }
.mode-name { font-size: 1.15rem; font-weight: 700; letter-spacing: -0.2px; }
.mode-desc { color: var(--muted); font-size: 0.88rem; line-height: 1.45; flex: 1; }
.mode-cta { color: var(--accent); font-weight: 650; font-size: 0.9rem; margin-top: 4px; }

.home-secondary { margin-top: 20px; text-align: center; }
.home-secondary .ghost-btn { font-size: 0.92rem; }

/* Ad slot stays hidden (no empty box, no layout shift) until ads.js confirms a
   configured publisher id and adds .ads-on to <body>. */
.ad-slot { display: none; }
body.ads-on .ad-slot { display: block; margin-top: 28px; min-height: 90px; }

@media (max-width: 480px) {
  .mode-grid { grid-template-columns: 1fr; }
  .mode-desc { flex: none; }
}
.seg {
  display: inline-flex; background: var(--bg); border: 1px solid var(--border);
  border-radius: 10px; padding: 3px;
}
.seg-btn {
  border: none; background: transparent; color: var(--muted);
  padding: 7px 14px; border-radius: 8px; cursor: pointer; font-weight: 600; font-size: 0.85rem;
}
.seg-btn.is-active { background: var(--surface); color: var(--fg); box-shadow: var(--shadow); }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.accent { color: var(--accent); }

/* ---- Game ---- */
.game-head { display: flex; justify-content: space-between; align-items: center; margin: 8px 0 4px; }
.pill {
  font-size: 0.82rem; font-weight: 600; padding: 5px 12px; border-radius: 999px;
  background: var(--accent-weak); color: var(--accent);
}
.pill-muted { background: var(--bg); color: var(--muted); border: 1px solid var(--border); }
.phase-banner {
  text-align: center; font-weight: 650; letter-spacing: 0.5px; text-transform: uppercase;
  font-size: 0.8rem; color: var(--muted); margin: 10px 0;
}

.stage {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 8px;
  touch-action: none;
  cursor: ew-resize;
}
.stage.dragging { cursor: grabbing; }
#angleSvg { width: 100%; height: auto; display: block; user-select: none; }
.countdown {
  position: absolute; top: 14px; right: 18px;
  font-size: 1.6rem; font-weight: 700; color: var(--accent);
  font-variant-numeric: tabular-nums;
}

/* Pre-round attention grabber. Pure transitions (no keyframes) so the pop-in
   and the flow-up-to-dock are each one continuous, uninterrupted motion. */
.round-intro {
  position: absolute; inset: 0; z-index: 5;
  background: var(--surface); border-radius: var(--radius);
  transition: background-color 0.6s ease;
}
.round-intro .ri-label,
.round-intro .ri-hint {
  position: absolute; left: 0; right: 0; text-align: center;
  color: var(--muted); opacity: 1; transition: opacity 0.4s ease;
}
.round-intro .ri-label {
  top: 26%; font-weight: 700; text-transform: uppercase; letter-spacing: 1.5px; font-size: 0.8rem;
}
.round-intro .ri-hint { top: 70%; font-size: 0.9rem; }
.round-intro .ri-angle {
  position: absolute; left: 50%; top: 50%;
  transform: translate(-50%, -50%) scale(1); opacity: 1;
  font-size: clamp(3rem, 18vw, 6.5rem); font-weight: 800; line-height: 1; color: var(--accent);
  font-variant-numeric: tabular-nums;
  transition: top 0.7s cubic-bezier(0.34, 1.56, 0.64, 1),
              transform 0.7s cubic-bezier(0.34, 1.56, 0.64, 1),
              opacity 0.4s ease;
}

/* Pop-in start state: number small + transparent, surrounding text faded. */
.round-intro.intro-enter .ri-angle { transform: translate(-50%, -50%) scale(0.3); opacity: 0; }
.round-intro.intro-enter .ri-label,
.round-intro.intro-enter .ri-hint { opacity: 0; }

/* Docked: number flows up and shrinks in one continuous motion; text fades. */
.round-intro.docked { background-color: transparent; pointer-events: none; }
.round-intro.docked .ri-label,
.round-intro.docked .ri-hint { opacity: 0; }
.round-intro.docked .ri-angle { top: 3%; transform: translate(-50%, 0) scale(0.4); }

/* Springy figure entrance — delayed so it waits for the number to dock first. */
#angleSvg.enter { animation: figPop 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) 0.55s both; }
@keyframes figPop {
  0% { transform: scale(0.4); opacity: 0; }
  60% { transform: scale(1.08); opacity: 1; }
  100% { transform: scale(1); }
}

/* Result score: pops in big at the top of the canvas (display toggle restarts it). */
.round-score {
  position: absolute; left: 50%; top: 4%; transform: translateX(-50%);
  z-index: 6; pointer-events: none;
  display: flex; flex-direction: column; align-items: center; text-align: center;
  animation: scorePop 0.75s cubic-bezier(0.2, 0.9, 0.3, 1.6) both;
}
.round-score .rs-label {
  font-size: 0.7rem; font-weight: 700; text-transform: uppercase; letter-spacing: 1.5px; color: var(--muted);
}
.round-score .rs-value {
  font-size: clamp(2.6rem, 14vw, 4.6rem); font-weight: 800; line-height: 1; color: var(--accent);
  font-variant-numeric: tabular-nums;
}
@keyframes scorePop {
  0% { transform: translateX(-50%) scale(0); opacity: 0; }
  50% { transform: translateX(-50%) scale(1.4); opacity: 1; }
  70% { transform: translateX(-50%) scale(0.88); }
  85% { transform: translateX(-50%) scale(1.08); }
  100% { transform: translateX(-50%) scale(1); }
}

/* ray/arc styling driven by classes set on SVG elements */
.baseline { stroke: var(--muted); stroke-width: 3; stroke-linecap: round; }
.ray-target { stroke: var(--target); stroke-width: 4; stroke-linecap: round; }
.ray-guess { stroke: var(--accent); stroke-width: 4; stroke-linecap: round; }
.arc-target { stroke: var(--target); fill: none; stroke-width: 2.5; opacity: 0.85; stroke-linecap: round; }
.arc-guess { stroke: var(--accent); fill: none; stroke-width: 3.5; opacity: 1; stroke-linecap: round; }
.sector-green { fill: var(--success); opacity: 0.22; }
.sector-red { fill: var(--error); opacity: 0.32; }
.sector-red.fade-in { animation: redFade 0.45s ease both; }
@keyframes redFade { from { opacity: 0; } to { opacity: 0.32; } }
.vertex-dot { fill: var(--fg); }
.handle { fill: var(--accent); cursor: grab; }
.stage.dragging .handle { cursor: grabbing; }

.controls { margin-top: 18px; display: flex; flex-direction: column; gap: 14px; align-items: center; }
.guess-readout { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.guess-value { font-size: 2rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.slider {
  width: min(420px, 100%);
  -webkit-appearance: none; appearance: none;
  height: 6px; border-radius: 999px; background: var(--border); outline: none;
}
.slider::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 22px; height: 22px; border-radius: 50%; background: var(--accent);
  cursor: pointer; box-shadow: var(--shadow);
}
.slider::-moz-range-thumb {
  width: 22px; height: 22px; border: none; border-radius: 50%; background: var(--accent);
  cursor: pointer;
}
.controls .primary-btn { width: min(420px, 100%); }
.hint { text-align: center; }

/* ---- Round result ---- */
.result { margin-top: 18px; display: flex; flex-direction: column; gap: 16px; align-items: center; }
.result-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; width: 100%;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px;
}
.result-grid > div { display: flex; flex-direction: column; align-items: center; gap: 2px; text-align: center; }
.result-num { font-size: 1.3rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.result .primary-btn { width: min(420px, 100%); }

/* ---- Final ---- */
.final-card { text-align: center; }
.final-score { font-size: clamp(2.4rem, 10vw, 3.4rem); margin: 6px 0 2px; font-weight: 700; font-variant-numeric: tabular-nums; }
.final-score .muted { font-size: 0.45em; font-weight: 600; }
.rank-line { font-weight: 600; color: var(--accent); }

/* Verdict badge — pops in above the score. */
.verdict-badge {
  display: inline-block; margin: 6px auto 2px;
  padding: 5px 16px; border-radius: 999px;
  font-weight: 800; font-size: 0.95rem; letter-spacing: 0.3px;
  color: #fff; animation: badgePop 0.6s cubic-bezier(0.2, 0.9, 0.3, 1.6) both;
}
.verdict-badge.v-great { background: var(--success); }
.verdict-badge.v-good  { background: #2bb673; }
.verdict-badge.v-ok    { background: var(--accent); }
.verdict-badge.v-meh   { background: #e08a3c; }
.verdict-badge.v-bad   { background: var(--error); }
@keyframes badgePop {
  0% { transform: scale(0); opacity: 0; }
  60% { transform: scale(1.2); opacity: 1; }
  100% { transform: scale(1); }
}

/* Per-round recap: a row of mini target-vs-guess figures tinted by accuracy. */
.recap-row {
  display: flex; flex-wrap: wrap; justify-content: center; gap: 10px;
  margin: 22px 0;
}
.recap-item {
  flex: 1 1 92px; max-width: 120px; min-width: 84px;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 10px 6px 8px; border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: 3px solid var(--acc);
  animation: recapIn 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}
@keyframes recapIn { from { transform: scale(0.7) translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }
.recap-fig { width: 100%; height: auto; display: block; }
.recap-baseline { stroke: var(--muted); stroke-width: 2.5; stroke-linecap: round; opacity: 0.6; }
.recap-target { stroke: var(--target); stroke-width: 3; stroke-linecap: round; stroke-dasharray: 4 4; }
.recap-guess { stroke-width: 3.5; stroke-linecap: round; }
.recap-agreed { fill: var(--success); opacity: 0.14; }
.recap-meta { display: flex; flex-direction: column; align-items: center; line-height: 1.15; }
.recap-round { color: var(--muted); font-size: 0.7rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.5px; }
.recap-score { font-weight: 800; font-size: 1.05rem; color: var(--acc); font-variant-numeric: tabular-nums; }
.recap-delta { color: var(--muted); font-size: 0.72rem; font-variant-numeric: tabular-nums; }

.final-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

/* ---- Play with friends ---- */
.friends-card { text-align: center; }
.friends-card h2 { margin: 0 0 8px; }
.friends-card #friendsIntro { margin: 0 auto 20px; max-width: 42ch; }
.friends-card .primary-btn.full,
.friends-card .secondary-btn.full,
.friends-card .ghost-btn.full { margin-top: 12px; }
.ghost-btn.full { display: block; width: 100%; }
.share-label { text-align: left; margin: 0 0 6px; }
.share-row { display: flex; gap: 8px; align-items: stretch; }
.share-row .text-input { margin-top: 0; flex: 1; min-width: 0; font-size: 0.9rem; }
.share-row .secondary-btn { white-space: nowrap; }

/* ---- Leaderboard ---- */
/* Constrain the leaderboard card to the viewport so the list scrolls inside it
   and the action buttons below stay reachable without scrolling the page. */
.view[data-view="leaderboard"] .card {
  display: flex;
  flex-direction: column;
  max-height: calc(100dvh - 120px);
}
.lb-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 8px; }
.lb-head h2 { margin: 0; }
.leaderboard {
  list-style: none; padding: 0; margin: 0 0 12px;
  flex: 1 1 auto; min-height: 0; overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
/* Soft fade at the bottom edge, only while more rows remain below the fold. */
.leaderboard.faded {
  -webkit-mask-image: linear-gradient(to bottom, #000 calc(100% - 40px), transparent);
          mask-image: linear-gradient(to bottom, #000 calc(100% - 40px), transparent);
}
.leaderboard li {
  display: grid; grid-template-columns: 32px 1fr auto auto; gap: 12px; align-items: center;
  padding: 12px 6px; border-bottom: 1px solid var(--border); font-variant-numeric: tabular-nums;
}
.leaderboard li:last-child { border-bottom: none; }
.leaderboard .lb-rank { color: var(--muted); font-weight: 700; }
.leaderboard .lb-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.leaderboard .lb-score { font-weight: 700; }
.leaderboard .lb-time { color: var(--muted); font-size: 0.8rem; }
.leaderboard li.is-me { background: var(--accent-weak); border-radius: 8px; }

/* ---- Modal ---- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.45);
  display: grid; place-items: center; padding: 20px; z-index: 50;
  animation: fade 0.18s ease;
}
.modal {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 24px; width: min(380px, 100%);
}
.modal h3 { margin: 0 0 4px; }
.text-input {
  width: 100%; margin-top: 14px; padding: 12px 14px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--fg); font-size: 1rem;
}
.text-input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.error-text { color: var(--error); font-size: 0.85rem; margin: 8px 0 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }

/* ---- Toast ---- */
.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%);
  background: var(--fg); color: var(--bg); padding: 10px 18px; border-radius: 999px;
  font-size: 0.9rem; font-weight: 600; box-shadow: var(--shadow); z-index: 60;
  animation: fade 0.2s ease;
}

@media (max-width: 460px) {
  .result-grid { grid-template-columns: repeat(2, 1fr); }
  .leaderboard li { grid-template-columns: 28px 1fr auto; }
  .leaderboard .lb-time { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
