:root {
  color-scheme: dark;
  --ink: #07080c;
  --panel: rgba(8, 10, 15, 0.86);
  --panel-strong: rgba(10, 12, 18, 0.94);
  --line: #f0cf75;
  --text: #fff2d0;
  --muted: #a7bcc5;
  --red: #ff5f81;
  --blue: #82e8ff;
  --green: #8de59f;
  --shadow: rgba(0, 0, 0, 0.55);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 0%, rgba(240, 207, 117, 0.11), transparent 32%),
    linear-gradient(180deg, #131b24 0%, #080a0f 60%, #030407 100%);
  font-family: Monaco, Consolas, "Courier New", monospace;
  color: var(--text);
  touch-action: none;
  user-select: none;
}

body {
  display: grid;
  place-items: center;
}

.game-shell {
  position: relative;
  width: min(100vw, 1180px);
  aspect-ratio: 16 / 9;
  max-height: 100vh;
  overflow: hidden;
  background: #05070a;
  border: 3px solid #111218;
  outline: 2px solid var(--line);
  box-shadow: 0 22px 80px var(--shadow);
}

.game-mount,
.game-mount canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
}

.hud,
.lower-ui,
.panel,
.rhythm-panel,
.touch-controls {
  position: absolute;
  z-index: 5;
}

.hud {
  inset: 12px 12px auto 12px;
  display: grid;
  grid-template-columns: 220px 176px 1fr 132px;
  gap: 8px;
  pointer-events: none;
}

.hud-card,
.prompt,
.mini-map,
.panel,
.rhythm-panel {
  background: var(--panel);
  border: 2px solid #08090d;
  box-shadow:
    inset 0 2px 0 rgba(255,255,255,0.08),
    inset 0 0 0 1px rgba(240, 207, 117, 0.28),
    0 10px 32px rgba(0,0,0,0.28);
  text-shadow: 0 2px 0 #000;
}

.hud-card {
  min-width: 0;
}

.hud-member {
  display: grid;
  grid-template-columns: 42px 1fr;
  gap: 8px;
  min-height: 48px;
  padding: 6px;
}

.member-chip,
.member-token {
  display: block;
  width: 34px;
  height: 40px;
  border: 2px solid #050609;
  background:
    radial-gradient(circle at 50% 27%, #f0c29f 0 19%, transparent 20%),
    linear-gradient(180deg, var(--member-a, #293f6c), var(--member-b, #111722));
  box-shadow:
    inset 0 -8px 0 rgba(0,0,0,0.24),
    0 0 14px var(--member-glow, #82e8ff);
}

.member-token {
  width: 42px;
  height: 54px;
  background-color: #111722;
  background-image: url("/assets/sakamichi-member-sprites.png");
  background-repeat: no-repeat;
  background-size: 168px 1008px;
  background-position: 0 calc(var(--member-row, 0) * -56px);
  image-rendering: pixelated;
}

.hud-member strong,
.hud-objective strong {
  display: block;
  color: var(--line);
  font-size: 12px;
  line-height: 1.2;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.hud-member small,
.hud-objective span,
.hud-stats span {
  display: block;
  color: var(--muted);
  font-size: 9px;
  line-height: 1.35;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.hud-bars {
  display: grid;
  gap: 5px;
  padding: 7px 8px;
}

.hud-bars label {
  display: grid;
  grid-template-columns: 22px 1fr;
  align-items: center;
  gap: 7px;
  font-size: 10px;
}

.hud-bars i {
  display: block;
  height: 8px;
  border: 1px solid #030406;
  background: #211a20;
  overflow: hidden;
}

.hud-bars i::before {
  content: "";
  display: block;
  width: var(--value, 100%);
  height: 100%;
  background: var(--fill, var(--red));
}

#energyMeter {
  --fill: var(--blue);
}

.hud-objective {
  min-width: 0;
  padding: 8px 10px;
  text-align: center;
}

.hud-objective span {
  margin-top: 5px;
  color: var(--text);
}

.hud-stats {
  display: grid;
  gap: 3px;
  padding: 7px 9px;
}

#resourceLabel {
  color: var(--green);
}

.lower-ui {
  inset: auto 12px 12px 12px;
  display: flex;
  align-items: end;
  gap: 8px;
  pointer-events: none;
}

.mini-map {
  width: 188px;
  height: 44px;
  padding: 5px;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 3px;
}

.mini-map span {
  display: block;
  border: 1px solid rgba(255,255,255,0.08);
  background: rgba(130, 232, 255, 0.16);
}

.mini-map .is-open {
  background: rgba(141, 229, 159, 0.55);
}

.mini-map .is-current {
  background: var(--line);
}

.mini-map .is-locked {
  background: rgba(80, 88, 96, 0.2);
}

.prompt {
  flex: 1;
  min-height: 34px;
  padding: 9px 12px;
  font-size: 10px;
  text-align: right;
}

.panel {
  display: none;
  pointer-events: auto;
  background: var(--panel-strong);
}

.panel.is-open,
.rhythm-panel.is-open {
  display: block;
}

.roster-panel {
  left: 50%;
  top: 52%;
  width: min(880px, calc(100vw - 48px));
  max-height: min(500px, calc(100vh - 130px));
  transform: translate(-50%, -50%);
  padding: 14px;
  overflow: auto;
}

.systems-panel {
  right: 12px;
  top: 76px;
  width: min(450px, calc(100vw - 24px));
  max-height: min(458px, calc(100vh - 140px));
  padding: 12px;
  overflow: auto;
}

.panel-head {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: start;
  margin-bottom: 10px;
}

.panel-head h2,
.panel-head h3 {
  margin: 0;
  color: var(--line);
  font-size: 15px;
}

.panel-head p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 9px;
  line-height: 1.45;
}

button {
  font-family: Monaco, Consolas, "Courier New", monospace;
}

.close,
.roster-card,
.tab,
.upgrade,
.stage,
.slot {
  border: 2px solid #050609;
  color: var(--text);
  background: #202a35;
  box-shadow:
    inset 0 2px 0 rgba(255,255,255,0.08),
    inset -4px -4px 0 rgba(0,0,0,0.25);
  cursor: pointer;
}

.close {
  width: 34px;
  height: 30px;
  font-weight: 700;
}

.roster-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(168px, 1fr));
  gap: 8px;
}

.roster-card {
  min-height: 92px;
  padding: 8px;
  display: grid;
  grid-template-columns: 42px 1fr;
  gap: 8px;
  text-align: left;
}

.roster-card.is-selected {
  outline: 2px solid var(--line);
  background: #303b4b;
}

.roster-card.is-locked {
  opacity: 0.44;
}

.roster-card strong,
.upgrade strong,
.stage strong,
.summary strong {
  display: block;
  color: var(--line);
  font-size: 11px;
  line-height: 1.25;
}

.roster-card small,
.upgrade small,
.stage small,
.summary small {
  display: block;
  margin-top: 3px;
  color: var(--muted);
  font-size: 8px;
  line-height: 1.35;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 5px;
}

.stats span {
  padding: 2px 4px;
  color: #d9f3f7;
  background: rgba(255,255,255,0.08);
  font-size: 8px;
}

.summary {
  display: grid;
  grid-template-columns: 42px 1fr;
  gap: 8px;
  align-items: center;
  margin-bottom: 9px;
  padding: 8px;
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(240, 207, 117, 0.24);
}

.tabs,
.slots {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 7px 0 10px;
}

.tab,
.slot {
  min-height: 27px;
  padding: 5px 8px;
  font-size: 9px;
}

.tab.is-selected,
.slot.is-selected {
  background: var(--line);
  color: #08090d;
}

.node-title {
  display: block;
  margin: 10px 0 6px;
  color: var(--line);
  font-size: 11px;
}

.route-audit {
  margin-bottom: 8px;
  padding: 8px;
  border: 1px solid rgba(130, 232, 255, 0.32);
  background: rgba(6, 9, 13, 0.54);
}

.route-audit strong {
  display: block;
  color: var(--line);
  font-size: 10px;
}

.route-audit small {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 8px;
  line-height: 1.4;
}

.route-audit.is-broken {
  border-color: rgba(255, 95, 129, 0.7);
}

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

.upgrade,
.stage {
  min-height: 58px;
  padding: 7px;
  text-align: left;
}

.upgrade.is-owned,
.stage.is-open {
  background: #26394a;
  outline: 1px solid rgba(130, 232, 255, 0.55);
}

.upgrade.is-locked,
.stage.is-locked {
  opacity: 0.42;
}

.rhythm-panel {
  display: none;
  left: 50%;
  bottom: 66px;
  width: min(540px, calc(100vw - 32px));
  transform: translateX(-50%);
  padding: 10px;
  text-align: center;
  pointer-events: none;
  box-shadow:
    inset 0 0 0 1px rgba(130, 232, 255, 0.32),
    0 0 30px rgba(130, 232, 255, 0.22);
}

.rhythm-panel strong {
  display: block;
  margin-bottom: 7px;
  color: var(--line);
  font-size: 12px;
}

.beat-lane {
  display: flex;
  justify-content: center;
  gap: 7px;
}

.beat {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 2px solid #050609;
  background: #1d2834;
  color: var(--text);
  font-weight: 700;
  box-shadow: 0 0 12px rgba(130, 232, 255, 0.2);
}

.beat.is-hit {
  background: var(--green);
  color: #071017;
}

.beat.is-current {
  background: var(--line);
  color: #071017;
  transform: translateY(-3px);
}

.touch-controls {
  display: none;
  inset: auto 18px 18px 18px;
  z-index: 8;
  justify-content: space-between;
  pointer-events: none;
}

.touch-dpad,
.touch-actions {
  position: relative;
  pointer-events: none;
}

.touch-dpad {
  width: 148px;
  height: 148px;
}

.touch-actions {
  width: 220px;
  height: 152px;
}

.touch-controls button {
  position: absolute;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 2px;
  border: 3px solid #050609;
  color: var(--text);
  background: #222c38;
  box-shadow:
    inset -5px -5px 0 rgba(0,0,0,0.34),
    inset 3px 3px 0 rgba(255,255,255,0.08),
    0 4px 0 #050609;
  font-weight: 700;
  pointer-events: auto;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

.dpad {
  width: 54px;
  height: 54px;
  border-radius: 10px;
  font-size: 20px;
}

.dpad.up,
.dpad.down {
  left: 47px;
}

.dpad.up {
  top: 0;
}

.dpad.down {
  bottom: 0;
}

.dpad.left,
.dpad.right {
  top: 47px;
}

.dpad.left {
  left: 0;
}

.dpad.right {
  right: 0;
}

.dpad.core {
  position: absolute;
  left: 52px;
  top: 52px;
  width: 44px;
  height: 44px;
  border: 3px solid #050609;
  border-radius: 8px;
  background: #141a21;
}

.action {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  font-size: 18px;
}

.action small {
  font-size: 8px;
}

.action.a {
  right: 0;
  top: 20px;
  background: #b73a5e;
}

.action.b {
  right: 58px;
  bottom: 0;
  background: #c94567;
}

.action.x {
  left: 28px;
  bottom: 16px;
  background: #3e78a7;
}

.action.y {
  left: 92px;
  top: 0;
  width: 52px;
  height: 52px;
  background: #566fa6;
}

.mini-button {
  left: 0;
  width: 54px;
  height: 30px;
  border-radius: 8px;
  font-size: 8px;
  letter-spacing: 0;
}

.mini-button.roster {
  top: 0;
}

.mini-button.map {
  top: 38px;
}

.touch-controls button.is-held,
.touch-controls button:active {
  transform: translateY(3px);
  box-shadow:
    inset 4px 4px 0 rgba(0,0,0,0.34),
    0 1px 0 #050609;
}

@media (hover: none) and (pointer: coarse), (max-width: 760px) {
  body {
    display: block;
  }

  .game-shell {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100dvh;
    max-height: none;
    aspect-ratio: auto;
    border: 0;
    outline: 0;
    box-shadow: none;
  }

  .game-mount {
    position: absolute;
    inset: 0 auto auto 0;
    width: 100vw;
    height: min(100vw, calc(100dvh - 214px));
  }

  .hud {
    inset: calc(7px + env(safe-area-inset-top)) 7px auto 7px;
    grid-template-columns: 116px 88px 1fr 70px;
    gap: 5px;
  }

  .hud-card {
    min-height: 38px;
  }

  .hud-member {
    grid-template-columns: 30px 1fr;
    padding: 5px;
  }

  .member-chip {
    width: 25px;
    height: 30px;
  }

  .hud-member strong,
  .hud-objective strong {
    font-size: 9px;
  }

  .hud-member small,
  .hud-objective span,
  .hud-stats span {
    font-size: 7px;
  }

  .hud-bars {
    padding: 6px;
  }

  .hud-bars label {
    grid-template-columns: 16px 1fr;
    gap: 4px;
    font-size: 8px;
  }

  .hud-objective {
    padding: 6px;
  }

  .hud-stats {
    padding: 6px;
  }

  .lower-ui {
    top: calc(min(100vw, calc(100dvh - 214px)) + 8px);
    bottom: auto;
    left: 7px;
    right: 7px;
  }

  .mini-map {
    width: 118px;
    height: 30px;
  }

  .prompt {
    min-height: 30px;
    padding: 8px;
    font-size: 8px;
  }

  .touch-controls {
    display: flex;
    left: max(12px, env(safe-area-inset-left));
    right: max(12px, env(safe-area-inset-right));
    bottom: calc(18px + env(safe-area-inset-bottom));
  }

  .touch-dpad {
    width: 136px;
    height: 136px;
  }

  .dpad {
    width: 50px;
    height: 50px;
  }

  .dpad.up,
  .dpad.down {
    left: 43px;
  }

  .dpad.left,
  .dpad.right {
    top: 43px;
  }

  .dpad.core {
    left: 48px;
    top: 48px;
    width: 40px;
    height: 40px;
  }

  .touch-actions {
    width: 198px;
    height: 146px;
  }

  .action {
    width: 56px;
    height: 56px;
  }

  .action.b {
    right: 54px;
  }

  .action.y {
    left: 78px;
  }

  .mini-button {
    width: 47px;
  }

  .roster-panel {
    top: 46%;
    width: calc(100vw - 16px);
    max-height: calc(100dvh - 310px);
    padding: 9px;
  }

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

  .roster-card {
    min-height: 84px;
    grid-template-columns: 33px 1fr;
    padding: 6px;
  }

  .systems-panel {
    left: 7px;
    right: 7px;
    top: calc(min(100vw, calc(100dvh - 214px)) + 44px);
    width: auto;
    max-height: 230px;
    padding: 9px;
  }

  .node-grid {
    grid-template-columns: 1fr;
  }

  .rhythm-panel {
    bottom: 188px;
    padding: 8px;
  }

  .beat {
    width: 29px;
    height: 29px;
  }
}

@media (max-width: 420px) {
  .touch-dpad {
    width: 136px;
    height: 136px;
  }

  .dpad {
    width: 50px;
    height: 50px;
  }

  .dpad.up,
  .dpad.down {
    left: 43px;
  }

  .dpad.left,
  .dpad.right {
    top: 43px;
  }

  .dpad.core {
    left: 48px;
    top: 48px;
    width: 40px;
    height: 40px;
  }

  .touch-actions {
    width: 198px;
    height: 146px;
  }

  .action {
    width: 56px;
    height: 56px;
  }

  .action.b {
    right: 54px;
  }

  .action.y {
    left: 78px;
  }

  .mini-button {
    width: 47px;
  }
}
