:root {
  color-scheme: light dark;
  --bg: #f3f4f6;
  --card: rgba(255,255,255,.92);
  --text: #111827;
  --muted: #6b7280;
  --line: #d1d5db;
  --accent: #2563eb;
  --accent-pressed: #1d4ed8;
  --shadow: 0 18px 45px rgba(17,24,39,.12);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at 10% 0%, rgba(37,99,235,.18), transparent 35%),
    linear-gradient(180deg, #eef2ff 0%, var(--bg) 42%);
  color: var(--text);
}

button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

.app-shell {
  width: min(760px, 100%);
  margin: 0 auto;
  padding: max(18px, env(safe-area-inset-top)) 16px max(28px, env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: 2rem; letter-spacing: -.04em; }
h2 { margin-bottom: 10px; letter-spacing: -.025em; }
h3 { margin-top: 28px; }

.eyebrow {
  margin-bottom: 4px;
  font-size: .72rem;
  letter-spacing: .14em;
  font-weight: 800;
  color: var(--muted);
}

.card {
  background: var(--card);
  border: 1px solid rgba(255,255,255,.7);
  border-radius: 24px;
  box-shadow: var(--shadow);
  padding: 20px;
  backdrop-filter: blur(18px);
}

.hidden { display: none !important; }
.intro-card p { line-height: 1.5; color: var(--muted); }

.button-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 18px;
}

button, .file-button {
  min-height: 48px;
  border-radius: 14px;
  border: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 750;
  cursor: pointer;
}

.primary { background: var(--accent); color: white; }
.primary:active { background: var(--accent-pressed); }
.primary:disabled { opacity: .45; cursor: default; }
.secondary { background: #e5e7eb; color: #111827; }
.compact { min-height: 40px; padding: 0 14px; }
.icon-button {
  width: 42px;
  height: 42px;
  min-height: 42px;
  border-radius: 50%;
  background: rgba(255,255,255,.78);
  color: #111827;
  box-shadow: 0 8px 20px rgba(17,24,39,.08);
}
.file-button input { display: none; }
.text-link { display: inline-block; margin-top: 16px; color: var(--accent); font-weight: 700; text-decoration: none; }

.media-frame, .canvas-wrap {
  position: relative;
  overflow: hidden;
  border-radius: 18px;
  background: #111827;
  min-height: 280px;
}
video { width: 100%; min-height: 280px; display: block; object-fit: cover; }
canvas { width: 100%; height: auto; display: block; touch-action: manipulation; }

.tap-hint {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  background: rgba(17,24,39,.78);
  color: white;
  padding: 8px 12px;
  border-radius: 999px;
  font-size: .85rem;
  pointer-events: none;
  transition: opacity .2s ease;
}

.step-header { display: flex; gap: 12px; align-items: flex-start; justify-content: space-between; }

.sample-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 14px;
}
.sample-chip {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 70px;
  border: 2px solid transparent;
  background: #f3f4f6;
  color: #111827;
  font-size: .78rem;
}
.sample-chip.active { border-color: var(--accent); background: #eff6ff; }
.sample-dot { width: 22px; height: 22px; border-radius: 50%; border: 1px solid #9ca3af; background: transparent; }

.fine-print { color: var(--muted); font-size: .82rem; line-height: 1.45; margin: 14px 0 0; }
.warning { margin-top: 18px; }
.muted { color: var(--muted); }

.result-hero { display: flex; align-items: center; gap: 18px; }
.result-swatch { width: 105px; height: 105px; flex: 0 0 auto; border-radius: 22px; border: 1px solid rgba(17,24,39,.12); box-shadow: inset 0 0 0 1px rgba(255,255,255,.3); }
.result-hero h2 { font-size: 2rem; margin-bottom: 4px; }

.confidence-row { display: flex; justify-content: space-between; margin-top: 24px; }
.confidence-track { height: 9px; border-radius: 999px; overflow: hidden; background: #e5e7eb; margin-top: 8px; }
#confidenceBar { height: 100%; width: 0; background: var(--accent); transition: width .35s ease; }

.metrics-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 18px; }
.metrics-grid div { background: #f3f4f6; border-radius: 14px; padding: 12px; }
.metrics-grid span { display: block; color: var(--muted); font-size: .75rem; }
.metrics-grid strong { display: block; margin-top: 4px; font-size: .92rem; }

.matches-list { display: grid; gap: 10px; }
.match-row { display: grid; grid-template-columns: 46px 1fr auto; align-items: center; gap: 12px; padding: 10px; border-radius: 14px; background: #f3f4f6; }
.match-swatch { width: 46px; height: 46px; border-radius: 12px; border: 1px solid rgba(17,24,39,.12); }
.match-name { font-weight: 750; }
.match-meta { font-size: .78rem; color: var(--muted); margin-top: 2px; }
.match-distance { font-variant-numeric: tabular-nums; color: var(--muted); font-size: .78rem; }

dialog { border: 0; padding: 0; border-radius: 24px; width: min(560px, calc(100% - 28px)); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(17,24,39,.52); backdrop-filter: blur(4px); }
.dialog-card { padding: 22px; background: white; color: #111827; }
.dialog-card ol { padding-left: 22px; line-height: 1.55; }

@media (prefers-color-scheme: dark) {
  :root { --bg: #090f1c; --card: rgba(17,24,39,.9); --text: #f9fafb; --muted: #9ca3af; --line: #374151; }
  body { background: radial-gradient(circle at 15% 0%, rgba(37,99,235,.22), transparent 34%), #090f1c; }
  .secondary, .sample-chip, .metrics-grid div, .match-row { background: #1f2937; color: #f9fafb; }
  .sample-chip.active { background: #172554; }
  .icon-button { background: rgba(31,41,55,.9); color: #f9fafb; }
  .confidence-track { background: #374151; }
}

@media (max-width: 430px) {
  body { min-height: 100dvh; }

  .app-shell {
    padding: max(8px, env(safe-area-inset-top)) 10px max(10px, env(safe-area-inset-bottom));
  }

  .topbar { margin-bottom: 8px; }
  .topbar .eyebrow { display: none; }
  h1 { font-size: 1.55rem; }
  h2 { margin-bottom: 6px; font-size: 1.15rem; }
  h3 { margin: 14px 0 8px; font-size: 1rem; }

  .card { padding: 12px; border-radius: 16px; }
  .icon-button { width: 36px; height: 36px; min-height: 36px; }

  .intro-card p { margin-bottom: 0; font-size: .9rem; line-height: 1.35; }
  .button-row { grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
  button, .file-button { min-height: 42px; border-radius: 12px; font-size: .92rem; }
  .compact { min-height: 36px; padding: 0 10px; }
  .text-link { margin-top: 11px; font-size: .88rem; }

  .media-frame, .canvas-wrap { min-height: 0; border-radius: 14px; }
  #cameraCard { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  #cameraCard .media-frame {
    height: 32vh;
    height: clamp(180px, 32dvh, 280px);
    touch-action: pan-y;
  }
  #cameraCard video {
    min-height: 0;
    height: 100%;
    pointer-events: none;
    touch-action: pan-y;
  }
  #cameraCard .button-row { margin-top: 8px; }
  #editorCard .canvas-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    max-height: 44dvh;
  }
  #editorCard canvas { max-height: 44dvh; object-fit: contain; }
  .tap-hint { bottom: 8px; padding: 5px 9px; font-size: .75rem; }

  .step-header { gap: 8px; }
  #editorCard .step-header h2 { margin-bottom: 0; font-size: 1.05rem; }
  .sample-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; margin-top: 8px; }
  .sample-chip { gap: 3px; min-height: 50px; padding: 4px 2px; font-size: .7rem; }
  .sample-dot { width: 16px; height: 16px; }
  #editorCard .button-row { margin-top: 8px; }
  #editorCard .fine-print { display: none; }

  .result-hero { gap: 12px; }
  .result-swatch { width: 70px; height: 70px; border-radius: 16px; }
  .result-hero h2 { font-size: 1.45rem; }
  .confidence-row { margin-top: 12px; font-size: .9rem; }
  .confidence-track { margin-top: 4px; }
  #resultsCard .fine-print { margin-top: 8px; font-size: .72rem; line-height: 1.3; }
  .metrics-grid { gap: 6px; margin-top: 8px; }
  .metrics-grid div { padding: 8px; border-radius: 10px; }
  .metrics-grid strong { font-size: .8rem; }
  .matches-list { gap: 6px; }
  .match-row { grid-template-columns: 36px 1fr auto; gap: 8px; padding: 6px; border-radius: 10px; }
  .match-swatch { width: 36px; height: 36px; border-radius: 9px; }
  .match-name { font-size: .82rem; }
  .match-meta, .match-distance { font-size: .7rem; }
  #resultsCard .button-row { margin-top: 10px; }
  .warning { margin-top: 8px; }

  dialog { max-height: calc(100dvh - 20px); overflow: auto; }
  .dialog-card { padding: 16px; }
  .dialog-card ol { margin-bottom: 8px; line-height: 1.4; }
}

@media (max-width: 430px) and (max-height: 700px) {
  #cameraCard .media-frame {
    height: 28vh;
    height: clamp(160px, 28dvh, 220px);
  }
  #editorCard .canvas-wrap, #editorCard canvas { max-height: 36dvh; }
}
