/* ===========================================================================
   BitMedico — components.css
   Tarjetas, botones, formularios, badges, tablas, alertas.

   Criterio visual: fondos claros y bordes MARCADOS (var(--bw), 2px por
   defecto). La separación se hace con borde, no con sombras difusas: se lee
   igual de bien en la pantalla del consultorio, en un celular a plena luz y en
   modo noche. También ayuda a quien no ve de cerca.
   =========================================================================== */

/* ── Tarjetas ─────────────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: var(--bw) solid var(--border);
  border-radius: var(--radius);
  padding: 1.125rem;
}
.card-pad-sm { padding: .75rem; }
.card-pad-lg { padding: 1.5rem; }

.card-head {
  display: flex; align-items: center; gap: .625rem;
  margin: -.25rem 0 .875rem;
}
.card-head h2, .card-head h3 { margin: 0; }
.card-body > :last-child { margin-bottom: 0; }

/* Tarjeta con acento del contexto: barra superior del color del rol */
.card-accent { border-top: 4px solid var(--accent); }
.card-hover { transition: border-color .15s, transform .15s; }
.card-hover:hover { border-color: var(--accent); transform: translateY(-2px); }

/* Tarjeta de bienvenida del dashboard */
.card-welcome {
  background: var(--accent-soft);
  border-color: var(--accent);
}

/* ── Botones ──────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .5rem;
  padding: .6875rem 1.125rem;
  border: var(--bw) solid transparent;
  border-radius: var(--radius-sm);
  font-weight: 600; font-size: .9375rem; line-height: 1.2;
  text-decoration: none; white-space: nowrap;
  /* 44px: el mínimo táctil que se puede tocar sin fallar en un celular */
  min-height: 44px;
  transition: background-color .15s, border-color .15s, color .15s, transform .06s;
  user-select: none;
}
.btn:hover { text-decoration: none; }
.btn:active { transform: translateY(1px); }
.btn:disabled, .btn.is-disabled { opacity: .5; cursor: not-allowed; transform: none; }

.btn-primary {
  background: var(--accent); color: var(--accent-ink); border-color: var(--accent);
}
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); }

.btn-outline {
  background: transparent; color: var(--accent); border-color: var(--accent);
}
.btn-outline:hover:not(:disabled) { background: var(--accent-soft); }

.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }

.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger:hover:not(:disabled) { filter: brightness(1.08); }

.btn-plain { background: transparent; color: var(--muted); border-color: transparent; }
.btn-plain:hover:not(:disabled) { color: var(--text); background: var(--surface-2); }

.btn-sm  { padding: .4375rem .75rem; font-size: .8125rem; min-height: 36px; }
.btn-lg  { padding: .875rem 1.5rem; font-size: 1.0625rem; min-height: 52px; }
.btn-block { display: flex; width: 100%; }
.btn-icon { padding: .5rem; min-height: 44px; min-width: 44px; }

/* Botón cargando: el texto se atenúa y aparece un spinner */
.btn.is-loading { pointer-events: none; position: relative; color: transparent !important; }
.btn.is-loading::after {
  content: ''; position: absolute; width: 18px; height: 18px;
  border: 2px solid currentColor; border-top-color: transparent; border-radius: 50%;
  animation: spin .6s linear infinite;
  color: var(--accent-ink);
}
.btn-outline.is-loading::after, .btn-ghost.is-loading::after { color: var(--accent); }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Formularios ──────────────────────────────────────────────────────────── */
.field { margin-bottom: 1rem; }
.label {
  display: block; margin-bottom: .375rem;
  font-weight: 600; font-size: .875rem; color: var(--text);
}
.label .req { color: var(--danger); margin-left: .125rem; }
.label .opt { color: var(--muted); font-weight: 400; margin-left: .25rem; font-size: .8125rem; }

.input, .select, .textarea {
  width: 100%;
  padding: .6875rem .875rem;
  background: var(--surface);
  color: var(--text);
  border: var(--bw) solid var(--border);
  border-radius: var(--radius-sm);
  min-height: 46px;
  transition: border-color .15s, box-shadow .15s;
  appearance: none;
}
.input::placeholder, .textarea::placeholder { color: var(--muted); }
.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.input:disabled, .select:disabled, .textarea:disabled {
  background: var(--surface-2); color: var(--muted); cursor: not-allowed;
}
.textarea { min-height: 110px; resize: vertical; line-height: 1.5; }

.select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%23888' stroke-width='2'%3E%3Cpath d='M4 6l4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right .75rem center;
  background-size: 16px;
  padding-right: 2.25rem;
}

.field.has-error .input, .field.has-error .select, .field.has-error .textarea {
  border-color: var(--danger);
}
.field-error { display: block; margin-top: .3125rem; font-size: .8125rem; color: var(--danger); font-weight: 500; }
.field-hint  { display: block; margin-top: .3125rem; font-size: .8125rem; color: var(--muted); }

/* Input con prefijo (lada telefónica, etc.) */
.input-group { display: flex; }
.input-group > .select { width: auto; flex: 0 0 auto; border-radius: var(--radius-sm) 0 0 var(--radius-sm); border-right: 0; }
/* min-width:0 es obligatorio aquí: por defecto un elemento flex no encoge por
   debajo de su min-content, y el min-content de un <input> lo fija su atributo
   `size` (~180px). Sumado al select de la lada, que no encoge, el grupo se salía
   de la pantalla en celulares de 320px. */
.input-group > .input  { min-width: 0; flex: 1 1 auto; border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }

/* Checkbox / radio */
.check { display: flex; align-items: flex-start; gap: .625rem; cursor: pointer; padding: .25rem 0; }
.check input { width: 20px; height: 20px; margin: .125rem 0 0; accent-color: var(--accent); flex: 0 0 auto; cursor: pointer; }
.check span { font-size: .9375rem; }

/* Opciones tipo tarjeta (el onboarding las usa en vez de <select>: se tocan
   mejor en celular y se leen de un vistazo) */
.choice-grid { display: grid; gap: .625rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 130px), 1fr)); }
.choice {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: .375rem; text-align: center;
  padding: .875rem .625rem; min-height: 76px;
  background: var(--surface);
  border: var(--bw) solid var(--border);
  border-radius: var(--radius-sm);
  font-weight: 600; font-size: .9375rem;
  cursor: pointer;
  transition: border-color .15s, background-color .15s, transform .06s;
}
.choice:hover { border-color: var(--accent); }
.choice:active { transform: scale(.98); }
.choice input { position: absolute; opacity: 0; pointer-events: none; }
.choice.is-selected, .choice:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
}
.choice-icon { font-size: 1.5rem; line-height: 1; }
.choice-sub { font-size: .75rem; font-weight: 400; color: var(--muted); }
.choice.is-selected .choice-sub { color: inherit; opacity: .8; }

/* ── Badges ───────────────────────────────────────────────────────────────── */
.badge {
  display: inline-flex; align-items: center; gap: .3125rem;
  padding: .1875rem .5rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: .75rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .03em;
  white-space: nowrap;
}
.badge-accent  { background: var(--accent-soft);  color: var(--accent);  border-color: var(--accent); }
.badge-success { background: var(--accent-soft);  color: var(--success); border-color: var(--success); }
.badge-warning { color: var(--warning); border-color: var(--warning); }
.badge-danger  { color: var(--danger);  border-color: var(--danger); }
.badge-muted   { color: var(--muted); }
.badge-dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }

/* ── Alertas ──────────────────────────────────────────────────────────────── */
.alert {
  display: flex; align-items: flex-start; gap: .625rem;
  padding: .875rem 1rem;
  border: var(--bw) solid var(--border);
  border-left-width: 5px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: .9375rem;
}
.alert-success { border-left-color: var(--success); }
.alert-warning { border-left-color: var(--warning); }
.alert-danger  { border-left-color: var(--danger); }
.alert-info    { border-left-color: var(--info); }
.alert-icon { flex: 0 0 auto; font-size: 1.125rem; line-height: 1.4; }
.alert > div > :last-child { margin-bottom: 0; }

/* ── Tablas ───────────────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border: var(--bw) solid var(--border); border-radius: var(--radius); }
.table { width: 100%; font-size: .9375rem; }
.table th, .table td { padding: .75rem .875rem; text-align: left; }
.table thead th {
  background: var(--surface-2);
  font-size: .75rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted);
  border-bottom: var(--bw) solid var(--border);
  white-space: nowrap;
}
.table tbody tr + tr td { border-top: 1px solid var(--border); }
.table tbody tr:hover td { background: var(--surface-2); }
.table td.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ── Avatar ───────────────────────────────────────────────────────────────── */
.avatar {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; flex: 0 0 auto;
  border-radius: 50%;
  border: var(--bw) solid var(--border);
  background: var(--accent-soft); color: var(--accent);
  font-weight: 700; font-size: .875rem;
  overflow: hidden;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-sm { width: 32px; height: 32px; font-size: .75rem; }
.avatar-lg { width: 64px; height: 64px; font-size: 1.25rem; }
.avatar-xl { width: 88px; height: 88px; font-size: 1.75rem; }

/* ── Estado vacío ─────────────────────────────────────────────────────────── */
.empty { text-align: center; padding: 2.5rem 1rem; color: var(--muted); }
.empty-icon { font-size: 2.5rem; margin-bottom: .5rem; opacity: .6; }
.empty h3 { color: var(--text); }
/* Dos salidas en un estado vacío: se centran y se apilan solas si no caben. */
.empty-actions { display: flex; flex-wrap: wrap; gap: .5rem; justify-content: center; }

/* ── Barra de progreso ────────────────────────────────────────────────────── */
.progress {
  height: 8px; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 999px; overflow: hidden;
}
.progress-bar { height: 100%; background: var(--accent); border-radius: 999px; transition: width .35s ease; }

/* ── Skeleton (carga) ─────────────────────────────────────────────────────── */
.skeleton {
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--border) 50%, var(--surface-2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.3s infinite;
  border-radius: var(--radius-sm);
}
@keyframes shimmer { to { background-position: -200% 0; } }

/* ── Separador con texto ──────────────────────────────────────────────────── */
.divider { display: flex; align-items: center; gap: .75rem; color: var(--muted); font-size: .8125rem; }
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: var(--border); }

/* ── Toast (avisos flotantes) ─────────────────────────────────────────────── */
.toast-host {
  /* Por encima de la hoja de la agenda (1000) y del diálogo (2000): si no,
     el aviso queda escondido detrás del modal, abajo. */
  position: fixed; z-index: 3000;
  left: 50%; transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom, 0px) + 5.5rem);
  display: flex; flex-direction: column; gap: .5rem;
  width: min(92vw, 420px);
  pointer-events: none;
}
@media (min-width: 900px) { .toast-host { bottom: 1.5rem; } }
.toast {
  pointer-events: auto;
  background: var(--surface);
  border: var(--bw) solid var(--border);
  border-left-width: 5px;
  border-radius: var(--radius-sm);
  padding: .75rem 1rem;
  font-size: .9375rem; font-weight: 500;
  box-shadow: 0 8px 24px rgb(0 0 0 / .16);
  animation: toast-in .2s ease-out;
}
.toast-success { border-left-color: var(--success); }
.toast-danger  { border-left-color: var(--danger); }
.toast-warning { border-left-color: var(--warning); }
.toast-info    { border-left-color: var(--info); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ── Diálogo modal propio (reemplaza alert/confirm/prompt nativos) ────────── */
.bm-dialog-ov {
  position: fixed; inset: 0; z-index: 2000;
  display: flex; align-items: center; justify-content: center; padding: 1rem;
  background: rgba(15, 23, 42, .5); backdrop-filter: blur(3px);
  animation: bm-dialog-fade .15s ease;
}
.bm-dialog {
  width: 100%; max-width: 400px;
  background: var(--surface); border: var(--bw) solid var(--border);
  border-radius: var(--radius); padding: 1.5rem;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .3);
  animation: bm-dialog-pop .18s ease-out;
}
.bm-dialog-title { margin: 0 0 .5rem; font-size: 1.15rem; }
.bm-dialog-msg { margin: 0 0 1.125rem; color: var(--muted); line-height: 1.55; }
.bm-dialog-title + .bm-dialog-msg { margin-top: -.125rem; }
.bm-dialog-input { margin-bottom: 1.25rem; }
.bm-dialog-foot { display: flex; gap: .625rem; justify-content: flex-end; flex-wrap: wrap; }
.bm-dialog-foot .btn { min-width: 96px; }
@keyframes bm-dialog-fade { from { opacity: 0; } }
@keyframes bm-dialog-pop { from { transform: translateY(10px) scale(.97); opacity: 0; } }
@media (max-width: 420px) {
  .bm-dialog-foot { flex-direction: column-reverse; }
  .bm-dialog-foot .btn { width: 100%; }
}
@media (prefers-reduced-motion: reduce) { .bm-dialog-ov, .bm-dialog { animation: none; } }

/* ── Modo "familiar autorizado" ─────────────────────────────────────────────
   Alguien está viendo el expediente de OTRA persona. La barra es fija y de un
   color que no se confunde con nada del producto: confundirse de expediente en
   una plataforma médica es el peor error posible. */
.care-bar {
  position: sticky; top: 0; z-index: 60;
  display: flex; align-items: center; gap: .75rem; flex-wrap: wrap;
  padding: .5rem 1rem; background: #7c3aed; color: #fff;
  font-size: .875rem; font-weight: 600;
}
.care-bar-who { flex: 1 1 auto; min-width: 0; }
.care-bar-ro {
  flex: 0 0 auto; font-size: .6875rem; font-weight: 800; text-transform: uppercase;
  letter-spacing: .04em; padding: .1rem .45rem; border-radius: 999px;
  background: rgba(255,255,255,.22);
}
.care-bar-out {
  flex: 0 0 auto; padding: .25rem .75rem; border-radius: 999px;
  background: #fff; color: #7c3aed; font-weight: 800; font-size: .8125rem;
}
.care-bar-out:hover { background: #f4f0ff; }

/* Tarjetas de familiar en /accesos */
.care-row { display: flex; align-items: center; gap: .75rem; padding: .75rem 0; border-top: 1px solid var(--border); }
.care-row:first-of-type { border-top: 0; }
.care-row-main { flex: 1 1 auto; min-width: 0; }
.care-row-name { font-weight: 700; margin: 0; }
.care-row-meta { margin: .1rem 0 0; font-size: .75rem; color: var(--muted); }
.care-badge {
  font-size: .625rem; font-weight: 800; text-transform: uppercase; letter-spacing: .03em;
  padding: .1rem .4rem; border-radius: 999px; margin-left: .35rem;
  background: color-mix(in srgb, var(--warning) 20%, transparent); color: var(--warning);
}
.care-badge.is-active { background: color-mix(in srgb, var(--success, #16a34a) 18%, transparent); color: var(--success, #16a34a); }
.care-share { display: flex; gap: .5rem; margin-top: .75rem; flex-wrap: wrap; }
.care-share .input { flex: 1 1 220px; }

/* ── Captura de dirección con mapa (consulta a domicilio) ─────────────────── */
.dirmap-row { display: flex; gap: .5rem; align-items: center; }
.dirmap-row .input { flex: 1 1 auto; }
.dirmap-canvas {
  height: 200px; margin-top: .5rem; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface-2);
  display: flex; align-items: center; justify-content: center;
}
.dirmap-hint { margin: .35rem 0 0; }
.dirmap-off { margin: 0; padding: .75rem; text-align: center; }
/* La ficha de la visita: a quién voy a ver y con qué respaldo */
.hv-card { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: .75rem; margin-top: .75rem; }
.hv-trust { display: inline-flex; align-items: center; gap: .3rem; font-size: .75rem; font-weight: 700; padding: .15rem .5rem; border-radius: 999px; }
.hv-trust.is-known    { background: color-mix(in srgb, var(--success, #16a34a) 16%, transparent); color: var(--success, #16a34a); }
.hv-trust.is-verified { background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent); }
.hv-trust.is-unknown  { background: color-mix(in srgb, var(--warning) 20%, transparent); color: var(--warning); }
.hv-actions { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: .6rem; }
.hv-panic { background: #dc2626; color: #fff; font-weight: 800; }
.hv-panic:hover { background: #b91c1c; }
.hv-map { width: 100%; height: auto; border-radius: var(--radius-sm); border: 1px solid var(--border); display: block; }
.hv-panic-txt { color: #dc2626; font-weight: 700; }
.cal-appt-mod.is-home { background: color-mix(in srgb, #7c3aed 16%, transparent); color: #7c3aed; }

/* ── Chips de opción (radio o checkbox disfrazado de pastilla) ─────────────────
   Nacieron en Apariencia del médico y los reusa Admin → Apariencia, así que
   viven aquí y no en el CSS de un módulo. El input real se esconde pero sigue
   mandando: el label lo activa, el teclado lo alcanza y el foco se ve. */
.ui-chips { display: flex; flex-wrap: wrap; gap: .375rem; }
.ui-chip {
  display: inline-flex; align-items: center; gap: .3rem; padding: .3rem .625rem;
  border: var(--bw) solid var(--border); border-radius: 999px;
  background: var(--surface); color: var(--muted);
  font-size: .8125rem; cursor: pointer; user-select: none;
}
.ui-chip:hover { border-color: var(--accent); }
.ui-chip.is-on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.ui-chip input { position: absolute; opacity: 0; width: 0; height: 0; }
.ui-chip:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ── Botón primario en modo noche ───────────────────────────────────────────
   De noche el acento de cada rol es un tono CLARO, para que se lea sobre el
   fondo oscuro. Sobre ese claro la tinta tiene que ser oscura — y por eso el
   botón primario salía con el icono y el texto casi negros: legible, pero
   apagado y distinto al de día.

   Aquí el BOTÓN, y sólo el botón, toma un tono profundo del mismo color para
   poder llevar blanco encima igual que de día. Medido contra blanco (mínimo
   4.5): médico 5.06 · paciente 5.33 · clínica 7.55 · admin 9.44. Con el acento
   claro tal cual daba 2.06 en médico, que es ilegible.

   Va en components.css y no en theme.css porque ese lo regenera
   Theme::generate() cada vez que el admin guarda colores. */
[data-theme="dark"] .ctx-patient { --accent-btn: #0d7a58; }
[data-theme="dark"] .ctx-doctor  { --accent-btn: #2f6fbf; }
[data-theme="dark"] .ctx-clinic  { --accent-btn: #5b3fb0; }
[data-theme="dark"] .ctx-admin   { --accent-btn: #3a4759; }
[data-theme="dark"] .btn-primary {
  background: var(--accent-btn, var(--accent));
  border-color: var(--accent-btn, var(--accent));
  color: #fff;
}

@media (prefers-color-scheme: dark) {
  [data-theme="auto"] .ctx-patient { --accent-btn: #0d7a58; }
  [data-theme="auto"] .ctx-doctor  { --accent-btn: #2f6fbf; }
  [data-theme="auto"] .ctx-clinic  { --accent-btn: #5b3fb0; }
  [data-theme="auto"] .ctx-admin   { --accent-btn: #3a4759; }
  [data-theme="auto"] .btn-primary {
    background: var(--accent-btn, var(--accent));
    border-color: var(--accent-btn, var(--accent));
    color: #fff;
  }
}

/* ── Tema propio del médico + modo noche ────────────────────────────────────
   DoctorTheme escribe los colores del médico EN LÍNEA en el <body>
   (`--bg`, `--accent-soft`, `--sb-icon`…). Un estilo en línea le gana a
   cualquier hoja, así que de noche esos valores —elegidos sobre papel claro—
   se quedaban puestos: el fondo seguía siendo #f4f6f8 mientras el texto pasaba
   a claro. Resultado: el título «Mi consultorio» y los botones de contorno
   desaparecían, y la tarjeta de bienvenida hacía un degradado de blanco a
   oscuro a media tarjeta.

   Aquí se le devuelven al modo noche SÓLO los tokens de fondo. El ACENTO que
   el médico eligió se respeta —funciona en los dos temas— y su tinte suave se
   recalcula sobre el fondo oscuro para que su color se siga viendo.

   Es el único sitio del proyecto con !important sobre estas variables, y es a
   propósito: es la única manera de ganarle a un `style=""` en el <body>. Por
   eso NO va en ui-theme.css, que tiene la regla de no usarlo nunca. */
[data-theme="dark"] body {
  --bg:           #0f1621 !important;
  --accent-soft:  color-mix(in srgb, var(--accent) 22%, #0f1621) !important;
  --doctor-soft:  color-mix(in srgb, var(--accent) 22%, #0f1621) !important;
  --patient-soft: color-mix(in srgb, var(--accent) 22%, #0f1621) !important;
  --sb-icon:      var(--text)   !important;
  --sb-line:      var(--border) !important;
}

@media (prefers-color-scheme: dark) {
  [data-theme="auto"] body {
    --bg:           #0f1621 !important;
    --accent-soft:  color-mix(in srgb, var(--accent) 22%, #0f1621) !important;
    --doctor-soft:  color-mix(in srgb, var(--accent) 22%, #0f1621) !important;
    --patient-soft: color-mix(in srgb, var(--accent) 22%, #0f1621) !important;
    --sb-icon:      var(--text)   !important;
    --sb-line:      var(--border) !important;
  }
}
