:root {
  color-scheme: dark;
  --bg: #12110f;
  --surface: #1b1c18;
  --surface-2: #25261f;
  --surface-3: #10110f;
  --line: #3a3d32;
  --line-strong: #5d624f;
  --text: #f4f1e8;
  --muted: #aaa999;
  --accent: #35c7b2;
  --accent-2: #8f7ae5;
  --gold: #e8bd4c;
  --green: #7ccf75;
  --red: #dc6258;
  --blue: #5fa9e8;
  --tile: clamp(44px, 7.2vw, 72px);
}

* {
  box-sizing: border-box;
}

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

body {
  min-height: 100vh;
  background:
    linear-gradient(180deg, rgba(18, 17, 15, 0.86), rgba(18, 17, 15, 0.98)),
    repeating-linear-gradient(90deg, rgba(232, 189, 76, 0.04) 0 1px, transparent 1px 36px),
    repeating-linear-gradient(0deg, rgba(53, 199, 178, 0.035) 0 1px, transparent 1px 36px),
    var(--bg);
  color: var(--text);
  font-family: "Microsoft YaHei", "PingFang SC", system-ui, sans-serif;
}

button {
  border: 0;
  color: inherit;
  font: inherit;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

button:focus-visible,
.tile:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.app-shell {
  width: min(1180px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: 18px;
  display: grid;
  grid-template-columns: minmax(328px, 1fr) 340px;
  grid-template-rows: auto minmax(178px, 1fr);
  gap: 16px;
}

.game-area,
.side-panel,
.bottom-panel {
  background: rgba(27, 28, 24, 0.96);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 22px 48px rgba(0, 0, 0, 0.34);
}

.game-area {
  padding: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.top-bar {
  width: min(calc(var(--tile) * 8 + 54px), 100%);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.game-title {
  display: grid;
  gap: 4px;
}

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

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 30px;
  line-height: 1.05;
  letter-spacing: 0;
}

h2 {
  font-size: 15px;
  letter-spacing: 0;
}

h3 {
  font-size: 15px;
  letter-spacing: 0;
}

#floorName,
.muted,
.help-list,
.target-info,
.slot-label,
.game-title p {
  color: var(--muted);
}

.text-button,
.icon-button,
.move-button,
.slot-button {
  min-height: 38px;
  border-radius: 8px;
  background: linear-gradient(180deg, #303127, #24251f);
  border: 1px solid var(--line-strong);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

.text-button:hover,
.icon-button:hover,
.move-button:hover,
.slot-button:hover {
  background: linear-gradient(180deg, #3a3b30, #2a2b24);
  border-color: var(--gold);
}

.text-button:active,
.icon-button:active,
.move-button:active,
.slot-button:active {
  transform: translateY(1px);
}

.text-button {
  padding: 0 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  white-space: nowrap;
}

.text-button svg,
.icon-button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-button {
  width: 38px;
  display: inline-grid;
  place-items: center;
  color: var(--gold);
}

.icon-button.active {
  color: #061513;
  background: linear-gradient(180deg, #6fe2d2, #35c7b2);
  border-color: var(--accent);
}

.text-button.active {
  color: #061513;
  background: linear-gradient(180deg, #f5d772, #e8bd4c);
  border-color: var(--gold);
}

.map-grid {
  width: calc(var(--tile) * 8 + 54px);
  max-width: 100%;
  display: grid;
  grid-template-columns: repeat(8, var(--tile));
  grid-template-rows: repeat(8, var(--tile));
  gap: 6px;
  padding: 6px;
  border: 3px solid #0c0d0b;
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(232, 189, 76, 0.22), transparent 28%),
    #0c0d0b;
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.04),
    0 16px 28px rgba(0, 0, 0, 0.36);
  touch-action: manipulation;
}

.tile {
  width: var(--tile);
  height: var(--tile);
  display: grid;
  place-items: center;
  position: relative;
  overflow: hidden;
  padding: 0;
  border-radius: 6px;
  background: #14150f;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.05);
  user-select: none;
}

.tile::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.06), transparent 42%, rgba(0, 0, 0, 0.18));
  pointer-events: none;
}

.tile-img {
  pointer-events: none;
  user-select: none;
}

.tile-base {
  width: 100%;
  height: 100%;
  object-fit: cover;
  image-rendering: pixelated;
}

.tile-entity {
  position: absolute;
  width: 76%;
  height: 76%;
  object-fit: contain;
  filter: drop-shadow(0 8px 8px rgba(0, 0, 0, 0.42));
  image-rendering: pixelated;
  z-index: 1;
}

.actor-sprite {
  position: absolute;
  z-index: 2;
  width: 82%;
  aspect-ratio: 1;
  --face-scale: 1;
  --lunge-x: 0%;
  --lunge-y: 0%;
  background-repeat: no-repeat;
  background-size: 400% 300%;
  background-position: 0 var(--sprite-y);
  filter: drop-shadow(0 9px 8px rgba(0, 0, 0, 0.42));
  image-rendering: auto;
  pointer-events: none;
  transform-origin: 50% 76%;
  transform: translate(0, 0) scaleX(var(--face-scale));
  animation: spriteCycle 960ms steps(1, end) infinite;
}

.actor-hero {
  width: 88%;
}

.actor-enemy {
  width: 82%;
}

.actor-sprite.action-walk {
  animation-duration: 520ms;
}

.actor-sprite.action-attack {
  animation-duration: 420ms;
}

.actor-sprite.facing-left {
  --face-scale: -1;
}

.actor-hero.action-attack {
  animation:
    spriteCycle 420ms steps(1, end) infinite,
    heroAttackLunge 420ms ease-out 1;
}

.actor-hero.facing-right { --lunge-x: 18%; }
.actor-hero.facing-left { --lunge-x: -18%; }
.actor-hero.facing-up { --lunge-y: -18%; }
.actor-hero.facing-down { --lunge-y: 18%; }

.actor-enemy.effect-enemy-ghost {
  z-index: 1;
  opacity: 0.82;
  animation:
    spriteCycle 420ms steps(1, end) infinite,
    enemyHitShake 280ms ease-out 1,
    enemyDissolve 680ms ease-out forwards;
}

.combat-hit {
  animation: tileHitFlash 360ms ease-out 1;
}

.combat-effect {
  position: absolute;
  z-index: 4;
  width: 96%;
  height: 96%;
  pointer-events: none;
  opacity: 0;
}

.combat-effect::before,
.combat-effect::after {
  content: "";
  position: absolute;
  inset: 0;
}

.effect-slash {
  animation: slashBurst 420ms ease-out 1;
}

.effect-slash::before {
  inset: 24% 10% 38%;
  border-top: 5px solid rgba(244, 201, 93, 0.95);
  border-radius: 50%;
  filter: drop-shadow(0 0 8px rgba(244, 201, 93, 0.88));
  transform: rotate(-28deg);
}

.effect-slash::after {
  inset: 36% 18% 46%;
  border-top: 3px solid rgba(255, 255, 255, 0.86);
  border-radius: 50%;
  transform: rotate(-22deg);
}

.effect-magic {
  animation: magicBurst 560ms ease-out 1;
}

.effect-magic::before {
  inset: 22%;
  border: 3px solid rgba(94, 234, 212, 0.9);
  border-radius: 50%;
  box-shadow:
    0 0 12px rgba(94, 234, 212, 0.84),
    inset 0 0 14px rgba(143, 122, 229, 0.42);
}

.effect-magic::after {
  inset: 36%;
  background: radial-gradient(circle, rgba(244, 201, 93, 0.95), rgba(94, 234, 212, 0.2) 56%, transparent 70%);
  border-radius: 50%;
}

.actor-sprite.facing-up {
  filter: drop-shadow(0 7px 7px rgba(0, 0, 0, 0.42)) saturate(0.94);
}

.item-entity {
  animation:
    itemFloat 1.8s ease-in-out infinite,
    itemGlow 2.4s ease-in-out infinite;
}

.tile.item-ruby .item-entity,
.tile.item-emerald .item-entity {
  animation:
    itemFloat 1.8s ease-in-out infinite,
    itemGlow 2.4s ease-in-out infinite,
    itemSpin 3.6s linear infinite;
}

.tile.boss .actor-sprite {
  width: 94%;
  animation-duration: 820ms;
}

.tile.reachable {
  box-shadow:
    inset 0 0 0 3px rgba(53, 199, 178, 0.88),
    inset 0 0 18px rgba(53, 199, 178, 0.16);
}

.tile.selected {
  box-shadow: inset 0 0 0 3px rgba(232, 189, 76, 0.92);
}

.mobile-controls {
  width: min(308px, 100%);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.move-button {
  min-height: 52px;
  display: grid;
  place-items: center;
  color: var(--accent);
}

.control-icon {
  width: 26px;
  height: 26px;
  fill: currentColor;
}

.move-button[data-move="up"] {
  grid-column: 2;
}

.move-button[data-move="left"] {
  grid-column: 1;
}

.move-button[data-move="down"] {
  grid-column: 2;
}

.move-button[data-move="right"] {
  grid-column: 3;
}

.shop-prompt {
  width: min(calc(var(--tile) * 8 + 54px), 100%);
  min-height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 7px 10px;
  border: 1px solid rgba(232, 189, 76, 0.62);
  border-radius: 6px;
  background: rgba(232, 189, 76, 0.12);
  color: var(--gold);
  font-size: 13px;
  font-weight: 700;
}

.shop-prompt[hidden] {
  display: none;
}

.side-panel {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 0;
}

.panel-section {
  padding: 13px 0;
  border-top: 1px solid rgba(93, 98, 79, 0.54);
}

.panel-section:first-child {
  padding-top: 0;
  border-top: 0;
}

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

.difficulty-panel {
  display: grid;
  gap: 9px;
}

.segmented-control {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.difficulty-button {
  min-height: 34px;
  padding: 0 8px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: rgba(11, 12, 10, 0.62);
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.difficulty-button:hover {
  color: var(--text);
  border-color: var(--gold);
}

.difficulty-button[aria-pressed="true"] {
  color: #061513;
  background: linear-gradient(180deg, #f5d772, #e8bd4c);
  border-color: var(--gold);
}

.difficulty-hint {
  min-height: 18px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.38;
}

.hero-portrait {
  width: 64px;
  height: 64px;
  display: block;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background-color: #11120f;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.04);
}

.portrait-sprite {
  background-image: url("../assets/sprites/hero.png");
  background-repeat: no-repeat;
  background-size: 400% 300%;
  background-position: 0 0;
  animation: spriteCycle 960ms steps(1, end) infinite;
}

.stats-grid,
.key-grid {
  display: grid;
  gap: 8px;
}

.stats-grid {
  grid-template-columns: repeat(2, 1fr);
}

.stats-grid div,
.key-grid div,
.target-row,
.slot-row {
  min-height: 38px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px;
  background: rgba(11, 12, 10, 0.58);
  border: 1px solid rgba(93, 98, 79, 0.45);
  border-radius: 6px;
}

.stats-grid span,
.key-grid span,
.target-row span {
  color: var(--muted);
}

.stats-grid strong,
.key-grid strong,
.target-row strong {
  font-variant-numeric: tabular-nums;
}

.key-swatch {
  width: 12px;
  height: 12px;
  display: inline-block;
  border-radius: 3px;
  margin-right: 5px;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.18);
}

.key-swatch.yellow { background: var(--gold); }
.key-swatch.blue { background: var(--blue); }
.key-swatch.red { background: var(--red); }

.enemy-preview h2,
.save-panel h2 {
  margin-bottom: 10px;
}

.target-info {
  display: grid;
  gap: 6px;
  font-size: 14px;
}

.target-row strong {
  color: var(--text);
}

.save-slots {
  display: grid;
  gap: 8px;
}

.slot-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
}

.slot-label {
  min-width: 0;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.slot-button {
  min-height: 34px;
  padding: 0 10px;
}

.bottom-panel {
  grid-column: 1 / -1;
  padding: 14px;
  min-height: 190px;
}

.tutorial-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: end center;
  padding: 22px;
  background: rgba(0, 0, 0, 0.54);
}

.tutorial-overlay[hidden] {
  display: none;
}

.tutorial-panel {
  width: min(520px, 100%);
  display: grid;
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: rgba(27, 28, 24, 0.98);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.52);
  z-index: 40;
}

.tutorial-panel p {
  line-height: 1.55;
  color: var(--text);
}

.tutorial-progress {
  display: flex;
  gap: 6px;
}

.tutorial-dot {
  width: 18px;
  height: 4px;
  border-radius: 999px;
  background: rgba(170, 169, 153, 0.36);
}

.tutorial-dot.active {
  background: var(--gold);
}

.tutorial-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.tutorial-focus {
  position: relative;
  z-index: 30;
  pointer-events: none;
  outline: 3px solid var(--gold);
  outline-offset: 4px;
  box-shadow: 0 0 0 8px rgba(232, 189, 76, 0.18), 0 0 28px rgba(232, 189, 76, 0.48);
}

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

.action-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.battle-log {
  height: 130px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px;
  background: rgba(8, 9, 8, 0.78);
  border-radius: 6px;
  border: 1px solid rgba(93, 98, 79, 0.5);
  font-family: Consolas, "Microsoft YaHei", monospace;
  font-size: 13px;
}

.log-entry {
  color: #ddd9cc;
}

.log-entry.warn {
  color: #f1ce73;
}

.log-entry.good {
  color: #9fe2ad;
}

.log-entry.bad {
  color: #ff9a90;
}

.modal {
  width: min(470px, calc(100vw - 28px));
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 0;
  box-shadow: 0 28px 80px rgba(0, 0, 0, 0.48);
}

.modal::backdrop {
  background: rgba(0, 0, 0, 0.62);
}

.modal form {
  padding: 14px;
}

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

.shop-items {
  display: grid;
  gap: 10px;
}

.shop-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 10px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.shop-item h3 {
  margin: 0 0 4px;
}

.shop-item p {
  color: var(--muted);
  font-size: 13px;
}

.buy-button {
  min-height: 38px;
  padding: 0 12px;
  border-radius: 7px;
  background: var(--accent);
  color: #061513;
  font-weight: 800;
}

.buy-button:hover {
  background: #6fe2d2;
}

.help-list {
  display: grid;
  gap: 8px;
  line-height: 1.6;
}

@keyframes spriteCycle {
  0%, 24.99% { background-position: 0 var(--sprite-y, 0); }
  25%, 49.99% { background-position: 33.333% var(--sprite-y, 0); }
  50%, 74.99% { background-position: 66.667% var(--sprite-y, 0); }
  75%, 100% { background-position: 100% var(--sprite-y, 0); }
}

@keyframes heroAttackLunge {
  0%, 100% { transform: translate(0, 0) scaleX(var(--face-scale)); }
  42% { transform: translate(var(--lunge-x), var(--lunge-y)) scaleX(var(--face-scale)); }
}

@keyframes enemyHitShake {
  0%, 100% { transform: translate(0, 0) scaleX(var(--face-scale)); }
  22% { transform: translate(-6%, 2%) scaleX(var(--face-scale)); }
  44% { transform: translate(6%, -2%) scaleX(var(--face-scale)); }
  66% { transform: translate(-4%, -1%) scaleX(var(--face-scale)); }
}

@keyframes enemyDissolve {
  0% { opacity: 0.9; filter: drop-shadow(0 9px 8px rgba(0, 0, 0, 0.42)) brightness(1.8); }
  55% { opacity: 0.68; filter: drop-shadow(0 0 10px rgba(244, 201, 93, 0.65)) brightness(1.35); }
  100% { opacity: 0; filter: drop-shadow(0 0 16px rgba(94, 234, 212, 0.5)) brightness(1.8); }
}

@keyframes tileHitFlash {
  0%, 100% { box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.05); }
  36% {
    box-shadow:
      inset 0 0 0 3px rgba(244, 201, 93, 0.96),
      inset 0 0 20px rgba(244, 201, 93, 0.32);
  }
}

@keyframes slashBurst {
  0% { opacity: 0; transform: scale(0.72) rotate(-6deg); }
  28% { opacity: 1; transform: scale(1.08) rotate(0deg); }
  100% { opacity: 0; transform: scale(1.28) rotate(8deg); }
}

@keyframes magicBurst {
  0% { opacity: 0; transform: scale(0.54) rotate(0deg); }
  32% { opacity: 1; transform: scale(1.04) rotate(45deg); }
  100% { opacity: 0; transform: scale(1.32) rotate(120deg); }
}

@keyframes itemFloat {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-7%); }
}

@keyframes itemGlow {
  0%, 100% { filter: drop-shadow(0 6px 6px rgba(0, 0, 0, 0.42)); }
  50% { filter: drop-shadow(0 0 10px rgba(232, 189, 76, 0.68)); }
}

@keyframes itemSpin {
  0% { rotate: -3deg; }
  50% { rotate: 5deg; }
  100% { rotate: -3deg; }
}

@media (max-width: 860px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto;
    padding: 10px;
  }

  .bottom-panel {
    grid-column: auto;
  }

  .side-panel {
    order: 2;
  }

  .bottom-panel {
    order: 3;
  }

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

@media (max-width: 520px) {
  :root {
    --tile: clamp(28px, 9vw, 38px);
  }

  .app-shell {
    padding: 8px;
    gap: 10px;
  }

  .game-area,
  .side-panel,
  .bottom-panel {
    border-radius: 0;
  }

  .top-bar,
  .log-header {
    align-items: stretch;
    flex-direction: column;
  }

  .action-row {
    width: 100%;
  }

  .text-button {
    flex: 1 1 calc(50% - 8px);
  }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .slot-row {
    grid-template-columns: 1fr 1fr;
  }

  .slot-label {
    grid-column: 1 / -1;
  }
}
