:root {
  --navy-0: #050d17;
  --navy-1: #081726;
  --navy-2: #0d2436;
  --navy-3: #12384a;
  --ice: #8ff0e6;
  --ice-soft: #bdf6f0;
  --ice-dim: #5aa9b5;
  --gold: #e2c078;
  --gold-bright: #ffe6a3;
  --gold-dim: #a8864c;
  --text: #e8f3f7;
  --muted: #91aab8;
  --danger: #ff8a7a;
  --panel: linear-gradient(180deg, rgba(10, 27, 42, .96), rgba(5, 15, 26, .98));
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: "Avenir Next", "Segoe UI", "Trebuchet MS", sans-serif;
  --scale: 1;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; width: 100%; height: 100%;
  overflow: hidden; background: #03080e; color: var(--text);
  font-family: var(--sans); user-select: none; -webkit-user-select: none;
  touch-action: manipulation;
}
body {
  background:
    radial-gradient(circle at 50% 30%, #0c1f2e 0%, #03080e 70%);
}
button { font-family: var(--sans); color: var(--text); cursor: pointer; }
.hidden { display: none !important; }

#app {
  position: fixed; inset: 0; overflow: hidden;
  display: flex; align-items: center; justify-content: center;
}

#stage {
  position: absolute; left: 50%; top: 50%;
  width: 1180px; height: 820px; flex: none;
  transform-origin: center center;
  transform: translate(-50%, -50%) scale(var(--scale));
  background: #0a1a27;
  box-shadow: 0 30px 90px rgba(0, 0, 0, .65);
  overflow: hidden;
}

#game { position: absolute; inset: 0; width: 1180px; height: 820px; display: block; }

#hud { position: absolute; inset: 0; pointer-events: none; z-index: 5; }
#hud button, #hud .tower-card, #hud .preview { pointer-events: auto; }

/* ------------------------------- top bar ------------------------------- */
.topbar {
  position: absolute; top: 0; left: 0; right: 0; height: 78px;
  display: flex; align-items: center; gap: 20px; padding: 0 18px;
  background: linear-gradient(180deg, rgba(4, 12, 21, .98) 0%, rgba(6, 19, 31, .92) 72%, rgba(6, 19, 31, 0) 100%);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 224px; }
.crest { width: 40px; height: 40px; filter: drop-shadow(0 0 8px rgba(143, 240, 230, .25)); }
.brand-text { display: flex; align-items: baseline; gap: 8px; }
.brand-name {
  font-family: var(--serif); font-size: 24px; letter-spacing: 2.5px;
  color: #f2fbff; text-shadow: 0 1px 0 #000, 0 0 18px rgba(143, 240, 230, .28);
}
.brand-sub { font-size: 11px; letter-spacing: 2px; color: var(--gold); }

.stats { display: flex; gap: 8px; margin-left: auto; }
.stat {
  display: flex; align-items: center; gap: 7px; height: 40px; padding: 0 12px;
  min-width: 94px; border: 1px solid rgba(143, 240, 230, .16);
  background: linear-gradient(180deg, rgba(18, 49, 68, .72), rgba(7, 21, 34, .78));
  border-radius: 4px;
}
.stat-ico { font-size: 15px; color: var(--ice); }
.stat.lives .stat-ico { color: #ff8a8a; }
.stat.gold .stat-ico { color: var(--gold-bright); }
.stat.wave .stat-ico { color: #b8c9d4; }
.stat-label { font-size: 10px; letter-spacing: 1.4px; text-transform: uppercase; color: var(--muted); }
.stat b { font-size: 17px; font-variant-numeric: tabular-nums; color: #fff; margin-left: 2px; }
.stat.lives b { color: #ffdada; }
.stat.gold b { color: var(--gold-bright); }

.top-actions { display: flex; align-items: center; gap: 7px; }
.btn {
  /* Logical floor for the 44 px physical touch target at the smallest
     landscape scale (1024x768 => 0.8678): 54/56 * 0.8678 = 46.9/48.6 px. */
  min-height: 56px; min-width: 54px; padding: 0 14px; border-radius: 4px; border: 1px solid rgba(143, 240, 230, .26);
  font-family: inherit; -webkit-appearance: none; appearance: none; cursor: pointer;
  background: linear-gradient(180deg, rgba(20, 52, 71, .9), rgba(8, 23, 37, .95));
  color: var(--text); font-size: 13px; font-weight: 600; letter-spacing: .4px;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  transition: border-color .15s, background .15s, transform .05s;
}
.btn:hover { border-color: rgba(143, 240, 230, .55); background: linear-gradient(180deg, rgba(28, 69, 90, .95), rgba(10, 30, 46, .95)); }
.btn:active { transform: translateY(1px); }
.btn-ghost { background: rgba(7, 20, 32, .72); }
.btn.active { border-color: var(--gold-bright); color: var(--gold-bright); background: linear-gradient(180deg, rgba(52, 61, 50, .95), rgba(14, 30, 38, .95)); box-shadow: inset 0 0 0 1px rgba(255, 230, 163, .22); }
.btn-primary {
  border: 1px solid #e2c078;
  background: linear-gradient(180deg, #f3d791 0%, #c99f55 52%, #a17b36 100%);
  color: #1c1508; text-shadow: 0 1px 0 rgba(255, 255, 255, .35);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .55), 0 4px 16px rgba(226, 192, 120, .22);
  font-weight: 800;
}
.btn-primary:hover { background: linear-gradient(180deg, #ffe9ad 0%, #dab166 52%, #b28a41 100%); border-color: #ffe6a3; }
.btn-primary:disabled, .btn:disabled { opacity: .4; cursor: not-allowed; filter: saturate(.5); }
.btn-danger { border-color: rgba(255, 138, 122, .42); color: #ffc8c0; }
.btn-round { width: 54px; min-width: 54px; padding: 0; }
.btn-xl { min-height: 58px; padding: 0 30px; font-size: 18px; letter-spacing: .8px; }
.btn-xl span { font-size: 21px; margin-left: 6px; }

/* ----------------------------- wave banner ----------------------------- */
.wave-banner {
  position: absolute; top: 88px; left: 50%; transform: translateX(-50%);
  min-width: 250px; padding: 8px 20px 9px; text-align: center;
  background: linear-gradient(180deg, rgba(7, 24, 38, .93), rgba(4, 13, 23, .88));
  border: 1px solid rgba(143, 240, 230, .22); border-top-color: rgba(226, 192, 120, .5);
  border-radius: 5px; box-shadow: 0 8px 26px rgba(0, 0, 0, .4);
}
.wb-title { font-family: var(--serif); font-size: 17px; letter-spacing: 1.4px; color: #f4fcff; }
.wb-sub { font-size: 11px; letter-spacing: 1px; color: var(--ice); margin-top: 1px; font-variant-numeric: tabular-nums; }
.wb-sub.warn { color: var(--gold-bright); }
.wb-sub.lost { color: var(--danger); }
.wb-bar { height: 3px; background: rgba(255, 255, 255, .09); margin-top: 6px; border-radius: 2px; overflow: hidden; }
.wb-bar i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--ice-dim), var(--ice), var(--gold-bright)); transition: width .12s linear; }

/* ------------------------------ preview ------------------------------- */
.preview {
  position: absolute; top: 88px; left: 18px; width: 224px; padding: 11px 12px 10px;
  background: var(--panel); border: 1px solid rgba(143, 240, 230, .2);
  border-left: 2px solid var(--gold-dim); border-radius: 5px; box-shadow: 0 10px 30px rgba(0, 0, 0, .35);
}
.preview-head { display: flex; align-items: center; justify-content: space-between; font-size: 9.5px; letter-spacing: 1.5px; color: var(--muted); }
.preview-head b { font-size: 20px; color: var(--gold-bright); font-family: var(--serif); }
.preview-label { font-size: 14px; font-family: var(--serif); color: #eafaff; letter-spacing: .4px; margin: 1px 0 7px; }
.preview-chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip {
  font-size: 10px; padding: 2px 6px; border-radius: 3px; color: #cfeaf0;
  background: rgba(90, 169, 181, .16); border: 1px solid rgba(143, 240, 230, .2);
}
.chip.boss { color: #ffd9a0; border-color: rgba(226, 192, 120, .5); background: rgba(200, 140, 60, .16); }
.chip.more { color: var(--muted); }
.preview-boss { margin-top: 6px; font-size: 10px; letter-spacing: 1.2px; color: #ffd9a0; }

/* -------------------------------- toast -------------------------------- */
.toast {
  position: absolute; left: 50%; bottom: 166px; transform: translateX(-50%);
  padding: 9px 18px; border-radius: 4px; background: rgba(20, 12, 10, .94);
  border: 1px solid rgba(255, 138, 122, .55); color: #ffd8d2;
  font-size: 13px; letter-spacing: .3px; box-shadow: 0 8px 24px rgba(0, 0, 0, .45);
  transition: opacity .2s; z-index: 12;
}

/* ------------------------------ bottom bar ----------------------------- */
.bottombar {
  position: absolute; left: 0; right: 0; bottom: 0; height: 168px;
  display: flex; gap: 14px; padding: 10px 16px 12px;
  background: linear-gradient(0deg, rgba(3, 10, 18, .99) 0%, rgba(6, 18, 30, .97) 78%, rgba(6, 18, 30, .55) 100%);
  border-top: 1px solid rgba(226, 192, 120, .3);
}
.shop { width: 640px; flex: none; display: flex; flex-direction: column; gap: 4px; }
.shop-head { display: flex; align-items: baseline; gap: 10px; }
.shop-title { font-family: var(--serif); font-size: 14px; letter-spacing: 1.6px; color: var(--gold-bright); text-transform: uppercase; }
.shop-hint { font-size: 10px; color: var(--muted); letter-spacing: .5px; }
.cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; height: 110px; }
.tower-card {
  position: relative; text-align: left; padding: 6px 8px; border-radius: 5px;
  font-family: inherit; -webkit-appearance: none; appearance: none; cursor: pointer;
  border: 1px solid rgba(143, 240, 230, .2);
  background: linear-gradient(160deg, rgba(19, 48, 64, .78), rgba(7, 20, 32, .92));
  display: flex; flex-direction: column; gap: 2px; overflow: hidden;
  transition: border-color .15s, background .15s, transform .05s;
}
.tower-card:hover { border-color: rgba(143, 240, 230, .5); }
.tower-card:active { transform: translateY(1px); }
.tower-card.active { border-color: var(--gold-bright); background: linear-gradient(160deg, rgba(55, 73, 66, .9), rgba(17, 35, 42, .96)); box-shadow: inset 0 0 0 1px rgba(255, 230, 163, .35), 0 0 18px rgba(226, 192, 120, .18); }
.tower-card.occupied { opacity: .82; }
.tc-top { display: flex; align-items: center; gap: 5px; min-width: 0; }
.tc-icon { width: 24px; height: 24px; flex: none; filter: drop-shadow(0 2px 3px rgba(0, 0, 0, .4)); }
.tc-name { min-width: 0; flex: 1; font-size: 12px; font-weight: 700; letter-spacing: .2px; color: #effbff; line-height: 1.1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tc-cost { flex: none; font-size: 10px; font-weight: 800; color: var(--gold-bright); background: rgba(0, 0, 0, .28); padding: 1px 3px; border-radius: 3px; white-space: nowrap; }
.tc-role { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 10px; letter-spacing: .3px; color: var(--ice); text-transform: uppercase; line-height: 1.2; }
.tc-stats { margin-top: auto; display: flex; flex-direction: column; gap: 1px; font-size: 10.5px; color: #a9c2ce; font-variant-numeric: tabular-nums; line-height: 1.25; }
.tc-stats > span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tc-stats .tc-effect { color: var(--ice); }
.tc-stats b { color: #e9fbff; font-weight: 700; }
.tc-afford { color: var(--gold) !important; }
.tc-no { color: #ff9e94 !important; }

.inspector {
  flex: 1; min-width: 0; height: 146px; padding: 6px 10px; overflow: hidden;
  border: 1px solid rgba(143, 240, 230, .18);
  border-top-color: rgba(226, 192, 120, .42);
  background: linear-gradient(180deg, rgba(13, 36, 54, .72), rgba(5, 15, 26, .9));
  border-radius: 5px; display: flex; flex-direction: column; gap: 2px;
}
.insp-empty { color: var(--muted); font-size: 12px; line-height: 1.45; height: 100%; display: flex; flex-direction: column; justify-content: center; }
.insp-empty b { color: var(--ice-soft); font-size: 14px; display: block; margin-bottom: 2px; font-family: var(--serif); letter-spacing: .5px; }
.insp-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; min-width: 0; }
.insp-head > div:first-child { min-width: 0; }
.insp-title { font-family: var(--serif); font-size: 16.5px; letter-spacing: .5px; color: #f4fdff; line-height: 1.15; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.insp-title .lvl { font-family: var(--sans); font-size: 9px; letter-spacing: 1.2px; color: #201708; background: var(--gold); padding: 2px 5px; border-radius: 3px; vertical-align: middle; margin-left: 6px; }
.insp-role { font-size: 9.5px; color: var(--ice); letter-spacing: .9px; text-transform: uppercase; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.2; }
.insp-gold { font-size: 11px; color: var(--gold-bright); white-space: nowrap; }
.insp-grid { display: grid; grid-template-columns: .9fr .9fr 1fr 1.3fr; gap: 4px; }
.sg { background: rgba(3, 12, 20, .42); border: 1px solid rgba(143, 240, 230, .1); border-radius: 3px; padding: 3px 5px; min-width: 0; }
.sg span { display: block; font-size: 8.5px; letter-spacing: 1px; color: var(--muted); text-transform: uppercase; }
.sg b { display: block; font-size: 12.5px; font-variant-numeric: tabular-nums; color: #f2fdff; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sg-effect b { font-size: 11.5px; letter-spacing: -.1px; }
.sg i { font-style: normal; color: var(--gold-bright); font-size: 11px; }
.sg i.down { color: #ffb0a4; }
.insp-actions { display: flex; gap: 6px; margin-top: auto; }
.insp-actions .btn { min-height: 54px; flex: 1; font-size: 12px; padding: 0 8px; }
.insp-special { font-size: 9.5px; color: #a9c7d2; line-height: 1.3; white-space: normal; overflow: hidden; }
.insp-special b { color: var(--ice-soft); }
.build-hint { display: flex; align-items: center; gap: 8px; height: 100%; }
.build-hint .bh-ico { font-size: 26px; color: var(--ice); opacity: .8; }
.build-hint .bh-text { font-size: 12px; color: var(--muted); line-height: 1.4; }
.build-hint .bh-text b { color: #eafaff; display: block; font-family: var(--serif); font-size: 15px; }

/* ------------------------------- overlays ------------------------------ */
.overlay {
  position: absolute; inset: 0; z-index: 30; display: flex; align-items: center; justify-content: center;
  background: radial-gradient(circle at 50% 45%, rgba(8, 22, 34, .55), rgba(2, 7, 12, .9));
  backdrop-filter: blur(2px); -webkit-backdrop-filter: blur(2px);
  padding: 26px;
}
.title-panel {
  width: 858px; max-height: 760px; padding: 30px 38px 24px; position: relative;
  background:
    linear-gradient(180deg, rgba(12, 33, 49, .97), rgba(5, 15, 25, .99)),
    radial-gradient(circle at 50% 0, rgba(143, 240, 230, .12), transparent 60%);
  border: 1px solid rgba(226, 192, 120, .55);
  box-shadow: 0 0 0 1px rgba(4, 12, 20, .9), 0 30px 90px rgba(0, 0, 0, .7), inset 0 1px 0 rgba(255, 255, 255, .08);
  border-radius: 6px;
}
.title-panel::before, .title-panel::after {
  content: ""; position: absolute; width: 70px; height: 70px; pointer-events: none;
  border: 1px solid rgba(226, 192, 120, .45);
}
.title-panel::before { top: 8px; left: 8px; border-right: 0; border-bottom: 0; }
.title-panel::after { bottom: 8px; right: 8px; border-left: 0; border-top: 0; }
.title-kicker { text-align: center; font-size: 10px; letter-spacing: 3.4px; color: var(--ice); text-transform: uppercase; }
.title-panel h1 {
  margin: 6px 0 2px; text-align: center; font-family: var(--serif); font-weight: 400;
  font-size: 54px; letter-spacing: 8px; color: #f5fdff;
  text-shadow: 0 2px 0 #000, 0 0 34px rgba(143, 240, 230, .35);
}
.title-panel h1 span { color: var(--gold); font-size: 22px; letter-spacing: 4px; display: inline-block; transform: translateY(-14px); margin-left: 4px; }
.title-lead { text-align: center; margin: 2px 0 20px; font-family: var(--serif); font-size: 16px; color: #a9c6d3; letter-spacing: .6px; }
.tutorial-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin: 0 0 22px; }
.tut { padding: 12px 13px 11px; background: rgba(5, 17, 28, .62); border: 1px solid rgba(143, 240, 230, .16); border-radius: 4px; position: relative; }
.tut-n { position: absolute; top: 9px; right: 11px; font-family: var(--serif); font-size: 20px; color: rgba(226, 192, 120, .38); }
.tut h3 { margin: 0 0 5px; font-family: var(--serif); font-size: 16px; letter-spacing: .8px; color: var(--gold-bright); }
.tut p { margin: 0; font-size: 11px; line-height: 1.5; color: #9fbac7; }
.title-actions { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.title-meta { display: flex; gap: 8px; align-items: center; font-size: 10px; letter-spacing: .7px; color: var(--muted); text-transform: uppercase; }
.title-meta b { color: var(--ice); letter-spacing: 1.2px; }
.title-meta span { padding: 2px 7px; border: 1px solid rgba(143, 240, 230, .17); border-radius: 3px; }
.title-foot { text-align: center; margin: 16px 0 0; font-size: 10.5px; color: #7f9baa; letter-spacing: .4px; }

.modal {
  width: 760px; max-height: 680px; padding: 24px 28px; position: relative;
  background: linear-gradient(180deg, rgba(12, 33, 49, .98), rgba(4, 13, 23, .99));
  border: 1px solid rgba(226, 192, 120, .5); border-radius: 6px;
  box-shadow: 0 30px 90px rgba(0, 0, 0, .7), inset 0 1px 0 rgba(255, 255, 255, .07);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid rgba(143, 240, 230, .14); padding-bottom: 10px; margin-bottom: 14px; }
.modal h2 { margin: 0; font-family: var(--serif); font-weight: 400; font-size: 32px; letter-spacing: 3px; color: #f2fcff; }
.help-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
.help-cols h3 { margin: 0 0 8px; font-family: var(--serif); font-size: 17px; color: var(--gold-bright); letter-spacing: 1px; }
.help-cols ul { margin: 0; padding-left: 17px; }
.help-cols li { font-size: 11.5px; line-height: 1.55; color: #b8cdd7; margin-bottom: 8px; }
.help-cols b { color: #eafaff; }
.modal-foot { margin-top: 16px; padding-top: 10px; border-top: 1px solid rgba(143, 240, 230, .12); font-size: 10px; color: #7f9baa; letter-spacing: .8px; }
.end-modal { width: 560px; text-align: center; padding: 30px 34px; }
.end-modal h2 { font-size: 46px; letter-spacing: 6px; margin: 6px 0 8px; }
.end-modal h2.win { color: var(--gold-bright); text-shadow: 0 0 30px rgba(226, 192, 120, .35); }
.end-modal h2.lose { color: #ff9c8f; text-shadow: 0 0 30px rgba(255, 90, 70, .3); }
.end-modal p { color: #a9c2ce; font-size: 13px; line-height: 1.5; margin: 0 0 16px; }
.end-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 20px; }
.end-stats div { background: rgba(4, 14, 23, .6); border: 1px solid rgba(143, 240, 230, .14); padding: 8px; border-radius: 4px; }
.end-stats span { display: block; font-size: 8.5px; color: var(--muted); letter-spacing: 1px; text-transform: uppercase; }
.end-stats b { font-size: 18px; color: var(--gold-bright); font-variant-numeric: tabular-nums; }
.end-actions { display: flex; gap: 10px; justify-content: center; }

/* ------------------------------ rotate hint ---------------------------- */
.rotate-hint {
  position: absolute; inset: 0; z-index: 60; display: flex; align-items: center; justify-content: center;
  background: rgba(2, 8, 14, .92); text-align: center; padding: 24px;
}
.rotate-hint > div { max-width: 340px; }
.rotate-hint b { display: block; font-family: var(--serif); font-size: 22px; letter-spacing: 1px; color: var(--gold-bright); margin-bottom: 6px; }
.rotate-hint span { display: block; color: #a9c2ce; font-size: 13px; margin-bottom: 18px; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
