:root {
  --bg: #0a101a;
  --panel: #111b29;
  --panel-2: #182538;
  --panel-3: #21334b;
  --border: #25364c;
  --border-soft: #1d2b3d;
  --text: #edf4ff;
  --muted: #98a9bf;
  --primary: #67a5ff;
  --primary-600: #4387ed;
  --danger: #f05252;
  --warn: #f5a524;
  --ok: #30c76f;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 8px 24px rgba(0,0,0,.18), 0 24px 50px -32px rgba(0,0,0,.65);
  --ring: 0 0 0 3px rgba(103,165,255,.35);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background:
    radial-gradient(900px 500px at 80% -10%, rgba(70,128,201,.14), transparent 62%),
    radial-gradient(700px 400px at 0% 50%, rgba(45,101,134,.07), transparent 60%),
    var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.sm { font-size: 0.85rem; }
a { color: var(--primary); }
code { background: var(--panel-2); padding: 2px 6px; border-radius: 6px; font-size: 0.9em; }
h1, h2, h3 { line-height: 1.25; }

/* ---- Buttons ------------------------------------------------------------ */
button {
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  padding: 10px 15px;
  border-radius: var(--radius-sm);
  transition: transform .08s ease, filter .15s ease, background .15s ease, border-color .15s ease;
}
button:hover { filter: brightness(1.12); border-color: #33415a; }
button:active { transform: translateY(1px); }
button:focus-visible { outline: none; box-shadow: var(--ring); }
button.sm { padding: 7px 11px; font-size: 0.85rem; }
button.primary { background: var(--primary); border-color: var(--primary-600); color: #fff; }
button.primary:hover { background: var(--primary-600); }
button.ghost { background: transparent; }
button.danger { background: transparent; border-color: rgba(240,82,82,.5); color: #ff9a9a; }
button.danger:hover { background: rgba(240,82,82,.12); }

/* ---- Cards -------------------------------------------------------------- */
.card {
  background: linear-gradient(145deg, rgba(255,255,255,.035), transparent 45%), var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.card > h3 {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 16px;
  padding-bottom: 12px;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  border-bottom: 1px solid var(--border-soft);
}

/* Collapsible cards */
.card.collapsible > h3 { cursor: pointer; user-select: none; }
.card.collapsible > h3:hover { color: var(--text); }
.card > h3 .caret { margin-left: auto; font-size: 0.9rem; color: var(--muted); transition: transform .2s ease; }
.card.collapsed > h3 .caret { transform: rotate(-90deg); }
.card.collapsed > h3 { margin-bottom: 0; padding-bottom: 0; border-bottom: none; }
.card.collapsed > :not(h3) { display: none !important; }

/* ---- Auth --------------------------------------------------------------- */
.auth { display: grid; place-items: center; min-height: 100vh; padding: 20px; }
.auth-card { width: 100%; max-width: 380px; padding: 28px; }
.auth-card h1 { margin: 0 0 4px; font-size: 1.6rem; }
.auth input {
  width: 100%;
  margin: 8px 0;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
}
.auth input:focus-visible { outline: none; box-shadow: var(--ring); border-color: var(--primary); }
.auth .row { display: flex; gap: 10px; margin-top: 12px; }
.auth .row button { flex: 1; }
.err { background: rgba(240,82,82,.12); color: #ffb4b4; padding: 10px 12px; border-radius: var(--radius-sm); margin: 8px 0; border: 1px solid rgba(240,82,82,.3); }

/* ---- Topbar ------------------------------------------------------------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: 0 20px; height: 58px;
  border-bottom: 1px solid var(--border);
  background: rgba(13,17,24,.8);
  backdrop-filter: blur(10px);
}
.mobile-menu-button { display: none; }
.brand { font-weight: 800; font-size: 1.05rem; letter-spacing: 0.02em; }
.spacer { flex: 1; }

/* ---- App layout --------------------------------------------------------- */
.layout { display: flex; align-items: flex-start; min-height: calc(100vh - 58px); }
.sidebar {
  width: 272px; flex: none; align-self: stretch;
  border-right: 1px solid var(--border);
  padding: 18px;
  background: var(--panel);
}
.side-head { display: flex; align-items: center; justify-content: space-between; }
.side-head h2 { margin: 0; font-size: 0.8rem; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.content { flex: 1; min-width: 0; padding: 30px; max-width: 1440px; margin: 0 auto; width: 100%; }

.device-list { list-style: none; padding: 0; margin: 16px 0 0; display: flex; flex-direction: column; gap: 8px; }
.device-list li {
  padding: 12px 13px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  cursor: pointer; display: flex; align-items: center; gap: 10px;
  transition: background .15s, border-color .15s;
}
.device-list li:hover { background: var(--panel-2); }
.device-list li.active { border-color: var(--primary); background: rgba(79,140,255,.1); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.online { background: var(--ok); box-shadow: 0 0 0 3px rgba(48,199,111,.2); }
.dot.stolen { background: var(--danger); box-shadow: 0 0 0 3px rgba(240,82,82,.2); }

/* ---- Device view grid --------------------------------------------------- */
#deviceView { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; align-items: start; }
#deviceView > * { min-width: 0; margin-bottom: 0; }
.device-header { order: 1; grid-column: 1 / -1; }
#overviewCard { order: 2; grid-column: 1 / -1; }
#pairBox      { order: 0; grid-column: 1 / -1; }
#commandBox   { order: 3; grid-column: 1 / -1; }
#mapCard      { order: 4; }
#commandsCard { order: 5; }
#audioCard    { order: 6; grid-column: 1 / -1; }
#activityCard { order: 7; grid-column: 1 / -1; }
#photoAlbumCard { order: 8; grid-column: 1 / -1; }
#callLogsCard { order: 9; grid-column: 1 / -1; }
#notificationsCard { order: 10; grid-column: 1 / -1; }

.device-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.device-header h2 { margin: 0 0 10px; font-size: 1.65rem; letter-spacing: -.025em; }
.chips { display: flex; flex-wrap: wrap; gap: 7px; }
.chip {
  font-size: 0.75rem; font-weight: 600; padding: 4px 10px; border-radius: 999px;
  background: var(--panel-2); border: 1px solid var(--border); color: var(--muted);
}
.chip.ok { color: var(--ok); border-color: rgba(48,199,111,.4); background: rgba(48,199,111,.08); }
.chip.bad { color: #ff9a9a; border-color: rgba(240,82,82,.4); background: rgba(240,82,82,.08); }

.overview-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.overview-stat {
  display: grid; gap: 5px; padding: 18px 20px; min-width: 0;
  background: linear-gradient(145deg, rgba(103,165,255,.09), transparent 65%), var(--panel);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.overview-stat span { color: var(--muted); font-size: .72rem; font-weight: 700; letter-spacing: .07em; text-transform: uppercase; }
.overview-stat strong { font-size: 1.45rem; line-height: 1.2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.overview-stat small { color: var(--muted); }

.audio-recordings { display: grid; gap: 10px; max-height: 520px; overflow-y: auto; padding: 2px 5px 2px 2px; }
.audio-recording {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 14px 16px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: linear-gradient(100deg, rgba(103,165,255,.07), transparent 55%), var(--panel-2);
}
.audio-recording-info, .audio-player-wrap { display: flex; align-items: center; gap: 12px; min-width: 0; }
.audio-icon {
  display: grid; place-items: center; width: 42px; height: 42px; flex: none;
  color: var(--primary); background: rgba(103,165,255,.12); border-radius: 12px; font-size: 1.25rem;
}
.audio-recording-info > div { min-width: 0; }
.audio-recording-info strong { display: block; }
.audio-player { width: min(420px, 44vw); height: 38px; }
.audio-error { max-width: 240px; }
.audio-empty { margin: 0; padding: 8px 2px; }

/* ---- Pairing ------------------------------------------------------------ */
.pair { text-align: center; }
.pairing-code {
  font-size: 2rem; font-weight: 800; letter-spacing: 4px;
  background: var(--panel-2); padding: 16px; border-radius: var(--radius); margin: 12px 0;
  border: 1px dashed var(--border);
}

/* ---- Actions ------------------------------------------------------------ */
.cmd-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.cmd {
  text-align: center; padding: 14px 10px; background: var(--panel-2);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
}
.cmd:hover { background: var(--panel-3); transform: translateY(-1px); }
.cmd.warn { border-color: rgba(245,165,36,.4); color: #f8c877; }
.cmd.danger { border-color: rgba(240,82,82,.45); color: #ff9a9a; }
.test-row { margin-top: 14px; }
.toast { margin-top: 14px; padding: 11px 13px; border-radius: var(--radius-sm); background: var(--panel-2); border: 1px solid var(--border); }
.toast.ok { color: var(--ok); border-color: rgba(48,199,111,.4); }
.toast.bad { color: #ff9a9a; border-color: rgba(240,82,82,.4); }

/* ---- Map ---------------------------------------------------------------- */
#map { min-height: 340px; border-radius: var(--radius-sm); overflow: hidden; }
.google-map { display: block; width: 100%; height: 340px; border: 0; border-radius: var(--radius-sm); }
.map-controls { display: flex; align-items: center; flex-wrap: wrap; gap: 10px 14px; padding-top: 10px; }
.map-controls .map-external-link { margin: 0; }
#map.map-expanded .google-map { height: min(72vh, 760px); }
#map:fullscreen {
  display: flex; flex-direction: column; width: 100%; height: 100%; min-height: 0;
  padding: 14px; background: var(--bg);
}
#map:fullscreen .google-map { flex: 1; height: auto; min-height: 0; }
.map-fullscreen-status { flex-basis: 100%; }
.map-external-link { display: inline-block; margin-top: 10px; font-size: .85rem; }
.map-fallback { height: 340px; display: grid; place-content: center; text-align: center; gap: 6px; padding: 16px; background: var(--panel-2); border-radius: var(--radius-sm); }

/* ---- Command results ---------------------------------------------------- */
.commands { display: flex; flex-direction: column; gap: 8px; max-height: 360px; overflow-y: auto; padding-right: 4px; }
.cmd-row { display: grid; grid-template-columns: auto auto 1fr; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--panel-2); }
.cmd-type { font-weight: 600; }
.cmd-when { color: var(--muted); font-size: 0.8rem; text-align: right; }
.cmd-result { grid-column: 1 / -1; color: var(--muted); font-size: 0.85rem; word-break: break-word; }
.badge { font-size: 0.68rem; padding: 3px 9px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; font-weight: 700; }
.badge.ok { color: var(--ok); border-color: rgba(48,199,111,.4); background: rgba(48,199,111,.08); }
.badge.bad { color: #ff9a9a; border-color: rgba(240,82,82,.4); background: rgba(240,82,82,.08); }
.badge.warn { color: #f8c877; border-color: rgba(245,165,36,.4); background: rgba(245,165,36,.08); }

/* ---- Activity ----------------------------------------------------------- */
.events { display: flex; flex-direction: column; gap: 10px; max-height: 520px; overflow-y: auto; padding: 2px 5px 2px 2px; }
.event { display: flex; gap: 12px; align-items: flex-start; padding: 12px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--panel-2); }
.event .ic { font-size: 1.25rem; line-height: 1.4; }
.event .when { color: var(--muted); font-size: 0.8rem; margin-top: 2px; }
.event img { max-width: 180px; border-radius: 8px; display: block; margin-top: 8px; border: 1px solid var(--border); }

.photo-album {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 14px;
  max-height: 520px; overflow-y: auto; padding: 2px 4px 2px 2px;
}
.photo-item { margin: 0; min-width: 0; }
.photo-thumb {
  display: grid; place-items: center; width: 100%; aspect-ratio: 4 / 3; overflow: hidden;
  padding: 0; color: var(--muted); background: var(--panel-2); border-radius: var(--radius-sm);
}
.photo-thumb img { display: block; width: 100%; height: 100%; object-fit: cover; transition: transform .2s ease; }
.photo-thumb:hover img { transform: scale(1.035); }
.photo-loading { padding: 12px; font-size: .85rem; }
.photo-item figcaption { margin-top: 7px; color: var(--muted); font-size: .8rem; }
.photo-empty { margin: 0; padding: 8px 2px; }
.photo-viewer {
  position: fixed; inset: 0; max-width: min(94vw, 1200px); max-height: 92vh;
  padding: 48px 20px 18px; overflow: auto; color: var(--text);
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.photo-viewer::backdrop { background: rgba(2, 7, 14, .82); backdrop-filter: blur(5px); }
.photo-viewer img { display: block; max-width: 100%; max-height: 76vh; margin: 0 auto; object-fit: contain; }
.photo-viewer p { margin: 12px 0 0; text-align: center; color: var(--muted); }
.photo-viewer-close {
  position: absolute; top: 10px; right: 10px; width: 36px; height: 36px;
  padding: 0; font-size: 1.5rem; line-height: 1;
}

.empty { color: var(--muted); display: grid; place-items: center; height: 60vh; text-align: center; }

/* ---- Call logs (preserved from your additions, refined) ----------------- */
.call-logs-container {
  max-height: 360px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.call-logs-table { width: 100%; border-collapse: collapse; text-align: left; }
#callLogsCard .call-logs-table { min-width: 600px; }
#notificationsCard .call-logs-table { min-width: 760px; }
.call-logs-table th {
  background: var(--panel-2);
  padding: 11px 14px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
}
.call-logs-table td { padding: 12px 14px; border-bottom: 1px solid var(--border-soft); font-size: 0.9rem; }
.call-logs-table tbody tr:nth-child(even) { background: rgba(255,255,255,.015); }
.call-logs-table tbody tr:hover { background: var(--panel-2); }
.call-logs-table tr:last-child td { border-bottom: none; }

.collection-tools {
  display: flex; align-items: end; flex-wrap: wrap; gap: 12px; margin: 0 0 14px;
}
.collection-tools label { display: grid; gap: 5px; color: var(--muted); font-size: .78rem; font-weight: 600; }
.collection-search, .collection-select {
  min-width: 190px; padding: 9px 11px; color: var(--text); background: var(--panel-2);
  border: 1px solid var(--border); border-radius: var(--radius-sm); font: inherit;
}
.collection-search { width: min(340px, 65vw); }
.collection-search:focus-visible, .collection-select:focus-visible { outline: none; box-shadow: var(--ring); border-color: var(--primary); }
.pagination {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: 42px; margin-top: 10px; color: var(--muted); font-size: .82rem;
}
.pagination-controls { display: flex; gap: 8px; }
.pagination button:disabled { opacity: .45; cursor: default; }
.table-empty { text-align: center; padding: 20px !important; }
.notification-content { min-width: 200px; max-width: 480px; white-space: pre-wrap; overflow-wrap: anywhere; }
.event-detail { margin-top: 4px; overflow-wrap: anywhere; }
.event-detail-list { display: grid; gap: 3px; margin-top: 5px; }
.event > div { min-width: 0; overflow-wrap: anywhere; }

.text-ok { color: var(--ok); font-weight: 700; }
.text-primary { color: var(--primary); font-weight: 700; }
.text-danger { color: var(--danger); font-weight: 700; }
.badge-dir { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em; }
.log-number { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.95rem; }

/* ---- Responsive --------------------------------------------------------- */
@media (max-width: 900px) {
  #deviceView { grid-template-columns: 1fr; }
  #mapCard, #commandsCard { grid-column: 1 / -1; }
}
@media (max-width: 720px) {
  html, body { min-width: 320px; }
  .layout { display: block; min-height: calc(100vh - 58px); }
  .mobile-menu-button { display: inline-flex; align-items: center; justify-content: center; }
  .topbar { padding: 0 12px; }
  .brand { font-size: .95rem; }
  .topbar > button { padding: 8px 10px; }
  #whoami { max-width: 34vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .8rem; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 41; width: min(84vw, 340px);
    align-self: auto; overflow-y: auto; border: 0; border-right: 1px solid var(--border);
    box-shadow: 16px 0 48px rgba(0,0,0,.42);
    transform: translateX(-105%); visibility: hidden;
    transition: transform .22s ease, visibility .22s ease;
  }
  .sidebar.drawer-open { transform: translateX(0); visibility: visible; }
  .drawer-backdrop:not(.hidden) {
    position: fixed; inset: 0; z-index: 40; background: rgba(2,7,14,.66);
    backdrop-filter: blur(2px);
  }
  body.device-drawer-open { overflow: hidden; }
  .content { max-width: none; padding: 14px 12px calc(88px + env(safe-area-inset-bottom)); }
  .card { padding: 16px; border-radius: 12px; }
  .card > h3 { gap: 6px; margin-bottom: 12px; padding-bottom: 10px; font-size: .74rem; }
  .device-header { flex-direction: row; align-items: center; }
  .device-header h2 { font-size: 1.3rem; overflow-wrap: anywhere; }
  .device-header > button { flex: none; }
  .overview-grid { grid-template-columns: 1fr; gap: 8px; }
  .overview-stat { grid-template-columns: minmax(0, 1fr) auto; gap: 2px 8px; padding: 11px 13px; }
  .overview-stat span { font-size: .65rem; }
  .overview-stat small { grid-column: 1; font-size: .75rem; }
  .overview-stat strong { grid-column: 2; grid-row: 1 / 3; font-size: 1.2rem; }
  .cmd-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .cmd { min-height: 54px; padding: 10px 7px; }
  #map, .google-map, .map-fallback { min-height: 240px; height: 240px; }
  #map.map-expanded .google-map { height: min(65vh, 600px); }
  .audio-recording { align-items: flex-start; flex-direction: column; }
  .audio-player-wrap, .audio-player { width: 100%; }
  .audio-player-wrap { flex-wrap: wrap; }
  .photo-album { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .photo-viewer { padding: 48px 12px 14px; }
  .collection-tools { align-items: stretch; flex-direction: column; }
  .collection-tools label { width: 100%; }
  .collection-search, .collection-select { width: 100%; min-width: 0; }
  .pagination { align-items: flex-start; flex-direction: column; gap: 8px; }
  .pagination-controls { width: 100%; }
  .pagination-controls button { flex: 1; }
  .call-logs-container { max-height: 320px; }
  .call-logs-table th, .call-logs-table td { padding: 10px; }
  .event { gap: 9px; padding: 10px; }
  .notification-content { min-width: 0; }
  .mobile-card-nav:not(.hidden) {
    position: fixed; inset: auto 0 0; z-index: 30; display: flex; gap: 4px;
    min-height: calc(62px + env(safe-area-inset-bottom)); padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    overflow-x: auto; overscroll-behavior-x: contain; scrollbar-width: none;
    background: rgba(17,27,41,.96); border-top: 1px solid var(--border);
    backdrop-filter: blur(14px);
  }
  .mobile-card-nav::-webkit-scrollbar { display: none; }
  .mobile-nav-item {
    display: flex; flex: 0 0 64px; flex-direction: column; align-items: center; justify-content: center;
    gap: 3px; min-height: 48px; padding: 4px; border: 1px solid transparent;
    color: var(--muted); background: transparent; font-weight: 500;
  }
  .mobile-nav-item > span { font-size: 1.1rem; line-height: 1.1; }
  .mobile-nav-item small { font-size: .65rem; white-space: nowrap; }
  .mobile-nav-item.is-active { color: var(--primary); background: rgba(103,165,255,.1); border-color: rgba(103,165,255,.24); }
  #deviceView > .card, #deviceView > section, #deviceView > .overview-grid { scroll-margin-top: 72px; }
}

@media (max-width: 380px) {
  .content { padding-right: 10px; padding-left: 10px; }
  .topbar { gap: 8px; }
  #whoami { max-width: 27vw; }
  .device-header { align-items: flex-start; }
  .device-header h2 { font-size: 1.15rem; }
  .overview-stat { padding: 10px; }
  .mobile-nav-item { flex-basis: 58px; }
}
