:root {
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --bg-main: #f1f5f9;
  --bg-sidebar: #0f172a;
  --text-main: #1e293b;
  --text-muted: #64748b;
  --white: #ffffff;
  --border: #e2e8f0;
  --radius: 10px;
  --shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --success: #10b981;
  --error: #ef4444;
  --warning: #f59e0b;
}

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

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg-main);
  color: var(--text-main);
  display: flex;
  min-height: 100vh;
}

body.unauthenticated aside { display: none; }
body.unauthenticated main { padding: 0; }

/* ── Sidebar ─────────────────────────────────────────────────────────────── */
aside {
  width: 240px;
  background: var(--bg-sidebar);
  color: #fff;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  height: 100vh;
}
.sidebar-header { padding: 28px 20px 20px; border-bottom: 1px solid rgba(255,255,255,0.08); }
.sidebar-header h1 {
  font-size: 18px; font-weight: 700; letter-spacing: -0.5px;
  background: linear-gradient(to right, #818cf8, #c084fc);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent;
}
.sidebar-nav { padding: 16px 10px; flex-grow: 1; }
.tab-btn {
  width: 100%; text-align: left; background: none; border: none;
  color: #94a3b8; padding: 10px 14px; border-radius: 8px; cursor: pointer;
  font-size: 13px; font-weight: 500; display: flex; align-items: center;
  gap: 10px; transition: all 0.15s; margin-bottom: 2px;
}
.tab-btn:hover { background: rgba(255,255,255,0.06); color: #e2e8f0; }
.tab-btn.active { background: var(--primary); color: #fff; }
.sidebar-footer { padding: 16px; border-top: 1px solid rgba(255,255,255,0.08); }

/* ── Main ────────────────────────────────────────────────────────────────── */
main { flex-grow: 1; padding: 36px; overflow-y: auto; }
.tab { display: none; animation: fadeIn 0.2s ease; }
.tab.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; } }
h2 { font-size: 22px; font-weight: 700; margin-bottom: 24px; color: #0f172a; }

/* ── Cards ───────────────────────────────────────────────────────────────── */
.card {
  background: var(--white); border-radius: var(--radius);
  padding: 24px; box-shadow: var(--shadow); border: 1px solid var(--border);
  margin-bottom: 20px;
}
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; margin-bottom: 20px; }
.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group label { font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
input[type="text"], input[type="password"], input[type="file"], textarea {
  padding: 9px 13px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 14px; transition: border-color 0.15s; width: 100%; background: #fff;
}
input:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79,70,229,0.1); }
button.primary { background: var(--primary); color: #fff; border: none; padding: 10px 22px; border-radius: 8px; font-weight: 600; cursor: pointer; font-size: 14px; transition: background 0.15s; }
button.primary:hover { background: var(--primary-hover); }
.hidden { display: none !important; }
.hint { color: var(--text-muted); font-size: 14px; }

/* ── Badges ──────────────────────────────────────────────────────────────── */
.badge { padding: 3px 9px; border-radius: 9999px; font-size: 11px; font-weight: 600; }
.status-complete, .status-COMPLETE { background: #d1fae5; color: #065f46; }
.status-failed, .status-FAILED { background: #fee2e2; color: #991b1b; }
.status-pending, .status-PENDING, .status-QUEUED, .status-PROCESSING { background: #dbeafe; color: #1e40af; }

/* ── Login ───────────────────────────────────────────────────────────────── */
.login-page { display: flex; align-items: center; justify-content: center; width: 100%; min-height: 100vh; }
.login-card { width: 100%; max-width: 380px; padding: 40px; }
.login-header { text-align: center; margin-bottom: 28px; }
body.unauthenticated #app-view { display: none !important; }
body:not(.unauthenticated) #login-view { display: none !important; }

/* ── Dashboard table ─────────────────────────────────────────────────────── */
.dashboard-table-container { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--border); }
.dashboard-table { width: 100%; border-collapse: collapse; background: #fff; }
.dashboard-table th { background: #f8fafc; padding: 13px 16px; text-align: left; font-size: 12px; font-weight: 600; color: var(--text-muted); border-bottom: 1px solid var(--border); text-transform: uppercase; letter-spacing: 0.4px; }
.dashboard-table td { padding: 14px 16px; border-bottom: 1px solid var(--border); font-size: 13px; vertical-align: middle; }
.dashboard-table tr:last-child td { border-bottom: none; }
.dashboard-table tr:hover td { background: #f8fafc; }
.btn-icon { background: none; border: 1px solid var(--border); cursor: pointer; padding: 5px 10px; border-radius: 6px; font-size: 12px; color: var(--text-muted); transition: all 0.15s; }
.btn-icon:hover { background: var(--primary); color: #fff; border-color: var(--primary); }
.result-box { margin-top: 16px; padding: 14px; border-radius: 8px; font-family: ui-monospace, Menlo, monospace; font-size: 12px; white-space: pre-wrap; background: #f1f5f9; }
.result-box.error { background: #fef2f2; color: #dc2626; border-left: 3px solid #dc2626; }
.result-box.success { background: #f0fdf4; color: #16a34a; border-left: 3px solid #16a34a; }
.progress-bar { margin-top: 12px; background: #e2e8f0; border-radius: 99px; height: 6px; overflow: hidden; }
#status-progress-fill { height: 100%; background: var(--primary); border-radius: 99px; transition: width 0.4s; }

/* ══════════════════════════════════════════════════════════════════════════
   REPORT OVERLAY
   ══════════════════════════════════════════════════════════════════════════ */

#report-overlay {
  position: fixed; inset: 0; background: #f8fafc;
  z-index: 1000; overflow-y: auto; display: flex; flex-direction: column;
}

.report-toolbar {
  position: sticky; top: 0; z-index: 10;
  background: #0f172a; padding: 14px 32px;
  display: flex; align-items: center; gap: 12px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}
.report-toolbar-title { flex-grow: 1; color: #94a3b8; font-size: 13px; font-weight: 500; }
.btn-back { background: rgba(255,255,255,0.08); color: #e2e8f0; border: none; padding: 8px 16px; border-radius: 7px; cursor: pointer; font-size: 13px; font-weight: 500; display: flex; align-items: center; gap: 6px; transition: background 0.15s; }
.btn-back:hover { background: rgba(255,255,255,0.14); }
.btn-print { background: var(--primary); color: #fff; border: none; padding: 8px 16px; border-radius: 7px; cursor: pointer; font-size: 13px; font-weight: 600; transition: background 0.15s; }
.btn-print:hover { background: var(--primary-hover); }
.btn-generate { background: #f59e0b; color: #fff; border: none; padding: 8px 16px; border-radius: 7px; cursor: pointer; font-size: 13px; font-weight: 600; transition: background 0.15s; }
.btn-generate:hover { background: #d97706; }

/* Report page layout */
.report-page {
  max-width: 860px; margin: 32px auto 64px; padding: 0 24px;
}

/* ── Report header ─────────────────────────────────────────────────────── */
.report-header {
  background: #fff; border: 1px solid var(--border); border-radius: 14px;
  padding: 32px 36px; margin-bottom: 20px; box-shadow: var(--shadow);
}
.report-header-top {
  display: flex; justify-content: space-between; align-items: flex-start;
  margin-bottom: 28px;
}
.report-meta h1 { font-size: 22px; font-weight: 700; color: #0f172a; margin-bottom: 6px; }
.report-meta p { font-size: 13px; color: var(--text-muted); line-height: 1.6; }
.report-meta .meta-row { display: flex; gap: 20px; flex-wrap: wrap; }
.meta-chip { display: flex; align-items: center; gap: 5px; font-size: 12px; color: var(--text-muted); }
.meta-chip strong { color: var(--text-main); }

/* Score ring */
.score-ring-wrap { display: flex; flex-direction: column; align-items: center; gap: 8px; flex-shrink: 0; }
.score-ring { position: relative; width: 110px; height: 110px; }
.score-ring svg { transform: rotate(-90deg); }
.score-ring-inner { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.score-pct { font-size: 24px; font-weight: 800; line-height: 1; }
.score-grade { font-size: 12px; font-weight: 700; letter-spacing: 1px; text-transform: uppercase; margin-top: 2px; }
.score-raw { font-size: 13px; color: var(--text-muted); font-weight: 500; }

/* Score bar */
.score-bar-wrap { display: flex; align-items: center; gap: 12px; }
.score-bar-track { flex-grow: 1; background: #e2e8f0; border-radius: 99px; height: 8px; overflow: hidden; }
.score-bar-fill { height: 100%; border-radius: 99px; transition: width 0.8s ease; }
.score-bar-label { font-size: 12px; color: var(--text-muted); white-space: nowrap; }

/* ── Teacher narrative block ───────────────────────────────────────────── */
.teacher-narrative {
  background: #fff; border: 1px solid var(--border); border-radius: 14px;
  padding: 28px 32px; margin-bottom: 20px; box-shadow: var(--shadow);
}
.teacher-narrative h2 {
  font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 1px;
  color: var(--text-muted); margin-bottom: 14px;
}
.narrative-text {
  font-size: 15px; line-height: 1.75; color: #334155;
  border-left: 3px solid var(--primary); padding-left: 18px;
  font-style: italic;
}
.teacher-cols {
  display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-top: 20px;
}
.teacher-col h3 { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 1px; color: var(--text-muted); margin-bottom: 8px; }
.teacher-col p { font-size: 14px; line-height: 1.6; color: var(--text-main); }
.strength-col { border-left: 3px solid #10b981; padding-left: 14px; }
.focus-col { border-left: 3px solid #f59e0b; padding-left: 14px; }

/* ── Study plan ──────────────────────────────────────────────────────────── */
.study-plan {
  background: #fff; border: 1px solid var(--border); border-radius: 14px;
  padding: 24px 32px; margin-bottom: 20px; box-shadow: var(--shadow);
}
.study-plan h2 { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 1px; color: var(--text-muted); margin-bottom: 14px; }
.study-plan ol { padding-left: 0; list-style: none; display: flex; flex-direction: column; gap: 10px; }
.study-plan li {
  display: flex; align-items: flex-start; gap: 12px;
  font-size: 14px; line-height: 1.6; color: var(--text-main); padding: 12px 16px;
  background: #f8fafc; border-radius: 8px; border: 1px solid var(--border);
}
.study-plan .step-num {
  flex-shrink: 0; width: 24px; height: 24px; background: var(--primary); color: #fff;
  border-radius: 50%; font-size: 12px; font-weight: 700; display: flex; align-items: center; justify-content: center;
}

/* ── Report section ──────────────────────────────────────────────────────── */
.report-section {
  background: #fff; border: 1px solid var(--border); border-radius: 14px;
  margin-bottom: 20px; box-shadow: var(--shadow); overflow: hidden;
}
.section-header {
  padding: 18px 28px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 16px;
  background: #f8fafc;
}
.section-title { font-size: 14px; font-weight: 700; color: #0f172a; flex-grow: 1; }
.section-score { font-size: 13px; font-weight: 600; color: var(--text-muted); }
.section-badge { font-size: 11px; font-weight: 700; padding: 3px 10px; border-radius: 99px; }
.section-badge.good { background: #d1fae5; color: #065f46; }
.section-badge.poor { background: #fee2e2; color: #991b1b; }
.section-badge.mixed { background: #fef3c7; color: #92400e; }

/* ── Question row ────────────────────────────────────────────────────────── */
.question-row {
  border-bottom: 1px solid var(--border); padding: 16px 28px;
  display: flex; gap: 16px; align-items: flex-start;
}
.question-row:last-child { border-bottom: none; }
.question-row.correct { border-left: 3px solid #10b981; }
.question-row.incorrect { border-left: 3px solid #ef4444; }

.q-status-icon {
  flex-shrink: 0; width: 28px; height: 28px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 700; margin-top: 1px;
}
.q-status-icon.correct { background: #d1fae5; color: #065f46; }
.q-status-icon.incorrect { background: #fee2e2; color: #991b1b; }

.q-body { flex-grow: 1; min-width: 0; }
.q-header { display: flex; align-items: baseline; gap: 10px; margin-bottom: 6px; flex-wrap: wrap; }
.q-id { font-size: 14px; font-weight: 700; color: #0f172a; }
.q-topic { font-size: 11px; color: var(--text-muted); background: #f1f5f9; padding: 2px 8px; border-radius: 99px; }
.q-skill { font-size: 11px; color: var(--text-muted); }

.q-answer-row { display: flex; gap: 20px; margin-bottom: 8px; flex-wrap: wrap; }
.q-answer-chip { font-size: 12px; }
.q-answer-chip .label { color: var(--text-muted); margin-right: 4px; }
.q-answer-chip .value { font-weight: 600; color: var(--text-main); }
.q-answer-chip .value.wrong { color: #ef4444; }
.q-answer-chip .value.right { color: #10b981; }

.q-teacher-comment {
  font-size: 13px; line-height: 1.65; color: #475569;
  background: #f8fafc; padding: 10px 14px; border-radius: 8px; margin-bottom: 6px;
}
.q-tip {
  font-size: 12px; color: #92400e; background: #fffbeb; padding: 8px 12px;
  border-radius: 6px; border-left: 3px solid #f59e0b; line-height: 1.5;
}
.q-tip strong { display: block; margin-bottom: 2px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; color: #b45309; }

.q-score { flex-shrink: 0; text-align: right; }
.q-marks { font-size: 16px; font-weight: 800; }
.q-marks.full { color: #10b981; }
.q-marks.zero { color: #ef4444; }
.q-max { font-size: 11px; color: var(--text-muted); margin-top: 1px; }

/* ── Collapsed MCQ grid (for correct-only sections) ──────────────────── */
.mcq-summary-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(52px, 1fr)); gap: 8px; padding: 20px 28px;
}
.mcq-chip {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 8px 4px; border-radius: 8px; border: 1px solid var(--border);
  font-size: 11px; gap: 4px; cursor: default;
}
.mcq-chip.correct { background: #f0fdf4; border-color: #bbf7d0; }
.mcq-chip.incorrect { background: #fef2f2; border-color: #fecaca; }
.mcq-chip .cid { font-weight: 700; color: #0f172a; }
.mcq-chip .cmark { font-size: 13px; }

/* ── No report placeholder ─────────────────────────────────────────────── */
.no-report-banner {
  background: #fffbeb; border: 1px solid #fcd34d; border-radius: 10px;
  padding: 20px 24px; display: flex; align-items: center; justify-content: space-between;
  gap: 16px; margin-bottom: 20px;
}
.no-report-banner p { font-size: 14px; color: #92400e; }

/* ── Step analysis (written questions) ──────────────────────────────────── */
.step-list { margin-top: 10px; border-top: 1px solid var(--border); padding-top: 10px; }
.step-row {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 7px 0; border-bottom: 1px solid #f1f5f9; font-size: 12px;
}
.step-row:last-child { border-bottom: none; }
.step-num {
  flex-shrink: 0; width: 20px; height: 20px; border-radius: 50%;
  background: #e2e8f0; color: #475569; font-size: 10px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}
.step-body { flex-grow: 1; min-width: 0; }
.step-desc { font-weight: 600; color: #334155; margin-bottom: 2px; }
.step-work { font-family: monospace; font-size: 11px; color: #475569; background: #f8fafc; padding: 3px 7px; border-radius: 4px; margin-bottom: 3px; white-space: pre-wrap; word-break: break-word; }
.step-feedback { color: var(--text-muted); line-height: 1.5; }
.step-mark {
  flex-shrink: 0; font-size: 11px; font-weight: 700; white-space: nowrap;
  padding: 2px 7px; border-radius: 99px; align-self: flex-start; margin-top: 1px;
}
.step-mark.correct { background: #d1fae5; color: #065f46; }
.step-mark.zero    { background: #f1f5f9; color: #64748b; }
.step-mark.wrong   { background: #fee2e2; color: #991b1b; }

/* ══════════════════════════════════════════════════════════════════════════
   ARCHIVE TAB
   ══════════════════════════════════════════════════════════════════════════ */

.archive-filters {
  display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end;
}
.filter-group {
  display: flex; flex-direction: column; gap: 5px; min-width: 110px;
}
.filter-group-grow { flex-grow: 1; }
.filter-group label {
  font-size: 11px; font-weight: 600; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.5px;
}
.filter-group select {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 13px; background: #fff; color: var(--text-main);
  cursor: pointer; transition: border-color 0.15s;
}
.filter-group select:focus { outline: none; border-color: var(--primary); }

.archive-pagination {
  display: flex; align-items: center; gap: 12px; justify-content: center;
  margin-top: 16px; font-size: 13px; color: var(--text-muted);
}
.archive-pagination button {
  padding: 6px 14px; border: 1px solid var(--border); border-radius: 6px;
  background: #fff; cursor: pointer; font-size: 13px; transition: all 0.15s;
}
.archive-pagination button:hover:not(:disabled) { background: var(--primary); color: #fff; border-color: var(--primary); }
.archive-pagination button:disabled { opacity: 0.4; cursor: default; }

/* Ingestion-specific status badges */
.status-chunked   { background: #d1fae5; color: #065f46; }
.status-stored    { background: #dbeafe; color: #1e40af; }
.status-downloaded { background: #fef3c7; color: #92400e; }
.status-extracted { background: #e0e7ff; color: #3730a3; }
.status-running   { background: #dbeafe; color: #1e40af; }
.status-completed { background: #d1fae5; color: #065f46; }
.status-partial   { background: #fef3c7; color: #92400e; }

/* Doc type chips */
.doc-type-chip {
  display: inline-block; font-size: 10px; font-weight: 600; padding: 2px 7px;
  border-radius: 4px; letter-spacing: 0.3px; text-transform: uppercase;
}
.doc-type-question_paper { background: #ede9fe; color: #5b21b6; }
.doc-type-specimen_paper { background: #e0f2fe; color: #075985; }
.doc-type-syllabus       { background: #fce7f3; color: #9d174d; }

/* ══════════════════════════════════════════════════════════════════════════
   PRINT STYLES
   ══════════════════════════════════════════════════════════════════════════ */
@media print {
  /* Force colour rendering */
  * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }

  /* Reset body from flex/viewport-height layout to normal document flow */
  html, body {
    display: block !important;
    height: auto !important;
    min-height: unset !important;
    overflow: visible !important;
    background: #fff !important;
  }

  /* Hide everything except the report overlay */
  body > *:not(#report-overlay) { display: none !important; }

  /* Pull the overlay out of fixed/scrolling positioning */
  #report-overlay {
    position: static !important;
    inset: auto !important;
    z-index: auto !important;
    overflow: visible !important;
    display: block !important;
    height: auto !important;
    min-height: unset !important;
    background: #fff !important;
  }

  .report-toolbar,
  #btn-generate-report,
  .no-report-banner { display: none !important; }

  .report-page { max-width: 100% !important; margin: 0 !important; padding: 12px 20px !important; }

  /* Remove overflow constraints that clip content */
  .report-section,
  .mcq-summary-grid,
  .dashboard-table-container { overflow: visible !important; }

  /* Flatten visuals */
  .report-header, .teacher-narrative, .study-plan, .report-section {
    box-shadow: none !important; border: 1px solid #ccc !important;
  }

  /* Page-break rules */
  .report-header       { break-after: avoid; }
  .teacher-narrative   { break-inside: avoid; }
  .study-plan          { break-inside: avoid; }
  .section-header      { break-after: avoid; }
  .question-row        { break-inside: avoid; }
  .report-section      { break-inside: auto; }  /* long sections may span pages */
}
