/* ==========================================================================
   Control de estudios PACS
   Tokens: gris frío clínico + un acento azul acero. Plex Sans para interfaz,
   Plex Mono solo para identificadores DICOM.
   ========================================================================== */

:root {
  --bg: #eef1f3;
  --surface: #ffffff;
  --surface-sunk: #f6f8f9;
  --ink: #17222d;
  --ink-2: #3c4a57;
  --muted: #66737f;
  --line: #d9dfe3;
  --line-strong: #c3ccd3;
  --accent: #24577a;
  --accent-hover: #1b4562;
  --accent-soft: #e3edf4;
  --ok: #2f7d4f;
  --warn: #a5650b;
  --danger: #b3261e;
  --row-hover: #f3f6f8;
  --ok-dot: #22a559;
  --off-dot: #d93025;

  --mod-ct: #2c6a96;
  --mod-mr: #6b4f9e;
  --mod-xr: #8a6212;
  --mod-us: #2f7d5c;
  --mod-mg: #a0466e;
  --mod-nm: #a43c2c;

  --font: 'IBM Plex Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --mono: 'IBM Plex Mono', ui-monospace, Consolas, monospace;
  --radius: 6px;
  --row-pad: 6px;
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12171c;
    --surface: #1a2026;
    --surface-sunk: #161b20;
    --ink: #e3e8ec;
    --ink-2: #c2cbd2;
    --muted: #8e9aa5;
    --line: #2a323a;
    --line-strong: #3a444e;
    --accent: #7fb2d6;
    --accent-hover: #a2c8e3;
    --accent-soft: #1f2e3a;
    --ok: #6cc38f;
    --warn: #e0a84b;
    --danger: #f08a80;
    --row-hover: #1f262d;
    --mod-ct: #7fb2d6;
    --mod-mr: #b49ae0;
    --mod-xr: #dcb35e;
    --mod-us: #74c6a0;
    --mod-mg: #e28fb4;
    --mod-nm: #ec907f;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #12171c;
  --surface: #1a2026;
  --surface-sunk: #161b20;
  --ink: #e3e8ec;
  --ink-2: #c2cbd2;
  --muted: #8e9aa5;
  --line: #2a323a;
  --line-strong: #3a444e;
  --accent: #7fb2d6;
  --accent-hover: #a2c8e3;
  --accent-soft: #1f2e3a;
  --ok: #6cc38f;
  --warn: #e0a84b;
  --danger: #f08a80;
  --row-hover: #1f262d;
  --mod-ct: #7fb2d6;
  --mod-mr: #b49ae0;
  --mod-xr: #dcb35e;
  --mod-us: #74c6a0;
  --mod-mg: #e28fb4;
  --mod-nm: #ec907f;
  color-scheme: dark;
}


*, *::before, *::after { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  font-variant-numeric: tabular-nums;
}

h1, h2, p { margin: 0; }

button, input, select { font: inherit; color: inherit; }

[hidden] { display: none !important; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

kbd {
  font-family: var(--mono);
  font-size: 11px;
  padding: 0 5px;
  border: 1px solid var(--line-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--surface);
}

.mono { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--muted); }

/* ---------- Botones ---------- */

.btn-primary, .btn-secondary, .btn-ghost {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 36px;
  padding: 0 16px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 120ms var(--ease), border-color 120ms var(--ease);
  white-space: nowrap;
}

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
:root[data-theme="dark"] .btn-primary { color: #0f1a22; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .btn-primary { color: #0f1a22; }
}

.btn-secondary { background: var(--surface); border-color: var(--line-strong); }
.btn-secondary:hover { border-color: var(--accent); color: var(--accent); }

.btn-ghost { background: transparent; color: var(--ink-2); }
.btn-ghost:hover { background: var(--surface-sunk); }

.btn-primary:active, .btn-secondary:active, .btn-ghost:active { transform: translateY(1px); }
button:disabled { opacity: 0.55; cursor: default; }

.btn-link {
  background: none;
  border: 0;
  padding: 0;
  color: var(--accent);
  font-weight: 500;
  cursor: pointer;
}
.btn-link:hover { text-decoration: underline; text-underline-offset: 3px; }

.btn-icon {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.btn-icon:hover { background: var(--surface-sunk); color: var(--ink); }

/* ---------- Barra superior ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  height: 52px;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 8px; min-width: 0; }
.brand-toggle { width: 36px; height: 36px; margin-left: -8px; }
.brand-toggle[aria-expanded="false"] { background: var(--accent-soft); }
.brand-mark { color: var(--accent); flex: none; }
.brand-name { font-weight: 600; font-size: 15px; white-space: nowrap; }

.topbar-right { display: flex; align-items: center; gap: 12px; font-size: 13px; }

.divider { width: 1px; height: 20px; background: var(--line); }

.db-status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 28px;
  padding: 0 8px;
  border: 0;
  border-radius: var(--radius);
  font-size: 13px;
  color: var(--ink-2);
  background: transparent;
  cursor: pointer;
}
.db-status:hover { background: var(--surface-sunk); }
.db-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--line-strong); }
.db-status[data-state="online"] .db-dot { background: var(--ok-dot); }
.db-status[data-state="offline"] .db-dot { background: var(--off-dot); }
.db-status[data-state="offline"] .db-text { color: var(--danger); }
.db-status:disabled .db-dot { opacity: 0.4; }

.user-name { color: var(--ink-2); font-weight: 500; }

/* ---------- Estructura ---------- */

.layout {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  height: calc(100vh - 52px);
}

.sidebar {
  position: sticky;
  top: 52px;
  align-self: start;
  height: 100%;
  overflow: hidden auto;
  padding: 20px;
  border-right: 1px solid var(--line);
  background: var(--surface);
}

.content {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  padding: 20px 24px 16px;
}

.layout.filters-hidden { grid-template-columns: minmax(0, 1fr); }
.layout.filters-hidden .sidebar { display: none; }

/* ---------- Filtros ---------- */

.filters { display: flex; flex-direction: column; gap: 16px; }

.field { display: flex; flex-direction: column; gap: 6px; margin: 0; padding: 0; border: 0; min-width: 0; }

.field label, .field legend {
  padding: 0;
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-2);
}
.field legend { margin-bottom: 6px; }

.hint { font-size: 12px; color: var(--muted); }

input, select {
  width: 100%;
  height: 36px;
  padding: 0 10px;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  transition: border-color 120ms var(--ease), box-shadow 120ms var(--ease);
}
input::placeholder { color: var(--muted); opacity: 0.8; }
input:hover, select:hover { border-color: var(--muted); }
input:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.range { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.range input { min-width: 0; }
.range.dates { grid-template-columns: 1fr; }

.presets { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }

.chip-btn {
  height: 28px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface-sunk);
  font-size: 12.5px;
  color: var(--ink-2);
  cursor: pointer;
}
.chip-btn:hover { border-color: var(--accent); color: var(--accent); }

.filter-actions { display: flex; gap: 8px; padding-top: 4px; }
.filter-actions .btn-primary { flex: 1; }

.server-totals {
  margin-top: 28px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}
.server-totals h2 { font-size: 13px; font-weight: 600; margin-bottom: 8px; }
.server-totals dl { margin: 0; display: grid; gap: 4px; }
.server-totals dl div { display: flex; justify-content: space-between; font-size: 13px; }
.server-totals dt { color: var(--muted); }
.server-totals dd { margin: 0; font-weight: 500; }

/* ---------- Barra de resultados ---------- */

.toolbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.results { display: flex; align-items: baseline; gap: 12px; }
.results h1 { font-size: 20px; font-weight: 600; letter-spacing: -0.01em; }
.results-sub { color: var(--muted); font-size: 13px; }

.toolbar-actions { display: flex; align-items: center; gap: 12px; }

.page-size { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.page-size select { width: auto; height: 36px; }

.active-filters {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
  font-size: 13px;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 26px;
  padding: 0 4px 0 10px;
  border-radius: 13px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 500;
  font-size: 12.5px;
}
.chip button {
  width: 20px; height: 20px;
  border: 0; border-radius: 50%;
  background: transparent; color: inherit;
  font-size: 15px; line-height: 1;
  cursor: pointer;
}
.chip button:hover { background: var(--surface); }

/* ---------- Tabla ---------- */

.table-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.table-wrap[aria-busy="true"] tbody { opacity: 0.45; transition: opacity 120ms; }

table { width: 100%; border-collapse: collapse; }

.studies th, .series th {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: 9px 10px;
  background: var(--surface-sunk);
  border-bottom: 1px solid var(--line);
  text-align: left;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-2);
  white-space: nowrap;
}

.studies td, .series td {
  padding: var(--row-pad) 10px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
  white-space: nowrap;
}
.studies tbody tr:last-child td { border-bottom: 0; }
.studies tbody tr:hover td { background: var(--row-hover); }

.num, th.num { text-align: right; }
.center, th.center { text-align: center; }
.strong { font-weight: 600; }
.date .cell-main { font-weight: 400; color: var(--ink-2); }
.cell-main { display: block; font-weight: 500; }
.cell-sub { display: block; margin-top: 1px; font-size: 12px; color: var(--muted); }
.desc .cell-main { max-width: 260px; overflow: hidden; text-overflow: ellipsis; font-weight: 400; }
.aets { white-space: normal !important; min-width: 140px; }
.aet { white-space: nowrap; }
.actions { text-align: right; }

.copy {
  padding: 2px 4px;
  margin: -2px -4px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  font: inherit;
  cursor: copy;
}
.copy:hover { background: var(--accent-soft); color: var(--accent); }
.copy.copied { background: var(--accent-soft); color: var(--ok); }

.mod {
  display: inline-block;
  min-width: 30px;
  margin-right: 4px;
  padding: 1px 6px;
  border: 1px solid currentColor;
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 11.5px;
  font-weight: 500;
  text-align: center;
  color: var(--muted);
}
.mod-ct { color: var(--mod-ct); }
.mod-mr { color: var(--mod-mr); }
.mod-dx, .mod-cr, .mod-rf, .mod-xa { color: var(--mod-xr); }
.mod-us { color: var(--mod-us); }
.mod-mg { color: var(--mod-mg); }
.mod-pt, .mod-nm { color: var(--mod-nm); }

.aet {
  display: inline-block;
  margin-right: 6px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--ink-2);
}

.btn-row {
  height: 28px;
  padding: 0 10px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
}
.btn-row:hover { border-color: var(--accent); color: var(--accent); }

.table-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 56px 16px;
  text-align: center;
}
.state-title { font-weight: 600; font-size: 15px; }
.state-help { color: var(--muted); margin-bottom: 10px; }

.cell-state { padding: 32px 12px !important; text-align: center; color: var(--muted); white-space: normal !important; }
.cell-state.error { color: var(--danger); }

/* ---------- Indicador de carga ---------- */

.loader {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: color-mix(in srgb, var(--bg) 72%, transparent);
  animation: loader-in 200ms var(--ease);
}
.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.loader-text { font-size: 14px; font-weight: 500; color: var(--ink-2); }

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes loader-in { from { opacity: 0; } }

/* ---------- Paginación ---------- */

.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 12px;
  font-size: 13px;
  color: var(--muted);
}
.pages { display: flex; align-items: center; gap: 4px; }
.pages button {
  min-width: 32px;
  height: 32px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  color: var(--ink-2);
  cursor: pointer;
}
.pages button:hover:not(:disabled) { background: var(--surface); border-color: var(--line); }
.pages button[aria-current="page"] {
  background: var(--surface);
  border-color: var(--line-strong);
  color: var(--ink);
  font-weight: 600;
}
.pages .gap { padding: 0 4px; }

/* ---------- Diálogo de series ---------- */

.series-dialog {
  width: min(860px, calc(100vw - 32px));
  max-height: calc(100vh - 64px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 24px 60px rgba(10, 20, 30, 0.25);
}
.series-dialog[open] { animation: dialog-in 180ms var(--ease); }
.series-dialog::backdrop { background: rgba(12, 18, 24, 0.45); }

@keyframes dialog-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

.dialog-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 20px;
  border-bottom: 1px solid var(--line);
}
.dialog-head h2 { font-size: 17px; font-weight: 600; }
.dialog-meta { color: var(--muted); font-size: 13px; margin-top: 2px; }
.dialog-body { overflow: auto; max-height: calc(100vh - 180px); }

/* ---------- Avisos ---------- */

.toasts {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  max-width: 360px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 8px 24px rgba(10, 20, 30, 0.12);
  font-size: 13px;
  animation: toast-in 180ms var(--ease);
  transition: opacity 180ms, transform 180ms;
}
.toast.success { border-left-color: var(--ok); }
.toast.error { border-left-color: var(--danger); }
.toast.leaving { opacity: 0; transform: translateY(4px); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

/* ---------- Login ---------- */

.login-page {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 16px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  padding: 32px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.login-card .brand-mark { margin-bottom: 20px; }
.login-card h1 { font-size: 20px; font-weight: 600; letter-spacing: -0.01em; }
.login-sub { color: var(--muted); margin: 4px 0 24px; }
.login-card form { display: flex; flex-direction: column; gap: 16px; }
.login-card .btn-primary { margin-top: 4px; height: 40px; }

.login-error {
  margin-bottom: 16px;
  padding: 10px 12px;
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  color: var(--danger);
  font-size: 13px;
}

/* ---------- Responsivo ---------- */

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; height: auto; }
  .content { min-height: 70vh; }
  .sidebar {
    position: static;
    height: auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .filters { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
  .filter-actions { grid-column: 1 / -1; }
  .table-wrap { flex: none; }
}

@media (max-width: 640px) {
  .topbar { padding: 0 16px; }
  .user-name, .divider, .db-text { display: none; }
  .content, .sidebar { padding: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .spinner { animation: spin 1.6s linear infinite !important; }
}
