:root {
  --ink: #171713;
  --muted: #706c61;
  --paper: #f7f4ec;
  --surface: rgba(255, 255, 255, 0.94);
  --line: #dcd5c5;
  --gold: #b7832d;
  --gold-dark: #805a1e;
  --green: #1e7a47;
  --red: #a13a32;
  --shadow: 0 18px 50px rgba(47, 38, 22, 0.11);
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--paper); }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 0%, rgba(183, 131, 45, 0.13), transparent 31rem),
    linear-gradient(180deg, #fbfaf6 0%, var(--paper) 100%);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.48; }

.topbar {
  position: sticky;
  z-index: 20;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 1rem;
  min-height: 54px;
  padding: 0.5rem max(0.75rem, calc((100vw - 520px) / 2));
  border-bottom: 1px solid rgba(51, 44, 32, 0.13);
  background: rgba(251, 250, 246, 0.9);
  backdrop-filter: blur(18px);
}

.topbar .brand,
#secureBadge,
#environmentBadge,
#operatorBadge { display: none !important; }
.brand { display: inline-flex; align-items: center; gap: 0.7rem; color: inherit; text-decoration: none; }
.brand-mark {
  display: grid; place-items: center; width: 42px; height: 42px; border-radius: 50%;
  color: white; background: var(--ink); font-weight: 900; letter-spacing: -0.08em;
  box-shadow: inset 0 0 0 3px var(--gold);
}
.brand strong { display: block; font-size: 0.93rem; letter-spacing: 0.12em; }
.brand small { display: block; margin-top: 0.1rem; color: var(--muted); font-size: 0.68rem; }
.topbar-status { display: flex; justify-content: flex-end; }
.topbar-button { min-height: 36px; padding: 0.48rem 0.82rem; border: 1px solid var(--line); border-radius: 10px; color: var(--ink); background: #fff; font-size: 0.64rem; font-weight: 850; letter-spacing: 0.06em; }
.topbar-button:hover { border-color: var(--gold); color: var(--gold-dark); background: #fffaf0; }

.page-shell { width: min(520px, calc(100% - 1.25rem)); margin: 1.35rem auto 3rem; }
.intro-card, .setup-card, .scanner-card, .history-card, .queue-card {
  border: 1px solid var(--line); border-radius: 22px; background: var(--surface); box-shadow: var(--shadow);
}
.intro-card { display: flex; align-items: center; justify-content: space-between; gap: 2rem; padding: 1.6rem; }
.auth-shell { min-height: calc(100vh - 175px); display: grid; place-items: center; padding: 2rem 0; }
.auth-card { width: min(440px, 100%); padding: clamp(1.25rem, 4vw, 2.2rem); border: 1px solid var(--line); border-radius: 22px; background: var(--surface); box-shadow: var(--shadow); }
.auth-card h1 { font-size: clamp(1.8rem, 5vw, 2.5rem); }
.auth-card > p:not(.eyebrow):not(.auth-message) { color: var(--muted); line-height: 1.6; }
.auth-card form { display: grid; gap: 1rem; margin-top: 1.4rem; }
.auth-message { min-height: 1.25rem; margin: 0.85rem 0 0; color: var(--red); font-size: 0.76rem; text-align: center; }
[hidden] { display: none !important; }
h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0.5rem; font-family: Georgia, serif; font-size: clamp(1.8rem, 4vw, 3.25rem); line-height: 1.04; }
h2 { margin-bottom: 0; font-family: Georgia, serif; font-size: clamp(1.15rem, 2.4vw, 1.55rem); }
.intro-card p:not(.eyebrow) { max-width: 690px; margin-bottom: 0; color: var(--muted); line-height: 1.65; }
.eyebrow { margin-bottom: 0.4rem; color: var(--gold-dark); font-size: 0.7rem; font-weight: 850; letter-spacing: 0.16em; }

.connection-card { display: flex; align-items: center; gap: 0.75rem; min-width: 260px; padding: 0.85rem 1rem; border-radius: 14px; background: #f2ede1; }
.connection-card span:last-child { display: grid; gap: 0.18rem; }
.connection-card strong { font-size: 0.78rem; }
.connection-card small { color: var(--muted); font-size: 0.67rem; }
.connection-dot { width: 10px; height: 10px; flex: 0 0 auto; border-radius: 50%; background: var(--gold); box-shadow: 0 0 0 5px rgba(183, 131, 45, 0.15); }

.scanner-page { display: grid; gap: 1rem; }
.setup-card { padding: 1rem; }
.scanner-context { padding: 0.15rem 0.2rem; }
.scanner-context h1 { margin-bottom: 0.3rem; font-family: inherit; font-size: 1.08rem; font-weight: 850; letter-spacing: 0.01em; }
.scanner-context > p { margin-bottom: 0.85rem; color: var(--muted); font-size: 0.76rem; }
.section-heading { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; }
.step-badge, .count-badge { border-radius: 999px; background: #eee5d3; color: var(--gold-dark); font-size: 0.68rem; font-weight: 850; letter-spacing: 0.08em; }
.step-badge { padding: 0.5rem 0.7rem; }
.count-badge { display: grid; place-items: center; min-width: 34px; height: 34px; padding: 0 0.6rem; }
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.8rem; }
label > span, .manual-form > label { display: block; margin: 0 0 0.4rem 0.15rem; color: var(--muted); font-size: 0.68rem; font-weight: 760; text-transform: uppercase; letter-spacing: 0.05em; }
select, input {
  width: 100%; min-height: 46px; border: 1px solid var(--line); border-radius: 11px; outline: none; background: #fff; color: var(--ink); padding: 0 0.85rem;
}
select:focus, input:focus { border-color: var(--gold); box-shadow: 0 0 0 3px rgba(183, 131, 45, 0.15); }

.scanner-card, .history-card, .queue-card { padding: 1.15rem; }
.checkpoint-field { margin-bottom: 1.2rem; padding: 0.85rem; border: 1px solid var(--line); border-radius: 14px; background: #fbfaf6; }
.checkpoint-field label { display: block; margin-bottom: 0.4rem; color: var(--muted); font-size: 0.67rem; font-weight: 800; letter-spacing: 0.06em; text-transform: uppercase; }
.checkpoint-field small { display: block; margin-top: 0.42rem; color: var(--muted); font-size: 0.65rem; }
.checkpoint-field.is-locked { border-color: rgba(183, 131, 45, 0.48); text-align: center; background: #fff9ec; }
.checkpoint-field.is-locked select { border: 0; box-shadow: none; color: var(--gold-dark); background: transparent; text-align: center; font-family: Georgia, serif; font-size: 1.2rem; font-weight: 800; }
.camera-stage { position: relative; display: grid; place-items: center; overflow: hidden; width: 100%; height: min(62vw, 390px); min-height: 250px; border-radius: 14px; background: #10110f; isolation: isolate; }
#cameraVideo { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity 180ms ease; }
.camera-stage.is-live #cameraVideo { opacity: 1; }
.camera-placeholder { display: grid; place-items: center; gap: 0.45rem; color: #f2eee4; text-align: center; }
.camera-placeholder span { color: #aca99f; font-size: 0.78rem; }
.camera-stage.is-live .camera-placeholder { display: none; }
.camera-symbol { width: 62px; height: 45px; border: 3px solid var(--gold); border-radius: 12px; position: relative; }
.camera-symbol::before { content: ""; position: absolute; width: 16px; height: 16px; border: 3px solid var(--gold); border-radius: 50%; top: 11px; left: 20px; }
.camera-symbol::after { content: ""; position: absolute; width: 22px; height: 7px; border-radius: 5px 5px 0 0; top: -9px; left: 6px; background: var(--gold); }
.scan-frame { position: absolute; z-index: 2; width: min(66%, 340px); aspect-ratio: 1; opacity: 0; transition: opacity 180ms ease; }
.camera-stage.is-live .scan-frame { opacity: 1; }
.scan-frame i { position: absolute; width: 38px; height: 38px; border-color: #f5bf54; border-style: solid; filter: drop-shadow(0 2px 4px #000); }
.scan-frame i:nth-child(1) { top: 0; left: 0; border-width: 4px 0 0 4px; border-radius: 12px 0 0; }
.scan-frame i:nth-child(2) { top: 0; right: 0; border-width: 4px 4px 0 0; border-radius: 0 12px 0 0; }
.scan-frame i:nth-child(3) { bottom: 0; left: 0; border-width: 0 0 4px 4px; border-radius: 0 0 0 12px; }
.scan-frame i:nth-child(4) { right: 0; bottom: 0; border-width: 0 4px 4px 0; border-radius: 0 0 12px; }
.scan-line { position: absolute; left: 7%; right: 7%; top: 10%; height: 2px; background: #f5bf54; box-shadow: 0 0 14px #f5bf54; animation: scan 2s ease-in-out infinite alternate; }
@keyframes scan { to { top: 88%; } }

.scanner-message { min-height: 1.3rem; margin: 0.9rem 0; color: var(--muted); font-size: 0.78rem; line-height: 1.55; }
.scanner-message.is-error { color: var(--red); }
.scanner-message.is-success { color: var(--green); }
.status-chip { display: inline-flex; align-items: center; min-height: 27px; padding: 0.35rem 0.65rem; border-radius: 999px; font-size: 0.62rem; font-weight: 850; letter-spacing: 0.06em; }
.status-neutral { color: #5e594f; background: #eae6dd; }
.status-warn { color: #845b18; background: #f3e8d1; }
.status-good { color: var(--green); background: #e0f1e6; }
.status-bad { color: var(--red); background: #f8e3df; }

.primary-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 0.6rem; margin-bottom: 0.7rem; }
.live-controls { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.55rem; margin-top: 0.7rem; }
.button { min-height: 44px; border: 1px solid transparent; border-radius: 11px; padding: 0.65rem 0.8rem; font-size: 0.69rem; font-weight: 850; letter-spacing: 0.035em; transition: transform 120ms ease, box-shadow 120ms ease, background 120ms ease; }
.button:not(:disabled):hover { transform: translateY(-1px); }
.button-primary { color: #fff; background: linear-gradient(135deg, var(--gold), var(--gold-dark)); box-shadow: 0 8px 18px rgba(128, 90, 30, 0.2); }
.button-secondary { border-color: var(--line); color: var(--ink); background: #fff; }
.button-wide { width: 100%; }
.fallback-divider { display: flex; align-items: center; gap: 0.7rem; margin: 1.2rem 0; color: #9b9588; font-size: 0.62rem; font-weight: 800; }
.fallback-divider::before, .fallback-divider::after { content: ""; height: 1px; flex: 1; background: var(--line); }
.manual-form { display: grid; gap: 0.7rem; }
.manual-form input { min-height: 58px; text-align: center; font-family: Georgia, serif; font-size: 1.35rem; font-weight: 800; letter-spacing: 0.05em; }
.visually-hidden { position: absolute !important; width: 1px !important; height: 1px !important; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.last-result { padding: 1rem; border: 1px solid #d5bd8e; border-radius: 16px; background: #fff9ec; box-shadow: var(--shadow); }
.last-result span, .last-result small { display: block; color: var(--muted); font-size: 0.66rem; }
.last-result strong { display: block; overflow-wrap: anywhere; margin: 0.35rem 0; color: var(--gold-dark); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 1.08rem; }
.scan-history { display: grid; gap: 0.55rem; max-height: 470px; overflow-y: auto; margin: 0; padding: 0; list-style: none; }
.scan-history li { display: grid; grid-template-columns: auto 1fr; gap: 0.7rem; padding: 0.75rem; border: 1px solid var(--line); border-radius: 12px; background: #fff; }
.scan-history .history-empty { display: block; color: var(--muted); text-align: center; font-size: 0.75rem; }
.history-index { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; color: white; background: var(--ink); font-size: 0.67rem; font-weight: 800; }
.history-value { min-width: 0; }
.history-value strong { display: block; overflow-wrap: anywhere; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.78rem; }
.history-value small { display: block; margin-top: 0.25rem; color: var(--muted); font-size: 0.66rem; }
.text-button { margin-top: 0.9rem; border: 0; background: transparent; color: var(--gold-dark); font-size: 0.7rem; font-weight: 760; }
.queue-card { background: #fff9ec; border-color: rgba(183, 131, 45, 0.38); }
.queue-card p { margin: -0.3rem 0 0.9rem; color: var(--muted); font-size: 0.7rem; line-height: 1.55; }
.compact-heading { margin-bottom: 0.7rem; }
footer { padding: 1.2rem; border-top: 1px solid var(--line); color: var(--muted); text-align: center; font-size: 0.67rem; letter-spacing: 0.06em; }

@media (max-width: 860px) {
  .connection-card { min-width: 0; }
}

@media (max-width: 640px) {
  .topbar { min-height: 52px; padding: 0.45rem 0.75rem; }
  .brand-mark { width: 38px; height: 38px; }
  .brand strong { font-size: 0.78rem; }
  .brand small { font-size: 0.61rem; }
  .topbar-status { flex-wrap: nowrap; max-width: none; }
  .page-shell { width: min(100% - 0.75rem, 520px); margin-top: 0.65rem; }
  .setup-card, .scanner-card, .history-card, .queue-card { border-radius: 17px; padding: 0.95rem; }
  .form-grid { grid-template-columns: 1fr 1fr; gap: 0.55rem; }
  .camera-stage { height: min(74vw, 390px); min-height: 260px; }
  .scanner-heading { align-items: flex-start; }
  .live-controls { grid-template-columns: 1fr 1fr; }
  .live-controls #torchBtn:not([hidden]) { grid-column: 1 / -1; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}
