:root {
  color-scheme: dark;
  --ink: #081615;
  --paper: #f3eddb;
  --soft: #adbfaf;
  --gold: #e9cc91;
  --mint: #a7e8c5;
  --border: rgba(177, 206, 174, .22);
  --gold-border: rgba(231, 201, 139, .34);
  --ui: Inter, 'Microsoft YaHei UI', 'PingFang SC', 'Noto Sans CJK SC', Arial, sans-serif;
  --display: 'Kaiti SC', KaiTi, 'Songti SC', 'Noto Serif CJK SC', SimSun, Georgia, serif;
  --play-width: clamp(362px, calc((100vh - 155px) * .848485), 560px);
}

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

body {
  position: relative;
  isolation: isolate;
  min-width: 280px;
  min-height: 100vh;
  margin: 0;
  color: var(--paper);
  background: var(--ink);
  font: 14px/1.5 var(--ui);
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; color: inherit; }
button:not(:disabled) { cursor: pointer; }
button:disabled { opacity: .5; }
button, canvas { -webkit-tap-highlight-color: transparent; }
button:focus-visible, canvas:focus-visible { outline: 2px solid var(--mint); outline-offset: 4px; }
h1, h2, p { margin: 0; }
img { display: block; max-width: 100%; }
[hidden] { display: none !important; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.scene-art {
  position: absolute;
  z-index: -1;
  inset: 0 0 auto;
  height: clamp(444px, 46vw, 650px);
  background:
    linear-gradient(90deg, rgba(8, 22, 21, .89), rgba(8, 22, 21, .44) 47%, rgba(8, 22, 21, .06) 90%),
    linear-gradient(180deg, rgba(8, 22, 21, .13), rgba(8, 22, 21, .12) 24%, var(--ink) 96%),
    url('assets/hero.jpg') center 36% / cover;
  pointer-events: none;
}

.page-width { width: min(100% - 56px, calc(var(--play-width) + 345px)); margin-inline: auto; }

.topbar {
  min-height: clamp(99px, 14vh, 126px);
  padding-block: 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}

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

.brand-mark {
  width: 54px;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  flex: none;
  border: 1px solid var(--gold-border);
  border-radius: 12px;
  background: rgba(58, 66, 33, .5);
  box-shadow: 0 3px 11px rgba(0, 0, 0, .29), inset 0 0 10px rgba(235, 201, 120, .07);
}

.brand-mark img { width: 93%; height: 93%; object-fit: contain; }

.brand h1 {
  color: #fff2cd;
  font: 700 clamp(40px, 5.1vw, 59px)/.99 var(--display);
  letter-spacing: .04em;
  text-shadow: 0 3px 18px #071814;
}

.brand p {
  margin-top: 8px;
  color: #d9d6bc;
  font-size: 13px;
  letter-spacing: .07em;
  text-shadow: 0 2px 6px #06110c;
}

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

.control-button, .secondary-button {
  position: relative;
  min-height: 41px;
  padding: 0 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 7px;
  color: #f2ebd7;
  background: rgba(14, 35, 31, .86);
  font-size: 12px;
  white-space: nowrap;
  transition: background 140ms ease, border-color 140ms ease, color 140ms ease;
}

.control-button:hover:not(:disabled), .secondary-button:hover {
  background: #21443a;
  border-color: var(--mint);
  color: #fff4d9;
}

.control-button::before { display: block; width: 12px; height: 12px; flex: none; color: var(--gold); content: ''; }
.control-button[data-icon='pause']::before { border-inline: 2px solid currentColor; width: 8px; }
.control-button[data-icon='pause'][data-paused='true']::before { width: 12px; border: 0; background: currentColor; clip-path: polygon(12% 0, 100% 50%, 12% 100%); }
.control-button[data-icon='sound']::before { width: 15px; background: currentColor; clip-path: polygon(0 30%, 22% 30%, 47% 4%, 47% 96%, 22% 70%, 0 70%, 0 30%, 61% 24%, 76% 39%, 91% 24%, 100% 32%, 84% 50%, 100% 68%, 91% 76%, 76% 61%, 61% 76%, 52% 68%, 68% 50%, 52% 32%, 61% 24%); }
.control-button[data-icon='sound'][aria-pressed='true']::before { clip-path: polygon(0 30%, 22% 30%, 47% 4%, 47% 96%, 22% 70%, 0 70%, 0 30%, 64% 35%, 71% 25%, 82% 37%, 87% 50%, 82% 63%, 71% 75%, 64% 65%, 72% 57%, 75% 50%, 72% 43%, 64% 35%, 86% 17%, 94% 10%, 100% 28%, 100% 72%, 94% 90%, 86% 83%, 92% 66%, 92% 34%, 86% 17%); }
.control-button[data-icon='help']::before {
  display: grid;
  place-items: center;
  width: 14px;
  height: 14px;
  border: 1px solid currentColor;
  border-radius: 50%;
  font: 700 10px/1 sans-serif;
  content: '?';
}
.control-button[data-icon='restart']::before { width: 14px; height: 17px; font: 400 18px/.92 Arial, sans-serif; content: '↻'; }

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, var(--play-width)) minmax(220px, 320px);
  align-items: start;
  gap: 25px;
  padding-bottom: max(25px, env(safe-area-inset-bottom));
}

.pond-panel { min-width: 0; }

.stage-frame {
  display: grid;
  position: relative;
  isolation: isolate;
  aspect-ratio: 560 / 660;
  width: 100%;
  overflow: hidden;
  border: 1px solid rgba(200, 211, 157, .49);
  border-radius: 17px;
  background: #193e34 url('assets/pond.jpg') center / cover;
  box-shadow: 0 15px 45px rgba(0, 0, 0, .38), 0 0 22px rgba(162, 215, 168, .07);
}

.stage-frame::after {
  position: absolute;
  z-index: 2;
  inset: 0;
  border-radius: inherit;
  box-shadow: inset 0 0 0 2px rgba(227, 217, 174, .13), inset 0 0 20px rgba(5, 24, 18, .49);
  pointer-events: none;
  content: '';
}

#pond {
  display: block;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  aspect-ratio: 560 / 660;
  touch-action: none;
  user-select: none;
  cursor: crosshair;
}

#pond:focus-visible { outline-offset: -5px; }

.stage-overlay {
  position: absolute;
  z-index: 3;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  background: radial-gradient(ellipse at center, rgba(6, 27, 21, .54), rgba(6, 27, 21, .10) 95%);
}

.overlay-content {
  position: relative;
  width: min(100%, 310px);
  padding: 27px 20px 22px;
  border: 1px solid var(--gold-border);
  border-radius: 13px;
  background: linear-gradient(151deg, rgba(20, 47, 37, .94), rgba(7, 28, 23, .96));
  box-shadow: 0 10px 37px rgba(3, 13, 10, .55), inset 0 1px rgba(255, 237, 187, .05);
  text-align: center;
}

.overlay-content::before {
  position: absolute;
  top: 0;
  left: 50%;
  width: 72px;
  height: 1px;
  transform: translateX(-50%);
  background: linear-gradient(90deg, transparent, #e7c988, transparent);
  content: '';
}

.overlay-content h2 {
  color: #fff1cf;
  font: 700 38px/1.06 var(--display);
  letter-spacing: .05em;
  text-wrap: balance;
}

.overlay-art {
  width: 78px;
  height: 78px;
  margin: -7px auto 6px;
  object-fit: contain;
  filter: drop-shadow(0 0 12px rgba(138, 231, 173, .29));
}

.stage-overlay[data-mode='won'] .overlay-art { width: 98px; height: 98px; }

.overlay-content p { margin: 11px 0 17px; color: #e0dfc8; font-size: 14px; text-wrap: balance; }

.primary-button {
  min-height: 46px;
  padding: 8px 18px;
  border: 1px solid rgba(238, 244, 196, .78);
  border-radius: 8px;
  color: #132a21;
  background: linear-gradient(110deg, #e0e8aa, #9dd9b1);
  box-shadow: inset 0 1px rgba(255, 255, 255, .33), 0 4px 12px rgba(0, 0, 0, .20);
  font-weight: 750;
  transition: filter 140ms ease, transform 140ms ease;
}

.primary-button:not(:disabled):hover { filter: brightness(1.1); }
.primary-button:not(:disabled):active { transform: translateY(1px); }
.overlay-content .primary-button { min-width: 124px; letter-spacing: .035em; }

.hint-line {
  min-height: 20px;
  margin: 10px 3px 0;
  color: var(--soft);
  font-size: 12px;
  line-height: 1.55;
}

.hint-line[data-kind='evolve'] { color: var(--mint); }
.hint-line[data-kind='hit'] { color: #f5b7a2; }

.mobile-controls { display: none; }

.sidebar { display: grid; gap: 13px; }

.panel {
  min-width: 0;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 13px;
  background: linear-gradient(156deg, rgba(19, 40, 35, .97), rgba(9, 24, 23, .99));
  box-shadow: 0 9px 23px rgba(0, 0, 0, .21), inset 0 1px rgba(244, 246, 220, .05);
}

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

.tier-portrait {
  width: 62px;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  flex: none;
  border: 1px solid rgba(150, 208, 176, .34);
  border-radius: 9px;
  background: radial-gradient(circle at 50% 55%, #264b3e 0, #112f27 85%);
}

.tier-portrait img { width: 96%; height: 96%; object-fit: contain; }
.current-tier-copy h2 { margin-bottom: 3px; color: var(--soft); font-size: 12px; font-weight: 500; }
.current-tier-copy strong { color: #f5eccf; font: 700 29px/1.12 var(--display); }
.next-tier {
  margin-left: auto;
  padding: 4px 6px;
  border: 1px solid var(--gold-border);
  border-radius: 5px;
  color: var(--gold);
  background: rgba(203, 171, 105, .07);
  font-size: 11px;
  white-space: nowrap;
}

.progress-caption {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 5px;
  margin: 12px 0 5px;
  color: var(--soft);
  font-size: 12px;
}

#progress-text { color: var(--gold); font-variant-numeric: tabular-nums; }

.progress-track {
  height: 6px;
  overflow: hidden;
  border-radius: 9px;
  background: rgba(160, 212, 184, .16);
}

#progress-fill, #mobile-progress-fill {
  display: block;
  width: 33.333%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #7cd5b1, #e7da92);
  transition: width 180ms ease;
}

.scoreboard { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--border); }
.scoreboard > div { display: grid; gap: 2px; min-width: 0; }
.scoreboard > div + div { padding-left: 12px; border-left: 1px solid var(--border); }
.scoreboard span { color: var(--soft); font-size: 11px; }
.scoreboard strong { min-width: 0; overflow-wrap: anywhere; font: 650 21px/1.13 var(--ui); font-variant-numeric: tabular-nums; }

.codex-panel h2 { color: var(--gold); font: 700 24px/1.04 var(--display); }
.tier-list { display: grid; gap: 3px; margin: 10px 0 0; padding: 0; list-style: none; }

.tier-list li {
  display: grid;
  grid-template-columns: 30px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  min-height: 37px;
  padding-inline: 4px 8px;
  border: 1px solid transparent;
  border-radius: 6px;
  color: var(--soft);
  background: rgba(0, 12, 11, .22);
  font-size: 13px;
}

.tier-list li img { width: 30px; height: 30px; object-fit: contain; }
.tier-list .tier-name { min-width: 0; font-weight: 600; }
.tier-list .tier-level { color: var(--soft); font-size: 11px; white-space: nowrap; }
.tier-list li.locked img { filter: brightness(.68) saturate(.35); opacity: .64; }
.tier-list li.discovered { color: var(--paper); }
.tier-list li.current, .tier-list li[aria-current='step'] {
  color: #f8dfaa;
  border-color: rgba(234, 208, 153, .38);
  background: linear-gradient(90deg, rgba(191, 177, 104, .21), rgba(185, 177, 110, .07));
}
.tier-list li.current .tier-level { color: var(--gold); }

.game-dialog {
  width: min(100% - 28px, 440px);
  max-height: calc(100% - 28px);
  padding: 0;
  border: 1px solid var(--gold-border);
  border-radius: 14px;
  color: var(--paper);
  background: linear-gradient(148deg, #1c3a31, #102521);
  box-shadow: 0 14px 75px rgba(0, 0, 0, .65);
}

.game-dialog::backdrop { background: rgba(3, 12, 10, .83); backdrop-filter: blur(4px); }
.dialog-content { padding: 24px; }
.dialog-content h2 { margin-bottom: 10px; color: var(--gold); font: 700 32px/1.05 var(--display); }
.dialog-content p { color: #e0dfcc; }
.dialog-content ol { display: grid; gap: 8px; margin: 0; padding-left: 1.45em; color: #e0dfcc; }
.dialog-content li::marker { color: var(--gold); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.secondary-button { min-height: 46px; padding: 8px 12px; font-size: 14px; }

@media (max-width: 820px) {
  .game-layout { grid-template-columns: minmax(0, 1.63fr) minmax(210px, 1fr); gap: 14px; }
  .page-width { width: calc(100% - 34px); }
  .topbar { gap: 8px; }
}

@media (max-width: 640px) {
  .scene-art {
    height: 335px;
    background:
      linear-gradient(90deg, rgba(8, 22, 21, .85), rgba(8, 22, 21, .46) 44%, rgba(8, 22, 21, .06) 85%),
      linear-gradient(180deg, rgba(8, 22, 21, .23), rgba(8, 22, 21, .12) 33%, var(--ink) 98%),
      url('assets/hero.jpg') 70% 30% / cover;
  }

  .page-width { width: min(100% - 22px, 515px); }
  .topbar { min-height: 79px; padding-block: 10px; }
  .brand { gap: 7px; }
  .brand-mark { width: 40px; border-radius: 8px; }
  .brand h1 { font-size: clamp(30px, 7.4vw, 43px); }
  .brand p { margin-top: 5px; font-size: 11px; letter-spacing: 0; }
  .top-actions { gap: 3px; }
  .control-button { min-width: 44px; min-height: 44px; padding: 0 5px; }
  .game-layout { grid-template-columns: minmax(0, 1fr); gap: 11px; }
  .stage-frame { border-radius: 12px; }
  .overlay-content { padding: 20px 14px 18px; }
  .overlay-content h2 { font-size: 34px; }
  .hint-line { margin-top: 6px; }
  .sidebar { gap: 10px; }
  .tier-list { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px; }
  .tier-list li { padding-inline: 2px 4px; gap: 4px; }
}

@media (max-width: 640px) and (orientation: portrait) {
  .mobile-controls {
    --stick-size: clamp(96px, 30vw, 124px);
    display: grid;
    grid-template-columns: minmax(0, 1fr) var(--stick-size) minmax(0, 1fr);
    align-items: center;
    gap: clamp(7px, 3vw, 15px);
    margin-top: 7px;
    padding: 9px clamp(8px, 3.1vw, 15px) 7px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: radial-gradient(ellipse at 50% 0%, rgba(94, 139, 110, .15), transparent 76%), #102522;
    box-shadow: inset 0 1px rgba(235, 228, 183, .05), 0 5px 16px rgba(0, 0, 0, .16);
  }

  .mobile-form, .mobile-metrics { min-width: 0; }
  .mobile-form { display: grid; justify-items: start; gap: 3px; }
  #mobile-tier { color: var(--paper); font: 700 clamp(22px, 6.1vw, 29px)/1.04 var(--display); }
  .mobile-stat-label { color: var(--soft); font-size: 11px; white-space: nowrap; }
  .mobile-next { color: var(--gold); font-size: 11px; }

  .joystick-column { display: grid; justify-items: center; gap: 2px; }
  .joystick-caption { color: var(--soft); font-size: 10px; line-height: 1.3; }

  #joystick {
    position: relative;
    width: var(--stick-size);
    height: var(--stick-size);
    border: 1px solid rgba(182, 224, 186, .42);
    border-radius: 50%;
    background: radial-gradient(circle at 50% 48%, #143c32, #102b25 72%);
    box-shadow: inset 0 2px 10px rgba(0, 0, 0, .35), 0 0 0 3px rgba(168, 207, 166, .07);
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
  }

  #joystick::before, #joystick::after {
    position: absolute;
    inset: 8%;
    border-radius: inherit;
    pointer-events: none;
    content: '';
  }

  #joystick::before {
    background:
      linear-gradient(var(--gold), var(--gold)) center top / 1px 8% no-repeat,
      linear-gradient(var(--gold), var(--gold)) center bottom / 1px 8% no-repeat,
      linear-gradient(var(--gold), var(--gold)) left center / 8% 1px no-repeat,
      linear-gradient(var(--gold), var(--gold)) right center / 8% 1px no-repeat;
    opacity: .55;
  }

  #joystick::after { inset: 20%; border: 1px dashed rgba(165, 216, 180, .3); }

  #joystick-knob {
    position: absolute;
    z-index: 1;
    top: 50%;
    left: 50%;
    width: 43%;
    aspect-ratio: 1;
    border: 1px solid rgba(222, 245, 184, .72);
    border-radius: 50%;
    background:
      radial-gradient(circle, rgba(25, 65, 45, .6) 0 9%, transparent 10%),
      linear-gradient(145deg, #c3dfa9, #69ae8d);
    box-shadow: inset 0 1px 2px rgba(244, 255, 219, .46), 0 3px 10px rgba(0, 0, 0, .48);
    transform: translate(calc(-50% + var(--stick-x, 0px)), calc(-50% + var(--stick-y, 0px)));
    pointer-events: none;
  }

  #joystick.is-active { border-color: var(--mint); }
  #joystick.is-active #joystick-knob { filter: brightness(1.08); box-shadow: 0 0 12px rgba(167, 232, 197, .52); }

  .mobile-progress-heading, .mobile-score, .mobile-record { display: flex; align-items: baseline; justify-content: space-between; gap: 4px; }
  #mobile-progress-text { color: var(--gold); font: 600 11px/1 var(--ui); white-space: nowrap; font-variant-numeric: tabular-nums; }
  .mobile-progress { margin-top: 4px; }
  .mobile-score { margin-top: 9px; padding-top: 6px; border-top: 1px solid var(--border); }
  #mobile-score { min-width: 0; color: var(--paper); font: 650 clamp(13px, 4.2vw, 18px)/1.1 var(--ui); overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
  .mobile-record { margin-top: 3px; }
  #mobile-record { min-width: 0; color: var(--soft); font: 600 11px/1 var(--ui); overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }

  .status-panel { display: none; }
}

@media (max-width: 508px) {
  .top-actions .control-button > span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }
}

@media (max-width: 389px) {
  .brand-mark { display: none; }
}

@media (max-width: 365px) {
  .topbar { gap: 4px; }
  .brand h1 { font-size: 26px; }
  .brand p { display: none; }
  .top-actions { gap: 2px; }
}

@media (max-width: 310px) {
  .topbar { flex-wrap: wrap; justify-content: space-between; }
  .top-actions { margin-left: auto; }
}

@media (min-width: 641px) and (max-height: 625px) {
  :root { --play-width: clamp(216px, calc((100vh - 101px) * .848485), 500px); }
  .page-width { width: min(100% - 34px, calc(var(--play-width) + 345px)); }
  .game-layout { grid-template-columns: minmax(0, var(--play-width)) minmax(220px, 320px); }
  .topbar { min-height: 79px; padding-block: 8px; }
  .brand h1 { font-size: 39px; }
  .brand-mark { width: 42px; }
}

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