:root {
  --bg: #070b16;
  --panel: #121a2f;
  --panel-2: #18233d;
  --ink: #e8eefc;
  --muted: #8b9bb8;
  --line: rgba(120, 160, 255, 0.18);
  --neon: #5b8cff;
  --neon-2: #36e0c7;
  --warn: #ffb020;
  --bad: #ff4d6d;
  --ok: #3dd68c;
  --gold: #ffd166;
  --radius: 18px;
  --header-h: 0px;
  --bnav-h: 0px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --tap: 44px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: "PingFang SC", "Noto Sans SC", "Microsoft YaHei", system-ui, sans-serif;
  color: var(--ink);
  min-height: 100vh;
  min-height: 100dvh;
  background:
    radial-gradient(900px 420px at 8% -8%, rgba(91,140,255,.30), transparent 55%),
    radial-gradient(720px 380px at 92% 0%, rgba(54,224,199,.14), transparent 50%),
    radial-gradient(560px 360px at 70% 100%, rgba(255,77,109,.10), transparent 45%),
    var(--bg);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

.wrap {
  max-width: 1100px;
  margin: 0 auto;
  padding: 12px 14px calc(28px + var(--bnav-h) + var(--safe-bottom));
}

/* —— Banner —— */
.banner {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.035);
  color: var(--muted);
  padding: 10px 12px;
  border-radius: 14px;
  font-size: 12px;
  margin-bottom: 12px;
  line-height: 1.45;
}
.banner-text { margin: 0; flex: 1; min-width: 0; }
.banner-subj {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  font-weight: 800;
  color: var(--ink);
  font-size: 13px;
}
.select-inline {
  width: auto !important;
  display: inline-block;
  margin: 0 !important;
  padding: 8px 10px !important;
  min-height: var(--tap);
  font-size: 16px !important;
}

/* —— Sticky header —— */
.sticky-header {
  position: sticky;
  top: 0;
  z-index: 40;
  margin: 0 -14px 12px;
  padding: 8px 14px 10px;
  padding-top: calc(8px + var(--safe-top));
  background: linear-gradient(180deg, rgba(7,11,22,.96) 60%, rgba(7,11,22,.88));
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(120,160,255,.12);
}
.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo {
  width: 42px; height: 42px; border-radius: 13px;
  background: linear-gradient(135deg, #5b8cff, #36e0c7);
  display: grid; place-items: center; font-size: 20px;
  box-shadow: 0 0 22px rgba(91,140,255,.45);
  flex-shrink: 0;
}
.brand h1 {
  margin: 0;
  font-size: 18px;
  letter-spacing: 0.5px;
  font-weight: 900;
  line-height: 1.2;
}
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 11px; }
.hud { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.chip {
  background: rgba(18,26,47,.92);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 11px;
  font-size: 12px;
  font-weight: 700;
  min-height: 34px;
  display: inline-flex;
  align-items: center;
}
.chip em { color: var(--neon-2); font-style: normal; }
.chip.gold em { color: var(--gold); }

/* —— Top tabs (desktop / tablet) —— */
.tabs-top {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  width: 100%;
  background: rgba(18,26,47,.8);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 6px;
}
.tab {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 11px 14px;
  min-height: var(--tap);
  border-radius: 11px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 700;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.tab.active {
  color: #fff;
  background: linear-gradient(135deg, rgba(91,140,255,.95), rgba(54,224,199,.55));
  box-shadow: 0 0 16px rgba(91,140,255,.35);
}
.tab:active { transform: scale(0.98); }

/* —— Bottom nav (mobile) —— */
.bnav {
  display: none;
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 50;
  grid-template-columns: repeat(5, 1fr);
  gap: 0;
  padding: 6px 4px calc(6px + var(--safe-bottom));
  background: rgba(10,14,28,.96);
  border-top: 1px solid rgba(120,160,255,.2);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  box-shadow: 0 -8px 28px rgba(0,0,0,.35);
}
.bnav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  width: 100%;
  min-height: 52px;
  padding: 6px 2px;
  border-radius: 12px;
  background: transparent;
  color: var(--muted);
  font-size: 10px;
  font-weight: 800;
  border: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.bnav-item.active {
  color: #fff;
  background: rgba(91,140,255,.22);
  box-shadow: inset 0 0 0 1px rgba(91,140,255,.35), 0 0 14px rgba(91,140,255,.2);
}
.bnav-ico { font-size: 18px; line-height: 1; }
.bnav-lbl { letter-spacing: 0.02em; }

.main { min-width: 0; }
.screen { display: none; }
.screen.active { display: block; }

.grid { display: grid; gap: 12px; }
.grid-2 { grid-template-columns: 1.35fr 1fr; }
.grid-parent {
  grid-template-columns: 1fr 2fr;
}
@media (max-width: 860px) {
  .grid-2, .grid-parent { grid-template-columns: 1fr; }
}

.card {
  background: linear-gradient(165deg, rgba(28,40,68,.96), rgba(16,24,44,.96));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: 0 14px 36px rgba(0,0,0,.32), inset 0 1px 0 rgba(255,255,255,.04);
}
.card h2 { margin: 0 0 4px; font-size: 17px; font-weight: 900; letter-spacing: 0.2px; }
.card .sub { margin: 0 0 12px; color: var(--muted); font-size: 13px; line-height: 1.5; }
.hero { position: relative; overflow: hidden; border-color: rgba(91,140,255,.38); }
.hero::before {
  content: "";
  position: absolute; inset: -40% -20% auto auto; width: 260px; height: 260px;
  background: radial-gradient(circle, rgba(91,140,255,.28), transparent 70%);
  pointer-events: none;
}

.kpi { display: flex; gap: 8px; flex-wrap: wrap; }
.kpi .item {
  flex: 1; min-width: 88px;
  background: rgba(255,255,255,.04);
  border: 1px solid var(--line);
  border-radius: 14px; padding: 12px;
}
.kpi .n { font-size: 22px; font-weight: 900; letter-spacing: -0.02em; }
.kpi .l { font-size: 12px; color: var(--muted); margin-top: 2px; }

.bar {
  height: 10px; border-radius: 999px; background: rgba(255,255,255,.06);
  overflow: hidden; margin: 12px 0 8px; border: 1px solid var(--line);
}
.bar > span {
  display: block; height: 100%; width: 0%;
  background: linear-gradient(90deg, #5b8cff, #36e0c7);
  box-shadow: 0 0 12px rgba(54,224,199,.5);
  transition: width .35s ease;
}

.row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; padding: 12px 0; border-bottom: 1px solid rgba(255,255,255,.06);
  min-height: var(--tap);
}
.row:last-child { border-bottom: 0; }
.c-bad { color: var(--bad); }
.c-warn { color: var(--warn); }

.pill {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 5px 10px; border-radius: 999px; font-size: 12px; font-weight: 800;
  white-space: nowrap; flex-shrink: 0;
}
.pill.ok { background: rgba(61,214,140,.15); color: var(--ok); }
.pill.warn { background: rgba(255,176,32,.15); color: var(--warn); }
.pill.bad { background: rgba(255,77,109,.15); color: var(--bad); }
.pill.idle { background: rgba(91,140,255,.15); color: #9bbcff; }

.btn {
  border: 0; border-radius: 14px; padding: 12px 16px;
  min-height: var(--tap);
  cursor: pointer;
  font-size: 15px; font-weight: 800;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn:active:not(:disabled) { transform: scale(0.98); }
.btn.primary {
  color: #081018;
  background: linear-gradient(135deg, #5b8cff, #36e0c7);
  box-shadow: 0 0 22px rgba(91,140,255,.35);
}
.btn.ghost {
  background: rgba(91,140,255,.12);
  color: #b7ccff;
  border: 1px solid var(--line);
}
.btn.danger {
  background: rgba(255,77,109,.15);
  color: var(--bad);
  border: 1px solid rgba(255,77,109,.35);
}
.btn.block { width: 100%; }
.btn.sm { padding: 10px 12px; font-size: 13px; border-radius: 10px; min-height: 40px; }

.quest {
  display: flex; gap: 12px; align-items: flex-start;
  width: 100%;
  padding: 14px; margin-bottom: 10px;
  border-radius: 16px;
  background: rgba(255,255,255,.03);
  border: 1px solid var(--line);
  cursor: pointer;
  transition: border-color .15s, background .15s, transform .1s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  min-height: var(--tap);
}
.quest:hover, .quest:focus-visible {
  border-color: rgba(91,140,255,.55);
  background: rgba(91,140,255,.08);
  outline: none;
}
.quest:active {
  border-color: rgba(91,140,255,.7);
  background: rgba(91,140,255,.14);
  transform: scale(0.99);
}
.quest .lv {
  width: 42px; height: 42px; border-radius: 12px;
  display: grid; place-items: center; font-weight: 900; font-size: 11px;
  background: linear-gradient(135deg, rgba(91,140,255,.35), rgba(54,224,199,.2));
  border: 1px solid var(--line); flex: 0 0 auto;
}
.quest.boss {
  border-color: rgba(255,77,109,.45);
  background: linear-gradient(90deg, rgba(255,77,109,.12), rgba(255,255,255,.02));
}

.map {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 10px;
}
.node {
  aspect-ratio: 1; border-radius: 14px; display: grid; place-items: center;
  font-size: 11px; font-weight: 800; text-align: center; padding: 6px;
  border: 1px solid var(--line); background: rgba(255,255,255,.03);
  cursor: pointer; line-height: 1.25;
  min-height: 56px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.node:active { transform: scale(0.96); }
.node.ok { color: var(--ok); border-color: rgba(61,214,140,.4); background: rgba(61,214,140,.1); }
.node.warn { color: var(--warn); border-color: rgba(255,176,32,.4); background: rgba(255,176,32,.1); }
.node.bad { color: var(--bad); border-color: rgba(255,77,109,.45); background: rgba(255,77,109,.12); }
.node.idle { color: #6f7f9c; }
.node.focus {
  box-shadow: 0 0 0 2px rgba(91,140,255,.7), 0 0 18px rgba(91,140,255,.35);
}
.legend {
  display: flex; gap: 12px; flex-wrap: wrap; margin-top: 12px;
  font-size: 12px; color: var(--muted);
}
.dot {
  width: 10px; height: 10px; border-radius: 50%;
  display: inline-block; margin-right: 4px; vertical-align: middle;
}

.qbox {
  background: rgba(0,0,0,.28);
  border: 1px solid var(--line);
  border-radius: 16px; padding: 14px; margin: 12px 0;
}
.q-meta { font-size: 12px; color: var(--muted); margin-bottom: 8px; font-weight: 700; }
.q-prompt { font-size: 17px; line-height: 1.55; font-weight: 800; word-break: break-word; }

.choices { display: grid; gap: 10px; }
.choice {
  text-align: left;
  width: 100%;
  padding: 14px 16px;
  min-height: var(--tap);
  border-radius: 14px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.03);
  color: var(--ink);
  cursor: pointer;
  font-weight: 700;
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.choice:hover, .choice:focus-visible, .choice.selected {
  border-color: rgba(91,140,255,.7);
  background: rgba(91,140,255,.12);
  outline: none;
}
.choice:active {
  border-color: rgba(91,140,255,.85);
  background: rgba(91,140,255,.18);
}
.choice.correct { border-color: rgba(61,214,140,.7); background: rgba(61,214,140,.12); }
.choice.wrong { border-color: rgba(255,77,109,.7); background: rgba(255,77,109,.12); }

.battle-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.battle-actions-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.battle-actions-row .btn { width: 100%; font-size: 14px; padding: 12px 8px; }

.path { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 8px; }
.path .step {
  background: rgba(255,255,255,.04);
  border: 1px solid var(--line);
  border-radius: 999px; padding: 8px 12px; font-size: 12px; font-weight: 700;
  max-width: 100%;
  word-break: break-word;
}
.path .arrow { color: var(--neon-2); font-weight: 900; }

.note { margin-top: 12px; font-size: 12px; color: var(--muted); line-height: 1.6; }
.tagline {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 6px 10px; border-radius: 999px;
  background: rgba(54,224,199,.1); border: 1px solid rgba(54,224,199,.25);
  color: var(--neon-2); font-size: 11px; font-weight: 800; margin-bottom: 10px;
  letter-spacing: 0.04em;
}

.monster {
  display: grid; grid-template-columns: 56px 1fr; gap: 12px; align-items: center;
  padding: 12px; border-radius: 14px; margin-bottom: 10px;
  background: rgba(255,77,109,.08); border: 1px solid rgba(255,77,109,.25);
}
.monster .avatar {
  width: 56px; height: 56px; border-radius: 14px;
  display: grid; place-items: center; font-size: 28px;
  background: rgba(0,0,0,.25); border: 1px solid var(--line);
}
.monster-info .sub { margin: 2px 0 0; }
.hp { height: 8px; border-radius: 999px; background: rgba(255,255,255,.08); overflow: hidden; margin-top: 6px; }
.hp > i {
  display: block; height: 100%; width: 100%;
  background: linear-gradient(90deg, #ff4d6d, #ffb020);
  transition: width .3s;
}

.input-ans {
  width: 100%; padding: 14px 16px; border-radius: 14px;
  border: 1px solid var(--line); background: rgba(0,0,0,.25);
  color: var(--ink); font-size: 16px; font-weight: 700; outline: none;
  min-height: var(--tap);
}
.input-ans:focus { border-color: rgba(91,140,255,.7); box-shadow: 0 0 0 3px rgba(91,140,255,.15); }

.footer {
  margin-top: 24px; padding-top: 14px;
  border-top: 1px solid var(--line);
  color: var(--muted); font-size: 12px; line-height: 1.6;
  overflow-wrap: anywhere;
}
.footer a { color: var(--neon-2); }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.toolbar .btn { flex: 1; min-width: 120px; }

.loading {
  min-height: 40vh; display: grid; place-items: center;
  color: var(--muted); font-weight: 700; padding: 24px;
}

.feedback {
  margin-top: 12px; padding: 12px 14px; border-radius: 12px;
  font-size: 14px; line-height: 1.5; font-weight: 700;
}
.feedback.ok { background: rgba(61,214,140,.12); color: var(--ok); border: 1px solid rgba(61,214,140,.3); }
.feedback.bad { background: rgba(255,77,109,.12); color: #ff8da3; border: 1px solid rgba(255,77,109,.3); }
.feedback.warn { background: rgba(255,176,32,.12); color: var(--warn); border: 1px solid rgba(255,176,32,.3); }

.empty { color: var(--muted); font-size: 13px; padding: 20px 0; text-align: center; }

.select-book {
  width: 100%; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: rgba(0,0,0,.25);
  color: var(--ink); font-weight: 700; margin-bottom: 12px;
  font-size: 16px; min-height: var(--tap);
}

/* ========== Mobile ≤640px ========== */
@media (max-width: 640px) {
  :root { --bnav-h: 64px; --radius: 16px; }

  .wrap {
    padding: 10px 12px calc(20px + var(--bnav-h) + var(--safe-bottom));
  }

  .banner {
    flex-direction: column;
    align-items: stretch;
    padding: 10px;
    margin-bottom: 10px;
  }
  .banner-text {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .banner-subj { justify-content: space-between; }

  .sticky-header {
    margin: 0 -12px 10px;
    padding: 6px 12px 8px;
    padding-top: calc(6px + var(--safe-top));
  }
  .header-top { margin-bottom: 0; }
  .logo { width: 38px; height: 38px; font-size: 18px; border-radius: 11px; }
  .brand h1 { font-size: 16px; }
  .brand p { font-size: 10px; }
  .chip { padding: 6px 9px; font-size: 11px; min-height: 30px; }

  /* Prefer bottom nav on phone — hide top tabs */
  .tabs-top { display: none; }
  .bnav { display: grid; }

  .card { padding: 14px; }
  .card h2 { font-size: 16px; }
  .kpi .n { font-size: 20px; }
  .kpi .item { min-width: 0; padding: 10px; }

  .map { grid-template-columns: repeat(4, 1fr); gap: 8px; }
  .node { font-size: 10px; min-height: 64px; border-radius: 12px; padding: 5px; }

  .quest {
    flex-wrap: wrap;
    padding: 12px;
  }
  .quest .pill { margin-left: auto; }

  .q-prompt { font-size: 16px; }
  .monster { grid-template-columns: 48px 1fr; }
  .monster .avatar { width: 48px; height: 48px; font-size: 24px; }

  .fail-path-card { order: 2; }
  .combat-card { order: 1; }

  .toolbar .btn { min-width: 0; }
  .note { font-size: 11px; }
  .tagline { font-size: 10px; }
}

/* ========== Narrow phones ≤400px ========== */
@media (max-width: 400px) {
  .wrap { padding-left: 10px; padding-right: 10px; }
  .sticky-header { margin-left: -10px; margin-right: -10px; padding-left: 10px; padding-right: 10px; }

  .hud { width: 100%; }
  .chip { flex: 1; justify-content: center; }

  .map { grid-template-columns: repeat(3, 1fr); gap: 6px; }
  .node { font-size: 9px; min-height: 58px; line-height: 1.2; }

  .battle-actions-row { grid-template-columns: 1fr; }
  .battle-actions-row .btn { font-size: 15px; }

  .bnav-lbl { font-size: 9px; }
  .bnav-ico { font-size: 17px; }

  .kpi { gap: 6px; }
  .pill { font-size: 11px; padding: 4px 8px; }
}

/* Desktop polish */
@media (min-width: 641px) {
  .bnav { display: none !important; }
  .wrap { padding: 18px 16px 44px; }
  .sticky-header {
    margin: 0 0 14px;
    padding: 0;
    background: transparent;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    border-bottom: 0;
    position: static;
  }
  .header-top { margin-bottom: 10px; }
  .brand h1 { font-size: 20px; }
  .logo { width: 48px; height: 48px; font-size: 22px; }
  .map { grid-template-columns: repeat(6, 1fr); }
}

@media (min-width: 861px) and (max-width: 1100px) {
  .map { grid-template-columns: repeat(5, 1fr); }
}

#btnContinueScan { margin-top: 12px; }
#btnScanNext { margin-top: 8px; }
