/* WMP Eyewear — PD measurement.
   Direction: "digital prescription pad" — cool paper + ink, mono instrument
   readouts, one amber accent shared with the on-canvas card outline, and a
   millimetre tick-ruler as the signature motif. */

:root {
  --paper: #f6f8f7;
  --panel: #ffffff;
  --ink: #171d1f;
  --muted: #5d6b70;
  --line: #d9dfdd;
  --brand: #ffd23f; /* matches the card outline drawn on canvas */
  --brand-deep: #b8860b; /* amber legible on paper */
  --good: #177a4e;
  --bad: #c24040;
  --display: "Fraunces", georgia, serif;
  --mono: "IBM Plex Mono", ui-monospace, "SF Mono", menlo, monospace;
  --body: -apple-system, blinkmacsystemfont, "Segoe UI", roboto, helvetica, arial, sans-serif;
}

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

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--body);
  font-size: 15px;
  line-height: 1.55;
  min-height: 100vh;
}

/* ---- header ---------------------------------------------------------- */

.app-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 18px 24px;
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 800; letter-spacing: 0.08em; font-size: 15px; }
.brand span { color: var(--brand-deep); }
.tag {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

#app { max-width: 720px; margin: 0 auto; padding: 40px 20px 72px; }

/* ---- steps ------------------------------------------------------------ */

.step { display: none; }
.step.active { display: block; animation: step-in 0.25s ease both; }
@keyframes step-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

/* ---- type -------------------------------------------------------------- */

h1 {
  font-family: var(--display);
  font-weight: 600;
  font-size: clamp(32px, 6vw, 44px);
  line-height: 1.1;
  letter-spacing: -0.01em;
  margin-bottom: 14px;
}
h2, .camera-title {
  font-family: var(--display);
  font-weight: 600;
  font-size: 23px;
  line-height: 1.25;
  margin-bottom: 10px;
}
.camera-title { text-align: center; min-height: 29px; margin-bottom: 14px; }

.eyebrow {
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--brand-deep);
  margin-bottom: 10px;
}

.lede { color: var(--muted); max-width: 54ch; margin-bottom: 26px; }
.hint { color: var(--muted); font-size: 13.5px; margin-bottom: 22px; }
.stepnote { color: var(--muted); margin-bottom: 6px; max-width: 60ch; }
.privacy { margin-top: 20px; color: var(--muted); font-size: 12.5px; max-width: 60ch; }

/* ---- signature: millimetre ruler ---------------------------------------- */

.ruler {
  width: 190px;
  height: 13px;
  margin: 0 0 22px 2px;
  background:
    repeating-linear-gradient(to right, var(--ink) 0 1px, transparent 1px 5px)
      left bottom / 100% 6px no-repeat,
    repeating-linear-gradient(to right, var(--ink) 0 1px, transparent 1px 25px)
      left bottom / 100% 13px no-repeat;
  opacity: 0.55;
}

/* ---- intro steps -------------------------------------------------------- */

.howto {
  list-style: none;
  counter-reset: pdstep;
  border-top: 1px solid var(--line);
  margin-bottom: 22px;
}
.howto li {
  counter-increment: pdstep;
  display: grid;
  grid-template-columns: 44px 150px 1fr;
  gap: 12px;
  align-items: baseline;
  padding: 13px 2px;
  border-bottom: 1px solid var(--line);
}
.howto li::before {
  content: counter(pdstep, decimal-leading-zero);
  font-family: var(--mono);
  font-weight: 600;
  font-size: 13px;
  color: var(--brand-deep);
}
.howto li strong { font-size: 14.5px; letter-spacing: 0.01em; }
.howto li span { color: var(--muted); font-size: 13.5px; }

/* ---- buttons ------------------------------------------------------------ */

.controls { display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; }

.btn {
  appearance: none;
  border: 0;
  border-radius: 10px;
  padding: 12px 22px;
  font-family: var(--body);
  font-size: 14.5px;
  font-weight: 600;
  cursor: pointer;
  transition: filter 0.15s ease, background 0.15s ease;
}
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn.primary { background: var(--brand); color: var(--ink); }
.btn.primary:hover:not(:disabled) { filter: brightness(0.94); }
.btn.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
}
.btn.ghost:hover:not(:disabled) { color: var(--ink); border-color: var(--muted); }
.btn:focus-visible,
button:focus-visible {
  outline: 2px solid var(--brand-deep);
  outline-offset: 2px;
}

/* ---- camera / freeze stage ----------------------------------------------- */

.stage {
  position: relative;
  width: 100%;
  border-radius: 16px;
  overflow: hidden;
  background: #000;
  aspect-ratio: 4 / 3;
  border: 1px solid var(--line);
}
.stage video,
.stage canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
#video { transform: scaleX(-1); } /* mirror preview; overlay mirrors in-code */
#freeze { position: static; display: block; touch-action: none; }

.guide-msg {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  background: rgba(15, 20, 22, 0.78);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(255, 255, 255, 0.16);
  color: #f2f5f4;
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 13.5px;
  white-space: nowrap;
  max-width: 94%;
  overflow: hidden;
  text-overflow: ellipsis;
}

.countdown-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  background: var(--brand);
  color: var(--ink);
  font-family: var(--mono);
  font-weight: 600;
  font-size: 30px;
  line-height: 1;
  padding: 10px 14px;
  border-radius: 12px;
}
.countdown-badge:empty { display: none; }

/* Frame-sampling progress: fills as good frames are measured. */
.frame-progress {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 5px;
  background: rgba(255, 255, 255, 0.18);
}
#frame-progress-fill {
  height: 100%;
  width: 0%;
  background: var(--brand);
  transition: width 0.15s linear;
}

/* ---- quality chips --------------------------------------------------------- */

.quality-list {
  list-style: none;
  margin-top: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
}
.quality-list li {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 5px 11px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.quality-list li::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentcolor;
}
.quality-list li.ok { border-color: #bfe3d1; background: #ecf7f1; color: var(--good); }
.quality-list li.fail { border-color: #ecc7c7; background: #fbefef; color: var(--bad); }

/* ---- calibrate ---------------------------------------------------------------- */

.detect-status {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--brand-deep);
  min-height: 18px;
  margin-bottom: 10px;
}
.detect-status.ok { color: var(--good); }

/* ---- results -------------------------------------------------------------------- */

.result-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 30px 26px 26px;
  margin-top: 10px;
}

.pd-main { text-align: center; margin-bottom: 24px; }
.pd-value {
  font-family: var(--mono);
  font-weight: 600;
  font-size: clamp(48px, 11vw, 64px);
  line-height: 1;
  letter-spacing: -0.02em;
}
.pd-value::after {
  content: " mm";
  font-size: 0.32em;
  font-weight: 500;
  color: var(--muted);
  letter-spacing: 0;
}

.pd-ruler {
  position: relative;
  width: 210px;
  height: 15px;
  margin: 14px auto 8px;
  background:
    repeating-linear-gradient(to right, var(--ink) 0 1px, transparent 1px 5px)
      left bottom / 100% 7px no-repeat,
    repeating-linear-gradient(to right, var(--ink) 0 1px, transparent 1px 25px)
      left bottom / 100% 15px no-repeat;
  opacity: 0.5;
}
.pd-ruler::after {
  content: "";
  position: absolute;
  left: 50%;
  top: -2px;
  width: 2px;
  height: calc(100% + 2px);
  background: var(--brand-deep);
}

.pd-label {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

.pd-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 14px;
  text-align: center;
  border-top: 1px solid var(--line);
  padding-top: 20px;
}
.pd-grid span {
  display: block;
  font-family: var(--mono);
  font-weight: 600;
  font-size: 21px;
}
.pd-grid label {
  font-size: 12px;
  color: var(--muted);
}

.confidence {
  margin-top: 20px;
  text-align: center;
  font-weight: 700;
  font-size: 14px;
  padding: 9px;
  border-radius: 10px;
}
.confidence.high { background: #ecf7f1; color: var(--good); }
.confidence.medium { background: #fbf3df; color: #8a6410; }
.confidence.low { background: #fbefef; color: var(--bad); }

.result-notes {
  margin-top: 14px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

/* ---- debug -------------------------------------------------------------------------- */

.debug-panel {
  margin-top: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
}
.debug-panel.hidden { display: none; }
.debug-canvases { display: flex; gap: 8px; flex-wrap: wrap; }
.debug-canvases canvas {
  width: 205px;
  height: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  image-rendering: pixelated;
}
#debug-text {
  margin-top: 10px;
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.6;
  color: var(--muted);
  white-space: pre-wrap;
}

/* ---- small screens -------------------------------------------------------------------- */

@media (max-width: 480px) {
  #app { padding: 26px 14px 56px; }
  .howto li { grid-template-columns: 36px 1fr; }
  .howto li span { grid-column: 2; }
  .controls .btn { flex: 1 1 auto; }
  .countdown-badge { font-size: 24px; padding: 8px 12px; }
}

/* Camera view is portrait on ALL devices: it crops the stream's sides
   (object-fit: cover crops video and overlay identically, so guides stay
   aligned) and centers the face in the lens's low-distortion zone.
   !important beats the inline aspect-ratio set from the stream's native
   shape. Calibrate stage keeps its native aspect — corner dragging maps
   pointer coords linearly and must not be cropped. */
#step-camera .stage {
  aspect-ratio: 3 / 4 !important;
  max-height: 72vh;
  max-width: 540px;
  margin-left: auto;
  margin-right: auto;
}

@media (max-width: 640px) {
  .guide-msg {
    white-space: normal;
    text-align: center;
    width: 92%;
    font-size: 13px;
    line-height: 1.4;
  }
}

/* Calibrate photo is portrait now too — cap its width so it doesn't tower. */
#step-calibrate .stage {
  max-width: 540px;
  margin-left: auto;
  margin-right: auto;
}

/* Desktop: camera step splits into details (left) | camera (right).
   Mobile keeps the stacked flow untouched. */
@media (min-width: 700px) {
  #app:has(#step-camera.active) { max-width: 1000px; }

  #step-camera.active {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(320px, 440px);
    grid-template-areas:
      "title stage"
      "chips stage"
      "controls stage"
      "debug debug";
    grid-template-rows: auto auto 1fr auto;
    column-gap: 32px;
    align-items: start;
  }
  #step-camera .camera-title {
    grid-area: title;
    text-align: left;
    margin-bottom: 6px;
  }
  #step-camera .stage { grid-area: stage; }
  #step-camera .quality-list {
    grid-area: chips;
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    margin-top: 8px;
  }
  #step-camera .controls {
    grid-area: controls;
    align-self: end;
    margin-top: 24px;
  }
  #step-camera .debug-panel { grid-area: debug; margin-top: 24px; }
}

@media (prefers-reduced-motion: reduce) {
  .step.active { animation: none; }
  .btn { transition: none; }
}
