:root {
  --bg: #f2f2f7;
  --card: #ffffff;
  --text: #0b0b0f;
  --muted: #6c6c72;
  --line: rgba(60, 60, 67, .14);
  --accent: #0a84ff;
  --green: #30d158;
  --red: #ff3b30;
  --glass: rgba(255, 255, 255, .78);
  --fill: rgba(120, 120, 128, .12);
  --shadow: 0 6px 24px rgba(0, 0, 0, .14);
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000;
    --card: #1c1c1e;
    --text: #f5f5f7;
    --muted: #98989f;
    --line: rgba(84, 84, 88, .55);
    --glass: rgba(28, 28, 30, .78);
    --fill: rgba(120, 120, 128, .24);
    --shadow: 0 6px 24px rgba(0, 0, 0, .5);
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overscroll-behavior: none; }
body {
  font: 16px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased; -webkit-user-select: none; user-select: none; position: fixed; inset: 0; overflow: hidden;
}
.hidden { display: none !important; }
button { font: inherit; color: inherit; border: 0; background: none; padding: 0; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.glass { background: var(--glass); -webkit-backdrop-filter: saturate(180%) blur(22px); backdrop-filter: saturate(180%) blur(22px); box-shadow: var(--shadow); }

.screen { position: absolute; inset: 0; }

/* ---------- Danh sách phòng ---------- */
#rooms { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: calc(var(--sat) + 8px) 16px calc(var(--sab) + 24px); }
.large-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 4px 14px; }
.large-head h1 { margin: 0; font-size: 34px; font-weight: 700; letter-spacing: -.02em; }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; }
.icon-btn.accent { background: var(--accent); color: #fff; }
.icon-btn.small { width: 32px; height: 32px; background: var(--fill); }
.icon-btn.small svg { width: 16px; height: 16px; }
.room-list { background: var(--card); border-radius: 16px; overflow: hidden; }
.room { display: flex; align-items: center; gap: 14px; width: 100%; padding: 14px 16px; text-align: left; position: relative; }
.room:active { background: var(--fill); }
.room + .room::before { content: ""; position: absolute; top: 0; left: 62px; right: 0; height: 1px; background: var(--line); }
.room .ico { width: 36px; height: 36px; border-radius: 10px; background: var(--fill); color: var(--accent); display: grid; place-items: center; flex: none; }
.room .ico svg { width: 18px; height: 18px; }
.room .body { flex: 1; min-width: 0; }
.room .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.room .meta { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; margin-top: 1px; }
.room .chev { color: var(--muted); width: 16px; height: 16px; opacity: .6; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; flex: none; }
.dot.on { background: var(--green); }
.dot.warn { background: #ff9f0a; }
.empty { padding: 40px 24px; text-align: center; color: var(--muted); }

/* ---------- Bản đồ ---------- */
#map { position: absolute; inset: 0; background: #e8e6df; }
.leaflet-control-attribution { font-size: 9px; background: rgba(255,255,255,.6) !important; margin-bottom: calc(var(--sab) + 4px) !important; }
.leaflet-control-container .leaflet-bottom { display: none; }
.topbar {
  position: absolute; left: 10px; right: 10px; top: calc(var(--sat) + 8px); z-index: 500;
  border-radius: 20px; display: flex; align-items: center; gap: 4px; padding: 6px;
}
.top-title { flex: 1; min-width: 0; text-align: center; }
.t1 { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.t2 { font-size: 12.5px; color: var(--muted); display: flex; align-items: center; gap: 6px; justify-content: center; white-space: nowrap; }
.fabs { position: absolute; right: 12px; top: calc(var(--sat) + 76px); z-index: 500; display: flex; flex-direction: column; gap: 10px; }
.fab { width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center; }
.fab.on { background: var(--accent); color: #fff; -webkit-backdrop-filter: none; backdrop-filter: none; }
.fab:active { transform: scale(.94); }

.dock {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 500; display: flex; gap: 10px; overflow-x: auto;
  padding: 10px 12px calc(var(--sab) + 12px); scroll-snap-type: x proximity; -webkit-overflow-scrolling: touch;
  transition: transform .28s ease, opacity .2s; scrollbar-width: none;
}
.dock::-webkit-scrollbar { display: none; }
.dock.away { transform: translateY(130%); opacity: 0; pointer-events: none; }
.card {
  flex: none; display: flex; align-items: center; gap: 10px; min-width: 168px; padding: 9px 14px 9px 9px; border-radius: 18px;
  scroll-snap-align: start; background: var(--glass); -webkit-backdrop-filter: saturate(180%) blur(22px); backdrop-filter: saturate(180%) blur(22px); box-shadow: var(--shadow);
}
.card:active { transform: scale(.97); }
.card .info { min-width: 0; text-align: left; }
.card .nm { font-weight: 600; font-size: 15px; max-width: 120px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .sub { font-size: 12.5px; color: var(--muted); white-space: nowrap; }

.avatar {
  width: 42px; height: 42px; border-radius: 50%; flex: none; display: grid; place-items: center; color: #fff;
  font-weight: 700; background-size: cover; background-position: center; position: relative; overflow: hidden;
}
.avatar.big { width: 76px; height: 76px; font-size: 28px; }
.avatar.off { filter: grayscale(1); opacity: .7; }

.route-sheet {
  position: absolute; left: 10px; right: 10px; bottom: calc(var(--sab) + 10px); z-index: 600; border-radius: 24px; padding: 14px;
  animation: up .28s ease;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
.rs-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.rs-who { flex: 1; min-width: 0; }
.rs-who .t1 { font-size: 17px; }
.rs-who .t2 { justify-content: flex-start; }
.seg { display: flex; background: var(--fill); border-radius: 11px; padding: 2px; gap: 2px; }
.seg button { flex: 1; padding: 8px 6px; border-radius: 9px; font-weight: 600; font-size: 14.5px; display: flex; align-items: center; justify-content: center; gap: 7px; color: var(--muted); transition: background .15s; }
.seg button svg { width: 19px; height: 19px; }
.seg button.active { background: var(--card); color: var(--text); box-shadow: 0 1px 4px rgba(0, 0, 0, .15); }
.seg.compact button { padding: 7px 4px; font-size: 14px; }
.rs-stats { display: flex; padding: 14px 4px 12px; }
.rs-stats > div { flex: 1; display: flex; flex-direction: column; }
.rs-stats .big { font-size: 26px; font-weight: 700; letter-spacing: -.02em; }
.rs-stats .lbl { font-size: 12.5px; color: var(--muted); }

/* marker */
.mk { position: absolute; transform: translate(-50%, -100%); display: flex; flex-direction: column; align-items: center; pointer-events: auto; }
.mk .nm { background: var(--card); color: var(--text); font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 10px; box-shadow: 0 2px 8px rgba(0,0,0,.25); margin-bottom: 3px; max-width: 110px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mk .pin { position: relative; width: 46px; height: 46px; }
.mk .pin .avatar { width: 46px; height: 46px; border: 3px solid #fff; box-shadow: 0 3px 10px rgba(0,0,0,.35); }
.mk .pin .kind { position: absolute; right: -4px; bottom: -4px; width: 20px; height: 20px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; border: 2px solid #fff; }
.mk .pin .kind svg { width: 11px; height: 11px; stroke-width: 2.4; }
.mk::after { content: ""; width: 0; height: 0; border: 6px solid transparent; border-top-color: #fff; margin-top: -2px; filter: drop-shadow(0 2px 2px rgba(0,0,0,.25)); }
.mk.me .pin .avatar { border-color: var(--accent); }
.mk.off { opacity: .6; }
.mk.sel .pin .avatar { border-color: #ff9f0a; }
.mk-wrap { background: none; border: 0; }

/* ---------- Sheets ---------- */
.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .38); z-index: 900; animation: fade .2s; }
@keyframes fade { from { opacity: 0; } }
.sheet {
  position: fixed; left: 8px; right: 8px; bottom: calc(var(--sab) + 8px); z-index: 1000; background: var(--card); border-radius: 26px;
  padding: 8px 18px 18px; animation: up .28s ease; max-height: 92dvh; overflow-y: auto; -webkit-user-select: text; user-select: text;
}
.grab { width: 36px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 12px; }
.sheet h2 { margin: 0 0 14px; font-size: 20px; text-align: center; }
.input {
  width: 100%; font-size: 16px; padding: 13px 14px; border-radius: 12px; border: 0; outline: 0; background: var(--fill); color: var(--text);
  margin-bottom: 10px; -webkit-appearance: none; appearance: none;
}
.input:focus { box-shadow: 0 0 0 2px var(--accent) inset; }
.btn { display: block; width: 100%; padding: 14px; border-radius: 14px; font-weight: 600; font-size: 16px; text-align: center; text-decoration: none; }
.btn.primary { background: var(--accent); color: #fff; margin-top: 4px; }
.btn.primary:disabled { opacity: .55; }
.btn.secondary { background: var(--fill); color: var(--accent); }
.btn.ghost { color: var(--muted); margin-top: 4px; }
.btn:active { opacity: .8; }
.err { color: var(--red); font-size: 14px; min-height: 18px; margin: 0 2px 6px; }
.profile-row { display: flex; gap: 14px; align-items: center; margin-bottom: 12px; }
.fields { flex: 1; min-width: 0; }
.fields .input { margin-bottom: 8px; }
.avatar-pick { position: relative; display: block; }
.avatar-pick .cam { position: absolute; right: -2px; bottom: -2px; width: 26px; height: 26px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; border: 2px solid var(--card); }
.avatar-pick .cam svg { width: 13px; height: 13px; }
.menu-item { display: block; width: 100%; text-align: left; padding: 15px 14px; border-radius: 12px; font-size: 17px; }
.menu-item:active { background: var(--fill); }
.menu-item.danger { color: var(--red); }

.qr-box { background: #fff; border-radius: 18px; padding: 14px; width: min(78vw, 280px); margin: 0 auto 12px; line-height: 0; }
.qr-box svg { width: 100%; height: auto; stroke: none; fill: #000; }
.qr-note { text-align: center; color: var(--muted); font-size: 14px; margin: 0 8px 14px; }
.toast {
  position: fixed; left: 50%; top: calc(var(--sat) + 72px); transform: translate(-50%, -12px); z-index: 2000; background: rgba(30, 30, 32, .92); color: #fff;
  padding: 10px 16px; border-radius: 14px; font-size: 14px; opacity: 0; pointer-events: none; transition: .25s; max-width: 86vw; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
