/* Mobile-first styles */
:root {
  --bg: #f3f5f8;
  --card: #ffffff;
  --text: #1c2430;
  --muted: #677385;
  --line: #dde2e9;
  --primary: #1f4e79;
  --primary-2: #2a6aa3;
  --primary-ink: #ffffff;
  --ok-bg: #e5f5ea; --ok-ink: #1d6b35;
  --warn-bg: #fff4dc; --warn-ink: #8a5a00;
  --err-bg: #fde8e8; --err-ink: #a12424;
  --danger: #c0392b;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161d; --card: #1a212b; --text: #e6ebf2; --muted: #95a1b2; --line: #2b3542;
    --primary: #3b82c4; --primary-2: #5a9bd6;
    --ok-bg: #173323; --ok-ink: #8fdcaa; --warn-bg: #3a2e12; --warn-ink: #f2cc78; --err-bg: #3d1d1d; --err-ink: #f5a3a3;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", Arial, sans-serif;
}
body.no-scroll { overflow: hidden; }
a { color: var(--primary-2); text-decoration: none; }
h1 { font-size: 1.4rem; margin: .2rem 0 1rem; }
h2 { font-size: 1.1rem; margin: 0 0 .8rem; }
h3 { font-size: 1.05rem; margin: 0; }
.muted { color: var(--muted); font-size: .9rem; }
.center { text-align: center; }
.mono { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: .92em; }
.inline { display: inline; margin: 0; }

.container { max-width: 1100px; margin: 0 auto; padding: 16px 16px calc(32px + env(safe-area-inset-bottom)); }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--primary); color: #fff;
}
.topbar .brand { color: #fff; font-weight: 700; white-space: nowrap; }
.company-pill { background: rgba(255,255,255,.18); padding: 2px 10px; border-radius: 999px; font-size: .85rem; max-width: 45vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu-btn { margin-left: auto; background: none; border: 0; color: #fff; font-size: 1.5rem; padding: 4px 8px; cursor: pointer; }
.nav { display: none; width: 100%; flex-direction: column; gap: 2px; padding-top: 6px; }
.nav-open .nav { display: flex; }
.nav a, .nav .linklike { color: #fff; padding: 10px 8px; border-radius: 8px; opacity: .9; text-align: left; }
.nav a.on { background: rgba(255,255,255,.18); opacity: 1; }
.linklike { background: none; border: 0; font: inherit; cursor: pointer; width: 100%; }
@media (min-width: 800px) {
  .menu-btn { display: none; }
  .nav { display: flex; flex-direction: row; width: auto; margin-left: auto; padding: 0; align-items: center; }
  .nav a, .nav .linklike { padding: 6px 10px; }
  .linklike { width: auto; }
}

/* Cards & layout */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 12px; border: 1px solid var(--line); }
.list { display: flex; flex-direction: column; gap: 0; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 6px; }
.page-head h1 { margin: .2rem 0 .6rem; }
.group-title { margin-top: 1.4rem; color: var(--muted); font-size: .95rem; text-transform: uppercase; letter-spacing: .03em; }

/* Alerts */
.alert { padding: 12px 14px; border-radius: 10px; margin-bottom: 12px; font-weight: 500; }
.alert-ok { background: var(--ok-bg); color: var(--ok-ink); }
.alert-warn { background: var(--warn-bg); color: var(--warn-ink); }
.alert-error { background: var(--err-bg); color: var(--err-ink); }

/* Forms */
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: .85rem; font-weight: 600; color: var(--muted); margin-bottom: 5px; }
.field.check { display: flex; align-items: center; gap: 8px; font-weight: 600; }
input, select {
  width: 100%; font-size: 16px; /* >=16px: no zoom on iOS */
  padding: 12px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); color: var(--text); outline: none; min-height: 48px;
}
input[type=checkbox] { width: 22px; height: 22px; min-height: 0; }
input:focus, select:focus { border-color: var(--primary-2); box-shadow: 0 0 0 3px rgba(42,106,163,.18); }
fieldset { border: 1px dashed var(--line); border-radius: 10px; padding: 10px 12px 0; margin: 0 0 14px; }
legend { font-weight: 700; font-size: .9rem; padding: 0 6px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row-form { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.row-form input { flex: 1 1 200px; }
.grid-form { display: grid; grid-template-columns: 1fr; gap: 0 12px; margin-top: 10px; }
@media (min-width: 700px) { .grid-form { grid-template-columns: repeat(3, 1fr); align-items: end; } }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 10px 16px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--card); color: var(--text); font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .6; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-sm { min-height: 36px; padding: 6px 12px; font-size: .9rem; }
.btn-lg { min-height: 56px; font-size: 1.1rem; }
.btn-block { width: 100%; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; margin: 6px 6px 0 0; padding: 6px 12px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit; font-size: .9rem; cursor: pointer;
}
.chip-x { border-color: var(--primary-2); color: var(--primary-2); }

/* Autocomplete */
.ac, .with-btn { position: relative; display: flex; gap: 8px; }
.ac input { padding-right: 40px; }
.ac .clear {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%); width: 36px; height: 36px;
  border: 0; background: none; color: var(--muted); font-size: 1.4rem; cursor: pointer; line-height: 1;
}
.ac input:placeholder-shown + .clear { display: none; }
.ac-list {
  position: absolute; z-index: 30; top: calc(100% + 4px); left: 0; right: 0; margin: 0; padding: 4px; list-style: none;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.15);
  max-height: 50vh; overflow-y: auto;
}
.ac-list li { display: flex; justify-content: space-between; gap: 8px; padding: 12px 10px; border-radius: 8px; cursor: pointer; }
.ac-list li b { color: var(--primary-2); }
.ac-list li small { color: var(--muted); }
.ac-list li.on, .ac-list li:hover { background: var(--bg); }
.btn-scan { flex: 0 0 52px; min-height: 48px; border-radius: 10px; border: 1px solid var(--primary); background: var(--primary); color: #fff; font-size: 1.3rem; cursor: pointer; }
.dup-warn { color: var(--warn-ink); font-size: .9rem; font-weight: 600; margin-top: 4px; }
.dup-warn:empty { display: none; }

/* Scan form sticky save */
.scan-form { padding-bottom: 0; }
.sticky-actions {
  position: sticky; bottom: 0; display: flex; gap: 10px; margin: 0 -16px; padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--card); border-top: 1px solid var(--line); border-radius: 0 0 var(--radius) var(--radius);
}
.sticky-actions .btn { flex: 1; }
.counter { background: var(--ok-bg); color: var(--ok-ink); font-weight: 700; padding: 4px 12px; border-radius: 999px; font-size: .9rem; }

/* Items list */
.item { display: block; color: inherit; margin-bottom: 8px; padding: 12px 14px; }
.item-main { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.item-sub { margin-top: 2px; }
.count { background: var(--primary); color: #fff; border-radius: 999px; padding: 1px 10px; font-weight: 700; font-size: .9rem; }
.row-link { display: flex; justify-content: space-between; align-items: center; color: inherit; }
.floors { margin-top: 4px; }

/* Stats & tabs */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; text-align: center; color: inherit; }
.stat b { display: block; font-size: 1.5rem; color: var(--primary-2); }
.stat span { font-size: .85rem; color: var(--muted); }
.tabs { display: flex; gap: 4px; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 4px; margin-bottom: 12px; }
.tabs a { flex: 1; text-align: center; padding: 10px 6px; border-radius: 9px; color: var(--muted); font-weight: 600; font-size: .92rem; }
.tabs a.on { background: var(--primary); color: #fff; }

.filters { display: flex; flex-wrap: wrap; gap: 8px; }
.filters input { flex: 1 1 100%; }
.filters select { flex: 1 1 60%; }
.filters .btn { flex: 1 1 auto; }
@media (min-width: 800px) { .filters input { flex: 2 1 300px; } .filters select { flex: 1 1 200px; } .filters .btn { flex: 0 0 auto; } }
.active-filters { margin: -4px 0 8px; }

/* Responsive table -> cards on mobile */
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: .82rem; color: var(--muted); padding: 8px; border-bottom: 2px solid var(--line); }
.table td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
@media (max-width: 799px) {
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table tr { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 10px; padding: 8px 12px; position: relative; }
  .table td { border: 0; padding: 3px 0; display: flex; gap: 10px; }
  .table td:empty { display: none; }
  .table td::before { content: attr(data-label); flex: 0 0 90px; color: var(--muted); font-size: .82rem; padding-top: 2px; }
  .table td.actions-cell { position: absolute; top: 8px; right: 8px; width: auto; }
  .table td.actions-cell::before { content: none; }
}
@media (min-width: 800px) { .table { background: var(--card); border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); } .table tbody tr:hover { background: var(--bg); } }
.pager { display: flex; justify-content: center; align-items: center; gap: 12px; margin-top: 14px; }

/* Admin */
.company.is-active { border-color: var(--primary-2); box-shadow: 0 0 0 2px var(--primary-2); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 4px; }
details { margin-top: 10px; }
summary { cursor: pointer; color: var(--muted); font-weight: 600; padding: 6px 0; }
details form { margin-top: 10px; }
.user.inactive { opacity: .6; }
.badge { display: inline-block; font-size: .75rem; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--bg); border: 1px solid var(--line); margin-left: 6px; }
.badge-admin { background: #7a2d8c; color: #fff; border-color: transparent; }
.badge-owner { background: #1f6f43; color: #fff; border-color: transparent; }
.badge-auditor { background: var(--primary); color: #fff; border-color: transparent; }

/* Login */
.login-wrap { min-height: 80vh; display: flex; align-items: center; justify-content: center; }
.login { width: 100%; max-width: 380px; padding: 28px 22px; }
.login h1 { text-align: center; }
.login-logo { font-size: 3rem; text-align: center; }

/* Camera scanner */
.scanner { position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.85); display: flex; align-items: center; justify-content: center; padding: 16px; }
.scanner[hidden] { display: none; }
.scanner-box { width: 100%; max-width: 480px; background: var(--card); border-radius: var(--radius); padding: 12px; }
#reader { width: 100%; border-radius: 8px; overflow: hidden; background: #000; min-height: 200px; }
#reader video { width: 100% !important; }
