:root {
  color-scheme: dark;
  --color-page: #030303;
  --color-surface: #0f1210;
  --color-text: #f8f5ec;
  --color-muted: #d3d0c6;
  --color-border: #d8d2c2;
  --color-track: #252925;
  --color-accent: #f2d15f;
  --color-accent-strong: #ffe36d;
  --color-danger: #ff6b5f;
  --color-button: rgba(3, 3, 3, 0.82);
  --color-button-hover: rgba(40, 42, 38, 0.92);
  --color-focus: #8cd8ff;
  --space-edge: clamp(0.75rem, 2.5vw, 1.5rem);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --viewport-width: 100vw;
  --viewport-height: 100vh;
  --font-base: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

@supports (height: 100dvh) {
  :root {
    --viewport-width: 100dvw;
    --viewport-height: 100dvh;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--color-page);
}

body {
  margin: 0;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
  width: 100%;
  min-height: 100vh;
  min-height: var(--viewport-height);
  height: 100vh;
  height: var(--viewport-height);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  background: var(--color-page);
  color: var(--color-text);
  font-family: var(--font-base);
}

button,
input {
  font: inherit;
}

button {
  min-height: 2.75rem;
  border: 1px solid var(--color-border);
  border: 1px solid color-mix(in srgb, var(--color-border) 78%, transparent);
  background: var(--color-button);
  color: var(--color-text);
  cursor: pointer;
}

button:hover {
  background: var(--color-button-hover);
}

button:focus-visible,
input:focus-visible,
#gameCanvas:focus-visible {
  outline: 3px solid var(--color-focus);
  outline-offset: 3px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

#loading-screen {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: grid;
  place-items: center;
  padding: calc(var(--safe-top) + 1rem) calc(var(--safe-right) + 1rem) calc(var(--safe-bottom) + 1rem) calc(var(--safe-left) + 1rem);
  background: var(--color-page);
}

.loading-panel {
  width: min(20rem, 100%);
  padding: 1.25rem;
  border-top: 1px solid var(--color-border);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface);
  text-align: center;
}

.loading-label {
  margin: 0 0 0.5rem;
  color: var(--color-muted);
  font-size: 0.75rem;
  line-height: 1.4;
}

#loading-title {
  margin: 0 0 1rem;
  color: var(--color-text);
  font-size: 1.25rem;
  font-weight: 600;
  line-height: 1.2;
}

#loading-progress {
  display: block;
  width: 100%;
  height: 0.625rem;
  overflow: hidden;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  appearance: none;
  background: var(--color-track);
}

#loading-progress::-webkit-progress-bar {
  border-radius: 4px;
  background: var(--color-track);
}

#loading-progress::-webkit-progress-value {
  border-radius: 3px;
  background: var(--color-accent);
}

#loading-progress::-moz-progress-bar {
  border-radius: 3px;
  background: var(--color-accent);
}

#gameCanvas {
  display: block;
  width: 100%;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 16 / 9;
  border: 1px solid var(--color-border);
  background: #000;
  object-fit: contain;
  touch-action: none;
  user-select: none;
}

#game-shell {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

#canvas-frame {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.companion-ui {
  position: fixed;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

.companion-ui:focus-within {
  z-index: 20;
  right: var(--space-edge);
  bottom: var(--space-edge);
  width: min(22rem, calc(100vw - 2rem));
  height: auto;
  overflow: visible;
  clip: auto;
  clip-path: none;
  padding: 0.875rem;
  border-top: 1px solid var(--color-border);
  border-bottom: 1px solid var(--color-border);
  border-top: 1px solid color-mix(in srgb, var(--color-border) 65%, transparent);
  border-bottom: 1px solid color-mix(in srgb, var(--color-border) 45%, transparent);
  background: rgba(3, 3, 3, 0.92);
  white-space: normal;
}

.control-panel {
  width: 100%;
  color: var(--color-text);
}

.panel-kicker {
  margin: 0 0 0.25rem;
  color: var(--color-accent);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0;
}

.control-panel h2,
.control-panel p {
  margin: 0;
}

.button-stack {
  display: grid;
  gap: 0.625rem;
  margin-top: 0.875rem;
}

.movement-pad {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.625rem;
  margin-top: 0.875rem;
}

.move-button {
  width: 100%;
  min-width: 0;
  white-space: nowrap;
  text-wrap: balance;
  touch-action: manipulation;
  user-select: none;
}

.move-button[aria-pressed="true"] {
  border-color: var(--color-accent);
  background: var(--color-accent);
  color: var(--color-page);
}

.instruction-grid {
  display: grid;
  gap: 0.625rem;
  margin: 0.875rem 0;
}

.instruction-grid div {
  display: grid;
  grid-template-columns: 4rem 1fr;
  gap: 0.75rem;
  align-items: start;
}

.instruction-grid dt {
  color: var(--color-accent);
  font-weight: 700;
}

.instruction-grid dd {
  margin: 0;
  color: var(--color-muted);
}

.play-status {
  color: var(--color-muted);
  line-height: 1.6;
}

.mobile-note {
  color: var(--color-muted);
  line-height: 1.6;
}

.volume-control {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin: 0.875rem 0 0.5rem;
  color: var(--color-muted);
}

#volume-range {
  width: 100%;
  accent-color: var(--color-danger);
}

#volume-range + button,
.control-panel > button {
  width: 100%;
  margin-top: 0.875rem;
}

#loading-screen:not([hidden]) ~ #game-shell .companion-ui {
  visibility: hidden;
}

@media (min-aspect-ratio: 16 / 9) {
  #gameCanvas {
    width: auto;
    height: 100%;
  }
}

@media (orientation: portrait) {
  html,
  body {
    overflow-x: hidden;
    overflow-y: auto;
    touch-action: pan-y;
  }

  body {
    align-items: stretch;
    justify-content: flex-start;
    height: auto;
    min-height: var(--viewport-height);
  }

  #game-shell {
    min-height: 100%;
    height: auto;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    align-content: start;
    gap: clamp(0.75rem, 3vh, 1.5rem);
    padding: calc(var(--safe-top) + var(--space-edge)) var(--safe-right) calc(var(--safe-bottom) + var(--space-edge)) var(--safe-left);
  }

  #canvas-frame {
    width: 100%;
    height: auto;
  }

  #gameCanvas {
    width: 100%;
    height: auto;
    max-height: 48vh;
  }

  .companion-ui {
    position: static;
    width: min(100% - 2rem, 31rem);
    height: auto;
    overflow: visible;
    clip: auto;
    clip-path: none;
    justify-self: center;
    align-self: start;
    padding: 0 var(--space-edge);
    white-space: normal;
    touch-action: manipulation;
  }

  .mobile-note {
    margin: 0 0 0.875rem;
    padding-block: 0.625rem;
    border-top: 1px solid var(--color-border);
    border-bottom: 1px solid var(--color-border);
    border-top: 1px solid color-mix(in srgb, var(--color-border) 60%, transparent);
    border-bottom: 1px solid color-mix(in srgb, var(--color-border) 40%, transparent);
  }

  .control-panel {
    padding: 0.875rem 0 0;
    border-top: 1px solid var(--color-border);
    border-top: 1px solid color-mix(in srgb, var(--color-border) 45%, transparent);
  }

  .control-panel h2 {
    font-size: 1.375rem;
    line-height: 1.25;
  }
}
