:root {
  --sky-a: #8fd4f0;
  --sky-b: #c9ecff;
  --leaf-a: #3f9e4a;
  --leaf-b: #7cc85f;
  --ink: #2b3a3f;
  --panel: rgba(255, 255, 255, 0.9);
  --gold: #ffcf5c;
  --accent: #ff7a6b;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--sky-b);
  font-family: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", system-ui, sans-serif;
  color: var(--ink);
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

.game-root {
  position: relative;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  height: 100svh;
  overflow: hidden;
  touch-action: none;
}

canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

/* ---------- HUD ---------- */
.hud {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: calc(12px + env(safe-area-inset-top)) calc(14px + env(safe-area-inset-left)) 0
    calc(14px + env(safe-area-inset-right));
  pointer-events: none;
  z-index: 5;
}

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

.hearts {
  display: flex;
  gap: 6px;
}

.heart {
  width: 22px;
  height: 22px;
  background: #fff;
  border-radius: 50% 50% 50% 50%/60% 60% 40% 40%;
  position: relative;
  transform: rotate(45deg);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.18);
  transition: opacity 0.2s, transform 0.2s;
}

.heart::before,
.heart::after {
  content: "";
  position: absolute;
  width: 22px;
  height: 22px;
  background: #fff;
  border-radius: 50%;
}

.heart::before {
  left: -11px;
}

.heart::after {
  top: -11px;
}

.heart.full {
  background: var(--accent);
}

.heart.full::before,
.heart.full::after {
  background: var(--accent);
}

.heart.empty {
  opacity: 0.28;
}

.scoreboard {
  display: flex;
  align-items: baseline;
  gap: 6px;
  background: var(--panel);
  padding: 6px 12px;
  border-radius: 999px;
  font-weight: 700;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.12);
}

.score-label {
  font-size: 12px;
  opacity: 0.75;
}

.score {
  font-size: 18px;
  color: #24607a;
}

.progress-track {
  margin-top: 10px;
  height: 8px;
  background: rgba(255, 255, 255, 0.5);
  border: 1px solid rgba(43, 58, 63, 0.18);
  border-radius: 999px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--leaf-a), var(--leaf-b));
  border-radius: 999px;
  transition: width 0.15s linear;
}

.progress-bar.enraged {
  background: linear-gradient(90deg, #ff5a3a, #ff2b1a);
  animation: hp-pulse 0.5s ease-in-out infinite;
}

@keyframes hp-pulse {
  0%, 100% { filter: brightness(1); }
  50% { filter: brightness(1.45); }
}

.level-tag {
  position: absolute;
  top: calc(12px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(255, 255, 255, 0.9);
  color: #2d7a3a;
  font-weight: 800;
  font-size: 14px;
  padding: 6px 14px;
  border-radius: 999px;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.12);
  letter-spacing: 0.5px;
}

.ctl-action {
  width: 94px;
  height: 94px;
  background: radial-gradient(circle at 35% 30%, #ffe07a, #ff9c43 58%, #f57b1f);
  border: 3px solid #fff;
  box-shadow: 0 0 0 4px rgba(245, 123, 31, 0.45), 0 6px 18px rgba(245, 123, 31, 0.6), 0 0 26px rgba(255, 170, 60, 0.85);
  animation: attack-pulse 1.3s ease-in-out infinite;
}

.ctl-action .act-ico {
  font-size: 40px;
  font-weight: 900;
  color: #111;
  text-shadow: none;
}

@keyframes attack-pulse {
  0%,
  100% {
    transform: scale(1);
    box-shadow: 0 0 0 4px rgba(245, 123, 31, 0.45), 0 6px 18px rgba(245, 123, 31, 0.6), 0 0 20px rgba(255, 170, 60, 0.7);
  }
  50% {
    transform: scale(1.07);
    box-shadow: 0 0 0 6px rgba(245, 123, 31, 0.6), 0 6px 22px rgba(245, 123, 31, 0.7), 0 0 34px rgba(255, 170, 60, 1);
  }
}

.mini-btn {
  position: absolute;
  top: calc(12px + env(safe-area-inset-top));
  right: calc(96px + env(safe-area-inset-right));
  background: rgba(255, 255, 255, 0.92);
  color: #2d7a3a;
  font-weight: 800;
  font-size: 13px;
  border: 1px solid rgba(45, 122, 58, 0.4);
  border-radius: 999px;
  padding: 6px 14px;
  cursor: pointer;
  z-index: 6;
}

.primary.secondary {
  background: rgba(45, 122, 58, 0.12);
  color: #2d7a3a;
  box-shadow: none;
  margin-top: 10px;
}

.level-btn {
  display: block;
  width: 100%;
  margin: 0 0 10px;
  padding: 13px;
  font-size: 15px;
  font-weight: 700;
  color: #2d7a3a;
  background: rgba(45, 122, 58, 0.1);
  border: 1px solid rgba(45, 122, 58, 0.35);
  border-radius: 12px;
  cursor: pointer;
}

.level-btn:active {
  background: rgba(45, 122, 58, 0.22);
}

.height-flag {
  position: absolute;
  top: calc(74px + env(safe-area-inset-top));
  right: calc(14px + env(safe-area-inset-right));
  background: linear-gradient(135deg, #ffd97a, #ffb653);
  color: #6a4a00;
  font-weight: 800;
  font-size: 13px;
  padding: 5px 12px;
  border-radius: 999px;
  box-shadow: 0 4px 10px rgba(255, 170, 40, 0.4);
  animation: pop 0.4s ease;
}

@keyframes pop {
  0% {
    transform: scale(0.6);
    opacity: 0;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

/* ---------- Dialogue ---------- */
.dialogue {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  top: calc(118px + env(safe-area-inset-top));
  display: flex;
  align-items: flex-start;
  gap: 10px;
  max-width: min(86%, 540px);
  z-index: 6;
  pointer-events: none;
  animation: dlg-in 0.3s ease;
}

@keyframes dlg-in {
  from {
    opacity: 0;
    transform: translate(-50%, -12px);
  }
  to {
    opacity: 1;
    transform: translate(-50%, 0);
  }
}

.dlg-avatar {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  object-fit: contain;
  background: #fff;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.18);
  border: 2px solid rgba(255, 255, 255, 0.9);
}

.dlg-bubble {
  position: relative;
  background: var(--panel);
  padding: 10px 14px;
  border-radius: 16px 16px 16px 4px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.16);
  font-size: 15px;
  line-height: 1.5;
}

.dlg-bubble::before {
  content: "";
  position: absolute;
  left: -9px;
  bottom: 12px;
  width: 0;
  height: 0;
  border-top: 8px solid transparent;
  border-bottom: 8px solid transparent;
  border-right: 9px solid var(--panel);
}

#dlg-name {
  display: inline-block;
  font-weight: 800;
  color: #2d7a3a;
  margin-bottom: 2px;
}

#dlg-name.other {
  color: #b23a3a;
}

#dlg-text {
  display: block;
}

/* ---------- Touch controls ---------- */
.touch-controls {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: space-between;
  padding: 0 calc(18px + env(safe-area-inset-left)) calc(18px + env(safe-area-inset-bottom))
    calc(18px + env(safe-area-inset-right));
  z-index: 6;
  pointer-events: none;
}

.ctl-btn {
  pointer-events: auto;
  width: 74px;
  height: 74px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.32);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 62px);
  grid-template-rows: repeat(2, 62px);
  gap: 6px;
  pointer-events: none;
}

.dpad .ctl-btn {
  width: 62px;
  height: 62px;
  pointer-events: auto;
}

.dp-up { grid-column: 2; grid-row: 1; }
.dp-left { grid-column: 1; grid-row: 2; }
.dp-down { grid-column: 2; grid-row: 2; }
.dp-right { grid-column: 3; grid-row: 2; }

#btn-up .chev { transform: rotate(135deg); }
#btn-down .chev { transform: rotate(-45deg); }

.ctl-btn:active {
  background: rgba(255, 255, 255, 0.6);
}

.ctl-btn .chev {
  width: 18px;
  height: 18px;
  border-left: 5px solid #2b3a3f;
  border-bottom: 5px solid #2b3a3f;
}

#btn-left .chev {
  transform: rotate(45deg);
}

#btn-right .chev {
  transform: rotate(225deg);
}

/* ---------- Screens ---------- */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 10;
  background: linear-gradient(160deg, rgba(143, 212, 240, 0.92), rgba(201, 236, 255, 0.94));
  overflow: auto;
}

.screen[hidden] {
  display: none;
}

.panel {
  width: min(92%, 460px);
  background: var(--panel);
  backdrop-filter: blur(6px);
  border-radius: 22px;
  padding: 26px 26px 28px;
  box-shadow: 0 12px 40px rgba(30, 60, 80, 0.25);
  text-align: center;
}

.title {
  margin: 0 0 6px;
  font-size: 28px;
  letter-spacing: 1px;
  color: #1f5f3a;
}

.subtitle {
  margin: 0 0 18px;
  font-size: 15px;
  opacity: 0.82;
}

.hints {
  list-style: none;
  padding: 0;
  margin: 0 0 22px;
  text-align: left;
  font-size: 14px;
  line-height: 2;
}

.hints li {
  display: flex;
  align-items: center;
  gap: 8px;
}

.bean-dot {
  display: inline-block;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffe28a, #f7b733);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.primary {
  width: 100%;
  padding: 14px;
  font-size: 17px;
  font-weight: 800;
  color: #fff;
  border: none;
  border-radius: 14px;
  background: linear-gradient(135deg, #4fb45a, #2d8a41);
  box-shadow: 0 6px 16px rgba(45, 138, 65, 0.4);
  cursor: pointer;
}

.primary:active {
  transform: translateY(1px);
}

.skip-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px dashed rgba(43, 58, 63, 0.2);
}

.skip-label {
  font-size: 12px;
  opacity: 0.6;
}

.skip-btn {
  font-size: 13px;
  font-weight: 700;
  color: #2d7a3a;
  background: rgba(45, 122, 58, 0.1);
  border: 1px solid rgba(45, 122, 58, 0.35);
  border-radius: 999px;
  padding: 6px 14px;
  cursor: pointer;
}

.skip-btn:active {
  background: rgba(45, 122, 58, 0.2);
}

.star-summary {
  margin: 0 0 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: left;
}

.star-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: rgba(0, 0, 0, 0.05);
  border-radius: 12px;
  padding: 8px 14px;
}

.star-label {
  font-size: 14px;
  font-weight: 700;
  color: #2b3a3f;
}

.stars {
  font-size: 20px;
  letter-spacing: 2px;
  color: #ffc94d;
}

.stars.good {
  text-shadow: 0 0 10px rgba(255, 190, 60, 0.7);
}

.end-stats {
  display: flex;
  gap: 10px;
  margin: 0 0 22px;
}

.stat {
  flex: 1;
  background: rgba(0, 0, 0, 0.05);
  border-radius: 12px;
  padding: 12px 6px;
}

.stat-label {
  display: block;
  font-size: 12px;
  opacity: 0.7;
  margin-bottom: 4px;
}

.stat span:last-child {
  font-size: 22px;
  font-weight: 800;
  color: #24607a;
}

@media (min-height: 500px) and (max-height: 700px) {
  .panel {
    padding: 20px 22px 22px;
  }
}
