:root {
  color-scheme: dark;
  --ink: #f7edd3;
  --muted: #a9bac3;
  --cyan: #70e2dd;
  --amber: #f2b84b;
  --coral: #f06f5e;
  --navy: #071521;
  --panel: rgba(8, 24, 36, 0.9);
}

* { box-sizing: border-box; }

html, body {
  min-height: 100%;
  margin: 0;
  overscroll-behavior: none;
}

body {
  color: var(--ink);
  background:
    radial-gradient(circle at 50% -20%, #17445a 0, transparent 48%),
    linear-gradient(160deg, #071521 0%, #050c15 72%);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button, a { -webkit-tap-highlight-color: transparent; }
button { font: inherit; }

.game-shell {
  width: min(100%, 980px);
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) 12px max(14px, env(safe-area-inset-bottom));
  display: grid;
  align-content: center;
  gap: 10px;
}

.topbar {
  min-height: 48px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
}

.back-link, .icon-button {
  min-width: 44px;
  min-height: 44px;
  border: 1px solid rgba(112, 226, 221, 0.24);
  border-radius: 13px;
  color: var(--ink);
  background: rgba(8, 24, 36, 0.78);
  display: grid;
  place-items: center;
  text-decoration: none;
}

.back-link { padding: 0 13px; font-size: .76rem; font-weight: 800; }
.icon-button { cursor: pointer; font-size: 1.2rem; }
.icon-button[aria-pressed="true"] { color: #71828b; }

.game-identity { min-width: 0; text-align: center; display: grid; gap: 2px; }
.game-identity span { color: var(--muted); font-size: .62rem; letter-spacing: .08em; text-transform: uppercase; }
.game-identity strong { overflow: hidden; font-size: .78rem; text-overflow: ellipsis; white-space: nowrap; }

.stage {
  position: relative;
  width: 100%;
  aspect-ratio: 12 / 7;
  max-height: calc(100dvh - 238px);
  min-height: 220px;
  margin: 0 auto;
  overflow: hidden;
  border: 1px solid rgba(112, 226, 221, .28);
  border-radius: 18px;
  background: #071521;
  box-shadow: 0 24px 80px rgba(0,0,0,.42), inset 0 0 0 1px rgba(255,255,255,.035);
  touch-action: none;
}

canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
}

.screen {
  position: absolute;
  inset: 0;
  z-index: 5;
  padding: clamp(16px, 4vw, 36px);
  background: linear-gradient(90deg, rgba(4, 11, 18, .96), rgba(4, 11, 18, .58)), url('./cover.png') center / cover;
  display: none;
  align-items: center;
}

.screen-visible { display: flex; }
#chapterScreen { justify-content: center; background: rgba(4, 13, 22, .93); text-align: center; }
#endScreen { justify-content: center; background: linear-gradient(rgba(4, 13, 22, .74), rgba(4, 13, 22, .93)), url('./cover.png') center / cover; text-align: center; }
.compact-screen { justify-content: center; background: rgba(4, 13, 22, .82); text-align: center; backdrop-filter: blur(8px); }

.title-card { width: min(58%, 470px); }
.chapter-card, .ending-card, .pause-card { width: min(100%, 560px); }
.kicker { margin: 0 0 8px; color: var(--cyan); font-size: clamp(.58rem, 1.6vw, .72rem); font-weight: 900; letter-spacing: .12em; text-transform: uppercase; }
h1, h2, p { text-wrap: balance; }
h1 { margin: 0; font-size: clamp(1.9rem, 5.5vw, 4.4rem); line-height: .85; letter-spacing: -.055em; text-transform: uppercase; }
h1 span { display: block; margin-top: 10px; color: var(--amber); font-size: .45em; letter-spacing: .015em; line-height: 1; }
h2 { margin: 0 0 12px; font-size: clamp(1.55rem, 4vw, 3rem); line-height: .95; }
.story, .chapter-card p, .ending-card p { color: #d3dfe1; font-size: clamp(.72rem, 1.55vw, .98rem); line-height: 1.5; }

.primary-button, .text-button {
  min-height: 46px;
  border-radius: 13px;
  cursor: pointer;
  font-weight: 900;
}

.primary-button {
  padding: 0 18px;
  border: 1px solid rgba(242, 184, 75, .72);
  background: linear-gradient(180deg, #f4c15c, #d89226);
  color: #18202a;
  box-shadow: 0 8px 24px rgba(242, 184, 75, .2);
}

.text-button {
  padding: 0 14px;
  border: 0;
  color: var(--cyan);
  background: transparent;
}

.as-link { display: inline-grid; place-items: center; text-decoration: none; }
.controls-hint { margin: 12px 0 0; color: var(--muted); font-size: .65rem; }
blockquote { margin: 16px auto; color: var(--amber); font-family: Georgia, serif; font-size: clamp(.9rem, 2vw, 1.2rem); font-style: italic; }
.ending-stats { margin: 12px auto; display: grid; }
.ending-stats span { font-size: 1.55rem; font-weight: 950; }
.ending-stats small { color: var(--muted); text-transform: uppercase; }
.local-best { margin: 4px 0 8px; color: var(--cyan) !important; font-size: .7rem !important; }

.hud {
  position: absolute;
  z-index: 3;
  inset: 8px 8px auto;
  display: flex;
  align-items: center;
  gap: 6px;
  pointer-events: none;
}

.hud[hidden] { display: none; }
.hud > div, .hud button {
  min-height: 38px;
  padding: 5px 10px;
  border: 1px solid rgba(112, 226, 221, .25);
  border-radius: 10px;
  background: rgba(3, 12, 20, .78);
  backdrop-filter: blur(5px);
}
.hud > div { display: grid; gap: 1px; }
.hud small { color: var(--muted); font-size: .48rem; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
.hud strong { font-size: .7rem; }
.hud button { margin-left: auto; min-width: 38px; padding: 0; color: var(--ink); pointer-events: auto; cursor: pointer; }
.timer-hud { min-width: 54px; }

.boss-hud {
  position: absolute;
  z-index: 3;
  top: 58px;
  left: 50%;
  min-width: 160px;
  padding: 7px 11px;
  border: 1px solid rgba(240, 111, 94, .65);
  border-radius: 11px;
  background: rgba(20, 8, 18, .9);
  text-align: center;
  transform: translateX(-50%);
  pointer-events: none;
}
.boss-hud[hidden] { display: none; }
.boss-hud small { display: block; color: #ffd0c5; font-size: .52rem; font-weight: 900; letter-spacing: .08em; text-transform: uppercase; }
.boss-hud span { display: block; height: 7px; margin-top: 5px; border-radius: 99px; background: linear-gradient(90deg, var(--coral) var(--boss-health, 100%), rgba(255,255,255,.12) 0); }

.tutorial-toast {
  position: absolute;
  z-index: 4;
  top: 58px;
  right: 9px;
  width: min(280px, calc(100% - 18px));
  padding: 10px 12px;
  border: 1px solid rgba(242, 184, 75, .62);
  border-radius: 12px;
  background: rgba(5, 16, 25, .94);
  box-shadow: 0 10px 28px rgba(0,0,0,.32);
  display: grid;
  gap: 2px;
  pointer-events: none;
}

.tutorial-toast[hidden] { display: none; }
.tutorial-toast strong { color: var(--amber); font-size: .72rem; letter-spacing: .08em; text-transform: uppercase; }
.tutorial-toast span { color: var(--ink); font-size: .68rem; line-height: 1.3; }
.tutorial-toast small { color: var(--cyan); font-size: .56rem; font-weight: 850; }
.boss-toast { right: auto; left: 9px; border-color: rgba(240, 111, 94, .7); }
.boss-toast strong { color: var(--coral); }

.touch-controls {
  min-height: 134px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 2px 10px;
  user-select: none;
  touch-action: none;
}
.touch-controls[hidden] { display: none; }
.joystick-control {
  --joystick-angle: 0deg;
  width: 126px;
  display: grid;
  justify-items: center;
  gap: 3px;
  touch-action: none;
  cursor: grab;
}
.joystick-control[data-active="true"] { cursor: grabbing; }
.joystick-control > small { color: var(--muted); font-size: .56rem; font-weight: 850; letter-spacing: .08em; text-transform: uppercase; }
.joystick-base {
  position: relative;
  width: 118px;
  height: 118px;
  overflow: hidden;
  border: 2px solid rgba(112, 226, 221, .36);
  border-radius: 50%;
  background: radial-gradient(circle, rgba(112, 226, 221, .08) 0 17%, transparent 18% 43%, rgba(112, 226, 221, .08) 44% 46%, transparent 47%), linear-gradient(180deg, rgba(20, 53, 69, .96), rgba(7, 25, 38, .96));
  box-shadow: inset 0 1px rgba(255,255,255,.1), 0 8px 22px rgba(0,0,0,.24);
}
.joystick-axis { position: absolute; inset: 0; display: grid; place-items: center; color: rgba(112, 226, 221, .24); font-size: .66rem; font-weight: 900; pointer-events: none; }
.joystick-axis-vertical { line-height: 2.8; }
.joystick-knob {
  position: absolute;
  z-index: 2;
  top: calc(50% - 27px);
  left: calc(50% - 27px);
  width: 54px;
  height: 54px;
  display: grid;
  place-items: center;
  border: 2px solid rgba(181, 255, 249, .72);
  border-radius: 50%;
  color: var(--ink);
  background: radial-gradient(circle at 38% 32%, #32818a, #123748 70%);
  box-shadow: 0 5px 15px rgba(0,0,0,.42), inset 0 1px rgba(255,255,255,.2);
  transition: transform 130ms ease-out;
  pointer-events: none;
}
.joystick-control[data-active="true"] .joystick-knob { transition: none; }
.joystick-arrow { color: var(--cyan); font-size: 1.15rem; transform: rotate(var(--joystick-angle)); }
.touch-button {
  min-width: 70px;
  min-height: 70px;
  border: 1px solid rgba(112, 226, 221, .3);
  border-radius: 20px;
  color: var(--ink);
  background: linear-gradient(180deg, rgba(20, 53, 69, .96), rgba(7, 25, 38, .96));
  box-shadow: inset 0 1px rgba(255,255,255,.08), 0 8px 22px rgba(0,0,0,.2);
  font-size: 1.3rem;
  font-weight: 950;
}
.touch-button.pressed { transform: translateY(2px); color: var(--cyan); background: #173f52; }
.action-button { min-width: 112px; min-height: 112px; border-radius: 50%; color: #1b2330; background: radial-gradient(circle at 38% 28%, #ffd879, #d88924 72%); border-color: rgba(255,213,126,.78); display: grid; place-content: center; gap: 0; font-size: .9rem; }
.action-button span { font-size: 1.45rem; line-height: .9; }
.action-button b { font-size: .82rem; text-transform: uppercase; }
.action-button small { font-size: .58rem; }

.game-footer { color: var(--muted); display: grid; gap: 3px; font-size: .66rem; line-height: 1.35; text-align: center; }
.game-footer p { margin: 0; }
.game-footer strong { color: var(--ink); }

@media (hover: hover) and (pointer: fine) {
  .touch-controls { display: none !important; }
  .stage { max-height: calc(100dvh - 150px); }
  .back-link:hover, .icon-button:hover { border-color: var(--cyan); }
  .primary-button:hover { filter: brightness(1.08); transform: translateY(-1px); }
}

@media (max-width: 620px) {
  .game-shell { align-content: start; padding-inline: 8px; }
  .stage { border-radius: 13px; max-height: none; min-height: 0; }
  .stage:has(.screen-visible) { aspect-ratio: auto; min-height: 420px; }
  .stage:has(.compact-screen.screen-visible) { min-height: 300px; }
  .touch-controls:not([hidden]) { display: flex !important; }
  .title-card { width: min(88%, 390px); }
  .game-footer p:last-child { display: none; }
}

.qa-tools {
  position: fixed;
  z-index: 50;
  right: 6px;
  bottom: 6px;
  display: flex;
  gap: 4px;
}

.qa-mode .touch-controls:not([hidden]) {
  display: flex !important;
}

.qa-tools button {
  min-height: 32px;
  border: 1px solid var(--cyan);
  border-radius: 8px;
  color: var(--ink);
  background: #071521;
  font-size: .62rem;
}

@media (max-width: 430px) {
  .game-identity span { display: none; }
  .title-card { width: min(82%, 340px); }
  .screen { padding: 14px; }
  .primary-button { min-height: 42px; font-size: .78rem; }
  .controls-hint { display: none; }
  .touch-controls { min-height: 126px; padding-inline: 4px; }
  .joystick-control { width: 116px; }
  .joystick-base { width: 110px; height: 110px; }
  .action-button { min-width: 104px; min-height: 104px; }
  .tutorial-toast { top: 54px; padding: 8px 10px; }
}

@media (orientation: landscape) and (max-height: 560px) {
  .game-shell { width: min(100%, 900px); min-height: 100dvh; padding-bottom: 2px; grid-template-columns: 1fr minmax(138px, 22vw); grid-template-rows: auto 1fr; align-content: stretch; }
  .topbar { grid-column: 1 / -1; min-height: 40px; }
  .back-link, .icon-button { min-height: 38px; }
  .stage { grid-column: 1; grid-row: 2; max-height: calc(100dvh - 62px); }
  .touch-controls { grid-column: 2; grid-row: 2; min-height: 0; flex-direction: column; justify-content: space-around; gap: 5px; padding: 0; }
  .joystick-control { width: 98px; gap: 0; }
  .joystick-control > small { display: none; }
  .joystick-base { width: 92px; height: 92px; }
  .joystick-knob { top: calc(50% - 23px); left: calc(50% - 23px); width: 46px; height: 46px; }
  .action-button { min-width: 78px; min-height: 78px; flex: 0 0 78px; }
  .game-footer { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
}
