:root {
  --red: #c8281e;
  --red-dark: #7a120c;
  --gold: #f2c14e;
  --ink: #f4ece6;
  --dim: rgba(244, 236, 230, 0.6);
  --glass: rgba(0, 0, 0, 0.35);
  --line: rgba(244, 236, 230, 0.45);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --ui-scale: 1;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: #000;
  color: var(--ink);
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  font-family: "Arial Black", "Helvetica Neue", Arial, sans-serif;
}

body { position: fixed; inset: 0; }

/* The engine sizes its picture to the chosen screen mode; object-fit keeps it in proportion */
#canvas {
  position: absolute;
  inset: 0;
  width: 100% !important;
  height: 100% !important;
  object-fit: contain;
  background: #000;
  outline: none;
}

button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

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

#controls { position: absolute; inset: 0; z-index: 5; touch-action: none; }

.ctl {
  position: absolute;
  border: 2px solid var(--line);
  background: var(--glass);
  border-radius: 999px;
  font-size: calc(14px * var(--ui-scale));
  letter-spacing: 0.06em;
  display: grid;
  place-items: center;
  padding: 0;
  touch-action: none;
}
.ctl.down { background: rgba(200, 40, 30, 0.55); border-color: var(--ink); }

#btn-fire {
  width: calc(96px * var(--ui-scale));
  height: calc(96px * var(--ui-scale));
  right: calc(28px + var(--safe-r));
  bottom: calc(28px + var(--safe-b));
  background: rgba(200, 40, 30, 0.45);
  border-color: rgba(255, 180, 160, 0.8);
  font-size: calc(18px * var(--ui-scale));
}
#btn-use {
  width: calc(64px * var(--ui-scale));
  height: calc(64px * var(--ui-scale));
  right: calc(52px + var(--safe-r));
  bottom: calc(150px * var(--ui-scale) + var(--safe-b));
}
#btn-menu, #btn-map {
  top: calc(14px + var(--safe-t));
  height: 40px;
  border-radius: 10px;
}
#btn-menu { left: calc(20px + var(--safe-l)); width: 48px; }
#btn-map { left: calc(78px + var(--safe-l)); width: 64px; }

#weapon-bar {
  position: absolute;
  top: calc(14px + var(--safe-t));
  right: calc(20px + var(--safe-r));
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--dim);
}
#weapon-bar .ctl { position: static; width: 52px; height: 40px; border-radius: 10px; font-size: 16px; }

#stick {
  position: absolute;
  width: 130px;
  height: 130px;
  margin: -65px 0 0 -65px;
  border: 2px solid var(--line);
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.2);
  pointer-events: none;
}
#stick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 56px;
  height: 56px;
  margin: -28px 0 0 -28px;
  border-radius: 50%;
  background: rgba(244, 236, 230, 0.55);
}

#menu-nav {
  position: absolute;
  right: calc(20px + var(--safe-r));
  bottom: calc(20px + var(--safe-b));
  display: grid;
  grid-template-columns: repeat(2, 72px);
  gap: 10px;
}
#menu-nav .ctl { position: static; height: 56px; border-radius: 12px; }
#menu-nav .ctl:last-child { grid-column: span 2; }

/* ---------- Title and pause screens ---------- */

#screens { position: absolute; inset: 0; z-index: 10; pointer-events: none; }

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: safe center;
  gap: 14px;
  padding: calc(16px + var(--safe-t)) calc(24px + var(--safe-r)) calc(16px + var(--safe-b)) calc(24px + var(--safe-l));
  background: radial-gradient(ellipse at center, rgba(40, 0, 0, 0.55), rgba(0, 0, 0, 0.88));
  pointer-events: auto;
  overflow-y: auto;
  touch-action: pan-y;
  text-align: center;
}

h1 {
  margin: 0;
  font-size: clamp(36px, min(11vh, 14vw), 84px);
  line-height: 0.9;
  letter-spacing: 0.04em;
  color: var(--ink);
  text-shadow: 0 4px 0 var(--red-dark), 0 0 24px rgba(200, 40, 30, 0.6);
}
h1 span { color: var(--red); }
h2 { margin: 0; font-size: 26px; letter-spacing: 0.05em; text-transform: uppercase; }
.sub { margin: 0; color: var(--gold); font-family: Georgia, "Times New Roman", serif; font-style: italic; font-size: 18px; }

.menu { display: flex; flex-direction: column; gap: 10px; width: min(380px, 100%); }
.menu button, .back {
  min-height: 52px;
  border: 2px solid var(--red);
  background: rgba(122, 18, 12, 0.6);
  border-radius: 10px;
  font-size: 19px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.menu button:active, .back:active { background: var(--red); }
.menu button.minor, .back { background: transparent; border-color: var(--line); font-size: 16px; min-height: 44px; }
.back { width: min(200px, 100%); }
.menu button small { display: block; font-family: Georgia, serif; font-style: italic; text-transform: none; color: var(--dim); font-size: 13px; letter-spacing: 0; }

.slider {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 14px;
  color: var(--dim);
  text-transform: uppercase;
  padding: 4px 0;
}
.slider input { width: 100%; accent-color: var(--red); height: 32px; }

.choice { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--dim); text-transform: uppercase; }
.choice-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.choice-buttons button {
  min-height: 44px;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: transparent;
  font-size: 15px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.choice-buttons button[aria-pressed="true"] { border-color: var(--red); background: rgba(122, 18, 12, 0.6); color: var(--ink); }

.bar { width: min(320px, 80%); height: 8px; border: 1px solid var(--line); border-radius: 4px; overflow: hidden; }
#loading-bar { height: 100%; width: 0; background: var(--red); transition: width 0.2s; }

.tip, .toast { margin: 0; font-family: Georgia, serif; font-size: 15px; color: var(--dim); max-width: 34em; }
.toast { color: var(--gold); }

.howto { text-align: left; max-width: 36em; margin: 0; padding-left: 1.2em; font-family: Georgia, serif; font-size: 16px; line-height: 1.45; display: flex; flex-direction: column; gap: 6px; }
.howto b { font-family: "Arial Black", Arial, sans-serif; font-size: 14px; letter-spacing: 0.04em; }

.menu button.recommended { border-color: var(--gold); }
.menu button.recommended::after {
  content: "Recommended";
  display: block;
  margin-top: 2px;
  font-size: 11px;
  letter-spacing: 0.12em;
  color: var(--gold);
}

/* ---------- First-game walkthrough ---------- */

#coach {
  position: absolute;
  inset: 0;
  z-index: 8;
  background: rgba(0, 0, 0, 0.62);
  font-family: Georgia, serif;
  font-size: 17px;
  line-height: 1.35;
}
.coach-zone {
  position: absolute;
  top: calc(70px + var(--safe-t));
  bottom: calc(16px + var(--safe-b));
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 6px;
  padding: 12px;
  border: 2px dashed rgba(242, 193, 78, 0.8);
  border-radius: 16px;
  text-align: center;
  color: var(--ink);
}
.coach-zone b, .coach-tag b {
  font-family: "Arial Black", Arial, sans-serif;
  color: var(--gold);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
#coach-move { left: calc(12px + var(--safe-l)); width: calc(45% - 24px); }
#coach-turn { left: 45%; right: calc(310px + var(--safe-r)); }
.coach-tag {
  position: absolute;
  transform: translateY(-50%);
  padding: 8px 12px;
  border-radius: 10px;
  background: rgba(20, 5, 4, 0.92);
  border: 2px solid var(--gold);
  white-space: nowrap;
}
#coach-go {
  position: absolute;
  left: 50%;
  top: calc(16px + var(--safe-t));
  transform: translateX(-50%);
  min-height: 52px;
  padding: 0 28px;
  border: 2px solid var(--red);
  border-radius: 10px;
  background: var(--red);
  font-size: 18px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  white-space: nowrap;
}

/* ---------- Upright phone: picture on top, control pad below ---------- */

.pad-label { display: none; }

@media (orientation: portrait) {
  :root { --pic-bottom: calc(var(--safe-t) + 75vw); }

  /* The engine centres its 4:3 picture in a wider buffer; widen the canvas so the picture spans the phone */
  #canvas {
    inset: var(--safe-t) auto auto 50%;
    width: calc(75vw * var(--buf-ar, 1.3334)) !important;
    height: 75vw !important;
    transform: translateX(-50%);
  }

  #controls {
    background:
      linear-gradient(to bottom, transparent var(--pic-bottom), #140504 var(--pic-bottom));
  }

  #btn-menu, #btn-map, #weapon-bar { top: calc(var(--pic-bottom) + 14px); }

  #btn-fire {
    width: calc(112px * var(--ui-scale));
    height: calc(112px * var(--ui-scale));
    right: calc(24px + var(--safe-r));
    bottom: calc(36px + var(--safe-b));
  }
  #btn-use {
    width: calc(72px * var(--ui-scale));
    height: calc(72px * var(--ui-scale));
    right: calc(44px + var(--safe-r));
    bottom: calc(168px * var(--ui-scale) + var(--safe-b));
  }

  .pad-label {
    display: block;
    position: absolute;
    top: calc(var(--pic-bottom) + 90px);
    width: 45%;
    text-align: center;
    font-size: 12px;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: rgba(244, 236, 230, 0.28);
    pointer-events: none;
  }
  #coach .coach-zone { top: calc(var(--pic-bottom) + 70px); bottom: calc(256px + var(--safe-b)); }
  #coach-move { width: calc(50% - 18px); }
  #coach-turn { left: 50%; right: calc(12px + var(--safe-r)); margin-left: 6px; }
  #coach-go { top: calc(var(--pic-bottom) - 72px); }

  #pad-move { left: 0; }
  #pad-turn { right: 0; width: 55%; }
}

[hidden] { display: none !important; }
