:root {
  --bg: #05070e;
  --panel: #0a1120;
  --panel-2: #0e1830;
  --ink: #e9f1ff;
  --muted: #8192b4;
  --line: rgba(120, 160, 255, 0.14);
  --cyan: #62e6ff;
  --gold: #ffd166;
  --red: #ff5d7a;
  --violet: #8b7bff;
  --narrator-h: 88px;
  --font: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background:
    radial-gradient(110% 70% at 50% -12%, rgba(52, 107, 255, 0.16), transparent 62%),
    radial-gradient(80% 55% at 100% 105%, rgba(255, 127, 80, 0.07), transparent 65%),
    var(--bg);
  color: var(--ink);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(120, 160, 255, 0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(120, 160, 255, 0.035) 1px, transparent 1px);
  background-size: 44px 44px;
  mask-image: radial-gradient(90% 70% at 50% 45%, #000 20%, transparent 100%);
  -webkit-mask-image: radial-gradient(90% 70% at 50% 45%, #000 20%, transparent 100%);
}

button {
  font: inherit;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

.app {
  width: 100%;
  max-width: 560px;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  padding:
    calc(10px + env(safe-area-inset-top, 0px))
    14px
    calc(10px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 8px;
  animation: app-in 0.45s ease-out both;
}

/* ---------- HUD ---------- */

.hud {
  flex: none;
  height: 28px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.level-block {
  display: flex;
  align-items: baseline;
  gap: 6px;
  min-width: 0;
  white-space: nowrap;
}

.level-kicker {
  font: 700 9px/1 var(--mono);
  letter-spacing: 0.16em;
  color: var(--muted);
}

.level-num {
  font: 700 19px/1 var(--mono);
  letter-spacing: -0.04em;
  color: var(--cyan);
  text-shadow: 0 0 14px rgba(98, 230, 255, 0.35);
}

.level-name {
  font-size: 11px;
  letter-spacing: 0.08em;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
}

.pips {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 5px;
}

.pip {
  width: 6px;
  height: 6px;
  flex: none;
  border-radius: 50%;
  background: #1a2540;
  box-shadow: inset 0 0 0 1px rgba(120, 160, 255, 0.12);
  transition: background 0.25s ease, box-shadow 0.25s ease, transform 0.25s ease;
}

.pip.done {
  background: rgba(98, 230, 255, 0.5);
}

.pip.active {
  background: var(--gold);
  box-shadow: 0 0 9px rgba(255, 209, 102, 0.65);
  transform: scale(1.3);
}

.icon-btn {
  flex: none;
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(13, 23, 44, 0.75);
  color: var(--muted);
  transition: transform 0.15s ease, color 0.2s ease, border-color 0.2s ease;
}

.icon-btn svg { width: 16px; height: 16px; }

.icon-btn:active {
  transform: scale(0.9) rotate(-20deg);
  color: var(--cyan);
  border-color: rgba(98, 230, 255, 0.35);
}

/* ---------- Narrator ---------- */

.narrator {
  flex: none;
  height: var(--narrator-h);
  display: flex;
  align-items: stretch;
  gap: 9px;
}

.avatar {
  --face: 44px;
  position: relative;
  width: var(--face);
  height: var(--face);
  flex: none;
  border-radius: 14px;
  border: 1px solid rgba(120, 170, 255, 0.28);
  background:
    radial-gradient(80% 60% at 50% 20%, rgba(98, 230, 255, 0.14), transparent 60%),
    linear-gradient(150deg, #16264a, #081021 70%);
  box-shadow:
    inset 0 0 16px rgba(70, 130, 255, 0.18),
    0 8px 18px rgba(0, 0, 0, 0.35);
  overflow: hidden;
}

.avatar::after {
  content: "";
  position: absolute;
  inset: 4px;
  border-radius: 10px;
  border: 1px solid rgba(120, 170, 255, 0.08);
  pointer-events: none;
}

.avatar .face {
  position: absolute;
  inset: 0;
}

.avatar i {
  position: absolute;
  display: block;
  font-style: normal;
}

.avatar .eye {
  width: 11%;
  height: 11%;
  top: 37%;
  border-radius: 50%;
  background: var(--cyan);
  box-shadow: 0 0 7px rgba(98, 230, 255, 0.85);
  animation: blink 5.8s infinite;
  transform-origin: center;
}

.avatar .eye.left { left: 27%; }
.avatar .eye.right { right: 27%; }

.avatar .brow {
  width: 18%;
  height: 2px;
  top: 25%;
  border-radius: 2px;
  background: var(--red);
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.avatar .brow.left { left: 21%; transform: rotate(16deg); }
.avatar .brow.right { right: 21%; transform: rotate(-16deg); }

.avatar .mouth {
  left: 50%;
  bottom: 22%;
  width: 32%;
  height: 15%;
  transform: translateX(-50%);
  border-bottom: 2px solid var(--cyan);
  border-radius: 0 0 999px 999px;
  opacity: 0.9;
  transition: border-color 0.2s ease, border-radius 0.2s ease, height 0.2s ease, transform 0.2s ease;
}

.avatar.big { --face: 62px; border-radius: 18px; }
.avatar.big::after { inset: 5px; border-radius: 13px; }
.avatar.big .mouth { border-bottom-width: 2px; }

[data-mood="angry"] .avatar .brow { opacity: 1; }
[data-mood="angry"] .avatar .eye {
  background: var(--red);
  box-shadow: 0 0 7px rgba(255, 93, 122, 0.8);
}
[data-mood="angry"] .avatar .mouth {
  height: 8%;
  border-bottom: 0;
  border-top: 2px solid var(--red);
  border-radius: 999px 999px 0 0;
  transform: translateX(-50%) translateY(4px);
}

[data-mood="happy"] .avatar .eye {
  background: var(--gold);
  box-shadow: 0 0 8px rgba(255, 209, 102, 0.9);
}
[data-mood="happy"] .avatar .mouth {
  height: 18%;
  border-color: var(--gold);
}

[data-mood="deadpan"] .avatar .mouth,
.avatar[data-mood="deadpan"] .mouth {
  width: 30%;
  height: 0;
  bottom: 27%;
  border-bottom: 1px solid var(--muted);
  border-radius: 0;
  opacity: 0.7;
}

.avatar[data-mood="deadpan"] .eye {
  background: var(--muted);
  box-shadow: 0 0 6px rgba(129, 146, 180, 0.5);
}

.bubble {
  position: relative;
  flex: 1;
  min-width: 0;
  height: var(--narrator-h);
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: linear-gradient(180deg, rgba(15, 25, 48, 0.96), rgba(8, 14, 29, 0.97));
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.24);
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.25s ease;
}

.bubble::before {
  content: "";
  position: absolute;
  left: -5px;
  top: 17px;
  width: 8px;
  height: 8px;
  border-left: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: #0c1528;
  transform: rotate(45deg);
}

.bubble-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  height: 14px;
}

.who {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.16em;
  color: var(--gold);
}

.mood {
  font-size: 9px;
  letter-spacing: 0.08em;
  color: var(--muted);
  padding: 1px 6px;
  border: 1px solid var(--line);
  border-radius: 999px;
  transition: color 0.2s ease, border-color 0.2s ease;
}

.line {
  margin: 4px 0 0;
  min-height: 3.9em;
  font-size: 13px;
  line-height: 1.35;
  color: var(--ink);
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.line.typing::after {
  content: "▍";
  margin-left: 1px;
  color: var(--gold);
  animation: caret 0.9s steps(1) infinite;
}

.narrator[data-mood="smug"] .bubble { border-color: rgba(255, 209, 102, 0.18); }
.narrator[data-mood="smug"] .mood { color: var(--gold); }

.narrator[data-mood="angry"] .bubble { border-color: rgba(255, 93, 122, 0.28); }
.narrator[data-mood="angry"] .mood {
  color: var(--red);
  border-color: rgba(255, 93, 122, 0.3);
}

.narrator[data-mood="happy"] .bubble { border-color: rgba(98, 230, 255, 0.26); }
.narrator[data-mood="happy"] .mood {
  color: var(--cyan);
  border-color: rgba(98, 230, 255, 0.3);
}

.narrator[data-mood="deadpan"] .mood { color: var(--muted); }

.narrator[data-mood="glitch"] .bubble {
  border-color: rgba(139, 123, 255, 0.55);
  animation: glitch-shake 0.18s steps(2, end) 4;
}

.narrator[data-mood="glitch"] .mood {
  color: var(--violet);
  border-color: rgba(139, 123, 255, 0.42);
}

.narrator[data-mood="glitch"] .avatar {
  animation: glitch-shake 0.2s steps(2, end) 4;
  border-color: rgba(139, 123, 255, 0.55);
}

.narrator[data-mood="glitch"] .avatar .eye {
  background: var(--violet);
  box-shadow: 0 0 8px rgba(139, 123, 255, 0.9);
  animation: blink 1.1s infinite;
}

.narrator[data-mood="glitch"] .avatar .mouth {
  width: 28%;
  height: 0;
  bottom: 26%;
  border-bottom: 2px solid var(--violet);
  border-radius: 0;
}

.narrator[data-mood="glitch"] .line {
  text-shadow: 1px 0 rgba(255, 93, 122, 0.28), -1px 0 rgba(98, 230, 255, 0.22);
}

.narrator[data-mood="glitch"] .line.typing::after { color: var(--violet); }

/* ---------- Stage ---------- */

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  border: 1px solid rgba(120, 160, 255, 0.14);
  border-radius: 20px;
  background: #060a14;
  overflow: hidden;
  box-shadow:
    0 24px 60px rgba(0, 0, 0, 0.42),
    inset 0 0 0 1px rgba(255, 255, 255, 0.016);
}

.stage::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: inset 0 0 70px rgba(0, 0, 0, 0.55);
  pointer-events: none;
}

#board {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}

.hint {
  flex: none;
  height: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  letter-spacing: 0.12em;
  color: var(--muted);
  transition: color 0.2s ease;
}

.hint.bad { color: var(--red); }
.hint.good { color: var(--gold); }
.hint.hold { color: var(--cyan); }

/* ---------- Gate ---------- */

.gate {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    calc(18px + env(safe-area-inset-top, 0px))
    18px
    calc(18px + env(safe-area-inset-bottom, 0px));
  overflow: auto;
  background:
    radial-gradient(100% 70% at 50% 0%, rgba(52, 107, 255, 0.17), transparent 60%),
    radial-gradient(70% 50% at 100% 100%, rgba(255, 93, 122, 0.07), transparent 60%),
    #05070e;
}

.gate-card {
  width: min(440px, 100%);
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: linear-gradient(180deg, rgba(14, 24, 46, 0.97), rgba(7, 12, 24, 0.98));
  box-shadow:
    0 40px 120px rgba(0, 0, 0, 0.6),
    inset 0 0 0 1px rgba(255, 255, 255, 0.016);
  animation: card-in 0.45s ease-out both;
}

.gate-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.gate-badge {
  font: 700 9px/1 var(--mono);
  letter-spacing: 0.2em;
  color: var(--muted);
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.gate-light {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--red);
  box-shadow: 0 0 12px rgba(255, 93, 122, 0.8);
  animation: gate-pulse 1.8s ease-in-out infinite;
}

.gate-face-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
}

.gate-speech {
  flex: 1;
  margin: 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(8, 14, 29, 0.7);
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.gate h1 {
  margin: 0 0 6px;
  font-size: clamp(28px, 8vw, 36px);
  line-height: 1.1;
  letter-spacing: -0.03em;
}

.gate-lead {
  margin: 0 0 20px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

.gate-facts {
  margin: 0 0 18px;
  border-top: 1px solid var(--line);
}

.gate-facts > div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 9px 0;
  border-bottom: 1px solid rgba(120, 160, 255, 0.08);
}

.gate-facts dt {
  font-size: 10px;
  letter-spacing: 0.12em;
  color: var(--muted);
}

.gate-facts dd {
  margin: 0;
  font-size: 12px;
  color: var(--ink);
  text-align: right;
}

.gate-facts dd.bad {
  color: var(--red);
  font-weight: 600;
}

.gate-address {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(5, 9, 18, 0.7);
}

.gate-address > span {
  display: block;
  margin-bottom: 6px;
  font-size: 9px;
  letter-spacing: 0.14em;
  color: var(--muted);
}

.gate-address code {
  display: block;
  font: 11px/1.5 var(--mono);
  color: var(--cyan);
  word-break: break-all;
}

.gate-address small {
  display: block;
  margin-top: 8px;
  font-size: 10px;
  line-height: 1.5;
  color: var(--muted);
}

.gate-foot {
  margin: 18px 0 0;
  text-align: center;
  font-size: 10px;
  letter-spacing: 0.1em;
  color: var(--muted);
}

/* ---------- Ending ---------- */

.ending {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 22px;
  background: rgba(3, 6, 13, 0.76);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
}

.ending-card {
  width: min(380px, 100%);
  padding: 28px 22px 22px;
  text-align: center;
  border: 1px solid rgba(255, 209, 102, 0.22);
  border-radius: 24px;
  background:
    radial-gradient(100% 60% at 50% 0%, rgba(255, 209, 102, 0.1), transparent 60%),
    linear-gradient(180deg, rgba(16, 26, 48, 0.98), rgba(7, 12, 24, 0.99));
  box-shadow: 0 40px 120px rgba(0, 0, 0, 0.65);
  animation: card-in 0.45s ease-out both;
}

.ending-kicker {
  font: 700 10px/1 var(--mono);
  letter-spacing: 0.24em;
  color: var(--gold);
}

.ending-card h2 {
  margin: 12px 0 6px;
  font-size: 30px;
  letter-spacing: -0.03em;
}

.ending-sub {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 22px 0 18px;
}

.stat {
  padding: 12px 10px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(6, 11, 22, 0.6);
}

.stat span {
  display: block;
  margin-bottom: 4px;
  font-size: 9px;
  letter-spacing: 0.14em;
  color: var(--muted);
}

.stat strong {
  font: 700 22px/1 var(--mono);
  color: var(--cyan);
}

.stat:last-child strong { color: var(--gold); }

.ending-card blockquote {
  margin: 0 0 18px;
  padding: 12px;
  border-left: 2px solid rgba(255, 209, 102, 0.4);
  border-radius: 0 8px 8px 0;
  background: rgba(255, 209, 102, 0.04);
  color: var(--ink);
  font-size: 12px;
  line-height: 1.6;
  text-align: left;
}

.primary {
  width: 100%;
  padding: 13px;
  border: 1px solid rgba(98, 230, 255, 0.34);
  border-radius: 12px;
  background: linear-gradient(180deg, rgba(98, 230, 255, 0.18), rgba(98, 230, 255, 0.05));
  color: var(--cyan);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.1em;
  transition: transform 0.15s ease, background 0.2s ease;
}

.primary:active {
  transform: scale(0.98);
  background: rgba(98, 230, 255, 0.22);
}

/* ---------- Motion ---------- */

@keyframes blink {
  0%, 90%, 100% { transform: scaleY(1); }
  94% { transform: scaleY(0.12); }
}

@keyframes caret {
  50% { opacity: 0; }
}

@keyframes gate-pulse {
  0%, 100% { opacity: 0.45; }
  50% { opacity: 1; }
}

@keyframes glitch-shake {
  0% { transform: translate(0, 0); }
  20% { transform: translate(-1.5px, 0) skewX(-0.6deg); }
  40% { transform: translate(1.5px, 0); }
  60% { transform: translate(-1px, 1px) skewX(0.5deg); }
  80% { transform: translate(1px, -1px); }
  100% { transform: translate(0, 0); }
}

@keyframes app-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes card-in {
  from { opacity: 0; transform: translateY(10px) scale(0.99); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@media (max-height: 700px) {
  :root { --narrator-h: 82px; }
  .app {
    gap: 6px;
    padding-top: calc(6px + env(safe-area-inset-top, 0px));
    padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px));
  }
  .line { font-size: 12px; line-height: 1.32; }
  .app .avatar { --face: 40px; }
}

@media (max-width: 350px) {
  .level-name { display: none; }
  .pips { gap: 4px; }
  .hint { font-size: 9px; }
  .gate-card { padding: 18px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
