/* ─── Reset & Base ───────────────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --orange:   #E89B2C;
  --orange-dk:#C07D18;
  --blue:     #2B5F8C;
  --blue-lt:  #3A7AB0;
  --cream:    #F6EDD8;
  --cream-dk: #EDE0C4;
  --ink:      #2A2318;
  --ink-lt:   #5A4E3C;
  --muted:    #8C7D6A;
  --wrong-bg: #F0E8D0;
  --wrong-fg: #7A5A30;
  --radius:   12px;
  --shadow:   0 4px 24px rgba(42,35,24,.14), 0 1px 4px rgba(42,35,24,.08);
}

html {
  font-size: 18px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background-color: var(--cream);
  color: var(--ink);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 0 env(safe-area-inset-bottom, 20px);

  /* Subtle paper grain via SVG turbulence */
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='300'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.75' numOctaves='4' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3CfeBlend in='SourceGraphic' mode='multiply'/%3E%3C/filter%3E%3Crect width='300' height='300' filter='url(%23n)' opacity='0.055'/%3E%3C/svg%3E");
}

/* ─── Header ─────────────────────────────────────────────────────── */
header {
  width: 100%;
  text-align: center;
  padding: 32px 20px 20px;
  position: relative;
}

header::after {
  content: '';
  display: block;
  margin: 16px auto 0;
  width: 60px;
  height: 3px;
  border-radius: 2px;
  background: linear-gradient(90deg, var(--orange), var(--orange-dk));
}

h1 {
  font-size: clamp(1.4rem, 5vw, 2rem);
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--blue);
  line-height: 1.2;
}

h1 span {
  color: var(--orange-dk);
}

/* ─── Main card ───────────────────────────────────────────────────── */
main {
  width: min(480px, 100%);
  padding: 0 16px 40px;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.clue-card {
  background: #FFFDF5;
  border: 1px solid var(--cream-dk);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 24px 24px;
  position: relative;
  overflow: hidden;
  min-height: 140px;
}

/* Ruled-line feel — three subtle horizontal lines at top */
.clue-card::before {
  content: '';
  position: absolute;
  inset: 0;
  background-image: repeating-linear-gradient(
    transparent,
    transparent 31px,
    rgba(42,35,24,.06) 31px,
    rgba(42,35,24,.06) 32px
  );
  border-radius: var(--radius);
  pointer-events: none;
}

.clue-label {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 14px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.clue-label::before {
  content: '';
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--orange);
}

.clue-text {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.15rem;
  line-height: 1.75;
  color: var(--ink);
  white-space: pre-wrap;
  text-wrap: pretty;
  position: relative;
  z-index: 1;
}

/* Stamp overlay for correct-code animation */
.stamp-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  opacity: 0;
  z-index: 10;
}

.stamp-mark {
  width: 90px;
  height: 90px;
  border-radius: 50%;
  border: 5px double var(--orange);
  color: var(--orange);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.8rem;
  font-weight: 900;
  transform: scale(1.5) rotate(-15deg);
  opacity: 0;
  transition: none;
  background: rgba(255,253,245,.85);
}

.stamp-overlay.stamping {
  opacity: 1;
}
.stamp-overlay.stamping .stamp-mark {
  animation: stamp-press 0.45s cubic-bezier(0.22, 0.61, 0.36, 1) forwards;
}

@keyframes stamp-press {
  0%   { transform: scale(1.5) rotate(-15deg); opacity: 0; }
  40%  { transform: scale(0.92) rotate(-15deg); opacity: 1; }
  60%  { transform: scale(1.02) rotate(-15deg); opacity: 1; }
  100% { transform: scale(1.0) rotate(-15deg); opacity: 1; }
}

/* ─── Input area ─────────────────────────────────────────────────── */
.input-area {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.code-input {
  width: 100%;
  height: 68px;
  text-align: center;
  font-family: "Courier New", Courier, monospace;
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: 0.25em;
  text-transform: uppercase;
  color: var(--ink);
  background: #FFFDF5;
  border: 2.5px solid var(--cream-dk);
  border-radius: var(--radius);
  box-shadow: inset 0 2px 6px rgba(42,35,24,.07);
  padding: 0 12px;
  transition: border-color .15s, box-shadow .15s;
  -webkit-appearance: none;
  appearance: none;
  caret-color: var(--orange-dk);
  touch-action: manipulation;
}

.code-input:focus {
  outline: none;
  border-color: var(--orange);
  box-shadow: inset 0 2px 6px rgba(42,35,24,.07), 0 0 0 3px rgba(232,155,44,.22);
}

.code-input.shake {
  animation: shake 0.38s ease;
  border-color: var(--wrong-fg);
}

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

.submit-btn {
  width: 100%;
  height: 60px;
  background: var(--orange);
  color: #fff;
  font-family: inherit;
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background .15s, transform .1s, box-shadow .15s;
  box-shadow: 0 3px 12px rgba(192,125,24,.35);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  position: relative;
  overflow: hidden;
}

.submit-btn:active {
  background: var(--orange-dk);
  transform: translateY(1px);
  box-shadow: 0 1px 6px rgba(192,125,24,.30);
}

.submit-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

/* Ripple on tap */
.submit-btn .ripple {
  position: absolute;
  border-radius: 50%;
  background: rgba(255,255,255,.35);
  transform: scale(0);
  animation: ripple-out 0.5s linear;
  pointer-events: none;
}

@keyframes ripple-out {
  to { transform: scale(4); opacity: 0; }
}

/* ─── Feedback ───────────────────────────────────────────────────── */
.feedback {
  min-height: 28px;
  text-align: center;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--wrong-fg);
  background: transparent;
  border-radius: 8px;
  padding: 4px 8px;
  opacity: 0;
  transition: opacity .2s;
}

.feedback.visible {
  opacity: 1;
  background: var(--wrong-bg);
}

/* ─── Clue transition ────────────────────────────────────────────── */
.clue-card.fading-out {
  opacity: 0;
  transform: translateY(-6px);
  transition: opacity .35s ease, transform .35s ease;
}

.clue-card.fading-in {
  opacity: 0;
  transform: translateY(6px);
}

.clue-card.fading-in-active {
  opacity: 1;
  transform: translateY(0);
  transition: opacity .45s ease, transform .45s ease;
}

/* ─── Finale ─────────────────────────────────────────────────────── */
.finale-card {
  background: linear-gradient(160deg, #FFFDF5 60%, #FFF3D4);
  border: 2px solid var(--orange);
  border-radius: var(--radius);
  box-shadow: var(--shadow), 0 0 0 6px rgba(232,155,44,.12);
  padding: 36px 28px 32px;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.finale-card .sparkle-ring {
  font-size: 2rem;
  display: block;
  margin-bottom: 12px;
  animation: pulse-sparkle 2s ease-in-out infinite;
}

@keyframes pulse-sparkle {
  0%, 100% { transform: scale(1) rotate(0deg); }
  50% { transform: scale(1.1) rotate(8deg); }
}

.finale-heading {
  font-family: Georgia, serif;
  font-size: 1.7rem;
  font-weight: 700;
  color: var(--blue);
  margin-bottom: 16px;
  line-height: 1.2;
}

.finale-message {
  font-family: Georgia, serif;
  font-size: 1.1rem;
  line-height: 1.7;
  color: var(--ink);
  text-wrap: pretty;
}

/* Confetti canvas */
#confetti-canvas {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 999;
}

/* ─── Footer ─────────────────────────────────────────────────────── */
footer {
  padding: 12px 0 max(16px, env(safe-area-inset-bottom));
  text-align: center;
  font-size: 0.72rem;
  color: var(--muted);
  letter-spacing: 0.05em;
}

/* ─── Accessibility ───────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
