:root {
  --bg: #f5f6f4;
  --surface: #ffffff;
  --surface-2: #eef1ee;
  --text: #1b221f;
  --muted: #5f6b66;
  --border: #d9dfdb;
  --accent: #1f6f5c;
  --accent-ink: #ffffff;
  --danger: #b3362b;
  --warn-bg: #fff4dc;
  --warn-ink: #7a5300;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
  --radius: 10px;
  --top-h: 52px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #121614;
    --surface: #1b211e;
    --surface-2: #242b28;
    --text: #e6ebe8;
    --muted: #9aa7a1;
    --border: #333d39;
    --accent: #3fae8f;
    --accent-ink: #0d1a15;
    --danger: #ef7a6e;
    --warn-bg: #3a2f14;
    --warn-ink: #f4cf7a;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }

/* ---------- Top bar ---------- */
.topbar {
  height: var(--top-h);
  padding: 0 16px;
  padding-top: env(safe-area-inset-top);
  display: flex; align-items: center; justify-content: space-between;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: relative; z-index: 1001;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 17px; }
.top-actions { position: relative; }
.menu {
  position: absolute; right: 0; top: 44px; min-width: 220px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 6px; display: flex; flex-direction: column;
}
.menu[hidden] { display: none; }
.menu button {
  text-align: left; background: none; border: 0; padding: 10px 12px; border-radius: 8px; cursor: pointer;
}
.menu button:hover { background: var(--surface-2); }
.menu .danger { color: var(--danger); }

/* ---------- Layout ---------- */
.layout {
  height: calc(100% - var(--top-h));
  display: grid;
  grid-template-columns: 1fr 420px;
}
.map-wrap { position: relative; min-height: 0; }
#map { position: absolute; inset: 0; background: var(--surface-2); }
.panel {
  display: flex; flex-direction: column; min-height: 0;
  background: var(--surface); border-left: 1px solid var(--border);
}
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: 42vh 1fr; }
  .panel { border-left: 0; border-top: 1px solid var(--border); }
}

/* ---------- Map overlays ---------- */
.map-tools { position: absolute; left: 12px; bottom: 12px; z-index: 500; }
.seg {
  display: inline-flex; background: var(--surface); border: 1px solid var(--border);
  border-radius: 999px; padding: 3px; box-shadow: var(--shadow);
}
.seg-btn { border: 0; background: none; padding: 7px 12px; border-radius: 999px; cursor: pointer; font-size: 14px; }
.seg-btn.active { background: var(--accent); color: var(--accent-ink); }
.search { position: absolute; top: 12px; left: 56px; right: 12px; z-index: 500; max-width: 420px; }
.search input {
  width: 100%; padding: 10px 14px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); box-shadow: var(--shadow); outline: none;
}
.search input:focus { border-color: var(--accent); }
.search-results {
  list-style: none; margin: 6px 0 0; padding: 4px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  max-height: 240px; overflow: auto;
}
.search-results[hidden] { display: none; }
.search-results li { padding: 9px 10px; border-radius: 8px; cursor: pointer; font-size: 14px; }
.search-results li:hover, .search-results li:focus { background: var(--surface-2); outline: none; }

/* ---------- Tabs ---------- */
.tabs { display: flex; border-bottom: 1px solid var(--border); overflow-x: auto; flex-shrink: 0; }
.tab {
  flex: 1; border: 0; background: none; padding: 12px 8px; cursor: pointer; white-space: nowrap;
  color: var(--muted); border-bottom: 2px solid transparent; font-weight: 550;
}
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.badge {
  display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 999px; font-size: 12px;
  background: var(--surface-2); color: var(--muted); text-align: center;
}
.tab-body { flex: 1; overflow: auto; padding: 14px 16px; min-height: 0; }
.tab-body[hidden] { display: none; }
.panel-footer {
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border); flex-shrink: 0;
}

/* ---------- Generic ---------- */
.btn {
  border: 1px solid var(--border); background: var(--surface); padding: 9px 14px; border-radius: 8px;
  cursor: pointer; font-weight: 550;
}
.btn:hover { background: var(--surface-2); }
.btn.ghost { border: 0; font-size: 22px; line-height: 1; padding: 6px 10px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.big { width: 100%; padding: 13px; font-size: 16px; }
.btn:disabled { opacity: 0.6; cursor: progress; }
.hint { color: var(--muted); font-size: 13.5px; margin: 0 0 12px; }
.warn { background: var(--warn-bg); color: var(--warn-ink); padding: 10px 12px; border-radius: 8px; margin-bottom: 10px; font-size: 14px; }

.depot-line {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; margin-bottom: 10px;
  background: var(--surface-2); border-radius: var(--radius); font-size: 14px;
}
.depot-line .dot { width: 14px; height: 14px; border-radius: 3px; background: var(--text); flex-shrink: 0; }

.totals { display: flex; gap: 14px; font-size: 13.5px; color: var(--muted); margin-bottom: 8px; flex-wrap: wrap; }
.totals b { color: var(--text); }

.list { list-style: none; margin: 0 0 12px; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.item {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; background: var(--surface);
}
.item.flash { animation: flash 1.2s ease-out; }
@keyframes flash { from { background: var(--surface-2); border-color: var(--accent); } }
.item-head { display: flex; align-items: center; gap: 8px; }
.item-head input.name { flex: 1; min-width: 0; border: 0; background: none; font-weight: 600; padding: 2px 0; outline: none; }
.item-head input.name:focus { border-bottom: 1px solid var(--accent); }
.num {
  width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; flex-shrink: 0;
  background: var(--accent); color: var(--accent-ink); font-size: 12.5px; font-weight: 700;
}
.icon-btn { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 4px 6px; border-radius: 6px; font-size: 16px; }
.icon-btn:hover { background: var(--surface-2); color: var(--danger); }
.grid-fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 8px; margin-top: 8px; }
.grid-fields label { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); gap: 3px; }
.grid-fields input, .field input[type='number'] {
  width: 100%; padding: 7px 9px; border: 1px solid var(--border); border-radius: 7px; background: var(--bg); outline: none;
}
.grid-fields input:focus, .field input:focus { border-color: var(--accent); }

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 16px; }
.field > span { font-weight: 600; }
.field small { color: var(--muted); }
.field.check { flex-direction: row; align-items: center; gap: 10px; }
.field.check input { width: 18px; height: 18px; accent-color: var(--accent); }

/* ---------- Results ---------- */
.kpis { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 14px; }
.kpi { background: var(--surface-2); border-radius: var(--radius); padding: 10px 12px; }
.kpi .k { font-size: 12.5px; color: var(--muted); }
.kpi .v { font-size: 19px; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi.main { grid-column: 1 / -1; background: var(--accent); color: var(--accent-ink); }
.kpi.main .k { color: inherit; opacity: 0.85; }
.kpi.main .v { font-size: 24px; }

.route { border: 1px solid var(--border); border-left: 5px solid var(--c); border-radius: var(--radius); padding: 10px 12px; cursor: pointer; }
.route.sel { box-shadow: 0 0 0 2px var(--c); }
.route-head { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.route-head b { font-size: 15px; }
.route-cost { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.route-meta { font-size: 13px; color: var(--muted); margin: 3px 0 6px; }
.bar { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; margin: 2px 0 6px; }
.bar > i { display: block; height: 100%; background: var(--c); }
.bar-label { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; }
.stops { margin: 6px 0 4px; padding-left: 20px; font-size: 14px; }
.stops li { margin: 2px 0; }
.route-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 6px; }
.route-actions a, .route-actions button {
  font-size: 13px; padding: 6px 10px; border-radius: 7px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); text-decoration: none; cursor: pointer;
}

/* ---------- Map markers ---------- */
.pin {
  width: 26px; height: 26px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg);
  background: var(--c, #1f6f5c); border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
  display: grid; place-items: center;
}
.pin span { transform: rotate(45deg); color: #fff; font-size: 11px; font-weight: 700; }
.depot-pin {
  width: 28px; height: 28px; border-radius: 6px; background: #1b221f; border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5); color: #fff; display: grid; place-items: center; font-size: 14px;
}

.toast {
  position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: 0.2s; z-index: 2000; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Bổ sung sau kiểm thử trên điện thoại ---------- */
.toast { border-radius: 14px; text-align: center; }
.depot-line .btn { padding: 8px 10px; }
.legs-label { width: 100%; font-size: 12.5px; color: var(--muted); }
.dialog-backdrop {
  position: fixed; inset: 0; z-index: 3000; background: rgba(0, 0, 0, 0.45);
  display: grid; place-items: center; padding: 16px;
}
.dialog-backdrop[hidden] { display: none; }
.dialog {
  background: var(--surface); border-radius: 14px; padding: 16px; width: 100%; max-width: 480px;
  box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 10px;
}
.dialog h3 { margin: 0; font-size: 17px; }
.dialog .hint { margin: 0; }
.dialog textarea {
  width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 10px; background: var(--bg);
  font: 13.5px/1.45 ui-monospace, Consolas, monospace; resize: vertical;
}
@media (max-width: 820px) {
  .kpis { grid-template-columns: repeat(2, 1fr); gap: 6px; }
  .kpi { padding: 8px 10px; }
  .kpi .v { font-size: 16px; }
  .kpi.main .v { font-size: 21px; }
  /* Ô nhập ≥16px để iPhone không tự phóng to trang khi chạm vào */
  input, textarea { font-size: 16px !important; }
  .route-actions a, .route-actions button { padding: 9px 12px; }
}

/* ---------- Đồng bộ, danh bạ, lịch sử ---------- */
.top-actions { display: flex; align-items: center; gap: 4px; }
.sync-status {
  border: 0; background: none; cursor: pointer; font-size: 17px; line-height: 1; padding: 6px 8px;
  border-radius: 8px; color: var(--muted);
}
.sync-status[hidden] { display: none; }
.sync-status:hover { background: var(--surface-2); }
.sync-status[data-state='saved'] { color: var(--accent); }
.sync-status[data-state='offline'], .sync-status[data-state='login'], .sync-status[data-state='error'] { color: var(--warn-ink); }
.sync-status[data-state='checking'], .sync-status[data-state='saving'] { animation: spin 1.2s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

#btnPlaces { width: 100%; margin-bottom: 10px; }
.icon-btn.star.on { color: var(--warn-ink); }
.icon-btn.star:hover { color: var(--warn-ink); }

.dialog .filter {
  width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 9px 10px; background: var(--bg);
}
.dialog .places { max-height: min(50vh, 420px); overflow: auto; margin: 0; }
.place-row {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px;
}
.place-row label { flex: 1; display: flex; align-items: center; gap: 10px; cursor: pointer; min-width: 0; }
.place-row input { width: 18px; height: 18px; accent-color: var(--accent); flex-shrink: 0; }
.place-row span { display: flex; flex-direction: column; min-width: 0; }
.place-row b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.place-row small { color: var(--muted); }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; }
.dialog-actions .btn { flex: 1; }

.hist .route-meta { margin-top: 4px; }
.hist-foot { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; }
.hist-foot b { font-size: 16px; }
.muted { color: var(--muted); }
