:root {
  --font: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", system-ui, -apple-system, sans-serif;
  --radius-lg: 32px;
  --radius-md: 22px;
  --page-bg: #eaf4ff;
  --page-bg-2: #fdf3e3;
  --surface: rgba(255, 255, 255, 0.78);
  --surface-solid: #ffffff;
  --text: #26324d;
  --text-soft: #5d6c8b;
  --line: rgba(78, 102, 150, 0.16);
  --primary: #4c8dff;
  --primary-deep: #2d63da;
  --accent: #ffb52e;
  --btn-bg: linear-gradient(180deg, #ffffff, #eef3ff);
  --btn-shadow: 0 1px 0 #ffffff inset, 0 6px 14px -8px rgba(50, 80, 150, 0.6), 0 0 0 1px rgba(78, 102, 150, 0.14);
  --input-bg: #f7faff;
  --shadow-lg: 0 30px 60px -30px rgba(58, 88, 150, 0.5);
  --shadow-sm: 0 10px 24px -16px rgba(58, 88, 150, 0.65);
  color-scheme: light;
}

html[data-theme="night"] {
  --page-bg: #070b1e;
  --page-bg-2: #121a3a;
  --surface: rgba(22, 30, 62, 0.78);
  --surface-solid: #161e3e;
  --text: #eaf0ff;
  --text-soft: #9aabd0;
  --line: rgba(160, 190, 255, 0.16);
  --primary: #6fa8ff;
  --primary-deep: #3f77e0;
  --accent: #ffd166;
  --btn-bg: linear-gradient(180deg, #2a3763, #1c2649);
  --btn-shadow: 0 1px 0 rgba(255, 255, 255, 0.12) inset, 0 8px 18px -10px rgba(0, 0, 0, 0.8), 0 0 0 1px rgba(160, 190, 255, 0.16);
  --input-bg: #101735;
  --shadow-lg: 0 34px 70px -30px rgba(0, 0, 0, 0.85);
  --shadow-sm: 0 12px 26px -18px rgba(0, 0, 0, 0.9);
  color-scheme: dark;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  padding: 30px 20px 44px;
  display: flex;
  justify-content: center;
  font-family: var(--font);
  color: var(--text);
  background:
    radial-gradient(1100px 560px at 12% -12%, rgba(255, 214, 140, 0.5), transparent 62%),
    radial-gradient(980px 640px at 106% -4%, rgba(140, 200, 255, 0.55), transparent 60%),
    linear-gradient(160deg, var(--page-bg), var(--page-bg-2));
  background-attachment: fixed;
  transition: background 0.6s ease, color 0.4s ease;
  -webkit-font-smoothing: antialiased;
}

html[data-theme="night"] body {
  background:
    radial-gradient(1000px 560px at 10% -10%, rgba(90, 70, 190, 0.38), transparent 62%),
    radial-gradient(900px 600px at 104% -6%, rgba(40, 90, 180, 0.34), transparent 60%),
    linear-gradient(160deg, var(--page-bg), var(--page-bg-2));
}

.app {
  width: min(1040px, 100%);
  display: flex;
  flex-direction: column;
  gap: 18px;
}

/* ---------- header ---------- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-star {
  flex: none;
  width: 52px;
  height: 52px;
  border-radius: 16px;
  background: linear-gradient(150deg, #fff6bd, #ffcb3d 46%, #f39a12);
  clip-path: polygon(50% 2%, 61% 36%, 98% 36%, 68% 58%, 79% 95%, 50% 72%, 21% 95%, 32% 58%, 2% 36%, 39% 36%);
  filter: drop-shadow(0 8px 10px rgba(243, 154, 18, 0.4));
  transform: rotate(-6deg);
}

.brand-text h1 {
  margin: 0;
  font-size: clamp(28px, 3.4vw, 40px);
  font-weight: 800;
  letter-spacing: 0.02em;
  background: linear-gradient(118deg, #2f6bff, #8a4cff 52%, #ff7a59);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

html[data-theme="night"] .brand-text h1 {
  background-image: linear-gradient(118deg, #9cc8ff, #d4b5ff 52%, #ffd8a8);
}

.tagline {
  margin: 4px 0 0;
  font-size: 14px;
  color: var(--text-soft);
}

.theme-switch {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 5px 6px 5px 12px;
  border-radius: 999px;
  background: var(--surface);
  box-shadow: var(--shadow-sm), 0 0 0 1px var(--line);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.theme-switch-label {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: var(--text-soft);
  margin-right: 4px;
}

.theme-switch button {
  border: 0;
  border-radius: 999px;
  padding: 8px 13px;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-soft);
  background: transparent;
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
}

.theme-switch button:hover {
  color: var(--text);
  background: rgba(76, 141, 255, 0.1);
}

.theme-switch button.is-active {
  color: #fff;
  background: linear-gradient(165deg, #7cb1ff, var(--primary) 55%, var(--primary-deep));
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.45) inset, 0 6px 12px -6px rgba(45, 99, 218, 0.85);
}

/* ---------- HUD ---------- */
.hud {
  display: flex;
  align-items: center;
  gap: 26px;
  flex-wrap: wrap;
  padding: 12px 20px;
  border-radius: var(--radius-md);
  background: var(--surface);
  box-shadow: var(--shadow-sm), 0 0 0 1px var(--line);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

.stat {
  display: flex;
  align-items: center;
  gap: 12px;
}

.stat-label {
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.18em;
  color: var(--text-soft);
}

.stat-value {
  display: inline-block;
  min-width: 1.5em;
  font-size: 32px;
  line-height: 1;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.stat-value.pop {
  animation: scorePop 0.34s cubic-bezier(0.2, 1.4, 0.4, 1);
}

.level-value {
  min-width: 1.2em;
  text-align: center;
  font-size: 28px;
  color: var(--primary);
}

@keyframes scorePop {
  0% { transform: scale(1); }
  45% { transform: scale(1.28); color: var(--accent); }
  100% { transform: scale(1); }
}

.lives {
  display: flex;
  align-items: center;
  gap: 7px;
}

.heart {
  width: 24px;
  height: 22px;
  background: linear-gradient(160deg, #ffa2b6, #f2456a 58%, #c81e4c);
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 22'%3E%3Cpath d='M12 21s-9.5-5.9-9.5-12A5.6 5.6 0 0 1 12 5.6 5.6 5.6 0 0 1 21.5 9c0 6.1-9.5 12-9.5 12z'/%3E%3C/svg%3E") center / contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 22'%3E%3Cpath d='M12 21s-9.5-5.9-9.5-12A5.6 5.6 0 0 1 12 5.6 5.6 5.6 0 0 1 21.5 9c0 6.1-9.5 12-9.5 12z'/%3E%3C/svg%3E") center / contain no-repeat;
  filter: drop-shadow(0 3px 3px rgba(200, 30, 76, 0.35));
  transition: background 0.25s ease, filter 0.25s ease, transform 0.25s ease;
}

.heart.lost {
  background: rgba(125, 138, 165, 0.35);
  filter: none;
  transform: scale(0.86);
}

html[data-theme="night"] .heart.lost {
  background: rgba(150, 170, 210, 0.22);
}

/* ---------- buttons ---------- */
.btn {
  padding: 11px 19px;
  border: 0;
  border-radius: 14px;
  font: inherit;
  font-size: 15px;
  font-weight: 800;
  color: var(--text);
  background: var(--btn-bg);
  box-shadow: var(--btn-shadow);
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.2s ease, background 0.2s ease;
  white-space: nowrap;
}

.btn:hover {
  transform: translateY(-1px);
}

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

.btn:focus-visible {
  outline: 3px solid rgba(76, 141, 255, 0.45);
  outline-offset: 2px;
}

.btn-primary {
  color: #fff;
  background: linear-gradient(168deg, #82b6ff, #4c8dff 52%, #2d63da);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.5) inset, 0 12px 22px -12px rgba(45, 99, 218, 0.95), 0 0 0 1px rgba(45, 99, 218, 0.25);
}

html[data-theme="night"] .btn-primary {
  background: linear-gradient(168deg, #7fb4ff, #4a86f0 52%, #2f5fc8);
}

.btn-ghost {
  color: var(--text);
}

.btn-lg {
  padding: 14px 34px;
  font-size: 17px;
  border-radius: 16px;
}

.hud-actions {
  margin-left: auto;
  display: flex;
  gap: 10px;
}

/* ---------- stage ---------- */
.stage {
  position: relative;
  height: clamp(390px, 58vh, 600px);
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: linear-gradient(180deg, #b8e6ff 0%, #daf3ff 42%, #fff6df 100%);
  box-shadow: var(--shadow-lg), 0 0 0 1px var(--line), inset 0 1px 0 rgba(255, 255, 255, 0.8);
  transition: background 0.6s ease, box-shadow 0.4s ease;
  user-select: none;
  -webkit-user-select: none;
  isolation: isolate;
}

html[data-theme="night"] .stage {
  background: linear-gradient(180deg, #070b24 0%, #121a4a 52%, #2a2360 100%);
  box-shadow: var(--shadow-lg), 0 0 0 1px var(--line), inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

.stage.is-hit {
  animation: stageShake 0.36s ease;
}

@keyframes stageShake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-7px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(3px); }
}

#game {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  z-index: 2;
  cursor: grab;
  touch-action: none;
}

.stage.is-dragging #game {
  cursor: grabbing;
}

.sky {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 1;
}

.sun,
.moon {
  position: absolute;
  border-radius: 50%;
  transition: opacity 0.7s ease, transform 0.7s ease;
}

.sun {
  right: 7%;
  top: 8%;
  width: clamp(64px, 9vw, 96px);
  aspect-ratio: 1;
  background: radial-gradient(circle at 34% 30%, #fffdf0, #ffe27a 44%, #ffb830 74%, #ff9a1f);
  box-shadow: 0 0 70px 22px rgba(255, 214, 92, 0.55), inset -10px -12px 22px rgba(240, 140, 10, 0.3);
  opacity: 1;
}

.moon {
  right: 9%;
  top: 7%;
  width: clamp(54px, 7.5vw, 78px);
  aspect-ratio: 1;
  background: radial-gradient(circle at 34% 30%, #fffef8, #f3f0e2 55%, #d9d4c4);
  box-shadow: 0 0 54px 14px rgba(190, 215, 255, 0.32), inset -7px -9px 18px rgba(120, 130, 170, 0.32);
  opacity: 0;
  transform: translateY(-14px) scale(0.9);
}

.moon::before,
.moon::after {
  content: "";
  position: absolute;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #e6e2d2, #c8c3b2);
  box-shadow: inset 1px 2px 3px rgba(110, 115, 135, 0.45);
}

.moon::before {
  width: 22%;
  height: 22%;
  left: 22%;
  top: 30%;
}

.moon::after {
  width: 14%;
  height: 14%;
  right: 24%;
  bottom: 26%;
}

html[data-theme="night"] .sun {
  opacity: 0;
  transform: translateY(-14px) scale(0.9);
}

html[data-theme="night"] .moon {
  opacity: 1;
  transform: translateY(0) scale(1);
}

.cloud {
  position: absolute;
  width: 140px;
  height: 44px;
  border-radius: 100px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow:
    34px 7px 0 -5px rgba(255, 255, 255, 0.92),
    68px 2px 0 -11px rgba(255, 255, 255, 0.88);
  opacity: 0.9;
  animation: drift 22s ease-in-out infinite alternate;
  transition: opacity 0.6s ease, background 0.6s ease;
}

.cloud-1 {
  left: 6%;
  top: 17%;
}

.cloud-2 {
  left: 56%;
  top: 8%;
  transform: scale(0.78);
  opacity: 0.7;
  animation-duration: 28s;
  animation-delay: -6s;
}

.cloud-3 {
  left: 30%;
  top: 36%;
  transform: scale(0.58);
  opacity: 0.55;
  animation-duration: 34s;
  animation-delay: -14s;
}

html[data-theme="night"] .cloud {
  background: rgba(160, 185, 255, 0.24);
  box-shadow:
    34px 7px 0 -5px rgba(160, 185, 255, 0.24),
    68px 2px 0 -11px rgba(160, 185, 255, 0.2);
  opacity: 0.75;
}

@keyframes drift {
  from { translate: -22px 0; }
  to { translate: 26px 0; }
}

.sky-stars {
  position: absolute;
  inset: 0;
  opacity: 0;
  transition: opacity 0.8s ease;
}

html[data-theme="night"] .sky-stars {
  opacity: 1;
}

.sky-dot {
  position: absolute;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 7px 1px rgba(180, 210, 255, 0.85);
  animation: twinkle 3.4s ease-in-out infinite;
}

@keyframes twinkle {
  0%, 100% { opacity: 0.25; transform: scale(0.85); }
  50% { opacity: 1; transform: scale(1.1); }
}

.hit-flash {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
  opacity: 0;
  background: radial-gradient(ellipse at 50% 60%, transparent 40%, rgba(255, 80, 120, 0.55) 100%);
  transition: opacity 0.4s ease;
}

.stage.is-hit .hit-flash {
  opacity: 1;
  transition: opacity 0.06s ease;
}

/* ---------- overlays ---------- */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  overflow: auto;
  background: rgba(238, 248, 255, 0.52);
  backdrop-filter: blur(9px);
  -webkit-backdrop-filter: blur(9px);
  animation: overlayIn 0.25s ease;
}

html[data-theme="night"] .overlay {
  background: rgba(6, 9, 28, 0.6);
}

.overlay.modal {
  z-index: 7;
}

@keyframes overlayIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.panel {
  width: min(470px, 100%);
  margin: auto;
  padding: 26px 28px 24px;
  text-align: center;
  border-radius: 26px;
  background: var(--surface-solid);
  box-shadow: 0 34px 70px -32px rgba(20, 40, 90, 0.65), 0 0 0 1px var(--line);
  animation: panelIn 0.3s cubic-bezier(0.2, 0.9, 0.3, 1.15);
  transition: background 0.5s ease;
}

@keyframes panelIn {
  from { opacity: 0; transform: translateY(10px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.panel h2 {
  margin: 0 0 8px;
  font-size: 25px;
  font-weight: 800;
  letter-spacing: 0.01em;
}

.panel-sub {
  margin: 0 0 16px;
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-soft);
}

.rules {
  list-style: none;
  margin: 0 0 16px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 9px;
  text-align: left;
}

.rules li {
  display: flex;
  align-items: center;
  gap: 11px;
  font-size: 14.5px;
  line-height: 1.5;
}

.rule-key {
  flex: none;
  min-width: 54px;
  padding: 5px 9px;
  border-radius: 9px;
  text-align: center;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--text);
  background: var(--btn-bg);
  box-shadow: 0 2px 0 rgba(78, 102, 150, 0.22), 0 0 0 1px var(--line);
}

.rule-tag {
  flex: none;
  min-width: 54px;
  padding: 5px 9px;
  border-radius: 9px;
  text-align: center;
  font-size: 13px;
  font-weight: 800;
  color: #fff;
}

.rule-tag.is-gain {
  background: linear-gradient(160deg, #ffd76a, #f5a623);
  box-shadow: 0 4px 10px -5px rgba(245, 166, 35, 0.9);
}

.rule-tag.is-loss {
  background: linear-gradient(160deg, #ff9db1, #ef4d70);
  box-shadow: 0 4px 10px -5px rgba(239, 77, 112, 0.9);
}

.rule-tag.is-time {
  background: linear-gradient(160deg, #8fc0ff, #4c8dff);
  box-shadow: 0 4px 10px -5px rgba(76, 141, 255, 0.9);
}

.panel-note {
  margin: 0 0 18px;
  font-size: 13px;
  color: var(--text-soft);
}

.final-line {
  margin: 6px 0 20px;
  font-size: 16px;
  color: var(--text-soft);
}

.final-line strong {
  font-size: 30px;
  font-weight: 800;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  margin: 0 4px;
}

.over-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

.record-tip {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 700;
  color: var(--primary);
}

.name-form {
  margin: 0 0 16px;
}

.name-row {
  display: flex;
  gap: 9px;
}

.name-row input {
  flex: 1;
  min-width: 0;
  padding: 12px 14px;
  border-radius: 13px;
  border: 1px solid var(--line);
  background: var(--input-bg);
  color: var(--text);
  font: inherit;
  font-size: 15px;
  outline: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.name-row input::placeholder {
  color: var(--text-soft);
  opacity: 0.8;
}

.name-row input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(76, 141, 255, 0.24);
}

.board-list {
  list-style: none;
  margin: 4px 0 20px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 264px;
  overflow: auto;
}

.board-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-radius: 14px;
  background: linear-gradient(120deg, rgba(76, 141, 255, 0.08), rgba(255, 181, 46, 0.12));
  border: 1px solid var(--line);
}

.rank {
  flex: none;
  width: 27px;
  height: 27px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  font-size: 13px;
  font-weight: 800;
  color: var(--text-soft);
  background: rgba(125, 138, 165, 0.16);
}

.rank-gold {
  color: #7a4a00;
  background: linear-gradient(160deg, #ffeaa8, #ffc02e);
  box-shadow: 0 4px 9px -4px rgba(240, 160, 10, 0.85);
}

.rank-silver {
  color: #4c5568;
  background: linear-gradient(160deg, #f4f7fb, #c6d2e2);
  box-shadow: 0 4px 9px -5px rgba(120, 140, 170, 0.8);
}

.rank-bronze {
  color: #5f3516;
  background: linear-gradient(160deg, #ffdcb8, #d79356);
  box-shadow: 0 4px 9px -5px rgba(200, 120, 50, 0.75);
}

.board-name {
  flex: 1;
  min-width: 0;
  text-align: left;
  font-weight: 700;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.board-score {
  flex: none;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.board-empty {
  padding: 22px 10px;
  font-size: 14px;
  color: var(--text-soft);
  border-radius: 14px;
  border: 1px dashed var(--line);
}

/* ---------- help ---------- */
.help {
  padding: 2px 6px;
  color: var(--text-soft);
}

.help h2 {
  margin: 0 0 6px;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.18em;
  color: var(--text);
}

.help p {
  margin: 0;
  font-size: 13.5px;
  line-height: 1.75;
}

kbd {
  display: inline-block;
  padding: 1px 7px;
  margin: 0 1px;
  border-radius: 6px;
  font: inherit;
  font-size: 12px;
  font-weight: 800;
  color: var(--text);
  background: var(--btn-bg);
  box-shadow: 0 2px 0 rgba(78, 102, 150, 0.25), 0 0 0 1px var(--line);
}

/* ---------- responsive ---------- */
@media (max-width: 780px) {
  body {
    padding: 20px 14px 32px;
  }

  .app {
    gap: 14px;
  }

  .app-header {
    gap: 12px;
  }

  .theme-switch {
    padding-left: 8px;
  }

  .theme-switch-label {
    display: none;
  }

  .hud {
    gap: 14px 20px;
    padding: 12px 16px;
  }

  .stat-value {
    font-size: 27px;
  }

  .hud-actions {
    width: 100%;
    margin-left: 0;
  }

  .hud-actions .btn {
    flex: 1;
  }

  .stage {
    border-radius: 24px;
    height: clamp(360px, 60vh, 520px);
  }

  .panel {
    padding: 22px 20px 20px;
  }

  .rules li {
    font-size: 13.5px;
  }

  .cloud {
    transform: scale(0.72);
  }

  .cloud-2 {
    transform: scale(0.58);
  }

  .cloud-3 {
    transform: scale(0.44);
  }
}

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