:root {
  --bg: #0b1220;
  --bg-2: #111c30;
  --card: #16233b;
  --card-2: #1c2c49;
  --line: #26375a;
  --text: #eaf0fb;
  --muted: #9fb0cc;
  --brand: #3b82f6;
  --brand-2: #2563eb;
  --ok: #22c55e;
  --ok-bg: #0f3320;
  --warn: #f59e0b;
  --danger: #ef4444;
  --danger-bg: #3a1618;
  --grey: #64748b;
  --radius: 14px;
  --shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
  --body-grad: linear-gradient(180deg, #0b1220, #070d18 80%);
  --topbar-bg: rgba(11, 18, 32, 0.92);
  --tabbar-bg: rgba(11, 18, 32, 0.96);
  --toast-bg: #0f1a2e;
  --overlay: rgba(2, 6, 14, 0.7);
  --knob: #fbbf24;
}

/* ---------- Light theme ---------- */
:root[data-theme="light"] {
  --bg: #eef2f8;
  --bg-2: #ffffff;
  --card: #ffffff;
  --card-2: #f4f7fc;
  --line: #dbe4f0;
  --text: #0f1b2d;
  --muted: #5a6b83;
  --brand: #2563eb;
  --brand-2: #1d4ed8;
  --ok: #16a34a;
  --ok-bg: #e6f7ee;
  --warn: #d97706;
  --danger: #dc2626;
  --danger-bg: #fdeaea;
  --grey: #94a3b8;
  --shadow: 0 6px 20px rgba(20, 40, 80, 0.10);
  --body-grad: linear-gradient(180deg, #f7f9fd, #eef2f8 80%);
  --topbar-bg: rgba(255, 255, 255, 0.9);
  --tabbar-bg: rgba(255, 255, 255, 0.95);
  --toast-bg: #1f2937;
  --overlay: rgba(15, 23, 42, 0.35);
  --knob: #f59e0b;
}
:root[data-theme="light"] .badge.ok { color: #15803d; }
:root[data-theme="light"] .badge.overdue { color: #b91c1c; }
:root[data-theme="light"] .badge.never { color: #64748b; background: #eef2f7; }
:root[data-theme="light"] .banner.warn { color: #b91c1c; }
:root[data-theme="light"] .banner.info { color: #1e40af; background: #eaf1fe; }
:root[data-theme="light"] .node.overdue .meta { color: #b91c1c; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--body-grad);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  transition: background .25s ease, color .25s ease;
}

.app { max-width: 780px; margin: 0 auto; padding: 0 14px 90px; }

/* Header */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; padding: 12px 14px;
  background: var(--topbar-bg);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.topbar .brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: .2px; }
.topbar .brand .logo {
  width: 30px; height: 30px; border-radius: 8px;
  background: linear-gradient(135deg, var(--brand), #22d3ee);
  display: grid; place-items: center; font-size: 16px;
}
.topbar .who { font-size: 13px; color: var(--muted); }
.topbar .who b { color: var(--text); }

/* Buttons */
button, .btn {
  font: inherit; cursor: pointer; border: none; border-radius: 12px;
  padding: 12px 16px; background: var(--brand); color: #fff; font-weight: 600;
  transition: transform .05s ease, background .15s ease; touch-action: manipulation;
}
button:active { transform: scale(.98); }
button.secondary { background: var(--card-2); color: var(--text); border: 1px solid var(--line); }
button.ghost { background: transparent; color: var(--muted); padding: 8px 10px; }
button.danger { background: var(--danger); }
button.small { padding: 8px 12px; font-size: 13px; border-radius: 10px; }
button:disabled { opacity: .5; cursor: not-allowed; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }

/* Inputs */
label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 6px; }
input, textarea, select {
  width: 100%; font: inherit; color: var(--text);
  background: var(--bg-2); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px 14px; outline: none;
}
input:focus, textarea:focus { border-color: var(--brand); }
textarea { min-height: 76px; resize: vertical; }

/* Cards / sections */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.section { margin-top: 18px; }
.section h2 { font-size: 15px; margin: 0 0 10px; display: flex; align-items: center; gap: 8px; color: var(--text); }
.section h2 .count { font-size: 12px; color: var(--muted); font-weight: 500; }
.hint { color: var(--muted); font-size: 13px; }

/* Node grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; }
.node {
  position: relative; border-radius: 12px; padding: 12px 8px; text-align: center;
  background: var(--card-2); border: 1px solid var(--line); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 6px; min-height: 78px; justify-content: center;
}
.node .nid { font-weight: 700; font-size: 15px; }
.node .meta { font-size: 11px; color: var(--muted); }
.node .dot { position: absolute; top: 8px; right: 8px; width: 10px; height: 10px; border-radius: 50%; background: var(--grey); }
.node.ok { border-color: rgba(34,197,94,.4); }
.node.ok .dot { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.node.overdue { border-color: rgba(239,68,68,.55); background: linear-gradient(180deg, var(--danger-bg), var(--card-2)); }
.node.overdue .dot { background: var(--danger); box-shadow: 0 0 8px var(--danger); animation: pulse 1.4s infinite; }
.node.never .dot { background: var(--grey); }
.node .disp { position: absolute; top: 7px; left: 8px; font-size: 10px; opacity: .8; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }

/* Badges */
.badge { display: inline-block; font-size: 11px; font-weight: 700; padding: 3px 8px; border-radius: 999px; }
.badge.ok { background: var(--ok-bg); color: #6ee7a8; }
.badge.overdue { background: var(--danger-bg); color: #fca5a5; }
.badge.never { background: #26313f; color: var(--muted); }

/* Capture */
.capture-photo { width: 100%; border-radius: 12px; border: 1px solid var(--line); display: block; background: #000; }
.dropzone {
  border: 2px dashed var(--line); border-radius: 14px; padding: 26px 16px; text-align: center; color: var(--muted);
  background: var(--bg-2);
}
.dropzone .big { font-size: 40px; }

/* Banner / toast */
.banner { border-radius: 12px; padding: 12px 14px; margin-top: 12px; font-size: 14px; }
.banner.warn { background: var(--danger-bg); color: #fecaca; border: 1px solid rgba(239,68,68,.4); }
.banner.info { background: #10233f; color: #bfdbfe; border: 1px solid rgba(59,130,246,.4); }
#toast {
  position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%);
  background: var(--toast-bg); color: #fff; padding: 12px 18px; border-radius: 12px; border: 1px solid var(--line);
  box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 60; max-width: 90%;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(-6px); }
#toast.err { border-color: var(--danger); }

/* Tab bar */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  display: flex; justify-content: space-around; gap: 4px;
  background: var(--tabbar-bg); border-top: 1px solid var(--line); padding: 8px 6px calc(8px + env(safe-area-inset-bottom));
  backdrop-filter: blur(8px);
}
.tabbar button { background: transparent; color: var(--muted); flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; padding: 6px; }
.tabbar button.active { color: var(--brand); }
.tabbar button .ic { font-size: 18px; }

/* Stat tiles (admin) */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px; text-align: center; }
.stat .num { font-size: 26px; font-weight: 800; }
.stat .lbl { font-size: 12px; color: var(--muted); }
.stat.good .num { color: var(--ok); }
.stat.bad .num { color: var(--danger); }

/* Table-ish list */
.list-item { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.list-item:last-child { border-bottom: none; }
.thumb { width: 52px; height: 52px; border-radius: 10px; object-fit: cover; background: #000; border: 1px solid var(--line); flex: none; }
.list-item .grow { flex: 1; min-width: 0; }
.list-item .grow .t1 { font-weight: 600; }
.list-item .grow .t2 { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Modal */
.modal-back { position: fixed; inset: 0; background: var(--overlay); display: none; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.modal-back.show { display: flex; }
.modal { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 18px; width: 100%; max-width: 460px; max-height: 88vh; overflow: auto; }
.modal h3 { margin: 0 0 6px; }

.hidden { display: none !important; }
.center { text-align: center; }
.spacer { height: 8px; }
.muted { color: var(--muted); }
a { color: var(--brand); }

/* ---------- Theme toggle (sun/moon pill) ---------- */
.theme-toggle {
  position: relative; width: 52px; height: 28px; border-radius: 999px;
  background: var(--card-2); border: 1px solid var(--line); padding: 0; cursor: pointer;
  flex: none; transition: background .2s ease;
}
.theme-toggle .knob {
  position: absolute; top: 2px; left: 2px; width: 22px; height: 22px; border-radius: 50%;
  background: var(--knob); display: grid; place-items: center; font-size: 12px; line-height: 1;
  transition: transform .22s ease; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
}
.theme-toggle .knob::after { content: "🌙"; }
:root[data-theme="light"] .theme-toggle .knob { transform: translateX(24px); }
:root[data-theme="light"] .theme-toggle .knob::after { content: "☀️"; }
.theme-toggle.on-login { position: absolute; top: 14px; right: 14px; z-index: 5; }

/* ---------- Header avatar + logo ---------- */
.avatar {
  width: 30px; height: 30px; border-radius: 50%; flex: none;
  background: linear-gradient(135deg, var(--brand), #22d3ee); color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: 12px; letter-spacing: .3px;
}
.topbar .brand .logo.has-img { background: transparent; overflow: hidden; }
.topbar .brand .logo img { width: 100%; height: 100%; object-fit: contain; }
.who { display: flex; align-items: center; gap: 8px; }
@media (max-width: 540px) { .who .fullname { display: none; } }

/* ---------- Inactive nodes ---------- */
.node.inactive { opacity: .6; border-style: dashed; }
.node.inactive .dot { background: var(--grey) !important; box-shadow: none !important; animation: none !important; }
.badge.off { background: #26313f; color: #cbd5e1; }
:root[data-theme="light"] .badge.off { background: #eef2f7; color: #64748b; }

/* ---------- Admin node manager ---------- */
.noderow { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--line); }
.noderow:last-child { border-bottom: none; }
.noderow .nlabel { font-weight: 600; }
.noderow .nstate { font-size: 12px; color: var(--muted); }
.switch { position: relative; width: 42px; height: 24px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch .track { position: absolute; inset: 0; background: var(--line); border-radius: 999px; transition: .2s; cursor: pointer; }
.switch .track::before { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.switch input:checked + .track { background: var(--ok); }
.switch input:checked + .track::before { transform: translateX(18px); }

/* ---------- Logo settings ---------- */
.logo-preview { height: 46px; max-width: 190px; object-fit: contain; background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px; padding: 4px 8px; }
.cat-subhead { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; margin: 14px 0 4px; }
.cat-subhead:first-child { margin-top: 0; }
