/* SkipBoard v2 — box check screen.
   Owner: capture flow. Scoped to .screen-boxes.

   Paper / emerald (theme-aware): this is the review table, not the camera. The page artefact is
   white in every theme; everything around it follows the tokens. */

.screen-boxes { padding-bottom: env(safe-area-inset-bottom); }

.boxes-flow {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
.boxes-flow .flow-head { border-bottom-color: var(--line-soft); }
.boxes-flow .flow-title h1 { font-size: var(--t-20); }
.boxes-flow .flow-title span { font-size: 12px; }

.boxes-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: var(--s-3);
  width: 100%;
  max-width: 620px;
  margin: 0 auto;
  padding: var(--s-3) var(--gutter) var(--s-2);
}
.boxes-main {
  display: flex;
  flex-direction: column;
  gap: var(--s-3);
  min-width: 0;
}
.boxes-name .field-label { margin-top: 0; }

/* ---- The page + its boxes ----------------------------------------------- */
.boxes-view {
  position: relative;
  flex-shrink: 0;
  height: clamp(300px, 46dvh, 560px);
  overflow: hidden;
  border-radius: var(--r-card);
  border: 1px solid var(--line);
  background:
    radial-gradient(120% 70% at 50% 0%, var(--accent-soft), transparent 70%),
    var(--surface-2);
  box-shadow: var(--e-1);
  touch-action: pan-y;
}
.boxes-view.is-zoomed { touch-action: none; }
.boxes-view.is-drawing {
  touch-action: none;
  cursor: crosshair;
  outline: 3px dashed var(--accent);
  outline-offset: 2px;
}
.boxes-canvas {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
  will-change: transform;
}
.box-page {
  position: absolute;
  inset: 0;
  background-color: #fff;
  background-position: 0 0;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  border-radius: 6px;
  box-shadow: 0 10px 30px rgba(20, 27, 24, 0.18);
}

/* Detected box. Sizes/borders are counter-scaled by --bz (the zoom factor) so a box stays the same
   on screen when the page is zoomed in. Pointer taps are resolved by JS hit testing, so the element
   itself does not need to be the hit target (small boxes get a padded 56px area). */
.boxes-flow .box {
  position: absolute;
  padding: 0;
  display: block;
  border: calc(2px / var(--bz, 1)) solid var(--accent);
  border-radius: calc(4px / var(--bz, 1));
  background: color-mix(in srgb, var(--accent) 13%, transparent);
  overflow: visible;
  pointer-events: none;
  font: inherit;
  text-align: left;
  color: var(--text);
}
.boxes-flow .box-check { background: color-mix(in srgb, var(--accent) 22%, transparent); }
.boxes-flow .box:not(.has-tag) .box-tag { display: none; }
.boxes-flow .box.box-tight:not(.is-sel) { border: 2px dashed var(--warn); }
.boxes-flow .box.is-sel {
  border-color: var(--info);
  background: color-mix(in srgb, var(--info) 20%, transparent);
  z-index: 3;
}
.boxes-flow .box:focus-visible {
  outline: calc(3px / var(--bz, 1)) solid var(--info);
  outline-offset: calc(3px / var(--bz, 1));
}
.box-tag {
  position: absolute;
  top: 0;
  left: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: calc(20px / var(--bz, 1));
  height: calc(20px / var(--bz, 1));
  padding: 0 calc(5px / var(--bz, 1));
  border-radius: calc(5px / var(--bz, 1));
  background: var(--accent);
  color: var(--on-accent);
  font-family: var(--font-body);
  font-size: calc(11px / var(--bz, 1));
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.box.is-sel .box-tag { background: var(--info); color: var(--bg-deep); }
.box-label {
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: calc(3px / var(--bz, 1));
  max-width: min(60vw, 260px);
  padding: calc(3px / var(--bz, 1)) calc(7px / var(--bz, 1));
  border-radius: calc(6px / var(--bz, 1));
  background: var(--info);
  color: var(--bg-deep);
  font-family: var(--font-body);
  font-size: calc(11px / var(--bz, 1));
  font-weight: 600;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.draw-rect {
  position: absolute;
  z-index: 4;
  border: 2px dashed var(--info);
  border-radius: 4px;
  background: color-mix(in srgb, var(--info) 16%, transparent);
  pointer-events: none;
}

/* ---- Detection view (toggle): traced lines, rejects, partitions, pen size --- */
.dbg-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: visible;
}
.dbg-baseline { fill: none; stroke: var(--dbg-baseline); stroke-width: 2; }
.dbg-reject { stroke: var(--dbg-reject); stroke-width: 1.5; fill: none; }
.dbg-prose { fill: color-mix(in srgb, var(--dbg-reject) 14%, transparent); stroke: var(--dbg-reject); stroke-width: 0.8; stroke-dasharray: 3 2; }
.dbg-part { stroke: var(--dbg-partition); stroke-width: 2; fill: none; }
.dbg-layer * { vector-effect: non-scaling-stroke; }
.dbg-badge {
  position: absolute;
  left: 10px;
  top: 10px;
  z-index: 5;
  padding: 6px 10px;
  border-radius: 10px;
  background: var(--dbg-badge-bg);
  color: var(--dbg-badge-text);
  font: 600 var(--t-13) / 1.3 var(--font-body);
  pointer-events: none;
}
.boxes-zoom-btn.is-on { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }

/* ---- Floating view controls -------------------------------------------- */
.boxes-zoom {
  position: absolute;
  right: 10px;
  bottom: 10px;
  z-index: 5;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.boxes-zoom-btn {
  width: var(--tap);
  height: var(--tap);
  min-width: var(--tap);
  min-height: var(--tap);
  border-radius: 16px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border: 1px solid var(--line);
  box-shadow: var(--e-1);
  color: var(--text);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.boxes-zoom-btn:hover { background: var(--surface); }
.boxes-pagetag,
.boxes-zoomtag {
  position: absolute;
  top: 10px;
  z-index: 4;
  padding: 5px 10px;
  border-radius: var(--r-pill);
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: var(--t-11);
  font-weight: 700;
  letter-spacing: 0.02em;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.boxes-pagetag { left: 10px; }
.boxes-zoomtag { right: 10px; font-variant-numeric: tabular-nums; }

/* Guidance + progress */
.boxes-hintrow {
  display: flex;
  align-items: flex-start;
  gap: var(--s-3);
}
.boxes-hint {
  flex: 1;
  margin: 0;
  min-height: 18px;
  color: var(--muted);
  font-size: var(--t-13);
  line-height: 1.45;
}
.boxes-list-btn { flex-shrink: 0; gap: 6px; }
.boxes-meta {
  display: flex;
  align-items: center;
  gap: var(--s-3);
}
.boxes-meta .progress {
  flex: 1;
  height: 6px;
}
.boxes-meta .hint { white-space: nowrap; }

/* ---- Page thumbs -------------------------------------------------------- */
.boxes-thumbs {
  display: flex;
  gap: var(--s-2);
  overflow-x: auto;
  padding: 2px 2px 4px;
  scrollbar-width: none;
}
.boxes-thumbs::-webkit-scrollbar { display: none; }
.boxes-thumb {
  position: relative;
  flex-shrink: 0;
  width: var(--tap);
  height: 72px;
  padding: 0;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  overflow: hidden;
}
.boxes-thumb-img {
  position: absolute;
  inset: 0;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  opacity: 0.92;
}
.boxes-thumb-label {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 2px 0;
  background: color-mix(in srgb, var(--bg-deep) 80%, transparent);
  color: var(--text);
  font-size: var(--t-11);
  font-weight: 700;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.boxes-thumb.is-on { border-color: var(--accent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 32%, transparent); }
.boxes-thumb.is-on .boxes-thumb-label { background: var(--accent); color: var(--on-accent); }
.boxes-thumb.is-add {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border-style: dashed;
  border-color: var(--accent-line);
  background: transparent;
  color: var(--accent);
  font-size: var(--t-11);
  font-weight: 700;
}

/* ---- Empty state -------------------------------------------------------- */
.boxes-empty {
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
  padding: 18px;
}
.boxes-empty h2 {
  font-family: var(--font-display);
  font-size: var(--t-20);
  font-weight: 650;
  letter-spacing: -0.02em;
}
.boxes-empty .btn-row { margin-top: var(--s-1); }
.boxes-empty .btn-ghost { color: var(--accent); }

/* ---- Box list (desktop panel + phone sheet) ----------------------------- */
.boxes-side { display: none; }
.boxes-side-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--s-3);
}
.boxes-side-head h2 {
  font-family: var(--font-display);
  font-size: var(--t-20);
  font-weight: 650;
  letter-spacing: -0.02em;
}
.boxes-side-list {
  display: flex;
  flex-direction: column;
  gap: var(--s-4);
}
.boxes-sheet-list {
  display: flex;
  flex-direction: column;
  gap: var(--s-4);
}
.boxes-list-group {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.boxes-list-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--s-3);
  padding: 0 4px 4px;
}
.boxes-list-head strong {
  font-size: var(--t-13);
  font-weight: 700;
}
.boxes-list-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: var(--tap);
  padding: 6px 10px;
  border: 1px solid transparent;
  border-radius: 12px;
  background: none;
  color: var(--text);
  text-align: left;
}
.boxes-list-row:hover { background: var(--surface-2); }
.boxes-list-row.is-sel {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.boxes-list-tag {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  min-width: 28px;
  height: 28px;
  padding: 0 6px;
  border-radius: 9px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: var(--t-11);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.boxes-list-row.is-sel .boxes-list-tag { background: var(--accent); color: var(--on-accent); }
.boxes-list-row .row-text strong { font-size: var(--t-15); }
.boxes-list-done { display: inline-flex; color: var(--accent); flex-shrink: 0; }

/* ---- Undo bar ----------------------------------------------------------- */
.boxes-undo {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--foot-h) + 10px);
  z-index: 30;
  display: flex;
  align-items: center;
  gap: var(--s-3);
  width: calc(100% - 32px);
  max-width: 520px;
  padding: 8px 10px 8px 18px;
  border-radius: var(--r-pill);
  background: var(--text);
  color: var(--bg);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.24);
  font-size: var(--t-13);
  font-weight: 600;
}
.boxes-undo span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.boxes-undo .btn { color: var(--bg); flex-shrink: 0; }
.boxes-undo .btn svg { color: var(--accent); }

/* ---- Editor sheet bits -------------------------------------------------- */
.sheet-foot-inline {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: var(--s-4);
}
.sheet-foot-inline .btn { min-height: var(--tap); }

/* Save state */
.boxes-flow [data-save] .spinner { width: 18px; height: 18px; border-width: 2px; }
.boxes-flow[aria-busy='true'] .boxes-view { opacity: 0.72; }

@media (prefers-reduced-motion: reduce) {
  .boxes-undo,
  .boxes-thumb,
  .boxes-canvas { transition: none !important; }
}

@media (min-width: 900px) {
  .boxes-body {
    display: grid;
    grid-template-columns: minmax(0, 1.1fr) minmax(0, 0.9fr);
    align-items: start;
    gap: var(--s-6);
    max-width: 900px;
    padding: var(--s-4) var(--s-6) var(--s-4);
  }
  .boxes-view { height: clamp(360px, 52dvh, 620px); }
  .boxes-side {
    display: flex;
    flex-direction: column;
    gap: var(--s-3);
    position: sticky;
    top: 84px;
    max-height: calc(100dvh - 200px);
    min-height: 0;
  }
  .boxes-side-list {
    overflow-y: auto;
    padding-right: 4px;
    min-height: 0;
  }
  .boxes-list-btn { display: none; }
  .boxes-undo { bottom: 96px; }
}

/* ---- Form memory: "you've scanned this form before" ---------------------- */
.boxes-memory { align-items: flex-start; margin-bottom: var(--s-3, 12px); }
.boxes-memory-text { display: flex; flex-direction: column; gap: 6px; }
.boxes-memory-text .btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }

/* ---- "Boxes look wrong? Report it" ------------------------------------------ */
.boxes-bug { align-self: flex-start; min-height: var(--tap); margin-top: 4px; }

/* Experiment (audit #13b): a space only the hand rules found. Faint and dashed; a tap adds it. */
.boxes-flow .box.box-ghost {
  border: calc(2px / var(--bz, 1)) dashed var(--muted);
  background: transparent;
  opacity: 0.55;
}
.boxes-flow .box.box-ghost .box-tag { display: inline-flex; }
