:root {
  --bg-top: #d7f7ee;
  --bg-bottom: #fff7e6;
  --surface: rgba(255, 253, 248, 0.92);
  --surface-strong: #ffffff;
  --border: rgba(212, 160, 102, 0.42);
  --text: #352019;
  --muted: #76584b;
  --accent: #18a979;
  --accent-deep: #0f7e58;
  --gold: #f59e0b;
  --rose: #e63e6d;
  --shadow: 0 12px 28px rgba(74, 42, 22, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  font-family: "Avenir Next", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
  color: var(--text);
  background: linear-gradient(180deg, var(--bg-top) 0%, var(--bg-bottom) 100%);
  -webkit-tap-highlight-color: transparent;
}

.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: max(8px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
  background:
    repeating-linear-gradient(
      90deg,
      rgba(255, 255, 255, 0.22) 0 28px,
      rgba(255, 255, 255, 0) 28px 56px
    ),
    linear-gradient(180deg, var(--bg-top) 0%, var(--bg-bottom) 100%);
  transition: background 0.35s ease;
}

.app.theme-mint { --bg-top: #d7f7ee; --bg-bottom: #fff7e6; --accent: #18a979; --accent-deep: #0f7e58; }
.app.theme-peach { --bg-top: #ffe3cf; --bg-bottom: #fff6e8; --accent: #eb6f3b; --accent-deep: #c44d1d; }
.app.theme-berry { --bg-top: #ffd9e8; --bg-bottom: #fff3f8; --accent: #d93d7a; --accent-deep: #a62257; }
.app.theme-caramel { --bg-top: #fde6bc; --bg-bottom: #fff8ea; --accent: #c87719; --accent-deep: #99560a; }
.app.theme-starlight {
  --bg-top: #242b57;
  --bg-bottom: #433878;
  --surface: rgba(34, 40, 78, 0.9);
  --surface-strong: #2d3569;
  --border: rgba(243, 198, 105, 0.36);
  --text: #fff7e8;
  --muted: #c6cae8;
  --accent: #f2b84b;
  --accent-deep: #d48f1f;
  --shadow: 0 14px 32px rgba(9, 12, 34, 0.45);
}

.topbar {
  position: relative;
  width: 100%;
  max-width: 980px;
  margin: 0 auto 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
}

.home-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--surface-strong);
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.12);
  flex-shrink: 0;
}

.home-btn img {
  display: block;
  border-radius: 6px;
}

.title-stack {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
  flex-wrap: wrap;
}

.title-stack h1 {
  margin: 0;
  font-size: clamp(18px, 4vw, 24px);
  font-weight: 900;
  letter-spacing: 0.02em;
  line-height: 1.1;
}

.stage-pill {
  font-size: 12px;
  font-weight: 700;
  color: var(--accent-deep);
  background: var(--surface-strong);
  border: 1px solid var(--border);
  padding: 2px 9px;
  border-radius: 999px;
  white-space: nowrap;
}

.app.theme-starlight .stage-pill {
  color: #ffe39b;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.icon-btn,
.top-actions #game-seo-info summary {
  border: 1px solid var(--border);
  background: var(--surface-strong);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  padding: 7px 11px;
  border-radius: 12px;
  cursor: pointer;
  box-shadow: 0 3px 8px rgba(48, 24, 14, 0.08);
  transition: transform 0.12s ease, background 0.15s ease;
}

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

.top-actions #game-seo-info {
  position: static;
  width: auto;
  color: var(--text);
  font: inherit;
  z-index: 2;
}

body > #game-seo-info {
  position: static;
  margin: 12px;
}

.top-actions #game-seo-info summary {
  width: 36px;
  min-width: 0;
  height: 34px;
  padding: 0;
}

.top-actions #game-seo-info summary svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
}

.top-actions #game-seo-info summary:hover,
.top-actions #game-seo-info[open] summary {
  color: var(--text);
  background: var(--surface);
}

.top-actions #game-seo-info .game-info-content {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: min(460px, 100%);
  max-height: calc(100dvh - 100px);
  padding: clamp(14px, 4vw, 20px);
  color: var(--text);
  background: var(--surface-strong);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  overscroll-behavior: contain;
}

.top-actions #game-seo-info .game-info-content h2,
.top-actions #game-seo-info .game-info-content a {
  color: var(--accent-deep);
}

.top-actions #game-seo-info .game-info-content p {
  color: var(--text);
  font: inherit;
  font-size: 14px;
  line-height: 1.6;
}

.top-actions #game-seo-info :focus-visible {
  outline-color: var(--accent-deep);
}

.sound-btn[aria-pressed="true"] {
  background: var(--accent);
  color: #ffffff;
  border-color: transparent;
}

.layout {
  width: 100%;
  max-width: 980px;
  margin: 0 auto;
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 560px) minmax(240px, 1fr);
  gap: 18px;
  align-items: start;
}

.play-column {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}

.hud-bar {
  display: grid;
  grid-template-columns: 76px minmax(0, 1fr) 108px;
  gap: 8px;
}

.hud-box {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 16px;
  padding: 7px 10px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.moves-box,
.score-box {
  align-items: center;
  text-align: center;
}

.hud-caption {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.08em;
  color: var(--muted);
  text-transform: uppercase;
}

.hud-num {
  font-size: 24px;
  font-weight: 900;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.moves-box.low-moves .hud-num {
  color: #e11d48;
  animation: pulseWarn 0.9s infinite;
}

@keyframes pulseWarn {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.12); }
}

.goals-list {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 3px;
}

.goal-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px 3px 5px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid var(--border);
  font-size: 13px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.app.theme-starlight .goal-chip {
  background: rgba(18, 23, 52, 0.7);
}

.goal-chip.done {
  opacity: 0.62;
  background: rgba(24, 169, 121, 0.16);
}

.goal-swatch {
  width: 18px;
  height: 18px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  color: #fff;
  font-weight: 900;
  flex-shrink: 0;
}

.star-bar {
  position: relative;
  width: 100%;
  height: 10px;
  margin-top: 4px;
  border-radius: 999px;
  background: rgba(120, 90, 68, 0.18);
  overflow: visible;
}

.star-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, #fbbf24, #f59e0b);
  transition: width 0.28s ease;
}

.star-mark {
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 12px;
  color: rgba(120, 90, 68, 0.38);
  transition: color 0.2s ease, transform 0.2s ease;
}

.star-mark.lit {
  color: #f59e0b;
  transform: translate(-50%, -50%) scale(1.22);
}

#star-1 { left: 33%; }
#star-2 { left: 66%; }
#star-3 { left: 97%; }

.bingo-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 5px 12px;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 14px;
}

.bingo-letters {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.bingo-pip {
  width: 24px;
  height: 24px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 900;
  color: var(--muted);
  background: rgba(120, 90, 68, 0.12);
  transition: transform 0.18s ease, background 0.18s ease, color 0.18s ease;
}

.bingo-pip.lit {
  background: linear-gradient(135deg, #ff5d8f, #f59e0b);
  color: #ffffff;
  transform: scale(1.08);
  box-shadow: 0 3px 8px rgba(245, 158, 11, 0.35);
}

.bingo-tip {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-align: right;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tool-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 12px;
  background: var(--accent);
  color: #ffffff;
  font-size: 13px;
  font-weight: 700;
}

.tool-banner[hidden] {
  display: none;
}

.tool-banner button {
  border: 1.5px solid rgba(255, 255, 255, 0.7);
  background: rgba(255, 255, 255, 0.16);
  color: #ffffff;
  font: inherit;
  font-size: 12px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 8px;
  cursor: pointer;
}

.board-wrap {
  width: 100%;
  aspect-ratio: 1 / 1;
  max-height: min(64dvh, 540px);
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

#board-canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: pointer;
  outline: none;
}

.tools-bar {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.tool-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 10px;
  border-radius: 14px;
  border: 1.5px solid var(--border);
  background: var(--surface-strong);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 4px 10px rgba(48, 24, 14, 0.08);
  transition: transform 0.12s ease, border-color 0.15s ease, background 0.15s ease;
}

.tool-btn:active {
  transform: scale(0.96);
}

.tool-btn.active {
  border-color: var(--accent);
  background: var(--accent);
  color: #ffffff;
}

.tool-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.tool-count {
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: rgba(245, 158, 11, 0.18);
  color: var(--accent-deep);
  font-size: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.tool-btn.active .tool-count {
  background: rgba(255, 255, 255, 0.25);
  color: #ffffff;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.mode-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  padding: 5px;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 16px;
}

.mode-tab {
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  font-weight: 800;
  padding: 8px 6px;
  border-radius: 11px;
  cursor: pointer;
}

.mode-tab.active {
  background: var(--accent);
  color: #ffffff;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.14);
}

.side-card {
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 18px;
  padding: 14px 16px;
  box-shadow: var(--shadow);
}

.side-card h2 {
  margin: 0 0 6px;
  font-size: 16px;
  font-weight: 900;
}

.side-desc {
  margin: 0;
  font-size: 13px;
  line-height: 1.5;
  color: var(--muted);
}

.pref-row {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.toggle-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

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

.stats-grid dt {
  font-size: 11px;
  color: var(--muted);
  font-weight: 700;
}

.stats-grid dd {
  margin: 2px 0 0;
  font-size: 16px;
  font-weight: 900;
}

/* Modals */
.modal {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(24, 14, 18, 0.54);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal[hidden] {
  display: none;
}

.modal-card {
  width: min(100%, 460px);
  max-height: 86dvh;
  overflow-y: auto;
  background: var(--surface-strong);
  color: var(--text);
  border: 2px solid var(--border);
  border-radius: 22px;
  padding: 18px;
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.28);
}

.modal-wide {
  width: min(100%, 620px);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.modal-head h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 900;
}

.star-summary {
  margin-left: auto;
  font-size: 13px;
  font-weight: 800;
  color: var(--gold);
}

.close-btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  width: 34px;
  height: 34px;
  border-radius: 10px;
  font-size: 16px;
  cursor: pointer;
}

.chapters-list {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.chapter-block {
  border: 1.5px solid var(--border);
  border-radius: 16px;
  padding: 12px;
  background: var(--surface);
}

.chapter-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.chapter-head h3 {
  margin: 0;
  font-size: 15px;
  font-weight: 900;
}

.chapter-head p {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
}

.stage-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 7px;
}

.stage-btn {
  border: 1.5px solid var(--border);
  background: var(--surface-strong);
  color: var(--text);
  border-radius: 12px;
  padding: 8px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  font: inherit;
  cursor: pointer;
}

.stage-btn.current {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(24, 169, 121, 0.22);
}

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

.stage-num {
  font-size: 15px;
  font-weight: 900;
}

.stage-stars {
  font-size: 11px;
  color: var(--gold);
  letter-spacing: 1px;
}

.guide-body h3 {
  margin: 12px 0 6px;
  font-size: 15px;
  color: var(--accent-deep);
}

.app.theme-starlight .guide-body h3 {
  color: #ffe39b;
}

.guide-body ul {
  margin: 0;
  padding-left: 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13.5px;
  line-height: 1.55;
}

.modal-result-card {
  text-align: center;
  padding: 24px 20px;
}

.result-stars {
  font-size: 38px;
  letter-spacing: 6px;
  margin: 10px 0 6px;
  color: #f59e0b;
}

.result-score {
  margin: 0 0 8px;
  font-size: 18px;
  font-weight: 800;
}

.result-reward {
  margin: 0 0 14px;
  padding: 8px 12px;
  border-radius: 12px;
  background: rgba(245, 158, 11, 0.16);
  color: var(--accent-deep);
  font-size: 13px;
  font-weight: 800;
}

.result-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 14px;
}

.primary-btn,
.secondary-btn,
.ghost-btn {
  border-radius: 12px;
  padding: 10px 18px;
  font: inherit;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
}

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

.secondary-btn {
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.ghost-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
}

/* Mobile & narrow portrait layout: keep HUD + Board + Tools in one clean viewport */
@media (max-width: 820px) {
  .layout {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .side-panel {
    order: 2;
  }

  .board-wrap {
    max-height: min(56dvh, 460px);
  }

  .hud-bar {
    grid-template-columns: 64px minmax(0, 1fr) 92px;
    gap: 6px;
  }

  .hud-box {
    padding: 5px 7px;
    border-radius: 13px;
  }

  .hud-num {
    font-size: 20px;
  }

  .bingo-tip {
    display: none;
  }

  .bingo-strip {
    justify-content: center;
    padding: 4px 10px;
  }

  .icon-btn {
    padding: 6px 8px;
    font-size: 12px;
  }

  .top-actions #game-seo-info summary {
    width: 32px;
    height: 32px;
  }

  .stage-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 420px) {
  .topbar {
    flex-wrap: wrap;
  }
  .brand-group {
    min-width: min-content;
  }
  .top-actions {
    margin-inline-start: auto;
  }
  .top-actions .icon-btn {
    min-height: 32px;
    padding-inline: 6px;
  }
  .title-stack h1 {
    font-size: 16px;
  }
  .stage-pill {
    display: none;
  }
  .tool-name {
    font-size: 11.5px;
  }
  .tools-bar {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 5px;
  }
  .tool-btn {
    gap: 4px;
    padding-inline: 4px;
  }
}

@media (max-width: 350px) {
  .brand-group,
  .top-actions {
    gap: 4px;
  }
  .title-stack h1 {
    font-size: 14px;
  }
}
