:root {
  /* SIGNAL // GRID — machined near-black mass, Bone text, one amber signal. */
  --void: #05070C;
  --deep: #0D1117;
  --panel: rgba(17, 21, 28, 0.92);
  --panel-strong: rgba(5, 7, 12, 0.96);
  --cyan: #5BD8E8;
  --violet: #6B53E8;
  --gold: #FFB000;
  --cream: #E8E5DC;
  --muted: #8A919C;
  --glass: rgba(232, 229, 220, 0.06);
  --font-display: 'Marcellus', 'Palatino Linotype', serif;
  --font-ui: 'IBM Plex Mono', 'Courier New', monospace;
  --font-body: 'Inter', system-ui, -apple-system, sans-serif;
  --glow-cyan: 0 0 24px rgba(91, 216, 232, 0.10);
  --glow-violet: 0 0 24px rgba(107, 83, 232, 0.10);
  --glow-gold: 0 0 24px rgba(255, 176, 0, 0.10);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(ellipse at 50% -10%, rgba(91, 216, 232, 0.04), transparent 42%),
    radial-gradient(ellipse at 50% 115%, rgba(255, 176, 0, 0.03), transparent 40%),
    linear-gradient(178deg, #070A10 0%, #05070C 60%, #03040A 100%);
  color: var(--cream);
  font-family: var(--font-body);
  overflow-x: hidden;
  touch-action: manipulation;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: clamp(6px, 1.5vw, 18px);
}

/* Subtle grain over everything */
body::after {
  content: "";
  position: fixed;
  inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='200' height='200' filter='url(%23n)' opacity='0.04'/%3E%3C/svg%3E");
  pointer-events: none;
  z-index: 999;
  opacity: 0.42;
}

button {
  font: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.game-shell {
  width: min(100%, 1380px);
  display: grid;
  justify-items: center;
  gap: 10px;
}

/* ── Screen wrap: hairline instrument frame with corner brackets ── */
.screen-wrap {
  position: relative;
  width: min(100%, 1280px);
  aspect-ratio: 16 / 9;
  padding: clamp(6px, 1.2vw, 14px);
  background: #07090E;
  border: 1px solid rgba(232, 229, 220, 0.14);
  border-radius: 6px;
  box-shadow:
    0 32px 100px rgba(0, 0, 0, 0.72),
    inset 0 0 0 1px rgba(5, 7, 12, 0.9);
}

/* Calibration corner brackets */
.screen-wrap::before,
.screen-wrap::after {
  content: "";
  position: absolute;
  width: 14px;
  height: 14px;
  pointer-events: none;
  border: 0 solid rgba(232, 229, 220, 0.5);
}

.screen-wrap::before {
  top: -3px;
  left: -3px;
  border-top-width: 1px;
  border-left-width: 1px;
}

.screen-wrap::after {
  bottom: -3px;
  right: -3px;
  border-bottom-width: 1px;
  border-right-width: 1px;
}

#gameCanvas {
  width: 100%;
  height: 100%;
  display: block;
  background: #05070C;
  border: 1px solid rgba(232, 229, 220, 0.08);
  border-radius: 3px;
  image-rendering: auto;
  cursor: pointer;
  transform: translateZ(0);
  box-shadow: inset 0 -50px 130px rgba(0, 0, 0, 0.40);
  transition: border-color 0.3s ease;
}

#gameCanvas:hover {
  border-color: rgba(232, 229, 220, 0.14);
}

/* ── Title screen overlay: held establishing shot ──────── */
.overlay {
  position: absolute;
  inset: clamp(6px, 1.2vw, 14px);
  display: grid;
  place-items: center;
  background:
    radial-gradient(ellipse at 50% 30%, rgba(91, 216, 232, 0.05), transparent 45%),
    linear-gradient(180deg, rgba(5, 7, 12, 0.88), rgba(3, 4, 10, 0.94));
  border-radius: 3px;
  backdrop-filter: blur(10px);
}

.overlay.hidden { display: none; }

.title-card {
  position: relative;
  width: min(90%, 620px);
  padding: clamp(20px, 3.5vw, 42px);
  text-align: center;
  background: rgba(17, 21, 28, 0.86);
  border: 1px solid rgba(232, 229, 220, 0.16);
  border-radius: 6px;
  box-shadow:
    0 36px 80px rgba(0, 0, 0, 0.52),
    inset 0 0 0 1px rgba(5, 7, 12, 0.8);
  overflow: hidden;
}

/* Single hairline amber rule above the buttons — the only warm note */
.title-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 120px;
  height: 1px;
  background: rgba(255, 176, 0, 0.55);
  pointer-events: none;
}

.eyebrow {
  margin: 0 0 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.34em;
  font-size: clamp(0.56rem, 1.2vw, 0.7rem);
  font-weight: 500;
  font-family: var(--font-ui);
}

.title-tag {
  margin: 0 0 8px;
  color: var(--muted);
  font-family: var(--font-display);
  font-size: clamp(0.72rem, 1.5vw, 0.98rem);
  font-weight: 400;
  letter-spacing: 0.42em;
  text-transform: uppercase;
}

h1 {
  margin: 0 0 10px;
  color: var(--cream);
  font-family: var(--font-display);
  font-size: clamp(1.7rem, 5.6vw, 4.2rem);
  font-weight: 400;
  line-height: 1.02;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.8);
}

.subtitle,
.save-status {
  margin: 14px auto;
  max-width: 42rem;
  line-height: 1.6;
}

.subtitle {
  color: rgba(232, 229, 220, 0.62);
  font-size: clamp(0.74rem, 1.4vw, 0.9rem);
  letter-spacing: 0.01em;
}

.save-status {
  min-height: 1.4em;
  color: var(--muted);
  font-size: clamp(0.66rem, 1.1vw, 0.78rem);
  font-family: var(--font-ui);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

/* ── Buttons (shared): matte instrument plates ─────────── */
.title-card button,
.interact-btn,
.dpad-btn {
  border: 1px solid rgba(232, 229, 220, 0.18);
  background: #1A202A;
  color: rgba(232, 229, 220, 0.88);
  box-shadow:
    inset 1px 1px 0 rgba(43, 50, 61, 0.9),
    inset -1px -1px 0 rgba(5, 7, 12, 0.9);
  transition: border-color 150ms ease, color 150ms ease, box-shadow 150ms ease, background 150ms ease;
}

.title-card button {
  min-width: 140px;
  margin: 4px;
  padding: 11px 18px;
  border-radius: 2px;
  font-weight: 500;
  font-family: var(--font-ui);
  letter-spacing: 0.14em;
  font-size: clamp(0.68rem, 1.2vw, 0.78rem);
  text-transform: uppercase;
  position: relative;
  overflow: hidden;
}

/* Amber underline sweep on hover — the system's one moving accent */
.title-card button::after {
  content: '';
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 1px;
  background: var(--gold);
  transform: scaleX(0);
  transform-origin: left center;
  transition: transform 150ms ease;
  pointer-events: none;
}
.title-card button:hover::after,
.title-card button:focus-visible::after {
  transform: scaleX(1);
}

.title-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 8px;
}

#startButton {
  border-color: rgba(255, 176, 0, 0.45);
  color: var(--cream);
}

.title-card button:hover,
.interact-btn:hover,
.dpad-btn:hover,
.title-card button:focus-visible,
.interact-btn:focus-visible,
.dpad-btn:focus-visible {
  outline: none;
  border-color: rgba(255, 176, 0, 0.6);
  color: var(--cream);
  box-shadow:
    inset 1px 1px 0 rgba(43, 50, 61, 0.9),
    inset -1px -1px 0 rgba(5, 7, 12, 0.9),
    0 0 18px rgba(255, 176, 0, 0.08);
}

.title-card button:active,
.interact-btn:active,
.dpad-btn:active {
  transform: translateY(1px);
}

/* ── Mobile controls ───────────────────────────────────── */
.mobile-controls {
  width: min(100%, 560px);
  display: none;
  grid-template-columns: 120px 1fr;
  align-items: end;
  gap: 12px;
  user-select: none;
  padding-bottom: max(4px, env(safe-area-inset-bottom));
}

.dpad {
  width: 120px;
  height: 120px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 5px;
}

.dpad-btn {
  min-width: 0;
  padding: 0;
  border-radius: 2px;
  font-size: 1rem;
  display: grid;
  place-items: center;
  position: relative;
}

/* Active press: amber edge */
.dpad-btn:active {
  border-color: rgba(255, 176, 0, 0.6) !important;
  box-shadow:
    inset 1px 1px 0 rgba(43, 50, 61, 0.9),
    inset -1px -1px 0 rgba(5, 7, 12, 0.9) !important;
}

.dpad-btn.up { grid-area: 1 / 2; }
.dpad-btn.left { grid-area: 2 / 1; }
.dpad-btn.right { grid-area: 2 / 3; }
.dpad-btn.down { grid-area: 3 / 2; }

.action-buttons {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px;
}

.interact-btn {
  width: 100%;
  min-height: 56px;
  padding: 10px 14px;
  border-radius: 2px;
  font-size: clamp(0.74rem, 1.9vw, 0.86rem);
  font-weight: 500;
  font-family: var(--font-ui);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  position: relative;
}

.interact-btn:first-child {
  border-color: rgba(255, 176, 0, 0.4);
}

/* Interact button: steady amber baseline tick */
.interact-btn:first-child::before {
  content: '';
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 4px;
  height: 1px;
  background: rgba(255, 176, 0, 0.5);
  pointer-events: none;
}

.interact-btn:first-child { grid-column: 1 / -1; }

.small-action {
  min-height: 44px;
  padding: 8px 8px;
  font-size: clamp(0.68rem, 1.8vw, 0.78rem);
}

/* ── Mobile-first breakpoint ────────────────────────────── */
@media (hover: none), (max-width: 760px) {
  body {
    align-content: start;
    padding: max(4px, env(safe-area-inset-top)) max(4px, env(safe-area-inset-right)) max(4px, env(safe-area-inset-bottom)) max(4px, env(safe-area-inset-left));
  }

  .game-shell { gap: 6px; }

  .screen-wrap {
    width: 100%;
    padding: 4px;
    border-radius: 4px;
  }

  #gameCanvas { border-radius: 2px; }

  .overlay {
    inset: 4px;
    border-radius: 2px;
  }

  /* The d-pad / action buttons are only meaningful once a journey has started.
     Keeping them hidden on the title screen stops the (tall) title card from
     visually colliding with them on portrait phones. */
  .mobile-controls { display: none; }
  body.playing .mobile-controls { display: grid; }

  /* On mobile the canvas is short (16:9 of the phone width), so the title card
     was overflowing the little canvas and getting clipped top & bottom. Promote
     the title overlay to a full-viewport sheet that centres and scrolls cleanly.
     NOTE: a backdrop-filter on an ancestor turns position:fixed into a contained
     box, so we must drop the screen-wrap's blur while the title is up (below) for
     this to fill the real viewport. */
  body:not(.playing) .screen-wrap { backdrop-filter: none; }

  .overlay.title-screen:not(.hidden) {
    position: fixed;
    inset: 0;
    z-index: 60;
    display: grid;
    place-items: center;
    /* "safe center" keeps the card centred when it fits but pins it to the top
       (instead of clipping) if it is ever taller than the viewport. */
    align-content: safe center;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding:
      max(16px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
      max(16px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
    border-radius: 0;
  }

  .title-card {
    width: 94%;
    padding: 16px 14px;
    border-radius: 4px;
  }

  .title-card button {
    width: 100%;
    margin: 0;
    padding: 12px 16px;
    min-height: 44px;
  }

  .title-buttons {
    flex-direction: column;
    gap: 6px;
  }

  h1 {
    font-size: clamp(1.6rem, 8vw, 3rem);
  }

  .subtitle {
    font-size: clamp(0.72rem, 2.4vw, 0.88rem);
    margin: 10px auto;
  }

  .eyebrow {
    margin-bottom: 6px;
    font-size: clamp(0.56rem, 2vw, 0.72rem);
  }

  .title-tag {
    font-size: clamp(0.6rem, 2vw, 0.78rem);
  }
}

/* ── Narrow phones (360-420px) ──────────────────────────── */
@media (max-width: 420px) {
  .mobile-controls {
    grid-template-columns: 100px 1fr;
    gap: 8px;
  }

  .dpad {
    width: 100px;
    height: 100px;
    gap: 4px;
  }

  .dpad-btn {
    border-radius: 2px;
    font-size: 0.88rem;
  }

  .interact-btn {
    min-height: 48px;
    border-radius: 2px;
  }

  .small-action {
    min-height: 40px;
    font-size: clamp(0.64rem, 1.6vw, 0.72rem);
  }

  .title-card {
    padding: 14px 10px;
  }

  .title-card button {
    padding: 10px 12px;
    font-size: 0.72rem;
  }
}

/* ── Tall narrow phones ─────────────────────────────────── */
@media (max-width: 420px) and (min-height: 700px) {
  .game-shell {
    gap: 8px;
  }
  .screen-wrap {
    /* On tall phones, let canvas breathe more */
    aspect-ratio: 16 / 10;
  }
}

/* ── iPad / Tablet ──────────────────────────────────────── */
@media (min-width: 761px) and (max-width: 1024px) {
  .mobile-controls {
    display: grid;
    width: min(100%, 520px);
    grid-template-columns: 128px 1fr;
    gap: 14px;
  }

  .dpad {
    width: 128px;
    height: 128px;
  }

  .interact-btn {
    min-height: 60px;
  }
}

/* ── V2 polish round: short-screen + accessibility refinements ──────────── */

/* Landscape phones / very short viewports: keep title card fully visible
   and let it scroll internally instead of clipping the buttons off-screen. */
@media (max-height: 560px) {
  body { align-content: start; }
  .title-card {
    max-height: 92vh;
    max-height: 92dvh;
    overflow-y: auto;
    padding: 12px 14px;
    -webkit-overflow-scrolling: touch;
  }
  h1 { font-size: clamp(1.4rem, 6vw, 2.4rem); }
  .subtitle { margin: 8px auto; }
  .mobile-controls { gap: 8px; }
  .dpad { width: 96px; height: 96px; }
  .interact-btn:first-child { min-height: 44px; }
  .small-action { min-height: 36px; }
}

/* Title heading never overflows its card on any width. */
h1 {
  overflow-wrap: break-word;
  word-break: normal;
  hyphens: none;
}

/* Honour reduced-motion preferences: stop the decorative loops so the
   experience stays calm and battery-friendly for users who ask for it. */
@media (prefers-reduced-motion: reduce) {
  .title-card button::after { transition: none; }
}

/* Slightly larger, easier-to-hit d-pad glyphs on small phones without
   changing layout geometry (purely visual legibility). */
@media (max-width: 420px) {
  .dpad-btn { font-size: 0.96rem; }
}

/* Keep focus rings visible for keyboard users (accessibility), layered on
   top of the existing hover glow. */
.title-card button:focus-visible,
.interact-btn:focus-visible,
.dpad-btn:focus-visible {
  outline: 2px solid rgba(255, 176, 0, 0.5);
  outline-offset: 2px;
}

/* ── Secondary action chips (mobile): quiet instruments ──────────────── */
.small-action {
  border-color: rgba(232, 229, 220, 0.14);
  background: #11151C;
  color: var(--muted);
  letter-spacing: 0.12em;
}
#controlsButton.small-action {
  border-color: rgba(232, 229, 220, 0.14);
}

/* ── Avatar / identity pass: title polish + reduced-motion support ────────── */
.title-card button {
  transition: transform 160ms ease, box-shadow 160ms ease, border-color 160ms ease;
}
.title-card button:hover {
  transform: translateY(-1px);
}
#newGameButton {
  border-color: rgba(232, 229, 220, 0.22);
}

/* Respect the OS "reduce motion" preference: drop non-essential CSS animation
   and transitions. (The Canvas engine also tones down particle effects via the
   in-game Lite quality mode.) */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
  .title-card button:hover {
    transform: none;
  }
}
