/* CamTrack — mobile-first styles */
:root {
  --bg: #f1f4f8; --card: #ffffff; --ink: #0f172a; --muted: #64748b; --line: #e2e8f0;
  --brand: #0369a1; --brand-ink: #ffffff; --brand-soft: #e0f2fe;
  --good: #15803d; --good-bg: #dcfce7; --warn: #a16207; --warn-bg: #fef3c7; --bad: #b91c1c; --bad-bg: #fee2e2;
  --top: #0f172a; --top-ink: #f8fafc;
  --radius: 14px; --tap: 46px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220; --card: #131c2e; --ink: #e5e9f0; --muted: #94a3b8; --line: #23304a;
    --brand: #38bdf8; --brand-ink: #04121f; --brand-soft: #0c2a40;
    --good: #4ade80; --good-bg: #0f2e1d; --warn: #fbbf24; --warn-bg: #33270a; --bad: #f87171; --bad-bg: #3a1414;
    --top: #060b16;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
body.authed { padding-bottom: calc(70px + var(--safe-b)); }
a { color: var(--brand); text-decoration: none; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .95em; }
code.sel { user-select: all; background: var(--brand-soft); padding: 1px 6px; border-radius: 6px; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20; display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px;
  min-height: 56px; padding: 8px 14px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--top); color: var(--top-ink);
}
.topbar h1 { margin: 0; font-size: 1.08rem; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .brand { font-weight: 800; color: #38bdf8; font-size: .9rem; letter-spacing: .02em; }
.topbar .back { color: var(--top-ink); font-size: 2rem; line-height: 1; padding: 0 8px 4px 0; }
.topbar .btn { min-height: 36px; }

.wrap { max-width: 860px; margin: 0 auto; padding: 14px 14px 24px; }

/* Bottom tab bar */
.tabbar {
  position: fixed; inset: auto 0 0 0; z-index: 20; display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--card); border-top: 1px solid var(--line); padding-bottom: var(--safe-b);
}
.tabbar a { display: flex; flex-direction: column; align-items: center; gap: 1px; padding: 8px 0 9px; color: var(--muted); font-size: .74rem; font-weight: 600; }
.tabbar a span { font-size: 1.35rem; line-height: 1.1; }
.tabbar a.on { color: var(--brand); }

/* Cards & lists */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin: 0 0 14px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 8px; }
.card h2 { font-size: 1.02rem; margin: 0; }
h3.sub { font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 14px 0 8px; }
.count { display: inline-block; min-width: 22px; padding: 0 7px; border-radius: 99px; background: var(--brand-soft); color: var(--brand); font-size: .78rem; text-align: center; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { border-top: 1px solid var(--line); }
.list li:first-child { border-top: 0; }
.list li > a, .list.log li { display: flex; align-items: center; gap: 10px; padding: 11px 2px; color: inherit; min-height: var(--tap); }
.list li > a::after { content: "›"; color: var(--muted); font-size: 1.3rem; margin-left: auto; padding-left: 4px; }
.li-main { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.li-main strong { font-weight: 600; }
.li-main .muted { font-size: .86rem; overflow: hidden; text-overflow: ellipsis; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.crumbs { font-size: .86rem; color: var(--muted); margin: -2px 2px 10px; }
.ch { display: inline-grid; place-items: center; min-width: 30px; height: 26px; margin-right: 8px; border-radius: 8px; background: var(--brand-soft); color: var(--brand); font-size: .82rem; font-weight: 700; }
.ch.none { background: var(--line); color: var(--muted); }
.pill { display: inline-block; margin: 1px 0; font-size: .75rem; font-weight: 600; color: var(--muted); }

/* Badges */
.badge { display: inline-block; padding: 2px 9px; border-radius: 99px; font-size: .76rem; font-weight: 650; white-space: nowrap; }
.badge.good { background: var(--good-bg); color: var(--good); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge.muted { background: var(--line); color: var(--muted); }
.badges { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }

/* Key / value */
.kv { display: grid; grid-template-columns: minmax(96px, 34%) 1fr; gap: 7px 12px; margin: 0; }
.kv dt { color: var(--muted); font-size: .88rem; }
.kv dd { margin: 0; overflow-wrap: anywhere; }

/* Stats */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 14px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 6px; text-align: center; }
.stat b { display: block; font-size: 1.35rem; }
.stat span { font-size: .74rem; color: var(--muted); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: var(--tap); padding: 0 16px;
  border: 1px solid var(--brand); border-radius: 11px; background: var(--brand); color: var(--brand-ink);
  font: inherit; font-weight: 650; cursor: pointer; text-align: center; -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: translateY(1px); }
.btn.ghost { background: transparent; color: var(--brand); }
.btn.danger { background: transparent; color: var(--bad); border-color: var(--bad); }
.btn.small { min-height: 34px; padding: 0 12px; font-size: .86rem; border-radius: 9px; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: .55; }
.row-btns, .quick { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.quick .btn { flex: 1 1 auto; }
form.inline { display: inline; }
.danger-zone { margin: 22px 0; text-align: center; }

/* Forms */
.form { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 640px) { .form { grid-template-columns: 1fr 1fr; } .field.wide, .form .wide, .form-actions, .form h3 { grid-column: 1 / -1; } }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > label { font-size: .86rem; font-weight: 600; color: var(--muted); }
input, select, textarea {
  width: 100%; min-height: var(--tap); padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--ink); font: inherit; font-size: 16px;
}
textarea { min-height: 84px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: 0; border-color: transparent; }
input[disabled] { opacity: .65; }
label.check { display: flex; align-items: center; gap: 10px; font-weight: 500; color: var(--ink); font-size: .95rem; min-height: var(--tap); }
label.check input { width: 22px; height: 22px; min-height: 0; flex: none; }
.scan-wrap { display: flex; gap: 6px; }
.scan-wrap input { flex: 1; min-width: 0; }
.scan-btn { flex: none; padding: 0 12px; }
.form-actions { display: grid; gap: 8px; margin-top: 4px; }
.warranty-quick { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.warranty-quick button { min-height: 32px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--card); color: var(--brand); font: inherit; font-size: .82rem; font-weight: 600; }

.searchbar { display: flex; gap: 8px; margin-bottom: 14px; }
.searchbar input { flex: 1; min-width: 0; }

/* Flash */
.flash { padding: 11px 14px; border-radius: 11px; margin-bottom: 12px; font-weight: 500; }
.flash.ok { background: var(--good-bg); color: var(--good); }
.flash.warn, .flash.info { background: var(--warn-bg); color: var(--warn); }
.flash.error { background: var(--bad-bg); color: var(--bad); }

/* Segmented control */
.seg { display: flex; gap: 0; border: 1px solid var(--line); border-radius: 11px; overflow: hidden; margin-bottom: 10px; }
.seg label, .seg a { flex: 1; position: relative; }
.seg input { position: absolute; inset: 0; width: 1px; height: 1px; min-height: 0; padding: 0; opacity: 0; pointer-events: none; }
.seg span, .seg a { display: block; text-align: center; padding: 9px 4px; font-size: .86rem; font-weight: 600; color: var(--muted); }
.seg input:checked + span, .seg a.on { background: var(--brand); color: var(--brand-ink); }
.seg.links { background: var(--card); margin-bottom: 14px; }

/* Photos */
.uploader { border: 1px dashed var(--line); border-radius: 12px; padding: 12px; margin-bottom: 12px; }
.uploader .cap { margin-bottom: 2px; }
.uploader .row-btns .btn { flex: 1; }
.upl-status:empty { display: none; }
.upl-status { margin-top: 10px; font-size: .88rem; }
.upl-status div { padding: 3px 0; }
.upl-status .err { color: var(--bad); }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
.gallery figure { margin: 0; background: var(--bg); border-radius: 10px; overflow: hidden; border: 1px solid var(--line); }
.gallery img { display: block; width: 100%; aspect-ratio: 4 / 3; object-fit: cover; background: var(--line); }
.gallery figcaption { position: relative; padding: 6px 8px 8px; font-size: .82rem; min-height: 40px; }
.gallery figcaption small { display: block; color: var(--muted); font-size: .72rem; padding-right: 36px; }
.gallery figcaption form { position: absolute; right: 6px; bottom: 6px; }
.gallery figcaption .btn.small { min-height: 28px; padding: 0 8px; }

/* Credentials */
.cred { border-top: 1px solid var(--line); padding: 10px 0; }
.cred:first-of-type { border-top: 0; }
.cred-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 4px; }
.cred-vals > div:not(.row-btns) { display: flex; gap: 8px; align-items: baseline; padding: 2px 0; }
.cred-vals .muted { width: 40px; font-size: .82rem; }
.cred-vals code { user-select: all; overflow-wrap: anywhere; }

/* NVR channel map */
.chmap { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 6px; }
.chcell { display: flex; flex-direction: column; padding: 7px 8px; border-radius: 10px; border: 1px solid var(--line); color: var(--ink); min-height: 54px; }
.chcell b { font-size: .8rem; color: var(--brand); }
.chcell span { font-size: .8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chcell.free { border-style: dashed; color: var(--muted); }
.chcell.free b { color: var(--muted); }
.chcell.down { background: var(--warn-bg); }
.chcell.clash { background: var(--bad-bg); border-color: var(--bad); }

.pager { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 10px; }

/* Login */
.login-box { max-width: 380px; margin: 8vh auto 0; text-align: center; }
.login-box .stack { display: grid; gap: 12px; text-align: left; }
.login-logo { width: 64px; height: 64px; }

/* Modals: scanner + lightbox */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(2, 6, 23, .82); display: flex; align-items: center; justify-content: center; padding: 14px; }
.modal[hidden] { display: none; }
.modal-box { width: 100%; max-width: 480px; background: var(--card); border-radius: var(--radius); padding: 14px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
#scan-region { width: 100%; min-height: 220px; border-radius: 10px; overflow: hidden; background: #000; }
#scan-region video { width: 100% !important; display: block; }
.scan-msg { margin: 10px 0 0; font-size: .9rem; }
.scan-result { margin-top: 10px; }
.scan-result code { display: block; font-size: 1.15rem; padding: 10px; background: var(--brand-soft); border-radius: 8px; overflow-wrap: anywhere; }
#lightbox { flex-direction: column; gap: 12px; }
#lightbox img { max-width: 100%; max-height: 82vh; border-radius: 8px; }

@media (min-width: 640px) { .stats { gap: 12px; } .stat b { font-size: 1.6rem; } }
