:root {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: #f6efe1;
  background: #07090f;
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body { min-width: 320px; min-height: 480px; }

#game {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
  background: #07090f;
  cursor: crosshair;
  touch-action: none;
}

.hud-panel {
  position: fixed;
  background: rgba(5, 7, 12, 0.83);
  border: 1px solid rgba(246, 239, 225, 0.18);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(8px);
  border-radius: 14px;
  color: #f6efe1;
}

.top-hud {
  top: 14px;
  left: 14px;
  width: min(760px, calc(100vw - 420px));
  min-width: 510px;
  padding: 14px 16px;
  pointer-events: auto;
}

.game-title {
  font-size: 22px;
  font-weight: 850;
  letter-spacing: 0.2px;
}

.game-subtitle {
  margin-top: 2px;
  font-size: 12px;
  color: #d2c2a1;
  font-weight: 650;
}

.stat-row {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  margin-top: 12px;
  color: #d2c2a1;
  font-size: 13px;
  font-weight: 700;
}

.stat-row strong { color: #f6efe1; }

.button-row {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}

button, select, input {
  font: inherit;
}

button {
  background: rgba(38, 45, 62, 0.95);
  color: #f6efe1;
  border: 1px solid rgba(246, 239, 225, 0.18);
  border-radius: 9px;
  padding: 8px 10px;
  font-weight: 800;
  cursor: pointer;
}

button:hover { border-color: #d4a84e; background: rgba(60, 70, 94, 0.96); }
button.active { border-color: #7fc987; background: rgba(58, 91, 63, 0.96); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

kbd {
  margin-left: 5px;
  color: #d2c2a1;
  font-size: 10px;
}

.status-text {
  margin-top: 10px;
  color: #f6efe1;
  font-size: 13px;
  line-height: 1.35;
  min-height: 34px;
}

.tower-panel {
  top: 14px;
  right: 14px;
  width: 374px;
  max-height: calc(100vh - 28px);
  padding: 14px;
  pointer-events: auto;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.panel-heading {
  font-size: 17px;
  font-weight: 850;
  margin-bottom: 6px;
}

.small-note {
  font-size: 12px;
  line-height: 1.35;
  color: #d2c2a1;
  margin-bottom: 10px;
}

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

select, input {
  width: 100%;
  background: rgba(14, 18, 28, 0.96);
  color: #f6efe1;
  border: 1px solid rgba(246, 239, 225, 0.18);
  border-radius: 8px;
  padding: 8px;
  outline: none;
}

select:focus, input:focus { border-color: #d4a84e; }

.tower-list {
  overflow-y: auto;
  padding-right: 4px;
}

.tower-card {
  border: 1px solid rgba(246, 239, 225, 0.13);
  background: rgba(15, 19, 30, 0.94);
  border-radius: 10px;
  padding: 9px;
  margin-bottom: 8px;
  cursor: pointer;
}

.tower-card:hover { border-color: rgba(212, 168, 78, 0.8); }
.tower-card.selected { border-color: #7fc987; background: rgba(33, 55, 43, 0.94); }
.tower-card.gem { border-color: rgba(184, 116, 255, 0.55); }

.tower-card-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: start;
}

.tower-name { font-weight: 850; font-size: 13px; }
.tower-cost { color: #d4a84e; font-size: 12px; font-weight: 850; white-space: nowrap; }
.tower-meta { color: #d2c2a1; font-size: 11px; margin-top: 4px; line-height: 1.35; }
.tower-notes { color: #aeb9d4; font-size: 11px; margin-top: 5px; line-height: 1.3; }

.gem-stock {
  border: 1px solid rgba(184, 116, 255, 0.45);
  background: rgba(54, 31, 78, 0.45);
  border-radius: 10px;
  padding: 8px;
  margin-bottom: 10px;
  font-size: 12px;
  color: #f6efe1;
}

.selection-panel {
  right: 14px;
  bottom: 14px;
  width: 374px;
  padding: 14px;
  pointer-events: auto;
}

.selection-content {
  color: #d2c2a1;
  font-size: 12px;
  line-height: 1.45;
}

.selection-title {
  color: #f6efe1;
  font-size: 14px;
  font-weight: 850;
  margin-bottom: 7px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 4px 10px;
  margin: 8px 0 10px;
}

.info-grid div {
  border-bottom: 1px solid rgba(246, 239, 225, 0.08);
  padding-bottom: 3px;
}

.target-row, .action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.target-row button, .action-row button {
  padding: 6px 8px;
  font-size: 12px;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.66);
  z-index: 10;
}

.modal-card {
  width: min(780px, calc(100vw - 48px));
  background: rgba(9, 11, 18, 0.96);
  border: 2px solid rgba(184, 116, 255, 0.7);
  border-radius: 18px;
  padding: 24px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.5);
}

.modal-card h2 {
  margin: 0 0 6px;
  font-size: 30px;
}

.modal-card p {
  margin: 0 0 18px;
  color: #d2c2a1;
}

.gem-choices {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

.gem-choice {
  min-height: 160px;
  text-align: left;
  border-color: rgba(184, 116, 255, 0.45);
}

.hidden { display: none !important; }


.hero-ability-panel {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 10px;
  color: #d2c2a1;
  font-size: 12px;
  font-weight: 750;
}

.hero-ability-panel span {
  border: 1px solid rgba(193, 154, 91, 0.28);
  border-radius: 999px;
  padding: 5px 8px;
  background: rgba(38, 30, 18, 0.42);
}


.hero-select {
  min-width: 180px;
  max-width: 240px;
  background: rgba(14, 18, 28, 0.96);
  color: #f6efe1;
  border-color: rgba(212, 168, 78, 0.45);
}

.hero-ability-panel span {
  display: inline-block;
  margin-right: 10px;
  margin-top: 8px;
  padding: 4px 7px;
  border: 1px solid rgba(246, 239, 225, 0.14);
  border-radius: 8px;
  background: rgba(15, 19, 30, 0.72);
  font-size: 12px;
  color: #d2c2a1;
}

.hero-ability-panel strong { color: #f6efe1; }

.mission-row {
  display: grid;
  grid-template-columns: auto minmax(220px, 1fr) auto minmax(160px, 1fr) auto;
  gap: 8px;
  align-items: center;
  margin-top: 10px;
  color: #d2c2a1;
  font-size: 12px;
  font-weight: 800;
}

.mission-select {
  min-width: 220px;
}

.mission-name-value {
  color: #f6efe1;
  font-weight: 850;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.star-value {
  color: #d4a84e;
  font-size: 15px;
  font-weight: 900;
  white-space: nowrap;
}

.bonus-list {
  margin-top: 8px;
  display: grid;
  gap: 4px;
  font-size: 11px;
  color: #d2c2a1;
}

.bonus-list div {
  border: 1px solid rgba(246, 239, 225, 0.1);
  background: rgba(13, 18, 26, 0.62);
  border-radius: 8px;
  padding: 5px 7px;
}

.bonus-list .complete { border-color: rgba(127, 201, 135, 0.5); color: #9ee5a7; }
.bonus-list .failed { border-color: rgba(221, 77, 77, 0.45); color: #ffb2a8; }

.story-card { max-width: 820px; }
.story-meta {
  color: #d4a84e;
  font-size: 13px;
  font-weight: 850;
  margin-bottom: 8px;
}
.story-card p {
  white-space: pre-line;
  line-height: 1.45;
}


/* Phase 8+ PWA/fullscreen adjustments */
@media (display-mode: fullscreen), (display-mode: standalone) {
  body { background: #07090f; }
  .hud-panel { backdrop-filter: blur(10px); }
}

@media (max-width: 980px) {
  .top-hud {
    top: max(8px, env(safe-area-inset-top));
    left: max(8px, env(safe-area-inset-left));
    width: calc(100vw - 16px - env(safe-area-inset-left) - env(safe-area-inset-right));
    min-width: 0;
    max-height: 45vh;
    overflow-y: auto;
    padding: 10px;
  }

  .game-title { font-size: 18px; }
  .game-subtitle { font-size: 11px; }
  .mission-row { grid-template-columns: 1fr; }
  .stat-row, .button-row, .hero-ability-panel { gap: 6px; }
  button, select, input { padding: 7px 8px; font-size: 12px; }

  .tower-panel {
    left: max(8px, env(safe-area-inset-left));
    right: auto;
    top: auto;
    bottom: max(8px, env(safe-area-inset-bottom));
    width: min(58vw, 420px);
    max-height: 32vh;
    padding: 10px;
  }

  .selection-panel {
    right: max(8px, env(safe-area-inset-right));
    bottom: max(8px, env(safe-area-inset-bottom));
    width: min(38vw, 320px);
    max-height: 32vh;
    overflow-y: auto;
    padding: 10px;
  }

  .small-note { display: none; }
  .filters { grid-template-columns: 1fr; }
  .tower-card { padding: 7px; }
}

/* Phase 9 art/audio polish */
.hud-panel {
  border-color: rgba(212, 168, 78, 0.24);
  background:
    linear-gradient(180deg, rgba(12, 14, 22, 0.92), rgba(5, 7, 12, 0.86)),
    radial-gradient(circle at top left, rgba(212, 168, 78, 0.12), transparent 42%);
}

.game-title::before {
  content: "✦";
  color: #d4a84e;
  margin-right: 8px;
}

.music-select {
  min-width: 96px;
  max-width: 116px;
  padding: 8px 9px;
  border-color: rgba(137, 182, 255, 0.35);
}

.element-badge {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 999px;
  margin-right: 7px;
  box-shadow: 0 0 12px currentColor;
  vertical-align: -1px;
}

.tower-card {
  background:
    linear-gradient(135deg, rgba(246,239,225,0.035), transparent 46%),
    rgba(15, 19, 30, 0.94);
}

.story-card {
  width: min(920px, calc(100vw - 48px));
  padding: 0;
  overflow: hidden;
  border-color: rgba(212, 168, 78, 0.72);
}

.story-visual {
  min-height: 190px;
  position: relative;
  border-bottom: 1px solid rgba(246, 239, 225, 0.14);
  overflow: hidden;
}

.story-banner {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center;
  opacity: 0.92;
  transform: scale(1.03);
}

.story-portrait-row {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: end;
  gap: 16px;
  padding: 28px 28px 22px;
  min-height: 190px;
  background: linear-gradient(90deg, rgba(5,7,12,.82), rgba(5,7,12,.42), rgba(5,7,12,.75));
}

.story-portrait-row img {
  width: 132px;
  height: 132px;
  border-radius: 24px;
  border: 2px solid rgba(246, 239, 225, 0.34);
  box-shadow: 0 16px 42px rgba(0,0,0,.45);
  object-fit: cover;
  background: rgba(5,7,12,.72);
}

.story-portrait-row strong {
  display: block;
  font-size: 26px;
  line-height: 1.1;
  color: #f6efe1;
  text-shadow: 0 2px 18px rgba(0,0,0,.65);
}

.story-portrait-row span,
.story-portrait-row em {
  display: block;
  margin-top: 6px;
  color: #d2c2a1;
  font-size: 14px;
  font-style: normal;
  font-weight: 800;
}

.story-portrait-row em { color: #d4a84e; }
.story-card .story-meta,
.story-card h2,
.story-card p,
.story-card button {
  margin-left: 28px;
  margin-right: 28px;
}
.story-card .story-meta { margin-top: 22px; }
.story-card p { margin-bottom: 18px; }
.story-card button { margin-bottom: 26px; }

@media (max-width: 980px) {
  .music-select { max-width: none; min-width: 110px; }
  .story-portrait-row { padding: 18px; min-height: 150px; }
  .story-portrait-row img { width: 96px; height: 96px; border-radius: 18px; }
  .story-portrait-row strong { font-size: 20px; }
  .story-card .story-meta,
  .story-card h2,
  .story-card p,
  .story-card button { margin-left: 18px; margin-right: 18px; }
}


/* Phase 9.5 visual/mobile upgrade */
.mobile-action-bar {
  display: none;
}

.tower-card::after {
  content: "";
  display: block;
  height: 3px;
  margin-top: 7px;
  border-radius: 999px;
  background: linear-gradient(90deg, rgba(212,168,78,.55), rgba(137,182,255,.14), transparent);
}

button, select, input {
  min-height: 38px;
}

@media (hover: none) and (pointer: coarse) {
  #game { cursor: default; }
  button, select, input { min-height: 44px; }
  .tower-card { min-height: 76px; }
}

@media (max-width: 980px), (max-height: 720px) {
  body.mobile-layout .top-hud {
    top: max(6px, env(safe-area-inset-top));
    left: max(6px, env(safe-area-inset-left));
    right: max(6px, env(safe-area-inset-right));
    width: auto;
    max-height: 30vh;
    overflow-y: auto;
    padding: 9px;
    border-radius: 13px;
  }

  body.mobile-layout .game-title { font-size: 16px; }
  body.mobile-layout .game-subtitle { display: none; }
  body.mobile-layout .mission-row {
    display: grid;
    grid-template-columns: 1fr auto auto;
    gap: 6px;
    align-items: center;
  }
  body.mobile-layout .mission-row label,
  body.mobile-layout .mission-name-value { display: none; }
  body.mobile-layout .stat-row {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 4px 8px;
    font-size: 11px;
    margin-top: 7px;
  }
  body.mobile-layout .button-row {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
    margin-top: 7px;
  }
  body.mobile-layout .button-row button,
  body.mobile-layout .button-row select {
    font-size: 11px;
    padding: 6px;
  }
  body.mobile-layout kbd,
  body.mobile-layout .status-text,
  body.mobile-layout .bonus-list {
    display: none;
  }
  body.mobile-layout .hero-ability-panel {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 5px;
    font-size: 10px;
    margin-top: 6px;
  }
  body.mobile-layout .hero-ability-panel span:first-child {
    grid-column: 1 / -1;
  }

  body.mobile-layout .tower-panel {
    left: max(6px, env(safe-area-inset-left));
    right: max(6px, env(safe-area-inset-right));
    bottom: calc(58px + env(safe-area-inset-bottom));
    top: auto;
    width: auto;
    max-height: 28vh;
    padding: 9px;
    border-radius: 13px;
  }
  body.mobile-layout .panel-heading { font-size: 14px; margin-bottom: 4px; }
  body.mobile-layout .filters {
    grid-template-columns: 0.85fr 1.15fr;
    gap: 6px;
    margin-bottom: 7px;
  }
  body.mobile-layout .tower-list {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: minmax(172px, 48vw);
    gap: 8px;
    overflow-x: auto;
    overflow-y: hidden;
    padding-bottom: 6px;
    scroll-snap-type: x proximity;
  }
  body.mobile-layout .tower-card {
    margin-bottom: 0;
    scroll-snap-align: start;
    padding: 8px;
  }
  body.mobile-layout .tower-notes { display: none; }

  body.mobile-layout .selection-panel {
    display: none;
  }

  body.mobile-layout .mobile-action-bar {
    position: fixed;
    left: max(6px, env(safe-area-inset-left));
    right: max(6px, env(safe-area-inset-right));
    bottom: max(6px, env(safe-area-inset-bottom));
    height: 46px;
    display: grid;
    grid-template-columns: 1.25fr 1fr 1fr 1.25fr .75fr .75fr .75fr;
    gap: 6px;
    z-index: 20;
    pointer-events: auto;
  }
  body.mobile-layout .mobile-action-bar button {
    min-height: 46px;
    padding: 4px;
    border-radius: 12px;
    background: rgba(10, 13, 20, .92);
    border-color: rgba(212,168,78,.34);
    box-shadow: 0 8px 24px rgba(0,0,0,.34);
    font-size: 12px;
  }
}

@media (max-width: 680px) {
  body.mobile-layout .top-hud { max-height: 32vh; }
  body.mobile-layout .tower-panel { max-height: 30vh; }
  body.mobile-layout .button-row { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  body.mobile-layout .music-select { display: none; }
  body.mobile-layout .hero-select { grid-column: span 2; }
  body.mobile-layout .mobile-action-bar { grid-template-columns: repeat(7, minmax(0, 1fr)); }
  body.mobile-layout .mobile-action-bar button { font-size: 10.5px; }
}
