/* ==========================================================================
   Kanga’s Kitchen — camera scanner

   The one screen in the app that is dark in both themes, on purpose: it sits
   on top of a live video frame, and the gingham palette would only fight it.
   The few pinned colours below are the dark-mode ends of the token set, so
   they still belong to the same family — they just don't follow the theme.

   Sized for a thumb on a heavy iPad held in two hands: nothing under 44pt,
   the shutter far larger, and every fixed edge inside the safe area.
   ========================================================================== */

.cam {
  position: fixed; inset: 0; z-index: 300;
  overflow: hidden;
  background: #000;
  color: #fff;
  font: 17px/1.45 var(--sans);
  touch-action: manipulation;
  -webkit-user-select: none; user-select: none;

  --cam-ok:    #8DBE80;              /* meadow, dark-mode value */
  --cam-warn:  #F2C14E;              /* sun — the same in both themes */
  --cam-panel: rgba(8, 14, 22, .74);
  --cam-top:   3.75rem;              /* room the title bar takes off the top   */
  /* Room the controls take off the bottom. The dock grows when the filmstrip
     appears, so JS measures it and feeds the real height back in; the literal
     is only the value used for the first frame. */
  --cam-dock: var(--cam-dock-measured, 13rem);
}

.cam__video {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;                 /* cropFromGuide() undoes exactly this */
  background: #000;
}

/* ── The framing rectangle ─────────────────────────────────────────────── */

.cam__frame {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding:
    calc(env(safe-area-inset-top) + var(--cam-top)) calc(env(safe-area-inset-right) + 1rem)
    calc(env(safe-area-inset-bottom) + var(--cam-dock)) calc(env(safe-area-inset-left) + 1rem);
  pointer-events: none;
  container-type: size;              /* so the guide can fit itself to this box */
}

/* CSS owns the box and JS measures it back, so an orientation change needs no
   recalculation anywhere — the guide simply re-lays out and the next analysis
   tick reads the new rectangle.

   `aspect-ratio` alone will not do it: with a definite width the ratio wins and
   max-height is quietly overrun, which on a landscape iPad shoves a portrait
   page guide off the bottom of the screen. Fitting the width to the container's
   height is the only version that holds in both orientations. The plain 100%
   is the fallback for anything without container units — the crop follows
   whatever rectangle is really on screen, so the photo stays honest either way. */
.cam__guide {
  position: relative;
  width: 100%;
  width: min(100%, calc(100cqh * var(--cam-ratio, 0.7727)));
  max-width: 100%; max-height: 100%;
  aspect-ratio: var(--cam-ratio, 0.7727);
  border-radius: 8px;
  outline: 2px solid rgba(255, 255, 255, .85);
  /* Everything outside the rectangle, dimmed in one declaration. */
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .55);
  transition: outline-color .2s, background .2s;
}
.cam.is-ready .cam__guide { outline-color: var(--cam-ok); outline-width: 3px; }

.cam__guide::before, .cam__guide::after {
  content: ""; position: absolute; width: 2rem; height: 2rem;
  border: 3px solid currentColor;
}
.cam__guide::before { top: -3px; left: -3px; border-right: 0; border-bottom: 0; border-radius: 8px 0 0 0; }
.cam__guide::after { bottom: -3px; right: -3px; border-left: 0; border-top: 0; border-radius: 0 0 8px 0; }
.cam.is-ready .cam__guide::before, .cam.is-ready .cam__guide::after { color: var(--cam-ok); }

/* ── Title bar ─────────────────────────────────────────────────────────── */

.cam__top {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; align-items: center; gap: .75rem;
  padding:
    calc(env(safe-area-inset-top) + .5rem) calc(env(safe-area-inset-right) + .75rem)
    .5rem calc(env(safe-area-inset-left) + .75rem);
  background: linear-gradient(rgba(0, 0, 0, .6), transparent);
}
.cam__title {
  flex: 1; margin: 0; text-align: center;
  font-family: var(--serif); font-size: 1.15rem; font-weight: 600;
  text-shadow: 0 1px 3px rgba(0, 0, 0, .8);
}
.cam__icon {
  flex: none; width: 44px; height: 44px;
  display: grid; place-items: center;
  border: 0; border-radius: 999px; padding: 0;
  background: var(--cam-panel); color: #fff; cursor: pointer;
}
.cam__icon .ico { width: 1.4rem; height: 1.4rem; }
span.cam__icon { background: none; }   /* the spacer that balances the close button */

/* ── Bubble level ──────────────────────────────────────────────────────── */

.cam__level {
  position: absolute;
  top: calc(env(safe-area-inset-top) + var(--cam-top) + .5rem);
  right: calc(env(safe-area-inset-right) + .9rem);
  width: 64px; height: 64px; border-radius: 999px;
  display: grid; place-items: center;
  background: var(--cam-panel);
  border: 1px solid rgba(255, 255, 255, .25);
}
.cam__level::before {
  content: ""; position: absolute; width: 20px; height: 20px;
  border-radius: 999px; border: 1px dashed rgba(255, 255, 255, .45);
}
.cam__bubble {
  width: 16px; height: 16px; border-radius: 999px;
  background: var(--cam-warn);
  transition: transform .1s linear, background .2s;
}
.cam__bubble[data-level="ok"] { background: var(--cam-ok); }

/* ── The dock: coaching line, filmstrip, controls ──────────────────────────
   One bottom-anchored column rather than three separately-offset bars, so
   nothing can end up sitting on top of the page she is trying to photograph.
   ────────────────────────────────────────────────────────────────────────── */

.cam__dock {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: flex; flex-direction: column; align-items: center; gap: .55rem;
  pointer-events: none;
}
.cam__dock > * { pointer-events: auto; }

.cam__coach {
  margin: 0; max-width: min(92vw, 30rem); text-align: center;
  padding: .6rem 1.1rem; border-radius: 999px;
  background: var(--cam-panel); backdrop-filter: blur(6px);
  font-weight: 600; font-size: 1.02rem;
}
.cam__coach[data-level="ok"]   { color: var(--cam-ok); }
.cam__coach[data-level="warn"] { color: var(--cam-warn); }

.cam__strip {
  align-self: stretch;
  display: flex; gap: .5rem; overflow-x: auto;
  padding: 0 calc(env(safe-area-inset-right) + .9rem) 0 calc(env(safe-area-inset-left) + .9rem);
  -webkit-overflow-scrolling: touch;
}
.cam__thumb {
  position: relative; flex: none;
  width: 60px; height: 60px; padding: 0;
  border: 2px solid rgba(255, 255, 255, .8); border-radius: var(--radius-sm);
  overflow: hidden; background: #000; cursor: pointer;
}
.cam__thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cam__thumb-n {
  position: absolute; left: 2px; bottom: 2px;
  min-width: 1.1rem; padding: 0 .2rem; border-radius: 4px;
  font-size: .72rem; font-weight: 700; line-height: 1.3;
  background: rgba(0, 0, 0, .7); color: #fff;
}
/* Permanently visible: a remove control that only shows on hover cannot be
   reached at all on an iPad. */
.cam__thumb-x {
  position: absolute; top: -1px; right: -1px;
  width: 22px; height: 22px; border-radius: 0 0 0 8px;
  display: grid; place-items: center;
  font-size: 1rem; font-weight: 700; line-height: 1;
  background: var(--danger); color: #fff;
}

.cam__bar {
  align-self: stretch;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: .75rem;
  min-height: 9.5rem;
  padding:
    .75rem calc(env(safe-area-inset-right) + 1rem)
    calc(env(safe-area-inset-bottom) + .9rem) calc(env(safe-area-inset-left) + 1rem);
  background: linear-gradient(transparent, rgba(0, 0, 0, .75) 35%);
}

.cam__aux { display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; }
.cam__chip {
  font: inherit; font-size: .85rem; font-weight: 600;
  min-height: 44px; padding: .45rem .8rem;
  border: 1px solid rgba(255, 255, 255, .3); border-radius: 999px;
  background: var(--cam-panel); color: #fff; cursor: pointer;
}
.cam__chip[aria-pressed="true"] { background: var(--cam-ok); border-color: var(--cam-ok); color: #10202A; }
.cam__chip:focus-visible, .cam__icon:focus-visible, .cam__thumb:focus-visible, .cam__shutter:focus-visible {
  outline: 3px solid #fff; outline-offset: 2px;
}

.cam__zoom { display: flex; align-items: center; min-height: 44px; }
.cam__zoom input { width: 8rem; accent-color: var(--cam-ok); }

.cam__shoot { display: flex; flex-direction: column; align-items: center; gap: .35rem; }
.cam__shutter {
  width: 76px; height: 76px; padding: 0;
  border-radius: 999px; border: 4px solid rgba(255, 255, 255, .9);
  background: transparent; cursor: pointer;
  display: grid; place-items: center;
}
.cam__shutter span {
  width: 58px; height: 58px; border-radius: 999px;
  background: #fff; transition: background .2s, transform .08s;
}
.cam__shutter:active span { transform: scale(.92); }
.cam.is-ready .cam__shutter span { background: var(--cam-ok); }
.cam__shutter:disabled { opacity: .4; cursor: default; }

.cam__caption {
  margin: 0; max-width: 12rem; text-align: center;
  font-size: .85rem; font-weight: 600;
  color: rgba(255, 255, 255, .85); text-shadow: 0 1px 3px rgba(0, 0, 0, .9);
}

/* Pinned to the light-theme accent rather than inheriting: the dark theme's
   paler blue is meant for a dark page, and white-on-pale over black video is
   the one place in the app where .btn--primary comes out too faint to read. */
.cam__done {
  justify-self: end; min-height: 44px;
  background: #3E7CB1; border-color: #3E7CB1; color: #fff;
}

/* The frame flashes rather than the whole screen — enough to say "got it"
   without blinding her in a dim kitchen. */
.cam.is-flashing .cam__guide { background: rgba(255, 255, 255, .75); }
@media (prefers-reduced-motion: reduce) {
  .cam__guide, .cam__bubble, .cam__shutter span { transition: none; }
  .cam.is-flashing .cam__guide { background: none; outline-color: var(--cam-ok); }
}

/* ── Landscape: an iPad photographing a cookbook ───────────────────────────
   A page guide is height-constrained here, so every pixel of height is worth
   more than a pixel of width. The dock stands up the right-hand side.
   ────────────────────────────────────────────────────────────────────────── */

@media (orientation: landscape) and (min-width: 700px) {
  /* Deliberately ignores the measured height — here the dock is a side column,
     and what has to be reserved is width, not height. */
  .cam { --cam-dock: 1rem; }

  .cam__frame { padding-right: calc(env(safe-area-inset-right) + 12rem); }

  .cam__dock {
    left: auto; top: 0; width: calc(env(safe-area-inset-right) + 12rem);
    justify-content: space-between; gap: .75rem;
    padding-top: calc(env(safe-area-inset-top) + var(--cam-top));
    background: linear-gradient(90deg, transparent, rgba(0, 0, 0, .78) 22%);
  }
  .cam__coach { max-width: 100%; margin: 0 .75rem; border-radius: var(--radius); font-size: .95rem; }
  .cam__strip { flex-wrap: wrap; justify-content: center; overflow: visible; }

  .cam__bar {
    grid-template-columns: none; grid-auto-flow: row;
    justify-items: center; gap: 1rem;
    min-height: 0; background: none;
  }
  .cam__aux { flex-direction: column; align-items: stretch; width: 100%; order: 3; }
  .cam__chip { text-align: center; }
  .cam__shoot { order: 1; }
  .cam__done { order: 2; justify-self: center; width: 100%; }
  .cam__level { right: calc(env(safe-area-inset-right) + 13rem); }
}

/* ── When the camera can't be used ─────────────────────────────────────── */

.cam__trouble {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 1.75rem; text-align: center;
  padding:
    calc(env(safe-area-inset-top) + 2rem) calc(env(safe-area-inset-right) + 1.5rem)
    calc(env(safe-area-inset-bottom) + 2rem) calc(env(safe-area-inset-left) + 1.5rem);
  background: var(--paper); color: var(--ink);
}
.cam__trouble-text {
  margin: 0; max-width: 28rem;
  font-family: var(--serif); font-size: 1.25rem; line-height: 1.5;
}
.cam__trouble-acts { display: flex; flex-wrap: wrap; gap: .75rem; justify-content: center; }
