:root {
  --ink: #ece9df;
  --muted: #b0aca0;
  --panel: rgba(12, 15, 20, 0.54);
  --panel-border: rgba(199, 178, 126, 0.34);
  --gold: #e2bf72;
  --danger: #ca6b6b;
  --safe: #7cbc90;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  min-height: 100%;
  overflow: hidden;
  color: var(--ink);
  font-family: "Avenir Next", "PingFang SC", "Noto Sans CJK SC", sans-serif;
  background: #0d1115;
}

.game-shell {
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr) auto;
  gap: 3px;
  width: 100vw;
  height: 100vh;
  height: calc(var(--app-vh, 1vh) * 100);
  min-height: 100vh;
  min-height: calc(var(--app-vh, 1vh) * 100);
  max-width: 720px;
  margin: 0 auto;
  padding: max(5px, env(safe-area-inset-top)) 8px max(6px, env(safe-area-inset-bottom)) 8px;
  background: linear-gradient(180deg, #11161c, #0d1217);
  overflow: hidden;
}

.hud {
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  background: var(--panel);
  backdrop-filter: blur(5px);
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.34);
}

.top-hud {
  padding: 3px 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.action-hud {
  padding: 3px 6px;
}

.title-wrap h1 {
  margin: 0;
  font-size: 13px;
  letter-spacing: 1px;
}

.title-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.title-row .mini-btn {
  flex: 0 0 auto;
  padding: 1px 7px;
}

.title-wrap p {
  margin: 1px 0 0;
  font-size: 9px;
  color: var(--muted);
}

.identity-row {
  margin-top: 1px;
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 9px;
  color: var(--muted);
  line-height: 1.1;
}

#playerNameText {
  color: #f5e9cb;
  font-size: 10px;
  max-width: 112px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.identity-row .mini-btn {
  flex: 0 0 auto;
  padding: 1px 6px;
}

.time-wrap {
  text-align: right;
  display: grid;
  gap: 0;
}

.audio-row {
  margin-top: 1px;
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 3px;
}

.mini-btn {
  border: 1px solid rgba(212, 193, 150, 0.45);
  border-radius: 8px;
  background: rgba(39, 46, 56, 0.75);
  color: #efe9dc;
  font-size: 9px;
  padding: 1px 6px;
  cursor: pointer;
}

#musicVolume {
  width: 100%;
  accent-color: #d7b869;
  cursor: pointer;
  -webkit-user-select: auto;
  user-select: auto;
}

.metric {
  font-size: 9px;
  color: #d8d4cb;
}

.metric strong {
  font-size: 11px;
  color: #f7f0d8;
}

.bars-hud {
  padding: 2px 6px;
  display: grid;
  gap: 2px;
}

.bar-head-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

#labelCourageText {
  font-weight: 700;
  color: var(--gold);
  white-space: nowrap;
  line-height: 1;
}

#courageText {
  line-height: 1;
  font-size: 21px;
}

progress {
  width: 100%;
  height: 5px;
  border-radius: 999px;
  overflow: hidden;
  border: 0;
}

progress::-webkit-progress-bar {
  background: rgba(57, 66, 76, 0.9);
}

progress::-webkit-progress-value {
  background: linear-gradient(90deg, #8aa6ba, #e1be71);
}

.status-row {
  display: flex;
  justify-content: space-between;
  font-size: 9px;
  color: #ddd8cc;
  margin-top: 1px;
  line-height: 1.1;
}

.status-row strong {
  color: #f4edd8;
}

.hint {
  margin: 0;
  min-height: 0;
  font-size: 10px;
  color: #d4d0c6;
  display: none;
}

.hint.good {
  color: var(--safe);
}

.hint.bad {
  color: var(--danger);
}

.help-panel {
  position: fixed;
  left: 12px;
  right: 12px;
  top: max(8px, env(safe-area-inset-top) + 6px);
  z-index: 20;
  padding: 10px 12px;
  display: grid;
  gap: 4px;
  max-width: 680px;
  margin: 0 auto;
  box-shadow: 0 18px 36px rgba(0, 0, 0, 0.45);
}

.help-panel p {
  margin: 0;
  font-size: 12px;
  color: #dfd9cb;
  line-height: 1.35;
}

.help-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 2px;
}

.help-head strong {
  color: #f2e5c0;
  font-size: 13px;
}

.leaderboard-panel {
  position: fixed;
  left: 12px;
  right: 12px;
  top: max(8px, env(safe-area-inset-top) + 6px);
  z-index: 21;
  padding: 10px 12px;
  display: grid;
  gap: 6px;
  max-width: 680px;
  margin: 0 auto;
  max-height: min(70vh, 620px);
  overflow: hidden;
  box-shadow: 0 18px 36px rgba(0, 0, 0, 0.45);
}

.leader-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.leader-head strong {
  color: #f2e5c0;
  font-size: 13px;
}

.leader-actions {
  display: flex;
  gap: 6px;
}

.leader-actions .mini-btn.active {
  background: linear-gradient(180deg, #d8ba76, #a88641);
  color: #21190f;
  border-color: rgba(224, 203, 158, 0.64);
}

.leader-meta {
  margin: 0;
  font-size: 11px;
  color: #d9d3c5;
  min-height: 16px;
}

.leader-list {
  margin: 0;
  padding: 0 0 0 20px;
  overflow-y: auto;
  max-height: min(48vh, 420px);
  list-style: decimal;
}

.leader-list li {
  display: grid;
  grid-template-columns: minmax(70px, 1.2fr) minmax(65px, 0.9fr) minmax(58px, 0.8fr);
  gap: 6px;
  align-items: center;
  font-size: 11px;
  line-height: 1.3;
  color: #eee7d6;
  border-bottom: 1px solid rgba(210, 191, 149, 0.18);
  padding: 5px 0;
}

.leader-list li strong {
  color: #f3d48d;
  justify-self: end;
}

.leader-list li span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leader-list li em {
  color: #c5bea9;
  font-style: normal;
  justify-self: end;
}

.hidden {
  display: none;
}

.stage-wrap {
  position: relative;
  min-height: 0;
  border-radius: 16px;
  border: 1px solid rgba(201, 182, 140, 0.25);
  overflow: hidden;
  background: #0f151c;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.02), 0 12px 28px rgba(0, 0, 0, 0.3);
}

.status-ticker {
  position: absolute;
  top: 8px;
  left: 10px;
  right: 10px;
  z-index: 6;
  pointer-events: none;
  text-align: center;
  font-size: 12px;
  line-height: 1.35;
  color: #f2ead2;
  background: linear-gradient(180deg, rgba(8, 12, 18, 0.86), rgba(8, 12, 18, 0.38));
  border: 1px solid rgba(214, 194, 149, 0.32);
  border-radius: 10px;
  padding: 4px 8px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  opacity: 0.9;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status-ticker.good {
  color: #c9f1d2;
}

.status-ticker.bad {
  color: #ffd1d1;
}

.status-ticker.flash {
  animation: tickerFlash 0.45s ease-out;
}

@keyframes tickerFlash {
  0% { transform: translateY(-4px); opacity: 0.2; }
  100% { transform: translateY(0); opacity: 0.95; }
}

#stage {
  width: 100%;
  height: 100%;
  display: block;
}

.controls-hud {
  padding: 6px;
  display: grid;
  gap: 5px;
}

.move-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}

.skill-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.control {
  border: 1px solid rgba(224, 203, 158, 0.46);
  border-radius: 12px;
  color: #f7f2e4;
  padding: 8px 5px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  display: grid;
  place-items: center;
  touch-action: manipulation;
}

.control small {
  margin-top: 2px;
  font-size: 10px;
  color: #d7d2c2;
  font-weight: 500;
}

.control:active {
  transform: translateY(1px) scale(0.99);
}

.control:disabled {
  cursor: not-allowed;
  opacity: 0.45;
  filter: grayscale(0.35);
  transform: none;
}

.control-move { background: linear-gradient(180deg, rgba(80, 104, 122, 0.56), rgba(42, 58, 70, 0.74)); }
.control-boost { background: linear-gradient(180deg, rgba(146, 132, 86, 0.6), rgba(87, 73, 45, 0.78)); }
.control-cover { background: linear-gradient(180deg, rgba(162, 135, 78, 0.58), rgba(95, 75, 41, 0.76)); }
.control-soothe { background: linear-gradient(180deg, rgba(109, 140, 106, 0.56), rgba(58, 84, 56, 0.74)); }

.action-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.top-actions {
  grid-template-columns: 1fr 1fr 1fr auto auto auto;
  align-items: center;
}

.top-actions .mini-btn {
  height: 24px;
  line-height: 1;
  white-space: nowrap;
}

.action-btn {
  border: 1px solid rgba(211, 188, 141, 0.45);
  border-radius: 8px;
  background: rgba(39, 46, 56, 0.75);
  color: #efe9dc;
  padding: 4px;
  font-weight: 700;
  font-size: 11px;
  cursor: pointer;
  touch-action: manipulation;
}

.action-btn.primary {
  background: linear-gradient(180deg, #d8ba76, #a88641);
  color: #21190f;
}

.action-btn.danger {
  background: linear-gradient(180deg, rgba(150, 84, 84, 0.82), rgba(97, 48, 48, 0.9));
  color: #f4e8e8;
}

@media (max-height: 860px) {
  .game-shell {
    gap: 2px;
    padding: max(3px, env(safe-area-inset-top)) 6px max(4px, env(safe-area-inset-bottom)) 6px;
  }

  .top-hud {
    padding: 2px 5px;
  }

  .title-wrap h1 {
    font-size: 12px;
  }

  .title-wrap p {
    display: none;
  }

  #playerNameText {
    max-width: 86px;
  }

  .bars-hud {
    padding: 2px 5px;
    gap: 1px;
  }

  .action-hud {
    padding: 2px 5px;
  }

  .hint {
    display: none;
  }

  .controls-hud {
    padding: 5px;
    gap: 4px;
  }

  .control {
    padding: 7px 4px;
    font-size: 12px;
  }

  .control small {
    font-size: 9px;
  }

  .action-btn {
    padding: 3px;
    font-size: 10px;
  }

  .status-ticker {
    top: 6px;
    left: 8px;
    right: 8px;
    font-size: 11px;
    padding: 3px 7px;
  }

  .top-actions .mini-btn {
    height: 22px;
    padding: 2px 6px;
    font-size: 9px;
  }

  .leaderboard-panel {
    padding: 9px 10px;
    gap: 5px;
    max-height: min(74vh, 640px);
  }

  .leader-list {
    max-height: min(50vh, 460px);
  }

  .leader-list li {
    font-size: 10px;
    padding: 4px 0;
  }
}

@media (max-height: 740px) {
  .game-shell {
    grid-template-rows: auto auto auto minmax(0, 1fr) auto;
    gap: 2px;
    padding: 2px 5px max(4px, env(safe-area-inset-bottom));
  }

  .title-wrap h1 {
    font-size: 11px;
  }

  .metric {
    font-size: 8px;
  }

  .metric strong {
    font-size: 10px;
  }

  #courageText {
    font-size: 19px;
  }

  progress {
    height: 4px;
  }

  .status-row {
    font-size: 8px;
  }

  .hint {
    display: none;
  }

  .control {
    padding: 6px 4px;
    font-size: 11px;
  }

  .control small {
    font-size: 9px;
  }

  .help-panel p {
    font-size: 11px;
  }

  .leaderboard-panel {
    left: 8px;
    right: 8px;
  }

  .leader-list li {
    grid-template-columns: minmax(62px, 1.15fr) minmax(58px, 0.9fr) minmax(54px, 0.8fr);
    gap: 5px;
    font-size: 10px;
  }

  .status-ticker {
    font-size: 10px;
    padding: 3px 6px;
  }

  .top-actions {
    grid-template-columns: 1fr 1fr 1fr;
  }
}

@media (min-width: 721px) {
  .game-shell {
    border-left: 1px solid rgba(255, 255, 255, 0.08);
    border-right: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 14px;
  }
}
