/* ===== Biến màu (sáng / tối) ===== */
:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eef1f7;
  --text: #111827;
  --muted: #6b7280;
  --border: #e2e6ef;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --pass: #15803d;
  --pass-bg: #dcfce7;
  --fail: #dc2626;
  --fail-bg: #fee2e2;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --neutral: #6b7280;
  --neutral-bg: #e5e7eb;
  --running: #2563eb;
  --running-bg: #dbeafe;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 4px 14px rgb(0 0 0 / 0.05);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --surface: #131c2e;
    --surface-2: #1b2640;
    --text: #e5e9f2;
    --muted: #94a0b8;
    --border: #26324d;
    --primary: #3b82f6;
    --pass: #4ade80;
    --pass-bg: #12331f;
    --fail: #f87171;
    --fail-bg: #3b1518;
    --warn: #fbbf24;
    --warn-bg: #3a2a0c;
    --neutral: #a1a9b8;
    --neutral-bg: #252f42;
    --running: #60a5fa;
    --running-bg: #172a4a;
    --shadow: none;
  }
}

* { 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, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
body.no-scroll { overflow: hidden; overscroll-behavior: none; }

.container {
  max-width: 640px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

h1 { font-size: 1.6rem; line-height: 1.25; margin: 0 0 4px; }
h2 { font-size: 1.15rem; margin: 0 0 8px; }
h3 { font-size: 1.05rem; margin: 12px 0 6px; }
p { margin: 0 0 8px; }
a { color: var(--primary); }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
code { background: var(--surface-2); padding: 1px 6px; border-radius: 6px; }

.screen { display: none; }
.screen.active { display: block; animation: fade-in 0.2s ease; }
@keyframes fade-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

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

.hero { text-align: center; padding: 16px 0 12px; }
.hero-icon { font-size: 3rem; line-height: 1; margin-bottom: 8px; }

.checklist { list-style: none; padding: 0; margin: 0; }
.checklist li { display: flex; gap: 10px; padding: 6px 0; border-bottom: 1px dashed var(--border); }
.checklist li:last-child { border-bottom: 0; }

.privacy { border-color: var(--pass); }

/* ===== Nút ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 10px 18px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
.btn:active { transform: scale(0.98); }
.btn:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn-pass { background: var(--pass); border-color: var(--pass); color: #fff; }
.btn-fail, .btn-danger { background: var(--fail); border-color: var(--fail); color: #fff; }
.btn-lg { min-height: 54px; font-size: 1.05rem; }
.btn-xl { width: 100%; min-height: 64px; font-size: 1.25rem; border-radius: 16px; margin: 8px 0 16px; }
.btn-small { min-height: 36px; padding: 6px 12px; font-size: 0.875rem; }
@media (prefers-color-scheme: dark) {
  .btn-pass { color: #052e12; }
  .btn-fail, .btn-danger { color: #fff; background: #dc2626; border-color: #dc2626; }
}
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }
.btn-row .btn { flex: 1 1 140px; }
.btn-col { display: flex; flex-direction: column; gap: 10px; }

/* ===== Thông báo ===== */
.alert { border-radius: var(--radius); padding: 12px 14px; margin: 0 0 12px; border: 1px solid transparent; }
.alert-danger { background: var(--fail-bg); color: var(--fail); border-color: var(--fail); }
.alert-warn { background: var(--warn-bg); color: var(--text); border-color: var(--warn); }
.perm-help h3 { margin-top: 10px; }
.perm-help ol { margin: 4px 0 0; padding-left: 20px; }

/* ===== Tiến trình ===== */
.progress-wrap { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: 8px 0 12px; }
.btn-stop { display: block; margin: 10px 0 0 auto; }
.progress-label { display: flex; justify-content: space-between; font-weight: 600; margin-bottom: 6px; }
.progress { height: 10px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
#progress-bar { height: 100%; width: 0; background: var(--primary); transition: width 0.3s ease; }

.stage { border-color: var(--primary); }
.stage-title { margin-bottom: 4px; }
.stage-hint { color: var(--muted); }
.stage-body:empty { display: none; }
.big-text { font-size: 1.15rem; font-weight: 600; }
.intro ul { padding-left: 20px; }
.ask-q { font-size: 1.15rem; font-weight: 600; margin-bottom: 12px; }

.cam-live { width: 100%; max-height: 40vh; object-fit: cover; border-radius: 10px; background: #000; }

.meter { height: 14px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.meter-bar { height: 100%; width: 0; background: var(--pass); transition: width 0.05s linear; }

/* ===== Danh sách test ===== */
.test-list { list-style: none; padding: 0; margin: 0; }
.test-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left-width: 5px;
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 8px;
}
.test-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.test-name { font-weight: 600; }
.test-detail { font-size: 0.875rem; color: var(--muted); margin: 4px 0 0; }
.test-detail:empty { display: none; }
.status-running { border-left-color: var(--running); }
.status-pass { border-left-color: var(--pass); }
.status-fail { border-left-color: var(--fail); }
.status-skip { border-left-color: var(--warn); }
.status-unsupported, .status-pending { border-left-color: var(--neutral-bg); }

.chip {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 99px;
  font-size: 0.8rem;
  font-weight: 700;
  white-space: nowrap;
}
.chip-pass { background: var(--pass-bg); color: var(--pass); }
.chip-fail { background: var(--fail-bg); color: var(--fail); }
.chip-skip { background: var(--warn-bg); color: var(--warn); }
.chip-unsupported, .chip-pending { background: var(--neutral-bg); color: var(--neutral); }
.chip-running { background: var(--running-bg); color: var(--running); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.55; } }

/* ===== Báo cáo ===== */
.report-header { margin: 8px 0 12px; }
.verdict { border-radius: var(--radius); padding: 14px; text-align: center; font-weight: 700; font-size: 1.15rem; margin-bottom: 12px; }
.verdict-ok { background: var(--pass-bg); color: var(--pass); }
.verdict-bad { background: var(--fail-bg); color: var(--fail); }
.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 4px; text-align: center; }
.tile b { display: block; font-size: 1.6rem; line-height: 1.1; }
.tile span { font-size: 0.75rem; color: var(--muted); }
.tile-pass b { color: var(--pass); }
.tile-fail b { color: var(--fail); }
.tile-unsupported b { color: var(--neutral); }
.tile-skip b { color: var(--warn); }
.report-group { margin-bottom: 16px; }
.result-list { list-style: none; margin: 0; padding: 0; }
.result {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left-width: 5px;
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 8px;
}
.result-pass { border-left-color: var(--pass); }
.result-fail { border-left-color: var(--fail); }
.result-skip { border-left-color: var(--warn); }
.result-unsupported { border-left-color: var(--neutral-bg); }
.result-head { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.result-name { font-weight: 600; }
.result-detail { font-size: 0.875rem; color: var(--muted); margin: 4px 0 0; overflow-wrap: anywhere; }
.result-preview { display: block; margin-top: 8px; width: 160px; height: 120px; object-fit: cover; border-radius: 8px; }
.device-table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: 12px; overflow: hidden; }
.device-table th, .device-table td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
.device-table th { color: var(--muted); font-weight: 500; width: 42%; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 12px 0; }
.actions .btn { width: 100%; }
.actions .btn[hidden] { display: none; }
.share { text-align: center; }
.share .code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 1.2rem; letter-spacing: 2px; }
.share-link { overflow-wrap: anywhere; }
.qr { width: 220px; max-width: 70vw; margin: 12px auto; background: #fff; border-radius: 8px; }
.qr svg { display: block; width: 100%; height: auto; }

/* ===== Lớp phủ toàn màn hình (test thủ công) ===== */
.overlay {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  z-index: 1000;
  background: #000;
  color: #fff;
  touch-action: none;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.ask-overlay {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(92vw, 420px);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: 0 10px 40px rgb(0 0 0 / 0.5);
  z-index: 3;
}

/* Cảm ứng */
.overlay-touch { background: #111; }
.touch-grid { display: grid; width: 100%; height: 100%; }
.touch-grid .cell { border: 1px solid rgb(255 255 255 / 0.12); background: #222; }
.touch-grid .cell.hit { background: #16a34a; border-color: #15803d; }
.touch-pill {
  position: absolute;
  left: 50%;
  top: max(12px, env(safe-area-inset-top));
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px 6px 14px;
  background: rgb(0 0 0 / 0.78);
  color: #fff;
  border-radius: 99px;
  font-weight: 700;
  white-space: nowrap;
  pointer-events: none;
  z-index: 2;
}
.touch-pill.bottom { top: auto; bottom: max(12px, env(safe-area-inset-bottom)); }
.touch-pill .btn { pointer-events: auto; }
.pill-timer { font-variant-numeric: tabular-nums; min-width: 3ch; }

/* Đa điểm */
.overlay-multi { background: #0f172a; }
.overlay-multi.success { background: #14532d; transition: background 0.2s; }
.multi-center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; pointer-events: none; }
.multi-count { font-size: 7rem; font-weight: 800; line-height: 1; }
.multi-label { font-size: 1.2rem; opacity: 0.85; text-align: center; padding: 0 16px; }
.touch-dot {
  position: absolute;
  left: -45px;
  top: -45px;
  width: 90px;
  height: 90px;
  border-radius: 50%;
  border: 4px solid #60a5fa;
  background: rgb(96 165 250 / 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 1.4rem;
  pointer-events: none;
}
.overlay-actions {
  position: absolute;
  left: 0;
  right: 0;
  bottom: max(16px, env(safe-area-inset-bottom));
  display: flex;
  justify-content: center;
  gap: 12px;
}
.overlay-actions .btn { min-width: 120px; }

/* Màn hình */
.overlay-screen { cursor: pointer; }
.screen-hint {
  position: absolute;
  left: 50%;
  /* Đặt ở trên: Chrome Android hiện thông báo "thoát toàn màn hình" ở đáy, che mất gợi ý */
  top: max(24px, env(safe-area-inset-top));
  transform: translateX(-50%);
  background: rgb(0 0 0 / 0.65);
  color: #fff;
  padding: 8px 14px;
  border-radius: 99px;
  font-weight: 600;
  white-space: nowrap;
  transition: opacity 0.6s;
  pointer-events: none;
}
.screen-hint.fade { opacity: 0; }

/* ===== Admin ===== */
body.admin .admin-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
body.admin .admin-bar form { margin: 0; }
.admin-brand { font-weight: 700; text-decoration: none; color: var(--text); }
.admin-main { max-width: 1100px; margin: 0 auto; padding: 16px; }
.login-card { max-width: 380px; margin: 40px auto; display: flex; flex-direction: column; gap: 12px; }
.login-card label, .filters label { display: flex; flex-direction: column; gap: 4px; font-size: 0.875rem; color: var(--muted); }
input, select {
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  min-height: 42px;
}
.pw-label { display: block; font-weight: 600; margin-bottom: 8px; }
.pw-field { position: relative; display: flex; }
.pw-field input { flex: 1; min-width: 0; padding-right: 48px; }
.pw-toggle { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); width: 40px; height: 36px; border: 0; background: transparent; font-size: 1.2rem; cursor: pointer; border-radius: 8px; }
.pw-toggle[aria-pressed="true"] { opacity: 0.55; }
.pw-error { color: var(--fail); margin: 8px 0 0; }
.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; align-items: end; }
.filter-actions { display: flex; gap: 8px; flex-wrap: wrap; grid-column: 1 / -1; }
.admin-table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; margin-bottom: 12px; }
.admin-table th, .admin-table td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.admin-table th { background: var(--surface-2); font-size: 0.85rem; }
.admin-table tr.row-fail td:first-child { box-shadow: inset 4px 0 0 var(--fail); }
.admin-table form { margin: 0; }
.pager { display: flex; gap: 12px; align-items: center; justify-content: center; }
pre.json { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 0.8rem; max-height: 60vh; overflow: auto; }

@media (max-width: 700px) {
  .admin-table thead { display: none; }
  .admin-table, .admin-table tbody, .admin-table tr, .admin-table td { display: block; width: 100%; }
  .admin-table tr { border-bottom: 1px solid var(--border); padding: 6px 0; }
  .admin-table td { border: 0; padding: 4px 10px; }
  .admin-table td[data-label]:not([data-label=""])::before { content: attr(data-label) ": "; color: var(--muted); font-size: 0.8rem; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
