:root {
  --sky: #5eb8f5;
  --sky-deep: #3aa0e8;
  --ink: #0f2740;
  --white: #ffffff;
  --pink: #ff4d8d;
  --green: #3ecf5a;
  --green-dark: #2aa344;
  --red: #ef4444;
  --red-dark: #dc2626;
  --capsule: #b8f0a8;
  --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);
  /* Phone defaults — tablet overrides below */
  --col-max: min(100%, 520px);
  --flag-w: min(78%, 340px);
  --answers-max: min(100%, 420px);
  --btn-min-h: 56px;
  --btn-font: clamp(0.82rem, 3.4vw, 0.98rem);
  --btn-pad-y: 12px;
  --btn-pad-x: 10px;
  --btn-radius: 16px;
  --btn-gap: 10px;
  --stage-gap: clamp(14px, 2.8vh, 24px);
  --wrap-pad-x: 14px;
  --wrap-pad-y: 12px;
  --q-size: 44px;
  --q-font: 1.25rem;
  --score-font: clamp(1.5rem, 6vw, 1.85rem);
  --tier-font: 0.58rem;
  --tier-pad-y: 4px;
  --tier-pad-x: 9px;
  --tier-gap: 5px;
  --timer-h: 22px;
  --timer-stripe: 22px;
  --flag-pad: 12px;
  --flag-radius: 18px;
  --pattern-size: 72px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--sky);
  color: var(--ink);
  font-family: 'Nunito', system-ui, -apple-system, sans-serif;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#wrap {
  position: relative;
  width: 100%;
  height: 100%;
  max-width: 100%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding:
    calc(var(--wrap-pad-y) + var(--safe-top))
    calc(var(--wrap-pad-x) + var(--safe-right))
    calc(var(--wrap-pad-y) + var(--safe-bottom))
    calc(var(--wrap-pad-x) + var(--safe-left));
  overflow: hidden;
}

.sky-pattern {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  opacity: 0.22;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='72' height='72' viewBox='0 0 72 72'%3E%3Cg fill='%23ffffff' fill-opacity='0.9'%3E%3Cpath d='M36 10l3.2 8.4H48l-6.8 5 2.6 8.4L36 26.6 28.2 31.8l2.6-8.4-6.8-5h8.8z' transform='rotate(-28 36 36)'/%3E%3C/g%3E%3C/svg%3E");
  background-size: var(--pattern-size) var(--pattern-size);
}

#top-bar,
#tier-rail,
#stage,
#timer-wrap,
#result {
  position: relative;
  z-index: 1;
}

#top-bar {
  position: relative;
  width: var(--col-max);
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
  min-height: var(--q-size);
  margin-bottom: 4px;
  flex-shrink: 0;
  z-index: 2;
}

#score-chip {
  position: absolute;
  left: 50%;
  top: calc(var(--q-size) / 2);
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.05;
  color: var(--white);
  text-shadow: 0 1px 0 rgba(15, 60, 110, 0.25);
  pointer-events: none;
}

.score-label {
  font-size: clamp(0.65rem, 1.4vw, 0.8rem);
  font-weight: 800;
  opacity: 0.85;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

#score-value {
  font-size: var(--score-font);
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

#top-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  flex-shrink: 0;
}

#q-badge {
  min-width: var(--q-size);
  height: var(--q-size);
  padding: 0 10px;
  border-radius: 999px;
  background: var(--pink);
  color: var(--white);
  font-weight: 900;
  font-size: clamp(0.85rem, calc(var(--q-font) * 0.78), 1.35rem);
  display: grid;
  place-items: center;
  box-shadow: 0 4px 0 rgba(200, 40, 100, 0.35);
  z-index: 1;
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

#tier-rail {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: var(--tier-gap);
  pointer-events: none;
  flex-shrink: 0;
  margin-top: 0;
}

.tier-pill {
  font-size: var(--tier-font);
  font-weight: 900;
  letter-spacing: 0.06em;
  color: rgba(255, 255, 255, 0.88);
  text-shadow: 0 1px 2px rgba(10, 50, 90, 0.35);
  padding: var(--tier-pad-y) var(--tier-pad-x);
  border-radius: 999px;
  transition: background 0.2s ease, color 0.2s ease, transform 0.2s ease;
  text-align: right;
}

.tier-pill.active {
  background: var(--capsule);
  color: #1a5a28;
  text-shadow: none;
  transform: scale(1.05);
  box-shadow: 0 2px 0 rgba(40, 120, 50, 0.25);
}

#stage {
  width: var(--col-max);
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--stage-gap);
  padding: 0 0 8px;
}

#flag-frame {
  position: relative;
  width: var(--flag-w);
  aspect-ratio: 3 / 2;
  background: var(--white);
  border-radius: var(--flag-radius);
  padding: var(--flag-pad);
  box-shadow:
    0 8px 0 rgba(20, 80, 140, 0.18),
    0 16px 28px rgba(20, 80, 140, 0.16);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  flex-shrink: 0;
}

#flag-img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  object-position: center center;
  border-radius: 8px;
  background: transparent;
}

#flag-fallback {
  position: absolute;
  inset: var(--flag-pad);
  display: grid;
  place-items: center;
  font-size: clamp(2.5rem, 8vw, 4rem);
  font-weight: 900;
  color: #7aa8c8;
  background: #e8f4fc;
  border-radius: 10px;
}

#flag-fallback.hidden {
  display: none;
}

#answers {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--btn-gap);
  max-width: var(--answers-max);
}

.answer-btn {
  appearance: none;
  border: 3px solid transparent;
  margin: 0;
  min-height: var(--btn-min-h);
  padding: var(--btn-pad-y) var(--btn-pad-x);
  border-radius: var(--btn-radius);
  background: var(--white);
  color: var(--ink);
  font-family: inherit;
  font-size: var(--btn-font);
  font-weight: 800;
  line-height: 1.15;
  box-shadow: 0 4px 0 rgba(20, 80, 140, 0.16);
  cursor: pointer;
    transition:
    transform 0.12s ease,
    background 0.15s ease,
    color 0.15s ease,
    border-color 0.15s ease,
    box-shadow 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}

.answer-btn:active:not(:disabled) {
  transform: translateY(2px);
  box-shadow: 0 2px 0 rgba(20, 80, 140, 0.16);
}

.answer-btn.selected {
  background: #1f8fff;
  color: var(--white);
  border-color: #0b5fbf;
  box-shadow:
    0 0 0 4px rgba(31, 143, 255, 0.35),
    0 6px 0 rgba(11, 95, 191, 0.45);
  transform: translateY(-2px) scale(1.03);
  z-index: 1;
}

.answer-btn.correct {
  background: var(--green);
  color: var(--white);
  border-color: var(--green-dark);
  box-shadow: 0 5px 0 rgba(42, 163, 68, 0.45);
  outline: none;
  transform: none;
}

.answer-btn.wrong {
  background: var(--red);
  color: var(--white);
  border-color: var(--red-dark);
  box-shadow: 0 5px 0 rgba(185, 28, 28, 0.4);
  outline: none;
  transform: none;
}

.answer-btn:disabled {
  cursor: default;
}

#timer-wrap {
  width: var(--col-max);
  flex-shrink: 0;
  padding-top: 4px;
}

#timer-track {
  height: var(--timer-h);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: inset 0 2px 4px rgba(20, 80, 140, 0.12);
  overflow: hidden;
  padding: 3px;
}

#timer-fill {
  height: 100%;
  width: 100%;
  max-width: 100%;
  border-radius: 999px;
  background-color: var(--green);
  background-image: repeating-linear-gradient(
    -45deg,
    rgba(255, 255, 255, 0.28) 0 8px,
    transparent 8px 16px
  );
  background-size: var(--timer-stripe) var(--timer-stripe);
  will-change: width, background-color;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.12);
  transition: none;
}

#result {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  background: rgba(30, 90, 140, 0.45);
  backdrop-filter: blur(4px);
  padding: 24px;
}

#result.hidden {
  display: none;
}

.result-card {
  width: min(400px, 100%);
  background: var(--white);
  border-radius: 24px;
  padding: 28px 22px;
  text-align: center;
  box-shadow: 0 16px 40px rgba(10, 40, 80, 0.25);
}

.result-eyebrow {
  margin: 0 0 6px;
  font-size: 0.75rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #6a8aa8;
}

#result-score {
  margin: 0;
  font-size: clamp(2.2rem, 5vw, 2.8rem);
  font-weight: 900;
  color: var(--ink);
}

#result-sub {
  margin: 4px 0 18px;
  font-weight: 700;
  color: #5a7a96;
}

#replay-btn {
  appearance: none;
  border: none;
  background: var(--pink);
  color: var(--white);
  font-family: inherit;
  font-weight: 900;
  font-size: 1rem;
  padding: 12px 28px;
  border-radius: 999px;
  box-shadow: 0 4px 0 rgba(200, 40, 100, 0.35);
  cursor: pointer;
}

#replay-btn:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 rgba(200, 40, 100, 0.35);
}

body.is-revealing .answer-btn {
  pointer-events: none;
}

/* —— Large phone / small tablet —— */
@media (min-width: 600px) {
  :root {
    --col-max: min(100%, 640px);
    --flag-w: min(78%, min(480px, 42vh));
    --answers-max: min(100%, 560px);
    --btn-min-h: 68px;
    --btn-font: 1.08rem;
    --btn-pad-y: 16px;
    --btn-pad-x: 14px;
    --btn-gap: 12px;
    --stage-gap: clamp(16px, 3vh, 28px);
    --wrap-pad-x: 24px;
    --wrap-pad-y: 16px;
    --q-size: 52px;
    --q-font: 1.4rem;
    --score-font: clamp(1.7rem, 4vw, 2.1rem);
    --tier-font: 0.72rem;
    --tier-pad-y: 6px;
    --tier-pad-x: 12px;
    --tier-gap: 7px;
    --timer-h: 26px;
    --timer-stripe: 24px;
    --flag-pad: 14px;
    --flag-radius: 20px;
    --pattern-size: 84px;
  }

  .answer-btn {
    border-radius: 18px;
  }

  #replay-btn {
    font-size: 1.1rem;
    padding: 14px 32px;
  }
}

/* —— iPad portrait —— */
@media (min-width: 768px) {
  :root {
    --col-max: min(92%, 780px);
    --flag-w: min(72%, min(560px, 40vh));
    --answers-max: min(100%, 680px);
    --btn-min-h: 80px;
    --btn-font: 1.2rem;
    --btn-pad-y: 20px;
    --btn-pad-x: 18px;
    --btn-radius: 22px;
    --btn-gap: 16px;
    --stage-gap: clamp(20px, 3.4vh, 36px);
    --wrap-pad-x: 36px;
    --wrap-pad-y: 24px;
    --q-size: 60px;
    --q-font: 1.55rem;
    --score-font: clamp(2rem, 3.2vw, 2.5rem);
    --tier-font: 0.82rem;
    --tier-pad-y: 8px;
    --tier-pad-x: 16px;
    --tier-gap: 9px;
    --timer-h: 32px;
    --timer-stripe: 28px;
    --flag-pad: 18px;
    --flag-radius: 24px;
    --pattern-size: 100px;
  }

  #wrap {
    max-width: none;
    width: 100%;
  }

  .result-card {
    width: min(480px, 100%);
    padding: 40px 32px;
    border-radius: 28px;
  }
}

/* —— iPad Pro / large tablet —— */
@media (min-width: 1024px) {
  :root {
    --col-max: min(88%, 920px);
    --flag-w: min(58%, min(640px, 42vh));
    --answers-max: min(100%, 780px);
    --btn-min-h: 88px;
    --btn-font: 1.3rem;
    --btn-pad-y: 22px;
    --btn-pad-x: 20px;
    --btn-gap: 18px;
    --stage-gap: clamp(24px, 3.8vh, 42px);
    --wrap-pad-x: 48px;
    --wrap-pad-y: 28px;
    --q-size: 64px;
    --q-font: 1.65rem;
    --score-font: clamp(2.2rem, 2.8vw, 2.75rem);
    --tier-font: 0.9rem;
    --tier-pad-y: 9px;
    --tier-pad-x: 18px;
    --tier-gap: 10px;
    --timer-h: 36px;
    --timer-stripe: 30px;
    --flag-pad: 20px;
    --flag-radius: 26px;
    --pattern-size: 112px;
  }
}

/* —— Extra-wide iPad Pro 12.9" —— */
@media (min-width: 1100px) {
  :root {
    --col-max: min(84%, 980px);
    --flag-w: min(54%, min(680px, 40vh));
    --answers-max: min(100%, 840px);
    --btn-min-h: 92px;
    --btn-font: 1.35rem;
  }
}

/* —— Landscape tablets: wide but height-aware —— */
@media (orientation: landscape) and (min-width: 900px) and (min-height: 600px) {
  :root {
    --col-max: min(78%, 900px);
    --flag-w: min(36vh, min(480px, 34vw));
    --answers-max: min(100%, 720px);
    --btn-min-h: 64px;
    --btn-font: clamp(1rem, 2.4vh, 1.2rem);
    --btn-pad-y: 12px;
    --btn-gap: 14px;
    --stage-gap: clamp(12px, 2.4vh, 22px);
    --wrap-pad-x: 40px;
    --wrap-pad-y: 14px;
    --timer-h: 28px;
    --q-size: 52px;
    --q-font: 1.35rem;
    --score-font: clamp(1.7rem, 3.5vh, 2.2rem);
    --tier-font: 0.75rem;
    --tier-pad-y: 6px;
    --tier-pad-x: 12px;
    --tier-gap: 6px;
  }

  #wrap {
    max-width: none;
    width: 100%;
    margin: 0 auto;
  }

  #stage {
    padding-bottom: 4px;
  }
}

@media (orientation: landscape) and (max-height: 500px) {
  :root {
    --col-max: min(90%, 700px);
    --flag-w: min(44vh, 300px);
    --answers-max: min(100%, 560px);
    --btn-min-h: 48px;
    --btn-font: 0.9rem;
    --btn-pad-y: 8px;
    --stage-gap: 8px;
    --wrap-pad-y: 6px;
    --timer-h: 18px;
    --q-size: 36px;
    --q-font: 1rem;
    --score-font: 1.25rem;
    --tier-font: 0.52rem;
    --tier-gap: 3px;
  }

  #top-bar {
    margin-bottom: 0;
  }
}

