/* Guardian Locate dashboard — utilitarian, legible, theme-aware.
   Static CSS, no framework. Light + dark via prefers-color-scheme. */

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #dfe3e8;
  --border-strong: #c6ccd4;
  --text: #1a2027;
  --text-muted: #5c6672;
  --text-faint: #8a94a0;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --focus: #2563eb;

  --fresh: #16a34a;
  --fresh-bg: #e7f6ec;
  --fresh-border: #b6e3c4;
  --stale: #b7791f;
  --stale-bg: #fdf1df;
  --stale-border: #f2d09a;
  --verystale: #dc2626;
  --verystale-bg: #fbe9e9;
  --verystale-border: #f2b8b8;
  --offline: #6b7280;
  --offline-bg: #eef0f2;
  --offline-border: #d7dce1;

  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --radius: 10px;
  --radius-sm: 7px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --surface: #171d25;
    --surface-2: #1d242e;
    --border: #2a323d;
    --border-strong: #3a4552;
    --text: #e7ecf1;
    --text-muted: #9aa6b2;
    --text-faint: #6d7885;
    --accent: #4f8cff;
    --accent-ink: #0b1220;
    --focus: #6ea8ff;

    --fresh: #34d17f;
    --fresh-bg: #10241a;
    --fresh-border: #1f4a35;
    --stale: #f4b048;
    --stale-bg: #2a2011;
    --stale-border: #574220;
    --verystale: #f87171;
    --verystale-bg: #2a1414;
    --verystale-border: #5a2626;
    --offline: #8b95a1;
    --offline-bg: #20262e;
    --offline-border: #333d48;

    --shadow: 0 1px 2px rgba(0, 0, 0, 0.35), 0 2px 6px rgba(0, 0, 0, 0.3);
  }
}

* { box-sizing: border-box; }

/* The `hidden` attribute must always win, even over class rules that set an
 * explicit `display` (e.g. .btn/.gate/.map-overlay). An author-origin `[hidden]`
 * only ties the UA rule on specificity and then loses on source order, so make
 * it authoritative here — otherwise toggling `.hidden` silently does nothing. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

h1, h2, h3 { margin: 0; font-weight: 650; line-height: 1.25; }
h1 { font-size: 1.05rem; }
h2 { font-size: 1rem; }
h3 { font-size: 0.9rem; }
p { margin: 0; }

code, .mono { font-family: var(--mono); }
code {
  font-size: 0.9em; background: var(--surface-2);
  padding: 1px 5px; border-radius: 4px; border: 1px solid var(--border);
}
.muted { color: var(--text-muted); }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

a { color: var(--accent); }

.skip-link {
  position: absolute; left: -999px; top: 0; z-index: 1000;
  background: var(--surface); color: var(--text); padding: 8px 12px; border-radius: 0 0 var(--radius-sm) 0;
}
.skip-link:focus { left: 0; }

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ---------- Header ---------- */
.app-header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 500;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; margin-right: auto; }
.brand-mark {
  font-size: 1.5rem; line-height: 1; color: var(--accent);
  display: inline-flex; width: 34px; height: 34px; align-items: center; justify-content: center;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 9px;
}
.brand-text .tagline { font-size: 0.72rem; color: var(--text-muted); margin-top: 1px; }

.header-status { display: flex; align-items: center; gap: 7px; font-size: 0.8rem; }
.conn-dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; flex: none; }
.conn-ok { background: var(--fresh); box-shadow: 0 0 0 3px var(--fresh-bg); }
.conn-warn { background: var(--stale); box-shadow: 0 0 0 3px var(--stale-bg); }
.conn-err { background: var(--verystale); box-shadow: 0 0 0 3px var(--verystale-bg); }
.conn-unknown { background: var(--offline); box-shadow: 0 0 0 3px var(--offline-bg); }
.conn-label { color: var(--text-muted); }

.header-actions { display: flex; gap: 8px; }

/* ---------- Buttons ---------- */
.btn {
  font: inherit; font-weight: 550; cursor: pointer;
  border-radius: var(--radius-sm); padding: 7px 12px;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(0.5px); }
.btn-sm { padding: 4px 9px; font-size: 0.8rem; }
.btn-ghost { border-color: var(--border); background: transparent; }
.btn-ghost:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-primary:hover { filter: brightness(1.06); background: var(--accent); }
.btn-danger-ghost { border-color: var(--stale-border); color: var(--verystale); background: transparent; }
.btn-danger-ghost:hover { background: var(--verystale-bg); }
.btn-icon { font-size: 1rem; line-height: 1; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

/* ---------- Settings panel ---------- */
.settings-panel {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 16px;
}
.settings-panel form { max-width: 720px; display: grid; gap: 14px; }
.field { display: grid; gap: 5px; }
.field label { font-weight: 600; font-size: 0.82rem; }
.field-row { display: flex; gap: 8px; }
.field-help { font-size: 0.75rem; color: var(--text-muted); }
input[type="text"], input[type="password"], input[type="url"], .select {
  font: inherit; width: 100%;
  padding: 8px 10px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text);
}
input::placeholder { color: var(--text-faint); }
.select { cursor: pointer; }
.settings-buttons { display: flex; gap: 10px; flex-wrap: wrap; }
.storage-warning {
  margin-top: 12px; font-size: 0.78rem; color: var(--stale);
  background: var(--stale-bg); border: 1px solid var(--stale-border);
  border-radius: var(--radius-sm); padding: 8px 10px; max-width: 720px;
}

/* ---------- Global banner ---------- */
.banner {
  margin: 12px 16px 0;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  border: 1px solid var(--verystale-border);
  background: var(--verystale-bg);
  color: var(--verystale);
}
.banner.banner-warn { border-color: var(--stale-border); background: var(--stale-bg); color: var(--stale); }

/* ---------- Layout ---------- */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 16px;
  padding: 16px;
  align-items: start;
  width: 100%;
  max-width: 1400px;
  margin: 0 auto;
}
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; }
}

/* ---------- Gate ---------- */
.gate { grid-column: 1 / -1; display: flex; justify-content: center; padding: 24px 8px; }
.gate-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 28px; max-width: 460px; text-align: center;
}
.gate-mark { font-size: 2rem; }
.gate-card h2 { margin: 12px 0 8px; font-size: 1.15rem; }
.gate-card p { color: var(--text-muted); font-size: 0.86rem; }
.gate-card form { display: grid; gap: 10px; margin-top: 18px; }
.gate-error {
  margin-top: 12px; color: var(--verystale); font-size: 0.82rem;
  background: var(--verystale-bg); border: 1px solid var(--verystale-border);
  border-radius: var(--radius-sm); padding: 8px 10px;
}

/* ---------- Sidebar / device list ---------- */
.sidebar {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px; position: sticky; top: 74px;
}
@media (max-width: 820px) { .sidebar { position: static; } }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.device-list { display: grid; gap: 8px; max-height: calc(100vh - 180px); overflow: auto; }
@media (max-width: 820px) { .device-list { max-height: 360px; } }
.device-card {
  text-align: left; width: 100%; cursor: pointer;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 10px 12px; display: grid; gap: 4px; color: inherit; font: inherit;
}
.device-card:hover { border-color: var(--border-strong); }
.device-card.selected { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.device-card-top { display: flex; align-items: center; gap: 8px; }
.device-name { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device-summary { font-size: 0.78rem; color: var(--text-muted); }
.status-dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.status-online { background: var(--fresh); }
.status-offline { background: var(--offline); }

.updated-note { margin-top: 12px; font-size: 0.74rem; color: var(--text-faint); display: grid; gap: 2px; }
.empty-note { font-size: 0.82rem; color: var(--text-muted); padding: 10px 2px; }

/* ---------- Pills / badges ---------- */
.pill {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 0.72rem; font-weight: 600; padding: 2px 8px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text-muted);
  white-space: nowrap;
}
.pill-muted { color: var(--text-muted); }
.pill-online { color: var(--fresh); background: var(--fresh-bg); border-color: var(--fresh-border); }
.pill-offline { color: var(--offline); background: var(--offline-bg); border-color: var(--offline-border); }

/* Freshness tags (device-list summary + history rows) */
.tag { display: inline-block; font-size: 0.72rem; font-weight: 650; padding: 1px 8px; border-radius: 999px; }
.tag-fresh { color: var(--fresh); background: var(--fresh-bg); }
.tag-stale { color: var(--stale); background: var(--stale-bg); }
.tag-verystale { color: var(--verystale); background: var(--verystale-bg); }
.tag-unavailable { color: var(--offline); background: var(--offline-bg); }

/* ---------- Detail ---------- */
.detail {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; min-width: 0;
}
.detail-empty { text-align: center; color: var(--text-muted); padding: 48px 16px; }
.detail-empty-mark { font-size: 1.8rem; display: block; margin-bottom: 8px; }
.detail-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.detail-head h2 { font-size: 1.15rem; }
.detail-sub { display: flex; align-items: center; gap: 10px; margin-top: 5px; font-size: 0.78rem; flex-wrap: wrap; }
.detail-sub .mono { word-break: break-all; }

/* Freshness banner — the anchor of the staleness UX */
.fix-banner {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 14px; border-radius: var(--radius-sm); margin-bottom: 10px;
  font-size: 0.9rem; font-weight: 550;
  border: 1px solid var(--border); background: var(--surface-2);
}
.fix-banner .fix-icon { font-size: 1.15rem; line-height: 1; flex: none; }
.fix-banner.is-fresh { color: var(--fresh); background: var(--fresh-bg); border-color: var(--fresh-border); }
.fix-banner.is-stale { color: var(--stale); background: var(--stale-bg); border-color: var(--stale-border); }
.fix-banner.is-verystale { color: var(--verystale); background: var(--verystale-bg); border-color: var(--verystale-border); }
/* "Unavailable" is an honest no-data state, not an error: keep it neutral, not red. */
.fix-banner.is-unavailable { color: var(--offline); background: var(--offline-bg); border-color: var(--offline-border); }
.fix-sub { font-weight: 450; font-size: 0.8rem; opacity: 0.9; }

.services-banner {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 12px; border-radius: var(--radius-sm); margin-bottom: 10px;
  font-size: 0.82rem; font-weight: 500;
  color: var(--stale); background: var(--stale-bg); border: 1px solid var(--stale-border);
}

/* Map */
.map-wrap { position: relative; margin-bottom: 16px; }
.map {
  height: 360px; width: 100%; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface-2); z-index: 1;
}
@media (max-width: 520px) { .map { height: 280px; } }
.map-overlay {
  position: absolute; inset: 0; z-index: 400;
  display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--surface) 84%, transparent);
  border-radius: var(--radius-sm); text-align: center; padding: 16px;
}
.map-overlay-inner { max-width: 340px; }
.map-overlay-mark { font-size: 1.6rem; }
.map-overlay-text, .map-overlay p { color: var(--text-muted); font-size: 0.9rem; margin-top: 6px; }

/* Leaflet marker glyphs (CSS only — no external image assets). */
.gl-pin {
  width: 20px; height: 20px; border-radius: 50%;
  border: 3px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,0.35), 0 1px 4px rgba(0,0,0,0.4);
}
.gl-pin-fresh { background: var(--fresh); }
.gl-pin-stale { background: var(--stale); }
.gl-pin-verystale { background: var(--verystale); }
.gl-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--accent); border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,0.3);
}

/* Details grid */
.section-title { margin: 6px 0 10px; color: var(--text); }
.details-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1px; margin: 0 0 20px; background: var(--border);
  border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden;
}
.detail-cell { background: var(--surface); padding: 10px 12px; }
.detail-cell dt { font-size: 0.72rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.03em; }
.detail-cell dd { margin: 3px 0 0; font-size: 0.92rem; font-weight: 550; }
.detail-cell dd .sub { font-size: 0.76rem; font-weight: 400; color: var(--text-muted); }
.detail-cell dd.na { color: var(--text-faint); font-weight: 400; }
.detail-cell dd.warn { color: var(--stale); }
.detail-cell dd.danger { color: var(--verystale); }

/* History */
.history-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 4px; }
.history-controls .select { width: auto; padding: 6px 8px; font-size: 0.82rem; }
.history-summary { margin: 0 0 8px; font-size: 0.76rem; }
.history-table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); }
.history-table { width: 100%; border-collapse: collapse; font-size: 0.8rem; min-width: 620px; }
.history-table th, .history-table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.history-table thead th { background: var(--surface-2); color: var(--text-muted); font-weight: 600; position: sticky; top: 0; }
.history-table tbody tr:last-child td { border-bottom: none; }
.history-table tbody tr:hover td { background: var(--surface-2); }
.history-table td.mono { font-family: var(--mono); }
.history-table tr.row-lastknown td { background: color-mix(in srgb, var(--stale-bg) 55%, transparent); }
.history-table tr.row-unavailable td { color: var(--text-faint); font-style: italic; }

/* Footer */
.app-footer { padding: 14px 16px; text-align: center; color: var(--text-faint); font-size: 0.75rem; border-top: 1px solid var(--border); background: var(--surface); }

/* Motion */
.spin { display: inline-block; animation: spin 0.9s linear infinite; }
@keyframes spin { 100% { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .spin { animation: none; }
  .btn:active { transform: none; }
}

/* Leaflet tweaks: keep container + popups legible in both themes */
.leaflet-container { background: var(--surface-2); font: inherit; }
.leaflet-popup-content { font-size: 0.82rem; }
