/* Fraunces, self-hosted (variable: weight 400–700 + optical size) */
@font-face {
  font-family: "Fraunces";
  src: url("../fonts/Fraunces-latin.woff2") format("woff2");
  font-weight: 400 700;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "Fraunces";
  src: url("../fonts/Fraunces-Italic-latin.woff2") format("woff2");
  font-weight: 400 700;
  font-style: italic;
  font-display: swap;
}

:root {
  --red-1: #c0392b;
  --red-2: #8f241b;
  --cream: #fff3dc;
  --ink: #3a2418;
  --gold: #e9b44c;
  --radius: 999px;
  --pad: calc(56px + max(env(safe-area-inset-top), env(safe-area-inset-bottom)));
  /* viewBox is 490x430 (aspect 1.14); also cap by height so title, hint and buttons stay on screen */
  --stage-w: min(calc(100vw - 32px), 680px, calc(50vh * 1.14));
  --stage-w: min(calc(100vw - 32px), 680px, calc(50svh * 1.14));
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100svh;
  color: var(--cream);
  font-family: "Fraunces", Georgia, "Times New Roman", serif;
  font-optical-sizing: auto;
  background: var(--red-2) radial-gradient(ellipse at 50% 42%, var(--red-1) 0%, var(--red-2) 75%) fixed;
}

.page {
  min-height: 100vh;
  min-height: 100svh;
  display: grid;
  grid-template-rows: 1fr auto 1fr;
  grid-template-columns: minmax(0, 1fr);
  justify-items: center;
  /* equal top and bottom padding keeps the cookie row at the exact vertical centre of the viewport */
  padding: var(--pad) 16px;
  position: relative;
}
.title { align-self: center; }
.below {
  align-self: start;
  padding-top: clamp(8px, 2vh, 20px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(8px, 2vh, 20px);
}

.credit {
  position: absolute;
  left: 0;
  right: 0;
  bottom: max(16px, env(safe-area-inset-bottom));
  text-align: center;
  font-size: .9rem;
  opacity: .85;
}
.credit a { color: inherit; text-underline-offset: 3px; }
.credit a:hover { text-decoration-thickness: 2px; }
.credit a:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }

.title {
  margin: 0;
  font-size: clamp(1.05rem, 3vw, 1.35rem);
  font-weight: 600;
  letter-spacing: .02em;
  opacity: .92;
}

/* ---------- Cookie stage ---------- */
.stage {
  display: block;
  width: var(--stage-w);
  padding: 0;
  margin: 0;
  border: 0;
  background: none;
  color: inherit;
  cursor: pointer;
  border-radius: 24px;
  -webkit-tap-highlight-color: transparent;
  transition: transform .25s ease;
}
.stage:hover:not(:disabled) { transform: scale(1.025) rotate(-1.2deg); }
.stage:active:not(:disabled) { transform: scale(.985); }
.stage.is-locked,
.stage.is-locked:hover:not(:disabled),
.stage.is-locked:active:not(:disabled) { cursor: default; transform: none; }
.stage:focus-visible { outline: 3px solid var(--gold); outline-offset: 6px; }

.frames { display: grid; }
.frame { grid-area: 1 / 1; }
.frame:not(.is-on) { visibility: hidden; }
.frame svg { display: block; width: 100%; height: auto; overflow: visible; }

body[data-state="rest"] .frames { animation: bob 4.2s ease-in-out infinite; }
@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(0); }
  50%      { transform: translateY(-6px) rotate(-.8deg); }
}

/* Final frame: the slip rotates to horizontal and the fortune is written on it */
.frame--last .paper {
  transform-box: fill-box;
  transform-origin: center;
}
.frame--last.is-animated .paper {
  transition: transform .6s cubic-bezier(.3, 1.35, .5, 1);
}
.frame--last.is-flat .paper {
  transform: translate(var(--paper-dx), var(--paper-dy)) scale(var(--paper-sx), var(--paper-sy)) rotate(var(--paper-angle));
}
.fortune-text {
  fill: var(--ink);
  font-family: "Fraunces", Georgia, serif;
  font-style: italic;
  font-weight: 500;
  text-anchor: middle;
  opacity: 0;
  pointer-events: none;
}
.frame--last.is-animated .fortune-text { transition: opacity .45s ease .3s; }
.frame--last.is-flat .fortune-text { opacity: 1; }

/* ---------- Hint + actions ---------- */
.hint {
  margin: 0;
  min-height: 1.5em;
  font-size: clamp(1rem, 2.6vw, 1.15rem);
  font-style: italic;
  text-align: center;
  opacity: .9;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 12px;
  min-height: 48px;
}

.btn {
  font: inherit;
  font-weight: 600;
  font-size: 1rem;
  padding: 12px 22px;
  border-radius: 0;
  border: 2px solid var(--cream);
  background: var(--cream);
  color: var(--red-2);
  cursor: pointer;
  transition: transform .15s ease, opacity .25s ease, background-color .15s ease;
}
.btn:hover:not(:disabled) { transform: translateY(-1px); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }
.btn--ghost { background: transparent; color: var(--cream); }
.btn--ghost:hover:not(:disabled) { background: rgba(255, 243, 220, .12); }
.btn:disabled { opacity: 0; pointer-events: none; }
.btn--share {
  min-width: 11.5em;
  max-width: 14em;
  overflow: hidden;
  white-space: nowrap;
  transition: max-width .35s ease, min-width .35s ease, padding .35s ease, margin .35s ease,
              opacity .25s ease, background-color .15s ease, transform .15s ease;
}
/* Collapsed (no fortune yet) so "Add a fortune" stays centred; grows in when a fortune opens */
.btn--share:disabled {
  min-width: 0;
  max-width: 0;
  padding-left: 0;
  padding-right: 0;
  margin-left: -12px;
  border-left-width: 0;
  border-right-width: 0;
}
.btn--share.is-copied { background: var(--gold); border-color: var(--gold); color: var(--ink); }

/* ---------- Add-a-fortune dialog ---------- */
.dialog {
  width: min(92vw, 440px);
  padding: 0;
  border: 0;
  border-radius: 0;
  background: var(--cream);
  color: var(--ink);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .35);
}
.dialog::backdrop { background: rgba(58, 20, 12, .6); }
.dialog__body { padding: 24px; display: flex; flex-direction: column; gap: 6px; }
.dialog h2 { margin: 0; font-size: 1.5rem; font-weight: 600; }
.dialog__lede { margin: 2px 0 10px; font-size: .98rem; line-height: 1.45; }
.dialog label { font-size: .9rem; font-weight: 600; margin-top: 8px; }
.dialog textarea,
.dialog input {
  font: inherit;
  font-size: 1rem;
  width: 100%;
  padding: 10px 12px;
  border: 2px solid #d9bf8f;
  border-radius: 0;
  background: #fffaf0;
  color: var(--ink);
}
.dialog textarea { resize: vertical; min-height: 84px; }
.dialog textarea:focus,
.dialog input:focus { outline: none; border-color: var(--ink); }
.dialog__count { align-self: flex-end; font-size: .8rem; opacity: .7; }
.dialog__status { min-height: 1.3em; margin: 6px 0 0; font-size: .92rem; }
.dialog__status.is-error { color: #a4161a; }
.dialog__actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }
.dialog__fallback { margin: 8px 0 0; font-size: .85rem; opacity: .8; }
.dialog__fallback a { color: inherit; }
.btn--dark { background: var(--red-2); border-color: var(--red-2); color: var(--cream); }
.btn--ghost.btn--dark { background: transparent; color: var(--red-2); }

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

@media (prefers-reduced-motion: reduce) {
  body[data-state="rest"] .frames { animation: none; }
  .stage, .btn { transition: none; }
  .frame--last.is-animated .paper,
  .frame--last.is-animated .fortune-text { transition: none; }
}
