:root {
  --bg: #f5f5f7;
  --card: #ffffff;
  --card-2: #f7f7f9;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --border: #e3e3e8;
  --accent: #0071e3;
  --accent-soft: #e8f1fb;
  --green: #34c759;
  --amber: #ff9500;
  --purple: #af52de;
  --red: #ff3b30;
  --bubble-in: #e9e9eb;
  --bubble-out: #0071e3;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.06);
  --radius: 16px;
}

[data-theme="dark"] {
  --bg: #0d0d0f;
  --card: #1c1c1e;
  --card-2: #232326;
  --text: #f5f5f7;
  --muted: #98989d;
  --border: #333336;
  --accent: #0a84ff;
  --accent-soft: #10233a;
  --bubble-in: #2c2c2e;
  --bubble-out: #0a84ff;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.4);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--text);
  -webkit-font-smoothing: antialiased;
  transition: background .25s ease, color .25s ease;
}
.app { max-width: 1440px; margin: 0 auto; padding: 16px; display: flex; flex-direction: column; gap: 14px; }

/* top bar */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-icon { color: var(--accent); }
.brand-name { font-weight: 700; font-size: 20px; letter-spacing: -.02em; }
.brand-sub { color: var(--muted); font-size: 13px; }
.topbar-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.token-wrap { display: flex; align-items: center; gap: 8px; background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 0 12px; height: 38px; }
.token-wrap i { color: var(--muted); width: 16px; height: 16px; }
.token-wrap input { border: 0; background: transparent; color: var(--text); font-size: 14px; width: 140px; outline: none; }

.btn { display: inline-flex; align-items: center; gap: 7px; height: 38px; padding: 0 14px; border-radius: 10px; border: 1px solid transparent; font-size: 14px; font-weight: 500; cursor: pointer; transition: all .15s ease; }
.btn i { width: 16px; height: 16px; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.06); }
.btn-ghost { background: var(--card); border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: var(--card-2); }
.btn-icon { width: 38px; padding: 0; justify-content: center; background: var(--card); border-color: var(--border); color: var(--text); }
.btn-icon:hover { background: var(--card-2); }

/* filters */
.filters { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.date-filter { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.presets { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { height: 34px; padding: 0 14px; border-radius: 20px; border: 1px solid var(--border); background: var(--card); color: var(--text); font-size: 13px; cursor: pointer; transition: all .15s ease; }
.chip:hover { background: var(--card-2); }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.custom-range { display: none; align-items: center; gap: 6px; }
.custom-range.visible { display: flex; }
.custom-range input { height: 34px; border: 1px solid var(--border); border-radius: 8px; background: var(--card); color: var(--text); padding: 0 8px; font-size: 13px; }
.period-label { color: var(--muted); font-size: 13px; }

/* device select */
.device-select { position: relative; }
.select-trigger { display: flex; align-items: center; gap: 8px; height: 38px; padding: 0 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--card); color: var(--text); font-size: 14px; cursor: pointer; }
.select-trigger i { width: 16px; height: 16px; color: var(--muted); }
.select-menu { position: absolute; top: 44px; left: 0; z-index: 1300; width: 300px; background: var(--card); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); padding: 8px; display: none; max-height: 360px; overflow: auto; }
.select-menu.open { display: block; }
.select-search { display: flex; align-items: center; gap: 8px; border: 1px solid var(--border); border-radius: 8px; padding: 0 10px; height: 34px; margin-bottom: 8px; }
.select-search i { color: var(--muted); width: 15px; height: 15px; }
.select-search input { border: 0; background: transparent; color: var(--text); font-size: 13px; outline: none; flex: 1; }
.opt { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; cursor: pointer; font-size: 14px; }
.opt:hover { background: var(--card-2); }
.opt input { accent-color: var(--accent); }
.opt .meta { margin-left: auto; color: var(--muted); font-size: 11px; }
.opt .edit { color: var(--muted); cursor: pointer; background: transparent; border: 0; padding: 4px; border-radius: 6px; display: inline-flex; align-items: center; justify-content: center; }
.opt .edit:hover { color: var(--accent); background: var(--card-2); }
.opt .edit i { width: 15px; height: 15px; }

/* stats */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; align-items: center; }
.stat-card { display: flex; align-items: center; gap: 12px; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat-icon { width: 40px; height: 40px; border-radius: 11px; display: flex; align-items: center; justify-content: center; }
.stat-icon i { width: 20px; height: 20px; color: #fff; }
.stat-icon.blue { background: var(--accent); }
.stat-icon.green { background: var(--green); }
.stat-icon.amber { background: var(--amber); }
.stat-icon.purple { background: var(--purple); }
.stat-value { font-size: 22px; font-weight: 700; letter-spacing: -.02em; }
.stat-label { font-size: 12px; color: var(--muted); }
.last-update { grid-column: 1 / -1; font-size: 12px; color: var(--muted); }

/* main tabs */
.main-tabs { display: flex; gap: 4px; background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 4px; width: fit-content; box-shadow: var(--shadow); }
.main-tabs .tab { display: inline-flex; align-items: center; gap: 7px; height: 36px; padding: 0 16px; border: 0; background: transparent; color: var(--muted); font-size: 14px; border-radius: 9px; cursor: pointer; }
.main-tabs .tab i { width: 16px; height: 16px; }
.main-tabs .tab.active { background: var(--accent); color: #fff; }

/* views */
.view { display: none; }
.view.active { display: block; }
.content { min-height: 480px; }

/* cards */
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; display: flex; flex-direction: column; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.card-title { display: flex; align-items: center; gap: 8px; }
.card-title i { color: var(--accent); width: 18px; height: 18px; }
.card-title h2 { font-size: 15px; margin: 0; font-weight: 600; }

/* map */
.map-wrap { position: relative; height: 440px; }
#map { height: 440px; background: var(--card-2); position: relative; z-index: 1; }
.map-empty { position: absolute; inset: 0; z-index: 500; background: var(--card); display: none; flex-direction: column; align-items: center; justify-content: center; gap: 8px; color: var(--muted); font-size: 13px; }
.map-empty i { width: 32px; height: 32px; opacity: .5; }

.map-controls { position: absolute; top: 12px; right: 12px; z-index: 600; display: flex; flex-direction: column; gap: 6px; }
.map-controls button { width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--border); background: var(--card); color: var(--text); cursor: pointer; display: flex; align-items: center; justify-content: center; box-shadow: var(--shadow); }
.map-controls button:hover { background: var(--card-2); }
.map-controls button.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.map-controls i { width: 16px; height: 16px; }
.legend { position: absolute; bottom: 12px; left: 12px; z-index: 600; background: var(--card); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); padding: 8px 10px; display: flex; flex-direction: column; gap: 4px; max-width: 200px; }
.legend-item { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--text); cursor: pointer; }
.legend-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }

.playback-bar { display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.playback-bar input[type="range"] { flex: 1; min-width: 120px; accent-color: var(--accent); }
.playback-bar .btn-icon.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.scrub-label { font-size: 12px; color: var(--muted); min-width: 44px; text-align: right; }
.play-time { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; min-width: 76px; text-align: right; }
.speed { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.speed button { border: 0; background: var(--card); color: var(--muted); font-size: 12px; padding: 6px 9px; cursor: pointer; }
.speed button.active { background: var(--accent); color: #fff; }
.activity-tabs { display: inline-flex; background: var(--card-2); border: 1px solid var(--border); border-radius: 10px; padding: 3px; margin-bottom: 12px; }
.activity-tabs .seg { border: 0; background: transparent; color: var(--muted); font-size: 14px; padding: 7px 16px; border-radius: 8px; cursor: pointer; }
.activity-tabs .seg.active { background: var(--card); color: var(--text); box-shadow: var(--shadow); }
.subview { display: none; }
.subview.active { display: block; }
.timeline { max-height: 170px; overflow: auto; border-top: 1px solid var(--border); padding: 8px; display: flex; flex-direction: column; gap: 2px; }
.timeline-item { display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: 8px; border: 0; background: transparent; color: var(--text); font-size: 13px; text-align: left; cursor: pointer; }
.timeline-item:hover { background: var(--card-2); }
.timeline-item.active { background: var(--accent-soft); }
.timeline-item .t { font-variant-numeric: tabular-nums; color: var(--muted); min-width: 76px; }
.timeline-item .c { font-variant-numeric: tabular-nums; }
.timeline-item .a { margin-left: auto; color: var(--muted); font-size: 12px; }
.gap { color: var(--muted); font-size: 11px; text-align: center; padding: 4px; }

/* split panels */
.split { display: grid; grid-template-columns: 360px 1fr; gap: 14px; height: 620px; }
.split-list { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); display: flex; flex-direction: column; overflow: hidden; }
.split-detail { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); display: flex; flex-direction: column; overflow: hidden; }
.panel-head { display: flex; flex-direction: column; gap: 8px; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.panel-head h2 { margin: 0; font-size: 16px; font-weight: 600; }
.list-scroll { flex: 1; overflow: auto; padding: 6px; }
.detail-empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; color: var(--muted); font-size: 13px; }
.detail-empty i { width: 32px; height: 32px; opacity: .5; }

.search { display: flex; align-items: center; gap: 8px; border: 1px solid var(--border); border-radius: 10px; padding: 0 12px; height: 36px; background: var(--card-2); }
.search i { color: var(--muted); width: 15px; height: 15px; }
.search input { border: 0; background: transparent; color: var(--text); font-size: 14px; outline: none; flex: 1; }

/* conversations */
.conv-item { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 12px; cursor: pointer; }
.conv-item:hover { background: var(--card-2); }
.conv-avatar { width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 600; font-size: 15px; flex-shrink: 0; }
.conv-body { flex: 1; min-width: 0; }
.conv-top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.conv-name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { color: var(--muted); font-size: 11px; flex-shrink: 0; }
.conv-bottom { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 2px; }
.conv-preview { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-count { background: var(--accent); color: #fff; font-size: 11px; font-weight: 600; border-radius: 10px; padding: 1px 7px; flex-shrink: 0; }

/* chat */
.chat { display: flex; flex-direction: column; height: 100%; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.chat-back { width: 34px; height: 34px; border-radius: 9px; border: 1px solid var(--border); background: var(--card); color: var(--text); cursor: pointer; display: flex; align-items: center; justify-content: center; }
.chat-back i { width: 16px; height: 16px; }
.chat-contact { font-weight: 600; font-size: 15px; }
.chat-device { color: var(--muted); font-size: 12px; }
.bubbles { flex: 1; overflow: auto; padding: 14px; display: flex; flex-direction: column; gap: 6px; }
.date-sep { align-self: center; color: var(--muted); font-size: 11px; background: var(--card-2); padding: 3px 10px; border-radius: 12px; margin: 8px 0; }
.bubble { max-width: 72%; padding: 9px 13px; border-radius: 18px; font-size: 14px; line-height: 1.35; position: relative; word-break: break-word; }
.bubble.in { align-self: flex-start; background: var(--bubble-in); color: var(--text); border-bottom-left-radius: 6px; }
.bubble.out { align-self: flex-end; background: var(--bubble-out); color: #fff; border-bottom-right-radius: 6px; }
.bubble .t { display: block; font-size: 10px; opacity: .7; margin-top: 4px; text-align: right; }

/* app groups */
.app-row { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 12px; cursor: pointer; }
.app-row:hover { background: var(--card-2); }
.app-avatar { width: 40px; height: 40px; border-radius: 11px; display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 700; font-size: 16px; flex-shrink: 0; }
.app-body { flex: 1; min-width: 0; }
.app-top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.app-name { font-weight: 600; font-size: 14px; }
.app-time { color: var(--muted); font-size: 11px; flex-shrink: 0; }
.app-bottom { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 2px; }
.app-preview { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.app-count { background: var(--purple); color: #fff; font-size: 11px; font-weight: 600; border-radius: 10px; padding: 1px 7px; flex-shrink: 0; }

/* notification detail */
.notif-detail-head { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.notif-list-scroll { flex: 1; overflow: auto; padding: 12px 14px; display: flex; flex-direction: column; gap: 8px; }
.notif-item { padding: 12px; border-radius: 12px; border: 1px solid var(--border); background: var(--card); }
.notif-item .n-top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.notif-item .n-title { font-weight: 600; font-size: 14px; }
.notif-item .n-time { color: var(--muted); font-size: 11px; flex-shrink: 0; }
.notif-item .n-text { font-size: 13px; margin-top: 3px; color: var(--text); }
.notif-item .n-dev { font-size: 11px; color: var(--muted); margin-top: 4px; }

/* modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.5); display: flex; align-items: center; justify-content: center; z-index: 3000; }
.modal { background: var(--card); border-radius: 16px; padding: 20px; width: 320px; max-width: calc(100vw - 40px); box-shadow: var(--shadow); }
.modal h3 { margin: 0 0 12px; font-size: 16px; font-weight: 600; }
.modal-input { width: 100%; height: 40px; border: 1px solid var(--border); border-radius: 10px; background: var(--card-2); color: var(--text); padding: 0 12px; font-size: 14px; }
.modal-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

/* map markers */
.flag-icon { background: transparent; border: none; }
.flag { width: 22px; height: 22px; border-radius: 50%; color: #fff; font-size: 11px; font-weight: 700; display: flex; align-items: center; justify-content: center; box-shadow: 0 1px 4px rgba(0,0,0,.3); }
.flag.start { background: var(--green); }
.flag.end { background: var(--red); }
.current-pin { width: 18px; height: 18px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 0 0 3px rgba(0,0,0,.15); }

/* leaflet dark tweaks */
[data-theme="dark"] .leaflet-container { background: #1c1c1e; }
[data-theme="dark"] .leaflet-popup-content-wrapper, [data-theme="dark"] .leaflet-popup-tip { background: #2c2c2e; color: #f5f5f7; }

/* media gallery */
.media-card { padding: 0; }
.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; padding: 12px; }
.media-item { border: 1px solid var(--border); border-radius: 12px; overflow: hidden; background: var(--card-2); display: flex; flex-direction: column; }
.media-item img { width: 100%; height: 110px; object-fit: cover; display: block; }
.thumb-ph { width: 100%; height: 110px; display: flex; align-items: center; justify-content: center; color: var(--muted); }
.thumb-ph i { width: 26px; height: 26px; }
.media-meta { padding: 6px 8px; display: flex; flex-direction: column; gap: 2px; }
.media-name { font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.media-size { font-size: 11px; color: var(--muted); }
.media-action { margin: 0 8px 8px; height: 30px; border-radius: 8px; border: 1px solid var(--border); background: var(--card); color: var(--text); font-size: 12px; cursor: pointer; text-decoration: none; display: flex; align-items: center; justify-content: center; }
.media-action:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.media-hint { color: var(--muted); font-size: 12px; }
.media-thumb { cursor: pointer; }

/* lightbox */
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.55); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); z-index: 4000; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; padding: 24px; }
.lightbox img, .lightbox video { max-width: 90vw; max-height: 82vh; border-radius: 14px; box-shadow: 0 10px 40px rgba(0,0,0,.5); background: #000; }
.lightbox-close { position: absolute; top: 18px; right: 18px; width: 38px; height: 38px; border-radius: 50%; border: 0; background: rgba(255,255,255,.95); color: #1d1d1f; cursor: pointer; display: flex; align-items: center; justify-content: center; box-shadow: 0 2px 8px rgba(0,0,0,.3); }
.lightbox-close:hover { background: #fff; }
.lightbox-close i { width: 18px; height: 18px; }
.lightbox-caption { color: #fff; font-size: 13px; text-shadow: 0 1px 3px rgba(0,0,0,.5); }

/* device info */
.device-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.device-card { border: 1px solid var(--border); border-radius: var(--radius); background: var(--card); box-shadow: var(--shadow); padding: 14px 16px; }
.device-card-head { display: flex; align-items: center; gap: 8px; font-weight: 600; margin-bottom: 10px; }
.device-rows { display: flex; flex-direction: column; gap: 6px; }
.device-row { display: flex; justify-content: space-between; gap: 12px; font-size: 13px; }
.d-label { color: var(--muted); }
.d-value { text-align: right; font-variant-numeric: tabular-nums; word-break: break-word; }

/* contacts */
.contacts-card { padding: 0; }
.contacts-list { padding: 6px; display: flex; flex-direction: column; gap: 2px; max-height: 560px; overflow: auto; }
.contact-row { display: flex; align-items: center; gap: 12px; padding: 8px 10px; border-radius: 10px; }
.contact-row:hover { background: var(--card-2); }
.contact-avatar { width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 600; font-size: 16px; flex-shrink: 0; }
.contact-body { min-width: 0; }
.contact-name { font-weight: 600; font-size: 14px; }
.contact-sub { color: var(--muted); font-size: 12px; margin-top: 1px; overflow: hidden; text-overflow: ellipsis; }
.gap { color: var(--muted); font-size: 12px; text-align: center; padding: 20px; }

/* reply bar */
.reply-bar { display: flex; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--border); }
.reply-bar input { flex: 1; height: 38px; border: 1px solid var(--border); border-radius: 10px; background: var(--card-2); color: var(--text); padding: 0 12px; font-size: 14px; outline: none; }
.reply-bar input:focus { border-color: var(--accent); }

/* responsive */
@media (max-width: 1100px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .split { grid-template-columns: 1fr; height: auto; }
  .split-detail { height: 560px; }
}
@media (max-width: 560px) {
  .brand-sub { display: none; }
  .topbar-actions { width: 100%; }
  .token-wrap { flex: 1; }
  .token-wrap input { width: 100%; }
  #map, .map-wrap { height: 320px; }
  .main-tabs { width: 100%; }
  .main-tabs .tab { flex: 1; justify-content: center; }
  .stats { grid-template-columns: 1fr 1fr; }
  /* full-screen detail on mobile */
  .split-detail { position: fixed; inset: 0; z-index: 2000; height: 100%; border-radius: 0; display: none; }
  .split-detail.open { display: flex; }
}
