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

html, body {
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}

body {
  background: #010409;
  color: #e6edf3;
  font-family: 'Segoe UI', system-ui, sans-serif;
  height: 100vh;
  height: 100dvh; /* mobile: exclude browser address bar */
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
}

button, .ctrl-btn, .tab-btn {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* ===== Top bar ===== */
#topBar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 14px;
  /* keep clear of iPhone notch / status bar */
  padding-top: calc(8px + env(safe-area-inset-top, 0px));
  padding-left: calc(14px + env(safe-area-inset-left, 0px));
  padding-right: calc(14px + env(safe-area-inset-right, 0px));
  background: #0d1117;
  border-bottom: 1px solid #21262d;
  flex-wrap: wrap;
}
#topBar .logo { font-weight: 800; color: #4a90d9; letter-spacing: 1px; margin-right: 8px; }
.stat { font-size: 13px; color: #8b949e; white-space: nowrap; }
.stat b { color: #e6edf3; font-size: 14px; font-variant-numeric: tabular-nums; }
.stat.cash b { color: #5cd65c; }
.stat.coins b { color: #f0c850; }
.stat.gems b { color: #d65cb0; }
.stat.tokens b { color: #9fd0ff; }

/* ===== Layout ===== */
#main { display: flex; flex: 1; min-height: 0; }
#canvasWrap { flex: 1; position: relative; min-width: 0; }
#gameCanvas { display: block; width: 100%; height: 100%; }

/* ===== Next wave preview ===== */
#wavePreview {
  position: absolute;
  top: 10px; left: 10px;
  max-width: 320px;
  background: rgba(3, 6, 13, 0.82);
  border: 1px solid rgba(0, 255, 220, 0.35);
  box-shadow: 0 0 12px rgba(0, 255, 220, 0.15), inset 0 0 18px rgba(0, 255, 220, 0.05);
  border-radius: 8px;
  padding: 8px 10px;
  pointer-events: none;
}
.wp-title {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 1px;
  color: #5dffe0;
  text-shadow: 0 0 8px rgba(0, 255, 220, 0.6);
  margin-bottom: 5px;
}
.wp-total { color: #8b949e; font-weight: 400; text-shadow: none; }
.wp-list { display: flex; flex-wrap: wrap; gap: 4px 8px; }
.wp-item {
  font-size: 11px;
  font-weight: 600;
  text-shadow: 0 0 6px currentColor;
  white-space: nowrap;
}
.wp-item.wp-boss {
  width: 100%;
  margin-top: 3px;
  padding-top: 4px;
  border-top: 1px dashed rgba(255, 255, 255, 0.2);
  font-size: 12px;
  letter-spacing: 0.5px;
}
.core-fx { color: #5dffe0; }

#canvasControls {
  position: absolute;
  bottom: 8px;
  left: calc(8px + env(safe-area-inset-left, 0px));
  right: calc(8px + env(safe-area-inset-right, 0px));
  display: flex; gap: 5px;
  flex-wrap: wrap;
}

#sidePanel {
  width: 380px;
  background: #0d1117;
  border-left: 1px solid #21262d;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#tabBar { display: flex; flex-wrap: wrap; border-bottom: 1px solid #21262d; }
.tab-btn {
  flex: 1 0 auto;
  padding: 9px 6px;
  background: none;
  border: none;
  color: #8b949e;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  letter-spacing: 0.5px;
}
.tab-btn:hover { color: #e6edf3; }
.tab-btn.active { color: #4a90d9; border-bottom: 2px solid #4a90d9; }
.tab-btn.locked { color: #444c56; }
.tab-btn.locked::after { content: ' 🔒'; font-size: 9px; }

#tabContent { flex: 1; overflow-y: auto; padding: 10px; }
#tabContent::-webkit-scrollbar { width: 8px; }
#tabContent::-webkit-scrollbar-thumb { background: #21262d; border-radius: 4px; }

/* ===== Rows ===== */
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 9px;
  margin-bottom: 5px;
  background: #161b22;
  border: 1px solid #21262d;
  border-radius: 6px;
}
.row-left { min-width: 0; }
.row-name { font-size: 13px; font-weight: 600; }
.row-info { font-size: 11px; color: #8b949e; margin-top: 2px; }
.row-extra { font-size: 11px; color: #8b949e; }
.lvl { color: #4a90d9; font-size: 11px; font-weight: 400; }

.buy-btn {
  background: #1f6feb;
  color: #fff;
  border: none;
  border-radius: 5px;
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}
.buy-btn:hover { background: #388bfd; }
.buy-btn.disabled { background: #21262d; color: #565d66; cursor: not-allowed; }
.btn-group { display: flex; gap: 5px; }

.big-btn {
  display: block;
  width: 100%;
  padding: 10px;
  margin: 6px 0;
  background: #1f6feb;
  color: #fff;
  border: none;
  border-radius: 6px;
  font-weight: 700;
  cursor: pointer;
}
.big-btn:hover { background: #388bfd; }
.big-btn.disabled { background: #21262d; color: #565d66; }
.big-btn.danger { background: #da3633; }
.big-btn.danger:hover { background: #f85149; }
.big-btn.gold { background: rgba(176, 124, 30, 0.9); }
.big-btn.gold:hover { background: #c8901f; }

.row-check { flex: 0 0 auto; width: 16px; height: 16px; cursor: pointer; accent-color: #1f6feb; }

.ctrl-btn {
  /* compact: height locked to font height, minimal padding */
  padding: 2px 8px;
  line-height: 1;
  height: auto;
  background: rgba(31, 111, 235, 0.85);
  color: #fff;
  border: none;
  border-radius: 5px;
  font-weight: 700;
  cursor: pointer;
  font-size: 12px;
}
.ctrl-btn:hover { background: #388bfd; }
.ctrl-btn.gold { background: rgba(176, 124, 30, 0.9); }
.ctrl-btn.gold:hover { background: #c8901f; }
.ctrl-btn.off { background: #21262d; color: #565d66; }
.row-info .next { color: #5cd65c; }
.stat-row { padding: 5px 9px; }
.stat-val { font-size: 12.5px; color: #5cd65c; font-weight: 600; white-space: nowrap; }

/* ===== Sub tabs / titles / hints ===== */
.sub-bar { display: flex; gap: 5px; margin-bottom: 8px; }
.sub-btn {
  flex: 1;
  padding: 7px;
  background: #161b22;
  border: 1px solid #21262d;
  color: #8b949e;
  border-radius: 5px;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}
.sub-btn.active { color: #4a90d9; border-color: #4a90d9; }

.tree-title {
  font-size: 12px;
  color: #f0c850;
  margin: 14px 0 6px;
  letter-spacing: 1px;
}
.hint { font-size: 11.5px; color: #8b949e; margin: 6px 0; line-height: 1.5; }
.hint.done { color: #5cd65c; }
.research-active {
  background: #11261a;
  border: 1px solid #2ea043;
  border-radius: 6px;
  padding: 9px;
  font-size: 12px;
  margin-bottom: 8px;
}

/* ===== Modal ===== */
#modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  z-index: 100;
  align-items: center;
  justify-content: center;
}
#modalBox {
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 10px;
  padding: 24px;
  width: min(440px, 92vw);
  max-height: 85vh;
  overflow-y: auto;
}
#modalBox h2 { font-size: 18px; margin-bottom: 14px; color: #4a90d9; }
.death-stats { font-size: 14px; line-height: 2; margin-bottom: 14px; }
.perk-btn {
  display: block;
  width: 100%;
  padding: 13px;
  margin: 7px 0;
  background: #161b22;
  border: 1px solid #30363d;
  color: #e6edf3;
  border-radius: 7px;
  cursor: pointer;
  font-size: 13px;
  line-height: 1.6;
  text-align: left;
}
.perk-btn:hover { border-color: #4a90d9; background: #1c2430; }
.perk-btn.tradeoff { border-color: #b07c1e; }
.perk-btn b { color: #4a90d9; }
.perk-btn.tradeoff b { color: #f0c850; }
.perk-delta { font-size: 11px; color: #5cd65c; }

/* ===== Settings / leaderboard / toasts ===== */
.settings-form label { display: block; font-size: 12px; color: #8b949e; margin: 7px 0; }
.settings-form input {
  display: block;
  width: 100%;
  margin-top: 3px;
  padding: 7px;
  background: #161b22;
  border: 1px solid #30363d;
  color: #e6edf3;
  border-radius: 5px;
}
.lb-table { width: 100%; border-collapse: collapse; font-size: 12px; margin-top: 6px; }
.lb-table th, .lb-table td { padding: 5px 8px; border-bottom: 1px solid #21262d; text-align: left; }
.lb-table th { color: #8b949e; }

#toasts {
  position: fixed;
  top: 56px; right: 396px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 6px;
  pointer-events: none;
}
.toast {
  background: #1c2430;
  border: 1px solid #4a90d9;
  border-radius: 6px;
  padding: 9px 14px;
  font-size: 12.5px;
  opacity: 0;
  transform: translateX(20px);
  transition: all 0.3s;
  max-width: 300px;
}
.toast.show { opacity: 1; transform: translateX(0); }

@media (max-width: 900px) {
  #main { flex-direction: column; }
  #sidePanel { width: 100%; height: 45%; border-left: none; border-top: 1px solid #21262d; }
  /* keep content clear of bottom address bar / home indicator */
  #tabContent { padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }
  #toasts { right: 10px; }
}

@media (max-width: 600px) {
  #topBar {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    gap: 10px;
    padding: 6px 10px;
    scrollbar-width: none;
  }
  #topBar::-webkit-scrollbar { display: none; }
  #topBar .logo { display: none; }
  .stat { font-size: 11px; flex: 0 0 auto; }
  .stat b { font-size: 12px; }
}
