/* Hogwarts Face Studio — themed, mobile-first, responsive. */
:root {
  --night: #0e0b1a;
  --night-2: #171231;
  --panel: rgba(28, 22, 54, 0.72);
  --panel-solid: #1c1636;
  --gold: #d3a625;
  --gold-soft: #e7c75a;
  --parch: #efe6cf;
  --muted: #b9aee0;
  --ink: #0b0816;
  --danger: #f0c36b;
  --radius: 18px;
  --display: "Cinzel", "Georgia", serif;
  --body: "EB Garamond", "Georgia", serif;
}

* { box-sizing: border-box; }

/* The `hidden` attribute must always win. Author rules like `.btn { display: inline-flex }`
   otherwise override the UA `[hidden] { display: none }`, leaving "hidden" buttons visible
   (e.g. "Choose character" showing before a photo is taken). */
[hidden] { display: none !important; }

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

body {
  min-height: 100dvh;
  font-family: var(--body);
  color: var(--parch);
  background:
    radial-gradient(1200px 700px at 50% -10%, #2a2152 0%, rgba(42, 33, 82, 0) 60%),
    linear-gradient(180deg, var(--night-2) 0%, var(--night) 60%, #07050f 100%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* faint starfield */
.stars {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background-image:
    radial-gradient(1.4px 1.4px at 20% 30%, rgba(255, 255, 255, 0.7), transparent),
    radial-gradient(1.2px 1.2px at 70% 20%, rgba(255, 255, 255, 0.55), transparent),
    radial-gradient(1.6px 1.6px at 40% 70%, rgba(255, 255, 255, 0.5), transparent),
    radial-gradient(1.1px 1.1px at 85% 60%, rgba(255, 255, 255, 0.5), transparent),
    radial-gradient(1.3px 1.3px at 10% 80%, rgba(255, 255, 255, 0.45), transparent);
  opacity: 0.6;
}

.app {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: clamp(16px, 4vw, 28px) clamp(14px, 4vw, 22px) 32px;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* masthead */
.masthead { text-align: center; margin: 6px 0 18px; }
.title {
  font-family: var(--display);
  font-weight: 700;
  letter-spacing: 0.04em;
  font-size: clamp(1.6rem, 7vw, 2.4rem);
  margin: 0;
  color: var(--gold);
  text-shadow: 0 0 18px rgba(211, 166, 37, 0.35);
}
.subtitle { margin: 6px 0 0; color: var(--muted); font-size: 0.98rem; }
.muted { opacity: 0.7; }

/* screens */
.screen { display: none; flex: 1; }
.screen.is-active { display: block; animation: fade 0.4s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* card */
.card {
  background: var(--panel);
  border: 1px solid rgba(211, 166, 37, 0.28);
  border-radius: var(--radius);
  padding: clamp(18px, 5vw, 26px);
  backdrop-filter: blur(6px);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
}
.card.center { text-align: center; }
.card h2 { font-family: var(--display); color: var(--parch); margin: 0 0 12px; font-size: 1.3rem; }
.card p { color: var(--muted); line-height: 1.55; }
.step { text-align: center; }

/* welcome */
.welcome-card { text-align: center; }
.crest { font-size: 3rem; filter: drop-shadow(0 0 16px rgba(211, 166, 37, 0.5)); }
.fine { font-size: 0.86rem; opacity: 0.75; margin-bottom: 0; }

/* buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4em;
  font-family: var(--display);
  font-size: 1rem;
  letter-spacing: 0.02em;
  padding: 14px 20px;
  border-radius: 999px;
  border: 1px solid transparent;
  cursor: pointer;
  text-decoration: none;
  user-select: none;
  transition: transform 0.08s ease, box-shadow 0.2s ease, background 0.2s ease;
  min-height: 50px;
}
.btn:active { transform: translateY(1px) scale(0.99); }
.btn-primary {
  color: var(--ink);
  background: linear-gradient(180deg, var(--gold-soft), var(--gold));
  box-shadow: 0 8px 22px rgba(211, 166, 37, 0.35);
  font-weight: 700;
}
.btn-primary:hover { box-shadow: 0 10px 28px rgba(211, 166, 37, 0.5); }
.btn-primary:disabled { opacity: 0.45; cursor: not-allowed; box-shadow: none; }
.btn-ghost {
  color: var(--parch);
  background: rgba(255, 255, 255, 0.06);
  border-color: rgba(211, 166, 37, 0.35);
}
.btn-ghost:hover { background: rgba(255, 255, 255, 0.12); }

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  margin-top: 18px;
}
.controls .btn { flex: 1 1 auto; min-width: 140px; }
.file-btn { position: relative; overflow: hidden; }

/* capture stage */
.stage {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 16px;
  overflow: hidden;
  background: #05040c;
  box-shadow: inset 0 0 0 1px rgba(211, 166, 37, 0.25);
}
#video, #upload-preview {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
#video { transform: scaleX(-1); } /* mirror for a natural selfie */
.oval-guide { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.oval-stroke {
  fill: none;
  stroke: var(--gold);
  stroke-width: 2.5;
  stroke-dasharray: 7 9;
  filter: drop-shadow(0 0 6px rgba(211, 166, 37, 0.7));
  animation: spinDash 22s linear infinite;
}
@keyframes spinDash { to { stroke-dashoffset: -320; } }
.stage-hint {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  background: rgba(8, 6, 18, 0.6);
  color: var(--parch);
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 0.85rem;
  white-space: nowrap;
}

/* notice / errors */
.notice {
  margin-top: 14px;
  padding: 12px 14px;
  border-radius: 12px;
  background: rgba(211, 166, 37, 0.12);
  border: 1px solid rgba(211, 166, 37, 0.4);
  color: var(--danger);
  font-size: 0.92rem;
  text-align: center;
}

/* character grid */
.grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-top: 6px;
}
.char {
  --house: #7f0909;
  --house2: #d3a625;
  text-align: center;
  cursor: pointer;
  color: var(--parch);
  border-radius: 14px;
  padding: 16px 10px 14px;
  background:
    radial-gradient(120% 90% at 50% 0%, color-mix(in srgb, var(--house) 60%, transparent) 0%, transparent 70%),
    rgba(255, 255, 255, 0.04);
  border: 1.5px solid color-mix(in srgb, var(--house2) 45%, transparent);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: transform 0.1s ease, box-shadow 0.2s ease, border-color 0.2s ease;
}
.char:hover { transform: translateY(-2px); }
.char.is-selected {
  border-color: var(--gold);
  box-shadow: 0 0 0 2px var(--gold), 0 10px 26px rgba(211, 166, 37, 0.3);
}
.char-emoji { font-size: 1.9rem; line-height: 1; }
.char-name { font-family: var(--display); font-weight: 700; font-size: 1rem; }
.char-tag { font-size: 0.8rem; color: var(--muted); line-height: 1.25; }
.char-house {
  margin-top: 2px;
  font-size: 0.68rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  opacity: 0.7;
}

/* loading */
.cauldron { font-size: 2.6rem; animation: float 2.4s ease-in-out infinite; }
@keyframes float { 0%, 100% { transform: translateY(0) rotate(-6deg); } 50% { transform: translateY(-8px) rotate(8deg); } }
.spinner {
  width: 54px;
  height: 54px;
  margin: 18px auto 14px;
  border-radius: 50%;
  border: 4px solid rgba(211, 166, 37, 0.25);
  border-top-color: var(--gold);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { font-family: var(--display); color: var(--gold-soft); min-height: 1.4em; }

/* result */
.result-frame {
  border-radius: 14px;
  overflow: hidden;
  margin: 6px auto 4px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.5);
  max-width: 420px;
}
#result-image { display: block; width: 100%; height: auto; }

/* footer */
.footer {
  margin-top: auto;
  padding-top: 22px;
  text-align: center;
  font-size: 0.74rem;
  color: var(--muted);
  opacity: 0.6;
}

/* larger screens */
@media (min-width: 680px) {
  .app { max-width: 600px; }
  .grid { grid-template-columns: repeat(3, 1fr); }
  .stage { max-width: 460px; margin: 0 auto; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* dimmed-but-tappable state for the generate button until a character is picked */
.btn-primary.is-idle { opacity: 0.62; }

/* on-screen toast — reliable feedback on mobile (unlike alert()) */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%) translateY(10px);
  z-index: 50;
  max-width: min(92vw, 520px);
  background: rgba(24, 18, 46, 0.97);
  color: var(--parch);
  border: 1px solid var(--gold);
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 0.95rem;
  text-align: center;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.55);
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
