@font-face { font-family: "Cairo"; src: url("../assets/fonts/Cairo-Regular.ttf") format("truetype"); font-weight: 400; font-display: swap; }
@font-face { font-family: "Cairo"; src: url("../assets/fonts/Cairo-SemiBold.ttf") format("truetype"); font-weight: 600; font-display: swap; }
@font-face { font-family: "Cairo"; src: url("../assets/fonts/Cairo-Bold.ttf") format("truetype"); font-weight: 700; font-display: swap; }

:root {
  --ink: #172a32;
  --ivory: #f7f2e8;
  --teal: #167c78;
  --coral: #f36149;
  --gold: #d7b655;
  --paper: #fffdf8;
  --muted: #5b6b72;
  --shadow: 0 18px 50px rgba(23, 42, 50, 0.22);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--ink); overflow: hidden; }
body {
  font-family: "Cairo", system-ui, sans-serif;
  font-weight: 400;
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  /* Panning stays possible in the overlays, the phone and the reader (their lists scroll by touch); the game canvas
     and the thumb controls take every touch themselves (touch-action: none on them). No pinch zoom. */
  touch-action: pan-x pan-y;
  user-select: none;
}
[hidden] { display: none !important; }

#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; outline: none; touch-action: none; }

.fade { position: fixed; inset: 0; background: #000; opacity: 0; pointer-events: none; transition: opacity 0.4s linear; z-index: 50; }

.noscript { position: fixed; inset: 0; display: grid; place-items: center; color: var(--ivory); padding: 24px; text-align: center; }

/* ---------- login ---------- */
.screen {
  position: fixed; inset: 0; z-index: 30; display: grid; place-items: stretch; padding: 16px; overflow-y: auto;
  background: radial-gradient(120% 90% at 50% 0%, rgba(22, 124, 120, 0.55), rgba(23, 42, 50, 0.92) 60%), var(--ink);
}
.card {
  width: min(92vw, 420px); background: var(--paper); border-radius: 22px; padding: 28px 24px 22px;
  box-shadow: var(--shadow); text-align: start;
}
.emblem { display: block; margin: 0 auto 6px; width: 96px; height: auto; background: #fff; border-radius: 50%; padding: 6px; box-shadow: 0 0 0 1px rgba(23,42,50,0.08); }
h1 { font-size: 1.6rem; font-weight: 700; margin: 6px 0 4px; text-align: center; letter-spacing: 0.2px; }
.lede { color: var(--muted); margin: 0 0 18px; text-align: center; font-size: 0.95rem; }
label, legend { display: block; font-weight: 600; margin-bottom: 6px; font-size: 0.95rem; }
input[type="text"] {
  width: 100%; font: inherit; font-size: 1.05rem; padding: 12px 14px; border-radius: 12px;
  border: 1.5px solid #cfd6d6; background: #fff; color: var(--ink);
}
input[type="text"]:focus { outline: 3px solid rgba(22, 124, 120, 0.35); border-color: var(--teal); }
.err { color: #b8372a; font-size: 0.9rem; min-height: 1.2em; margin: 6px 0 10px; }
.gender { border: 0; padding: 0; margin: 8px 0 0; }
.choice {
  display: inline-flex; align-items: center; gap: 8px; margin: 0 8px 8px 0; padding: 10px 16px; border-radius: 12px;
  border: 1.5px solid #cfd6d6; cursor: pointer; font-weight: 600; font-size: 1rem; margin-bottom: 0;
}
.choice input { accent-color: var(--teal); width: 18px; height: 18px; margin: 0; }
.choice:has(input:checked) { border-color: var(--teal); background: rgba(22, 124, 120, 0.08); }
.choice:focus-within { outline: 3px solid rgba(22, 124, 120, 0.3); }

button { font: inherit; cursor: pointer; border-radius: 14px; touch-action: manipulation; }
button:focus-visible { outline: 3px solid rgba(215, 182, 85, 0.85); outline-offset: 2px; }
.primary {
  display: block; width: 100%; margin-top: 10px; padding: 13px 16px; font-weight: 700; font-size: 1.05rem;
  color: #fff; background: var(--teal); border: 0; box-shadow: 0 6px 16px rgba(22, 124, 120, 0.3);
}
.primary:hover { background: #12716d; }
.ghost {
  display: inline-block; padding: 11px 16px; font-weight: 600; font-size: 0.98rem; color: var(--ink);
  background: transparent; border: 1.5px solid var(--ink); margin-top: 8px;
}
.link { display: block; margin: 12px auto 0; background: none; border: 0; color: var(--teal); font-weight: 600; text-decoration: underline; padding: 6px; }

/* ---------- HUD ---------- */
.hud { position: fixed; inset: 0; z-index: 20; pointer-events: none; }
.hud > * { pointer-events: auto; }
.objective {
  position: absolute; top: 14px; inset-inline-start: 14px; max-width: min(calc(100vw - 150px), 520px);
  background: rgba(23, 42, 50, 0.78); color: var(--ivory); padding: 10px 16px; border-radius: 14px;
  font-weight: 600; font-size: 1rem; line-height: 1.5; border-inline-start: 4px solid var(--gold);
}
.pause-btn {
  position: absolute; top: 14px; inset-inline-end: 14px; padding: 9px 14px; background: rgba(255,253,248,0.92);
  border: 0; color: var(--ink); font-weight: 600; box-shadow: 0 4px 12px rgba(0,0,0,0.12);
}
.prompt {
  position: absolute; bottom: 96px; left: 50%; transform: translateX(-50%); background: var(--paper); color: var(--ink);
  padding: 10px 18px; border-radius: 999px; font-weight: 600; box-shadow: var(--shadow); font-size: 1rem; white-space: nowrap;
}
.toast {
  position: absolute; top: 120px; left: 50%; transform: translateX(-50%); max-width: min(88vw, 520px); text-align: center;
  background: rgba(255, 253, 248, 0.96); color: var(--ink); padding: 12px 18px; border-radius: 14px; box-shadow: var(--shadow);
  font-weight: 600; line-height: 1.5; border-top: 4px solid var(--coral);
}
.hint {
  position: absolute; bottom: 12px; inset-inline: 0; text-align: center; color: rgba(247,242,232,0.85); font-size: 0.85rem;
  text-shadow: 0 1px 3px rgba(0,0,0,0.6); pointer-events: none;
}
.skip {
  position: absolute; bottom: 18px; inset-inline-end: 18px; padding: 9px 16px; border-radius: 12px; border: 1.5px solid var(--ivory);
  background: rgba(23,42,50,0.6); color: var(--ivory); font-weight: 600;
}
.touch { position: absolute; inset: auto 0 0 0; height: 220px; pointer-events: none; }
body.touch-mode .hint { display: none; }
/* Keyboard names are shown only with a keyboard; touch players see touch wording instead. */
body.touch-mode .key-hint { display: none; }
.touch-only { display: none; }
body.touch-mode .touch-only { display: inline; }
/* On touch the phone button sits above the joystick ring instead of on top of it. */
body.touch-mode .phone-btn { left: 24px; bottom: 176px; }
/* In the car on touch the speed box moves to the top right, so it does not sit over the joystick or the action button. */
body.touch-mode .car-hud {
  top: 204px; right: 16px; bottom: auto; left: auto; transform: none; flex-direction: column; align-items: center;
  gap: 4px; padding: 8px 14px; text-align: center; max-width: 150px;
}
.joy {
  position: absolute; bottom: 24px; left: 24px; width: 132px; height: 132px; border-radius: 50%;
  background: rgba(23, 42, 50, 0.22); border: 2px solid rgba(247,242,232,0.6); pointer-events: auto; touch-action: none;
}
.knob {
  position: absolute; width: 56px; height: 56px; border-radius: 50%; background: rgba(247,242,232,0.85);
  left: 38px; top: 38px; box-shadow: 0 4px 10px rgba(0,0,0,0.25);
}
.act {
  position: absolute; bottom: 42px; right: 24px; width: 96px; height: 96px; border-radius: 50%; border: 0;
  background: #c2432d; color: #fff; font-weight: 700; font-size: 1rem; box-shadow: 0 8px 20px rgba(194,67,45,0.4); pointer-events: auto;
}

/* Car pedals for touch: a small pad above the action button. Shown only in the car on a touch device (ui.showCar). */
.car-pedals {
  position: absolute; right: 24px; bottom: 150px; display: grid; grid-template-columns: 64px 64px; gap: 8px;
  pointer-events: auto; touch-action: none; direction: ltr;
}
.car-pedals[hidden] { display: none; }
.pedal {
  height: 56px; border-radius: 14px; border: 1.5px solid rgba(247,242,232,0.7); background: rgba(23,42,50,0.55);
  color: var(--ivory); font-weight: 700; font-size: 0.8rem; touch-action: none; user-select: none; direction: rtl;
}
.pedal.wide { grid-column: span 2; }
.pedal.held { background: rgba(22,124,120,0.92); border-color: #F7F2E8; }

/* ---------- overlays ---------- */
/* Children are centred with auto margins, so tall content scrolls from the top instead of being clipped. */
.overlay > *, .screen > .card { margin: auto; }
.overlay {
  position: fixed; inset: 0; z-index: 40; display: grid; place-items: stretch; padding: 14px; overflow-y: auto;
  background: rgba(23, 42, 50, 0.6); backdrop-filter: blur(3px);
}
.panel {
  width: min(94vw, 560px); max-height: 94vh; overflow: auto; background: var(--paper); border-radius: 20px; padding: 22px;
  box-shadow: var(--shadow); text-align: start;
}
.panel h2 { margin: 0 0 10px; font-size: 1.3rem; font-weight: 700; }
.status { margin: 0; color: var(--muted); font-size: 0.95rem; }
.msg { min-height: 1.5em; margin: 10px 0 0; font-weight: 600; color: var(--ink); text-align: center; }
.msg.bad { color: #b8372a; }
.msg.good { color: var(--teal); }
.row { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.row .primary, .row .ghost { display: inline-block; width: auto; margin-top: 0; }

/* maze */
.maze-panel { width: min(96vw, 640px); max-height: calc(100vh - 24px); overflow-y: auto; }
#mazeCanvas { width: min(100%, calc(100vh - 300px)); height: auto; display: block; margin: 8px auto; border-radius: 12px; background: var(--ivory); }
.maze-controls { display: flex; flex-wrap: wrap; gap: 14px; align-items: center; justify-content: center; }
.dpad { display: grid; grid-template-columns: repeat(3, 56px); grid-template-rows: repeat(3, 56px); gap: 6px; }
.dpad button {
  width: 56px; height: 56px; font-size: 1.2rem; border: 0; background: var(--ink); color: var(--ivory);
}
.dpad span { display: block; }

/* keypad */
.keypad-panel { width: min(92vw, 380px); text-align: center; }
.safe-hint { display: inline-block; background: var(--ivory); color: var(--ink); font-weight: 700; padding: 6px 14px; border-radius: 999px; border: 1px solid var(--gold); margin: 0 0 12px; }
.safe-input {
  width: 100%; text-align: center; font: 700 2rem "Cairo", monospace; letter-spacing: 0.4em; padding: 10px 12px;
  border-radius: 14px; border: 2px solid var(--ink); background: #fff; color: var(--ink); direction: ltr;
}
.pad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 14px; }
.pad button {
  padding: 14px 0; font-size: 1.3rem; font-weight: 700; background: var(--ink); color: var(--ivory); border: 0;
}
.pad button.ghost { background: transparent; color: var(--ink); border: 1.5px solid var(--ink); font-size: 1rem; }
.pad button.primary { margin: 0; font-size: 1rem; }
.keypad-panel .ghost { margin-top: 12px; }
.shake { animation: shake 0.36s; }
@keyframes shake { 20%,60% { transform: translateX(-8px);} 40%,80% { transform: translateX(8px);} }

/* invitation */
/* The card is an image drawn from the same model as the PNG export (js/invitation.js). */
.invite-wrap { width: min(94vw, 480px); display: grid; gap: 12px; justify-items: center; }
.invite-figure { margin: 0; width: 100%; display: flex; justify-content: center; }
/* The image is sized so the whole overlay (card, details, buttons) fits the screen without page scrolling. */
.invite-figure img {
  display: block; width: min(100%, calc((100dvh - 290px) * 0.675)); height: auto; aspect-ratio: 1080 / 1600;
  border-radius: 18px; box-shadow: var(--shadow); background: var(--ivory);
}
.invite-details { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; width: 100%; margin: 0; padding: 0; font-size: 0.95rem; color: var(--ivory); }
.invite-details div { display: contents; }
.invite-details dt { color: rgba(247, 242, 232, 0.72); text-align: start; }
.invite-details dd { margin: 0; font-weight: 700; text-align: start; overflow-wrap: anywhere; }
.invite-actions .ghost { color: var(--ivory); border-color: var(--ivory); }
.invite-actions { margin-top: 2px; }
.invite-actions .primary { background: #c2432d; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

@media (max-height: 560px) {
  .invite-figure img { width: min(100%, calc((100dvh - 230px) * 0.675)); }
}
@media (prefers-reduced-motion: reduce) {
  .fade, .shake { transition: none; animation: none; }
}

/* The study screen at a desk (js/rest.js): a sheet over the game, closed by «رجوع» or Escape. */
.study-panel { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(12, 22, 28, 0.55); z-index: 40; padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom)); }
.study-panel[hidden] { display: none; }
.study-card { direction: rtl; font-family: Cairo, "Noto Naskh Arabic", sans-serif; background: #FFFDF8; color: #172A32; border-radius: 18px; padding: 18px 20px; width: min(520px, 100%); max-height: 100%; overflow: auto; box-shadow: 0 12px 40px rgba(0,0,0,0.3); }
.study-card h2 { margin: 0 0 8px; font-size: 20px; }
.study-card ol { margin: 0 0 8px; padding-inline-start: 22px; line-height: 1.8; font-size: 16px; }
.study-note { margin: 0 0 12px; font-size: 13px; color: #5A6870; }
.study-follow { display: flex; gap: 14px; align-items: center; background: #F2F7F4; border-radius: 14px; padding: 10px; }
.study-follow img { width: 148px; height: 148px; background: #fff; padding: 6px; border-radius: 8px; image-rendering: pixelated; }
.study-follow div { display: flex; flex-direction: column; gap: 6px; }
.study-follow span { font-size: 14px; color: #2E4A6E; }
.study-card button { min-height: 44px; min-width: 44px; border-radius: 12px; border: 0; font: inherit; font-weight: 700; padding: 8px 14px; cursor: pointer; }
.study-follow button { background: #1F5A45; color: #fff; }
.study-close { margin-top: 12px; width: 100%; background: #E9E3CF; color: #172A32; }
