:root {
  --bg: #0b1120;
  --surface: #0f172a;
  --surface-2: #16213a;
  --surface-3: #1e2a44;
  --border: #26334d;
  --text: #e6edf7;
  --muted: #93a3bd;
  --faint: #64748b;
  --accent: #38bdf8;
  --accent-2: #0ea5e9;
  --on-accent: #062033;
  --danger: #f87171;
  --danger-bg: #3b1d24;
  --warn: #fbbf24;
  --warn-bg: #3a2f14;
  --ok: #34d399;
  --ok-bg: #143226;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 10px 30px rgba(0, 0, 0, .35);
  --tap: 44px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: radial-gradient(1200px 600px at 50% -10%, #13233f 0%, var(--bg) 55%) fixed, var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.12rem; }
h3 { font-size: 1rem; }
p { margin: 0; }

.boot {
  min-height: 100dvh; display: grid; place-items: center; color: var(--muted);
}

/* ---------- App shell ---------- */
.app-shell { min-height: 100dvh; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: rgba(11, 17, 32, .86); backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.topbar .brand img { width: 30px; height: 30px; border-radius: 8px; }
.topbar .brand b { font-size: 1.02rem; letter-spacing: .2px; white-space: nowrap; }
.topbar .spacer { flex: 1; }
.user-chip {
  display: flex; align-items: center; gap: 8px; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 999px; padding: 5px 6px 5px 12px;
  cursor: pointer; min-height: 38px;
}
.user-chip .avatar {
  width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), #6366f1); color: var(--on-accent);
  font-weight: 700; font-size: .82rem; text-transform: uppercase;
}
.user-chip span { max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .9rem; }

.main { flex: 1; width: 100%; max-width: 1100px; margin: 0 auto; padding: 16px 16px 96px; }
.view-head { display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.view-head .sub { color: var(--muted); font-size: .9rem; }
.view-head .grow { flex: 1; }

/* ---------- Nav ---------- */
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 35;
  display: flex; justify-content: space-around; gap: 2px;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  background: rgba(11, 17, 32, .94); backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
}
.nav button {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px;
  background: none; border: 0; color: var(--muted); padding: 6px 2px; border-radius: 10px;
  font-size: .68rem; font-weight: 600; cursor: pointer; min-height: 48px; justify-content: center;
}
.nav button .ic { font-size: 1.2rem; line-height: 1; }
.nav button[aria-current="true"] { color: var(--accent); background: rgba(56, 189, 248, .1); }
.nav button:disabled { opacity: .4; }

@media (min-width: 820px) {
  .main { padding-bottom: 32px; }
  .nav {
    position: sticky; top: 0; bottom: auto; justify-content: flex-start; gap: 4px;
    padding: 8px 16px; max-width: 1100px; margin: 0 auto; width: 100%;
    border-top: 0; border-bottom: 1px solid var(--border); background: transparent; backdrop-filter: none;
  }
  .nav button { flex: 0 0 auto; flex-direction: row; gap: 8px; font-size: .86rem; padding: 8px 14px; min-height: 40px; }
  .topbar { position: static; background: transparent; backdrop-filter: none; border-bottom: 0; padding-bottom: 0; }
}

/* ---------- Cards / layout ---------- */
.grid { display: grid; gap: 12px; }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.auto { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
@media (min-width: 700px) {
  .grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .grid.cols-4 { display: grid; gap: 12px; }
}

.card {
  background: linear-gradient(180deg, var(--surface-2), var(--surface));
  border: 1px solid var(--border); border-radius: var(--radius); padding: 14px;
}
.card.tight { padding: 12px; }
.card h2 { margin-bottom: 10px; }
.card .muted { color: var(--muted); font-size: .86rem; }

.stat { display: flex; flex-direction: column; gap: 2px; }
.stat .n { font-size: 1.7rem; font-weight: 800; letter-spacing: -.5px; }
.stat .l { color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .6px; }

.stack { display: flex; flex-direction: column; gap: 10px; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.row.nowrap { flex-wrap: nowrap; }
.row .grow { flex: 1; min-width: 0; }
.mt { margin-top: 12px; }
.mb { margin-bottom: 12px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.nowrap { white-space: nowrap; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  min-height: var(--tap); padding: 0 16px; border-radius: var(--radius-sm);
  background: var(--surface-3); border: 1px solid var(--border); color: var(--text);
  font-weight: 600; cursor: pointer; white-space: nowrap; transition: transform .05s, background .15s, border-color .15s;
  text-decoration: none;
}
.btn:active { transform: scale(.98); }
.btn:hover { border-color: #34456a; }
.btn.primary { background: linear-gradient(180deg, var(--accent), var(--accent-2)); border-color: transparent; color: var(--on-accent); }
.btn.ghost { background: transparent; }
.btn.danger { background: var(--danger-bg); border-color: #6b2a33; color: #ffb4b4; }
.btn.ok { background: var(--ok-bg); border-color: #1f5c45; color: #a7f3d0; }
.btn.sm { min-height: 36px; padding: 0 12px; font-size: .86rem; }
.btn.icon { min-height: 38px; width: 38px; padding: 0; font-size: 1.05rem; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

/* ---------- Forms ---------- */
.field { display: flex; flex-direction: column; gap: 6px; }
.field > label { font-size: .82rem; color: var(--muted); font-weight: 600; }
.input, .select, .textarea {
  width: 100%; min-height: var(--tap); padding: 10px 12px; border-radius: var(--radius-sm);
  background: #0a1526; border: 1px solid var(--border); color: var(--text); outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.textarea { min-height: 80px; resize: vertical; }
.input:focus, .select:focus, .textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(56, 189, 248, .18); }
.input::placeholder { color: var(--faint); }
.form-grid { display: grid; gap: 12px; grid-template-columns: 1fr; }
@media (min-width: 560px) { .form-grid.two { grid-template-columns: 1fr 1fr; } .form-grid .span2 { grid-column: 1 / -1; } }
.input-row { display: flex; gap: 8px; }
.input-row .input { flex: 1; }
.searchbar { position: relative; flex: 1; min-width: 160px; }
.searchbar .input { padding-left: 38px; }
.searchbar .mag { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--faint); pointer-events: none; }

/* ---------- Lists ---------- */
.list { display: flex; flex-direction: column; gap: 8px; }
.item {
  display: flex; align-items: center; gap: 12px; padding: 12px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
}
.item .grow { flex: 1; min-width: 0; }
.item .title { font-weight: 650; }
.item .meta { color: var(--muted); font-size: .82rem; }
.item .actions { display: flex; gap: 6px; flex-shrink: 0; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 999px;
  font-size: .74rem; font-weight: 700; background: var(--surface-3); border: 1px solid var(--border); color: var(--muted);
}
.chip.ok { background: var(--ok-bg); color: var(--ok); border-color: #1f5c45; }
.chip.warn { background: var(--warn-bg); color: var(--warn); border-color: #6b5a1c; }
.chip.danger { background: var(--danger-bg); color: var(--danger); border-color: #6b2a33; }
.chip.accent { background: rgba(56,189,248,.14); color: var(--accent); border-color: #1c4a63; }

.section-title { display: flex; align-items: center; gap: 8px; margin: 22px 0 10px; color: var(--muted); font-size: .8rem; font-weight: 700; text-transform: uppercase; letter-spacing: .8px; }
.section-title::after { content: ""; flex: 1; height: 1px; background: var(--border); }

.empty { text-align: center; color: var(--muted); padding: 28px 12px; border: 1px dashed var(--border); border-radius: var(--radius); }
.empty .big { font-size: 2rem; display: block; margin-bottom: 6px; }

/* ---------- Tree filter ---------- */
.filters { display: flex; flex-direction: column; gap: 10px; }
.filter-row { display: grid; grid-template-columns: 1fr; gap: 10px; }
@media (min-width: 640px) { .filter-row { grid-template-columns: repeat(3, 1fr) auto; align-items: end; } }

.tree-group { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.tree-group > summary {
  list-style: none; cursor: pointer; padding: 12px 14px; display: flex; align-items: center; gap: 10px;
  background: var(--surface-2); font-weight: 650;
}
.tree-group > summary::-webkit-details-marker { display: none; }
.tree-group > summary .caret { transition: transform .15s; color: var(--muted); }
.tree-group[open] > summary .caret { transform: rotate(90deg); }
.tree-unit { padding: 6px 12px 12px; }
.tree-unit .unit-head { display: flex; align-items: center; gap: 8px; padding: 8px 4px; color: var(--muted); font-size: .9rem; font-weight: 600; }
.comp { margin-left: 8px; border-left: 2px solid var(--border); padding-left: 12px; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 100; display: flex; align-items: flex-end; justify-content: center;
  background: rgba(2, 6, 16, .66); backdrop-filter: blur(3px); padding: 0;
}
@media (min-width: 640px) { .modal-backdrop { align-items: center; padding: 20px; } }
.modal {
  width: 100%; max-width: 560px; max-height: 92dvh; overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 18px 18px 0 0;
  padding: 18px; box-shadow: var(--shadow); animation: slideUp .18s ease-out;
}
@media (min-width: 640px) { .modal { border-radius: 18px; } }
@keyframes slideUp { from { transform: translateY(18px); opacity: .6; } to { transform: none; opacity: 1; } }
.modal-head { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.modal-head h2 { flex: 1; }
.modal-foot { display: flex; gap: 10px; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }
.modal-foot .btn { flex: 1; }
@media (min-width: 640px) { .modal-foot .btn { flex: 0 0 auto; min-width: 110px; } }

/* ---------- Toasts ---------- */
.toasts { position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%); z-index: 200; display: flex; flex-direction: column; gap: 8px; width: min(94vw, 460px); pointer-events: none; }
.toast {
  padding: 11px 14px; border-radius: 12px; background: var(--surface-3); border: 1px solid var(--border);
  color: var(--text); box-shadow: var(--shadow); font-size: .9rem; animation: toastIn .18s ease-out;
}
.toast.ok { background: var(--ok-bg); border-color: #1f5c45; color: #b9f6da; }
.toast.error { background: var(--danger-bg); border-color: #6b2a33; color: #ffc4c4; }
@keyframes toastIn { from { transform: translateY(10px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- Auth screens ---------- */
.auth-wrap { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.auth-card {
  width: 100%; max-width: 400px; background: linear-gradient(180deg, var(--surface-2), var(--surface));
  border: 1px solid var(--border); border-radius: 20px; padding: 26px; box-shadow: var(--shadow);
}
.auth-card .logo { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; }
.auth-card .logo img { width: 44px; height: 44px; border-radius: 12px; }
.auth-card h1 { font-size: 1.3rem; }
.auth-card .muted { font-size: .88rem; }
.auth-card form { display: flex; flex-direction: column; gap: 14px; margin-top: 20px; }

/* scanner */
.scan-box { width: 100%; border-radius: var(--radius); overflow: hidden; background: #000; min-height: 260px; }
.scan-box video { width: 100% !important; height: auto !important; }
.scan-hint { color: var(--muted); font-size: .85rem; text-align: center; margin-top: 10px; }

/* picker */
.picker-list { max-height: 280px; overflow: auto; display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }
.picker-opt {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 10px;
  background: var(--surface-2); border: 1px solid var(--border); cursor: pointer; text-align: left;
}
.picker-opt:hover { border-color: var(--accent); }
.picker-opt .grow { flex: 1; min-width: 0; }

.expiry-ok { color: var(--ok); }
.expiry-soon { color: var(--warn); }
.expiry-over { color: var(--danger); font-weight: 700; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: .88rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

.offline-banner {
  background: var(--warn-bg); color: var(--warn); border: 1px solid #6b5a1c;
  padding: 8px 14px; border-radius: 10px; margin-bottom: 12px; font-size: .86rem;
}

.ic { display: inline-flex; align-items: center; justify-content: center; }
.ic svg { display: block; }

@media (max-width: 520px) {
  .item { flex-wrap: wrap; }
  .item .actions { width: 100%; justify-content: flex-end; }
}
