:root {
  color-scheme: dark;
  --text: #eef4ff;
  --muted: #aab5cc;
  --panel: rgba(14, 20, 32, .88);
  --line: #344158;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  width: 100%;
  max-width: 100%;
  min-height: 100%;
  overflow-x: hidden;
  background:
    radial-gradient(circle at 25% 0%, rgba(78, 73, 139, .28), transparent 34rem),
    linear-gradient(135deg, #070a10, #141b29 70%, #090c12);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  display: grid;
  place-items: center;
  padding: 12px;
}

body.level-map-mode {
  place-items: start;
  overflow-x: auto;
}

main {
  width: 100%;
  max-width: 1120px;
  min-width: 0;
}

main.level-map-mode {
  width: max(min(1120px, 100%), var(--level-map-width));
  max-width: none;
}

header {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
  margin-bottom: 10px;
  min-width: 0;
}

.title-bar {
  display: flex;
  align-items: baseline;
  gap: 12px;
}

h1 {
  margin: 0;
  font-size: clamp(1.05rem, 3vw, 1.55rem);
  letter-spacing: .02em;
}

.language-switch {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #657189;
  font-size: .72rem;
}

.language-switch button {
  min-width: 28px;
  min-height: 28px;
  padding: 2px;
  border-radius: 3px;
  background: transparent;
  color: #77839a;
  font-size: inherit;
  font-weight: 500;
  box-shadow: none;
}

.language-switch button[aria-pressed="true"] { color: var(--text); }

.hud {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  min-width: 0;
}

.pill {
  padding: 6px 10px;
  border: 2px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  color: var(--muted);
  white-space: nowrap;
  font-size: .86rem;
}

.game-shell {
  position: relative;
  width: 100%;
  min-width: 0;
  max-width: 100%;
}

canvas {
  display: block;
  width: 100%;
  max-width: 100%;
  aspect-ratio: 30 / 17;
  background: #101622;
  border: 5px solid #2d3851;
  border-radius: 10px;
  box-shadow: 0 24px 75px rgba(0,0,0,.50);
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
  user-select: none;
}

canvas.level-map {
  width: var(--level-map-width);
  height: var(--level-map-height);
  max-width: none;
  aspect-ratio: auto;
}

.touch-controls {
  --touch-pad-size: clamp(88px, 27vw, 124px);
  --touch-pad-radius: clamp(44px, 13.5vw, 62px);
  --touch-buffer: clamp(18px, 5vw, 30px);
  --touch-pad-lift: clamp(14px, 4.33vw, 42px);
  --touch-safe-left: env(safe-area-inset-left, 0px);
  --touch-safe-right: env(safe-area-inset-right, 0px);
  position: absolute;
  inset: 0;
  z-index: 3;
  display: none;
  pointer-events: none;
  user-select: none;
}

.touch-zone {
  position: absolute;
  bottom: calc(11% + var(--touch-pad-lift) - var(--touch-buffer));
  width: calc(var(--touch-pad-size) + var(--touch-buffer) + var(--touch-buffer));
  aspect-ratio: 1;
  pointer-events: auto;
  touch-action: none;
}

.touch-zone[data-control-side="left"] {
  left: calc(clamp(14px, 6vw, 54px) + var(--touch-safe-left) - var(--touch-buffer));
}

.touch-zone[data-control-side="right"] {
  right: calc(clamp(14px, 6vw, 54px) + var(--touch-safe-right) - var(--touch-buffer));
}

.touch-pad {
  position: absolute;
  inset: var(--touch-buffer);
  border: 2px solid rgba(213, 226, 255, .55);
  border-radius: 50%;
  background: rgba(20, 29, 46, .16);
  opacity: .24;
  pointer-events: none;
  transition: opacity .8s ease;
}

.touch-pad::before,
.touch-pad::after {
  position: absolute;
  border-radius: 50%;
  pointer-events: none;
  content: "";
}

.touch-pad::before {
  inset: -2px;
  background:
    conic-gradient(
      from var(--active-sector-start, 0deg),
      var(--active-sector-color, transparent) 0 45deg,
      transparent 45deg 360deg
    ),
    repeating-conic-gradient(
      from 22.5deg,
      rgba(214, 226, 252, .16) 0 1.5deg,
      rgba(145, 169, 210, .025) 1.5deg 45deg
    );
}

.touch-pad::after {
  inset: 26%;
  border: 1px solid rgba(222, 232, 252, .42);
  background: #141d2e;
}

.touch-pad[data-active-sector] { --active-sector-color: rgba(133, 184, 255, .30); }
.touch-pad[data-active-sector="0"] { --active-sector-start: 67.5deg; }
.touch-pad[data-active-sector="1"] { --active-sector-start: 112.5deg; }
.touch-pad[data-active-sector="2"] { --active-sector-start: 157.5deg; }
.touch-pad[data-active-sector="3"] { --active-sector-start: 202.5deg; }
.touch-pad[data-active-sector="4"] { --active-sector-start: 247.5deg; }
.touch-pad[data-active-sector="5"] { --active-sector-start: 292.5deg; }
.touch-pad[data-active-sector="6"] { --active-sector-start: 337.5deg; }
.touch-pad[data-active-sector="7"] { --active-sector-start: 22.5deg; }

.touch-center {
  position: absolute;
  bottom: calc(11% + var(--touch-pad-lift) + var(--touch-pad-radius) - 4px);
  z-index: 2;
  width: 8px;
  height: 8px;
  border: 1px solid rgba(226, 235, 255, .50);
  border-radius: 50%;
  background: rgba(109, 135, 181, .38);
  box-shadow: 0 0 5px rgba(91, 139, 222, .30);
  opacity: 0;
  pointer-events: none;
  transition: opacity .8s ease;
}

.touch-center[data-control-side="left"] {
  left: calc(clamp(14px, 6vw, 54px) + var(--touch-safe-left) + var(--touch-pad-radius) - 4px);
}

.touch-center[data-control-side="right"] {
  right: calc(clamp(14px, 6vw, 54px) + var(--touch-safe-right) + var(--touch-pad-radius) - 4px);
}

.touch-controls.has-played .touch-pad { opacity: 0; }
.touch-controls.has-played:not(.is-active):not(.is-direct-active):not(.is-keyboard-active)[data-last-control-side="left"] .touch-center[data-control-side="left"],
.touch-controls.has-played:not(.is-active):not(.is-direct-active):not(.is-keyboard-active)[data-last-control-side="right"] .touch-center[data-control-side="right"] { opacity: .52; }
.touch-controls.is-active .touch-pad.is-active { opacity: .34; }
.touch-controls.remind .touch-pad { animation: touch-control-reminder 2.2s ease both; }

@keyframes touch-control-reminder {
  0%, 100% { opacity: 0; }
  40% { opacity: .22; }
}

.help {
  position: relative;
  z-index: 1;
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 10px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
  text-align: center;
  min-width: 0;
  max-width: 100%;
  overflow-wrap: anywhere;
}

.help-action {
  display: inline-grid;
  place-items: center;
  min-width: 2rem;
  padding: 4px 7px;
  border: 1px solid #485671;
  border-radius: 5px;
  background: rgba(39, 51, 76, .58);
  color: #dce6f8;
  font-size: .78rem;
  line-height: 1;
  box-shadow: inset 0 -1px rgba(0,0,0,.28);
}

.control-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 3px 8px;
}

.control-unit {
  flex: 0 0 auto;
  font: inherit;
  white-space: nowrap;
}

.control-unit:not(:last-child)::after {
  margin-left: 8px;
  color: #657189;
  content: "·";
}

.action-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 6px 10px;
}

.action-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-weight: 400;
  box-shadow: none;
  white-space: nowrap;
}

.action-item:not(:last-child)::after {
  margin-left: 5px;
  color: #657189;
  content: "·";
}

.action-item:focus { outline: none; }
.action-item:focus-visible .help-action {
  outline: 1px solid rgba(190, 210, 248, .65);
  outline-offset: 2px;
}

.map-help { display: none; }
body.level-map-mode .map-help { display: flex; }

.imprint {
  position: fixed;
  right: 12px;
  bottom: 8px;
  z-index: 3;
  font-size: 12px;
  text-align: right;
}

.imprint a { color: #888; }

.fullscreen-button {
  position: absolute;
  right: 12px;
  bottom: 12px;
  z-index: 4;
  width: 38px;
  height: 34px;
  padding: 7px;
  border: 1px solid rgba(210, 225, 255, .3);
  border-radius: 5px;
  background: rgba(8, 12, 20, .68);
  box-shadow: 0 1px 5px rgba(0,0,0,.5);
  opacity: .78;
  transition: opacity .8s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  appearance: none;
  line-height: 0;
}

.fullscreen-button:hover,
.fullscreen-button:focus-visible { opacity: 1; }

.fullscreen-button.is-input-hidden {
  opacity: 0;
  pointer-events: none;
}

.fullscreen-button svg {
  display: block;
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  overflow: visible;
}

.fullscreen-exit { display: none; }
.fullscreen-button.is-fullscreen .fullscreen-enter { display: none; }
.fullscreen-button.is-fullscreen .fullscreen-exit { display: inline; }

.summary-actions {
  position: absolute;
  left: var(--summary-panel-left, 28.65%);
  top: var(--summary-panel-top, 16.91%);
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: clamp(3px, .7vw, 8px);
  width: var(--summary-panel-width, 42.7%);
  height: var(--summary-panel-height, 55.5%);
  padding: clamp(5px, 1.25vw, 14px);
  pointer-events: none;
}

.summary-button {
  position: static;
  width: min(100%, 320px);
  min-width: 0;
  max-width: 100%;
  padding: clamp(2px, .7vw, 8px) clamp(6px, 2.4vw, 16px);
  border: 1px solid #8aa9e8;
  border-radius: 6px;
  background: #31466f;
  box-shadow: 0 3px 12px rgba(0,0,0,.45), inset 0 -2px rgba(0,0,0,.24);
  font-size: clamp(7px, 2.3vw, .9rem);
  line-height: 1.15;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: auto;
}

.summary-button small { margin-left: .35em; color: #b9c7df; font-size: .78em; }
.summary-button[hidden] { display: none; }

.game-shell:fullscreen,
.game-shell.fullscreen-fallback {
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 0;
  background: #05070b;
  box-shadow: none;
}

.game-shell:fullscreen canvas,
.game-shell.fullscreen-fallback canvas {
  width: min(100vw, 176.47vh);
  width: min(100vw, 176.47dvh);
  max-height: 100vh;
  max-height: 100dvh;
  border-width: 0;
  border-radius: 0;
  box-shadow: none;
}

.game-shell:fullscreen .touch-controls,
.game-shell.fullscreen-fallback .touch-controls {
  inset: 50% auto auto 50%;
  width: min(100vw, 176.47vh);
  width: min(100vw, 176.47dvh);
  height: min(56.67vw, 100vh);
  height: min(56.67vw, 100dvh);
  transform: translate(-50%, -50%);
}

.game-shell.fullscreen-fallback {
  position: fixed;
  inset: 0;
  z-index: 20;
}

body.game-fullscreen { overflow: hidden; }

.mobile {
  display: none;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-top: 10px;
}

button {
  border: 0;
  border-radius: 8px;
  padding: 12px 10px;
  background: #27334c;
  color: var(--text);
  font: inherit;
  font-weight: 700;
  box-shadow: inset 0 -3px rgba(0,0,0,.28);
  touch-action: manipulation;
  user-select: none;
}

button:active { transform: translateY(1px); }

@media (max-width: 760px), (any-pointer: coarse) {
  body { padding: 6px 6px 26px; }
  .game-shell {
    overflow: hidden;
    border: 5px solid #2d3851;
    border-radius: 10px;
    background: #101622;
    box-shadow: 0 24px 75px rgba(0,0,0,.50);
  }
  canvas {
    border: 0;
    border-radius: 0;
    box-shadow: none;
    image-rendering: auto;
  }
  header { grid-template-columns: 1fr; gap: 6px; text-align: center; }
  .title-bar { justify-content: center; }
  .hud { justify-content: center; }
  .pill { padding: 4px 7px; font-size: .75rem; }
  .help {
    margin-top: 7px;
    flex-direction: column;
    padding-inline: 4px;
    text-align: center;
    font-size: 10px;
    line-height: 1.45;
  }
  .action-list {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    width: 100%;
    margin: 3px 0 0;
    gap: 5px;
    justify-items: start;
  }
  .help.action-help {
    display: block;
    width: 100%;
    padding-inline: 8px;
    text-align: left;
  }
  .action-item {
    display: grid;
    grid-template-columns: 2.4rem auto;
    gap: 8px;
    width: fit-content;
    max-width: 100%;
    min-height: 2.4rem;
    padding: 2px 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    color: var(--muted);
    box-shadow: none;
    text-align: left;
    justify-self: start;
  }
  .action-item::after { display: none; }
  .help-action { min-width: 2.4rem; min-height: 2.4rem; }
  .touch-controls { display: block; }
  .mobile { display: grid; }
  .fullscreen-button { right: 8px; bottom: 8px; width: 44px; height: 44px; }
  .imprint { right: 8px; bottom: 5px; font-size: 11px; }
}

@media (max-width: 430px) {
  h1 { font-size: 1rem; }
  .hud { gap: 4px; }
}
