/* ==========================================================================
   Biblioteca — estilos de las tres páginas: inicio de sesión, mostrador del
   bibliotecario y catálogo del lector.
   ========================================================================== */

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e3e6eb;
  --border-strong: #cfd4dc;
  --text: #1b2230;
  --muted: #667085;

  --brand: #0f6e5c;
  --brand-hover: #0b5a4b;
  --brand-soft: #e5f2ee;

  --ok: #157347;
  --ok-soft: #e6f4ec;
  --warn: #9a5b00;
  --warn-soft: #fdf1dc;
  --danger: #c0352b;
  --danger-soft: #fceceb;
  --star: #e0a028;

  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, .18);
  --topbar: 60px;
}

* { box-sizing: border-box; }

/* El atributo `hidden` solo aporta `display: none` desde la hoja del
   navegador, así que cualquier `display` explícito lo pisa. */
[hidden] { display: none !important; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p { margin: 0; }
ul { margin: 0; padding: 0; list-style: none; }

.muted { color: var(--muted); }
.small { font-size: .8125rem; }

/* Fuera de la vista, pero lo leen los lectores de pantalla. */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* --- Barra superior -------------------------------------------------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 16px;
  height: var(--topbar);
  padding: 0 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-size: 1.125rem;
  font-weight: 700;
  text-decoration: none;
}

.brand-mark {
  width: 26px;
  height: 26px;
  fill: none;
  stroke: var(--brand);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.topbar-end {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-left: auto;
}

.lang-select {
  height: 34px;
  padding: 0 28px 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: .8125rem;
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-text { display: flex; flex-direction: column; line-height: 1.2; }
.user-text strong { font-size: .875rem; white-space: nowrap; }
.user-text small { color: var(--muted); font-size: .75rem; }

.avatar {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--brand);
  color: #fff;
  font-size: .875rem;
  font-weight: 700;
}

/* --- Botones --------------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 38px;
  padding: 0 16px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--text);
  font: inherit;
  font-size: .875rem;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
}

.btn.sm { height: 32px; padding: 0 12px; font-size: .8125rem; }
.btn.block { width: 100%; }

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

.btn.secondary { background: var(--surface); border-color: var(--border-strong); }
.btn.secondary:hover { background: var(--surface-2); border-color: var(--muted); }

.btn.ghost { color: var(--muted); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }

/* Acción frecuente que no debe pesar tanto como la principal. */
.btn.tonal { background: var(--brand-soft); color: var(--brand); }
.btn.tonal:hover { background: #d3eae2; }
.btn.tonal:disabled,
.btn.tonal:disabled:hover { background: var(--surface-2); color: var(--border-strong); }

.btn.danger { background: var(--danger); color: #fff; }
.btn.danger:hover { background: #a42b22; }
.btn.danger-text:hover { background: var(--danger-soft); color: var(--danger); }

.btn:disabled,
.btn:disabled:hover {
  background: none;
  border-color: transparent;
  color: var(--border-strong);
  cursor: not-allowed;
}

.btn.primary:disabled { background: var(--border-strong); color: #fff; }

.btn:focus-visible,
.icon-btn:focus-visible,
.star:focus-visible,
.tab:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
}

.link {
  padding: 0;
  border: none;
  background: none;
  color: var(--brand);
  font: inherit;
  font-size: .8125rem;
  font-weight: 600;
  cursor: pointer;
}

.link:hover { text-decoration: underline; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--muted);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
}

.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* --- Distribución -------------------------------------------------------------- */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 24px;
  align-items: start;
  max-width: 1320px;
  margin: 0 auto;
  padding: 24px;
}

.content { display: flex; flex-direction: column; gap: 20px; min-width: 0; }

/* La columna lateral acompaña al desplazarse: el chat siempre está a mano. */
.side {
  position: sticky;
  top: calc(var(--topbar) + 24px);
  display: flex;
  flex-direction: column;
  gap: 20px;
  height: calc(100vh - var(--topbar) - 48px);
  min-height: 460px;
}

/* --- Paneles --------------------------------------------------------------- */

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.panel-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.panel-head h2 { font-size: 1rem; font-weight: 700; }
.panel-head .btn,
.panel-head .toggle { margin-left: auto; }
.panel-head .toggle + .btn { margin-left: 0; }
.panel-body { padding: 16px 20px 20px; }

.count {
  color: var(--muted);
  font-size: .8125rem;
  font-weight: 600;
}

.empty {
  padding: 28px 20px;
  color: var(--muted);
  text-align: center;
}

/* --- Pestañas del bibliotecario ------------------------------------------------ */

.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: -8px;
  border-bottom: 1px solid var(--border);
}

.tab {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  color: var(--muted);
  font-weight: 600;
  text-decoration: none;
}

.tab:hover { color: var(--text); }
.tab.active { border-bottom-color: var(--brand); color: var(--text); }

.tab-count {
  min-width: 22px;
  padding: 0 7px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: .75rem;
  text-align: center;
}

.tab.active .tab-count { background: var(--brand-soft); border-color: transparent; color: var(--brand); }
.tab-count.alert,
.tab.active .tab-count.alert { background: var(--danger); border-color: transparent; color: #fff; }

/* --- Actividad (contadores del servidor) ---------------------------------------- */

.activity {
  padding: 16px 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.activity-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 4px 10px;
  margin-bottom: 12px;
}

.activity-head h2 { font-size: 1rem; }
.activity-head .muted { font-size: .8125rem; }
.activity-head .link { margin-left: auto; }

.stats {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 10px;
}

.stat {
  display: flex;
  flex-direction: column;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}

.stat-value { font-size: 1.5rem; font-weight: 700; line-height: 1.2; }
.stat-label { color: var(--muted); font-size: .75rem; }

/* --- Etiquetas de estado ---------------------------------------------------------- */

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  width: fit-content;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: .75rem;
  font-weight: 600;
  white-space: nowrap;
}

.pill::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.danger { background: var(--danger-soft); color: var(--danger); }

/* --- Portadas ------------------------------------------------------------------- */

.cover {
  display: block;
  flex: none;
  object-fit: cover;
  border-radius: 4px;
  background: hsl(var(--hue, 160), 35%, 88%);
  box-shadow: 0 1px 2px rgba(16, 24, 40, .12);
}

.cover-xs { width: 32px; height: 48px; }
.cover-sm { width: 44px; height: 66px; }
.cover-lg { width: 100%; aspect-ratio: 2 / 3; height: auto; border-radius: 6px; }

/* Sin portada en Open Library: la inicial del título sobre un color propio. */
.cover.placeholder {
  display: grid;
  place-items: center;
  background: linear-gradient(160deg, hsl(var(--hue, 160), 45%, 46%), hsl(var(--hue, 160), 50%, 32%));
  color: rgba(255, 255, 255, .92);
  font-family: Georgia, "Times New Roman", serif;
  font-weight: 700;
}

.cover-xs.placeholder { font-size: .875rem; }
.cover-sm.placeholder { font-size: 1.25rem; }
.cover-lg.placeholder { font-size: 3rem; }

.title { font-weight: 600; }
.meta { color: var(--muted); font-size: .8125rem; }
.meta.late { color: var(--danger); font-weight: 600; }
.isbn { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .75rem; }

/* --- Préstamos activos -------------------------------------------------------------- */

.loan-card {
  display: grid;
  grid-template-columns: 44px minmax(0, 1.3fr) minmax(0, 1fr) minmax(0, 1fr) auto;
  gap: 16px;
  align-items: center;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
}

.loan-card.late { background: linear-gradient(90deg, var(--danger-soft), transparent 55%); }
.loan-card .name { font-weight: 600; }
.loan-due { display: flex; flex-direction: column; gap: 4px; }

/* --- Historial -------------------------------------------------------------------- */

.history { border-top: 1px solid var(--border); }

.history summary {
  padding: 14px 20px;
  color: var(--muted);
  font-weight: 600;
  cursor: pointer;
}

.history summary:hover { color: var(--text); }
.history[open] summary { border-bottom: 1px solid var(--border); }

.table-wrap { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; font-size: .875rem; }

th {
  padding: 10px 20px;
  color: var(--muted);
  font-size: .75rem;
  font-weight: 600;
  letter-spacing: .03em;
  text-align: left;
  text-transform: uppercase;
}

td { padding: 10px 20px; border-top: 1px solid var(--border); vertical-align: top; }
td .title, td .meta { display: block; }

/* --- Catálogo del bibliotecario: una fila por libro ----------------------------------- */

.book-row {
  display: grid;
  grid-template-columns: 44px minmax(0, 1.4fr) minmax(0, 1fr) auto;
  gap: 16px;
  align-items: center;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
}

.book-row:last-child,
.loan-card:last-child { border-bottom: none; }

.book-status { display: flex; flex-direction: column; gap: 4px; }

.book-actions {
  display: flex;
  justify-content: flex-end;
  gap: 4px;
}

.book-actions .btn.primary,
.book-actions .btn.secondary { min-width: 92px; margin-right: 4px; }

/* --- Catálogo del lector: una tarjeta por libro ------------------------------------- */

.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 24px 18px;
  padding: 20px;
}

.book-card { display: flex; flex-direction: column; gap: 4px; }
.book-card .cover-wrap { position: relative; margin-bottom: 6px; }
.book-card .title { line-height: 1.3; }
/* En una tarjeta estrecha, «Debía volver el …» tiene que poder partirse. */
.book-card .pill { margin-top: 2px; border-radius: 10px; white-space: normal; }
.book-card .btn { margin-top: auto; }
.book-card .pill + .btn { margin-top: 10px; }

.star {
  position: absolute;
  top: 8px;
  right: 8px;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, .94);
  box-shadow: 0 1px 4px rgba(16, 24, 40, .2);
  color: var(--muted);
  font-size: 1.1rem;
  cursor: pointer;
}

.star:hover { color: var(--star); }
.star.active { color: var(--star); }

/* --- Lista de deseos ------------------------------------------------------------------ */

.wish-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px 10px 20px;
  border-bottom: 1px solid var(--border);
}

.wish-item:last-child { border-bottom: none; }
.wish-info { flex: 1; min-width: 0; }
.wish-info .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.state-ok { color: var(--ok); }
.state-warn { color: var(--warn); }
.state-danger { color: var(--danger); }
#wishlist .empty { padding: 20px; font-size: .875rem; }
#wishlist .panel-head .link { margin-left: auto; }

/* --- Plazos y multas -------------------------------------------------------------------- */

.terms-form {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 16px;
  margin: 14px 0;
}

.terms-form .field { flex: 1 1 200px; }

.terms-result {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}

.terms-result .late { color: var(--danger); }
.terms-result .on-time { color: var(--ok); }

/* --- Búsqueda en el catálogo ------------------------------------------------------------ */

.search {
  position: relative;
  flex: 1 1 220px;
  max-width: 340px;
}

.search svg {
  position: absolute;
  top: 50%;
  left: 11px;
  width: 16px;
  height: 16px;
  fill: none;
  stroke: var(--muted);
  stroke-width: 2;
  stroke-linecap: round;
  transform: translateY(-50%);
  pointer-events: none;
}

.search input {
  width: 100%;
  height: 36px;
  padding: 0 12px 0 34px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: .875rem;
}

.search input:focus { background: var(--surface); }

/* --- Lectores (bibliotecario) ------------------------------------------------------------- */

.reader-row {
  display: grid;
  grid-template-columns: 40px minmax(0, 1.4fr) minmax(0, 1fr) auto;
  gap: 16px;
  align-items: center;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
}

.reader-row:last-child { border-bottom: none; }
.reader-row > .avatar { width: 40px; height: 40px; font-size: 1rem; }
.reader-status { display: flex; flex-direction: column; gap: 4px; }
.reader-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 4px; }
.reader-actions .btn.primary { min-width: 92px; margin-right: 4px; }

.role-tag {
  margin-left: 6px;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--brand-soft);
  color: var(--brand);
  font-size: .6875rem;
  font-weight: 600;
  vertical-align: 1px;
}

.role-tag.reader { background: var(--surface-2); color: var(--muted); }

/* Datos del lector en el préstamo: llegan de su cuenta, no se escriben. */
.reader-data input[readonly] {
  border-color: var(--border);
  background: var(--surface-2);
  color: var(--text);
}

.reader-data input[readonly]:focus { outline: none; }

/* --- Mi carnet (lector) --------------------------------------------------------------------- */

.my-card-body {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 28px;
  padding: 16px 20px;
}

.my-card-who { display: flex; align-items: center; gap: 12px; }
.my-card-who .avatar { width: 40px; height: 40px; font-size: 1rem; }
.my-card-state { display: flex; flex-direction: column; gap: 2px; }

.my-card-loan {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1 1 260px;
  font-size: .875rem;
}

.my-card-loan.late { color: var(--danger); font-weight: 600; }

.my-card-note {
  flex-basis: 100%;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  background: var(--warn-soft);
  color: var(--warn);
  font-size: .8125rem;
  font-weight: 600;
}

/* --- Interruptor «Solo disponibles» --------------------------------------------------- */

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: .875rem;
  cursor: pointer;
  user-select: none;
}

.toggle input {
  appearance: none;
  position: relative;
  width: 34px;
  height: 20px;
  margin: 0;
  border-radius: 999px;
  background: var(--border-strong);
  cursor: pointer;
  transition: background .15s;
}

.toggle input::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(16, 24, 40, .25);
  transition: transform .15s;
}

.toggle input:checked { background: var(--brand); }
.toggle input:checked::after { transform: translateX(14px); }
.toggle input:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }

/* --- Formularios ------------------------------------------------------------------ */

.form { display: flex; flex-direction: column; gap: 14px; }

.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > label,
.field > span { font-size: .8125rem; font-weight: 600; }

input[type="text"],
input[type="password"],
input[type="date"],
select {
  width: 100%;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

input:focus,
select:focus {
  border-color: var(--brand);
  outline: 3px solid var(--brand-soft);
}

input.invalid { border-color: var(--danger); }
input.invalid:focus { outline-color: var(--danger-soft); }
input.valid { border-color: #7cc4a5; }
input:disabled { background: var(--surface-2); color: var(--muted); }

.hint { color: var(--muted); font-size: .75rem; }
.field-error { color: var(--danger); font-size: .75rem; font-weight: 600; }
/* Si hay error, se lee el error y no la pista. */
.field.has-error .hint { display: none; }

.field-row { display: flex; gap: 12px; }
.field-row > .field { flex: 1; }
.field-row > .field.narrow { flex: 0 0 110px; }

.input-row { display: flex; gap: 8px; }
.input-row input { flex: 1; }

.fieldset {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin: 0;
  padding: 14px 16px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.fieldset legend { padding: 0 6px; color: var(--muted); font-size: .8125rem; font-weight: 600; }

.form-error,
.dialog-error {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  color: var(--danger);
  font-size: .875rem;
  font-weight: 600;
}

/* --- Diálogos ---------------------------------------------------------------------- */

.dialog {
  width: min(560px, calc(100% - 32px));
  max-height: calc(100vh - 48px);
  padding: 0;
  border: none;
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
}

.dialog.small { width: min(440px, calc(100% - 32px)); }
.dialog::backdrop { background: rgba(16, 24, 40, .45); }
.dialog > form { display: flex; flex-direction: column; max-height: inherit; }

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 20px 10px 24px;
}

.dialog-head h2,
.dialog.small h2 { font-size: 1.125rem; }

.dialog-body {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 8px 24px 20px;
  overflow-y: auto;
}

.dialog.small .dialog-body { padding-top: 22px; }

.dialog-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 0 0 16px 16px;
}

/* Resultado de Open Library dentro del formulario de libro. */
.lookup {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: .875rem;
}

.lookup.neutral { background: var(--surface-2); color: var(--muted); }
.lookup.ok { background: var(--ok-soft); }
.lookup.warning { background: var(--warn-soft); color: var(--warn); }
.lookup.error { background: var(--danger-soft); color: var(--danger); }
.lookup.ok strong { color: var(--ok); }

.loan-summary {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}

.loan-summary:empty { display: none; }
.loan-summary .due { margin-top: 4px; color: var(--brand); font-size: .875rem; font-weight: 600; }

.confirm-book { display: flex; align-items: center; gap: 14px; margin-bottom: 12px; }

.callout {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-weight: 600;
}

.callout.ok { background: var(--ok-soft); color: var(--ok); }
.callout.danger { background: var(--danger-soft); color: var(--danger); }

/* --- Aviso flotante ------------------------------------------------------------------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 60;
  width: max-content;
  max-width: min(560px, calc(100% - 32px));
  padding: 12px 18px;
  border-radius: 10px;
  background: var(--text);
  color: #fff;
  box-shadow: var(--shadow-lg);
  font-weight: 500;
  cursor: pointer;
  transform: translateX(-50%);
}

.toast.error { background: var(--danger); }

/* --- Chat --------------------------------------------------------------------------- */

.chat {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 320px;
}

/* El estado de la conexión va debajo del título del chat. */
.chat .panel-head { flex-direction: column; align-items: flex-start; gap: 2px; }

.presence {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: .75rem;
}

.presence .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--border-strong); }
.presence.online .dot { background: #22a06b; box-shadow: 0 0 0 3px rgba(34, 160, 107, .18); }
.presence.offline { color: var(--danger); }
.presence.offline .dot { background: var(--danger); }

.chat-messages {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  padding: 16px;
  overflow-y: auto;
}

.chat-empty { margin: auto; padding: 0 20px; color: var(--muted); font-size: .875rem; text-align: center; }

.chat-message { display: flex; align-items: flex-end; gap: 8px; max-width: 88%; }
.chat-message .avatar { width: 28px; height: 28px; font-size: .75rem; }
.chat-message.own { flex-direction: row-reverse; align-self: flex-end; }

.bubble {
  padding: 8px 12px;
  border-radius: 14px 14px 14px 4px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.chat-message.own .bubble {
  border-radius: 14px 14px 4px 14px;
  background: var(--brand-soft);
  border-color: transparent;
}

.chat-message.staff:not(.own) .bubble { border-color: #b9dccf; }

.bubble .author { font-size: .75rem; font-weight: 700; }
.bubble .time { margin-left: 4px; color: var(--muted); font-weight: 400; }
.bubble .text { font-size: .875rem; white-space: pre-wrap; overflow-wrap: anywhere; }

/* Conversaciones del bibliotecario: una por lector. */
.conversations {
  flex: 0 1 auto;
  max-height: 38%;
  overflow-y: auto;
  border-bottom: 1px solid var(--border);
}

.conversation {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 16px;
  border: none;
  border-left: 3px solid transparent;
  background: none;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.conversation:hover { background: var(--surface-2); }
.conversation.active { border-left-color: var(--brand); background: var(--brand-soft); }

.conversation-avatar { position: relative; flex: none; }
.conversation-avatar .avatar { width: 32px; height: 32px; font-size: .8125rem; }

.status-dot {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 10px;
  height: 10px;
  border: 2px solid var(--surface);
  border-radius: 50%;
  background: var(--border-strong);
}

.status-dot.online { background: #22a06b; }

.conversation-text { display: flex; flex: 1; flex-direction: column; min-width: 0; line-height: 1.3; }
.conversation-text strong { font-size: .875rem; }

.conversation-text small {
  overflow: hidden;
  color: var(--muted);
  font-size: .75rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation.unread .conversation-text small { color: var(--text); font-weight: 600; }
.conversation .time { color: var(--muted); font-size: .6875rem; }

.unread-count {
  min-width: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--brand);
  color: #fff;
  font-size: .6875rem;
  font-weight: 700;
  line-height: 20px;
  text-align: center;
}

.chat-with {
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--muted);
  font-size: .75rem;
  font-weight: 600;
}

.chat-send {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.chat-send input { flex: 1; }
.chat > .form-error { margin: 0 16px 12px; }

/* --- Inicio de sesión --------------------------------------------------------------- */

.login-wrap {
  display: grid;
  place-items: center;
  min-height: calc(100vh - var(--topbar));
  padding: 32px 16px;
}

.login-card {
  width: min(420px, 100%);
  padding: 32px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.login-card h1 { font-size: 1.5rem; }
.login-card > .muted { margin: 4px 0 24px; }

.demo-accounts { margin-top: 28px; padding-top: 20px; border-top: 1px solid var(--border); }
.demo-title { margin-bottom: 10px; font-size: .8125rem; font-weight: 600; }
.demo-title .muted { font-weight: 400; }
.demo-accounts ul { display: flex; flex-direction: column; gap: 8px; }

.demo-account {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.demo-account:hover { border-color: var(--brand); background: var(--brand-soft); }
.demo-text { display: flex; flex-direction: column; line-height: 1.3; }
.demo-text small { color: var(--muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

/* Botón para ir al chat: solo existe cuando el chat queda debajo. */
.chat-jump { display: none; }

.chat-jump .unread {
  display: inline-grid;
  place-items: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: #fff;
  color: var(--brand);
  font-size: .75rem;
}

/* --- Pantallas medianas: el chat pasa debajo ------------------------------------------- */

@media (max-width: 1000px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .side { position: static; height: auto; min-height: 0; }
  .chat { height: 520px; flex: none; }

  .chat-jump.away {
    position: fixed;
    right: 16px;
    bottom: 16px;
    z-index: 30;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    height: 44px;
    padding: 0 18px;
    border-radius: 999px;
    background: var(--brand);
    color: #fff;
    font-weight: 600;
    text-decoration: none;
    box-shadow: var(--shadow-lg);
  }
}

/* --- Móvil ------------------------------------------------------------------------------- */

@media (max-width: 640px) {
  .topbar { gap: 8px; padding: 0 12px; }
  .brand span { display: none; }
  .user-text { display: none; }
  .layout { padding: 16px 12px; gap: 16px; }
  .panel-head { padding: 14px 16px; }

  /* Tres pestañas no caben a lo ancho: se compactan y, si hace falta, se deslizan. */
  .tabs { overflow-x: auto; scrollbar-width: none; }
  .tab { flex: none; gap: 6px; padding: 10px 8px; }

  /* Los contadores, en una tira que se desliza en lugar de tres filas. */
  .stats { display: flex; overflow-x: auto; scrollbar-width: none; }
  .stat { flex: 0 0 auto; min-width: 110px; }

  .loan-card {
    grid-template-columns: 44px minmax(0, 1fr);
    gap: 8px 12px;
    padding: 14px 16px;
  }
  .loan-card .cover { grid-row: span 2; }
  .loan-person, .loan-due { grid-column: 2; }
  .loan-card > .btn { grid-column: 1 / -1; }

  .book-row {
    grid-template-columns: 44px minmax(0, 1fr);
    gap: 8px 12px;
    padding: 12px 16px;
  }
  .book-row .cover { grid-row: span 2; }
  .book-status { grid-column: 2; }
  .book-actions { grid-column: 1 / -1; justify-content: flex-start; flex-wrap: wrap; }

  .book-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 20px 14px; padding: 16px; }

  .search { flex-basis: 100%; max-width: none; order: 5; }

  .reader-row {
    grid-template-columns: 40px minmax(0, 1fr);
    gap: 8px 12px;
    padding: 14px 16px;
  }
  .reader-status { grid-column: 2; }
  .reader-actions { grid-column: 1 / -1; justify-content: flex-start; }
  .my-card-body { padding: 14px 16px; }

  /* El historial tiene demasiadas columnas: cada fila pasa a ser una ficha. */
  .history-table thead { display: none; }
  .history-table, .history-table tbody, .history-table tr, .history-table td { display: block; width: 100%; }
  .history-table tr { padding: 10px 16px; border-top: 1px solid var(--border); }
  .history-table td { padding: 2px 0; border: none; }
  .history-table td[data-label]::before {
    content: attr(data-label);
    display: block;
    color: var(--muted);
    font-size: .6875rem;
    letter-spacing: .03em;
    text-transform: uppercase;
  }

  .field-row { flex-direction: column; }
  .field-row > .field.narrow { flex: 1; }
  .dialog-body { padding: 8px 18px 18px; }
  .login-card { padding: 24px 20px; }
}
