:root {
  --bg: #0b0b0f;
  --bg-elev: #16171d;
  --bg-elev-2: #1f2028;
  --text: #ffffff;
  --text-muted: #a0a3ad;
  --accent: #ff6b35;
  --accent-dim: #c44a1c;
  --green: #34c759;
  --yellow: #ffcc00;
  --red: #ff3b30;
  --border: #2a2b36;
  --radius: 14px;
  --radius-sm: 10px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
  font-size: 16px; line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: contain;
}

body {
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

button, input, textarea { font: inherit; color: inherit; }

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

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 10px;
  padding: 14px 16px; background: var(--bg); border-bottom: 1px solid var(--border);
}
.topbar .title { font-weight: 600; font-size: 18px; flex: 1; }
.topbar .sub { color: var(--text-muted); font-size: 13px; font-weight: 400; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: none;
  background: var(--bg-elev); color: var(--text);
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.icon-btn:active { transform: scale(0.96); }
.icon-btn.back-btn {
  width: auto; min-width: 40px; height: 40px; padding: 0 14px;
  border-radius: 20px; font-size: 15px; font-weight: 500; gap: 2px;
}

/* Content padding */
.content { padding: 16px; padding-bottom: 100px; }

/* Login */
.login-wrap {
  min-height: 100vh;
  display: flex; flex-direction: column; justify-content: center;
  padding: 32px;
  background: radial-gradient(ellipse at top, #23180f 0%, #0b0b0f 60%);
}
.login-brand {
  font-size: 42px; font-weight: 700; letter-spacing: -0.03em;
  margin-bottom: 8px; color: var(--accent);
}
.login-sub { color: var(--text-muted); margin-bottom: 40px; font-size: 15px; }
.form-group { margin-bottom: 16px; }
.form-label { display: block; font-size: 13px; color: var(--text-muted); margin-bottom: 6px; }
.form-input {
  width: 100%; padding: 14px 16px; font-size: 16px;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius-sm); color: var(--text);
}
.form-input:focus { outline: none; border-color: var(--accent); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 14px 20px; font-size: 16px; font-weight: 600;
  background: var(--accent); color: white; border: none;
  border-radius: var(--radius-sm); cursor: pointer;
  min-height: 48px; width: 100%;
  transition: transform 0.1s;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-secondary { background: var(--bg-elev-2); color: var(--text); }
.btn-success { background: var(--green); }
.btn-danger { background: var(--red); }
.btn-warn { background: var(--yellow); color: #000; }
.btn-ghost { background: transparent; border: 1px solid var(--border); color: var(--text); }

.err { color: var(--red); font-size: 13px; margin-top: 8px; min-height: 18px; }

/* Cards (buildings list) */
.card {
  background: var(--bg-elev); border-radius: var(--radius);
  padding: 18px; margin-bottom: 12px;
  border: 1px solid var(--border);
}
.card.pressable:active { transform: scale(0.98); }
.card-title { font-weight: 600; font-size: 17px; margin-bottom: 4px; }
.card-sub { color: var(--text-muted); font-size: 13px; margin-bottom: 10px; }
.card-stats { display: flex; gap: 10px; font-size: 12px; color: var(--text-muted); }
.pill {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 500;
  background: var(--bg-elev-2);
}
.pill.flagged { background: rgba(255, 59, 48, 0.15); color: var(--red); }
.pill.ok { background: rgba(52, 199, 89, 0.15); color: var(--green); }
.pill.open { background: rgba(255, 204, 0, 0.15); color: var(--yellow); }

/* Floating Action Button */
.fab {
  position: fixed; right: 20px; bottom: calc(20px + env(safe-area-inset-bottom));
  width: 60px; height: 60px; border-radius: 50%;
  background: var(--accent); color: white; border: none;
  box-shadow: 0 6px 20px rgba(255, 107, 53, 0.4);
  font-size: 28px; font-weight: 300;
  z-index: 20;
}
.fab:active { transform: scale(0.95); }

/* Tabs */
.tabs { display: flex; gap: 4px; padding: 4px; background: var(--bg-elev); border-radius: var(--radius); margin-bottom: 16px; }
.tab { flex: 1; padding: 10px; text-align: center; border-radius: var(--radius-sm); color: var(--text-muted); font-weight: 500; font-size: 14px; cursor: pointer; }
.tab.active { background: var(--bg-elev-2); color: var(--text); }

/* Scan list rows */
.scan-row {
  display: flex; align-items: center; gap: 12px;
  padding: 12px; background: var(--bg-elev); border-radius: var(--radius-sm);
  margin-bottom: 8px; border: 1px solid var(--border);
}
.scan-thumb { width: 52px; height: 52px; border-radius: 8px; background: var(--bg-elev-2); object-fit: cover; flex-shrink: 0; }
.scan-info { flex: 1; min-width: 0; }
.scan-info .line1 { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.scan-info .line2 { color: var(--text-muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status-dot { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; }
.status-dot.green { background: var(--green); }
.status-dot.yellow { background: var(--yellow); animation: pulse 1.4s infinite; }
.status-dot.red { background: var(--red); }
.status-dot.gray { background: var(--text-muted); }
.status-dot.orange { background: var(--accent); animation: pulse 1.4s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }

/* Camera — iPhone 16 Pro PWA in Safari. Uses 100dvh to exclude the dynamic
   browser chrome and avoid the shutter getting buried under the home bar. */
.camera-screen {
  position: fixed; inset: 0;
  width: 100vw; height: 100vh; height: 100dvh;
  background: #000;
  z-index: 30;
  overflow: hidden;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
}
.camera-video {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
  background: #000;
  z-index: 1;
  pointer-events: none;
}
/* Top + bottom scrims for control legibility over bright scenes */
.camera-screen::before,
.camera-screen::after {
  content: ''; position: fixed; left: 0; right: 0;
  pointer-events: none; z-index: 32;
}
.camera-screen::before {
  top: 0;
  height: calc(env(safe-area-inset-top) + 76px);
  background: linear-gradient(to bottom, rgba(0,0,0,0.55), rgba(0,0,0,0));
}
.camera-screen::after {
  bottom: 0;
  height: calc(env(safe-area-inset-bottom) + 260px);
  background: linear-gradient(to top, rgba(0,0,0,0.6), rgba(0,0,0,0));
}
.camera-guide {
  position: fixed;
  top: env(safe-area-inset-top);
  right: 0;
  bottom: env(safe-area-inset-bottom);
  left: 0;
  pointer-events: none;
  z-index: 33;
}
/* Two labeled zones. Positioned as % of the inner guide area so safe-area
   insets are respected. Zones themselves accept pointer events for tap-to-focus;
   labels do not, so they never block a tap. */
.guide-zone {
  position: absolute;
  left: 50%; transform: translateX(-50%);
  width: 85%; max-width: 560px;
  border: 3px solid transparent;
  border-radius: 16px;
  pointer-events: auto;
  cursor: crosshair;
  -webkit-tap-highlight-color: transparent;
  transition: border-color 0.15s, background-color 0.15s;
}
.guide-zone.top {
  top: 15%; height: 35%;
  border-color: #34C759;
  background: rgba(52, 199, 89, 0.08);
}
.guide-zone.bottom {
  top: 52%; height: 30%;
  border-color: #FF6B35;
  background: rgba(255, 107, 53, 0.08);
}
.guide-label {
  position: absolute;
  top: -14px;
  left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,0.6);
  color: #fff;
  font-size: 14px; font-weight: 600;
  letter-spacing: 0.5px;
  padding: 6px 12px;
  border-radius: 12px;
  white-space: nowrap;
  pointer-events: none;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  box-shadow: 0 2px 8px rgba(0,0,0,0.4);
}

/* Tap-to-focus indicator — brief 300ms animation on tap */
.focus-indicator {
  position: fixed;
  width: 40px; height: 40px;
  margin-left: -20px; margin-top: -20px;
  border: 2px solid #fff;
  border-radius: 4px;
  pointer-events: none;
  z-index: 50;
  box-shadow: 0 0 0 1px rgba(0,0,0,0.45);
  animation: focus-pulse 300ms ease-out forwards;
}
@keyframes focus-pulse {
  0%   { opacity: 0; transform: scale(1.6); }
  30%  { opacity: 1; transform: scale(1.1); }
  100% { opacity: 0; transform: scale(1); }
}

/* Portrait orientation lock — full-screen prompt in landscape */
.orientation-lock {
  display: none;
  position: fixed; inset: 0;
  background: #000; color: #fff;
  z-index: 99999;
  flex-direction: column;
  align-items: center; justify-content: center;
  text-align: center; padding: 20px;
}
.orientation-lock .icon {
  font-size: 72px; margin-bottom: 20px;
  animation: rotate-hint 2s ease-in-out infinite;
}
.orientation-lock .txt {
  font-size: 18px; font-weight: 600; letter-spacing: 0.3px;
}
@keyframes rotate-hint {
  0%, 100% { transform: rotate(0deg); }
  50%      { transform: rotate(-90deg); }
}
/* Rotate-to-portrait prompt is only for touch devices (phones/tablets).
   On desktop — which is almost always landscape with a mouse — the prompt
   would obscure the camera guide, so scope to coarse pointers only. */
@media (orientation: landscape) and (pointer: coarse) {
  .camera-screen .orientation-lock { display: flex; }
}
.camera-counter {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 8px);
  left: 16px;
  background: rgba(0,0,0,0.5);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  backdrop-filter: saturate(180%) blur(14px);
  padding: 10px 16px; border-radius: 999px;
  font-weight: 700; font-size: 16px; color: #fff;
  font-variant-numeric: tabular-nums;
  min-width: 52px; text-align: center;
  box-shadow: 0 2px 10px rgba(0,0,0,0.35);
  z-index: 40;
}
.camera-btn {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 6px);
  width: 46px; height: 46px; border-radius: 50%;
  background: rgba(0,0,0,0.5);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  backdrop-filter: saturate(180%) blur(14px);
  border: none; color: #fff; font-size: 20px;
  display: inline-flex; align-items: center; justify-content: center;
  transition: transform 0.12s cubic-bezier(.2,.9,.3,1), background-color 0.15s;
  box-shadow: 0 2px 10px rgba(0,0,0,0.35);
  z-index: 9998;
  cursor: pointer;
  -webkit-user-select: none; user-select: none;
}
.camera-btn:active { transform: scale(0.92); background: rgba(0,0,0,0.7); }
.cam-btn-close { right: 16px; }
.cam-btn-report { right: 72px; }
/* Torch sits directly below the close button, same right-edge, same style */
.cam-btn-torch {
  right: 16px;
  top: calc(env(safe-area-inset-top) + 6px + 46px + 10px);
}
.cam-btn-torch.on {
  background: #ffcc00;
  color: #1a1a1f;
  box-shadow: 0 2px 12px rgba(255, 204, 0, 0.55);
}
.cam-btn-torch[hidden] { display: none !important; }

/* Recent uploads tray — sits just above the capture button */
.camera-tray {
  position: fixed;
  left: 16px; right: 16px;
  bottom: calc(env(safe-area-inset-bottom) + 24px + 90px + 14px);
  display: flex; gap: 10px; justify-content: center;
  z-index: 9998;
  pointer-events: none;
}
.tray-thumb {
  width: 60px; height: 60px; border-radius: 12px;
  background: #1a1a1f center/cover no-repeat;
  border: 2px solid rgba(255,255,255,0.32);
  position: relative;
  pointer-events: auto;
  box-shadow: 0 3px 12px rgba(0,0,0,0.55);
  transition: transform 0.12s cubic-bezier(.2,.9,.3,1);
}
.tray-thumb:active { transform: scale(0.94); }
.tray-thumb .dot {
  position: absolute; right: -3px; top: -3px;
  width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid #000;
}

/* Capture button — 90px, white outer ring, gap, white inner circle.
   Positioned fixed relative to viewport + safe-area so it is never
   buried under Safari bottom chrome on iPhone 16 Pro. */
.capture-btn,
button.capture-btn {
  position: fixed !important;
  bottom: calc(env(safe-area-inset-bottom) + 24px);
  left: 50%;
  transform: translateX(-50%);
  width: 90px; height: 90px;
  border-radius: 50%;
  background: transparent;
  border: 4px solid #fff;
  padding: 0;
  cursor: pointer;
  z-index: 9999;
  box-shadow: 0 0 0 2px rgba(0,0,0,0.55), 0 8px 26px rgba(0,0,0,0.6);
  -webkit-user-select: none; user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  pointer-events: auto;
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.1s ease;
}
.capture-btn-inner {
  display: block;
  width: calc(100% - 14px); /* 4px border + ~3px gap on each side */
  height: calc(100% - 14px);
  border-radius: 50%;
  background: #fff;
  transition: background 0.15s, transform 0.1s;
}
.capture-btn:active { transform: translateX(-50%) scale(0.94); }
.capture-btn:active .capture-btn-inner { background: #e5e5e5; }
.capture-btn.busy .capture-btn-inner { background: var(--yellow); animation: pulse 1s infinite; }

.flash-overlay {
  position: fixed; inset: 0; background: #fff; opacity: 0;
  pointer-events: none; transition: opacity 0.08s linear;
  z-index: 9997;
}
.flash-overlay.on { opacity: 0.75; }

/* Review screen */
.review-photo { width: 100%; max-height: 50vh; object-fit: contain; background: black; border-radius: var(--radius); }
.kv-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 16px 0; }
.kv-cell { background: var(--bg-elev); padding: 10px 12px; border-radius: var(--radius-sm); border: 1px solid var(--border); }
.kv-cell .k { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 2px; }
.kv-cell .v { font-weight: 600; font-size: 15px; word-break: break-all; }
.kv-cell.mismatch { border-color: var(--red); background: rgba(255,59,48,0.08); }
.kv-cell.match { border-color: var(--green); }
.action-row { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.action-row .btn { flex: 1 1 calc(50% - 4px); min-width: 0; }

/* Report screen */
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 20px; }
.stat-tile {
  background: var(--bg-elev); padding: 16px; border-radius: var(--radius);
  text-align: center; border: 1px solid var(--border);
}
.stat-tile .num { font-size: 28px; font-weight: 700; line-height: 1; margin-bottom: 4px; }
.stat-tile .lbl { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.stat-tile.ok .num { color: var(--green); }
.stat-tile.warn .num { color: var(--yellow); }
.stat-tile.err .num { color: var(--red); }

/* Empty states */
.empty { text-align: center; padding: 48px 16px; color: var(--text-muted); }
.empty .icon { font-size: 48px; margin-bottom: 16px; opacity: 0.4; }

/* Toast */
.toast {
  position: fixed; bottom: calc(20px + env(safe-area-inset-bottom));
  left: 50%; transform: translateX(-50%);
  background: var(--bg-elev-2); color: var(--text);
  padding: 12px 20px; border-radius: 999px;
  font-size: 14px; font-weight: 500;
  border: 1px solid var(--border);
  z-index: 100; opacity: 0; transition: opacity 0.2s;
  pointer-events: none;
}
.toast.show { opacity: 1; }

/* Modal */
.modal-bg {
  position: fixed; inset: 0; background: rgba(0,0,0,0.7);
  display: none; align-items: flex-end; justify-content: center;
  z-index: 40;
}
.modal-bg.show { display: flex; }
.modal {
  width: 100%; max-width: 500px;
  background: var(--bg-elev); border-radius: var(--radius) var(--radius) 0 0;
  padding: 20px; padding-bottom: calc(20px + env(safe-area-inset-bottom));
  max-height: 80vh; overflow-y: auto;
}
.modal-title { font-size: 18px; font-weight: 600; margin-bottom: 12px; }

/* Loading spinner */
.spinner {
  width: 32px; height: 32px; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
  margin: 20px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }
