:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --text: #0f172a;
  --muted: #5b6474;
  --border: #d9dee5;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --danger: #b91c1c;
  --in: #15803d;
  --out: #c62828;
  --warn: #b45309;
  --blocked: #111111;
  --unknown: #4b5563;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 2px 8px rgb(0 0 0 / 6%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1017;
    --surface: #141b24;
    --surface-2: #1c2531;
    --text: #e6ebf1;
    --muted: #98a3b3;
    --border: #2a3441;
    --accent: #2dd4bf;
    --accent-text: #04201d;
    --danger: #f87171;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Noto Sans Arabic", Tahoma, sans-serif;
}

h1, h2, h3 { line-height: 1.2; margin: 0 0 12px; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 12px; }
a { color: var(--accent); }
[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; letter-spacing: 0.02em; direction: ltr; unicode-bidi: isolate; }
.nowrap { white-space: nowrap; }

/* ---------- layout ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { font-weight: 700; white-space: nowrap; }
.topbar nav { display: flex; gap: 4px; overflow-x: auto; flex: 1; }
.topbar nav a {
  padding: 6px 10px;
  border-radius: 8px;
  color: var(--muted);
  text-decoration: none;
  white-space: nowrap;
}
.topbar nav a.active { background: var(--surface-2); color: var(--text); font-weight: 600; }
.topbar .who { font-size: 0.85rem; color: var(--muted); white-space: nowrap; }
@media (max-width: 520px) {
  .topbar { gap: 8px; padding: 8px 12px; }
  .topbar:has(nav a) .brand { display: none; }
  .topbar:has(nav a) .who { display: none; }
}

.page { max-width: 1100px; margin: 0 auto; padding: 16px; }
.page.narrow { max-width: 560px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
}

.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: end; }
.row > * { flex: 1 1 160px; }
.row > .shrink { flex: 0 0 auto; }
.stack > * + * { margin-top: 12px; }

/* ---------- forms ---------- */

label { display: block; font-size: 0.875rem; font-weight: 600; color: var(--muted); }
label > input, label > select { margin-top: 4px; }

input, select, button { font: inherit; }

input[type="text"], input[type="number"], input[type="date"], input[type="password"], select {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
}
input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent);
  outline-offset: 1px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 34px; padding: 4px 10px; font-size: 0.875rem; }
.btn.big { min-height: 56px; font-size: 1.1rem; padding: 12px 24px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

.error-box {
  padding: 10px 12px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  color: var(--danger);
  font-weight: 600;
}

.code-box {
  display: inline-block;
  padding: 10px 14px;
  border: 2px dashed var(--border);
  border-radius: 10px;
  font-size: 1.4rem;
  font-weight: 700;
}

/* ---------- badges & lists ---------- */

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  background: var(--surface-2);
  color: var(--muted);
  white-space: nowrap;
}
.badge.in { background: color-mix(in srgb, var(--in) 16%, transparent); color: var(--in); }
.badge.out { background: color-mix(in srgb, var(--out) 16%, transparent); color: var(--out); }
.badge.warn { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.badge.bad { background: color-mix(in srgb, var(--danger) 16%, transparent); color: var(--danger); }
@media (prefers-color-scheme: dark) {
  .badge.in { color: #4ade80; }
  .badge.out { color: #f87171; }
  .badge.warn { color: #fbbf24; }
}

.list { list-style: none; margin: 0; padding: 0; }
.list > li {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.list > li:last-child { border-bottom: 0; }
.list .grow { flex: 1; min-width: 0; }
.list .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.list .voided .title, .list .voided .when { text-decoration: line-through; opacity: 0.6; }

.avatar {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface-2);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  color: var(--muted);
}

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.925rem; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: start; vertical-align: top; }
th { font-size: 0.8rem; color: var(--muted); font-weight: 700; white-space: nowrap; }
td.num, th.num { text-align: end; font-variant-numeric: tabular-nums; }
tr.clickable { cursor: pointer; }
tr.clickable:hover { background: var(--surface-2); }

.tabs { display: flex; gap: 4px; margin-bottom: 16px; overflow-x: auto; }
.tabs button {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 10px 14px;
  border-radius: 10px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.tabs button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.tile .value { font-size: 1.6rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.tile .label { color: var(--muted); font-size: 0.85rem; }

.grid-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  z-index: 100;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  font-weight: 600;
  box-shadow: 0 4px 16px rgb(0 0 0 / 25%);
}

dialog {
  width: min(520px, calc(100% - 32px));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 20px;
}
dialog::backdrop { background: rgb(0 0 0 / 50%); }

.qr-preview { background: #fff; padding: 12px; border-radius: 8px; display: inline-block; line-height: 0; }
.qr-preview svg { width: 180px; height: 180px; }

/* ---------- scanner ---------- */

.scanner { max-width: 560px; }

.sync-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 0.9rem;
  margin-bottom: 12px;
}
.dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto; background: var(--unknown); }
.dot.ok { background: #22c55e; }
.dot.pending { background: #f59e0b; }
.dot.offline { background: #ef4444; }

.camera {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: var(--radius);
  overflow: hidden;
  background: #000;
  margin-bottom: 12px;
}
.camera video { width: 100%; height: 100%; object-fit: cover; display: block; }
.camera .placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: #cbd5e1;
  text-align: center;
  padding: 24px;
}

.result-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  color: #fff;
  background: var(--unknown);
}
.result-overlay.r-in { background: var(--in); }
.result-overlay.r-out { background: var(--out); }
.result-overlay.r-warn { background: var(--warn); }
.result-overlay.r-blocked { background: var(--blocked); }
.result-overlay.r-unknown { background: var(--unknown); }
.result-card { width: min(420px, 100%); text-align: center; }
:root[dir="rtl"] .badge, :root[dir="rtl"] .result-label { letter-spacing: 0; }
.result-label { font-size: 2.2rem; font-weight: 900; letter-spacing: 0.04em; margin-bottom: 4px; }
.result-sub { font-size: 1.1rem; font-weight: 600; opacity: 0.95; margin-bottom: 16px; }
.result-photo {
  width: 220px;
  height: 220px;
  max-width: 70vw;
  max-height: 70vw;
  border-radius: 16px;
  object-fit: cover;
  background: rgb(255 255 255 / 18%);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 12px;
  font-size: 4rem;
  font-weight: 800;
  border: 4px solid rgb(255 255 255 / 85%);
  overflow: hidden;
}
.result-photo img { width: 100%; height: 100%; object-fit: cover; }
.result-name { font-size: 1.6rem; font-weight: 800; }
.result-detail { opacity: 0.9; margin: 4px 0 20px; }
.result-overlay .btn { background: rgb(255 255 255 / 18%); border-color: rgb(255 255 255 / 40%); color: #fff; min-width: 120px; }
.result-overlay .btn.solid { background: #fff; color: #111; }

/* ---------- print (keychain check sheet) ---------- */

.print-sheet { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.print-item { border: 1px solid var(--border); border-radius: 8px; padding: 10px; text-align: center; background: #fff; color: #000; break-inside: avoid; }
.print-item svg { width: 110px; height: 110px; margin: 8px; }

@media print {
  body { background: #fff; }
  body > *:not(#printArea) { display: none !important; }
  #printArea { display: block !important; padding: 0; }
  .print-item { border-color: #999; }
}
