:root {
  --primary: #870303;
  --secondary: #0247B3;
  --primary-hover: #690202;
  --ink: #1f2937;
  --muted: #5f6b7a;
  --line: #dbe2ec;
  --surface: #ffffff;
  --canvas: #f5f7fb;
  --danger: #870303;
  --shadow: 0 8px 20px rgba(24, 38, 61, 0.09);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0; min-height: 100vh; background: var(--canvas); color: var(--ink);
  font-family: system-ui, -apple-system, sans-serif;
}
button, input, select { font: inherit; }
[hidden] { display: none !important; }

/* Topbar */
.topbar {
  height: 60px; padding: 0 20px; background: var(--primary); color: #fff;
  display: flex; align-items: center; justify-content: space-between;
  position: sticky; top: 0; z-index: 30;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 36px; height: 36px; border-radius: 8px; display: grid; place-items: center; background: rgba(255,255,255,.15); }
.brand span { color: #f0cbcb; font-size: 12px; }
.brand strong, .brand span { display: block; }
.top-actions { display: flex; gap: 8px; }
.session-user { align-self: center; font-size: 12px; color: #f0cbcb; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); border: 0; }
h1 { margin: 6px 0; font-size: clamp(22px, 3vw, 30px); }
h2, h3 { margin: 0; font-size: 16px; }
p { color: var(--muted); font-size: 13px; line-height: 1.5; }
.eyebrow { text-transform: uppercase; color: var(--secondary); font-size: 10px; letter-spacing: .1em; font-weight: 700; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--secondary); outline-offset: 3px; }

/* Botones */
.btn {
  min-height: 38px; padding: 8px 14px; border: 0; border-radius: 8px;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  cursor: pointer; font-weight: 700; font-size: 13px; text-decoration: none;
}
.btn:disabled { opacity: .6; cursor: not-allowed; }
.btn-primary { color: #fff; background: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-light { color: #fff; background: rgba(255,255,255,.15); }
.btn-soft { color: var(--secondary); background: #eaf1fc; }
.btn-danger { color: var(--danger); background: #fceaea; }
.btn-icon { min-width: 32px; height: 32px; padding: 4px; }

/* Layout & Cards */
main { max-width: 1400px; margin: 0 auto; padding: 20px; }
.welcome { display: flex; justify-content: space-between; align-items: flex-end; gap: 15px; margin-bottom: 20px; }
.metrics { display: flex; gap: 10px; }
.metric { padding: 10px 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; }
.metric span { color: var(--muted); font-size: 11px; text-transform: uppercase; }
.metric strong { display: block; font-size: 20px; color: var(--primary); }

.workspace { display: grid; grid-template-columns: 300px 1fr; gap: 20px; align-items: start; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); }
.form-card { padding: 18px; position: sticky; top: 76px; }
.card-heading { display: flex; align-items: center; gap: 10px; margin-bottom: 15px; }

/* Formularios */
.field { margin-bottom: 12px; }
.field label { display: block; margin-bottom: 4px; font-size: 12px; font-weight: 700; }
.field input, .field select, .toolbar input, .toolbar select {
  width: 100%; height: 40px; padding: 0 10px; background: #fff;
  border: 1px solid #d0dbea; border-radius: 8px; outline: none;
}
.field input:focus, .toolbar input:focus { border-color: var(--secondary); }

/* Listas y Tablas */
.list-head { padding: 16px; border-bottom: 1px solid var(--line); }
.toolbar { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; }
.search-wrap { position: relative; }
.search-wrap input { padding-right: 34px; }
.search-clear { position: absolute; right: 8px; top: 7px; background: transparent; border: 0; font-size: 20px; cursor: pointer; color: var(--muted); }
.list-title-row { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 12px; }
.list-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.list-card { min-width: 0; overflow: hidden; }
.loading-line { height: 3px; background: linear-gradient(90deg, var(--primary), #e7baba, var(--secondary)); }
#studentGroups { padding: 12px; }
.empty { padding: 30px 16px; text-align: center; color: var(--muted); display: grid; gap: 8px; }
.form-status { font-size: 12px; }

.room-group { margin-bottom: 12px; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.room-header { padding: 10px 14px; background: #f2f6fd; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; }
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 12px; }
th, td { padding: 8px 12px; text-align: left; border-top: 1px solid var(--line); }
th { background: #f8faff; color: var(--muted); font-size: 10px; text-transform: uppercase; }
.code { font-family: monospace; font-weight: 700; color: #24446e; }
.student-name, .student-firstname { display: block; }
.student-name { font-weight: 700; }
.student-firstname { color: var(--muted); margin-top: 3px; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.barcode-button { background: transparent; border: 0; padding: 5px; cursor: pointer; }
.barcode-svg { display: block; max-width: 180px; height: 35px; }
.room-name { font-size: 13px; }
.room-count { font-size: 12px; font-weight: 700; }

/* Modales y Notificaciones */
dialog { width: min(90vw, 480px); padding: 20px; border: 0; border-radius: 12px; box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(0,0,0,.5); }
.toast-stack { position: fixed; right: 20px; bottom: 20px; z-index: 100; display: grid; gap: 8px; }
.toast { width: 300px; padding: 12px; background: #fff; border: 1px solid var(--line); border-radius: 8px; border-left: 4px solid var(--primary); font-size: 12px; }
.toast.error { border-left-color: var(--danger); }
.toast { display: flex; gap: 12px; align-items: flex-start; max-width: calc(100vw - 40px); }
.toast-content { display: grid; gap: 4px; flex: 1; overflow-wrap: anywhere; }
.toast-close, .close { border: 0; background: transparent; cursor: pointer; font-size: 22px; color: var(--muted); }
.dialog-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 18px; gap: 16px; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.barcode-preview { text-align: center; }
#barcodeCanvas { max-width: 100%; height: auto; }

/* Adaptabilidad (Responsive) */
@media (max-width: 850px) {
  .workspace { grid-template-columns: 1fr; }
  .form-card { position: static; }
  .toolbar { grid-template-columns: 1fr; }
  .welcome { flex-direction: column; align-items: flex-start; }
  .list-title-row { flex-direction: column; }
  .list-actions { justify-content: flex-start; }
  .topbar { height: auto; min-height: 60px; padding: 12px; gap: 12px; flex-wrap: wrap; }
  .top-actions { flex-wrap: wrap; }
  .brand strong { font-size: 14px; }
}

/* Impresión */
@media print {
  .topbar, .welcome, .form-card, .toolbar, .row-actions, .actions-cell, .list-head, .toast-stack, dialog, .loading-line { display: none !important; }
  main { padding: 0; }
  .card { border: 0; box-shadow: none; }
  .workspace { display: block; }
  .table-scroll { overflow: visible; }
  th:last-child { display: none; }
  tr { break-inside: avoid; }
  .room-header { break-after: avoid; }
  .barcode-button { padding: 0; }
}

.local-notice {padding:12px 16px;border-left:4px solid #0247B3;background:#edf3fd;font-size:14px;line-height:1.6;} .brand-mark{object-fit:contain;background:white;} @media print{.local-notice{display:none}}

/* Teléfonos estrechos: controles apilados y áreas táctiles amplias. */
@media screen and (max-width: 560px) {
  main { padding: 14px 10px; }
  .topbar { padding: 10px; gap: 10px; }
  .top-actions { display: grid; width: 100%; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
  .session-user { grid-column: 1 / -1; }
  .top-actions .btn { width: 100%; min-width: 0; padding: 8px 6px; font-size: 12px; }
  .welcome { gap: 12px; margin-bottom: 14px; }
  .metrics { display: grid; width: 100%; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .workspace { gap: 12px; }
  .form-card { padding: 14px; }
  .list-head { padding: 12px; }
  .list-actions { display: grid; width: 100%; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .list-actions .btn { width: 100%; min-width: 0; padding: 8px 5px; font-size: 12px; }
  .field input, .field select, .toolbar input, .toolbar select { min-height: 42px; height: auto; font-size: 16px; }
  .table-scroll { overscroll-behavior-x: contain; }
  table { min-width: 560px; }
  dialog { max-height: calc(100dvh - 24px); overflow-y: auto; padding: 16px; }
  .toast-stack { right: 10px; bottom: max(10px, env(safe-area-inset-bottom)); left: 10px; }
  .toast { width: 100%; max-width: none; }
}
