/* Bridgewright — 16-Bit Medieval & Steampunk Engineering Blueprint Theme */
:root {
  --bg-deep: #0b1120;
  --panel-bg: rgba(15, 23, 42, 0.94);
  --panel-border: rgba(217, 169, 88, 0.42);
  --brass: #eab308;
  --brass-bright: #fde047;
  --amber: #f59e0b;
  --emerald: #10b981;
  --crimson: #ef4444;
  --cyan: #38bdf8;
  --text: #f8fafc;
  --muted: #94a3b8;
  --radius: 12px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  max-width: 100vw;
  min-height: 100dvh;
  overflow-x: hidden;
  background: radial-gradient(circle at top, #172554 0%, #0b1120 65%);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  user-select: none;
  -webkit-user-select: none;
}

body.royal-brass-theme {
  --panel-border: rgba(250, 204, 21, 0.72);
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 8px 10px 6px;
  gap: 7px;
}

/* ==================== TOP HUD BAR ==================== */
.top-hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 7px 11px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
}

.hud-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.brand-badge {
  font-size: 15px;
  font-weight: 900;
  letter-spacing: 0.05em;
  color: var(--brass-bright);
  text-shadow: 0 2px 6px rgba(234, 179, 8, 0.35);
  white-space: nowrap;
}

.level-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: rgba(30, 41, 59, 0.9);
  border: 1px solid rgba(148, 163, 184, 0.3);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 12px;
  font-weight: 700;
  color: #e2e8f0;
  cursor: pointer;
  white-space: nowrap;
}
.level-pill:hover {
  border-color: var(--brass);
  color: #fff;
}

.hud-metrics {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.metric-box {
  display: flex;
  flex-direction: column;
  gap: 1px;
  background: rgba(2, 6, 23, 0.55);
  border: 1px solid rgba(148, 163, 184, 0.22);
  border-radius: 8px;
  padding: 3px 9px;
  min-width: 96px;
}

.metric-label {
  font-size: 9.5px;
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--muted);
  text-transform: uppercase;
}

.metric-val {
  font-size: 13px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: #f8fafc;
}
.metric-val.over-budget { color: #f87171; }
.metric-val.under-par { color: #34d399; }
.metric-val.stress-safe { color: #34d399; }
.metric-val.stress-warn { color: #fbbf24; }
.metric-val.stress-crit { color: #f87171; }

.stress-bar-track {
  width: 100%;
  height: 4px;
  background: rgba(148, 163, 184, 0.2);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 2px;
}

.stress-bar-fill {
  height: 100%;
  width: 0%;
  background: var(--emerald);
  transition: width 0.08s linear, background-color 0.12s ease;
}

.hud-actions {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-wrap: wrap;
}

.hud-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  background: rgba(30, 41, 59, 0.92);
  color: #e2e8f0;
  border: 1px solid rgba(148, 163, 184, 0.3);
  border-radius: 8px;
  padding: 5px 9px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  transition: transform 0.1s ease, border-color 0.14s ease, background-color 0.14s ease;
}
.hud-btn:hover {
  border-color: var(--brass);
  background: rgba(51, 65, 85, 0.95);
  color: #fff;
}
.hud-btn:active { transform: translateY(1px); }
.hud-btn.daily-btn {
  background: linear-gradient(135deg, rgba(14, 116, 144, 0.9), rgba(29, 78, 216, 0.85));
  border-color: rgba(56, 189, 248, 0.55);
  color: #fff;
}
.hud-btn.guild-btn {
  background: linear-gradient(135deg, rgba(120, 53, 15, 0.85), rgba(161, 98, 7, 0.85));
  border-color: rgba(250, 204, 21, 0.55);
  color: #fef08a;
}

/* ==================== CHALLENGE BANNER ==================== */
.challenge-banner {
  background: linear-gradient(90deg, rgba(180, 83, 9, 0.32), rgba(234, 179, 8, 0.2), rgba(180, 83, 9, 0.32));
  border: 1px solid rgba(250, 204, 21, 0.55);
  border-radius: 9px;
  padding: 5px 12px;
  font-size: 12px;
  font-weight: 700;
  color: #fef08a;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.challenge-banner.hidden { display: none; }

/* ==================== STAGE CANVAS WRAP ==================== */
.stage-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  max-height: calc(100dvh - 205px);
  margin: 0 auto;
  background: #0f172a;
  border: 2px solid var(--panel-border);
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.55);
  touch-action: none;
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  cursor: crosshair;
}

.stage-tip-bar {
  position: absolute;
  top: 8px;
  left: 10px;
  right: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
  z-index: 5;
}

.tip-chip {
  background: rgba(15, 23, 42, 0.82);
  border: 1px solid rgba(217, 169, 88, 0.38);
  border-radius: 999px;
  padding: 4px 11px;
  font-size: 11.5px;
  font-weight: 700;
  color: #f1f5f9;
  backdrop-filter: blur(4px);
  max-width: 72%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.convoy-chip {
  background: rgba(15, 23, 42, 0.86);
  border: 1px solid rgba(56, 189, 248, 0.42);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 11px;
  font-weight: 800;
  color: #bae6fd;
  white-space: nowrap;
}

.fail-banner {
  position: absolute;
  bottom: 14px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(127, 29, 29, 0.94);
  border: 2px solid #f87171;
  border-radius: 12px;
  padding: 8px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.55);
  z-index: 12;
  max-width: 92%;
}
.fail-banner.hidden { display: none; }
.fail-msg {
  font-size: 13px;
  font-weight: 800;
  color: #fee2e2;
}
.fail-edit-btn {
  background: #f59e0b;
  color: #0f172a;
  border: none;
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 900;
  cursor: pointer;
  white-space: nowrap;
}
.fail-edit-btn:hover { background: #fbbf24; }

/* ==================== BUILDER TOOLBAR ==================== */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 7px 10px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
}

.tool-group {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-wrap: wrap;
}

.mat-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  min-width: 72px;
  padding: 5px 8px;
  background: rgba(30, 41, 59, 0.9);
  border: 2px solid rgba(148, 163, 184, 0.25);
  border-radius: 10px;
  color: #e2e8f0;
  cursor: pointer;
  transition: transform 0.1s ease, border-color 0.12s ease, background-color 0.12s ease;
}
.mat-btn:hover {
  border-color: rgba(250, 204, 21, 0.65);
  background: rgba(51, 65, 85, 0.92);
}
.mat-btn.active {
  background: rgba(180, 83, 9, 0.38);
  border-color: var(--brass-bright);
  box-shadow: 0 0 12px rgba(234, 179, 8, 0.32);
  color: #fff;
}
.mat-btn.erase-btn.active {
  background: rgba(185, 28, 28, 0.42);
  border-color: #f87171;
  box-shadow: 0 0 12px rgba(239, 68, 68, 0.32);
}

.mat-top {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}
.mat-cost {
  font-size: 10.5px;
  font-weight: 700;
  color: #fde68a;
  font-variant-numeric: tabular-nums;
}
.mat-btn.erase-btn .mat-cost { color: #fca5a5; }

.util-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 7px 10px;
  background: rgba(30, 41, 59, 0.92);
  border: 1px solid rgba(148, 163, 184, 0.3);
  border-radius: 9px;
  color: #e2e8f0;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}
.util-btn:hover {
  border-color: var(--cyan);
  color: #fff;
}
.util-btn.hint-btn {
  border-color: rgba(56, 189, 248, 0.55);
  color: #7dd3fc;
}

.sim-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 18px;
  border-radius: 10px;
  border: 2px solid #4ade80;
  background: linear-gradient(135deg, #059669, #10b981);
  color: #ffffff;
  font-size: 14px;
  font-weight: 900;
  letter-spacing: 0.03em;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: 0 4px 16px rgba(16, 185, 129, 0.42);
  transition: transform 0.1s ease, filter 0.12s ease;
}
.sim-btn:hover { filter: brightness(1.1); transform: translateY(-1px); }
.sim-btn:active { transform: translateY(1px); }
.sim-btn.testing {
  background: linear-gradient(135deg, #d97706, #f59e0b);
  border-color: #fde047;
  color: #0f172a;
  box-shadow: 0 4px 16px rgba(245, 158, 11, 0.45);
}

/* ==================== FOOTER BAR ==================== */
.footer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 6px 10px;
  padding: 4px 6px 2px;
  font-size: 11.5px;
  color: var(--muted);
}

/* ==================== OVERLAYS & MODALS ==================== */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(4, 8, 18, 0.78);
  backdrop-filter: blur(5px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
}
.overlay.hidden { display: none; }

.card {
  width: 100%;
  max-width: 470px;
  max-height: min(90dvh, 720px);
  overflow-y: auto;
  background: linear-gradient(180deg, #162038, #0f172a);
  border: 2px solid var(--panel-border);
  border-radius: 16px;
  padding: 18px 20px;
  box-shadow: 0 22px 60px rgba(0, 0, 0, 0.65);
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 11px;
}
.card.wide { max-width: 620px; text-align: left; }

.card-title {
  margin: 0;
  font-size: 22px;
  font-weight: 900;
  color: var(--brass-bright);
  letter-spacing: 0.03em;
}
.card-sub {
  margin: 0;
  font-size: 13px;
  color: #cbd5e1;
  line-height: 1.45;
}

.star-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-size: 32px;
  line-height: 1;
}
.star-off { opacity: 0.25; filter: grayscale(1); }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}
.stat-cell {
  background: rgba(2, 6, 23, 0.55);
  border: 1px solid rgba(148, 163, 184, 0.22);
  border-radius: 10px;
  padding: 8px 6px;
}
.stat-k { font-size: 10px; font-weight: 700; color: var(--muted); text-transform: uppercase; }
.stat-v { font-size: 15px; font-weight: 900; color: #f8fafc; margin-top: 2px; }

.cliffhanger-box {
  background: rgba(234, 179, 8, 0.12);
  border: 1px solid rgba(250, 204, 21, 0.45);
  border-radius: 10px;
  padding: 8px 11px;
  font-size: 12px;
  font-weight: 700;
  color: #fef08a;
  text-align: left;
}

.btn-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
}

.primary-btn {
  padding: 10px 18px;
  border-radius: 10px;
  border: 2px solid #fde047;
  background: linear-gradient(135deg, #d97706, #eab308);
  color: #0f172a;
  font-size: 14px;
  font-weight: 900;
  cursor: pointer;
}
.primary-btn:hover { filter: brightness(1.08); }

.secondary-btn {
  padding: 9px 14px;
  border-radius: 10px;
  border: 1px solid rgba(148, 163, 184, 0.35);
  background: rgba(30, 41, 59, 0.92);
  color: #f1f5f9;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.secondary-btn:hover { border-color: var(--cyan); color: #fff; }

.share-preview {
  background: rgba(2, 6, 23, 0.7);
  border: 1px solid rgba(56, 189, 248, 0.32);
  border-radius: 9px;
  padding: 8px 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  color: #bae6fd;
  text-align: left;
  white-space: pre-wrap;
  word-break: break-word;
}
.share-preview.hidden { display: none; }

/* Level Select Grid */
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border-bottom: 1px solid rgba(148, 163, 184, 0.2);
  padding-bottom: 8px;
}
.close-x {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 1px solid rgba(148, 163, 184, 0.3);
  background: rgba(30, 41, 59, 0.9);
  color: #e2e8f0;
  font-size: 15px;
  font-weight: 800;
  cursor: pointer;
}

.levels-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}
.lvl-tile {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 9px 10px;
  border-radius: 10px;
  background: rgba(30, 41, 59, 0.85);
  border: 1px solid rgba(148, 163, 184, 0.25);
  color: #f8fafc;
  text-align: left;
  cursor: pointer;
}
.lvl-tile:hover:not(:disabled) { border-color: var(--brass); background: rgba(51, 65, 85, 0.95); }
.lvl-tile.current { border-color: var(--cyan); box-shadow: 0 0 0 1px var(--cyan); }
.lvl-tile:disabled { opacity: 0.45; cursor: not-allowed; }
.lvl-num { font-size: 10.5px; font-weight: 800; color: #94a3b8; text-transform: uppercase; }
.lvl-name { font-size: 12.5px; font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lvl-stars { font-size: 12px; color: #facc15; }
.lvl-best { font-size: 10px; color: #34d399; }

/* Guild Hall Perks & Leaderboard */
.perk-list {
  display: flex;
  flex-direction: column;
  gap: 7px;
}
.perk-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 11px;
  border-radius: 10px;
  background: rgba(30, 41, 59, 0.72);
  border: 1px solid rgba(148, 163, 184, 0.2);
}
.perk-row.unlocked {
  background: rgba(20, 83, 45, 0.32);
  border-color: rgba(52, 211, 153, 0.45);
}
.perk-info { display: flex; flex-direction: column; gap: 2px; }
.perk-name { font-size: 13px; font-weight: 800; color: #f8fafc; }
.perk-desc { font-size: 11.5px; color: #94a3b8; }
.perk-req {
  font-size: 11px;
  font-weight: 800;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.85);
  color: #fde047;
  white-space: nowrap;
}
.perk-row.unlocked .perk-req { color: #34d399; }

.lb-submit-row {
  display: flex;
  gap: 6px;
  align-items: center;
  justify-content: center;
}
.lb-submit-row.hidden { display: none; }
.lb-input {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid rgba(148, 163, 184, 0.35);
  background: rgba(2, 6, 23, 0.75);
  color: #f8fafc;
  font-size: 13px;
  font-weight: 700;
}
.lb-list {
  display: flex;
  flex-direction: column;
  gap: 5px;
  max-height: 240px;
  overflow-y: auto;
}
.lb-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  border-radius: 8px;
  background: rgba(30, 41, 59, 0.75);
  font-size: 12.5px;
  font-weight: 700;
}
.lb-rank { color: #fde047; width: 28px; }
.lb-name { flex: 1; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-score { color: #34d399; font-variant-numeric: tabular-nums; }

.start-features {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
  text-align: left;
  font-size: 11.5px;
  color: #cbd5e1;
}
.start-feat {
  background: rgba(2, 6, 23, 0.52);
  border: 1px solid rgba(148, 163, 184, 0.2);
  border-radius: 8px;
  padding: 6px 8px;
}

/* ==================== MOBILE RESPONSIVE (390×844 ZERO OVERFLOW) ==================== */
@media (max-width: 720px) {
  #app {
    padding: 6px 6px 6px;
    gap: 5px;
  }
  .top-hud {
    padding: 6px 8px;
    gap: 6px;
  }
  .brand-badge { font-size: 13.5px; }
  .level-pill { font-size: 11px; padding: 3px 8px; }
  .hud-metrics {
    width: 100%;
    justify-content: space-between;
    gap: 6px;
  }
  .metric-box {
    flex: 1;
    min-width: 0;
    padding: 3px 7px;
  }
  .metric-val { font-size: 12px; }
  .hud-actions {
    width: 100%;
    justify-content: space-between;
  }
  .hud-btn {
    padding: 5px 7px;
    font-size: 11px;
    flex: 1;
  }
  .stage-wrap {
    max-height: none;
    border-radius: 10px;
  }
  .stage-tip-bar {
    top: 4px;
    left: 6px;
    right: 6px;
  }
  .tip-chip {
    font-size: 10px;
    padding: 2px 7px;
    max-width: 64%;
  }
  .convoy-chip {
    font-size: 9.5px;
    padding: 2px 7px;
  }
  .toolbar {
    padding: 6px 6px;
    gap: 6px;
  }
  .tool-group {
    width: 100%;
    justify-content: space-between;
    gap: 4px;
  }
  .mat-btn {
    flex: 1;
    min-width: 0;
    padding: 5px 3px;
  }
  .mat-top { font-size: 10.5px; gap: 2px; }
  .mat-cost { font-size: 9.5px; }
  .util-btn {
    flex: 1;
    justify-content: center;
    padding: 6px 5px;
    font-size: 11px;
  }
  .sim-btn {
    width: 100%;
    padding: 10px 14px;
    font-size: 14px;
  }
  .levels-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .footer-bar {
    font-size: 10.5px;
    justify-content: center;
    text-align: center;
    padding-bottom: 4px;
  }
}
