* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #14171c;
  --panel: #1e232b;
  --panel2: #262d38;
  --line: #333c4a;
  --txt: #e6e9ef;
  --muted: #97a2b4;
  --accent: #5aa6ff;
  --gold: #f2c14e;
  --danger: #e0584b;
  --good: #54c08a;
}

html, body {
  background:
    radial-gradient(ellipse at 50% -10%, rgba(90,166,255,.10), transparent 55%),
    radial-gradient(ellipse at 50% 120%, rgba(224,88,75,.07), transparent 50%),
    var(--bg);
  background-attachment: fixed;
  color: var(--txt);
  font-family: "Segoe UI", system-ui, Arial, sans-serif;
  height: 100%;
}

#app {
  max-width: 1080px;
  margin: 0 auto;
  padding: 18px;
}

header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 14px;
}

h1 {
  font-size: 26px;
  letter-spacing: 1px;
}

h1 .tagline {
  font-size: 13px;
  font-weight: 400;
  letter-spacing: 0;
  color: var(--muted);
}

#stats {
  display: flex;
  gap: 16px;
  font-size: 15px;
  color: var(--muted);
}

#stats b { color: var(--txt); }
#stats .stat b#statGold { color: var(--gold); }

.phase {
  background: var(--accent);
  color: #06121f;
  padding: 2px 10px;
  border-radius: 20px;
  font-weight: 700;
}
.phase.battle { background: var(--danger); color: #fff; }

#main {
  display: flex;
  gap: 18px;
  align-items: flex-start;
}

canvas#board {
  background: #2a2f38;
  border-radius: 8px;
  box-shadow: 0 10px 38px rgba(0,0,0,.55), 0 0 0 1px rgba(255,255,255,.04);
  cursor: pointer;
  display: block;
  touch-action: manipulation;          /* tap to place — no double-tap zoom */
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;         /* no long-press image menu on the board */
}

/* vignette over the board for focus/depth */
.board-wrap::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 8px;
  pointer-events: none;
  box-shadow: inset 0 0 60px 8px rgba(0,0,0,.45);
}

#side {
  flex: 1;
  min-width: 280px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
}

.panel h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--muted);
  margin-bottom: 10px;
}

#hint { color: var(--muted); font-size: 14px; line-height: 1.5; }
#hint b { color: var(--gold); }
#hint em { color: var(--txt); font-style: normal; font-weight: 600; }

.legend ul { list-style: none; display: flex; flex-direction: column; gap: 7px; }
.legend li { font-size: 12.5px; color: var(--muted); line-height: 1.4; display: flex; gap: 9px; align-items: flex-start; }
.legend em { color: var(--txt); font-style: normal; font-weight: 600; }
.legend .sw {
  flex: none; width: 17px; height: 17px; border-radius: 4px; margin-top: 1px;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 9px; font-weight: 800; color: #2a1d00;
}
.legend .sw.green { background: rgba(86,196,140,.55); }
.legend .sw.gold  { background: transparent; border: 2px dashed var(--gold); }
.legend .sw.steel { background: #cdd4de; border: 2px solid #fff; }
.legend .sw.cyan  { background: rgba(95,224,208,.6); border: 2px dashed #5fe0d0; }
.legend .sw.star  { background: var(--gold); width: auto; padding: 0 5px; }
.legend .sw.fairy { background: var(--panel2); border: 2px solid var(--accent); color: var(--accent); font-size: 12px; }

.tray { display: flex; flex-wrap: wrap; gap: 8px; min-height: 28px; }

.tray-item {
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 15px;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.tray-item.sel { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(90,166,255,.35); }
.tray-item .glyph { font-size: 20px; margin-right: 4px; }
.tray-item .star { color: #f2c14e; font-weight: 700; }
.tray-empty { color: var(--muted); font-size: 13px; }

.shop { display: flex; flex-direction: column; gap: 6px; }

.shop-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  gap: 8px;
}
.shop-item .info { font-size: 13px; }
.shop-item .info small { display: block; color: var(--muted); }
.shop-item button {
  background: var(--gold);
  color: #2a1d00;
  border: none;
  border-radius: 5px;
  padding: 6px 10px;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}
.shop-item button:disabled { background: #4a4f59; color: #8b919c; cursor: not-allowed; }

.controls { display: flex; gap: 8px; flex-wrap: wrap; }

/* The segmented tab strip is a phones-only control; desktop shows every panel. */
#tabs { display: none; }

/* The board toast is phones-only; desktop keeps the persistent #hint panel. */
#toast { display: none; }

/* The Field Guide wrapper is layout-transparent on mobile (the legend keeps
   its tabpanel behavior); on desktop it switches to a hidden modal — see the
   ≥601px block below. The close button is never rendered until then. */
#fgWrap { display: contents; }
.fg-close { display: none; }

/* Desktop + tablet (≥601px): the Field Guide is a popup, the controls panel
   floats to the top of the side column, and the persistent legend is gone. */
@media (min-width: 601px) {
  #side > .panel.controls { order: -1; }       /* Start Wave / Speed / Mute / Field Guide / Restart on top */

  #fgWrap { display: none; }
  #fgWrap.show {
    display: flex;
    position: fixed;
    inset: 0;
    background: rgba(8, 10, 14, .78);
    align-items: center;
    justify-content: center;
    z-index: 60;
  }
  #fgWrap.show > .panel.legend {
    display: block;
    position: relative;
    width: 90%;
    max-width: 460px;
    max-height: 80vh;
    overflow-y: auto;
    margin: 0;
    padding: 18px 20px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, .6);
  }
  #fgWrap.show .fg-close {
    display: block;
    position: absolute;
    top: 6px;
    right: 8px;
    background: transparent;
    border: none;
    color: var(--muted);
    font-size: 20px;
    padding: 6px 10px;
    cursor: pointer;
  }
  #fgWrap.show .fg-close:hover { color: var(--txt); }
}

button {
  font-family: inherit;
  font-size: 14px;
  border: 1px solid var(--line);
  background: var(--panel2);
  color: var(--txt);
  border-radius: 6px;
  padding: 9px 14px;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: #06121f; border: none; font-weight: 700; }
button.danger { background: transparent; color: var(--danger); border-color: var(--danger); }
button:disabled { opacity: .5; cursor: not-allowed; }

#overlay {
  position: fixed;
  inset: 0;
  background: rgba(8,10,14,.82);
  display: flex;
  align-items: center;
  justify-content: center;
}
#overlay.hidden { display: none; }

.overlay-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 32px 40px;
  text-align: center;
  max-width: 380px;
}
.overlay-card h2 { font-size: 28px; margin-bottom: 10px; }
.overlay-card p { color: var(--muted); margin-bottom: 16px; line-height: 1.5; }

.reward { color: var(--muted); font-size: 14px; line-height: 1.6; margin-bottom: 20px; }
.reward .big { display: block; font-size: 26px; font-weight: 800; color: var(--gold); letter-spacing: 1px; }
.reward .best { color: var(--good); font-weight: 700; }
.reward small { color: var(--muted); }
.overlay-buttons { display: flex; gap: 10px; justify-content: center; }
.overlay-buttons button { flex: 1; }

/* ---- Home hub ---- */
#home {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse at 50% 30%, rgba(90,166,255,.14), transparent 60%),
    #0c0e12;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}
#home.hidden { display: none; }
.home-card { width: 100%; max-width: 420px; text-align: center; padding: 28px 24px; }
.home-card h1 {
  font-size: clamp(34px, 11vw, 54px);
  letter-spacing: clamp(3px, 1.5vw, 8px);
  margin-bottom: 4px;
  background: linear-gradient(180deg, #eaf2ff, #5aa6ff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 0 40px rgba(90,166,255,.35);
}
.home-tag { color: var(--muted); font-size: 13px; letter-spacing: 1px; text-transform: uppercase; margin-bottom: 22px; }
.home-crowns { font-size: 26px; color: var(--gold); margin-bottom: 8px; }
.home-crowns b { font-weight: 800; }
.home-crowns span { font-size: 14px; color: var(--muted); letter-spacing: 1px; }
.home-stats { display: flex; justify-content: center; gap: 22px; color: var(--muted); font-size: 13px; margin-bottom: 26px; }
.home-stats b { color: var(--txt); }
.home-card button { display: block; width: 100%; margin: 0 0 10px; font-size: 17px; padding: 14px; letter-spacing: 1px; }
.home-card #btnShopMeta { font-size: 14px; padding: 12px; letter-spacing: .3px; }
.home-card #btnDaily { font-size: 15px; padding: 11px; letter-spacing: .3px; }
.home-card #btnDaily small,
.home-card #btnPlay small { display: block; font-weight: 400; font-size: 12px; letter-spacing: 0; opacity: .8; margin-top: 2px; }

/* ascension stepper */
.asc-row {
  display: flex;
  align-items: stretch;
  gap: 8px;
  margin-bottom: 14px;
}
.asc-row button {
  flex: none;
  width: 48px;
  margin: 0;
  font-size: 16px;
  padding: 0;
}
.asc-row button:disabled { opacity: .35; }
.asc-info {
  flex: 1;
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  text-align: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.asc-info b { font-size: 14px; letter-spacing: .5px; }
.asc-info small { color: var(--muted); font-size: 12px; }

/* ---- cosmetics shop ---- */
#cosmetics {
  position: fixed;
  inset: 0;
  background: rgba(8,10,14,.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 55;
  padding: 16px;
}
#cosmetics.hidden { display: none; }
.cos-card {
  width: 100%;
  max-width: 460px;
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 20px;
}
.cos-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
.cos-head h2 { font-size: 22px; letter-spacing: 1px; }
.cos-bal { color: var(--gold); font-size: 18px; }
.cos-bal b { font-weight: 800; }
.cos-bal.nope { animation: nope .4s; }
@keyframes nope { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }
.cos-card h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin: 16px 0 8px; }
.cos-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.cos-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  background: var(--panel2);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}
.cos-item.equipped { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(90,166,255,.35); }
.cos-item.locked { opacity: .5; }
.cos-sw {
  flex: none;
  width: 40px; height: 40px;
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px;
  overflow: hidden;
}
.cos-label b { display: block; font-size: 14px; }
.cos-label small { color: var(--muted); font-size: 12px; }
.cos-card #btnCosBack { width: 100%; margin-top: 18px; padding: 13px; font-size: 16px; letter-spacing: 1px; }

/* ---- phase banner ---- */
.board-wrap { position: relative; flex: none; }

#banner {
  position: absolute;
  top: 38%;
  left: 0;
  right: 0;
  text-align: center;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
#banner.hidden { display: none; }
#banner.show { animation: bannerPop 2s ease-out forwards; }
#banner b {
  font-size: 46px;
  font-weight: 800;
  letter-spacing: 3px;
  color: #fff;
  text-shadow: 0 3px 14px rgba(0,0,0,.8), 0 0 22px rgba(90,166,255,.6);
}
#banner span {
  font-size: 18px;
  color: var(--gold);
  text-shadow: 0 2px 8px rgba(0,0,0,.8);
}
@keyframes bannerPop {
  0%   { opacity: 0; transform: scale(.6); }
  15%  { opacity: 1; transform: scale(1.06); }
  25%  { transform: scale(1); }
  75%  { opacity: 1; }
  100% { opacity: 0; transform: scale(1); }
}

/* ---- title screen ---- */
#title {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse at 50% 30%, rgba(90,166,255,.14), transparent 60%),
    #0c0e12;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
#title.hidden { display: none; }
.title-card { max-width: 540px; text-align: center; padding: 32px; }
.title-card h1 {
  font-size: 68px;
  letter-spacing: 10px;
  margin-bottom: 6px;
  background: linear-gradient(180deg, #eaf2ff, #5aa6ff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 0 40px rgba(90,166,255,.35);
}
.title-tag {
  color: var(--txt);
  font-size: 17px;
  letter-spacing: 2px;
  margin-bottom: 22px;
  text-transform: uppercase;
}
.title-blurb {
  color: var(--muted);
  font-size: 15px;
  line-height: 1.65;
  margin-bottom: 26px;
}
.title-card .primary { font-size: 18px; padding: 12px 38px; letter-spacing: 1px; }

.overlay-card { animation: overlayIn .35s ease-out; }
@keyframes overlayIn {
  from { opacity: 0; transform: translateY(14px) scale(.95); }
  to   { opacity: 1; transform: none; }
}

/* ---- cinematic video stingers (intro / game-over) ---- */
.vstinger {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: #07080b;
  display: flex;
  align-items: center;
  justify-content: center;
}
.vstinger.hidden { display: none; }
.vstinger video {
  width: 100%;
  height: 100%;
  object-fit: contain;   /* never crop the framed composition */
  background: #07080b;
}
.vstinger button {
  position: absolute;
  right: 22px;
  bottom: 20px;
  background: rgba(20, 23, 28, .7);
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 16px;
  font-size: 13px;
  letter-spacing: 1px;
  text-transform: uppercase;
  cursor: pointer;
  transition: color .15s, border-color .15s;
}
.vstinger button:hover { color: var(--txt); border-color: var(--accent); }

/* ---- tablet / small landscape: stack the board above the panels ---- */
@media (max-width: 900px) {
  #main { flex-direction: column; align-items: stretch; }
  .board-wrap { align-self: center; width: 100%; max-width: 640px; }
  canvas#board { width: 100%; height: auto; }
}

/* ---- phones: a full-height app shell. Header + board + tabs stay put; only
        the active tab panel scrolls; Start Wave is fixed at the bottom. The
        page itself never scrolls, so there's no hunting up and down. ---- */
@media (max-width: 600px) {
  html, body { height: 100%; overflow: hidden; }
  #app {
    max-width: none;
    height: 100vh;
    height: 100dvh;                /* tracks mobile browser chrome */
    padding: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  header { flex: none; margin: 0; padding: 8px 12px; gap: 4px 12px; }
  h1 { font-size: 18px; letter-spacing: .5px; }
  h1 .tagline { display: none; }
  #stats { width: 100%; flex-wrap: wrap; gap: 4px 12px; font-size: 12.5px; }

  #main { flex: 1; min-height: 0; gap: 0; }

  /* board is capped so the deck below always has room, and stays square */
  .board-wrap {
    position: relative;            /* keep the ::after vignette scoped here */
    flex: none;
    width: 100%;
    max-width: none;
    display: flex;
    justify-content: center;
    padding: 6px 8px;
    background: var(--bg);
  }
  canvas#board {
    width: auto;
    aspect-ratio: 1 / 1;
    height: min(46dvh, calc(100vw - 16px));
    max-width: calc(100vw - 16px);
  }

  #side {
    flex: 1;
    min-height: 0;
    min-width: 0;
    gap: 0;
    display: flex;
    flex-direction: column;
  }

  /* No persistent hint bar on phones — feedback pops as a transient toast over
     the board (#toast), so the deck keeps every pixel for To-Place / Shop. */
  #hint { display: none; }

  /* anchored to the TOP of the board (over the enemy-spawn rows) so it never
     covers the bottom placement zone where you're tapping */
  #toast {
    display: block;
    position: absolute;
    left: 8px; right: 8px; top: 10px;
    z-index: 15;
    padding: 8px 12px;
    background: rgba(12,14,18,.85);
    border: 1px solid var(--line);
    border-radius: 8px;
    color: var(--txt);
    font-size: 12.5px;
    line-height: 1.35;
    text-align: center;
    pointer-events: none;
    opacity: 0;
    transform: translateY(-6px);
    transition: opacity .25s ease, transform .25s ease;
  }
  #toast.show { opacity: 1; transform: translateY(0); }

  /* segmented tab strip */
  #tabs {
    display: flex;
    flex: none;
    gap: 6px;
    padding: 6px 10px;
    background: var(--bg);
  }
  #tabs .tab {
    flex: 1;
    padding: 10px 6px;
    font-size: 13px;
    font-weight: 600;
    background: var(--panel);
    color: var(--muted);
    border: 1px solid var(--line);
    border-radius: 8px;
  }
  #tabs .tab.active { background: var(--accent); color: #06121f; border-color: var(--accent); }

  /* only the active panel shows, and it owns the vertical scroll */
  .tabpanel { display: none; }
  .tabpanel.active {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    margin: 0 10px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .tabpanel.active h2 { position: sticky; top: 0; background: var(--panel); z-index: 1; }

  /* finger-sized targets (≥44px) */
  .tray { gap: 8px; }
  .tray-item { padding: 10px 12px; font-size: 16px; min-height: 46px; display: flex; align-items: center; }
  .tray-item .glyph { font-size: 22px; }
  .shop-item { padding: 10px 12px; }
  .shop-item .info { font-size: 13px; }
  .shop-item button { padding: 11px 16px; min-height: 44px; font-size: 15px; }
  .legend li { font-size: 12.5px; }

  /* fixed Start-Wave action bar */
  .controls {
    flex: none;
    gap: 8px;
    margin: 0;
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
    border: none;
    border-top: 1px solid var(--line);
    border-radius: 0;
    background: var(--panel);
  }
  .controls button { flex: 1 1 0; padding: 11px 8px; min-height: 46px; font-size: 14px; }
  .controls button.primary { flex-basis: 100%; }   /* Start Wave gets its own full-width row */
  #btnFieldGuide { display: none; }                /* phones use the Guide tab instead */

  /* title card, game-over card and battle banner fit narrow screens */
  .title-card { padding: 26px 18px; }
  .title-card h1 { font-size: clamp(38px, 13vw, 68px); letter-spacing: 5px; }
  .title-tag { font-size: 13px; letter-spacing: 1px; margin-bottom: 16px; }
  .title-blurb { font-size: 13.5px; line-height: 1.55; margin-bottom: 20px; }
  .overlay-card { margin: 16px; padding: 26px 22px; }
  .overlay-card h2 { font-size: 23px; }
  #banner b { font-size: clamp(30px, 9vw, 46px); letter-spacing: 2px; }
  #banner span { font-size: 15px; }
  .vstinger button { right: 14px; bottom: 14px; padding: 10px 16px; }
}
