:root {
  color-scheme: dark;
  --ink: #f6f3e8;
  --muted: #d5d0c0;
  --panel: rgba(12, 28, 18, 0.92);
  --line: rgba(244, 220, 160, 0.28);
  --gold: #f0c14e;
  --green: #3ddc84;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: #102216;
  color: var(--ink);
  font-family: "Noto Sans KR", "Apple SD Gothic Neo", sans-serif;
}

button, input, select { font: inherit; color: inherit; }

.app {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  grid-template-rows: minmax(0, 1fr);
}

.stage {
  grid-area: 1 / 1;
  position: relative;
  min-width: 0;
  min-height: 0;
  touch-action: none;
}

#game {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  background: #173222;
}

#map {
  position: absolute;
  top: calc(12px + env(safe-area-inset-top));
  left: calc(12px + env(safe-area-inset-left));
  width: 112px;
  height: 112px;
  border-radius: 16px;
  border: 1px solid var(--line);
}

.overlay {
  grid-area: 1 / 1;
  z-index: 3;
  display: grid;
  place-items: center;
  min-height: 0;
  overflow: auto;
  padding: 24px;
  background: rgba(8, 16, 10, 0.46);
}
.overlay[hidden] { display: none; }

.card {
  width: min(420px, 100%);
  padding: 22px;
  border-radius: 22px;
  background: var(--panel);
  border: 1px solid var(--line);
}

h1 { margin: 0 0 8px; font-size: 40px; letter-spacing: -0.04em; }
.lead, .help, .credit { color: var(--muted); line-height: 1.45; }
.card label { display: block; margin: 14px 0; }
.card input {
  width: 100%;
  margin-top: 6px;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #0d1c13;
  font-size: 16px;
}
.card button, .panel button, .dpad button {
  min-height: 48px;
  border: 0;
  border-radius: 14px;
  background: var(--green);
  color: #08210f;
  font-weight: 700;
}
.card button { width: 100%; }

.panel {
  grid-area: 1 / 2;
  min-height: 0;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  background: #0c1a12;
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: auto;
}
.hud, .dock {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}
.dock { margin-top: auto; }
.panel h2 { margin: 8px 0 0; font-size: 15px; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 0; }
.stats div, .people {
  padding: 8px 10px;
  border-radius: 12px;
  background: #143022;
}
.stats dt { color: var(--muted); font-size: 12px; }
.stats dd { margin: 2px 0 0; font-size: 22px; }
.people { margin: 0; word-break: keep-all; }
.help { word-break: keep-all; }
.energy {
  display: grid;
  grid-template-columns: auto auto minmax(48px, 1fr);
  align-items: center;
  gap: 8px;
  min-width: 0;
  margin: 0;
  padding: 8px 10px;
  border-radius: 12px;
  background: #143022;
}
.energy b { font-size: 22px; font-variant-numeric: tabular-nums; }
.energy-track {
  display: block;
  height: 10px;
  border-radius: 999px;
  background: #0b1a10;
  overflow: hidden;
}
#energy-bar {
  display: block;
  height: 100%;
  width: 0%;
  background: #3ddc84;
}
#energy-bar.is-ready { background: #e8ff7a; }
#energy-bar.is-on { background: #fff4a8; }
.actions {
  display: flex;
  gap: 8px;
}
#auto, #boost {
  flex: 1;
  min-height: 48px;
  touch-action: none;
  user-select: none;
}
#auto {
  background: #1d3a4a;
  color: #d7f0ff;
}
#auto.is-on {
  background: #7eb6ff;
  color: #08210f;
  box-shadow: 0 0 16px rgba(126, 182, 255, 0.75);
}
#boost {
  background: #1d4a32;
  color: #d7ffe4;
}
.idle {
  margin: 0;
  padding: 8px 10px;
  border-radius: 12px;
  background: #5a2a18;
  color: #ffe1a8;
  font-weight: 700;
}
.idle[hidden] { display: none; }
#boost.is-ready {
  background: var(--green);
  color: #08210f;
  box-shadow: 0 0 16px rgba(61, 220, 132, 0.7);
}
#boost.is-on {
  background: #e8ff7a;
  color: #08210f;
  box-shadow: 0 0 22px rgba(232, 255, 122, 0.9);
}
#boost:active { transform: scale(0.98); }
#board { margin: 0; padding-left: 18px; }
#board li { margin: 2px 0; }
.tools { display: flex; gap: 8px; }
.tools select, #sound {
  flex: 1;
  min-height: 48px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: #143022;
}
#sound { color: var(--ink); }
.dpad {
  display: grid;
  grid-template-columns: 56px 56px 56px;
  grid-template-areas: ". up ." "left . right" ". down .";
  justify-content: center;
  gap: 6px;
  touch-action: none;
  user-select: none;
}
.dpad button {
  min-width: 56px;
  min-height: 56px;
  font-size: 22px;
  touch-action: none;
  -webkit-touch-callout: none;
}
.dpad button:active { transform: scale(0.96); }
.dpad [data-dir="up"] { grid-area: up; }
.dpad [data-dir="down"] { grid-area: down; }
.dpad [data-dir="left"] { grid-area: left; }
.dpad [data-dir="right"] { grid-area: right; }
.credit { margin: 0; font-size: 13px; word-break: break-all; }
.credit a { color: var(--gold); }

@media (max-width: 800px), (max-height: 560px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(120px, 1fr) auto;
  }
  .stage { grid-area: 2 / 1; }
  .overlay { grid-area: 1 / 1 / -1 / 2; z-index: 4; }
  .panel {
    display: contents;
    padding: 0;
    border: 0;
    background: transparent;
    overflow: visible;
  }
  .hud {
    grid-area: 1 / 1;
    z-index: 2;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    padding: calc(8px + env(safe-area-inset-top)) calc(10px + env(safe-area-inset-right)) 8px calc(10px + env(safe-area-inset-left));
    background: #0c1a12;
    border-bottom: 1px solid var(--line);
  }
  .dock {
    grid-area: 3 / 1;
    z-index: 2;
    margin-top: 0;
    gap: 4px;
    padding: 8px calc(10px + env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) calc(10px + env(safe-area-inset-left));
    background: #0c1a12;
    border-top: 1px solid var(--line);
  }
  .hud .stats { flex: 1 1 190px; grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .hud .energy { flex: 1 1 100%; padding: 6px 8px; }
  .energy b { font-size: 18px; }
  .hud .people { flex: 1 1 100%; margin: 0; }
  .hud .tools { flex: 1 1 180px; }
  h1 { font-size: 32px; }
  .stats dd { font-size: 18px; }
  #map { width: 84px; height: 84px; }
  .dock .help, #board, .hud h2 { display: none; }
}

@media (max-height: 420px) {
  .lead { display: none; }
  .card { padding: 14px; }
  .hud { padding-top: calc(4px + env(safe-area-inset-top)); padding-bottom: 4px; }
  .hud .people { display: none; }
  .dock { padding-top: 4px; padding-bottom: calc(4px + env(safe-area-inset-bottom)); }
  .dpad { gap: 4px; }
  .dpad button { min-width: 48px; min-height: 48px; }
  .credit { font-size: 12px; line-height: 1.3; }
}

@media (pointer: coarse) and (min-height: 640px) {
  .dpad button { min-width: 64px; min-height: 64px; }
  #auto, #boost { min-height: 64px; }
}

@media (max-height: 560px) and (min-aspect-ratio: 1 / 1) {
  .app { display: block; position: relative; }
  .stage { position: absolute; inset: 0; }
  .overlay { position: absolute; inset: 0; z-index: 5; }
  .panel {
    display: block;
    position: absolute;
    inset: 0;
    z-index: 2;
    padding: 0;
    border: 0;
    background: transparent;
    overflow: visible;
    pointer-events: none;
  }
  .hud {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    flex-wrap: nowrap;
    gap: 6px;
    padding: calc(4px + env(safe-area-inset-top)) calc(8px + env(safe-area-inset-right)) 4px calc(8px + env(safe-area-inset-left));
    background: rgba(12, 26, 18, 0.9);
    pointer-events: auto;
  }
  .hud .people { display: none; }
  .dock {
    position: absolute;
    top: auto;
    right: calc(6px + env(safe-area-inset-right));
    bottom: calc(6px + env(safe-area-inset-bottom));
    left: auto;
    width: max-content;
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
    pointer-events: none;
  }
  .hud .energy { flex: 1 1 150px; }
  .dock .actions { pointer-events: auto; width: 220px; margin: 0 0 6px auto; }
  .dock .actions #auto,
  .dock .actions #boost { width: auto; min-height: 48px; }
  .dpad { pointer-events: auto; }
  .dock .credit {
    position: fixed;
    left: calc(8px + env(safe-area-inset-left));
    bottom: calc(6px + env(safe-area-inset-bottom));
    max-width: min(440px, 46vw);
    margin: 0;
    padding: 4px 8px;
    border-radius: 10px;
    background: rgba(12, 26, 18, 0.9);
    pointer-events: auto;
  }
  #map { top: calc(58px + env(safe-area-inset-top)); }
}
