/* Clock Quest — layout + component styles.
   Width, dvh, and overflow rules are deliberate (see the production plan):
   one shell owns width, the play column caps at 720px, no 100vw anywhere. */

:root {
  --site-header-height: 72px;
  --page-gutter: clamp(16px, 4vw, 32px);
  --cq-max-menu: 1100px;
  --cq-max-round: 720px;
  --cq-cream: #fff8ef;
  --cq-cream-2: #fdefdc;
  --cq-ink: #134e4a;
  --cq-ink-soft: #5b7570;
  --cq-teal: #0d9488;
  --cq-amber: #f97316;
  --cq-indigo: #6366f1;
  --cq-radius: 24px;
  --cq-hour: #ef4444;
  --cq-minute: #2563eb;
  --cq-face: #ffffff;
  --cq-face-ring: #134e4a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

.cq-body {
  min-height: 100vh;   /* fallback first */
  min-height: 100dvh;  /* dynamic viewport: URL bar collapse never clips */
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(1200px 600px at 50% -10%, #ffe8c7 0%, transparent 60%),
    linear-gradient(180deg, var(--cq-cream) 0%, var(--cq-cream-2) 100%);
  color: var(--cq-ink);
  font-family: 'Fredoka', system-ui, -apple-system, 'Segoe UI', sans-serif;
  overflow-x: clip; /* belt-and-braces: no sideways scroll on any screen */
  -webkit-tap-highlight-color: transparent;
}

.cq-skip {
  position: fixed;
  top: -80px;
  left: 12px;
  z-index: 100;
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--cq-ink);
  color: #fff;
  text-decoration: none;
}
.cq-skip:focus { top: 12px; }

/* --- shared in-game site header (matches the other games) --------------- */
.site-game-header {
  position: sticky;
  top: 0;
  z-index: 90;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  min-height: var(--site-header-height);
  padding: max(8px, env(safe-area-inset-top)) max(var(--page-gutter), env(safe-area-inset-right)) 8px max(var(--page-gutter), env(safe-area-inset-left));
  border-bottom: 1px solid rgba(15, 118, 110, 0.14);
  background: rgba(255, 248, 237, 0.94);
  color: var(--cq-ink);
  box-shadow: 0 12px 30px rgba(15, 118, 110, 0.12);
  backdrop-filter: blur(16px);
}
.site-game-brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  color: var(--cq-ink);
  text-decoration: none;
}
.site-game-mark {
  display: grid;
  width: 54px;
  height: 54px;
  place-items: center;
  flex: 0 0 auto;
  border: 3px solid var(--cq-ink);
  border-radius: 16px;
  background: linear-gradient(135deg, #fef08a, #5eead4 48%, #fb7185);
  box-shadow: 0 4px 0 rgba(19, 78, 74, 0.24);
  color: var(--cq-ink);
  font-size: 22px;
  font-weight: 900;
}
.site-game-brand strong {
  display: block;
  font-size: clamp(18px, 3.2vw, 26px);
  line-height: 1;
}
.site-game-brand small {
  display: block;
  margin-top: 4px;
  color: var(--cq-ink-soft);
  font-size: 14px;
  font-weight: 700;
}
.site-game-actions { display: flex; align-items: center; gap: 10px; flex: 0 0 auto; }
.site-home-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 50px;
  padding: 0 18px;
  border: 2px solid rgba(15, 118, 110, 0.18);
  border-radius: 18px;
  background: #fffdf8;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.08), 0 8px 18px rgba(15, 118, 110, 0.12);
  color: var(--cq-teal);
  font-size: 17px;
  font-weight: 800;
  text-decoration: none;
  white-space: nowrap;
}
.site-game-brand:focus-visible,
.site-home-link:focus-visible {
  outline: 4px solid rgba(20, 184, 166, 0.45);
  outline-offset: 3px;
}

/* --- app host: fills remaining height, never forces 100vh -------------- */
#app {
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  position: relative;
  padding-bottom: env(safe-area-inset-bottom);
}

#party-canvas {
  position: fixed;
  inset: 0;
  z-index: 60;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/* --- the single width shell every screen sits inside ------------------- */
.cq-screen {
  width: min(100% - 32px, var(--cq-max-menu));
  margin-inline: auto;
  padding: clamp(16px, 3vw, 28px) 0 40px;
  display: flex;
  flex-direction: column;
  gap: clamp(16px, 3vw, 26px);
}
.cq-round { width: min(100% - 32px, var(--cq-max-round)); }

/* --- in-game top bar (Home / Back / progress / hint / sound) ----------- */
.cq-topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.cq-icon-btn {
  display: inline-grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border: 2px solid rgba(15, 118, 110, 0.18);
  border-radius: 16px;
  background: #fffdf8;
  font-size: 22px;
  cursor: pointer;
  touch-action: manipulation;
}
.cq-icon-btn:active { transform: translateY(1px); }
.cq-screen-title, .cq-round-progress {
  font-size: clamp(18px, 3vw, 24px);
  font-weight: 700;
  margin: 0;
}
.cq-round-progress { margin-inline: auto 0; color: var(--cq-ink-soft); }

/* --- Town map ---------------------------------------------------------- */
.cq-town { align-items: stretch; }
.cq-town-hero {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  flex-wrap: wrap;
  padding: clamp(18px, 3vw, 30px);
  border-radius: var(--cq-radius);
  background: linear-gradient(135deg, #99f6e4, #a5b4fc);
  box-shadow: 0 18px 40px rgba(15, 118, 110, 0.18);
}
.cq-town-title h1 { margin: 0; font-size: clamp(30px, 7vw, 52px); }
.cq-town-title p { margin: 6px 0 0; font-size: clamp(15px, 3vw, 20px); font-weight: 500; }
.cq-town-clock { width: clamp(110px, 26vw, 160px); flex: 0 0 auto; }

.cq-mode-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr));
  gap: clamp(14px, 3vw, 22px);
}
.cq-mode-card {
  --accent: var(--cq-teal);
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: clamp(18px, 3vw, 26px);
  border: none;
  border-radius: var(--cq-radius);
  background: #fffdf8;
  border-bottom: 6px solid var(--accent);
  box-shadow: 0 12px 26px rgba(15, 118, 110, 0.12);
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.12s ease;
}
.cq-mode-card:active { transform: translateY(2px); }
.cq-mode-icon { font-size: 42px; }
.cq-mode-name { font-size: clamp(20px, 3.4vw, 26px); font-weight: 700; }
.cq-mode-tag { color: var(--cq-ink-soft); font-weight: 500; }
.cq-town-footer { display: flex; justify-content: center; }
.cq-star-count {
  font-size: 22px;
  font-weight: 700;
  padding: 8px 20px;
  border-radius: 999px;
  background: #fffdf8;
  box-shadow: 0 8px 18px rgba(15, 118, 110, 0.12);
}

/* --- Tier + level select ---------------------------------------------- */
.cq-tier-block { display: flex; flex-direction: column; gap: 12px; }
.cq-tier-title { margin: 0; font-size: clamp(20px, 3.4vw, 26px); color: var(--accent); }
.cq-tier-title small { color: var(--cq-ink-soft); font-weight: 500; font-size: 0.7em; }
.cq-level-row {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 14px;
}
.cq-level-bubble {
  --accent: var(--cq-teal);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 16px 8px;
  border: 3px solid var(--accent);
  border-radius: 20px;
  background: #fffdf8;
  cursor: pointer;
  touch-action: manipulation;
}
.cq-level-bubble:disabled { opacity: 0.5; border-color: #cbd5e1; cursor: not-allowed; }
.cq-level-num { font-size: 26px; font-weight: 700; }
.cq-level-stars { color: #f59e0b; letter-spacing: 2px; }

/* --- Round screen ------------------------------------------------------ */
.cq-prompt {
  margin: 0;
  text-align: center;
  font-size: clamp(20px, 4.6vw, 30px);
  font-weight: 700;
  min-height: 1.4em;
}
.cq-stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(14px, 3vw, 22px);
}

/* The clock stage sizes to fit portrait phone, landscape, and desktop
   without media-query forks, and never below 260px on phones. */
.cq-clock {
  width: min(78vw, 46vh, 420px);
  min-width: 260px;
  max-width: 100%;
  height: auto;
  touch-action: none; /* the rotation gesture is ours, not the scroller's */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.cq-clock-wrap { display: flex; justify-content: center; width: 100%; }

/* --- clock face internals --------------------------------------------- */
.cq-face { fill: var(--cq-face); stroke: var(--cq-face-ring); stroke-width: 8; }
.cq-face-inner { fill: none; stroke: rgba(19, 78, 74, 0.12); stroke-width: 2; }
.cq-tick-hour { stroke: var(--cq-ink); stroke-width: 5; stroke-linecap: round; }
.cq-tick-min { stroke: rgba(19, 78, 74, 0.35); stroke-width: 2; stroke-linecap: round; }
.cq-number { font-size: 34px; font-weight: 700; fill: var(--cq-ink); }
.cq-helper-num { font-size: 18px; font-weight: 600; fill: var(--cq-minute); }
.cq-hand { stroke-linecap: round; transition: transform 0.05s linear; }
.cq-hour { stroke: var(--cq-hour); stroke-width: 14; }
.cq-minute { stroke: var(--cq-minute); stroke-width: 9; }
.cq-hit { stroke: transparent; stroke-width: 40; stroke-linecap: round; }
.cq-cap { fill: var(--cq-ink); }
.cq-hand-group { cursor: grab; }
.cq-clock.cq-dragging .cq-hand-group { cursor: grabbing; }
.cq-hand-group:focus-visible { outline: none; }
.cq-hand-group:focus-visible .cq-hand { stroke: var(--cq-amber); }
.cq-ghost-hand { stroke: rgba(19, 78, 74, 0.25); stroke-width: 8; stroke-linecap: round; stroke-dasharray: 6 8; }
.cq-label { font-size: 18px; font-weight: 700; fill: var(--cq-ink-soft); }
.cq-label-hour { fill: var(--cq-hour); }
.cq-label-min { fill: var(--cq-minute); }

/* --- digital echo ------------------------------------------------------ */
.cq-digital {
  font-variant-numeric: tabular-nums;
  font-size: clamp(28px, 6vw, 40px);
  font-weight: 700;
  letter-spacing: 2px;
  color: var(--cq-teal);
  background: #06201d;
  color: #5eead4;
  padding: 8px 20px;
  border-radius: 14px;
}

/* --- builder / stop controls ------------------------------------------ */
.cq-builder, .cq-stop {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(12px, 2.6vw, 20px);
  width: 100%;
}
.cq-fallback {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}
.cq-btn {
  min-height: 52px;
  padding: 0 22px;
  border: none;
  border-radius: 16px;
  font-family: inherit;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}
.cq-btn-primary {
  background: var(--cq-teal);
  color: #fff;
  box-shadow: 0 8px 0 #0b756b;
}
.cq-btn-primary:active { transform: translateY(4px); box-shadow: 0 4px 0 #0b756b; }
.cq-btn-ghost {
  background: #fffdf8;
  color: var(--cq-ink);
  border: 2px solid rgba(15, 118, 110, 0.2);
}
.cq-check { min-width: 160px; }
.cq-stop-btn {
  min-width: 180px;
  min-height: 64px;
  font-size: 26px;
  background: var(--cq-amber);
  box-shadow: 0 8px 0 #c2410c;
}
.cq-stop-btn:active { transform: translateY(4px); box-shadow: 0 4px 0 #c2410c; }

/* --- Time Match -------------------------------------------------------- */
.cq-match-target {
  align-self: center;
  font-variant-numeric: tabular-nums;
  font-size: clamp(30px, 7vw, 46px);
  font-weight: 700;
  color: #fff;
  background: var(--cq-indigo);
  padding: 10px 28px;
  border-radius: 18px;
  box-shadow: 0 10px 0 #4f46e5;
}
.cq-match-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 150px), 1fr));
  gap: clamp(12px, 3vw, 20px);
  width: 100%;
}
.cq-match-option {
  border: 3px solid rgba(15, 118, 110, 0.16);
  border-radius: 20px;
  background: #fffdf8;
  padding: 10px;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.12s ease, border-color 0.12s ease;
}
.cq-match-option .cq-clock { width: 100%; min-width: 0; }
.cq-match-option:active { transform: scale(0.97); }
.cq-match-option.cq-correct { border-color: #22c55e; box-shadow: 0 0 0 4px rgba(34, 197, 94, 0.3); }
.cq-match-option.cq-wrong { border-color: #ef4444; }
.cq-match-option:disabled { opacity: 0.45; }
.cq-dim { opacity: 0.4; }

/* --- celebration ------------------------------------------------------- */
.cq-celebrate { align-items: center; justify-content: center; text-align: center; padding-top: 8vh; }
.cq-celebrate-card {
  background: #fffdf8;
  border-radius: var(--cq-radius);
  padding: clamp(24px, 5vw, 44px);
  box-shadow: 0 20px 50px rgba(15, 118, 110, 0.2);
}
.cq-celebrate-stars { font-size: clamp(40px, 10vw, 64px); letter-spacing: 8px; }
.cq-cel-star { color: #d1d5db; }
.cq-cel-star.on { color: #f59e0b; }
.cq-celebrate-card h2 { margin: 8px 0; font-size: clamp(26px, 5vw, 38px); }
.cq-celebrate-actions { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; }

/* --- leave-confirm modal ---------------------------------------------- */
.cq-modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 120;
  display: grid;
  place-items: center;
  background: rgba(6, 32, 29, 0.5);
  padding: 20px;
}
.cq-modal {
  background: #fffdf8;
  border-radius: var(--cq-radius);
  padding: 28px;
  max-width: 360px;
  width: 100%;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.3);
}
.cq-modal-title { font-size: 22px; font-weight: 700; margin: 0 0 18px; }
.cq-modal-actions { display: flex; gap: 12px; justify-content: center; }

/* --- desktop --------------------------------------------------------- */
@media (min-width: 1024px) {
  /* Time Match spreads its option clocks across the wider shell; the
     control-stacked modes (Clock Builder, Stop the Clock) stay a centered
     single column so the clock and its controls read as one group. */
  .cq-round-match { width: min(100% - 32px, var(--cq-max-menu)); }
}
