:root {
  --bg: #f3f6f1;
  --surface: #ffffff;
  --surface-2: #eef2ea;
  --text: #1c2a1f;
  --muted: #5b6b5e;
  --line: #d6dfd2;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --gold: #d99a06;
  --danger: #b42318;
  --ok: #15803d;
  --warn: #b45309;
  --shadow: 0 6px 24px rgba(20, 40, 25, .12);
  color-scheme: light;
}
:root:not([data-theme="light"]) {
  /* dark applied via media query below */
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1116; --surface: #161c24; --surface-2: #1d2530; --text: #e8efe6; --muted: #98a79b;
    --line: #2a3441; --accent: #2dd4bf; --accent-ink: #052a26; --gold: #fbbf24; --danger: #f87171;
    --ok: #4ade80; --warn: #fbbf24; --shadow: 0 6px 24px rgba(0, 0, 0, .45);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0d1116; --surface: #161c24; --surface-2: #1d2530; --text: #e8efe6; --muted: #98a79b;
  --line: #2a3441; --accent: #2dd4bf; --accent-ink: #052a26; --gold: #fbbf24; --danger: #f87171;
  --ok: #4ade80; --warn: #fbbf24; --shadow: 0 6px 24px rgba(0, 0, 0, .45);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) 0 env(safe-area-inset-left, 0);
}

/* top bar */
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 14px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 800; font-size: 17px; letter-spacing: .2px; }
.spacer { flex: 1; }
.goal-btn {
  display: inline-flex; gap: 6px; align-items: center; max-width: 46vw;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px;
}
.goal-btn span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn {
  min-width: 38px; height: 34px; padding: 0 10px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface-2); font-weight: 700;
}

/* KPIs */
.kpis {
  display: grid; grid-template-columns: 2.2fr 1fr 1.4fr 1fr; gap: 8px;
  padding: 8px 14px; background: var(--bg);
}
.kpi {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px; min-width: 0;
}
.kpi-label { font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-value { font-size: 18px; font-weight: 750; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar { height: 6px; border-radius: 99px; background: var(--surface-2); margin-top: 6px; overflow: hidden; }
.bar-fill { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), var(--gold)); transition: width .6s ease; }

/* stage */
.stage { flex: 1; min-height: 0; display: flex; gap: 10px; padding: 0 14px 10px; }
.board {
  position: relative; flex: 1; min-width: 0; border-radius: 16px; overflow: hidden;
  border: 1px solid var(--line); box-shadow: var(--shadow); background: var(--surface-2);
}
#map { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; }
#map:active { cursor: grabbing; }
.board-tools { position: absolute; right: 10px; top: 10px; display: flex; flex-direction: column; gap: 6px; }
.tool {
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); box-shadow: var(--shadow); font-size: 18px; line-height: 1;
}
.speed {
  position: absolute; left: 10px; top: 10px; display: flex; background: var(--surface);
  border: 1px solid var(--line); border-radius: 10px; overflow: hidden; box-shadow: var(--shadow);
}
.speed button { border: 0; background: transparent; padding: 7px 10px; font-weight: 700; }
.speed button.on { background: var(--accent); color: var(--accent-ink); }
.toast {
  position: absolute; left: 50%; top: 14px; transform: translateX(-50%); max-width: min(92%, 440px);
  background: #0f172a; color: #f8fafc; padding: 10px 16px; border-radius: 12px; box-shadow: var(--shadow);
  font-weight: 600; text-align: center; animation: pop .25s ease; z-index: 3;
}
@keyframes pop { from { opacity: 0; transform: translate(-50%, -8px); } }
.empty {
  position: absolute; inset: 0; display: grid; place-content: center; gap: 12px; text-align: center;
  background: color-mix(in srgb, var(--surface) 88%, transparent); padding: 24px; z-index: 2;
}

/* side panel */
.panel {
  width: 340px; flex: none; display: flex; flex-direction: column; min-height: 0;
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow);
}
.sheet-handle { display: none; }
.tabs { display: flex; gap: 4px; padding: 8px; border-bottom: 1px solid var(--line); }
.tabs button {
  flex: 1; padding: 8px 6px; border: 0; border-radius: 10px; background: transparent; font-weight: 650; color: var(--muted);
}
.tabs button.on { background: var(--surface-2); color: var(--text); }
.panel-body { flex: 1; overflow: auto; padding: 14px; min-height: 0; }
.foot { margin: 0; padding: 8px 14px 12px; font-size: 12px; color: var(--muted); }

.h { margin: 0 0 4px; font-size: 18px; font-weight: 800; }
.sub { color: var(--muted); margin: 0 0 12px; }
.stat { display: flex; justify-content: space-between; gap: 10px; padding: 7px 0; border-bottom: 1px dashed var(--line); }
.stat span:first-child { color: var(--muted); }
.stat b { font-variant-numeric: tabular-nums; text-align: right; }
.hint { color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 14px; }
.chip { padding: 4px 10px; border-radius: 999px; border: 1px solid var(--line); font-weight: 700; color: var(--muted); }
.chip.done { background: color-mix(in srgb, var(--gold) 22%, transparent); color: var(--text); border-color: var(--gold); }
.big { font-size: 28px; font-weight: 800; font-variant-numeric: tabular-nums; }
.good { color: var(--ok); } .bad { color: var(--warn); }
.hist { list-style: none; margin: 0; padding: 0; }
.hist li { padding: 8px 0; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; gap: 10px; }
.hist small { display: block; color: var(--muted); }
.skins { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 6px; }
.skins li { display: flex; justify-content: space-between; padding: 6px 10px; background: var(--surface-2); border-radius: 10px; }
.skins li.locked { opacity: .55; }

/* basket, routes, signals */
.badge:not(:empty) {
  display: inline-block; min-width: 18px; margin-left: 6px; padding: 0 5px; border-radius: 99px;
  background: var(--accent); color: var(--accent-ink); font-size: 11px; line-height: 18px; font-weight: 800;
}
.basket-chip {
  position: absolute; left: 50%; bottom: 12px; transform: translateX(-50%); z-index: 2;
  max-width: calc(100% - 90px); overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 999px;
  padding: 10px 18px; font-weight: 750; box-shadow: var(--shadow);
}
.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 3px; margin-bottom: 8px; }
.seg button { flex: 1; border: 0; background: transparent; border-radius: 8px; padding: 7px 4px; font-weight: 650; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.routes { list-style: none; margin: 0; padding: 0; }
.routes li { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.routes small { display: block; color: var(--muted); }
.routes .btn { padding: 6px 12px; white-space: nowrap; }
.basket a { color: var(--accent); font-weight: 750; text-decoration: none; }
.nowrap { white-space: nowrap; }
.x { border: 0; background: transparent; color: var(--muted); padding: 4px 6px; border-radius: 6px; }
.x:hover { color: var(--danger); }

/* buttons */
.btn {
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 12px; padding: 10px 16px; font-weight: 700;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.danger { background: var(--danger); color: #fff; border-color: transparent; }
.btn.block { display: block; width: 100%; margin-top: 10px; padding: 13px 16px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn:focus-visible, .tool:focus-visible, .icon-btn:focus-visible, input:focus-visible, select:focus-visible,
.tabs button:focus-visible, .goal-btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* dialogs */
dialog {
  border: 1px solid var(--line); border-radius: 18px; background: var(--surface); color: var(--text);
  padding: 20px; width: min(440px, calc(100vw - 32px)); box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(8, 14, 10, .55); }
dialog h2 { margin: 0 0 12px; font-size: 20px; }
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; font-weight: 650; }
.field input, .field select {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface-2); color: var(--text); font: inherit;
}
.row { display: flex; gap: 10px; }
.row .grow { flex: 1; }
.pace { margin: 0 0 8px; color: var(--muted); }
.pace b { color: var(--text); }
.error { color: var(--danger); font-weight: 650; margin: 0 0 8px; }
.actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
.strong { font-weight: 750; }
.check { display: flex; gap: 10px; align-items: flex-start; padding: 12px; background: var(--surface-2); border-radius: 12px; margin-top: 10px; }
.check input { width: 20px; height: 20px; margin-top: 1px; accent-color: var(--accent); flex: none; }
.goal-list { display: grid; gap: 8px; max-height: 50dvh; overflow: auto; }
.goal-item { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; background: var(--surface-2); }
.goal-item.active { border-color: var(--accent); }
.goal-item header { display: flex; justify-content: space-between; gap: 8px; font-weight: 750; }
.goal-item .bar { background: var(--line); }
.goal-item .actions { margin-top: 8px; }
.celebrate { text-align: center; }

/* mobile: bottom sheet */
@media (max-width: 820px) {
  .topbar { padding: 6px 10px; }
  .brand span { display: none; }
  .goal-btn { max-width: 52vw; }
  .kpis { grid-template-columns: 1fr 1fr 1fr; padding: 6px 10px; gap: 6px; }
  .kpi-wide { grid-column: 1 / -1; }
  .kpi { padding: 6px 10px; }
  .kpi-value { font-size: 16px; }
  .stage { padding: 0 10px 0; display: block; position: relative; }
  .board { position: absolute; inset: 0 10px calc(54px + env(safe-area-inset-bottom, 0px)) 10px; border-radius: 14px; }
  .panel {
    position: fixed; left: 0; right: 0; bottom: 0; width: auto; max-height: 68dvh;
    border-radius: 18px 18px 0 0; z-index: 5; transition: transform .25s ease;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    transform: translateY(calc(100% - 54px - env(safe-area-inset-bottom, 0px)));
  }
  .panel.open { transform: translateY(0); }
  .sheet-handle { display: block; border: 0; background: transparent; height: 14px; width: 100%; position: relative; }
  .sheet-handle::after { content: ""; position: absolute; left: 50%; top: 5px; width: 40px; height: 4px; margin-left: -20px; border-radius: 4px; background: var(--line); }
  .tabs { padding: 2px 8px 6px; }
  .foot { display: none; }
  .board-tools { top: auto; bottom: 10px; }
  .basket-chip { bottom: 10px; }
  .speed { top: 10px; }
}
@media (max-width: 820px) {
  .kpi-label { white-space: normal; line-height: 1.15; }
  .kpi-value { font-size: 15px; }
}
@media (max-width: 380px) {
  .kpi-label { font-size: 10px; }
}
@media (prefers-reduced-motion: reduce) {
  .bar-fill, .panel, .toast { transition: none; animation: none; }
}

/* clock */
.clock {
  position: absolute; left: 10px; bottom: 10px; z-index: 2; display: grid; gap: 1px;
  background: color-mix(in srgb, var(--surface) 92%, transparent); border: 1px solid var(--line);
  border-radius: 12px; padding: 6px 12px; box-shadow: var(--shadow); max-width: min(260px, 60%);
}
.clock b { font-size: 20px; font-variant-numeric: tabular-nums; }
.clock span { font-weight: 650; }
.clock small { color: var(--muted); }

/* options, charts, advisor */
.opt { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--line); }
.opt small { display: block; color: var(--muted); }
.opt .acts { display: flex; gap: 6px; flex: none; }
.opt .btn { padding: 6px 10px; font-size: 13px; }
.gain { color: var(--ok); font-weight: 700; }
.gain.neg { color: var(--warn); }
.chart { width: 100%; height: auto; display: block; margin: 8px 0 4px; }
.chart text { fill: var(--muted); font-size: 9px; font-family: inherit; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.legend { display: flex; gap: 12px; flex-wrap: wrap; color: var(--muted); font-size: 12px; margin-bottom: 8px; }
.legend i { display: inline-block; width: 12px; height: 3px; border-radius: 2px; margin-right: 5px; vertical-align: middle; }
.status-chip { display: inline-block; padding: 5px 12px; border-radius: 999px; font-weight: 750; margin: 6px 0; }
.status-chip.good { background: color-mix(in srgb, var(--ok) 18%, transparent); }
.status-chip.bad { background: color-mix(in srgb, var(--warn) 20%, transparent); }
.poi-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.poi-row .ico { font-size: 22px; width: 30px; text-align: center; }
.poi-row .grow { flex: 1; min-width: 0; }
.poi-row small { display: block; color: var(--muted); }
.tabs button { padding: 8px 2px; font-size: 13px; }
@media (max-width: 820px) {
  .clock { bottom: auto; top: 54px; left: 10px; max-width: 70%; }
  .kpis { grid-template-columns: 1fr 1fr 1fr; }
}

/* needs */
.needs { list-style: none; margin: 0 0 8px; padding: 0; }
.needs li { display: grid; grid-template-columns: minmax(0, 1.2fr) 1fr 40px; align-items: center; gap: 8px; padding: 4px 0; }
.needs li small { grid-column: 1 / -1; color: var(--danger); margin-top: -2px; }
.needs b { text-align: right; font-variant-numeric: tabular-nums; }
.nbar { height: 7px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.nbar i { display: block; height: 100%; border-radius: 99px; transition: width .5s ease; }
.cityneeds { margin-top: 8px; }

/* account */
.icon-btn { position: relative; }
.dot { position: absolute; right: 4px; top: 4px; width: 8px; height: 8px; border-radius: 50%; background: transparent; }
.dot.on { background: var(--ok); }
.dot.pending { background: var(--gold); }
.dot.off { background: var(--danger); }
.danger-zone { margin-top: 16px; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.danger-zone summary { cursor: pointer; font-weight: 700; color: var(--danger); }

/* quick road building */
.tool.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.basket-chip {
  display: flex; align-items: stretch; padding: 0; overflow: hidden; background: var(--accent);
  max-width: calc(100% - 100px);
}
.basket-chip button { border: 0; background: transparent; color: var(--accent-ink); font-weight: 750; padding: 10px 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.basket-chip .pay { background: var(--gold); color: #1c1400; }
.roadbar {
  position: absolute; left: 50%; top: 10px; transform: translateX(-50%); z-index: 3; display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  justify-content: center; background: var(--surface); border: 1px solid var(--accent); border-radius: 14px;
  padding: 6px 8px; box-shadow: var(--shadow); max-width: calc(100% - 120px);
}
.roadbar .seg { margin: 0; }
.roadbar .seg button { padding: 6px 10px; font-size: 13px; }
.roadhint { flex-basis: 100%; text-align: center; color: var(--muted); font-size: 12px; }
.btn.small { padding: 6px 12px; font-size: 13px; }
.strokeinfo {
  position: absolute; left: 50%; bottom: 62px; transform: translateX(-50%); z-index: 3; background: #0f172a; color: #f8fafc;
  padding: 8px 14px; border-radius: 12px; font-weight: 650; box-shadow: var(--shadow); max-width: calc(100% - 24px); text-align: center;
}
.quick {
  position: absolute; z-index: 4; display: flex; gap: 6px; padding: 6px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); transform: translate(-50%, -100%);
  max-width: calc(100% - 16px); flex-wrap: wrap; justify-content: center;
}
.quick::after {
  content: ""; position: absolute; left: 50%; bottom: -7px; width: 12px; height: 12px; margin-left: -6px; transform: rotate(45deg);
  background: var(--surface); border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
}
.quick.below { transform: translate(-50%, 0); }
.quick.below::after { bottom: auto; top: -7px; border: 0; border-left: 1px solid var(--line); border-top: 1px solid var(--line); }
.quick button {
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 10px; padding: 8px 12px; font-weight: 700; white-space: nowrap;
}
.quick button.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.quick button.warn { color: var(--danger); }
@media (max-width: 820px) {
  .roadbar { top: 8px; max-width: calc(100% - 64px); }
  .basket-chip { bottom: 10px; max-width: calc(100% - 70px); left: 10px; transform: none; }
  .clock { top: auto; bottom: 62px; }
}

/* problems */
.problems {
  position: absolute; left: 10px; top: 56px; z-index: 2; max-width: calc(100% - 70px); text-align: left;
  background: #b91c1c; color: #fff; border: 0; border-radius: 12px; padding: 7px 12px; font-weight: 750;
  box-shadow: var(--shadow); animation: pulse 2.4s ease-in-out infinite;
}
@keyframes pulse { 50% { box-shadow: 0 0 0 5px rgba(220, 38, 38, .25), var(--shadow); } }
.problems.ok { background: var(--ok); animation: none; }
.prob { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.prob small { display: block; color: var(--muted); }
.prob .btn { padding: 6px 10px; font-size: 13px; white-space: nowrap; }
.prob .tag { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 11px; font-weight: 800; color: #fff; margin-right: 6px; }
.tag.jam { background: #dc2626; } .tag.gap { background: #ea580c; } .tag.iso { background: #7c3aed; }
@media (prefers-reduced-motion: reduce) { .problems { animation: none; } }
.prob { flex-wrap: wrap; }
.prob > span:first-child { flex: 1 1 180px; min-width: 0; }
.prob .acts { display: flex; gap: 6px; flex: none; margin-left: auto; }
