/**
 * Scan Exams — mobile-first paper-exam scanning UI.
 *
 * Step containers are exclusive (only one visible at a time, controlled
 * by the orchestrator JS via the `hidden` attribute).
 *
 * Brand: matches homework-dashboard.css conventions (#6564b6 purple primary,
 * Lexend Deca, gradient-bordered cards). Most chrome is Lexend; the camera
 * step is full-bleed black for usability outdoors.
 */

:root {
  --scan-primary: #6564b6;
  --scan-primary-dark: #534f9a;
  --scan-text: #1a1a2e;
  --scan-muted: #6b6e80;
  --scan-bg: #fafbff;
  --scan-card: #ffffff;
  --scan-border: #d8d9eb;
  --scan-success: #22c55e;
  --scan-warning: #f59e0b;
  --scan-danger: #ef4444;
}

.scan-exams-main {
  min-height: 100vh;
  background: var(--scan-bg);
  font-family: 'Lexend Deca', system-ui, sans-serif;
  color: var(--scan-text);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.loading-spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--scan-border);
  border-top-color: var(--scan-primary);
  border-radius: 50%;
  animation: scan-spin 0.8s linear infinite;
  margin: 4rem auto;
}

@keyframes scan-spin {
  to { transform: rotate(360deg); }
}

/* ---------------------------------------------------------------- messages */

.scan-exams-msg {
  max-width: 32rem;
  margin: 4rem auto;
  padding: 2rem 1.5rem;
  background: var(--scan-card);
  border: 1px solid var(--scan-border);
  border-radius: 16px;
  text-align: center;
}

.scan-exams-msg h2 {
  margin-top: 0;
  color: var(--scan-primary);
}

.scan-error {
  color: var(--scan-danger);
  font-size: 0.95rem;
}

/* ----------------------------------------------------------------- layout */

.scan-exams {
  max-width: 40rem;
  margin: 0 auto;
  padding: 1rem;
}

.scan-step {
  padding: 1rem 0.5rem 6rem;
}

.scan-step h1 {
  font-size: 1.5rem;
  margin: 0.5rem 0 1.5rem;
}

.scan-hint {
  color: var(--scan-muted);
  font-size: 0.95rem;
  margin: 0.5rem 0;
}

/* ----------------------------------------------------------- sheet printing */

.scan-sheet-menu {
  margin: 0.75rem 0 1.25rem;
}

.scan-exams-msg .scan-sheet-menu {
  margin-top: 1.5rem;
}

.scan-sheet-menu > summary {
  list-style: none;
  width: 100%;
}

.scan-sheet-menu > summary::-webkit-details-marker {
  display: none;
}

.scan-sheet-links {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.6rem;
  margin-top: 0.75rem;
}

.scan-sheet-links a {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 3rem;
  padding: 0.75rem 0.95rem;
  background: var(--scan-card);
  border: 1px solid var(--scan-border);
  border-radius: 12px;
  color: var(--scan-text);
  font-size: 0.95rem;
  font-weight: 600;
  text-decoration: none;
}

.scan-sheet-links a::after {
  content: "PDF";
  color: var(--scan-muted);
  font-size: 0.75rem;
  font-weight: 700;
}

.scan-sheet-links a:hover {
  border-color: var(--scan-primary);
}

/* ---------------------------------------------------------------- breadcrumb */

.scan-breadcrumb {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  background: var(--scan-card);
  border: 1px solid var(--scan-border);
  border-radius: 999px;
  padding: 0.5rem 0.75rem 0.5rem 1rem;
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
}

.scan-breadcrumb-change {
  background: transparent;
  border: 0;
  color: var(--scan-primary);
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.85rem;
}

/* ----------------------------------------------------------------- tiles */

.scan-tile-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

.scan-tile-list {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.scan-tile {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.25rem;
  padding: 1.25rem 1.5rem;
  background: var(--scan-card);
  border: 1px solid var(--scan-border);
  border-radius: 16px;
  cursor: pointer;
  font-family: inherit;
  text-align: left;
  width: 100%;
  transition: border-color 0.15s, transform 0.05s;
}

.scan-tile:active:not([disabled]) {
  transform: scale(0.98);
}

.scan-tile:not([disabled]):hover {
  border-color: var(--scan-primary);
}

.scan-tile[disabled] {
  opacity: 0.45;
  cursor: not-allowed;
}

.scan-tile-title {
  font-size: 1.15rem;
  font-weight: 600;
}

.scan-tile-sub {
  font-size: 0.85rem;
  color: var(--scan-muted);
}

.scan-tile-tag {
  margin-left: auto;
  font-size: 0.75rem;
  background: var(--scan-bg);
  border: 1px solid var(--scan-border);
  border-radius: 999px;
  padding: 0.15rem 0.55rem;
  color: var(--scan-muted);
}

/* ------------------------------------------------------------------ form */

.scan-form-row {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
}

.scan-form-row > label {
  font-size: 0.9rem;
  font-weight: 600;
}

.scan-select,
.scan-input {
  width: 100%;
  padding: 0.75rem 0.9rem;
  font-size: 1rem;
  font-family: inherit;
  border: 1px solid var(--scan-border);
  border-radius: 12px;
  background: var(--scan-card);
}

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

.scan-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.85rem 1.5rem;
  border-radius: 999px;
  border: 0;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: filter 0.15s;
}

.scan-btn:active:not([disabled]) {
  filter: brightness(0.95);
}

.scan-btn[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
}

.scan-btn-primary {
  background: var(--scan-primary);
  color: #fff;
}

.scan-btn-ghost {
  background: transparent;
  border: 1px solid var(--scan-border);
  color: var(--scan-text);
}

/* Top-of-page link to Scan Review (visible on both desktop and mobile) */
.scan-review-link {
  display: inline-flex;
  align-items: center;
  padding: 0.55rem 1rem;
  margin: 0 0 1rem;
  background: transparent;
  border: 1px solid var(--scan-border);
  border-radius: 999px;
  color: var(--scan-text);
  font-weight: 600;
  font-size: 0.9rem;
  text-decoration: none;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.scan-review-link:hover {
  background: var(--scan-primary);
  color: #fff;
  border-color: var(--scan-primary);
}

/* Protected workflow: float the link to the top-right of the page */
#protected-content > .scan-review-link {
  float: right;
}

#protected-content > .scan-sheet-menu {
  clear: both;
}

/* Desktop blocker: center-align (sits above the "mobile feature" heading) */
#desktop-blocker .scan-review-link {
  margin-bottom: 1.5rem;
}

/* --------------------------------------------------------------- camera */

.scan-step-camera {
  position: fixed;
  inset: 0;
  background: #000;
  padding: 0;
  margin: 0;
  z-index: 50;
}

#scan-video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.scan-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.scan-overlay-frame {
  /* Fill the phone screen as much as possible while preserving A4 portrait
     aspect (1 / √2 ≈ 0.7071). On a typical 9:19.5 phone the frame is
     height-limited: ~96vh tall, ~68vh wide, leaving ~15-16% slivers on each
     side. The point is to force the user closer to the paper so the printed
     5mm corner fiducials grow from ~10px to ~30-40px in the captured frame —
     well above the OMR detector's noise floor. */
  height: 96vh;
  aspect-ratio: 1 / 1.4142;
  max-width: 96vw;
  /* When the viewport is wider than A4 allows at 96vh (landscape / desktop),
     keep A4 by also clamping height from max-width. */
  max-height: calc(96vw * 1.4142);
  border: 3px solid rgba(239, 68, 68, 0.95);
  border-radius: 4px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.45);
  transition: border-color 0.2s ease;
}

/* Camera feedback: moving/missing marks = red, searching/settling = amber,
   ready = green. Auto-shutter only fires from ready. */
.scan-overlay-frame[data-stability="moving"] {
  border-color: rgba(239, 68, 68, 0.95);     /* red */
}
.scan-overlay-frame[data-stability="searching"] {
  border-color: rgba(245, 158, 11, 0.95);    /* amber */
}
.scan-overlay-frame[data-stability="ready"] {
  border-color: rgba(34, 197, 94, 0.95);     /* green */
}

.scan-banner {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 0.75rem);
  left: 0.75rem;
  right: 0.75rem;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 0.85rem;
  text-align: center;
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  pointer-events: none;
}

.scan-camera-hud {
  position: absolute;
  bottom: calc(env(safe-area-inset-bottom) + 1.25rem);
  left: 0;
  right: 0;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 1rem;
  padding: 0 1.25rem;
}

.scan-counter {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  color: #fff;
  font-size: 0.85rem;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.6);
}

.scan-counter-num {
  font-size: 1.6rem;
  font-weight: 700;
}

.scan-name-banner {
  display: inline-block;
  max-width: 13rem;
  margin-top: 0.35rem;
  padding: 0.35rem 0.55rem;
  border-radius: 8px;
  font-size: 0.78rem;
  line-height: 1.25;
  color: #fff;
  text-shadow: none;
}

.scan-name-banner[hidden] {
  display: none;
}

.scan-shutter {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  background: #fff;
  border: 5px solid rgba(255, 255, 255, 0.4);
  box-shadow: 0 0 0 3px #fff inset;
  cursor: pointer;
}

.scan-shutter:active {
  transform: scale(0.94);
}

.scan-done-btn {
  justify-self: end;
}

/* Flash + checkmark default to invisible; they only show during the
   ~600ms after a successful capture, via the `is-firing` class. */
.scan-flash {
  position: absolute;
  inset: 0;
  background: #fff;
  opacity: 0;
  pointer-events: none;
}
.scan-flash.is-firing {
  animation: scan-flash 0.22s ease-out forwards;
}
@keyframes scan-flash {
  0%   { opacity: 0.9; }
  100% { opacity: 0; }
}

.scan-checkmark {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.6);
  width: 110px;
  height: 110px;
  border-radius: 50%;
  background: rgba(34, 197, 94, 0.92);
  color: #fff;
  font-size: 4.4rem;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  opacity: 0;
}
.scan-checkmark.is-firing {
  animation: scan-pop 0.6s ease-out forwards;
}
@keyframes scan-pop {
  0%   { transform: translate(-50%, -50%) scale(0.6); opacity: 0; }
  25%  { transform: translate(-50%, -50%) scale(1.05); opacity: 1; }
  70%  { transform: translate(-50%, -50%) scale(1); opacity: 1; }
  100% { transform: translate(-50%, -50%) scale(1); opacity: 0; }
}

.scan-banner-error {
  background: rgba(239, 68, 68, 0.85) !important;
}

.scan-banner-warning {
  background: rgba(245, 158, 11, 0.88);
  color: #111827;
}

.scan-banner-name-ok {
  background: rgba(34, 197, 94, 0.9) !important;
}

.scan-banner-name-warn {
  background: rgba(245, 158, 11, 0.92) !important;
}

.scan-banner-name-neutral {
  background: rgba(107, 114, 128, 0.88) !important;
}

/* ------------------------------------------------------------- review queue */

.scan-review-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.scan-review-item {
  display: grid;
  grid-template-columns: 64px 1fr auto;
  align-items: center;
  gap: 0.85rem;
  padding: 0.85rem;
  background: var(--scan-card);
  border: 1px solid var(--scan-border);
  border-radius: 14px;
  cursor: pointer;
  text-align: left;
  width: 100%;
  font-family: inherit;
  color: inherit;
}

.scan-review-thumb {
  width: 64px;
  height: 90px;
  background: #f0f0f5;
  border-radius: 6px;
  object-fit: cover;
}

.scan-review-name {
  font-weight: 600;
  font-size: 1rem;
}

.scan-review-sub {
  color: var(--scan-muted);
  font-size: 0.85rem;
}

.scan-review-pill {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.25rem 0.6rem;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.scan-review-pill[data-status="pending"]      { background: #eef0fb; color: #5b5da3; }
.scan-review-pill[data-status="parsed"]       { background: #ddf6e4; color: #117a3a; }
.scan-review-pill[data-status="needsReview"]  { background: #fff3cd; color: #8a6500; }
.scan-review-pill[data-status="committed"]    { background: #ddf6e4; color: #117a3a; }
.scan-review-pill[data-status="failed"]       { background: #fde2e2; color: #a31616; }
.scan-review-pill[data-status="alreadySubmitted"] {
  background: #fde2e2;
  color: #a31616;
  border: 1px solid #f5b5b5;
}

/* Overwrite-confirmation modal — see scan-exams.html and the onCommitAll
   flow in scan-exams.js. Mobile-first: full-width card with a backdrop. */
.scan-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.scan-modal[hidden] { display: none; }
.scan-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
}
.scan-modal-card {
  position: relative;
  max-width: 480px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--scan-bg, #fff);
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.25);
}
.scan-modal-card h2 { margin: 0 0 8px; font-size: 1.2rem; }
.scan-modal-card p { margin: 0 0 12px; color: #444; }
.scan-overwrite-list {
  list-style: none;
  padding: 0;
  margin: 0 0 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.scan-overwrite-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid #e2e2e2;
  border-radius: 8px;
}
.scan-overwrite-row input[type="checkbox"] { width: 22px; height: 22px; flex-shrink: 0; }
.scan-overwrite-row-meta { flex: 1; }
.scan-overwrite-row-name { font-weight: 600; }
.scan-overwrite-row-when { font-size: 0.85rem; color: #666; }
.scan-overwrite-actions { display: flex; gap: 12px; justify-content: flex-end; }

.scan-review-footer {
  position: sticky;
  bottom: 0;
  background: linear-gradient(to top, var(--scan-bg), var(--scan-bg) 60%, transparent);
  padding: 1rem 0 1.5rem;
  text-align: center;
  margin-top: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.scan-review-footer .scan-btn {
  width: 100%;
}

/* -------------------------------------------------------------- detail */

.scan-step-detail {
  padding: 1rem 0.5rem 4rem;
}

.scan-detail-meta {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  margin: 1rem 0;
}

.scan-detail-meta-row {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.scan-detail-meta-row label {
  font-size: 0.85rem;
  font-weight: 600;
}

.scan-detail-diag {
  font-size: 0.75rem;
  color: var(--scan-muted);
  margin: 0.4rem 0;
  line-height: 1.4;
  word-break: break-word;
}


.scan-mismatch {
  background: #fff3cd;
  border: 1px solid #ffe7a8;
  border-radius: 12px;
  padding: 0.85rem 1rem;
  font-size: 0.9rem;
  color: #8a6500;
  margin-bottom: 1rem;
}

.scan-detail-image-wrap {
  position: relative;
  width: 100%;
  margin: 1rem 0;
  background: #1a1a2e;
  border-radius: 12px;
  overflow: hidden;
}

#scan-detail-image {
  display: block;
  width: 100%;
  height: auto;
}

.scan-detail-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.scan-detail-overlay circle {
  fill: rgba(34, 197, 94, 0.25);
  stroke: rgba(34, 197, 94, 0.95);
  stroke-width: 2;
  cursor: pointer;
}

.scan-detail-overlay circle.scan-bubble-empty {
  fill: transparent;
  stroke: rgba(255, 255, 255, 0.5);
  stroke-width: 1;
}

/* Graded overlay — Handoff G */
.scan-detail-overlay circle.scan-bubble-right {
  fill: rgba(34, 197, 94, 0.3);
  stroke: #22c55e;
  stroke-width: 3;
}
.scan-detail-overlay circle.scan-bubble-wrong {
  fill: rgba(239, 68, 68, 0.25);
  stroke: #ef4444;
  stroke-width: 3;
}
.scan-detail-overlay circle.scan-bubble-key {
  fill: rgba(21, 128, 61, 0.2);
  stroke: #15803d;
  stroke-width: 3;
  stroke-dasharray: 4 3;
}
.scan-detail-overlay circle.scan-bubble-override {
  fill: rgba(245, 158, 11, 0.3);
  stroke: #f59e0b;
  stroke-width: 3;
}

/* Teacher override badge in answers list */
.scan-row-edited-badge {
  display: inline-block;
  font-size: 0.7rem;
  background: #f59e0b;
  color: #fff;
  border-radius: 4px;
  padding: 0.1rem 0.4rem;
  margin-left: 0.35rem;
  font-weight: 600;
  vertical-align: middle;
}

/* SVG "edited by teacher" badge in the overlay */
.scan-row-edited {
  fill: #f59e0b;
  font-size: 18px;
  font-family: 'Lexend Deca', system-ui, sans-serif;
  font-weight: 600;
}

/* Ambiguous (multi-bubble) row highlight in SVG overlay */
.scan-detail-overlay rect.scan-row-ambiguous {
  fill: rgba(245, 158, 11, 0.08);
  stroke: #f59e0b;
  stroke-width: 2;
  rx: 8;
  ry: 8;
}

/* Ambiguous (multi-bubble) row highlight in HTML answer list (future) */
.scan-row-ambiguous {
  background: #fff8e1;
  border-left: 3px solid #f59e0b;
  padding: 0.5rem 0.75rem;
  border-radius: 6px;
}

/* Answer list in detail view */
.scan-detail-answers {
  margin: 1rem 0;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.scan-detail-answer-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0.75rem;
  background: var(--scan-card);
  border: 1px solid var(--scan-border);
  border-radius: 10px;
  font-size: 0.9rem;
}

.scan-detail-answer-row .answer-label {
  font-weight: 600;
  min-width: 3rem;
  color: var(--scan-muted);
}

.scan-detail-answer-row .answer-status {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.12rem 0.45rem;
  border-radius: 4px;
}
.scan-detail-answer-row .answer-status.right {
  background: #ddf6e4;
  color: #117a3a;
}
.scan-detail-answer-row .answer-status.wrong {
  background: #fde2e2;
  color: #a31616;
}

.scan-detail-answer-row input.scan-answer-edit {
  flex: 1;
  padding: 0.35rem 0.6rem;
  font-family: inherit;
  font-size: 0.9rem;
  border: 1px solid var(--scan-border);
  border-radius: 6px;
  background: var(--scan-bg);
}
.scan-detail-answer-row input.scan-answer-edit:focus {
  outline: 2px solid var(--scan-primary);
  border-color: transparent;
}

/* Text answer editing section (char-box / free-form) */
.scan-answer-edits {
  margin: 1.5rem 0;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}
.scan-answer-edits h3 {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--scan-primary);
  margin: 0 0 0.25rem;
}

.scan-answer-edit-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 0.75rem;
  background: var(--scan-card);
  border: 1px solid var(--scan-border);
  border-radius: 10px;
  font-size: 0.9rem;
}

.scan-answer-edit-row-edited {
  border-color: #f59e0b;
  background: #fffcf0;
}

.scan-answer-edit-label {
  font-weight: 600;
  min-width: 3rem;
  color: var(--scan-muted);
  white-space: nowrap;
}

.scan-answer-edit-row input[type="text"] {
  flex: 1;
  padding: 0.35rem 0.6rem;
  font-family: inherit;
  font-size: 0.9rem;
  border: 1px solid var(--scan-border);
  border-radius: 6px;
  background: var(--scan-bg);
}
.scan-answer-edit-row input[type="text"]:focus {
  outline: 2px solid var(--scan-primary);
  border-color: transparent;
}

.scan-answer-key {
  font-size: 0.75rem;
  color: #15803d;
  font-weight: 600;
  white-space: nowrap;
}

.scan-detail-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
  margin-top: 1rem;
}

/* ------------------------------------------------------------- responsive */

@media (min-width: 600px) {
  .scan-tile-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  .scan-tile-grid > .scan-tile:first-child:nth-last-child(3),
  .scan-tile-grid > .scan-tile:first-child:nth-last-child(3) ~ .scan-tile {
    grid-column: span 1;
  }
}
