:root {
  --bg: #e6f2fb;
  --bar: #ffffff;
  --bar-line: #dbe2ea;
  --green: #2f855a;
  --green-dark: #276749;
  --btn-bg: #ffffff;
  --btn-active: #ffd166;
  --text-dark: #1a202c;
  --muted: #5a6778;
  --danger: #c53030;
}

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

html, body {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  background: var(--bg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text-dark);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

/* ---------- top bar ---------- */

#topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--bar);
  border-bottom: 1px solid var(--bar-line);
  z-index: 10;
}

#brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 800;
  font-size: 17px;
  white-space: nowrap;
}

#levels {
  flex: 1 1 auto;
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  touch-action: pan-x;
}
#levels::-webkit-scrollbar { display: none; }

.level-btn {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 78px;
  padding: 4px 10px;
  border: 2px solid #cbd5e0;
  border-radius: 10px;
  background: var(--btn-bg);
  font-size: 14px;
  font-weight: 700;
  line-height: 1.2;
}
.level-btn .stars { font-size: 12px; letter-spacing: 1px; color: #d69e2e; }
.level-btn .stars .off { color: #cbd5e0; }
.level-btn.active { border-color: var(--green); background: #f0fff4; }

#top-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

#budget {
  min-width: 150px;
  font-size: 13px;
  font-weight: 700;
}
#budget-bar {
  margin-top: 3px;
  height: 8px;
  border-radius: 4px;
  background: #e2e8f0;
  overflow: hidden;
}
#budget-fill {
  height: 100%;
  width: 0;
  background: var(--green);
  transition: width .15s, background .15s;
}
#budget.over #budget-fill { background: var(--danger); }
#budget.over #budget-text { color: var(--danger); }

.icon-btn {
  width: 40px;
  height: 40px;
  border: 1px solid #cbd5e0;
  border-radius: 10px;
  background: var(--btn-bg);
  font-size: 19px;
}

/* ---------- stage ---------- */

#stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
}

#canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

#toast {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  max-width: min(90%, 520px);
  padding: 10px 16px;
  border-radius: 12px;
  background: rgba(26, 32, 44, .88);
  color: #fff;
  font-size: 15px;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s;
}
#toast.show { opacity: 1; }

#hud {
  position: absolute;
  top: 10px;
  left: 12px;
  padding: 6px 12px;
  border-radius: 10px;
  background: rgba(255, 255, 255, .85);
  font-weight: 700;
  font-size: 15px;
  pointer-events: none;
}
#hud:empty { display: none; }
#hud .legend { display: flex; gap: 10px; margin-top: 4px; font-size: 12px; font-weight: 600; color: var(--muted); }
#hud .legend i { display: inline-block; width: 14px; height: 6px; border-radius: 3px; margin-right: 4px; vertical-align: middle; }

#result {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  width: min(92%, 440px);
  padding: 14px 18px;
  border-radius: 16px;
  background: #fff;
  box-shadow: 0 8px 30px rgba(0, 0, 0, .18);
  text-align: center;
  animation: pop .25s ease-out;
}
#result.success { border: 3px solid var(--green); }
#result.fail { border: 3px solid #ed8936; }
#result-stars { font-size: 28px; color: #d69e2e; letter-spacing: 4px; }
#result-stars .off { color: #e2e8f0; }
#result-text { margin: 6px 0 10px; font-size: 16px; font-weight: 600; line-height: 1.35; }
#result-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
#result-actions button {
  padding: 8px 14px;
  border: 0;
  border-radius: 10px;
  background: #edf2f7;
  font-weight: 700;
}
#result-actions button.primary { background: var(--green); color: #fff; }

@keyframes pop {
  from { transform: translateX(-50%) scale(.9); opacity: 0; }
  to { transform: translateX(-50%) scale(1); opacity: 1; }
}

/* ---------- toolbar ---------- */

#toolbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--bar);
  border-top: 1px solid var(--bar-line);
  overflow-x: auto;
  scrollbar-width: none;
  touch-action: pan-x;
}
#toolbar::-webkit-scrollbar { display: none; }

.group {
  display: flex;
  align-items: center;
  gap: 6px;
}
.group[hidden] { display: none; }

.sep {
  width: 1px;
  height: 40px;
  margin: 0 4px;
  background: var(--bar-line);
}

.tool {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: 64px;
  height: 58px;
  padding: 4px 8px;
  border: 2px solid #cbd5e0;
  border-radius: 12px;
  background: var(--btn-bg);
  font-size: 13px;
  font-weight: 700;
}
.tool small { font-size: 10px; font-weight: 600; color: var(--muted); }
.tool.active { border-color: #d69e2e; background: var(--btn-active); }
.tool.active small { color: #744210; }
.tool[hidden] { display: none; }
.tool:disabled { opacity: .4; }
.tool .emoji { font-size: 20px; line-height: 1; }

.swatch {
  width: 30px;
  height: 8px;
  border-radius: 4px;
}
.swatch.wood { background: #c08442; }
.swatch.steel { background: #6f8297; }
.swatch.road { height: 10px; background: linear-gradient(#3d4450 0 40%, #f6e05e 40% 60%, #3d4450 60%); }

#vehicles .label {
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  margin-right: 2px;
  white-space: nowrap;
}
.vehicle-btn .emoji { font-size: 22px; }

.go {
  flex: 0 0 auto;
  margin-left: auto;
  height: 58px;
  padding: 0 22px;
  border: 0;
  border-radius: 14px;
  background: var(--green);
  color: #fff;
  font-size: 18px;
  font-weight: 800;
  box-shadow: 0 3px 0 var(--green-dark);
  white-space: nowrap;
}
.go:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--green-dark); }
.go[hidden] { display: none; }

/* ---------- modal ---------- */

#modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(26, 32, 44, .45);
  z-index: 50;
}
#modal[hidden] { display: none; }
#modal-card {
  width: min(100%, 520px);
  max-height: 90dvh;
  overflow-y: auto;
  touch-action: pan-y;
  padding: 20px 22px;
  border-radius: 18px;
  background: #fff;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .25);
}
#modal-card h2 { margin: 0 0 10px; font-size: 22px; }
#modal-body { font-size: 16px; line-height: 1.45; }
#modal-body ul { padding-left: 0; list-style: none; margin: 0 0 6px; }
#modal-body li { display: flex; gap: 10px; margin-bottom: 10px; }
#modal-body li .ico { flex: 0 0 28px; font-size: 22px; text-align: center; }
#modal-body .note { color: var(--muted); font-size: 14px; }
#modal-close { display: block; margin: 14px auto 0; height: 48px; }

/* ---------- small screens ---------- */

@media (max-width: 760px) {
  #brand span { display: none; }
  #budget { min-width: 110px; font-size: 12px; }
  .tool { min-width: 56px; height: 54px; font-size: 12px; }
  .go { height: 54px; padding: 0 16px; font-size: 16px; }
}
