:root {
  color-scheme: dark;
  --bg: #08090d;
  --panel: #121824;
  --panel2: #0d111a;
  --text: #f5f7fb;
  --muted: #9aa6bc;
  --accent: #65f0d4;
  --gold: #ffd35a;
  --danger: #ff5d73;
  --line: #2b3447;
}

* {
  box-sizing: border-box;
}

html,
body,
#game,
.mobile-controls,
.mobile-controls *,
button {
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  background: radial-gradient(circle at top left, #182030 0, var(--bg) 46%);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
}

button,
textarea {
  font: inherit;
}

.shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 14px;
  height: 100dvh;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}

.stage-panel,
.side-panel {
  min-height: 0;
  border: 1px solid var(--line);
  background: rgba(13, 17, 26, 0.92);
}

.stage-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  padding: 10px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding-bottom: 8px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  font-weight: 800;
  letter-spacing: 0;
  white-space: nowrap;
}

.mark {
  width: 15px;
  height: 15px;
  display: inline-block;
  background:
    linear-gradient(90deg, transparent 38%, #10141e 38% 62%, transparent 62%),
    linear-gradient(#ffd35a, #f08a35);
  border: 1px solid #ffe8a1;
}

.hud {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
  color: var(--accent);
  font-size: 14px;
}

#game {
  width: 100%;
  height: 100%;
  min-height: 0;
  display: block;
  background: #020307;
  image-rendering: pixelated;
  touch-action: none;
}

.message {
  min-height: 22px;
  padding-top: 8px;
  color: var(--gold);
  font-size: 13px;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 10px;
  overflow: auto;
}

.tabs,
.actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.actions {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

button {
  min-height: 42px;
  border: 1px solid #34415a;
  border-radius: 6px;
  background: #172033;
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
}

button:hover,
button.active {
  border-color: var(--accent);
  background: #20304a;
}

.panel {
  display: none;
  flex-direction: column;
  gap: 12px;
}

.panel.active {
  display: flex;
}

.mobile-controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  align-items: end;
  touch-action: none;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 58px);
  grid-template-rows: repeat(3, 54px);
  gap: 6px;
}

.dpad button {
  min-height: 54px;
}

.dpad [data-dir="up"] {
  grid-column: 2;
}

.dpad [data-dir="left"] {
  grid-column: 1;
  grid-row: 2;
}

.dpad [data-dir="down"] {
  grid-column: 2;
  grid-row: 2;
}

.dpad [data-dir="right"] {
  grid-column: 3;
  grid-row: 2;
}

.digpad {
  display: grid;
  grid-template-columns: minmax(90px, 1fr);
  gap: 8px;
}

.digpad button {
  min-height: 116px;
  color: var(--gold);
  font-size: 24px;
}

.palette {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
}

.palette button {
  min-height: 50px;
  padding: 4px;
}

textarea {
  width: 100%;
  min-height: 170px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #070a10;
  color: var(--text);
  padding: 8px;
  font-size: 12px;
}

.status {
  min-height: 42px;
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

@media (max-width: 920px) {
  body {
    overflow: auto;
  }

  .shell {
    min-height: 100dvh;
    height: auto;
    grid-template-columns: 1fr;
    grid-template-rows: auto auto;
    align-content: start;
  }

  .stage-panel {
    min-height: 0;
    grid-template-rows: auto auto auto;
  }

  #game {
    aspect-ratio: 28 / 18;
    height: auto;
    max-height: min(62svh, 64vw);
  }

  .side-panel {
    overflow: visible;
    padding-bottom: 166px;
  }

  .mobile-controls {
    position: fixed;
    right: max(10px, env(safe-area-inset-right));
    bottom: max(10px, env(safe-area-inset-bottom));
    z-index: 20;
    grid-template-columns: auto auto;
    gap: 10px;
    padding: 8px;
    border: 1px solid rgba(101, 240, 212, 0.36);
    background: rgba(8, 9, 13, 0.82);
    backdrop-filter: blur(10px);
  }

  .dpad {
    grid-template-columns: repeat(3, 54px);
    grid-template-rows: repeat(2, 50px);
  }

  .dpad button {
    min-height: 50px;
  }

  .digpad {
    grid-template-columns: 82px;
  }

  .digpad button {
    min-height: 106px;
  }
}

@media (max-width: 920px) and (orientation: landscape) {
  .shell {
    padding-bottom: calc(70px + env(safe-area-inset-bottom));
  }

  .stage-panel {
    justify-items: start;
  }

  .topbar,
  .message {
    justify-self: stretch;
  }

  #game {
    width: min(100%, calc((100svh - 118px) * 28 / 18));
    max-height: calc(100svh - 118px);
  }

  .side-panel {
    padding-bottom: 10px;
  }
}

@media (max-width: 560px) {
  body {
    background: #08090d;
  }

  .shell {
    min-height: auto;
    padding: 8px 8px calc(112px + env(safe-area-inset-bottom));
    gap: 8px;
  }

  .stage-panel {
    padding: 8px;
  }

  .side-panel {
    padding-bottom: 10px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
    gap: 6px;
  }

  .brand {
    font-size: 20px;
  }

  .hud {
    justify-content: flex-start;
    gap: 8px;
    font-size: 12px;
    line-height: 1.3;
  }

  #game {
    max-height: none;
  }

  .mobile-controls {
    right: max(6px, env(safe-area-inset-right));
    bottom: max(6px, env(safe-area-inset-bottom));
    gap: 6px;
    padding: 6px;
  }

  .dpad {
    grid-template-columns: repeat(3, 42px);
    grid-template-rows: repeat(2, 40px);
    gap: 4px;
  }

  .dpad button {
    min-height: 40px;
  }

  .digpad {
    grid-template-columns: 66px;
  }

  .digpad button {
    min-height: 84px;
    font-size: 16px;
  }
}
