:root {
  --bg-page: #f4efe7;
  --bg-radial-1: rgba(214, 174, 118, 0.22);
  --bg-radial-2: rgba(154, 178, 168, 0.2);
  --surface: rgba(255, 252, 246, 0.86);
  --surface-solid: #fffcf6;
  --surface-elevated: #ffffff;
  --border: rgba(112, 86, 58, 0.14);
  --border-strong: rgba(112, 86, 58, 0.24);
  --text-primary: #2a2118;
  --text-secondary: #6e5d4b;
  --text-muted: #988775;

  --accent: #b86532;
  --accent-soft: rgba(184, 101, 50, 0.12);
  --accent-glow: rgba(184, 101, 50, 0.34);
  --valid: #2c8a62;
  --valid-soft: rgba(44, 138, 98, 0.14);
  --danger: #c64b4b;
  --danger-soft: rgba(198, 75, 75, 0.14);

  --wood-top: linear-gradient(180deg, #b8895a 0%, #996a3f 100%);
  --wood-front: linear-gradient(180deg, #7c512c 0%, #58361b 100%);
  --peg-rod: linear-gradient(90deg, #8e6b4a 0%, #d2ab7d 34%, #b18456 68%, #6d4c2f 100%);
  --peg-collar: linear-gradient(180deg, #d8b587 0%, #997043 100%);

  --shadow-card: 0 18px 46px -18px rgba(56, 38, 18, 0.18), 0 4px 14px -4px rgba(56, 38, 18, 0.08);
  --shadow-btn: 0 2px 6px rgba(56, 38, 18, 0.06);
  --radius-lg: 22px;
  --radius-md: 14px;
  --radius-sm: 10px;
}

html[data-theme="dark"] {
  --bg-page: #13151a;
  --bg-radial-1: rgba(209, 139, 73, 0.14);
  --bg-radial-2: rgba(74, 128, 145, 0.14);
  --surface: rgba(25, 29, 36, 0.86);
  --surface-solid: #1a1e26;
  --surface-elevated: #232833;
  --border: rgba(255, 240, 219, 0.1);
  --border-strong: rgba(255, 240, 219, 0.2);
  --text-primary: #f2ece2;
  --text-secondary: #b4aba0;
  --text-muted: #7c756c;

  --accent: #e08d4d;
  --accent-soft: rgba(224, 141, 77, 0.16);
  --accent-glow: rgba(224, 141, 77, 0.36);
  --valid: #45b988;
  --valid-soft: rgba(69, 185, 136, 0.16);
  --danger: #e46c6c;
  --danger-soft: rgba(228, 108, 108, 0.16);

  --wood-top: linear-gradient(180deg, #524237 0%, #3a2e26 100%);
  --wood-front: linear-gradient(180deg, #2b221c 0%, #1b1512 100%);
  --peg-rod: linear-gradient(90deg, #645342 0%, #c8a273 35%, #997449 68%, #4d3d2f 100%);
  --peg-collar: linear-gradient(180deg, #b89161 0%, #6d5031 100%);

  --shadow-card: 0 24px 56px -18px rgba(0, 0, 0, 0.55), 0 6px 18px -4px rgba(0, 0, 0, 0.35);
  --shadow-btn: 0 2px 8px rgba(0, 0, 0, 0.28);
}

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

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  color: var(--text-primary);
  background-color: var(--bg-page);
  background-image:
    radial-gradient(circle at 14% 12%, var(--bg-radial-1), transparent 46%),
    radial-gradient(circle at 86% 84%, var(--bg-radial-2), transparent 48%);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 0.3s ease, color 0.3s ease;
}

#confetti-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 60;
}

.app-shell {
  max-width: 920px;
  margin: 0 auto;
  padding: 18px 16px 26px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* 顶部栏 */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

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

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 13px;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-btn);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 6px;
}

.brand-mark span {
  display: block;
  height: 5px;
  border-radius: 999px;
}
.brand-mark span:nth-child(1) { width: 14px; background: #e17055; }
.brand-mark span:nth-child(2) { width: 21px; background: #e8a838; }
.brand-mark span:nth-child(3) { width: 28px; background: #3b9b87; }

.brand-text h1 {
  margin: 0;
  font-size: 1.28rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1.15;
}

.subtitle {
  margin: 2px 0 0;
  font-size: 0.72rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-secondary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: var(--shadow-btn);
  backdrop-filter: blur(10px);
  transition: transform 0.15s ease, color 0.2s ease, border-color 0.2s ease, background-color 0.2s ease;
}

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

.icon-btn:hover {
  color: var(--text-primary);
  border-color: var(--border-strong);
  transform: translateY(-1px);
}

.icon-btn:active {
  transform: scale(0.95);
}

/* 音效与主题图标切换 */
.icon-sound-off { display: none; }
#btn-sound[aria-pressed="false"] .icon-sound-on { display: none; }
#btn-sound[aria-pressed="false"] .icon-sound-off { display: block; color: var(--text-muted); }

.icon-sun { display: none; }
html[data-theme="dark"] .icon-sun { display: block; }
html[data-theme="dark"] .icon-moon { display: none; }

/* HUD */
.hud {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px 14px;
  box-shadow: var(--shadow-btn);
  backdrop-filter: blur(10px);
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.stat-label {
  font-size: 0.74rem;
  color: var(--text-muted);
  font-weight: 500;
}

.stat-value {
  font-size: 1.22rem;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.2;
}

.tabular {
  font-variant-numeric: tabular-nums;
}

/* 工具条 */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
}

.disc-selector {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 5px 8px 5px 12px;
  box-shadow: var(--shadow-btn);
}

.selector-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.pill-group {
  display: flex;
  gap: 4px;
}

.pill {
  min-width: 32px;
  height: 30px;
  border: none;
  border-radius: 9px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.86rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.18s ease, color 0.18s ease, transform 0.12s ease;
}

.pill:hover {
  background: var(--accent-soft);
  color: var(--text-primary);
}

.pill.active {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 3px 10px -2px var(--accent-glow);
}

.status-banner {
  flex: 1 1 260px;
  text-align: right;
  font-size: 0.84rem;
  color: var(--text-secondary);
  padding: 6px 4px;
  transition: color 0.2s ease;
}

.status-banner.is-hint {
  color: var(--accent);
  font-weight: 600;
}

.status-banner.is-error {
  color: var(--danger);
  font-weight: 600;
}

.status-banner.is-success {
  color: var(--valid);
  font-weight: 600;
}

/* 舞台容器 */
.stage-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  backdrop-filter: blur(14px);
  padding: 18px 16px 20px;
  overflow: hidden;
}

.stage {
  position: relative;
  width: 100%;
  height: 360px;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.pegs-grid {
  position: absolute;
  inset: 0 0 48px 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  z-index: 1;
}

.peg-zone {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  cursor: pointer;
  outline: none;
}

.peg-hover-glow {
  position: absolute;
  inset: 14px 12% 0;
  border-radius: 16px;
  background: transparent;
  border: 2px solid transparent;
  transition: background-color 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
  pointer-events: none;
}

.peg-zone.is-source .peg-hover-glow {
  background: var(--accent-soft);
  border-color: rgba(184, 101, 50, 0.35);
}

.peg-zone.is-valid-target .peg-hover-glow {
  background: var(--valid-soft);
  border-color: rgba(44, 138, 98, 0.45);
  box-shadow: inset 0 0 24px rgba(44, 138, 98, 0.08);
}

.peg-zone.is-invalid-target .peg-hover-glow {
  background: var(--danger-soft);
  border-color: rgba(198, 75, 75, 0.45);
}

.peg-zone.is-hint-from .peg-hover-glow,
.peg-zone.is-hint-to .peg-hover-glow {
  background: var(--accent-soft);
  border-color: var(--accent);
  animation: hintPulse 1.1s ease-in-out infinite;
}

@keyframes hintPulse {
  0%, 100% { box-shadow: 0 0 0 0 var(--accent-glow); }
  50% { box-shadow: 0 0 0 8px rgba(184, 101, 50, 0); }
}

.peg-rod {
  position: relative;
  width: 14px;
  height: 236px;
  border-radius: 999px 999px 2px 2px;
  background: var(--peg-rod);
  box-shadow: inset -2px 0 4px rgba(0, 0, 0, 0.22), 2px 4px 10px rgba(0, 0, 0, 0.14);
}

.peg-cap {
  position: absolute;
  top: -4px;
  left: 50%;
  transform: translateX(-50%);
  width: 18px;
  height: 8px;
  border-radius: 999px;
  background: var(--peg-collar);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.22);
}

.peg-collar {
  width: 46px;
  height: 8px;
  border-radius: 6px 6px 0 0;
  background: var(--peg-collar);
  box-shadow: 0 -1px 2px rgba(255, 255, 255, 0.2) inset;
}

/* 底座 */
.pedestal {
  position: absolute;
  left: 2%;
  right: 2%;
  bottom: 24px;
  height: 24px;
  z-index: 2;
  pointer-events: none;
}

.pedestal-top {
  height: 12px;
  border-radius: 8px 8px 3px 3px;
  background: var(--wood-top);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25);
}

.pedestal-front {
  height: 12px;
  margin: 0 2px;
  border-radius: 0 0 8px 8px;
  background: var(--wood-front);
  box-shadow: 0 10px 22px -6px rgba(32, 18, 6, 0.42);
}

.peg-label {
  position: absolute;
  bottom: -46px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.82rem;
  color: var(--text-secondary);
}

.peg-letter {
  width: 22px;
  height: 22px;
  border-radius: 7px;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.78rem;
  color: var(--text-primary);
}

.target-peg .peg-letter {
  border-color: rgba(44, 138, 98, 0.45);
  color: var(--valid);
}

.peg-tag {
  font-size: 0.72rem;
  color: var(--text-muted);
}

/* 圆盘图层 */
.discs-layer {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;
}

.disc {
  position: absolute;
  transform: translateX(-50%);
  border-radius: 999px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.74rem;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.92);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
  box-shadow:
    inset 0 2px 3px rgba(255, 255, 255, 0.42),
    inset 0 -3px 5px rgba(0, 0, 0, 0.26),
    0 5px 12px -2px rgba(20, 14, 8, 0.34);
  cursor: grab;
  pointer-events: auto;
  transition:
    left 0.26s cubic-bezier(0.22, 1, 0.36, 1),
    bottom 0.26s cubic-bezier(0.22, 1, 0.36, 1),
    box-shadow 0.2s ease,
    filter 0.2s ease;
  will-change: left, bottom, transform;
}

.disc::after {
  content: "";
  position: absolute;
  inset: 2px 10% auto;
  height: 32%;
  border-radius: 999px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.34), rgba(255, 255, 255, 0));
  pointer-events: none;
}

.disc.is-top:hover {
  filter: brightness(1.06);
}

.disc.is-selected {
  box-shadow:
    inset 0 2px 4px rgba(255, 255, 255, 0.55),
    inset 0 -3px 5px rgba(0, 0, 0, 0.24),
    0 18px 28px -6px rgba(20, 14, 8, 0.45),
    0 0 0 3px var(--accent-glow);
  z-index: 20 !important;
}

.disc.is-dragging {
  transition: none !important;
  cursor: grabbing;
  transform: translate(-50%, 0) scale(1.04);
  box-shadow:
    inset 0 2px 4px rgba(255, 255, 255, 0.55),
    inset 0 -3px 5px rgba(0, 0, 0, 0.24),
    0 24px 36px -6px rgba(20, 14, 8, 0.52);
  z-index: 30 !important;
}

.disc.is-shaking {
  animation: discShake 0.34s ease;
}

@keyframes discShake {
  0%, 100% { transform: translateX(-50%); }
  20% { transform: translateX(calc(-50% - 9px)); }
  40% { transform: translateX(calc(-50% + 9px)); }
  60% { transform: translateX(calc(-50% - 6px)); }
  80% { transform: translateX(calc(-50% + 6px)); }
}

/* 8 个漆器/釉色渐变圆盘 */
.disc-size-1 { background: linear-gradient(180deg, #f37b67 0%, #d44d3a 100%); }
.disc-size-2 { background: linear-gradient(180deg, #f4a44d 0%, #d67c22 100%); }
.disc-size-3 { background: linear-gradient(180deg, #e7c64b 0%, #c49b1e 100%); }
.disc-size-4 { background: linear-gradient(180deg, #6cc47f 0%, #3d9a55 100%); }
.disc-size-5 { background: linear-gradient(180deg, #47b8ab 0%, #248b80 100%); }
.disc-size-6 { background: linear-gradient(180deg, #549ee8 0%, #2d72bf 100%); }
.disc-size-7 { background: linear-gradient(180deg, #8b7ee6 0%, #5e50bf 100%); }
.disc-size-8 { background: linear-gradient(180deg, #c973b2 0%, #9b4787 100%); }

/* 底部控制栏 */
.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.ctrl-btn {
  height: 44px;
  padding: 0 16px;
  border-radius: 13px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-primary);
  font-size: 0.9rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  box-shadow: var(--shadow-btn);
  backdrop-filter: blur(10px);
  transition: transform 0.15s ease, background-color 0.2s ease, border-color 0.2s ease, opacity 0.2s ease;
}

.ctrl-btn svg {
  width: 17px;
  height: 17px;
}

.ctrl-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  border-color: var(--border-strong);
}

.ctrl-btn:active:not(:disabled) {
  transform: scale(0.97);
}

.ctrl-btn:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.ctrl-btn.primary-soft {
  background: var(--accent-soft);
  border-color: rgba(184, 101, 50, 0.28);
  color: var(--accent);
}

.auto-group {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.speed-btn {
  padding: 0 12px;
  min-width: 48px;
  font-variant-numeric: tabular-nums;
  font-size: 0.85rem;
}

.icon-pause { display: none; }
#btn-auto.is-playing .icon-play { display: none; }
#btn-auto.is-playing .icon-pause { display: block; }
#btn-auto.is-playing {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.shortcuts-hint {
  margin: 0;
  text-align: center;
  font-size: 0.76rem;
  color: var(--text-muted);
}

kbd {
  display: inline-block;
  padding: 1px 6px;
  font-size: 0.72rem;
  font-family: inherit;
  border-radius: 5px;
  background: var(--surface-solid);
  border: 1px solid var(--border-strong);
  color: var(--text-secondary);
}

/* 弹窗 */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(16, 14, 12, 0.48);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-backdrop[hidden] {
  display: none;
}

.modal-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface-solid);
  border: 1px solid var(--border-strong);
  border-radius: 24px;
  box-shadow: var(--shadow-card);
  padding: 28px 24px 24px;
  text-align: center;
  animation: modalIn 0.28s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes modalIn {
  from { opacity: 0; transform: translateY(14px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.victory-stars {
  font-size: 2rem;
  letter-spacing: 6px;
  color: #e8a838;
  margin-bottom: 6px;
}

.modal-card h2 {
  margin: 0;
  font-size: 1.45rem;
}

.modal-subtitle {
  margin: 6px 0 14px;
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.record-badge {
  display: inline-block;
  margin-bottom: 14px;
  padding: 4px 12px;
  border-radius: 999px;
  background: var(--valid-soft);
  color: var(--valid);
  font-size: 0.78rem;
  font-weight: 700;
}

.modal-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 14px 0 20px;
}

.m-stat {
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.m-stat span {
  font-size: 0.74rem;
  color: var(--text-muted);
}

.m-stat strong {
  font-size: 1.08rem;
  font-variant-numeric: tabular-nums;
}

.modal-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

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

.modal-btn {
  height: 44px;
  border-radius: 13px;
  border: 1px solid var(--border);
  font-size: 0.94rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.14s ease, filter 0.18s ease;
}

.modal-btn:active {
  transform: scale(0.97);
}

.modal-btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.modal-btn.secondary {
  background: var(--surface-elevated);
  color: var(--text-primary);
}

.rules-card {
  text-align: left;
}

.rules-card h2 {
  text-align: center;
  margin-bottom: 14px;
}

.rules-list {
  margin: 0 0 20px;
  padding-left: 18px;
  color: var(--text-secondary);
  font-size: 0.9rem;
  line-height: 1.65;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* 移动端适配 */
@media (max-width: 640px) {
  .app-shell {
    padding: 14px 12px 22px;
    gap: 11px;
  }

  .hud {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }

  .stat-card {
    padding: 8px 12px;
  }

  .stat-value {
    font-size: 1.08rem;
  }

  .toolbar {
    justify-content: center;
  }

  .disc-selector {
    width: 100%;
    justify-content: space-between;
  }

  .status-banner {
    text-align: center;
    font-size: 0.82rem;
    padding: 2px 4px;
  }

  .stage-card {
    padding: 12px 8px 16px;
  }

  .stage {
    height: 296px;
  }

  .peg-rod {
    width: 11px;
    height: 196px;
  }

  .peg-collar {
    width: 36px;
  }

  .controls {
    gap: 8px;
  }

  .ctrl-btn {
    height: 42px;
    padding: 0 13px;
    font-size: 0.86rem;
  }

  .shortcuts-hint {
    display: none;
  }
}
