:root {
  --ink: #10202c;
  --ink-soft: rgba(16, 32, 44, 0.62);
  --paper: rgba(255, 253, 248, 0.82);
  --paper-strong: rgba(255, 253, 248, 0.94);
  --line: rgba(28, 48, 64, 0.16);
  --accent: #c9562c;
  --gold: #b8862f;
  --shadow: 0 10px 34px rgba(9, 26, 38, 0.22), 0 2px 6px rgba(9, 26, 38, 0.12);
  --font: "Inter", "SF Pro Text", -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: #0d1a24;
  font-family: var(--font);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  touch-action: none;
}

#app { position: fixed; inset: 0; }

canvas#scene {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

.hidden { opacity: 0 !important; pointer-events: none; }

/* ---------------- Loading ---------------- */

.loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background:
    radial-gradient(120% 90% at 50% 10%, #eaf2f8 0%, #cddff0 45%, #9fb8cf 100%);
  z-index: 40;
  transition: opacity 900ms ease, visibility 900ms;
}
/* Stop catching input the moment the fade starts, not when it ends. */
.loading.gone { opacity: 0; visibility: hidden; pointer-events: none; }
.loading.failed .loading-note { color: #8c2f14; letter-spacing: 0.04em; }
.loading.failed .bar { visibility: hidden; }

.loading-inner { text-align: center; width: min(420px, 78vw); }
.brand {
  font-size: clamp(28px, 5.2vw, 46px);
  font-weight: 700;
  letter-spacing: 0.26em;
  color: #16303f;
  text-indent: 0.26em;
}
.sub {
  margin-top: 8px;
  font-size: 12px;
  letter-spacing: 0.34em;
  text-transform: uppercase;
  color: rgba(22, 48, 63, 0.58);
}
.bar {
  margin: 26px auto 0;
  height: 3px;
  width: 100%;
  background: rgba(22, 48, 63, 0.16);
  border-radius: 3px;
  overflow: hidden;
}
.bar i {
  display: block;
  height: 100%;
  width: 4%;
  background: linear-gradient(90deg, #2c6b8f, #c9562c);
  transition: width 380ms ease;
}
.loading-note {
  margin-top: 12px;
  font-size: 12px;
  letter-spacing: 0.12em;
  color: rgba(22, 48, 63, 0.5);
  min-height: 16px;
}

/* ---------------- HUD ---------------- */

.hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 20;
  opacity: 1;
  transition: opacity 700ms ease;
}

.objective {
  position: absolute;
  top: max(18px, env(safe-area-inset-top));
  left: max(18px, env(safe-area-inset-left));
  min-width: 246px;
  max-width: min(360px, 62vw);
  padding: 13px 15px 12px;
  background: var(--paper);
  backdrop-filter: blur(9px) saturate(1.15);
  -webkit-backdrop-filter: blur(9px) saturate(1.15);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.obj-label {
  font-size: 9.5px;
  letter-spacing: 0.26em;
  text-transform: uppercase;
  color: var(--accent);
  font-weight: 700;
}
.obj-title {
  margin-top: 5px;
  font-size: 15px;
  font-weight: 600;
  line-height: 1.25;
  letter-spacing: -0.01em;
}
.obj-meta {
  margin-top: 6px;
  font-size: 12px;
  color: var(--ink-soft);
  display: flex;
  gap: 7px;
  align-items: baseline;
  flex-wrap: wrap;
}
.obj-meta .dot { opacity: 0.45; }
.obj-bar {
  margin-top: 10px;
  height: 3px;
  border-radius: 3px;
  background: rgba(28, 48, 64, 0.13);
  overflow: hidden;
}
.obj-bar i {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #2f7fa8, var(--accent));
  transition: width 260ms ease;
}

.compass {
  position: absolute;
  top: max(18px, env(safe-area-inset-top));
  right: max(18px, env(safe-area-inset-right));
  width: 74px;
  text-align: center;
  transition: opacity 400ms ease;
}
.compass-ring {
  position: relative;
  width: 54px;
  height: 54px;
  margin: 0 auto;
  border-radius: 50%;
  background: var(--paper);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  backdrop-filter: blur(9px);
  -webkit-backdrop-filter: blur(9px);
}
.compass-arrow {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  transition: transform 120ms linear;
}
.compass-arrow svg { width: 20px; height: 20px; fill: var(--accent); }
.compass-label {
  margin-top: 6px;
  font-size: 9.5px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: rgba(255, 253, 248, 0.92);
  text-shadow: 0 1px 6px rgba(9, 26, 38, 0.6);
  font-weight: 600;
}

.prompt {
  position: absolute;
  left: 50%;
  bottom: max(74px, calc(env(safe-area-inset-bottom) + 60px));
  transform: translate(-50%, 14px);
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 9px 18px 9px 12px;
  min-height: 42px;
  font: 500 13.5px/1 var(--font);
  color: var(--ink);
  background: var(--paper-strong);
  border: 1px solid var(--line);
  border-radius: 999px;
  box-shadow: var(--shadow);
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: opacity 260ms ease, transform 260ms ease, background 160ms ease;
}
.prompt.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.prompt:hover { background: #fff; }
.prompt:active { transform: translate(-50%, 1px); }

button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

kbd {
  display: inline-grid;
  place-items: center;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  font: 600 11px/1 var(--font);
  color: #fdfbf6;
  background: #23384a;
  border-radius: 6px;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.28);
}

.toast {
  position: absolute;
  left: 50%;
  bottom: max(132px, calc(env(safe-area-inset-bottom) + 118px));
  transform: translate(-50%, 10px);
  max-width: min(520px, 84vw);
  padding: 11px 17px;
  background: var(--paper-strong);
  border: 1px solid var(--line);
  border-left: 3px solid var(--gold);
  border-radius: 10px;
  box-shadow: var(--shadow);
  font-size: 13px;
  line-height: 1.45;
  color: #1b2c38;
  opacity: 0;
  transition: opacity 320ms ease, transform 320ms ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast strong { font-weight: 650; }
.toast em { font-style: normal; color: var(--ink-soft); }

.controls {
  position: absolute;
  left: max(18px, env(safe-area-inset-left));
  bottom: max(18px, env(safe-area-inset-bottom));
  padding: 12px 14px;
  background: var(--paper);
  backdrop-filter: blur(9px);
  -webkit-backdrop-filter: blur(9px);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  font-size: 11.5px;
  color: var(--ink-soft);
  transition: opacity 900ms ease, transform 900ms ease;
  min-width: 172px;
}
.controls.fade { opacity: 0; transform: translateY(6px); }
.controls-title {
  font-size: 10px;
  letter-spacing: 0.24em;
  text-transform: uppercase;
  font-weight: 700;
  color: var(--ink);
  margin-bottom: 8px;
}
.controls .row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 5px;
}
.controls .keys { display: inline-flex; gap: 3px; }
.controls .keys + span + .keys { margin-left: 10px; }

/* The cinematic reveal clears the screen for the sunset. */
.objective { transition: opacity 700ms ease; }
.hud.cinematic > * { opacity: 0 !important; pointer-events: none !important; }

/* Desktop and touch each see only their own hints. */
.touch-only { display: none; }
body.touch-mode .touch-only { display: block; }
body.touch-mode .desk-only { display: none; }

/* ---------------- Title card ---------------- */

.titlecard {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: end center;
  padding-bottom: max(26px, env(safe-area-inset-bottom));
  opacity: 0;
  visibility: hidden;
  transition: opacity 900ms ease, visibility 900ms;
  background: linear-gradient(to top, rgba(8, 18, 26, 0.5) 0%, rgba(8, 18, 26, 0.16) 26%, rgba(8, 18, 26, 0) 52%);
  pointer-events: none;
}
.titlecard.show { opacity: 1; visibility: visible; pointer-events: auto; }

.tc-inner {
  width: min(760px, 92vw);
  padding: 18px 26px 18px;
  text-align: left;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "kicker button" "title button" "body stats" "hint stats";
  gap: 2px 24px;
  align-items: center;
  background: rgba(255, 253, 248, 0.9);
  backdrop-filter: blur(14px) saturate(1.1);
  -webkit-backdrop-filter: blur(14px) saturate(1.1);
  border: 1px solid rgba(28, 48, 64, 0.14);
  border-radius: 14px;
  box-shadow: 0 22px 60px rgba(6, 20, 30, 0.42);
  transform: translateY(18px);
  transition: transform 900ms cubic-bezier(0.16, 1, 0.3, 1);
}
.titlecard.show .tc-inner { transform: translateY(0); }

.tc-kicker {
  grid-area: kicker;
  font-size: 9.5px;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  color: var(--gold);
  font-weight: 700;
}
.tc-title {
  grid-area: title;
  margin-top: 4px;
  font-size: clamp(22px, 3.4vw, 31px);
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.1;
}
.tc-body {
  grid-area: body;
  margin: 7px 0 0;
  max-width: 46ch;
  font-size: 13px;
  line-height: 1.55;
  color: var(--ink-soft);
}
.tc-stats {
  grid-area: stats;
  display: flex;
  gap: 22px;
  flex-wrap: wrap;
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-soft);
  text-align: right;
  justify-content: flex-end;
}
.tc-stats b {
  display: block;
  font-size: 17px;
  letter-spacing: -0.01em;
  text-transform: none;
  color: var(--ink);
  font-weight: 650;
}
#restart-btn {
  grid-area: button;
  padding: 11px 22px;
  font: 600 13px/1 var(--font);
  letter-spacing: 0.04em;
  color: #fdfbf6;
  background: #23384a;
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(9, 26, 38, 0.3);
  transition: transform 180ms ease, background 180ms ease;
  white-space: nowrap;
}
#restart-btn:hover { background: #c9562c; transform: translateY(-1px); }
.tc-hint { grid-area: hint; margin-top: 6px; font-size: 11px; color: var(--ink-soft); }

/* ---------------- Touch ---------------- */

.touch { position: absolute; inset: 0; z-index: 25; pointer-events: none; }
/* At rest the stick waits bottom-left; a touch anywhere on the left part of
   the screen moves it under the thumb (.active, positioned by input.js). */
.stick {
  position: absolute;
  left: max(22px, env(safe-area-inset-left));
  bottom: max(26px, env(safe-area-inset-bottom));
  width: 118px;
  height: 118px;
  border-radius: 50%;
  background: rgba(255, 253, 248, 0.16);
  border: 1px solid rgba(255, 255, 255, 0.3);
  pointer-events: auto;
  touch-action: none;
  transition: opacity 200ms ease;
}
/* Hidden on desktop; the child must not keep catching the mouse. */
.touch.hidden .stick { pointer-events: none; }
.stick.active {
  bottom: auto;
  transform: translate(-50%, -50%);
  background: rgba(255, 253, 248, 0.22);
}
.stick i {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 46px;
  height: 46px;
  margin: -23px 0 0 -23px;
  border-radius: 50%;
  background: rgba(255, 253, 248, 0.62);
  box-shadow: 0 4px 14px rgba(9, 26, 38, 0.3);
}

/* Touch: keep the prompt and toast clear of the stick, and the help card
   bottom-right where the thumbs are not. */
body.touch-mode .prompt { bottom: max(176px, calc(env(safe-area-inset-bottom) + 160px)); min-height: 48px; padding: 12px 22px; }
body.touch-mode .toast { bottom: max(236px, calc(env(safe-area-inset-bottom) + 220px)); }
body.touch-mode .controls {
  left: auto;
  right: max(18px, env(safe-area-inset-right));
  min-width: 0;
}

@media (max-width: 640px) {
  .objective { min-width: 0; max-width: calc(100vw - 110px); padding: 10px 12px 10px; }
  .obj-title { font-size: 13.5px; }
  body:not(.touch-mode) .controls { display: none; }
  .compass { width: 66px; }
  .compass-ring { width: 44px; height: 44px; }
  .toast { font-size: 12.5px; max-width: 90vw; }
  .tc-inner {
    grid-template-columns: 1fr;
    grid-template-areas: "kicker" "title" "body" "stats" "button" "hint";
    gap: 4px;
    padding: 16px 18px;
  }
  .tc-stats { justify-content: flex-start; text-align: left; margin-top: 8px; }
  #restart-btn { margin-top: 10px; justify-self: start; min-height: 44px; }
}

/* Phones held sideways: little height to spare. */
@media (max-height: 480px) {
  .objective { padding: 8px 11px; }
  .obj-bar { margin-top: 7px; }
  body.touch-mode .prompt { bottom: max(18px, env(safe-area-inset-bottom)); }
  body.touch-mode .toast { bottom: max(76px, calc(env(safe-area-inset-bottom) + 60px)); max-width: 52vw; }
  body.touch-mode .controls { top: 86px; bottom: auto; }
  .tc-body { display: none; }
}

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