:root {
  --gold: #d4af37;
  --gold-soft: #c8a951;
  --parchment: #f3e6c4;
  --ink: #1d1208;
  --shadow: rgba(0,0,0,0.55);
  --gryffindor: #7f0909;
  --slytherin: #1a472a;
  --ravenclaw: #0e1a40;
  --hufflepuff: #ecb939;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: "Georgia", "Times New Roman", serif;
  color: var(--parchment);
  background: #0b0a08;
  overflow: hidden;
}

.bg {
  position: fixed; inset: 0;
  z-index: 0;
  background-color: #0b0a08;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  background-image: linear-gradient(135deg, #2a1d10 0%, #0b0a08 100%);
  transition: background-image 600ms ease;
}
.bg-overlay {
  position: fixed; inset: 0;
  z-index: 1;
  pointer-events: none;
  background:
    linear-gradient(180deg, rgba(0,0,0,0.55) 0%, rgba(0,0,0,0.35) 35%, rgba(0,0,0,0.7) 100%),
    radial-gradient(ellipse at center, rgba(0,0,0,0.15) 0%, rgba(0,0,0,0.55) 100%);
}

#app {
  position: relative;
  z-index: 2;
  width: 100%;
  height: 100vh;
  height: 100dvh; /* dynamic vh for iOS Safari toolbar */
  overflow: auto;
  padding: 24px;
  padding-top: max(24px, env(safe-area-inset-top));
  padding-bottom: max(24px, env(safe-area-inset-bottom));
  padding-left: max(24px, env(safe-area-inset-left));
  padding-right: max(24px, env(safe-area-inset-right));
  -webkit-tap-highlight-color: transparent;
}

.screen {
  display: none;
  max-width: 1100px;
  margin: 0 auto;
  animation: fade 240ms ease;
}
.screen.active { display: block; }

@keyframes fade { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.title {
  font-size: 56px;
  text-align: center;
  margin: 24px 0 4px;
  color: var(--gold);
  text-shadow: 0 2px 18px rgba(0,0,0,0.8);
  letter-spacing: 0.5px;
}
.subtitle {
  text-align: center;
  margin: 0 0 28px;
  color: #e8d8a8;
  font-style: italic;
}

.card {
  background: rgba(18, 13, 8, 0.82);
  border: 1px solid var(--gold-soft);
  border-radius: 14px;
  padding: 28px;
  box-shadow: 0 18px 40px var(--shadow);
  backdrop-filter: blur(2px);
}
.card.center { text-align: center; }

.label {
  display: block;
  margin: 14px 0 6px;
  font-size: 14px;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--gold-soft);
}
.input {
  width: 100%;
  padding: 12px 14px;
  font-size: 18px;
  font-family: inherit;
  background: rgba(243, 230, 196, 0.08);
  color: var(--parchment);
  border: 1px solid var(--gold-soft);
  border-radius: 8px;
  outline: none;
}
.input:focus { border-color: var(--gold); }

.house-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-bottom: 12px;
}
.house-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 18px 8px;
  background: rgba(0,0,0,0.4);
  border: 2px solid var(--gold-soft);
  color: var(--parchment);
  border-radius: 10px;
  font-family: inherit;
  font-size: 16px;
  cursor: pointer;
  transition: transform 120ms ease, border-color 120ms ease, background 120ms ease;
}
.house-btn:hover { transform: translateY(-2px); border-color: var(--gold); }
.house-btn.selected {
  border-color: var(--gold);
  background: rgba(212,175,55,0.18);
  box-shadow: 0 0 0 3px rgba(212,175,55,0.25) inset;
}
.house-crest {
  font-size: 42px;
  line-height: 1;
  display: inline-flex;
  width: 64px; height: 64px;
  align-items: center; justify-content: center;
  border-radius: 50%;
  border: 2px solid currentColor;
}
.house-crest.gryffindor { background: var(--gryffindor); color: var(--gold); }
.house-crest.slytherin  { background: var(--slytherin);  color: #b6c8b1; }
.house-crest.ravenclaw  { background: var(--ravenclaw);  color: #c5a572; }
.house-crest.hufflepuff { background: var(--hufflepuff); color: #2a2208; }
.house-name { font-weight: bold; letter-spacing: 0.5px; }

.actions {
  margin-top: 20px;
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  flex-wrap: wrap;
}
.actions.center { justify-content: center; }

button.primary, button.ghost {
  font-family: inherit;
  font-size: 16px;
  padding: 12px 22px;
  border-radius: 8px;
  cursor: pointer;
  transition: transform 100ms ease, background 120ms ease;
}
button.primary {
  background: linear-gradient(180deg, #e0bf52 0%, #b9912f 100%);
  color: #1a1108;
  border: 1px solid var(--gold);
  font-weight: bold;
}
button.primary:hover:not(:disabled) { transform: translateY(-1px); }
button.primary:disabled {
  opacity: 0.45; cursor: not-allowed;
  background: #555;
  border-color: #666;
  color: #222;
}
button.ghost {
  background: transparent;
  color: var(--parchment);
  border: 1px solid var(--gold-soft);
}
button.ghost:hover { background: rgba(212,175,55,0.12); }

button.hint-btn {
  font-family: inherit;
  font-size: 15px;
  padding: 10px 18px;
  border-radius: 8px;
  cursor: pointer;
  transition: transform 100ms ease, background 120ms ease;
  background: rgba(90, 60, 140, 0.25);
  color: var(--parchment);
  border: 1px dashed rgba(180, 140, 220, 0.55);
}
button.hint-btn:hover:not(:disabled) { background: rgba(90, 60, 140, 0.42); }
button.hint-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* HUD ---------------------------------------------------------- */
.hud {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  align-items: center;
  background: rgba(11,10,8,0.85);
  border: 1px solid var(--gold-soft);
  border-radius: 10px;
  padding: 10px 16px;
  margin-bottom: 18px;
  font-size: 15px;
  gap: 12px;
}
.hud-center { text-align: center; }
.hud-right  { text-align: right; }
.hud-name   { font-weight: bold; color: var(--gold); }
.hud-house  { margin-left: 8px; opacity: 0.85; font-style: italic; }
.hud-location { font-weight: bold; color: var(--gold); display: block; }
.hud-progress { font-size: 12px; opacity: 0.85; }
.hud-lives  { color: #ff7a7a; margin-right: 12px; }
.hud-score  { color: var(--gold); font-weight: bold; }

/* Stage --------------------------------------------------------- */
.stage {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 24px;
  align-items: start;
}
.mcgonagall {
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  position: sticky; top: 0;
}
.mcgonagall img {
  width: 220px;
  height: 280px;
  object-fit: cover;
  border-radius: 12px;
  border: 2px solid var(--gold-soft);
  box-shadow: 0 8px 28px var(--shadow);
  background: #1a140a;
}
.mcg-name {
  font-size: 13px;
  color: var(--gold);
  letter-spacing: 0.6px;
}

.dialogue {
  background: rgba(11,10,8,0.85);
  border: 1px solid var(--gold-soft);
  border-radius: 12px;
  padding: 22px;
  min-height: 360px;
  position: relative;
}
.preamble {
  font-style: italic;
  color: var(--gold-soft);
  margin-bottom: 12px;
  font-size: 15px;
}
.question-label {
  display: inline-block;
  margin-bottom: 8px;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: bold;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: #1a1108;
  background: var(--gold);
  border-radius: 4px;
}
.question-text {
  font-size: 22px;
  color: var(--parchment);
  margin-bottom: 18px;
  line-height: 1.4;
}
.answer-context {
  display: none;
  margin-top: 12px;
  padding: 12px 14px;
  background: rgba(108, 220, 122, 0.10);
  border-left: 3px solid #6cdc7a;
  border-radius: 6px;
  font-size: 16px;
  line-height: 1.5;
  color: #e8f5d8;
}
.answer-context.shown { display: block; }
.answer-context .ctx-label {
  display: block;
  font-size: 11px;
  letter-spacing: 0.8px;
  text-transform: uppercase;
  color: #6cdc7a;
  margin-bottom: 4px;
  font-weight: bold;
}
.hint-line {
  background: rgba(212,175,55,0.1);
  border-left: 3px solid var(--gold);
  padding: 8px 12px;
  margin-bottom: 14px;
  font-style: italic;
  color: #f0e3b8;
  display: none;
}
.hint-line.shown { display: block; }

.choices {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 14px;
}
.choice {
  text-align: left;
  padding: 14px 16px;
  background: rgba(0,0,0,0.45);
  color: var(--parchment);
  border: 1px solid var(--gold-soft);
  border-radius: 8px;
  font-family: inherit;
  font-size: 16px;
  cursor: pointer;
  transition: background 120ms ease, transform 80ms ease;
}
.choice:hover:not(:disabled) { background: rgba(212,175,55,0.18); }
.choice:disabled { cursor: default; opacity: 0.85; }
.choice .letter {
  display: inline-block;
  width: 24px; height: 24px;
  text-align: center;
  line-height: 24px;
  background: var(--gold);
  color: #1a1108;
  border-radius: 50%;
  margin-right: 10px;
  font-weight: bold;
  font-size: 13px;
}
.choice.correct {
  background: rgba(60,140,70,0.45);
  border-color: #6cdc7a;
}
.choice.incorrect {
  background: rgba(160,40,40,0.5);
  border-color: #ff6b6b;
}

.reaction {
  margin-top: 8px;
  padding: 12px 14px;
  border-radius: 8px;
  font-size: 17px;
  display: none;
}
.reaction.shown { display: block; }
.reaction.correct {
  background: rgba(60,140,70,0.18);
  border-left: 3px solid #6cdc7a;
}
.reaction.incorrect {
  background: rgba(160,40,40,0.18);
  border-left: 3px solid #ff6b6b;
}

.dialogue-actions {
  display: flex;
  justify-content: space-between;
  margin-top: 14px;
  gap: 10px;
}

.hidden { display: none !important; }

/* Transition / End screens ------------------------------------- */
#screen-transition .card,
#screen-end .card {
  margin-top: 80px;
}
.transition-meta {
  margin: 14px 0;
  color: var(--gold-soft);
  font-style: italic;
}
.end-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin: 18px 0;
}
.end-stats > div {
  background: rgba(212,175,55,0.08);
  border: 1px solid var(--gold-soft);
  border-radius: 8px;
  padding: 14px 8px;
}
.stat-label {
  display: block;
  font-size: 12px;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--gold-soft);
  margin-bottom: 4px;
}
.stat-value {
  display: block;
  font-size: 26px;
  color: var(--gold);
  font-weight: bold;
}

/* Leaderboard -------------------------------------------------- */
.lb-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 12px;
}
.lb-table th, .lb-table td {
  padding: 10px 8px;
  border-bottom: 1px solid rgba(212,175,55,0.25);
  text-align: left;
}
.lb-table th {
  font-size: 13px;
  letter-spacing: 0.5px;
  color: var(--gold-soft);
  text-transform: uppercase;
}
.lb-table tr:hover td { background: rgba(212,175,55,0.06); }
.lb-house-Gryffindor { color: #f5a3a3; }
.lb-house-Slytherin  { color: #9bc7a7; }
.lb-house-Ravenclaw  { color: #a4b6e8; }
.lb-house-Hufflepuff { color: #f3d97a; }

/* Level reward -------------------------------------------------- */
.transition-reward {
  margin: 20px 0 8px;
  padding: 18px 20px;
  background: rgba(212,175,55,0.09);
  border: 1px solid var(--gold);
  border-radius: 10px;
  text-align: center;
}
.reward-header {
  font-size: 12px;
  letter-spacing: 0.8px;
  text-transform: uppercase;
  color: var(--gold-soft);
  margin-bottom: 10px;
}
.reward-emoji { font-size: 52px; line-height: 1; margin-bottom: 10px; }
.reward-name  { font-size: 20px; font-weight: bold; color: var(--gold); margin-bottom: 8px; }
.reward-desc  { font-size: 15px; color: var(--parchment); opacity: 0.85; font-style: italic; }

/* Invite button ------------------------------------------------- */
.btn-invite { width: 100%; margin-top: 4px; }
body:not(.mobile-ua) .btn-invite { display: none !important; }

/* Loader -------------------------------------------------------- */
.loader {
  position: fixed; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.65);
  z-index: 100;
  gap: 14px;
  color: var(--gold);
  font-size: 14px;
  letter-spacing: 0.5px;
}
.spinner {
  width: 48px; height: 48px;
  border: 4px solid rgba(212,175,55,0.25);
  border-top-color: var(--gold);
  border-radius: 50%;
  animation: spin 900ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Mobile-UA — phones get no McGonagall sprite at all -------- */
body.mobile-ua .mcgonagall { display: none; }
body.mobile-ua .stage { grid-template-columns: 1fr; }
#screen-home #btn-show-leaderboard { order: -1; margin-right: auto; }

/* Home screen: Begin Journey full-width, secondary buttons side-by-side */
body.mobile-ua #screen-home .actions { flex-wrap: wrap; justify-content: flex-start; }
body.mobile-ua #screen-home #btn-begin { flex: 0 0 100%; }
body.mobile-ua #screen-home #btn-show-leaderboard { order: 0; margin-right: 0; flex: 1; }
body.mobile-ua #btn-invite-home { flex: 1; width: auto; margin-top: 0; }
body.mobile-ua #screen-home #btn-show-leaderboard,
body.mobile-ua #btn-invite-home {
  font-size: 14px;
  padding: 10px 6px;
  text-align: center;
  opacity: 0.7;
  background: rgba(212,175,55,0.06);
  border: 1px solid rgba(212,175,55,0.2);
  color: var(--parchment);
}

/* Leaderboard invite: subtle text link */
body.mobile-ua #btn-invite-lb {
  background: none;
  border: none;
  text-decoration: underline;
  opacity: 0.6;
  font-size: 14px;
  padding: 8px 0;
  width: 100%;
}

body.mobile-ua #btn-next,
body.mobile-ua #btn-hint,
body.mobile-ua #btn-back-home { width: 100%; }

/* Mobile ------------------------------------------------------- */
@media (max-width: 760px) {
  .stage { grid-template-columns: 1fr; }
  .mcgonagall { flex-direction: row; position: static; }
  .mcgonagall img { width: 100px; height: 130px; }
  .choices { grid-template-columns: 1fr; }
  .house-grid { grid-template-columns: repeat(2, 1fr); }
  .end-stats { grid-template-columns: 1fr; }
  .title { font-size: 36px; }
  .hud { grid-template-columns: 1fr; text-align: center; }
  .hud-right, .hud-center { text-align: center; }
}
