:root {
  --bg: #0b1220;
  --card: #151f35;
  --text: #e8edf7;
  --muted: #9fb0cf;
  --ok: #22c55e;
  --warn: #eab308;
  --bad: #ef4444;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: linear-gradient(180deg, #0b1220, #0f172a);
  color: var(--text);
  min-height: 100svh;
}

.app {
  max-width: 1024px;
  margin: 0 auto;
  padding: calc(10px + var(--safe-top)) 12px calc(12px + var(--safe-bottom));
}

.hero h1 {
  margin: 0;
  font-size: 1.35rem;
  line-height: 1.2;
}

.hero p {
  margin: 6px 0 12px;
  color: var(--muted);
  font-size: 0.92rem;
}

.controls {
  margin-top: 10px;
  background: rgba(15, 24, 45, 0.75);
  border: 1px solid #2d3c61;
  border-radius: 12px;
  padding: 10px 10px 12px;
  position: sticky;
  bottom: calc(8px + var(--safe-bottom));
  z-index: 20;
  backdrop-filter: blur(6px);
}

.extras {
  margin-top: 10px;
  background: #101a30;
  border: 1px solid #2d3c61;
  border-radius: 12px;
  padding: 10px;
}

.profile-details {
  margin-top: 10px;
}

.profile-details summary,
.diag-details summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 0.88rem;
  margin-bottom: 6px;
}

.profile-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-bottom: 10px;
}

.profile-grid input,
.profile-grid select,
.profile-grid label {
  background: #111a30;
  color: #d9e6ff;
  border: 1px solid #2d3c61;
  border-radius: 8px;
  min-height: 42px;
  padding: 10px;
  font-size: 0.9rem;
}

.profile-grid label {
  display: flex;
  align-items: center;
  gap: 6px;
}

#start-btn {
  width: 100%;
  border: 0;
  border-radius: 12px;
  min-height: 48px;
  padding: 12px 14px;
  font-weight: 600;
  font-size: 0.98rem;
  background: #2563eb;
  color: white;
}

#start-btn:disabled {
  opacity: 0.65;
}

.deep-scan-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 0;
  font-size: 0.88rem;
  color: var(--muted);
}

.deep-scan-toggle input {
  accent-color: #22c55e;
}

#scan-timer {
  margin: 8px 0 0;
  text-align: center;
  font-weight: 700;
  letter-spacing: 0.3px;
}

.distance-meter {
  margin-top: 10px;
}

.distance-track {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  border: 1px solid #2d3c61;
  border-radius: 8px;
  overflow: hidden;
  font-size: 0.76rem;
}

.distance-track .zone {
  text-align: center;
  padding: 6px 4px;
}

.distance-track .zone.far {
  background: rgba(59, 130, 246, 0.2);
}

.distance-track .zone.good {
  background: rgba(34, 197, 94, 0.2);
}

.distance-track .zone.near {
  background: rgba(239, 68, 68, 0.2);
}

.distance-track .needle {
  position: absolute;
  top: -2px;
  bottom: -2px;
  width: 2px;
  background: #f8fafc;
  left: 50%;
  transform: translateX(-50%);
  transition: left 0.15s ease;
}

#distance-label {
  margin: 6px 0 0;
  font-size: 0.82rem;
  color: var(--muted);
}

#status-text {
  margin: 8px 0 0;
  color: #d1def7;
  min-height: 1.3em;
  font-size: 0.9rem;
  font-weight: 500;
  text-align: center;
}

.secondary {
  width: 100%;
  margin-top: 8px;
  border-radius: 12px;
  min-height: 44px;
  border: 1px solid #3b4d78;
  background: #16233f;
  color: #dbe8ff;
}

.diag-panel {
  margin-top: 6px;
  border: 1px solid #2d3c61;
  border-radius: 10px;
  padding: 10px;
  background: #0f182d;
  font-size: 0.84rem;
  color: #c5d4ef;
}

.diag-panel p {
  margin: 4px 0;
}

.panel {
  display: grid;
  gap: 12px;
  margin-top: 12px;
  justify-items: center;
}

.camera-placeholder {
  border: 1px solid #2d3c61;
  border-radius: 12px;
  min-height: 220px;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 16px;
  color: var(--muted);
  background: #0f182d;
}

.camera-shell {
  position: relative;
  border: 1px solid #2d3c61;
  border-radius: 12px;
  overflow: hidden;
  aspect-ratio: var(--camera-ratio, 3 / 4);
  background: #0a0f1d;
  margin-inline: auto;
  width: 100%;
  max-width: 460px;
}

.hidden {
  display: none !important;
}

#video,
#overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

#video {
  object-fit: cover;
  transform: scaleX(-1);
}

#overlay {
  transform: scaleX(-1);
}

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

.metrics.hidden {
  display: none;
}

.metric-card {
  background: var(--card);
  border: 1px solid #2d3c61;
  border-radius: 12px;
  padding: 12px;
}

.metric-card h2 {
  margin: 0 0 6px;
  font-size: 0.92rem;
}

.metric-value {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
}

.metric-range,
.metric-status {
  margin: 4px 0 0;
  font-size: 0.82rem;
  color: var(--muted);
}

.confidence {
  margin-top: 8px;
  height: 6px;
  background: #1f2c49;
  border-radius: 999px;
  overflow: hidden;
}

.confidence span {
  display: block;
  width: 0%;
  height: 100%;
  background: var(--ok);
  transition: width 0.2s linear;
}

.metric-card.normal {
  border-color: var(--ok);
}

.metric-card.borderline {
  border-color: var(--warn);
}

.metric-card.alert {
  border-color: var(--bad);
}

.disclaimer {
  margin-top: 12px;
  padding: 12px;
  border-radius: 10px;
  background: rgba(12, 17, 31, 0.9);
  border: 1px solid #2d3c61;
  font-size: 0.8rem;
  color: #c7d2ea;
}

@media (min-width: 600px) {
  .app {
    padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
  }

  .hero h1 {
    font-size: 1.6rem;
  }

  .profile-grid {
    grid-template-columns: 1fr 1fr;
  }

  .metrics {
    grid-template-columns: 1fr 1fr;
  }

  .camera-shell {
    max-width: 520px;
  }
}

@media (min-width: 900px) {
  .panel {
    grid-template-columns: 1.2fr 1fr;
    align-items: start;
  }

  .metrics {
    grid-template-columns: 1fr;
  }

  .controls {
    position: static;
    backdrop-filter: none;
  }

  .extras {
    margin-top: 12px;
  }
}
