:root {
  color-scheme: light;
  --bg: #f7f7f5;
  --ink: #151219;
  --muted: #646970;
  --line: #e1e1dd;
  --panel: #ffffff;
  --stage: #111214;
  --red: #d71920;
  --red-dark: #b51218;
  --red-soft: #fde9ea;
  --green: #13785e;
  --green-soft: #e1f4ed;
  --amber: #a36308;
  --amber-soft: #fff0d4;
  --top-bar-h: 68px;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

body {
  background: var(--bg);
  color: var(--ink);
}

button, input { font: inherit; }
input { -webkit-user-select: text; user-select: text; }

/* ── ACCESS SCREEN ── */
.access-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: var(--bg);
  z-index: 10;
}

.access-screen[hidden] { display: none; }

.access-card {
  width: min(100%, 400px);
  display: flex;
  flex-direction: column;
  gap: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(18,18,18,0.10);
  overflow: hidden;
}

.access-logo-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 22px 24px 18px;
  background: var(--bg);
}

.access-logo {
  width: 160px;
  height: 52px;
  object-fit: contain;
}

.access-divider {
  height: 1px;
  background: var(--line);
}

.access-heading {
  padding: 20px 24px 0;
  display: grid;
  gap: 4px;
}

.access-heading h1 { font-size: 22px; }

.access-fields {
  display: grid;
  gap: 12px;
  padding: 16px 24px 24px;
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

input {
  width: 100%;
  min-height: 44px;
  border-radius: 8px;
  border: 1px solid var(--line);
  padding: 0 12px;
  outline: none;
  background: #fafafa;
  color: var(--ink);
  font-size: 15px;
}

input:focus {
  border-color: var(--red);
  box-shadow: 0 0 0 3px var(--red-soft);
  background: #fff;
}

.access-status {
  min-height: 18px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.4;
  margin-top: -2px;
  padding: 0 2px;
  transition: all 0.15s ease;
}

.access-status.bad {
  display: flex;
  align-items: center;
  gap: 7px;
  color: #fff;
  background: var(--red);
  font-size: 13px;
  font-weight: 800;
  padding: 10px 12px;
  border-radius: 7px;
  margin-top: 0;
  min-height: unset;
}

.access-status.bad::before {
  content: "⚠";
  font-size: 14px;
  flex-shrink: 0;
}

.access-fields.has-error input.invalid {
  border-color: var(--red);
  background: var(--red-soft);
  box-shadow: 0 0 0 3px rgba(215, 25, 32, 0.12);
}

/* ── APP SHELL ── */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.app-shell[hidden] { display: none; }

/* ── TOP BAR ── */
.top-bar {
  height: var(--top-bar-h);
  flex: 0 0 var(--top-bar-h);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.bar-side { display: flex; align-items: center; }
.bar-right { justify-content: flex-end; }

.brand-lockup {
  display: flex;
  align-items: center;
  justify-content: center;
}

.brand-logo {
  height: 48px;
  width: auto;
  max-width: 220px;
  object-fit: contain;
}

/* ── APP BODY ── */
.app-body {
  flex: 1 1 0;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  overflow: hidden;
}

body.debug-mode .app-body {
  grid-template-columns: minmax(0, 1fr) 320px;
}

/* ── CAMERA PANE ── */
.camera-pane {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 10px clamp(10px, 1.6vw, 20px);
  min-height: 0;
  overflow: hidden;
}

.screen-meta { flex: 0 0 auto; }

.screen-heading { display: grid; gap: 2px; }

.eyebrow {
  color: var(--red);
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

h1 { font-size: clamp(22px, 3.2vw, 36px); line-height: 1.08; }
h2 { font-size: 14px; }

/* ── STAGE ── */
.stage {
  position: relative;
  flex: 1 1 0;
  min-height: 0;
  border-radius: 8px;
  background: var(--stage);
  border: 1px solid #26272a;
  overflow: hidden;
}

#camera, #overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  transform: scaleX(-1);
}

#camera { object-fit: contain; }
#overlay { pointer-events: none; }

body.mobile-fit #camera { object-fit: cover; }

.timer-ring {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 68px;
  height: 68px;
  border-radius: 50%;
  display: none;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 950;
  font-size: 18px;
  background:
    radial-gradient(circle at center, rgba(0,0,0,0.68) 56%, transparent 58%),
    conic-gradient(var(--red) var(--progress, 100%), rgba(255,255,255,0.24) 0);
  border: 1px solid rgba(255,255,255,0.38);
}

.timer-ring.active { display: flex; }

.processing-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 10px;
  padding: 20px;
  color: #fff;
  text-align: center;
  background: rgba(12,13,15,0.72);
}

.processing-overlay[hidden] { display: none; }

.processing-spinner {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 4px solid rgba(255,255,255,0.24);
  border-top-color: #fff;
  animation: spin 0.9s linear infinite;
}

.processing-overlay p {
  font-size: clamp(18px, 2.6vw, 28px);
  font-weight: 950;
}

.processing-overlay span {
  color: rgba(255,255,255,0.78);
  font-size: 14px;
  font-weight: 800;
}

@keyframes spin { to { transform: rotate(360deg); } }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%       { transform: translateX(-6px); }
  40%       { transform: translateX(6px); }
  60%       { transform: translateX(-4px); }
  80%       { transform: translateX(4px); }
}

.access-card.shake {
  animation: shake 0.35s ease;
}

/* ── ACTION BAR ── */
.action-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.primary-status {
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: clamp(14px, 1.8vw, 18px);
  font-weight: 900;
  min-width: 0;
}

.dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--amber);
  box-shadow: 0 0 0 4px var(--amber-soft);
  flex: 0 0 auto;
}

.dot.ready { background: var(--green); box-shadow: 0 0 0 4px var(--green-soft); }
.dot.bad   { background: var(--red);   box-shadow: 0 0 0 4px var(--red-soft); }

/* ── BUTTONS ── */
.primary-button,
.ghost-button {
  border: 0;
  border-radius: 8px;
  min-height: 44px;
  padding: 0 20px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  flex: 0 0 auto;
}

.primary-button {
  background: var(--red);
  color: #fff;
}

.primary-button:hover:not(:disabled) { background: var(--red-dark); }
.primary-button:disabled { cursor: not-allowed; background: #b8b8b4; }

.ghost-button {
  background: #efefec;
  color: var(--ink);
}

/* ── CONNECTION BADGE ── */
.connection, .badge {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 4px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg);
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

/* ── SIDE PANE (DEBUG) ── */
.side-pane {
  border-left: 1px solid var(--line);
  background: #fbfbfa;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
}

.side-pane[hidden] { display: none; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}

.metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.metrics div {
  min-height: 60px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px;
  background: #fafbf9;
}

dt {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  margin-bottom: 4px;
}

dd {
  font-size: 20px;
  font-weight: 950;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  min-width: 46px;
  min-height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: #f6f7f5;
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
}

.chip.hit {
  color: #0f5d4b;
  border-color: #9dd9c6;
  background: var(--green-soft);
}

.panel-copy, .dialog-status {
  color: var(--muted);
  line-height: 1.45;
  font-size: 13px;
}

.dialog-status.bad {
  color: var(--red);
  font-weight: 800;
}

.errors {
  margin: 10px 0 0;
  padding-left: 16px;
  color: var(--red);
  font-weight: 700;
  font-size: 13px;
}

.setup-hint {
  flex: 0 0 auto;
  padding: 0 2px 2px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.35;
}

.instruction-list {
  margin: 0;
  padding-left: 18px;
  display: grid;
  gap: 4px;
  color: var(--ink);
  font-size: 14px;
  line-height: 1.1;
}

#instructionsDialog {
  width: min(520px, calc(100vw - 28px));
}

.instructions-dialog {
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-height: calc(100dvh - 32px);
}

.instructions-dialog h2 {
  font-size: 20px;
}

.instructions-video-wrap {
  flex: 0 0 auto;
  border-radius: 10px;
  overflow: hidden;
  background: var(--stage);
  border: 1px solid var(--line);
}

.instructions-video {
  display: block;
  width: 100%;
  max-height: 38dvh;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  background: #000;
}

.instructions-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  display: grid;
  gap: 6px;
}

.instructions-dialog .dialog-actions {
  flex: 0 0 auto;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
}

.instructions-dialog .dialog-actions .primary-button,
.instructions-dialog .dialog-actions .ghost-button {
  width: auto;
  flex: 1 1 0;
  min-width: 0;
}

.thank-you-toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  z-index: 30;
  width: min(360px, calc(100vw - 32px));
  padding: 16px 18px;
  border-radius: 12px;
  border: 1px solid #9dd9c6;
  background: var(--green-soft);
  color: var(--ink);
  box-shadow: 0 16px 40px rgba(18, 18, 18, 0.18);
  transform: translateX(-50%) translateY(140%);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.28s ease, opacity 0.28s ease;
}

.thank-you-toast.visible {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

.thank-you-toast__title {
  font-size: 16px;
  font-weight: 900;
  color: var(--green);
  margin-bottom: 4px;
}

.thank-you-toast__message {
  font-size: 14px;
  line-height: 1.4;
  color: var(--ink);
  font-weight: 600;
}

/* ── DIALOG ── */
dialog {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
  width: min(420px, calc(100vw - 28px));
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  border: 0;
  border-radius: 10px;
  padding: 0;
  box-shadow: 0 24px 80px rgba(0,0,0,0.24);
}

dialog::backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.dialog-body {
  display: grid;
  gap: 14px;
  padding: 22px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

/* ── MOBILE ── */
@media (max-width: 600px) {
  :root { --top-bar-h: 52px; }

  .access-screen { padding: 12px; align-items: center; }

  .access-card { width: 100%; max-width: none; border-radius: 10px; }

  .access-logo-wrap { padding: 20px 20px 16px; }

  .access-logo { width: 140px; height: 46px; }

  .access-heading { padding: 16px 20px 0; }

  .access-heading h1 { font-size: 20px; }

  .access-fields { padding: 14px 20px 20px; }

  .top-bar { padding: 0 12px; }

  .brand-logo { height: 32px; max-width: 160px; }

  .connection { font-size: 11px; padding: 3px 8px; min-height: 24px; }

  .camera-pane {
    padding: 8px 8px 10px;
    gap: 6px;
  }

  .screen-meta { padding: 0 2px; }

  .screen-heading h1,
  h1 { font-size: 20px; }

  .eyebrow { font-size: 10px; }

  .stage {
    flex: 1 1 auto;
    min-height: 52vh;
    border-radius: 6px;
    border-width: 0;
  }

  .action-bar {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 0 2px;
  }

  .primary-status { font-size: 14px; justify-content: center; }

  .primary-button,
  .ghost-button { width: 100%; }

  .dialog-actions { flex-direction: column-reverse; }
  .instructions-dialog .dialog-actions { flex-direction: row; }

  dialog {
    width: min(420px, calc(100vw - 24px));
    max-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 24px);
  }

  .dialog-body { padding: 20px; }

  body.debug-mode .app-body { grid-template-columns: 1fr; }
  .side-pane { border-left: 0; border-top: 1px solid var(--line); }
}

@media (max-width: 900px) {
  body.debug-mode .app-body { grid-template-columns: 1fr; }
  .side-pane { border-left: 0; border-top: 1px solid var(--line); }
}