:root {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: #1f2a37;
  /* Solid backdrop colour. iOS only ever paints the background-COLOUR (never a
     gradient image) into the landscape safe-area strips beside the notch and the
     overscroll area, so a gradient backdrop leaves a visible seam there in a
     browser tab. One solid colour fills every region identically; the gradients
     live on the cards, buttons and accent bar instead. */
  background-color: #6e64c6;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;

  /* Gradient design tokens — single source of truth for the brand colours.
     Change a gradient here instead of hunting down every literal. */
  --grad-primary: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  --grad-success: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
  --grad-info: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  --grad-warm: linear-gradient(135deg, #fbbf24 0%, #f97316 100%);
  --grad-danger: linear-gradient(135deg, #f5576c 0%, #f093fb 100%);
}

* {
  box-sizing: border-box;
}

/* Enforce the hidden attribute even against high-specificity layout rules
   (e.g. the short-landscape `#test.card { display: grid }`), so a hidden
   section can never render on top of the active one. */
[hidden] {
  display: none !important;
}

body {
  margin: 0;
  /* Inset content from the notch / translucent status bar / home indicator when
     the app runs full-screen as an installed PWA (viewport-fit=cover). The solid
     :root colour still fills these strips edge-to-edge; only the cards are inset.
     Resolves to 0 where there is no safe area (desktop / non-notched devices). */
  padding:
    env(safe-area-inset-top)
    env(safe-area-inset-right)
    env(safe-area-inset-bottom)
    env(safe-area-inset-left);
  min-height: 100vh;
  /* Dynamic viewport height: matches the actually-visible area on iOS Safari, so
     the centred card isn't pushed below the fold by the address/tab bars. */
  min-height: 100dvh;
  /* Transparent so the solid :root backdrop colour shows through uniformly. */
  background: transparent;
  /* Stops pull-to-refresh and rubber-band overscroll on Chromium (Android /
     Chromebook / desktop). No effect on iOS: every iOS browser, Chrome included,
     runs on WebKit, which ignores overscroll-behavior for the document. There the
     installed (standalone) app has no pull-to-refresh gesture anyway. */
  overscroll-behavior: none;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
  display: flex;
  align-items: center;
  /* `safe` keeps the top of an over-tall card reachable instead of clipping it
     above the scroll origin (falls back to plain `center` where unsupported). */
  align-items: safe center;
  justify-content: center;
}

main {
  max-width: 1024px;
  margin: 0 auto;
  padding: 0.5rem 1rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.card {
  background: #fff;
  border-radius: 20px;
  padding: 1.25rem;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

/* Static accent bar. (Previously an infinite gradient animation — removed so it
   doesn't repaint forever and drain battery on phones/tablets.) */
.card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 6px;
  background: linear-gradient(90deg, #667eea, #764ba2, #f093fb, #f5576c);
}

.keyboard {
  margin-top: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.keyboard-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(60px, 1fr));
  gap: 0.5rem;
}

.keyboard button {
  background: linear-gradient(135deg, #fff 0%, #f8f9ff 100%);
  border: 2px solid #e0e7ff;
  border-radius: 14px;
  font-size: 1.125rem;
  padding: 0.75rem 0;
  min-height: 50px;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.15), inset 0 -2px 0 rgba(102, 126, 234, 0.1);
  transition: all 0.2s ease;
  font-weight: 700;
  touch-action: manipulation;
  color: #667eea;
}

.keyboard button:active {
  background: var(--grad-primary);
  color: #fff;
  border-color: #667eea;
  transform: translateY(3px) scale(0.98);
  box-shadow: 0 2px 6px rgba(102, 126, 234, 0.3);
}

@media (hover: hover) and (pointer: fine) {
  .keyboard button:hover {
    background: var(--grad-primary);
    color: #fff;
    border-color: #667eea;
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
  }
}

.keyboard button[data-action] {
  font-size: 0.9rem;
  font-weight: 700;
}

.answer-field {
  flex: 1;
  padding: 0.65rem 0.85rem;
  border-radius: 12px;
  border: 2px solid #e0e7ff;
  font-size: 1.05rem;
  transition: all 0.2s ease;
  min-height: 44px;
  background: #fff;
  font-weight: 600;
  color: #1f2a37;
}

.answer-field:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
  background: linear-gradient(135deg, #fff 0%, #f8f9ff 100%);
}

/* Shake + red border when OK/Enter is pressed with an empty answer field. */
.answer-field.nudge {
  border-color: #f5576c;
  box-shadow: 0 0 0 4px rgba(245, 87, 108, 0.3);
  animation: shake 300ms ease;
}

button {
  border: none;
  border-radius: 999px;
  padding: 0.65rem 1.25rem;
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
  transition: all 0.2s ease;
  min-height: 44px;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

button[type="submit"] {
  background: var(--grad-primary);
  color: #fff;
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
  border: 3px solid transparent;
}

button[type="submit"]:active {
  transform: scale(0.96) translateY(2px);
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

@media (hover: hover) and (pointer: fine) {
  button[type="submit"]:hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 35px rgba(102, 126, 234, 0.5);
  }
}

button.secondary {
  background: linear-gradient(135deg, #f8f9ff 0%, #e0e7ff 100%);
  color: #667eea;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.15);
  border: 3px solid #e0e7ff;
}

button.secondary:active {
  transform: scale(0.96);
}

@media (hover: hover) and (pointer: fine) {
  /* Flat controls (text link, secondary) shouldn't lift; only raised buttons do.
     :where() keeps the exclusion at zero specificity so this stays a default
     lift — buttons with their own hover (.start-btn, .modal-danger) still win. */
  button:where(:not(.link-button):not(.secondary)):hover {
    transform: translateY(-3px);
  }
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.link-button {
  background: transparent;
  color: #667eea;
  padding: 0.5rem 1rem;
  box-shadow: none;
  border-radius: 12px;
  font-weight: 600;
  transition: all 0.2s ease;
}

@media (hover: hover) and (pointer: fine) {
  .link-button:hover {
    background: linear-gradient(135deg, #f8f9ff 0%, #e0e7ff 100%);
  }
}

.prompt {
  font-size: 1.75rem;
  font-weight: 800;
  margin: 0.75rem 0 0.5rem;
  color: #1f2a37;
  text-align: center;
  background: var(--grad-primary);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-shadow: 0 2px 10px rgba(102, 126, 234, 0.1);
}

.answer-form {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  align-items: center;
}

#answer-input {
  flex: 1;
}

.sr-only {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  width: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
}

@keyframes pop {
  0% {
    transform: scale(0.94);
  }
  50% {
    transform: scale(1.08);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes shake {
  0%, 100% {
    transform: translateX(0);
  }
  20%, 60% {
    transform: translateX(-8px);
  }
  40%, 80% {
    transform: translateX(8px);
  }
}

#confetti-container {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 999;
}

.confetti-piece {
  position: absolute;
  width: 12px;
  height: 16px;
  border-radius: 3px;
  opacity: 0;
  transform: translate3d(0, -60px, 0) rotate(0deg);
  animation: confetti-fall var(--confetti-duration, 1.8s) cubic-bezier(0.25, 0.46, 0.45, 0.94) forwards;
  animation-delay: var(--confetti-delay, 0s);
}

@keyframes confetti-fall {
  0% {
    opacity: 1;
    transform: translate3d(0, -60px, 0) rotate(0deg);
  }
  100% {
    opacity: 0;
    transform: translate3d(
        var(--confetti-drift, 0px),
        110vh,
        0
      )
      rotate(var(--confetti-rotation, 720deg));
  }
}

#result-screen h2 {
  background: var(--grad-success);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  font-size: 1.75rem;
  margin-bottom: 0.75rem;
}

@media (max-width: 540px) {
  main {
    padding: 0.5rem 0.75rem 0.75rem;
    gap: 0.5rem;
  }

  .answer-form {
    flex-direction: column;
  }

  button,
  .answer-field {
    width: 100%;
  }

  .keyboard {
    margin-top: 0.75rem;
  }

  .keyboard-row {
    gap: 0.5rem;
  }

  .keyboard button {
    min-height: 50px;
    font-size: 1.15rem;
    padding: 0.75rem 0;
  }

  .prompt {
    font-size: 1.85rem;
    margin: 0.65rem 0 0.5rem;
  }

  .card {
    padding: 1.15rem;
    border-radius: 18px;
  }

  .link-button {
    font-size: 0.85rem;
    padding: 0.35rem 0.75rem;
    min-height: 38px;
  }

  .answer-field {
    font-size: 1.1rem;
    padding: 0.65rem 0.9rem;
    min-height: 46px;
  }

  button[type="submit"],
  button.secondary {
    font-size: 1rem;
    padding: 0.65rem 1.35rem;
    min-height: 46px;
  }

  #result-screen h2 {
    font-size: 1.5rem;
    margin-bottom: 0.5rem;
  }
}

@media (min-width: 768px) and (max-width: 1024px) {
  .card {
    padding: 3rem;
  }

  .keyboard-row {
    grid-template-columns: repeat(3, minmax(110px, 150px));
    justify-content: center;
  }
}

@media (min-width: 1024px) {
  .keyboard-row {
    grid-template-columns: repeat(3, 150px);
    justify-content: center;
  }
}
