:root {
  color-scheme: dark;
  --bg: #0f120d;
  --ink: #151812;
  --paper: #1d2118;
  --paper-2: #25291e;
  --line: #4b432d;
  --line-soft: #342f22;
  --text: #f3ead2;
  --muted: #b8ad8e;
  --gold: #d4b05c;
  --jade: #91a36b;
  --cinnabar: #b85c48;
}

* {
  box-sizing: border-box;
}

html {
  width: 100%;
  min-height: 100%;
}

body {
  margin: 0;
  width: 100%;
  min-height: 100svh;
  overflow-x: hidden;
  background:
    linear-gradient(90deg, rgba(212, 176, 92, 0.045) 1px, transparent 1px),
    linear-gradient(0deg, rgba(212, 176, 92, 0.035) 1px, transparent 1px),
    var(--bg);
  background-size: 48px 48px;
  color: var(--text);
  font-family: "Noto Sans SC", "PingFang SC", "Microsoft YaHei", ui-sans-serif, system-ui, sans-serif;
}

button {
  font: inherit;
  -webkit-tap-highlight-color: transparent;
}

.game-shell {
  width: min(1380px, 100vw);
  margin: 0 auto;
  padding: 18px 20px 24px;
  padding-left: max(20px, env(safe-area-inset-left));
  padding-right: max(20px, env(safe-area-inset-right));
}

.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line-soft);
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-family: "Noto Serif SC", "Songti SC", STSong, Georgia, serif;
  font-size: 34px;
  line-height: 1.05;
  letter-spacing: 0;
  color: var(--text);
}

h2 {
  margin-bottom: 12px;
  font-family: "Noto Serif SC", "Songti SC", STSong, Georgia, serif;
  font-size: 15px;
  line-height: 1.2;
  color: var(--gold);
}

.topbar p {
  margin-top: 7px;
  color: var(--muted);
  font-size: 14px;
}

.desktop-note {
  margin: 10px 0 14px;
  color: #cbbf9c;
  font-size: 13px;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.stats span {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 34px;
  padding: 0 11px;
  border: 1px solid var(--line);
  background: rgba(21, 24, 18, 0.92);
  color: var(--muted);
  font-size: 13px;
}

.stats strong {
  color: var(--text);
  font-size: 15px;
}

.core-stat i {
  display: block;
  width: 72px;
  height: 7px;
  border: 1px solid #675a37;
  background: #0d0f0b;
}

.core-stat b {
  display: block;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, var(--cinnabar), var(--gold));
}

.layout {
  display: grid;
  grid-template-columns: 274px minmax(560px, 1fr) 286px;
  grid-template-areas: "controls stage info";
  gap: 14px;
  align-items: start;
}

.controls-panel {
  grid-area: controls;
  min-width: 0;
}

.stage-wrap {
  grid-area: stage;
  min-width: 0;
}

.info-panel {
  grid-area: info;
  min-width: 0;
}

.mobile-tabs {
  display: none;
}

.panel,
.stage-wrap {
  border: 1px solid var(--line);
  background: rgba(21, 24, 18, 0.94);
  box-shadow: inset 0 0 0 1px rgba(243, 234, 210, 0.035);
}

.panel {
  min-height: 640px;
  padding: 15px;
}

.stage-wrap {
  background:
    linear-gradient(90deg, rgba(212, 176, 92, 0.035) 1px, transparent 1px),
    linear-gradient(0deg, rgba(212, 176, 92, 0.035) 1px, transparent 1px),
    #10120e;
  background-size: 40px 40px;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#game,
#game canvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 960 / 640;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.tower-list {
  display: grid;
  gap: 9px;
}

.tower-button {
  display: grid;
  grid-template-columns: 50px 1fr;
  gap: 10px;
  align-items: center;
  width: 100%;
  padding: 9px;
  border: 1px solid var(--line-soft);
  background: var(--paper);
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.tower-button img {
  width: 50px;
  height: 50px;
  object-fit: cover;
  border: 1px solid rgba(212, 176, 92, 0.34);
  background: #11140f;
}

.tower-copy {
  min-width: 0;
}

.tower-button:hover,
.tower-button.selected {
  border-color: var(--gold);
  background: var(--paper-2);
}

.tower-button strong {
  display: block;
  margin-bottom: 3px;
  font-family: "Noto Serif SC", "Songti SC", STSong, Georgia, serif;
  font-size: 14px;
}

.tower-button span,
.tower-button small {
  display: block;
  font-size: 12px;
  line-height: 1.35;
  color: var(--muted);
}

.tower-button em {
  display: inline-block;
  margin-top: 6px;
  color: var(--gold);
  font-style: normal;
  font-size: 12px;
}

.tower-button small {
  margin-top: 4px;
  color: #d7c89e;
}

.mobile-build-slots {
  display: none;
}

.mobile-build-title {
  margin: 10px 0 7px;
  color: var(--gold);
  font-family: "Noto Serif SC", "Songti SC", STSong, Georgia, serif;
  font-size: 13px;
}

.mobile-slot-grid {
  display: grid;
  gap: 7px;
}

.mobile-slot {
  min-height: 54px;
  padding: 8px;
  border: 1px solid var(--line-soft);
  background: #181b14;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.mobile-slot b,
.mobile-slot span,
.mobile-slot small {
  display: block;
}

.mobile-slot b {
  color: var(--text);
  font-size: 12px;
  line-height: 1.25;
}

.mobile-slot span {
  margin-top: 3px;
  color: var(--gold);
  font-size: 12px;
  line-height: 1.25;
}

.mobile-slot small {
  margin-top: 3px;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.25;
}

.mobile-slot.fit {
  border-color: rgba(212, 176, 92, 0.72);
}

.mobile-slot.misfit {
  border-color: rgba(184, 92, 72, 0.7);
}

.mobile-slot.occupied {
  background: #202419;
}

.mobile-slot.selected {
  border-color: var(--gold);
  box-shadow: inset 0 0 0 1px rgba(212, 176, 92, 0.45);
}

.mobile-slot:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.primary,
.secondary {
  width: 100%;
  min-height: 40px;
  margin-top: 10px;
  border: 1px solid transparent;
  cursor: pointer;
}

.primary {
  background: var(--gold);
  color: #19160d;
  font-weight: 700;
}

.secondary {
  background: transparent;
  border-color: #7d6c40;
  color: var(--gold);
}

.danger-action {
  border-color: #7c4b3d;
  color: #d79281;
}

.primary:disabled,
.secondary:disabled {
  opacity: 0.46;
  cursor: not-allowed;
}

.hint,
.feedback {
  font-size: 13px;
  line-height: 1.55;
  color: var(--muted);
}

.hint {
  margin-top: 13px;
  padding-top: 13px;
  border-top: 1px solid var(--line-soft);
}

.secondary-hint {
  color: #d2c5a2;
}

.feedback {
  min-height: 132px;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--line-soft);
  color: var(--text);
}

.feedback b {
  color: var(--gold);
}

.feedback span {
  color: #d0c19b;
}

.matchup-table {
  width: 100%;
  margin-top: 10px;
  border-collapse: collapse;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.25;
}

.matchup-table th,
.matchup-table td {
  padding: 5px 4px;
  border-top: 1px solid rgba(212, 176, 92, 0.16);
  text-align: left;
  vertical-align: top;
}

.matchup-table th {
  color: var(--gold);
  font-weight: 600;
}

.escape-tooltip {
  position: fixed;
  right: 28px;
  bottom: 28px;
  z-index: 40;
  width: min(310px, calc(100vw - 56px));
  padding: 13px 14px;
  border: 1px solid #8d5a46;
  background: rgba(20, 16, 13, 0.96);
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.42);
  color: var(--text);
  opacity: 0;
  transform: translateY(10px);
  pointer-events: none;
}

.escape-tooltip.visible {
  animation: escape-pulse 2.8s ease forwards;
}

.escape-tooltip b,
.escape-tooltip span,
.escape-tooltip em,
.escape-tooltip small {
  display: block;
}

.escape-tooltip b {
  margin-bottom: 4px;
  color: #e2b37a;
  font-family: "Noto Serif SC", "Songti SC", STSong, Georgia, serif;
  font-size: 15px;
}

.escape-tooltip span,
.escape-tooltip em,
.escape-tooltip small {
  color: #d7c89e;
  font-size: 12px;
  line-height: 1.45;
}

.escape-tooltip em {
  margin-top: 5px;
  color: var(--gold);
  font-style: normal;
}

@keyframes escape-pulse {
  0% { opacity: 0; transform: translateY(10px); }
  12% { opacity: 1; transform: translateY(0); }
  78% { opacity: 1; transform: translateY(0); }
  100% { opacity: 0; transform: translateY(10px); }
}

.dilemma-panel {
  margin-bottom: 18px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--line-soft);
}

.dilemma-note {
  margin-bottom: 9px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.dilemma-note b {
  color: var(--gold);
}

.dilemma-options {
  display: grid;
  gap: 8px;
}

.dilemma-card {
  width: 100%;
  padding: 9px;
  border: 1px solid var(--line-soft);
  background: #181b14;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.dilemma-card:hover,
.dilemma-card.selected {
  border-color: var(--gold);
  background: #242519;
}

.dilemma-card b,
.dilemma-card span,
.dilemma-card em {
  display: block;
}

.dilemma-card b {
  margin-bottom: 4px;
  font-family: "Noto Serif SC", "Songti SC", STSong, Georgia, serif;
  color: var(--gold);
  font-size: 13px;
}

.dilemma-card span {
  color: var(--text);
  font-size: 12px;
  line-height: 1.35;
}

.dilemma-card em {
  margin-top: 5px;
  color: #d7c89e;
  font-style: normal;
  font-size: 11px;
  line-height: 1.3;
}

.enemy-cards {
  display: grid;
  gap: 9px;
  max-height: 318px;
  overflow: auto;
  padding-right: 4px;
  scrollbar-color: rgba(201, 168, 90, 0.65) rgba(243, 234, 210, 0.06);
  scrollbar-width: thin;
}

.enemy-cards::-webkit-scrollbar {
  width: 7px;
}

.enemy-cards::-webkit-scrollbar-track {
  background: rgba(243, 234, 210, 0.06);
}

.enemy-cards::-webkit-scrollbar-thumb {
  background: rgba(201, 168, 90, 0.65);
}

.enemy-card {
  display: grid;
  grid-template-columns: 42px 1fr;
  gap: 10px;
  align-items: start;
  padding: 9px;
  border: 1px solid var(--line-soft);
  background: #181b14;
}

.enemy-card img {
  width: 42px;
  height: 42px;
  object-fit: cover;
  border: 1px solid rgba(243, 234, 210, 0.15);
}

.enemy-card b,
.enemy-card span,
.enemy-card em,
.enemy-card small {
  display: block;
}

.enemy-card b {
  margin-bottom: 3px;
  font-size: 13px;
}

.enemy-card span {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.enemy-card em,
.enemy-card small {
  margin-top: 4px;
  color: #d7c89e;
  font-style: normal;
  font-size: 11px;
  line-height: 1.3;
}

.enemy-card small {
  color: #c9a85a;
}

@media (max-width: 1180px), (pointer: coarse) {
  html,
  body {
    height: 100%;
    overflow: hidden;
  }

  body {
    min-height: 100svh;
    height: 100dvh;
  }

  .game-shell {
    width: 100%;
    max-width: none;
    height: 100dvh;
    min-height: 100svh;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    gap: 6px;
    padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
    overflow: hidden;
  }

  .topbar {
    display: grid;
    grid-template-columns: auto 1fr;
    align-items: center;
    gap: 8px;
    min-width: 0;
    padding-bottom: 6px;
  }

  h1 {
    font-size: 22px;
    line-height: 1;
    white-space: nowrap;
  }

  .topbar p,
  .desktop-note,
  .controls-panel h2 {
    display: none;
  }

  .stats {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 4px;
    min-width: 0;
  }

  .stats span {
    min-width: 0;
    min-height: 34px;
    justify-content: center;
    gap: 3px;
    padding: 0 5px;
    font-size: 11px;
    white-space: nowrap;
  }

  .stats strong {
    font-size: 13px;
  }

  .core-stat i {
    display: none;
  }

  .layout {
    min-height: 0;
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) 46px minmax(148px, 34svh);
    grid-template-areas:
      "stage"
      "mobile-tabs"
      "drawer";
    gap: 6px;
    align-items: stretch;
    overflow: hidden;
  }

  .stage-wrap {
    grid-area: stage;
    min-height: 0;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
  }

  #game {
    width: 100%;
    height: auto;
    max-height: 100%;
    aspect-ratio: 960 / 640;
  }

  #game canvas {
    width: 100% !important;
    height: 100% !important;
    max-width: 100%;
    max-height: 100%;
  }

  .mobile-tabs {
    grid-area: mobile-tabs;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
    min-height: 46px;
    padding: 4px;
    border: 1px solid var(--line);
    background: rgba(15, 18, 13, 0.96);
  }

  .mobile-tab {
    min-width: 44px;
    min-height: 44px;
    border: 1px solid var(--line-soft);
    background: #181b14;
    color: var(--muted);
    cursor: pointer;
  }

  .mobile-tab.active {
    border-color: var(--gold);
    background: var(--gold);
    color: #19160d;
    font-weight: 700;
  }

  .controls-panel,
  .info-block {
    grid-area: drawer;
    min-height: 0;
    padding: 9px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    border: 1px solid var(--line);
    background: rgba(21, 24, 18, 0.98);
    box-shadow: inset 0 0 0 1px rgba(243, 234, 210, 0.035);
  }

  .info-panel {
    display: contents;
  }

  .controls-panel {
    min-width: 0;
    display: flex;
    flex-direction: column;
  }

  body.mobile-view-build .controls-panel {
    display: flex;
  }

  body.mobile-view-dilemma .dilemma-block,
  body.mobile-view-feedback .feedback-block,
  body.mobile-view-enemies .enemies-block {
    display: block;
  }

  body.mobile-view-build .dilemma-block,
  body.mobile-view-build .feedback-block,
  body.mobile-view-build .enemies-block,
  body.mobile-view-dilemma .controls-panel,
  body.mobile-view-dilemma .feedback-block,
  body.mobile-view-dilemma .enemies-block,
  body.mobile-view-feedback .controls-panel,
  body.mobile-view-feedback .dilemma-block,
  body.mobile-view-feedback .enemies-block,
  body.mobile-view-enemies .controls-panel,
  body.mobile-view-enemies .dilemma-block,
  body.mobile-view-enemies .feedback-block {
    display: none;
  }

  #startButton {
    order: 1;
    min-height: 52px;
    margin-top: 0;
    font-size: 16px;
  }

  .tower-list {
    order: 2;
  }

  .mobile-build-slots {
    order: 3;
    display: block;
  }

  .tower-list {
    display: flex;
    flex: 0 0 auto;
    gap: 8px;
    margin-top: 8px;
    min-height: 68px;
    overflow-x: auto;
    overflow-y: hidden;
    padding-bottom: 4px;
    scroll-snap-type: x proximity;
    scrollbar-width: thin;
  }

  #upgradeButton {
    order: 4;
  }

  #sellButton {
    order: 5;
  }

  #buildHint {
    order: 6;
  }

  #towerHint {
    order: 7;
  }

  .mobile-build-title {
    margin: 8px 0 6px;
  }

  .mobile-slot-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .mobile-slot,
  .tower-button,
  .dilemma-card,
  .primary,
  .secondary {
    min-height: 44px;
  }

  .mobile-slot {
    padding: 8px;
  }

  .tower-button {
    flex: 0 0 126px;
    height: 60px;
    min-height: 60px;
    grid-template-columns: 30px 1fr;
    gap: 7px;
    padding: 7px;
    scroll-snap-align: start;
  }

  .tower-button img {
    width: 30px;
    height: 30px;
  }

  .tower-button strong {
    font-size: 12px;
  }

  .tower-button span,
  .tower-button small {
    display: none;
  }

  .tower-button em,
  .tower-button small,
  .mobile-slot small {
    font-size: 11px;
  }

  .primary,
  .secondary {
    margin-top: 8px;
  }

  .hint {
    margin-top: 9px;
    padding-top: 9px;
    font-size: 12px;
  }

  .dilemma-panel,
  .feedback {
    min-height: 0;
    margin-bottom: 0;
    padding-bottom: 0;
    border-bottom: 0;
  }

  .dilemma-options {
    grid-template-columns: 1fr;
  }

  .enemy-cards {
    grid-template-columns: 1fr;
    max-height: none;
  }

  .escape-tooltip {
    right: max(10px, env(safe-area-inset-right));
    bottom: calc(56px + env(safe-area-inset-bottom));
    width: min(300px, calc(100vw - 20px));
  }
}

@media (max-width: 1180px) and (orientation: landscape), (pointer: coarse) and (orientation: landscape) {
  .game-shell {
    grid-template-rows: auto minmax(0, 1fr);
  }

  .topbar {
    grid-template-columns: auto minmax(360px, 1fr);
  }

  .layout {
    grid-template-columns: minmax(0, 1fr) clamp(264px, 34vw, 360px);
    grid-template-rows: 46px minmax(0, 1fr);
    grid-template-areas:
      "stage mobile-tabs"
      "stage drawer";
  }

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

  .tower-list {
    display: grid;
    grid-template-columns: 1fr;
    overflow: visible;
  }

  .tower-button {
    flex-basis: auto;
    min-height: 76px;
  }
}

@media (max-width: 430px) and (orientation: portrait) {
  h1 {
    font-size: 20px;
  }

  .topbar {
    grid-template-columns: 1fr;
  }

  .stats span {
    justify-content: space-between;
  }
}
