/* ============ RESET & BASE ============ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow-x: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #f3f4f6;
  color: #111827;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input, select { font-family: inherit; font-size: inherit; }
button { cursor: pointer; border: none; background: none; }
.hidden { display: none !important; }
.small { font-size: 0.85rem; color: #6b7280; }
a { color: #1e40af; }

/* ============ TOP BAR ============ */
.topbar {
  position: sticky; top: 0; z-index: 50;
  display: flex; align-items: center; gap: 0.5rem;
  background: #1e40af; color: #fff;
  padding: calc(env(safe-area-inset-top) * 0.5 + 0.5rem) 0.75rem 0.5rem;
  min-height: 3.5rem;
  box-shadow: 0 2px 6px rgba(0,0,0,.1);
}
.topbar h1 { flex: 1; font-size: 1.1rem; font-weight: 600; text-align: center; }
.icon-btn {
  width: 2.25rem; height: 2.25rem; border-radius: 50%;
  background: rgba(255,255,255,.15); color: #fff; font-size: 1.1rem;
  display: grid; place-items: center;
}
.icon-btn:active { background: rgba(255,255,255,.3); }

/* ============ PAGES ============ */
.page { display: none; padding: 0.75rem; animation: fadeIn .2s; }
.page.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; } }

/* ============ HOME ============ */
.hero { text-align: center; padding: 1.5rem 0 1rem; }
.logo { font-size: 3.5rem; margin-bottom: 0.5rem; }
.hero h2 { font-size: 1.5rem; }
.subtitle { color: #6b7280; margin-top: 0.25rem; }
.home-grid {
  display: grid; grid-template-columns: repeat(2, 1fr);
  gap: 0.75rem; margin-top: 1rem;
}
.home-card {
  background: #fff; border-radius: 14px; padding: 1.25rem 0.5rem;
  display: flex; flex-direction: column; align-items: center; gap: 0.5rem;
  box-shadow: 0 2px 6px rgba(0,0,0,.05);
  transition: transform .1s;
}
.home-card:active { transform: scale(0.97); }
.home-card .icon { font-size: 2rem; }
.home-card.primary { background: linear-gradient(135deg,#1e40af,#3b82f6); color: #fff; }
.home-card .label { font-weight: 500; }

.status-bar {
  margin-top: 1rem; background: #fff; border-radius: 12px;
  padding: 0.75rem; display: flex; justify-content: space-around;
  box-shadow: 0 2px 6px rgba(0,0,0,.05);
}
.status-item { display: flex; align-items: center; gap: 0.4rem; font-size: 0.85rem; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #9ca3af; }
.dot.ok { background: #10b981; }
.dot.warn { background: #f59e0b; }
.dot.err { background: #ef4444; }

/* ============ CAMERA / SCANNER ============ */
.camera-container {
  position: relative; width: 100%; height: calc(100vh - 4rem);
  background: #000; overflow: hidden; margin: -0.75rem;
}
#camera-preview { width: 100%; height: 100%; object-fit: cover; }
.scan-overlay {
  position: absolute; inset: 0; pointer-events: none;
  display: flex; flex-direction: column; justify-content: space-between;
  padding: 1rem;
}
.scan-frame {
  position: absolute; top: 15%; left: 8%; right: 8%; bottom: 30%;
  border: 2px solid rgba(255,255,255,.3); border-radius: 8px;
}
.corner {
  position: absolute; width: 28px; height: 28px;
  border: 3px solid #22d3ee;
}
.corner.tl { top: -2px; left: -2px; border-right: 0; border-bottom: 0; border-top-left-radius: 8px; }
.corner.tr { top: -2px; right: -2px; border-left: 0; border-bottom: 0; border-top-right-radius: 8px; }
.corner.bl { bottom: -2px; left: -2px; border-right: 0; border-top: 0; border-bottom-left-radius: 8px; }
.corner.br { bottom: -2px; right: -2px; border-left: 0; border-top: 0; border-bottom-right-radius: 8px; }
.scan-instructions {
  position: absolute; top: 5%; left: 0; right: 0; text-align: center;
  color: #fff; background: rgba(0,0,0,.4); padding: 0.5rem;
}
.scan-instructions p { margin: 2px 0; }
.scan-status {
  position: absolute; top: 45%; left: 50%; transform: translate(-50%,-50%);
  background: rgba(0,0,0,.6); color: #fff; padding: 0.5rem 1rem;
  border-radius: 20px; font-size: 0.9rem;
}
.camera-controls {
  position: absolute; bottom: 2rem; left: 0; right: 0;
  display: flex; justify-content: center; align-items: center; gap: 2rem;
}
.round-btn {
  width: 3rem; height: 3rem; border-radius: 50%;
  background: rgba(255,255,255,.2); color: #fff; font-size: 1.25rem;
  display: grid; place-items: center;
  backdrop-filter: blur(8px);
}
.round-btn.large { width: 4.5rem; height: 4.5rem; background: #fff; color: #000; font-size: 1.75rem; border: 4px solid #22d3ee; }
.round-btn:active { transform: scale(0.93); }
.link-btn {
  position: absolute; bottom: 0.5rem; left: 0; right: 0;
  color: #fff; text-align: center; padding: 0.5rem; font-size: 0.9rem;
  text-decoration: underline;
}

/* ============ UPLOAD ============ */
.upload-zone {
  margin-top: 2rem; padding: 3rem 1rem; text-align: center;
  border: 2px dashed #9ca3af; border-radius: 12px; background: #fff;
}
.upload-zone.dragover { border-color: #1e40af; background: #eff6ff; }
.upload-icon { font-size: 3rem; }

/* ============ CARD / FORM ============ */
.card {
  background: #fff; border-radius: 14px; padding: 1rem;
  margin-bottom: 0.75rem; box-shadow: 0 2px 6px rgba(0,0,0,.05);
}
.card h3 { margin-bottom: 0.75rem; font-size: 1.1rem; }
.form-group { margin-bottom: 0.75rem; }
.form-group label { display: block; font-size: 0.85rem; color: #374151; margin-bottom: 0.25rem; font-weight: 500; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; }
.input {
  width: 100%; padding: 0.7rem; border: 1px solid #d1d5db;
  border-radius: 8px; background: #fff;
}
.input:focus { outline: 2px solid #3b82f6; border-color: transparent; }
.btn {
  padding: 0.75rem 1rem; border-radius: 10px; font-weight: 500;
  transition: opacity .15s, transform .1s;
}
.btn:active { transform: scale(0.98); }
.btn.primary { background: #1e40af; color: #fff; }
.btn.secondary { background: #e5e7eb; color: #111; }
.btn.danger { background: #ef4444; color: #fff; }
.btn.full { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.checkbox { display: flex; align-items: center; gap: 0.5rem; padding: 0.5rem 0; cursor: pointer; }
.checkbox input { width: 1.2rem; height: 1.2rem; }

/* ============ REVIEW ============ */
.review-container { padding-bottom: 6rem; }
.review-card { background: #fff; border-radius: 14px; padding: 1rem; margin-bottom: 0.75rem; box-shadow: 0 2px 6px rgba(0,0,0,.05); }
.meta-block { font-size: 0.9rem; line-height: 1.6; margin-bottom: 0.75rem; color: #374151; }
.meta-block strong { color: #111827; }
.score-summary {
  background: linear-gradient(135deg,#1e40af,#3b82f6);
  color: #fff; padding: 1rem; border-radius: 12px;
  text-align: center; margin-bottom: 0.75rem;
}
.score-summary .score { font-size: 2rem; font-weight: 700; }
.score-summary .label { font-size: 0.85rem; opacity: 0.9; }
.stats-row { display: grid; grid-template-columns: repeat(2,1fr); gap: 0.5rem; margin-top: 0.5rem; font-size: 0.85rem; }
.stats-row div { background: rgba(255,255,255,.15); padding: 0.4rem; border-radius: 6px; }

.tabs { display: flex; gap: 0.5rem; margin-bottom: 0.75rem; overflow-x: auto; }
.tab {
  padding: 0.4rem 0.8rem; border-radius: 20px;
  background: #f3f4f6; color: #374151; font-size: 0.85rem; white-space: nowrap;
}
.tab.active { background: #1e40af; color: #fff; }

.answers-list { display: flex; flex-direction: column; gap: 0.4rem; }
.ans-item {
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.6rem 0.75rem; border-radius: 8px; background: #f9fafb;
  cursor: pointer; border-left: 4px solid #9ca3af;
}
.ans-item.correct { border-left-color: #10b981; background: #ecfdf5; }
.ans-item.wrong { border-left-color: #ef4444; background: #fef2f2; }
.ans-item.unanswered { border-left-color: #9ca3af; background: #f9fafb; }
.ans-item.multi { border-left-color: #f59e0b; background: #fffbeb; }
.ans-item.low { border-left-color: #f97316; }
.ans-q { font-weight: 600; width: 2rem; }
.ans-detected { flex: 1; font-family: monospace; }
.ans-correct { font-size: 0.8rem; color: #6b7280; }
.ans-status { font-size: 1.1rem; }
.review-actions {
  position: fixed; bottom: 0; left: 0; right: 0;
  padding: 0.75rem; background: #fff; border-top: 1px solid #e5e7eb;
  display: flex; flex-direction: column; gap: 0.5rem;
  padding-bottom: calc(0.75rem + env(safe-area-inset-bottom));
  box-shadow: 0 -2px 6px rgba(0,0,0,.05);
}

/* ============ MODAL ============ */
.modal {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,.5); display: grid; place-items: center;
  padding: 1rem;
}
.modal-content {
  background: #fff; border-radius: 14px; padding: 1.25rem;
  width: 100%; max-width: 420px; max-height: 90vh; overflow-y: auto;
}
.edit-options {
  display: grid; grid-template-columns: repeat(4,1fr); gap: 0.5rem;
  margin: 0.75rem 0;
}
.edit-opt {
  padding: 0.75rem; border-radius: 8px; border: 2px solid #d1d5db;
  text-align: center; font-weight: 600;
}
.edit-opt.selected { border-color: #1e40af; background: #eff6ff; color: #1e40af; }
.modal-actions { display: flex; gap: 0.5rem; margin-top: 0.75rem; }
.modal-actions .btn { flex: 1; }

/* ============ STATS GRID ============ */
.stats-grid {
  display: grid; grid-template-columns: repeat(2,1fr); gap: 0.5rem;
  margin-bottom: 0.75rem;
}
.stat-card {
  background: #fff; border-radius: 12px; padding: 0.75rem;
  box-shadow: 0 2px 6px rgba(0,0,0,.05);
}
.stat-value { font-size: 1.5rem; font-weight: 700; color: #1e40af; }
.stat-label { font-size: 0.8rem; color: #6b7280; }

/* ============ LOADER / TOAST ============ */
.loader {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,.7); display: grid; place-items: center; color: #fff;
  gap: 1rem;
}
.loader:not(.hidden) { display: flex; flex-direction: column; }
.spinner {
  width: 3rem; height: 3rem; border: 4px solid rgba(255,255,255,.2);
  border-top-color: #fff; border-radius: 50%;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
  position: fixed; bottom: calc(env(safe-area-inset-bottom) + 1rem);
  left: 50%; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 0.75rem 1.25rem;
  border-radius: 24px; z-index: 300; max-width: 90%;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
  font-size: 0.9rem; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }
.toast.error { background: #dc2626; }
.toast.success { background: #059669; }

/* ============ DEBUG ============ */
.debug-stage {
  background: #fff; border-radius: 12px; padding: 0.75rem;
  margin-bottom: 0.75rem; box-shadow: 0 2px 6px rgba(0,0,0,.05);
}
.debug-stage h4 { margin-bottom: 0.5rem; font-size: 0.95rem; }
.debug-stage canvas, .debug-stage img { width: 100%; height: auto; display: block; border-radius: 8px; }
.debug-answers { font-family: monospace; font-size: 0.8rem; line-height: 1.6; white-space: pre-wrap; }

/* ============ SEARCH RESULTS ============ */
.result-card {
  background: #fff; border-radius: 12px; padding: 0.75rem;
  margin-bottom: 0.5rem; box-shadow: 0 2px 6px rgba(0,0,0,.05);
}

/* ============ UTILS ============ */
@media (min-width: 768px) {
  .home-grid { grid-template-columns: repeat(3,1fr); }
  main { max-width: 640px; margin: 0 auto; }
}
