:root {
  --bg: #0f172a;
  --bg-soft: #1e293b;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --amber: #d97706;
  --amber-bg: #fef3c7;
  --grey: #475569;
  --grey-bg: #e2e8f0;
  --radius: 16px;
  --shadow: 0 10px 30px rgba(2, 6, 23, 0.18);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* The `hidden` attribute must win over component display rules
   (e.g. .result/.card use display:flex, which would otherwise override it). */
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overscroll-behavior: none;
}

.view { display: none; min-height: 100dvh; }
.view.active { display: block; }

.muted { color: var(--muted); font-size: 13px; }
.center { text-align: center; }

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 24px;
  padding-top: max(24px, env(safe-area-inset-top));
  gap: 24px;
}
.brand { text-align: center; color: #e2e8f0; }
.brand-logo { font-size: 56px; }
.brand h1 { margin: 8px 0 4px; font-size: 24px; }
.brand .muted { color: #94a3b8; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: var(--shadow);
}
label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: #334155; }
input[type=text], input[type=password], input[type=url], input[type=search] {
  font-size: 16px;
  padding: 12px 14px;
  border: 1px solid #cbd5e1;
  border-radius: 12px;
  width: 100%;
  outline: none;
}
input:focus { border-color: var(--primary); }
.advanced summary { font-size: 13px; color: var(--muted); cursor: pointer; }
.advanced { margin-top: -4px; }
.error { color: var(--red); font-size: 13px; margin: 0; }

.btn {
  font-size: 16px;
  font-weight: 600;
  padding: 13px 16px;
  border-radius: 12px;
  border: none;
  cursor: pointer;
  transition: transform .05s ease, background .15s ease;
}
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:active { background: var(--primary-dark); }
.btn-ghost { background: rgba(255,255,255,0.1); color: #e2e8f0; border: 1px solid rgba(255,255,255,0.2); }
.btn-light { background: #fff; color: var(--text); }

/* ---------- App shell ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--bg);
  color: #e2e8f0;
}
.topbar-title { flex: 1; display: flex; flex-direction: column; line-height: 1.2; overflow: hidden; }
.topbar-title span { font-weight: 700; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-title small { color: #94a3b8; }
.icon-btn {
  background: rgba(255,255,255,0.08);
  border: none; color: #e2e8f0;
  width: 40px; height: 40px; border-radius: 12px;
  font-size: 18px; cursor: pointer;
}

.tab { display: none; padding: 16px; padding-bottom: 40px; }
.tab.active { display: block; }
.tab-title { color: #e2e8f0; font-size: 18px; margin: 4px 0 16px; }

/* ---------- Scanner ---------- */
.reader {
  width: 100%;
  border-radius: var(--radius);
  overflow: hidden;
  background: #000;
  min-height: 280px;
}
.reader video { width: 100% !important; display: block; border-radius: var(--radius); }
.scan-controls { display: flex; gap: 10px; margin-top: 16px; }
.scan-controls .btn { flex: 1; }
.manual-box { margin-top: 16px; }

/* ---------- Events ---------- */
.event-row, .event-card {
  display: flex; flex-direction: column; gap: 4px;
  width: 100%; text-align: left;
  background: var(--card); border: none;
  border-radius: 14px; padding: 16px; margin-bottom: 12px;
  cursor: pointer; box-shadow: 0 4px 14px rgba(0,0,0,0.12);
}
.event-row strong, .event-card strong { font-size: 16px; }
.event-row--all { border: 2px dashed #475569; background: #1e293b; color: #e2e8f0; }
.event-row--all .muted { color: #94a3b8; }
.event-card.selected { outline: 3px solid var(--primary); }
.perf {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 0; border-top: 1px solid #e2e8f0; font-size: 13px;
}
.perf:first-of-type { border-top: none; }
.perf .perf-when { font-weight: 600; }
.badge {
  display: inline-block; padding: 2px 9px; border-radius: 999px;
  font-size: 12px; font-weight: 700; background: var(--grey-bg); color: var(--grey);
}
.badge.ok { background: var(--green-bg); color: var(--green); }

/* ---------- History ---------- */
.history-filters { display: flex; gap: 12px; align-items: center; margin-bottom: 14px; }
.history-filters input[type=search] { flex: 1; }
.chk { flex-direction: row; align-items: center; gap: 6px; color: #cbd5e1; white-space: nowrap; }
.hist-item {
  background: var(--card); border-radius: 12px; padding: 12px 14px; margin-bottom: 10px;
  display: flex; align-items: center; gap: 12px; box-shadow: 0 3px 10px rgba(0,0,0,0.1);
}
.hist-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.hist-main { flex: 1; min-width: 0; }
.hist-main .serial { font-weight: 700; font-size: 14px; }
.hist-main .sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hist-time { font-size: 12px; color: var(--muted); white-space: nowrap; }

/* ---------- Result overlay ---------- */
.result {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  animation: fade .15s ease;
}
.result.success { background: var(--green-bg); }
.result.danger  { background: var(--red-bg); }
.result.warn    { background: var(--amber-bg); }
.result.neutral { background: var(--grey-bg); }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.result-card {
  background: #fff; border-radius: 22px; padding: 28px 22px;
  width: 100%; max-width: 440px; text-align: center;
  box-shadow: var(--shadow); max-height: 92dvh; overflow-y: auto;
}
.result-icon {
  width: 84px; height: 84px; border-radius: 50%; margin: 0 auto 14px;
  display: flex; align-items: center; justify-content: center;
  font-size: 46px; color: #fff; font-weight: 700;
}
.success .result-icon { background: var(--green); }
.danger  .result-icon { background: var(--red); }
.warn    .result-icon { background: var(--amber); }
.neutral .result-icon { background: var(--grey); }
.result-card h2 { margin: 0 0 6px; font-size: 22px; }
.success h2 { color: var(--green); }
.danger  h2 { color: var(--red); }
.warn    h2 { color: var(--amber); }
.neutral h2 { color: var(--grey); }
.result-message { color: var(--muted); margin: 0 0 16px; font-size: 15px; }
.result-details { text-align: left; margin-bottom: 20px; }
.detail-row {
  display: flex; justify-content: space-between; gap: 12px;
  padding: 9px 0; border-bottom: 1px solid #f1f5f9; font-size: 14px;
}
.detail-row:last-child { border-bottom: none; }
.detail-row .k { color: var(--muted); flex: none; }
.detail-row .v { font-weight: 600; text-align: right; }
.result-warning {
  background: var(--red-bg); color: var(--red); border-radius: 12px;
  padding: 12px 14px; font-size: 14px; font-weight: 600; margin-bottom: 16px;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%);
  background: #0f172a; color: #fff; padding: 12px 18px; border-radius: 12px;
  font-size: 14px; z-index: 60; box-shadow: var(--shadow); max-width: 90%;
}
