:root {
  --ink: #18333a;
  --ink-deep: #0d2228;
  --paper: #fff7df;
  --cream: #fff4d2;
  --teal: #3ebd9d;
  --teal-dark: #167561;
  --coral: #f36955;
  --gold: #ffc94a;
  --sky: #bfe9e7;
  --line: rgba(24, 51, 58, 0.18);
  --shadow: rgba(9, 31, 35, 0.26);
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 15% 5%, rgba(255, 240, 188, 0.8), transparent 24rem),
    radial-gradient(circle at 88% 16%, rgba(92, 213, 186, 0.34), transparent 30rem),
    linear-gradient(145deg, #d9f0ea, #f4e9cc 52%, #cce7df);
  font-family: "Microsoft YaHei", "PingFang SC", "Noto Sans SC", sans-serif;
  min-height: 100vh;
  overflow-x: hidden;
}

body::before {
  position: fixed;
  inset: 0;
  z-index: -1;
  opacity: 0.27;
  background-image:
    linear-gradient(rgba(24, 51, 58, 0.07) 1px, transparent 1px),
    linear-gradient(90deg, rgba(24, 51, 58, 0.07) 1px, transparent 1px);
  background-size: 42px 42px;
  content: "";
  mask-image: linear-gradient(to bottom, black, transparent 80%);
}

button,
a {
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
}

.page-shell {
  width: min(1180px, calc(100% - 36px));
  margin: 0 auto;
  padding: 22px 0 28px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 58px;
  margin-bottom: 14px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 11px;
  color: inherit;
  text-decoration: none;
}

.brand-mark {
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  border: 2px solid var(--ink);
  border-radius: 12px 4px 12px 4px;
  color: var(--cream);
  background: var(--coral);
  box-shadow: 3px 3px 0 var(--ink);
  transform: rotate(-3deg);
}

.brand strong,
.brand small {
  display: block;
}

.brand strong {
  font-size: 18px;
  letter-spacing: 0.12em;
}

.brand small {
  margin-top: 1px;
  color: rgba(24, 51, 58, 0.62);
  font-family: Consolas, monospace;
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0.24em;
}

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

.best-label {
  color: rgba(24, 51, 58, 0.66);
  font-size: 12px;
}

.best-label b {
  color: var(--ink);
  font-family: Consolas, monospace;
  font-size: 13px;
}

.icon-button,
.pause-button {
  border: 2px solid var(--ink);
  color: var(--ink);
  background: rgba(255, 247, 223, 0.82);
  box-shadow: 2px 2px 0 var(--ink);
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease, background 120ms ease;
}

.icon-button:hover,
.pause-button:hover {
  background: var(--cream);
  transform: translate(-1px, -1px);
  box-shadow: 3px 3px 0 var(--ink);
}

.icon-button:active,
.pause-button:active {
  transform: translate(2px, 2px);
  box-shadow: none;
}

.icon-button {
  width: 38px;
  height: 38px;
  border-radius: 11px;
  font-family: Consolas, monospace;
  font-size: 17px;
  font-weight: 900;
}

.icon-button.is-muted {
  opacity: 0.55;
}

.game-shell {
  position: relative;
}

.game-frame {
  position: relative;
  width: 100%;
  overflow: hidden;
  border: 3px solid var(--ink);
  border-radius: 20px 8px 20px 8px;
  background: var(--sky);
  box-shadow:
    0 8px 0 rgba(24, 51, 58, 0.96),
    0 26px 60px rgba(20, 76, 72, 0.22);
  isolation: isolate;
}

.game-frame::before {
  position: absolute;
  inset: 0;
  z-index: 12;
  pointer-events: none;
  border: 5px solid rgba(255, 255, 255, 0.16);
  border-radius: 16px 5px 16px 5px;
  content: "";
}

#game {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
  background: var(--sky);
  image-rendering: pixelated;
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  visibility: hidden;
  opacity: 0;
  transition: opacity 220ms ease, visibility 220ms;
}

.overlay.is-visible {
  visibility: visible;
  opacity: 1;
}

.loading-screen {
  place-items: center;
  align-content: center;
  gap: 16px;
  color: var(--cream);
  background: var(--ink);
  font-size: 13px;
  letter-spacing: 0.14em;
}

.loading-crystal {
  width: 28px;
  height: 28px;
  border: 3px solid var(--cream);
  background: var(--teal);
  box-shadow: 5px 5px 0 rgba(0, 0, 0, 0.22);
  animation: crystal-hop 760ms ease-in-out infinite alternate;
  transform: rotate(45deg);
}

@keyframes crystal-hop {
  to {
    transform: translateY(-12px) rotate(135deg);
  }
}

.title-screen {
  grid-template-rows: 1fr auto;
  padding: clamp(18px, 3vw, 34px) clamp(22px, 5vw, 64px) 18px;
  background:
    linear-gradient(90deg, rgba(16, 47, 52, 0.94), rgba(16, 47, 52, 0.48) 56%, rgba(16, 47, 52, 0.12)),
    radial-gradient(circle at 80% 55%, rgba(255, 211, 90, 0.18), transparent 28%);
}

.title-screen::after {
  position: absolute;
  inset: 0;
  z-index: -1;
  opacity: 0.08;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.2) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.2) 1px, transparent 1px);
  background-size: 32px 32px;
  content: "";
}

.title-panel {
  width: min(650px, 74%);
  align-self: center;
  color: var(--cream);
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--teal);
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.22em;
}

.title-panel h1 {
  margin: 0;
  font-size: clamp(44px, 8vw, 92px);
  font-weight: 950;
  letter-spacing: -0.07em;
  line-height: 0.94;
  text-shadow: 5px 5px 0 rgba(9, 31, 35, 0.35);
}

.title-panel h1 span {
  color: var(--gold);
}

.subtitle {
  max-width: 550px;
  margin: 14px 0 22px;
  color: rgba(255, 247, 223, 0.75);
  font-size: clamp(13px, 1.5vw, 16px);
  line-height: 1.7;
}

.character-picker {
  display: flex;
  gap: 10px;
  width: min(600px, 100%);
}

.character-card {
  position: relative;
  display: grid;
  width: 112px;
  min-width: 0;
  padding: 12px 8px 9px;
  border: 2px solid rgba(255, 247, 223, 0.28);
  border-radius: 10px;
  color: var(--cream);
  background: rgba(255, 247, 223, 0.08);
  text-align: center;
  cursor: pointer;
  backdrop-filter: blur(8px);
  transition: transform 150ms ease, background 150ms ease, border-color 150ms ease;
}

.character-card:hover {
  transform: translateY(-3px);
  border-color: rgba(255, 247, 223, 0.65);
}

.character-card.is-selected {
  border-color: var(--gold);
  background: rgba(255, 201, 74, 0.16);
  box-shadow: inset 0 -3px 0 var(--gold);
}

.character-card b,
.character-card small {
  display: block;
}

.character-card b {
  margin-top: 7px;
  font-size: 13px;
}

.character-card small {
  margin-top: 2px;
  color: rgba(255, 247, 223, 0.5);
  font-size: 10px;
}

.hero-sprite {
  display: block;
  width: 52px;
  height: 52px;
  margin: 0 auto;
  background-image: url("assets/tilemap-characters_packed.png");
  background-repeat: no-repeat;
  background-size: 468px 156px;
  image-rendering: pixelated;
  filter: drop-shadow(0 5px 4px rgba(0, 0, 0, 0.2));
  animation: hero-bob 1.2s ease-in-out infinite alternate;
}

.hero-sprite[data-frame="0"] { background-position: 0 0; }
.hero-sprite[data-frame="4"] { background-position: -208px 0; }
.hero-sprite[data-frame="6"] { background-position: -312px 0; }
.hero-sprite[data-frame="9"] { background-position: 0 -104px; }

@keyframes hero-bob {
  to {
    transform: translateY(-3px);
  }
}

.start-row {
  display: flex;
  align-items: center;
  gap: 18px;
  margin-top: 22px;
}

.micro-copy {
  margin: 0;
  color: rgba(255, 247, 223, 0.48);
  font-size: 11px;
}

.primary-button,
.secondary-button {
  border: 2px solid var(--ink);
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease, background 120ms ease;
}

.primary-button {
  display: inline-flex;
  align-items: center;
  gap: 22px;
  padding: 13px 18px 13px 24px;
  border-radius: 10px 4px 10px 4px;
  color: var(--ink);
  background: var(--gold);
  box-shadow: 4px 4px 0 var(--ink);
  font-weight: 950;
  letter-spacing: 0.08em;
}

.primary-button:hover {
  background: #ffda72;
  transform: translate(-1px, -1px);
  box-shadow: 5px 5px 0 var(--ink);
}

.primary-button:active {
  transform: translate(3px, 3px);
  box-shadow: 1px 1px 0 var(--ink);
}

.primary-button.compact {
  padding: 12px 22px;
}

kbd {
  display: inline-grid;
  min-width: 24px;
  min-height: 23px;
  padding: 2px 6px;
  place-items: center;
  border: 1px solid currentColor;
  border-radius: 4px;
  color: inherit;
  background: rgba(255, 255, 255, 0.16);
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.18);
  font-family: Consolas, monospace;
  font-size: 10px;
  font-weight: 900;
  line-height: 1;
}

.primary-button kbd {
  opacity: 0.62;
}

.control-strip {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: clamp(10px, 2vw, 26px);
  padding-top: 14px;
  color: rgba(255, 247, 223, 0.6);
  font-size: 10px;
}

.control-strip span {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
}

.control-strip kbd {
  color: var(--cream);
}

.pause-screen,
.result-screen {
  place-items: center;
  padding: 24px;
  background: rgba(9, 31, 35, 0.7);
  backdrop-filter: blur(7px);
}

.dialog-card {
  width: min(470px, 92%);
  padding: clamp(24px, 4vw, 38px);
  border: 3px solid var(--ink);
  border-radius: 16px 5px 16px 5px;
  background:
    linear-gradient(145deg, rgba(255, 255, 255, 0.4), transparent 50%),
    var(--cream);
  box-shadow: 8px 8px 0 var(--ink);
  text-align: center;
  transform: translateY(8px);
  transition: transform 220ms ease;
}

.overlay.is-visible .dialog-card {
  transform: translateY(0);
}

.dialog-card h2 {
  margin: 0 0 8px;
  font-size: clamp(32px, 5vw, 50px);
  letter-spacing: -0.06em;
}

.dialog-card > p:not(.eyebrow) {
  margin: 0;
  color: rgba(24, 51, 58, 0.66);
  font-size: 14px;
  line-height: 1.7;
}

.dialog-actions {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 24px;
}

.secondary-button {
  padding: 12px 20px;
  border-radius: 8px;
  color: var(--ink);
  background: transparent;
  font-weight: 800;
}

.secondary-button:hover {
  background: rgba(24, 51, 58, 0.08);
}

.result-card {
  width: min(530px, 94%);
}

.result-badge {
  display: grid;
  width: 64px;
  height: 64px;
  margin: -62px auto 22px;
  place-items: center;
  border: 3px solid var(--ink);
  border-radius: 16px 5px 16px 5px;
  color: var(--cream);
  background: var(--coral);
  box-shadow: 5px 5px 0 var(--ink);
  font-size: 28px;
  transform: rotate(-4deg);
}

.result-card.is-loss .result-badge {
  background: var(--ink);
}

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

.result-stats span {
  padding: 12px 8px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.36);
}

.result-stats small,
.result-stats b {
  display: block;
}

.result-stats small {
  margin-bottom: 5px;
  color: rgba(24, 51, 58, 0.5);
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0.12em;
}

.result-stats b {
  font-family: Consolas, "Microsoft YaHei", monospace;
  font-size: 13px;
}

.hud {
  position: absolute;
  z-index: 10;
  top: 0;
  right: 0;
  left: 0;
  display: grid;
  grid-template-columns: 1fr minmax(210px, 32%) 1fr;
  align-items: start;
  gap: 16px;
  padding: 17px 20px;
  color: var(--cream);
  pointer-events: none;
  opacity: 0;
  transition: opacity 180ms ease;
}

.hud.is-visible {
  opacity: 1;
}

.hud-block {
  display: flex;
  align-items: center;
  gap: 12px;
  width: max-content;
  padding: 8px 11px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 8px 3px 8px 3px;
  background: rgba(13, 34, 40, 0.72);
  box-shadow: 0 3px 0 rgba(13, 34, 40, 0.18);
  backdrop-filter: blur(6px);
}

.score-block {
  justify-self: end;
}

.hud-caption {
  color: rgba(255, 247, 223, 0.55);
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0.1em;
}

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

.heart {
  display: inline-block;
  color: var(--coral);
  font-size: 18px;
  line-height: 1;
  text-shadow: 1px 1px 0 var(--ink);
  transition: transform 160ms ease, opacity 160ms ease;
}

.heart.is-empty {
  opacity: 0.22;
  filter: grayscale(1);
  transform: scale(0.85);
}

.hud-center {
  align-self: center;
  padding-top: 8px;
}

.ability-meter {
  display: none;
  align-items: center;
  justify-content: center;
  gap: 7px;
  width: max-content;
  margin: 0 auto 7px;
  padding: 4px 9px;
  border: 1px solid rgba(255, 247, 223, 0.28);
  border-radius: 5px;
  color: rgba(255, 247, 223, 0.76);
  background: rgba(13, 34, 40, 0.58);
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0.08em;
  backdrop-filter: blur(5px);
}

.ability-meter.is-visible {
  display: flex;
}

.ability-meter kbd {
  min-width: 18px;
  min-height: 17px;
  padding: 1px 4px;
  background: rgba(100, 229, 206, 0.16);
  color: #8ff0d9;
}

.ability-meter b {
  color: var(--gold);
  font-family: Consolas, monospace;
  font-size: 9px;
}

.progress-labels {
  display: flex;
  justify-content: space-between;
  margin-bottom: 5px;
  color: rgba(255, 247, 223, 0.56);
  font-size: 8px;
  font-weight: 800;
  letter-spacing: 0.1em;
}

.progress-track {
  height: 7px;
  overflow: hidden;
  border: 1px solid rgba(255, 247, 223, 0.5);
  border-radius: 99px;
  background: rgba(13, 34, 40, 0.56);
}

.progress-track span {
  display: block;
  width: 0;
  height: 100%;
  background: linear-gradient(90deg, var(--teal), var(--gold));
  box-shadow: 0 0 10px rgba(255, 201, 74, 0.6);
  transition: width 150ms linear;
}

.crystal-stat,
.time-stat {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-family: Consolas, monospace;
  font-size: 12px;
  font-weight: 900;
}

.crystal-icon {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 2px;
  border: 1px solid var(--cream);
  background: var(--teal);
  box-shadow: 1px 1px 0 var(--ink);
  transform: rotate(45deg);
}

.crystal-stat em {
  color: rgba(255, 247, 223, 0.45);
  font-size: 9px;
  font-style: normal;
}

.time-stat {
  min-width: 64px;
  padding-left: 10px;
  border-left: 1px solid rgba(255, 247, 223, 0.18);
  color: rgba(255, 247, 223, 0.72);
  font-size: 10px;
}

.pause-button {
  position: absolute;
  z-index: 13;
  right: 17px;
  bottom: 16px;
  display: none;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  font-family: Consolas, monospace;
  font-weight: 900;
}

.pause-button.is-visible {
  display: block;
}

.toast {
  position: absolute;
  z-index: 16;
  top: 17%;
  left: 50%;
  padding: 9px 17px;
  border: 2px solid var(--ink);
  border-radius: 7px;
  color: var(--ink);
  background: var(--cream);
  box-shadow: 3px 3px 0 var(--ink);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.08em;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -8px);
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.touch-controls {
  position: absolute;
  z-index: 14;
  right: 16px;
  bottom: 16px;
  left: 16px;
  display: none;
  justify-content: space-between;
  pointer-events: none;
}

.touch-group {
  display: flex;
  gap: 9px;
}

.touch-controls button {
  width: 54px;
  height: 54px;
  border: 2px solid rgba(255, 247, 223, 0.72);
  border-radius: 14px;
  color: var(--cream);
  background: rgba(13, 34, 40, 0.64);
  box-shadow: 0 3px 0 rgba(13, 34, 40, 0.56);
  font-family: Consolas, monospace;
  font-size: 17px;
  font-weight: 900;
  pointer-events: auto;
  touch-action: none;
  backdrop-filter: blur(5px);
}

.touch-controls button.is-pressed {
  background: rgba(255, 201, 74, 0.68);
  color: var(--ink);
  transform: translateY(3px);
  box-shadow: none;
}

.game-caption {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  padding: 17px 4px 0;
  color: rgba(24, 51, 58, 0.55);
  font-size: 10px;
}

.game-caption p {
  margin: 0;
}

.game-caption kbd {
  margin: 0 2px;
  border-color: rgba(24, 51, 58, 0.35);
  background: rgba(255, 255, 255, 0.25);
}

.status-dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--teal);
  box-shadow: 0 0 0 3px rgba(62, 189, 157, 0.16);
  transform: translateY(-1px);
}

.credit {
  text-align: right;
}

@media (max-width: 760px) {
  .page-shell {
    width: min(100% - 18px, 1180px);
    padding-top: 10px;
  }

  .topbar {
    min-height: 47px;
    margin-bottom: 8px;
  }

  .brand-mark {
    width: 33px;
    height: 33px;
    font-size: 13px;
  }

  .brand strong {
    font-size: 14px;
  }

  .best-label {
    display: none;
  }

  .game-frame {
    border-radius: 12px 5px 12px 5px;
  }

  .title-screen {
    padding: 14px 16px 10px;
  }

  .title-panel {
    width: 100%;
  }

  .title-panel h1 {
    font-size: clamp(39px, 12vw, 58px);
  }

  .subtitle {
    display: none;
  }

  .eyebrow {
    margin-bottom: 5px;
    font-size: 8px;
  }

  .character-picker {
    gap: 6px;
    margin-top: 13px;
  }

  .character-card {
    flex: 1;
    width: auto;
    padding: 6px 3px;
  }

  .hero-sprite {
    width: 38px;
    height: 38px;
    background-size: 342px 114px;
  }

  .hero-sprite[data-frame="4"] { background-position: -152px 0; }
  .hero-sprite[data-frame="6"] { background-position: -228px 0; }
  .hero-sprite[data-frame="9"] { background-position: 0 -76px; }

  .character-card b {
    margin-top: 3px;
    font-size: 10px;
  }

  .micro-copy {
    display: none;
  }

  .character-card small {
    font-size: 7px;
    line-height: 1.2;
    white-space: nowrap;
  }

  .start-row {
    margin-top: 12px;
  }

  .primary-button {
    padding: 10px 14px 10px 18px;
    gap: 14px;
    font-size: 12px;
  }

  .control-strip {
    justify-content: center;
    gap: 10px;
    padding-top: 8px;
    font-size: 8px;
  }

  .control-strip span:nth-child(5),
  .control-strip span:nth-child(6) {
    display: none;
  }

  .control-strip kbd {
    min-width: 18px;
    min-height: 18px;
    padding: 1px 4px;
    font-size: 8px;
  }

  .hud {
    grid-template-columns: auto 1fr auto;
    gap: 7px;
    padding: 9px 9px;
  }

  .hud-block {
    gap: 5px;
    padding: 5px 7px;
  }

  .hud-caption,
  .progress-labels,
  .crystal-stat em {
    display: none;
  }

  .heart {
    font-size: 13px;
  }

  .hud-center {
    padding-top: 10px;
  }

  .ability-meter {
    gap: 4px;
    margin-bottom: 5px;
    padding: 3px 6px;
    font-size: 7px;
  }

  .crystal-stat,
  .time-stat {
    font-size: 9px;
  }

  .time-stat {
    min-width: 50px;
    padding-left: 5px;
  }

  .game-caption {
    justify-content: center;
    padding-top: 13px;
    text-align: center;
  }

  .credit {
    display: none;
  }
}

@media (hover: none), (pointer: coarse) {
  .pause-button.is-visible {
    display: block;
    right: 18px;
    bottom: 82px;
  }

  .hud.is-visible ~ .pause-screen,
  .hud.is-visible ~ .result-screen {
    pointer-events: auto;
  }

  .touch-controls {
    display: flex;
  }

  .game-caption {
    display: none;
  }
}

@media (max-height: 650px) and (min-width: 761px) {
  .page-shell {
    width: min(1080px, calc(100% - 28px));
    padding-top: 8px;
  }

  .topbar {
    min-height: 44px;
    margin-bottom: 6px;
  }

  .title-screen {
    padding-top: 12px;
    padding-bottom: 9px;
  }

  .subtitle {
    margin: 8px 0 13px;
  }

  .start-row {
    margin-top: 13px;
  }

  .game-caption {
    padding-top: 11px;
  }
}

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