/* NeuroFleet frontend — mobile-first, theme-aware.
   Light by default; dark via prefers-color-scheme. */

:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eef1f7;
  --text: #131a2a;
  --text-muted: #5b6577;
  --border: #e2e6ef;
  --primary: #2563eb;
  --primary-ink: #ffffff;
  --danger: #dc2626;
  --high: #dc2626;
  --high-bg: #fdecec;
  --med: #d97706;
  --med-bg: #fdf2e2;
  --low: #2563eb;
  --low-bg: #e9f0fe;
  --on: #16a34a;
  --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 1px 2px rgba(16, 24, 40, .06);
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --nav-h: 62px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --surface: #131c2e;
    --surface-2: #1b2740;
    --text: #e8edf6;
    --text-muted: #9aa6bd;
    --border: #253150;
    --primary: #3b82f6;
    --danger: #ef4444;
    --high: #f87171; --high-bg: #3a1c1c;
    --med: #fbbf24;  --med-bg: #3a2c12;
    --low: #60a5fa;  --low-bg: #172a45;
    --on: #4ade80;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }
/* Author display rules (flex/grid below) otherwise override the UA
   `[hidden]{display:none}`, leaving hidden screens visible. Make hidden win. */
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  line-height: 1.45;
}

/* ---------- buttons / fields ---------- */
.btn {
  appearance: none; border: 1px solid transparent; cursor: pointer;
  font-size: 15px; font-weight: 600; padding: 12px 16px; border-radius: 12px;
  transition: transform .05s ease, background .15s ease, opacity .15s ease;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  color: var(--text); background: var(--surface-2);
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .6; cursor: default; }
.btn--primary { background: var(--primary); color: var(--primary-ink); }
.btn--ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn--danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn--block { width: 100%; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field__label { font-size: 14px; font-weight: 600; color: var(--text-muted); }
.field__input, .search {
  width: 100%; padding: 13px 14px; font-size: 16px; /* 16px avoids iOS zoom */
  border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
}
.field__input:focus, .search:focus { outline: 2px solid var(--primary); border-color: transparent; }
.field__input--code { text-align: center; font-size: 28px; letter-spacing: 0.4em; font-variant-numeric: tabular-nums; padding-left: 0.4em; }
.field__hint { font-size: 12.5px; color: var(--text-muted); }

/* ---------- onboarding ---------- */
.link-screen {
  min-height: 100dvh; display: flex; flex-direction: column;
  justify-content: center; align-items: stretch;
  max-width: 460px; margin: 0 auto; padding: 24px 22px calc(24px + var(--safe-b));
  gap: 14px;
}
.brandmark { font-size: 52px; text-align: center; }
.link__title { text-align: center; margin: 0; font-size: 30px; letter-spacing: -.5px; }
.link__sub { text-align: center; color: var(--text-muted); margin: 0 0 10px; }
.link__form { display: flex; flex-direction: column; gap: 16px; }
.link__demo { font-size: 13px; color: var(--text-muted); display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 6px; }
.chip {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 6px 10px; border-radius: 999px; font-size: 12.5px; cursor: pointer;
}
.chip:active { transform: scale(.97); }

/* ---------- app shell ---------- */
/* Fill the viewport and scroll ONLY the content area, so the top bar and bottom
   nav are always visible (no reliance on position:fixed, which breaks if any
   ancestor ever establishes a containing block). */
.app-shell { position: relative; height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px calc(14px + env(safe-area-inset-top, 0px));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar__brand { font-weight: 700; font-size: 18px; display: flex; align-items: center; gap: 8px; }
.topbar__group {
  display: flex; align-items: center; gap: 7px; font-size: 13px; font-weight: 600;
  color: var(--text-muted); background: var(--surface-2); padding: 6px 11px; border-radius: 999px;
  max-width: 55%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--on); flex: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--on) 25%, transparent); }

.main {
  flex: 1; min-height: 0; width: 100%; max-width: 640px; margin: 0 auto;
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  /* bottom room so the last item can scroll clear of the translucent nav */
  padding: 16px 16px calc(var(--nav-h) + var(--safe-b) + 24px);
}
.viewhead { display: flex; align-items: baseline; justify-content: space-between; margin: 4px 2px 14px; }
.viewhead h2 { margin: 0; font-size: 22px; letter-spacing: -.3px; }
.muted { color: var(--text-muted); }
.small { font-size: 13px; }
.iconbtn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  width: 38px; height: 38px; border-radius: 10px; font-size: 18px; cursor: pointer;
}
.list { display: flex; flex-direction: column; gap: 12px; }

/* ---------- Active / History segmented toggle ---------- */
.segmented { display: flex; gap: 4px; background: var(--surface-2); border-radius: 10px; padding: 4px; margin-bottom: 14px; }
.seg { flex: 1; border: none; background: none; color: var(--text-muted); font-size: 14px; font-weight: 600; padding: 8px; border-radius: 7px; cursor: pointer; }
.seg--active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* ---------- clear / history extras ---------- */
.alert--clickable { cursor: pointer; transition: transform .05s ease, box-shadow .15s ease; }
.alert--clickable:active { transform: scale(.995); }
.alert__event { margin: -4px 0 8px; font-size: 13px; color: var(--text-muted); }
.alert__hint { margin-top: 10px; font-size: 12px; font-weight: 600; color: var(--primary); }
.alert--cleared { opacity: .97; }
.cleared { margin-top: 12px; padding-top: 10px; border-top: 1px dashed var(--border); font-size: 13px; }
.cleared__by { color: var(--on); font-weight: 600; }
.cleared__comment { margin-top: 6px; color: var(--text); background: var(--surface-2); padding: 8px 10px; border-radius: 8px; }
.sheet__alert { margin: 4px 0 14px; }

/* ---------- alert cards ---------- */
.alert {
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--border);
  border-radius: var(--radius); padding: 14px 15px; box-shadow: var(--shadow);
}
.alert--error { border-left-color: var(--high); }
.alert--warn { border-left-color: var(--med); }
.alert--info { border-left-color: var(--low); }
.alert--unread { box-shadow: 0 0 0 1px var(--primary), var(--shadow); }
.alert__top { display: flex; align-items: center; justify-content: space-between; }
.alert__time { font-size: 12.5px; color: var(--text-muted); }
.alert__name { margin: 8px 0 10px; font-size: 17px; }
.alert__meta { margin: 0; display: grid; gap: 6px; }
.alert__meta div { display: grid; grid-template-columns: 78px 1fr; gap: 8px; font-size: 14px; }
.alert__meta dt { color: var(--text-muted); }
.alert__meta dd { margin: 0; }

.pill { font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .4px; padding: 4px 9px; border-radius: 999px; }
.pill--error { color: var(--high); background: var(--high-bg); }
.pill--warn { color: var(--med); background: var(--med-bg); }
.pill--info { color: var(--low); background: var(--low-bg); }

/* ---------- asset cards ---------- */
.asset { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 15px; box-shadow: var(--shadow); }
.asset__head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.asset__name { margin: 0 0 3px; font-size: 16px; }
.asset__sub { margin: 0; font-size: 13px; color: var(--text-muted); }
.status { font-size: 12px; font-weight: 700; padding: 4px 10px; border-radius: 999px; white-space: nowrap; flex: none; }
.status--on { color: var(--on); background: color-mix(in srgb, var(--on) 14%, transparent); }
.status--off { color: var(--text-muted); background: var(--surface-2); }
.asset__locate { margin-top: 2px; }

.search { margin-bottom: 14px; }

/* ---------- settings ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 14px; display: flex; flex-direction: column; gap: 12px; }
.card__title { margin: 0; font-size: 16px; }
.row { display: flex; align-items: center; justify-content: space-between; }
.row__label { font-size: 13px; color: var(--text-muted); }
.row__value { font-size: 16px; font-weight: 600; }
.help { margin: 0; padding-left: 18px; color: var(--text-muted); font-size: 14px; display: grid; gap: 6px; }
.help b { color: var(--text); }
code { background: var(--surface-2); padding: 1px 6px; border-radius: 6px; font-size: 13px; }
.footnote { text-align: center; color: var(--text-muted); font-size: 12.5px; margin: 8px 0; }

/* ---------- empty / loading ---------- */
.empty { text-align: center; padding: 40px 20px; color: var(--text-muted); }
.empty__icon { font-size: 40px; margin-bottom: 8px; }
.loading { text-align: center; padding: 30px; color: var(--text-muted); }

/* ---------- bottom nav ---------- */
.bottomnav {
  position: absolute; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex; border-top: 1px solid var(--border);
  /* frosted bar: content scrolls faintly visible underneath */
  background: color-mix(in srgb, var(--surface) 80%, transparent);
  -webkit-backdrop-filter: saturate(1.1) blur(14px); backdrop-filter: saturate(1.1) blur(14px);
  padding-bottom: var(--safe-b); height: calc(var(--nav-h) + var(--safe-b));
}
.navbtn {
  flex: 1; border: none; background: none; cursor: pointer; color: var(--text-muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font-size: 11px; font-weight: 600; padding-top: 8px;
}
.navbtn--active { color: var(--primary); }
.navbtn__icon { position: relative; font-size: 21px; line-height: 1; }
.badge {
  position: absolute; top: -6px; right: -12px; min-width: 17px; height: 17px; padding: 0 4px;
  background: var(--danger); color: #fff; border-radius: 999px; font-size: 10.5px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}

/* ---------- bottom sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 40; background: rgba(6, 10, 20, .5); display: flex; align-items: flex-end; }
.sheet__panel {
  width: 100%; max-width: 640px; margin: 0 auto; background: var(--surface);
  border-radius: 20px 20px 0 0; padding: 22px 20px calc(24px + var(--safe-b));
  transform: translateY(100%); transition: transform .25s ease; position: relative;
}
.sheet--open .sheet__panel { transform: translateY(0); }
.sheet__close { position: absolute; top: 14px; right: 14px; border: none; background: var(--surface-2); color: var(--text); width: 32px; height: 32px; border-radius: 50%; font-size: 15px; cursor: pointer; }
.sheet__title { margin: 0 0 6px; font-size: 19px; }
.sheet__desc { margin: 0 0 14px; color: var(--text-muted); font-size: 14px; }
.linkbox { display: block; word-break: break-all; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 12px; font-size: 13.5px; color: var(--primary); text-decoration: none; }
.sheet__actions { display: flex; gap: 10px; margin-top: 16px; }
.sheet__actions .btn { flex: 1; }

/* ---------- toasts ---------- */
.toasts { position: fixed; top: calc(12px + env(safe-area-inset-top, 0px)); left: 0; right: 0; z-index: 60; display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; padding: 0 12px; }
.toast {
  max-width: 480px; width: 100%; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-left: 4px solid var(--primary);
  border-radius: 12px; padding: 12px 14px; font-size: 14px; font-weight: 500;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .18);
  opacity: 0; transform: translateY(-12px); transition: opacity .25s ease, transform .25s ease;
}
.toast--show { opacity: 1; transform: translateY(0); }
.toast--success { border-left-color: var(--on); }
.toast--error { border-left-color: var(--high); }
.toast--warn { border-left-color: var(--med); }
.toast--info { border-left-color: var(--low); }

/* ---------- larger screens: nav becomes a sidebar rail ---------- */
@media (min-width: 860px) {
  :root { --nav-h: 0px; }
  .app-shell { display: grid; grid-template-columns: 220px 1fr; grid-template-rows: auto 1fr; height: 100dvh; overflow: hidden; }
  /* Explicit placement — auto-placement otherwise strands the nav in an implicit
     third row at the bottom instead of the left column. */
  .topbar { grid-column: 1 / -1; grid-row: 1; }
  .bottomnav {
    grid-column: 1; grid-row: 2;
    position: static; flex-direction: column; height: auto; border-top: none; overflow-y: auto;
    background: var(--surface); -webkit-backdrop-filter: none; backdrop-filter: none;
    border-right: 1px solid var(--border); padding: 16px 12px; gap: 4px; align-items: stretch; justify-content: flex-start;
  }
  .navbtn { flex: none; flex-direction: row; justify-content: flex-start; gap: 12px; font-size: 15px; padding: 12px 14px; border-radius: 10px; }
  .navbtn--active { background: var(--surface-2); }
  .navbtn__label { font-weight: 600; }
  .main { grid-column: 2; grid-row: 2; padding-bottom: 40px; }
  .sheet { align-items: center; }
  .sheet__panel { border-radius: 20px; max-width: 480px; transform: translateY(20px); }
}
