:root {
  color-scheme: dark;
  --bg: #101725;
  --panel: rgba(17, 25, 38, 0.88);
  --panel-2: rgba(29, 42, 62, 0.92);
  --line: rgba(158, 207, 255, 0.24);
  --text: #f3f8ff;
  --muted: #a9bad1;
  --cyan: #6ee7ff;
  --pink: #ff78a7;
  --lime: #a4f36b;
  --gold: #ffd166;
  --shadow: 0 18px 45px rgba(0, 0, 0, 0.38);
  --font: "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 15% 10%, #29405f 0%, transparent 34%),
    radial-gradient(circle at 85% 90%, #4a2b54 0%, transparent 36%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
  color: inherit;
}

#app {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  min-width: 320px;
}

#topbar {
  display: grid;
  grid-template-columns: minmax(150px, 0.28fr) minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  min-height: 64px;
  padding: 9px clamp(10px, 2vw, 22px);
  background: linear-gradient(180deg, rgba(25, 37, 55, 0.98), rgba(15, 22, 34, 0.94));
  border-bottom: 1px solid var(--line);
  z-index: 4;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  position: relative;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background:
    radial-gradient(circle at 32% 28%, rgba(255,255,255,.95), rgba(255,255,255,0) 36%),
    radial-gradient(circle at 50% 55%, #86f5ff, #2f7ddb 68%, #6f43d8);
  box-shadow: 0 0 18px rgba(110,231,255,.45), inset 0 -5px 10px rgba(0,0,0,.25);
}

.brand-mark::after {
  content: "";
  position: absolute;
  left: 7px;
  bottom: 6px;
  width: 6px;
  height: 4px;
  border-left: 2px solid rgba(255,255,255,.85);
  border-bottom: 2px solid rgba(255,255,255,.85);
  transform: rotate(-45deg);
}

.brand h1 {
  margin: 0;
  font-size: clamp(16px, 1.8vw, 23px);
  line-height: 1.1;
  letter-spacing: .03em;
}

.brand p {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--muted);
}

#hud {
  display: grid;
  grid-template-columns: minmax(170px, .8fr) minmax(0, 1.2fr);
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.hud-item.objective {
  min-width: 0;
}

#levelName {
  display: block;
  color: var(--cyan);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .1em;
}

#objectiveText {
  display: block;
  overflow: hidden;
  margin-top: 2px;
  font-size: clamp(12px, 1.25vw, 15px);
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hud-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-width: 0;
}

.hud-grid span {
  flex: 1 1 auto;
  min-width: 82px;
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255,255,255,.05);
  color: #dcecff;
  font-size: clamp(10px, .95vw, 12px);
  font-variant-numeric: tabular-nums;
  text-align: center;
  white-space: nowrap;
}

.top-actions {
  display: flex;
  gap: 7px;
}

.icon-button {
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: linear-gradient(180deg, rgba(255,255,255,.15), rgba(255,255,255,.04));
  color: #eaf5ff;
  font-size: 16px;
  cursor: pointer;
}

.icon-button:hover { background: rgba(110,231,255,.2); }
.icon-button:disabled { opacity: .38; cursor: not-allowed; }

#stage {
  position: relative;
  display: grid;
  place-items: center;
  flex: 1 1 auto;
  min-height: 0;
  padding: 10px;
  overflow: hidden;
}

#game {
  display: block;
  max-width: 100%;
  max-height: 100%;
  border-radius: 14px;
  background: #172436;
  box-shadow: var(--shadow), 0 0 0 1px rgba(180,225,255,.15);
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 10px;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(8, 13, 21, .66);
  backdrop-filter: blur(5px);
  border-radius: 14px;
  z-index: 3;
}

.overlay.visible { display: flex; }

.panel {
  width: min(820px, 100%);
  max-height: 100%;
  overflow-y: auto;
  padding: clamp(16px, 3vw, 28px);
  border: 1px solid rgba(158,207,255,.3);
  border-radius: 18px;
  background: linear-gradient(160deg, rgba(26,39,58,.97), rgba(17,25,38,.97));
  box-shadow: var(--shadow);
  text-align: center;
}

.panel h2 {
  margin: 0 0 6px;
  font-size: clamp(24px, 4vw, 42px);
  line-height: 1.05;
  background: linear-gradient(90deg, #8ef4ff, #ff9ec4);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.panel .subtitle {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: clamp(13px, 1.5vw, 17px);
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin: 16px 0;
}

.level-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 132px;
  padding: 13px;
  border: 1px solid rgba(158,207,255,.25);
  border-radius: 14px;
  background: linear-gradient(160deg, rgba(255,255,255,.1), rgba(255,255,255,.03));
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.level-card[aria-pressed="true"] {
  border-color: rgba(110,231,255,.85);
  background: linear-gradient(160deg, rgba(110,231,255,.2), rgba(255,120,167,.13));
  box-shadow: 0 0 0 2px rgba(110,231,255,.2);
}

.level-card:disabled {
  opacity: .42;
  cursor: not-allowed;
}

.level-card strong { font-size: 15px; }
.level-card small { color: var(--muted); line-height: 1.35; }
.level-card .best { color: var(--gold); font-size: 12px; }
.level-card .tag {
  align-self: flex-start;
  padding: 3px 6px;
  border-radius: 999px;
  background: rgba(110,231,255,.15);
  color: var(--cyan);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.primary-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-top: 6px;
}

.menu-button {
  min-width: 128px;
  padding: 12px 20px;
  border: 1px solid rgba(110,231,255,.5);
  border-radius: 12px;
  background: linear-gradient(180deg, #4ac7e8, #2a74b8);
  color: #08131f;
  font-size: 15px;
  font-weight: 800;
  cursor: pointer;
}

.menu-button.secondary {
  border-color: rgba(255,209,102,.5);
  background: linear-gradient(180deg, #ffd98a, #d99538);
}

.menu-button.ghost {
  border-color: var(--line);
  background: rgba(255,255,255,.08);
  color: #eaf4ff;
}

.menu-button:disabled { opacity: .45; cursor: not-allowed; }
.menu-button:not(:disabled):hover { filter: brightness(1.08); }

.help {
  margin-top: 16px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.55;
}

.result-stats {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
  margin: 12px 0 18px;
}

.result-stat {
  min-width: 108px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255,255,255,.06);
}

.result-stat b { display: block; color: var(--cyan); font-size: 19px; }
.result-stat span { color: var(--muted); font-size: 11px; }

#touchControls {
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  min-height: 112px;
  padding: 8px 14px calc(8px + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, rgba(18,27,40,.9), rgba(9,14,22,.98));
  border-top: 1px solid var(--line);
  z-index: 4;
  touch-action: none;
  user-select: none;
}

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

.dpad .up { grid-column: 2; grid-row: 1; }
.dpad .left { grid-column: 1; grid-row: 2; }
.dpad .right { grid-column: 3; grid-row: 2; }
.dpad .down { grid-column: 2; grid-row: 2; }
.dpad-middle { display: contents; }

.touch-button,
.action-button {
  border: 1px solid rgba(158,207,255,.34);
  background: linear-gradient(180deg, rgba(255,255,255,.17), rgba(255,255,255,.05));
  color: #eef8ff;
  font-size: 18px;
  cursor: pointer;
  touch-action: none;
  user-select: none;
}

.touch-button { border-radius: 12px; }
.touch-button:active,
.action-button:active {
  background: rgba(110,231,255,.32);
}

.action-button {
  width: 108px;
  height: 92px;
  border-radius: 50%;
  background: radial-gradient(circle at 34% 28%, rgba(255,255,255,.7), rgba(255,255,255,0) 34%), linear-gradient(180deg, #7ee9ff, #2f74d8);
  color: #071522;
  font-size: 15px;
  font-weight: 900;
  letter-spacing: .04em;
}

@media (max-width: 780px), (pointer: coarse) {
  #topbar {
    grid-template-columns: auto minmax(0, 1fr) auto;
    min-height: 54px;
    padding: 6px 8px;
    gap: 7px;
  }

  .brand p { display: none; }
  .brand h1 { font-size: 15px; }
  .brand-mark { width: 28px; height: 28px; }

  #hud {
    grid-template-columns: 1fr;
    gap: 3px;
  }

  #objectiveText {
    white-space: normal;
    font-size: 11px;
    line-height: 1.15;
  }

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

  .hud-grid span {
    min-width: 0;
    padding: 3px 3px;
    font-size: 9px;
  }

  .icon-button { width: 34px; height: 34px; font-size: 14px; }
  #stage { padding: 7px; }
  .overlay { inset: 7px; padding: 8px; }
  .panel { padding: 13px; border-radius: 14px; }
  .level-grid { grid-template-columns: 1fr; gap: 7px; margin: 10px 0; }
  .level-card { min-height: 0; padding: 10px; }
  .menu-button { width: 100%; padding: 11px 14px; }
  .help { display: none; }
  #touchControls { display: flex; }
}
