/* ── Reset & tokens ─────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary:  #4F8EF7;
  --success:  #34C759;
  --danger:   #FF3B30;
  --warning:  #FF9500;
  --bg:       #F2F2F7;
  --card:     #ffffff;
  --border:   #D1D1D6;
  --text:     #1C1C1E;
  --muted:    #8E8E93;
  --radius:   10px;
  --shadow:   0 1px 4px rgba(0,0,0,.10);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, sans-serif;
  background: var(--bg); color: var(--text); font-size: 15px; min-height: 100vh;
}

/* ── Header ─────────────────────────────────────────────────────────────── */
header {
  background: var(--card); border-bottom: 1px solid var(--border);
  padding: 10px 16px 0; position: sticky; top: 0; z-index: 100;
}
.header-top {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 8px; gap: 10px; flex-wrap: wrap;
}
header h1 { font-size: 18px; font-weight: 700; }

.key-badge {
  font-size: 12px; padding: 3px 10px; border-radius: 12px;
  border: 1px solid var(--border); color: var(--muted); background: var(--bg);
}
.key-badge--set {
  border-color: var(--success); color: var(--success);
  background: rgba(52,199,89,.08);
}

/* ── Tabs ───────────────────────────────────────────────────────────────── */
.tabs { display: flex; gap: 4px; }
.tabs button {
  padding: 7px 18px; border: 1px solid transparent; border-bottom: none;
  border-radius: var(--radius) var(--radius) 0 0;
  background: transparent; cursor: pointer; font-size: 14px; color: var(--muted);
}
.tabs button.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ── Layout ─────────────────────────────────────────────────────────────── */
main { padding: 20px 16px; max-width: 980px; margin: 0 auto; }
.tab-section { display: none; }
.tab-section.active { display: block; }
.muted { color: var(--muted); }
.tc { text-align: center; }
.pad20 { padding: 20px; }

/* ── Buttons ────────────────────────────────────────────────────────────── */
.btn-primary {
  background: var(--primary); color: #fff; border: none;
  padding: 10px 26px; border-radius: var(--radius);
  font-size: 15px; font-weight: 600; cursor: pointer;
}
.btn-primary:hover   { opacity: .88; }
.btn-primary:disabled { opacity: .45; cursor: default; }

.btn-sm-primary {
  background: var(--primary); color: #fff; border: none;
  padding: 6px 16px; border-radius: var(--radius); font-size: 13px; cursor: pointer;
}

.btn-secondary {
  background: var(--card); color: var(--primary);
  border: 1px solid var(--primary); padding: 10px 20px;
  border-radius: var(--radius); font-size: 14px; cursor: pointer;
}
.btn-secondary:hover { background: rgba(79,142,247,.07); }

.btn-danger {
  background: var(--danger); color: #fff; border: none;
  padding: 8px 16px; border-radius: var(--radius); font-size: 14px; cursor: pointer;
}

.btn-ghost {
  background: transparent; border: 1px solid var(--border);
  color: var(--muted); padding: 8px 16px; border-radius: var(--radius);
  font-size: 14px; cursor: pointer;
}

.btn-sm {
  background: var(--card); border: 1px solid var(--border);
  padding: 4px 12px; border-radius: 6px; font-size: 12px; cursor: pointer;
}
.btn-sm:hover { border-color: var(--primary); color: var(--primary); }

.danger-sm { border-color: var(--danger) !important; color: var(--danger) !important; }
.danger-sm:hover { background: rgba(255,59,48,.07) !important; }

.file-label { cursor: pointer; }

/* ── Scan tab ───────────────────────────────────────────────────────────── */
.scan-key-bar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 14px; margin-bottom: 14px;
  font-size: 14px;
}

.batch-dropzone {
  border: 2px dashed var(--border); border-radius: 14px;
  padding: 48px 24px; text-align: center; cursor: pointer;
  background: var(--card); transition: border-color .2s, background .2s;
  margin-bottom: 14px;
}
.batch-dropzone p { color: var(--muted); }
.batch-dropzone .hint { margin-top: 6px; font-size: 13px; }
.batch-dropzone:hover, .batch-dropzone.drag-over {
  border-color: var(--primary); background: rgba(79,142,247,.04);
}

.batch-controls {
  display: flex; align-items: center; justify-content: space-between;
  flex-wrap: wrap; gap: 10px; margin-bottom: 12px;
}
.batch-controls > div { display: flex; gap: 8px; flex-wrap: wrap; }

/* Progress */
.progress-wrap {
  display: flex; align-items: center; gap: 10px; margin-bottom: 12px;
}
.progress-bar {
  flex: 1; height: 8px; background: var(--border); border-radius: 4px; overflow: hidden;
}
#progress-bar-fill {
  height: 100%; width: 0; background: var(--primary);
  border-radius: 4px; transition: width .3s;
}

/* Batch table */
.table-wrapper {
  overflow: auto; max-height: 520px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--card); margin-bottom: 14px;
}
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  position: sticky; top: 0; background: var(--bg);
  padding: 8px 10px; text-align: left; font-weight: 600;
  border-bottom: 1px solid var(--border);
}
td { padding: 5px 10px; border-bottom: 1px solid rgba(0,0,0,.04); }
tr:last-child td { border-bottom: none; }

.td-num  { width: 36px; color: var(--muted); text-align: right; }
.td-file { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.td-rut  { font-family: monospace; }
.td-pct  { font-weight: 700; }
.td-corr { color: var(--success); }

.row-ok   { }
.row-warn { background: rgba(255,149,0,.07); }
.row-err  { background: rgba(255,59,48,.07); }

.st-icon  { font-size: 14px; }
.st-ok    { color: var(--success); }
.st-warn  { color: var(--warning); }
.st-err   { color: var(--danger);  }
.st-proc  { }
.st-pending { color: var(--border); }

.badge-warn {
  display: inline-block; font-size: 11px; padding: 1px 6px;
  background: rgba(255,149,0,.15); color: var(--warning);
  border-radius: 4px; margin-right: 4px;
}

.export-row {
  display: flex; gap: 8px; flex-wrap: wrap; margin-top: 4px;
}

/* ── Keys tab ───────────────────────────────────────────────────────────── */
.card {
  background: var(--card); border-radius: var(--radius);
  padding: 16px; border: 1px solid var(--border); margin-bottom: 14px;
}
.card-label {
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .6px; color: var(--muted); margin-bottom: 10px;
}
.card-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 10px;
}

.active-key-banner {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}
.key-meta { font-size: 12px; color: var(--muted); }

.key-form { display: flex; flex-direction: column; gap: 12px; }
.key-form label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 13px; color: var(--muted);
}
.key-form input[type=text] {
  padding: 9px 12px; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: 14px;
}
.key-form textarea {
  padding: 9px 12px; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: 14px; font-family: monospace;
  resize: vertical; letter-spacing: 1px;
}
.char-count { font-size: 12px; color: var(--muted); }
.key-form-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* Keys list */
.key-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 0; border-bottom: 1px solid rgba(0,0,0,.05);
  gap: 10px; flex-wrap: wrap;
}
.key-row:last-child { border-bottom: none; }
.key-row--active { background: rgba(52,199,89,.05); border-radius: 8px; padding: 10px 8px; }
.key-row-info { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.key-row-actions { display: flex; gap: 6px; }

.badge-active {
  font-size: 11px; padding: 2px 7px;
  background: rgba(52,199,89,.15); color: var(--success);
  border-radius: 4px; font-weight: 700;
}

/* ── Results tab ────────────────────────────────────────────────────────── */
.section-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px; flex-wrap: wrap; gap: 10px;
}
.section-header h2 { font-size: 20px; }
.export-actions { display: flex; gap: 8px; flex-wrap: wrap; }

#results-table th,
#results-table td { padding: 10px 12px; }
#results-table th { text-align: left; }

td.correct { color: var(--success); font-weight: 600; }
td.wrong   { color: var(--danger);  font-weight: 600; }
td.omit    { color: var(--muted);   font-weight: 600; }

/* ── Stats tab ──────────────────────────────────────────────────────────── */
.stat-cards { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 24px; }
.stat-card {
  background: var(--card); border-radius: 12px; padding: 14px 20px;
  display: flex; flex-direction: column; gap: 4px; box-shadow: var(--shadow);
}
.stat-val   { font-size: 26px; font-weight: 700; color: var(--primary); }
.stat-label { font-size: 12px; color: var(--muted); }

.charts-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 24px; }
@media (max-width: 600px) { .charts-grid { grid-template-columns: 1fr; } }

.chart-card { background: var(--card); border-radius: 12px; padding: 16px; box-shadow: var(--shadow); }
.chart-card h3 { font-size: 13px; color: var(--muted); margin-bottom: 12px; font-weight: 600; }

.student-search { background: var(--card); border-radius: 12px; padding: 18px; box-shadow: var(--shadow); }
.student-search h3 { margin-bottom: 12px; font-size: 16px; }
.search-row { display: flex; gap: 8px; margin-bottom: 14px; }
.search-row input {
  padding: 9px 12px; border: 1px solid var(--border);
  border-radius: var(--radius); font-size: 14px; flex: 1;
}
#stats-student { margin-top: 12px; }
.student-chart-wrap { margin-top: 10px; }

/* ── Diagnostic modal ───────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000; padding: 16px;
}
.modal-box {
  background: var(--card); border-radius: 14px;
  max-width: 940px; width: 100%; max-height: 92vh;
  display: flex; flex-direction: column; overflow: hidden;
  box-shadow: 0 8px 40px rgba(0,0,0,.30);
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.modal-title {
  font-size: 13px; font-weight: 600; font-family: monospace;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.modal-close { padding: 4px 10px; font-size: 16px; line-height: 1; }

.modal-body {
  display: flex; gap: 16px; padding: 16px;
  overflow: auto; flex: 1; min-height: 0;
}
.diag-canvas-wrap {
  flex: 1; min-width: 0; display: flex;
  align-items: flex-start; justify-content: center;
  overflow: auto;
}
.diag-canvas-wrap canvas {
  max-width: 100%; border-radius: 8px;
  border: 1px solid var(--border); display: block;
}
.diag-info {
  flex: 0 0 190px; display: flex; flex-direction: column; gap: 8px;
}
.diag-row {
  display: flex; flex-direction: column; padding: 8px 12px;
  background: var(--bg); border-radius: 8px;
}
.diag-row span { font-size: 11px; color: var(--muted); margin-bottom: 2px; }
.diag-row strong { font-size: 14px; font-weight: 700; }

/* ── Responsive ─────────────────────────────────────────────────────────── */
@media (max-width: 520px) {
  .tabs button { padding: 6px 11px; font-size: 13px; }
  main { padding: 14px 10px; }
  .batch-controls { flex-direction: column; align-items: flex-start; }
  .modal-body { flex-direction: column; }
  .diag-info { flex: none; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
}
