:root {
  --bg: #0b0d10;
  --panel: rgba(18, 21, 26, 0.92);
  --panel-solid: #14181e;
  --line: #2a313b;
  --text: #f2f4f7;
  --muted: #9aa4b2;
  --accent: #2f9bff;
  --danger: #e5252a;
  --warn: #f5a623;
  --ok: #2ecc71;
  --btn: 60px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--text);
  font: 18px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
}
button { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 15px; }
.fine { font-size: 13px; color: var(--muted); }
.err { color: #ff8a8a; }

/* ---------- Bản đồ ---------- */
#map-wrap { position: fixed; inset: 0; overflow: hidden; background: #111; }
#map {
  position: absolute;
  /* cạnh = đường chéo màn hình => xoay bao nhiêu độ cũng phủ kín */
  width: var(--diag, 150vmax); height: var(--diag, 150vmax);
  left: 50%; top: 50%;
  margin-left: calc(var(--diag, 150vmax) / -2);
  margin-top: calc(var(--diag, 150vmax) / -2);
  transform-origin: 50% 50%;
  background: #1a1d22;
}
#map.animate { transition: transform 0.6s linear; }
body.darkmap .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(0.9) contrast(0.9) saturate(0.6); }
.leaflet-container { background: #1a1d22; font: inherit; }

.me-icon { width: 44px; height: 44px; }
.me-icon svg { width: 44px; height: 44px; filter: drop-shadow(0 2px 4px rgba(0,0,0,.6)); transition: transform .4s linear; }
.me-icon.stale svg { opacity: .45; }

.cam-icon {
  width: 34px; height: 34px; border-radius: 50%;
  display: grid; place-items: center; font-size: 17px;
  border: 3px solid #fff; box-shadow: 0 2px 6px rgba(0,0,0,.6);
  background: var(--danger);
}
.cam-icon.red_light { background: #c0392b; }
.cam-icon.ai { background: #8e44ad; }
.cam-icon.checkpoint { background: #2c3e50; }
.cam-icon.user { border-color: var(--accent); }
.cam-icon.osm { border-color: #7fd37f; }
.cam-icon.active { animation: pulse 0.8s ease-in-out infinite alternate; }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--panel-solid); color: var(--text); }
.leaflet-popup-content { font-size: 16px; margin: 14px 16px; }
.leaflet-popup-content b { font-size: 17px; }
.popup-del { margin-top: 10px; min-height: 48px; width: 100%; border: 0; border-radius: 10px; background: var(--danger); color: #fff; font-weight: 700; }

/* ---------- Trên cùng ---------- */
#top {
  position: fixed; left: 0; right: 0; top: 0; z-index: 1000;
  padding: calc(var(--safe-t) + 8px) calc(var(--safe-r) + 8px) 0 calc(var(--safe-l) + 8px);
  pointer-events: none;
}
#alert-banner {
  display: flex; align-items: center; gap: 12px;
  background: var(--danger); color: #fff;
  border-radius: 16px; padding: 10px 16px; min-height: 76px;
  box-shadow: 0 6px 20px rgba(229, 37, 42, .45);
  animation: slidein .25s ease-out;
}
#alert-banner.near { animation: flash 0.6s steps(2) infinite; }
.ab-icon { font-size: 34px; }
.ab-text { flex: 1; min-width: 0; }
.ab-title { font-weight: 800; font-size: 22px; }
.ab-sub { font-size: 16px; opacity: .92; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ab-dist { font-size: 44px; font-weight: 900; font-variant-numeric: tabular-nums; letter-spacing: -1px; }
.ab-dist small { font-size: 20px; font-weight: 700; margin-left: 2px; }

#badges { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.badge {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 999px; padding: 6px 12px; font-size: 15px; font-weight: 600;
}
.badge.warn { background: var(--warn); color: #111; border-color: transparent; }
.badge.sim { background: var(--accent); color: #fff; border-color: transparent; }
.badge.road { max-width: 60vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#toast {
  margin: 10px auto 0; max-width: 520px; width: fit-content;
  background: var(--panel-solid); border: 1px solid var(--line);
  border-radius: 14px; padding: 12px 16px; font-size: 16px; text-align: center;
}

/* ---------- Nút ---------- */
.fab {
  width: var(--btn); height: var(--btn); min-width: 56px; min-height: 56px;
  border-radius: 18px; border: 1px solid var(--line);
  background: var(--panel); color: var(--text);
  display: grid; place-items: center; font-size: 30px; font-weight: 700;
  box-shadow: 0 4px 12px rgba(0,0,0,.45); cursor: pointer;
}
.fab:active { transform: scale(.95); }
.fab svg { width: 30px; height: 30px; fill: currentColor; }
.fab.on { background: var(--accent); border-color: transparent; color: #fff; }
.fab.busy { opacity: .6; animation: spin 1s linear infinite; }
.fab.small { width: 56px; height: 56px; font-size: 24px; }

#side {
  position: fixed; z-index: 900;
  right: calc(var(--safe-r) + 10px);
  top: 50%; transform: translateY(-58%);
  display: flex; flex-direction: column; gap: 10px;
}

/* ---------- Tốc độ & biển báo ---------- */
#speed-box {
  position: fixed; z-index: 900;
  left: calc(var(--safe-l) + 12px); bottom: calc(var(--safe-b) + 14px);
  min-width: 150px; padding: 8px 18px 10px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 22px;
  text-align: center; box-shadow: 0 6px 18px rgba(0,0,0,.5);
}
#speed-val { font-size: 84px; font-weight: 900; line-height: 1; font-variant-numeric: tabular-nums; letter-spacing: -3px; }
#speed-unit { font-size: 17px; color: var(--muted); font-weight: 600; }
#speed-box.over { background: var(--danger); border-color: transparent; }
#speed-box.over #speed-unit { color: #fff; }

#limit-sign {
  position: fixed; z-index: 900;
  right: calc(var(--safe-r) + 12px); bottom: calc(var(--safe-b) + 14px);
  width: 128px; height: 128px; border-radius: 50%;
  background: #fff; color: #111; border: 14px solid var(--danger);
  display: grid; place-items: center;
  font-size: 52px; font-weight: 900; letter-spacing: -2px;
  box-shadow: 0 6px 18px rgba(0,0,0,.55);
}
#limit-sign.unknown { color: #888; border-color: #777; font-size: 44px; }
#limit-sign.cam { border-style: double; }
#limit-sign.over { animation: signflash .5s steps(2) infinite; }

/* ---------- Giả lập ---------- */
#sim-bar {
  position: fixed; z-index: 950;
  left: 50%; transform: translateX(-50%);
  bottom: calc(var(--safe-b) + 160px);
  display: flex; gap: 4px; align-items: center;
  max-width: calc(100vw - 16px);
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 4px;
}
.sim-rate { display: flex; gap: 4px; }
.sbtn { min-width: 46px; min-height: 52px; padding: 0 4px; border-radius: 12px; border: 1px solid var(--line); background: #222831; font-weight: 700; }
.sbtn.on { background: var(--accent); border-color: transparent; }
.sbtn.danger { background: #3a1517; }
#sim-prog { min-width: 40px; text-align: center; font-variant-numeric: tabular-nums; font-size: 15px; color: var(--muted); }

#attrib {
  position: fixed; z-index: 800; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--safe-b) + 2px); font-size: 11px; color: #aaa;
  background: rgba(0,0,0,.45); padding: 1px 6px; border-radius: 6px;
}
#attrib a { color: #cde; }

/* ---------- Màn hình / bảng ---------- */
.screen {
  position: fixed; inset: 0; z-index: 2000;
  background: radial-gradient(120% 80% at 50% 0%, #1b2a3d 0%, var(--bg) 60%);
  display: grid; place-items: center; overflow-y: auto;
  padding: calc(var(--safe-t) + 24px) 20px calc(var(--safe-b) + 24px);
}
.start-inner { width: 100%; max-width: 460px; display: flex; flex-direction: column; gap: 14px; text-align: center; }
.start-inner h1 { margin: 4px 0; font-size: 32px; line-height: 1.15; }
.logo img { width: 96px; height: 96px; border-radius: 24px; }
.steps { text-align: left; color: var(--muted); }

.big {
  display: flex; align-items: center; justify-content: center; text-align: center;
  min-height: 60px; padding: 12px 18px; width: 100%;
  border-radius: 16px; border: 1px solid var(--line);
  background: #1d232c; font-size: 19px; font-weight: 700; cursor: pointer;
}
.big.primary { background: var(--accent); border-color: transparent; color: #fff; font-size: 22px; min-height: 72px; }
.big.ghost { background: transparent; }
.big:active { transform: scale(.98); }
.big.file { position: relative; }

.sheet {
  position: fixed; inset: 0; z-index: 1500;
  background: var(--panel-solid);
  display: flex; flex-direction: column;
  padding-top: var(--safe-t);
}
.sheet header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px calc(var(--safe-r) + 14px) 10px calc(var(--safe-l) + 18px);
  border-bottom: 1px solid var(--line);
}
.sheet h2 { margin: 0; font-size: 24px; }
.sheet h3 { margin: 22px 0 8px; font-size: 19px; }
.sheet-body > * { flex-shrink: 0; }
.sheet-body { flex: 1; min-height: 0; overflow-y: auto; padding: 8px calc(var(--safe-r) + 18px) calc(var(--safe-b) + 30px) calc(var(--safe-l) + 18px); display: flex; flex-direction: column; gap: 10px; }

.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 60px; border-bottom: 1px solid var(--line); padding: 6px 0; }
.row.col { flex-direction: column; align-items: stretch; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.seg { display: flex; gap: 6px; }
.seg.wrap { flex-wrap: wrap; }
.seg button {
  flex: 1 1 0; min-height: 56px; min-width: 64px;
  border-radius: 12px; border: 1px solid var(--line); background: #1d232c;
  font-weight: 700; cursor: pointer;
}
.seg button.on { background: var(--accent); border-color: transparent; color: #fff; }

.switch {
  appearance: none; -webkit-appearance: none; flex: none;
  width: 64px; height: 36px; border-radius: 999px; background: #3a414b; position: relative; cursor: pointer;
  transition: background .2s;
}
.switch::after {
  content: ""; position: absolute; top: 4px; left: 4px; width: 28px; height: 28px; border-radius: 50%;
  background: #fff; transition: transform .2s;
}
.switch:checked { background: var(--ok); }
.switch:checked::after { transform: translateX(28px); }

dialog {
  width: min(520px, calc(100vw - 24px)); max-height: calc(100vh - 24px);
  background: var(--panel-solid); color: var(--text);
  border: 1px solid var(--line); border-radius: 20px; padding: 18px;
}
dialog::backdrop { background: rgba(0,0,0,.6); }
dialog h2 { margin: 0 0 4px; }
dialog form { display: flex; flex-direction: column; gap: 10px; }
.lbl { font-weight: 700; margin-top: 6px; }

@keyframes pulse { from { transform: scale(1); } to { transform: scale(1.25); } }
@keyframes flash { 0% { background: var(--danger); } 100% { background: #ff5a5f; } }
@keyframes signflash { 0% { background: #fff; color: #111; } 100% { background: var(--danger); color: #fff; } }
@keyframes slidein { from { transform: translateY(-20px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes spin { to { transform: rotate(360deg); } }

/* Màn hình thấp (xoay ngang) */
@media (max-height: 520px) {
  #speed-val { font-size: 60px; }
  #limit-sign { width: 100px; height: 100px; font-size: 40px; border-width: 11px; }
  #side { gap: 6px; }
  #sim-bar { bottom: calc(var(--safe-b) + 8px); }
}
@media (prefers-reduced-motion: reduce) {
  #alert-banner.near, #limit-sign.over, .cam-icon.active { animation-duration: 1.4s; }
}
