:root {
  --bg: #eef2f6;
  --surface: #ffffff;
  --surface-2: #f7fafc;
  --ink: #15232e;
  --muted: #5f7484;
  --muted-2: #8294a2;
  --line: #e4e9f0;
  --line-strong: #d2dbe4;
  --primary: #0e7c86;
  --primary-dark: #0a5f67;
  --primary-soft: #e6f3f4;
  --primary-softer: #f2f9fa;
  --accent: #1769aa;
  --warn: #a86a13;
  --warn-soft: #fbf0db;
  --ok: #1c7a49;
  --ok-soft: #e6f4ec;
  --danger: #b3261e;
  --danger-soft: #fbeae9;
  --radius: 12px;
  --radius-sm: 9px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(20, 40, 60, .05), 0 1px 3px rgba(20, 40, 60, .05);
  --shadow-md: 0 4px 14px rgba(20, 40, 60, .07), 0 12px 32px rgba(20, 40, 60, .06);
  --shadow: 0 4px 14px rgba(20, 40, 60, .07), 0 12px 32px rgba(20, 40, 60, .06);
  --ring: 0 0 0 3px rgba(14, 124, 134, .16);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: linear-gradient(180deg, #f4f7fa 0%, #e9eef3 100%);
  background-attachment: fixed;
  color: var(--ink);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}
::selection { background: var(--primary-soft); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* Topbar */
.topbar {
  display: flex; align-items: center; gap: 22px;
  background: rgba(255, 255, 255, .9);
  backdrop-filter: saturate(1.4) blur(8px);
  border-bottom: 1px solid var(--line);
  padding: 0 26px; height: 62px;
  position: sticky; top: 0; z-index: 10;
  box-shadow: 0 1px 0 rgba(20, 40, 60, .03), 0 6px 20px -12px rgba(20, 40, 60, .25);
}
.brand { display: flex; align-items: center; gap: 11px; font-size: 16.5px; letter-spacing: -.01em; }
.brand strong { color: var(--primary-dark); font-weight: 700; }
.brand-mark {
  display: grid; place-items: center;
  width: 32px; height: 32px; border-radius: 9px;
  background: linear-gradient(140deg, #15929e 0%, #0a5f67 100%);
  color: #fff; font-weight: 800; letter-spacing: -.02em;
  box-shadow: 0 2px 6px rgba(10, 95, 103, .35);
}
.brand-mark.big { width: 56px; height: 56px; border-radius: 16px; font-size: 28px; margin: 0 auto; }
.topnav { display: flex; gap: 2px; }
.nav-btn {
  border: none; background: none; padding: 8px 14px; border-radius: 8px;
  color: var(--muted); font-size: 14.5px; font-weight: 500; cursor: pointer;
  transition: background .15s, color .15s;
}
.nav-btn:hover { background: var(--surface-2); color: var(--ink); }
.nav-btn.active { background: var(--primary-soft); color: var(--primary-dark); font-weight: 600; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 16px; }
.user-info { display: flex; align-items: center; gap: 9px; }
#user-name { font-weight: 600; font-size: 14.5px; }
.role-badge {
  font-size: 10.5px; text-transform: uppercase; letter-spacing: .06em;
  background: var(--primary-soft); color: var(--primary-dark);
  padding: 3px 9px; border-radius: 20px; font-weight: 700;
}

/* Layout */
.view { max-width: 1060px; margin: 0 auto; padding: 30px 26px 90px; }
.view-login { max-width: none; display: grid; place-items: center; min-height: 100vh; }
.view-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 22px; }
.view-head h2 { margin: 0; font-size: 23px; font-weight: 700; letter-spacing: -.02em; }

/* Login */
.login-card {
  background: var(--surface); border-radius: var(--radius-lg); box-shadow: var(--shadow-md);
  padding: 40px 38px; width: 372px; border: 1px solid var(--line);
}
.login-brand { text-align: center; margin-bottom: 26px; }
.login-brand h1 { font-size: 22px; margin: 16px 0 4px; font-weight: 700; letter-spacing: -.02em; }

/* Field labels (editor, login, user/surgeon forms) */
.card label, .login-card label, .modal .field label {
  display: block; margin-bottom: 14px;
  font-size: 12.5px; font-weight: 600; color: var(--muted); letter-spacing: .005em;
}

/* Inputs */
input, select, textarea {
  width: 100%; margin-top: 6px; padding: 9px 12px;
  border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
  font: inherit; color: var(--ink); background: var(--surface);
  transition: border-color .15s, box-shadow .15s, background .15s;
  color-scheme: light;
}
input::placeholder, textarea::placeholder { color: var(--muted-2); }
input:hover, select:hover, textarea:hover { border-color: #bfccd8; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: var(--ring); }
textarea { resize: vertical; }
/* Campos del informe: crecen con el contenido (autoGrow en JS), sin scroll. */
textarea[data-field] { line-height: 1.6; overflow: hidden; resize: none; }
textarea.big { min-height: 128px; }

/* Buttons */
.btn {
  border: 1px solid transparent; border-radius: var(--radius-sm); padding: 9px 17px;
  font: inherit; font-weight: 600; cursor: pointer; background: var(--surface);
  transition: background .15s, border-color .15s, box-shadow .15s, transform .05s;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); color: #fff; box-shadow: 0 1px 2px rgba(10, 95, 103, .25); }
.btn-primary:hover { background: var(--primary-dark); box-shadow: 0 2px 8px rgba(10, 95, 103, .3); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; box-shadow: none; }
.btn-ghost { background: transparent; color: var(--muted); border-color: transparent; }
.btn-ghost:hover { background: var(--surface-2); color: var(--ink); }
.btn-block { width: 100%; margin-top: 8px; padding: 11px; }
.btn-danger-soft { color: var(--danger); }
.btn-danger-soft:hover { background: var(--danger-soft); }
/* Un <label> que actúa como botón (subida de fichero oculto). */
.btn-upload { display: inline-flex; align-items: center; gap: 6px; }

/* Fila de acciones (subir/descargar plantilla, etc.). */
.form-row-actions { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; margin-top: 16px; }

/* Vista previa de la firma del médico en el modal de datos profesionales. */
.prof-firma-preview { margin: 8px 0; }
.prof-firma-preview img { max-width: 240px; max-height: 90px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: #fff; padding: 4px; }

/* Toolbar */
.toolbar { display: flex; gap: 14px; align-items: center; margin-bottom: 20px; flex-wrap: wrap; }
.toolbar #search { flex: 1; min-width: 240px; margin: 0; padding: 10px 14px; }
.filters { display: flex; gap: 6px; }
.chip {
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--muted);
  padding: 8px 15px; border-radius: 20px; cursor: pointer; font-size: 13.5px; font-weight: 500;
  transition: background .15s, color .15s, border-color .15s;
}
.chip:hover { border-color: #bfccd8; color: var(--ink); }
.chip.active { background: var(--primary); color: #fff; border-color: var(--primary); font-weight: 600; }

/* Cards / fieldsets */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow-sm); padding: 20px 22px; margin: 0 0 18px;
}
.card legend {
  /* float saca a la legend de su posición por defecto SOBRE el borde superior;
     así el borde queda entero y la etiqueta cae dentro de la tarjeta. */
  float: left; width: 100%;
  font-weight: 700; color: var(--primary-dark); padding: 0; margin: 0 0 14px;
  font-size: 11.5px; text-transform: uppercase; letter-spacing: .07em;
  display: flex; align-items: center; gap: 8px;
}
.card > legend ~ * { clear: both; }   /* TODO el contenido va debajo de la legend (robusto aunque haya hijos ocultos) */
.card legend::before {
  content: ""; width: 7px; height: 7px; border-radius: 50%;
  background: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); flex: none;
}
.editor-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.editor-grid .card { margin-bottom: 0; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 18px; }
.grid-2 .span-2, .span-2 { grid-column: 1 / -1; }
#firmante-search-wrap { max-width: 560px; }

/* Reports table */
.reports-table { width: 100%; border-collapse: separate; border-spacing: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow-sm); }
.reports-table th, .reports-table td { text-align: left; padding: 13px 16px; border-bottom: 1px solid var(--line); font-size: 14px; }
.reports-table th { background: var(--surface-2); color: var(--muted); font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; }
.reports-table tbody tr { cursor: pointer; transition: background .12s; }
.reports-table tbody tr:last-child td { border-bottom: 0; }
.reports-table tbody tr:hover { background: var(--primary-softer); }
.reports-table .pt-name { font-weight: 600; color: var(--ink); }
.empty { text-align: center; color: var(--muted); padding: 54px 0; }

/* Status badges */
.status-badge, .badge { display: inline-flex; align-items: center; gap: 6px; padding: 4px 11px; border-radius: 20px; font-size: 11.5px; font-weight: 700; letter-spacing: .02em; text-transform: uppercase; }
.status-badge::before, .badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .8; }
.badge-borrador, .status-badge.borrador { background: var(--warn-soft); color: var(--warn); }
.badge-firmado, .status-badge.firmado { background: var(--ok-soft); color: var(--ok); }
/* Estado del informe en el editor: prominente, visible a primera vista. */
#report-status { font-size: 12.5px; padding: 6px 16px; border: 1px solid transparent; }
#report-status.borrador { border-color: #e8cfa0; }
#report-status.firmado { border-color: #bfe3cf; }

/* Editor */
.editor-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.editor-head-info { display: flex; align-items: center; gap: 14px; }
.save-status { font-size: 13px; color: var(--muted); font-weight: 500; }
.save-status.saving { color: var(--accent); }
.save-status.saved { color: var(--ok); }
.save-status.error { color: var(--danger); }
.editor-actions {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  margin-top: 22px; padding: 16px 20px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow-sm);
}
.editor-actions .btn-primary { padding: 10px 22px; }

.signed-banner {
  background: linear-gradient(180deg, #ecf7f0, var(--ok-soft)); border: 1px solid #c4e4d1; color: #14532d;
  border-radius: var(--radius); padding: 16px 20px; margin-bottom: 18px;
}
.signed-banner .sig-hash { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; word-break: break-all; color: var(--muted); }

.access-summary { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 20px; box-shadow: var(--shadow-sm); margin-top: 16px; }
.access-summary h4 { margin: 0 0 8px; font-size: 14px; }
.access-summary ul { margin: 6px 0 0; padding-left: 18px; }

/* Patient / autocomplete search */
.patient-search { position: relative; margin-bottom: 10px; }
.autocomplete { position: relative; }
.search-results {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 5;
  background: #fff; border: 1px solid var(--line-strong); border-radius: var(--radius-sm); box-shadow: var(--shadow-md);
  max-height: 244px; overflow-y: auto; padding: 4px;
}
.search-results div { padding: 9px 12px; cursor: pointer; font-size: 14px; border-radius: 7px; }
.search-results div:hover { background: var(--primary-soft); }

/* Modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(15, 30, 45, .5); backdrop-filter: blur(2px); display: grid; place-items: center; z-index: 50; padding: 20px; }
.modal { background: #fff; border-radius: var(--radius-lg); padding: 28px; width: 470px; max-width: 100%; max-height: 90vh; overflow-y: auto; box-shadow: var(--shadow-md); border: 1px solid var(--line); }
.modal h3 { margin: 0 0 10px; font-size: 19px; font-weight: 700; letter-spacing: -.01em; }
.share-block { margin: 16px 0; display: grid; gap: 10px; }
.check { display: flex; align-items: center; gap: 10px; font-weight: 500; color: var(--ink); margin: 0; }
.check input { width: auto; margin: 0; }
.share-label { font-weight: 700; margin: 14px 0 6px; }
/* Bloque de adjunto dentro de la tarjeta Paciente (debajo de Sexo). */
.patient-images { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--border); }
.patient-images .share-label { display: block; margin-top: 0; }
#indicacion-current { margin-top: 8px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.indicacion-link { font-weight: 600; color: var(--primary-dark); text-decoration: none; }
.indicacion-link:hover { text-decoration: underline; }
.share-user-list { max-height: 200px; overflow-y: auto; border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 6px; }
.share-user-list label { display: flex; align-items: center; gap: 10px; padding: 8px; margin: 0; font-weight: 500; color: var(--ink); border-radius: 7px; }
.share-user-list label:hover { background: var(--surface-2); }
.share-user-list input { width: auto; margin: 0; }
.share-user-role { margin-left: auto; font-size: 11px; color: var(--muted); text-transform: uppercase; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 24px; }

/* Form error / toast */
.form-error { color: var(--danger); font-size: 13px; min-height: 18px; margin-bottom: 8px; }
.toast {
  position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 13px 22px; border-radius: 10px;
  box-shadow: var(--shadow-md); z-index: 100; font-size: 14px; max-width: 90%; font-weight: 500;
}
.toast.ok { background: var(--ok); }
.toast.error { background: var(--danger); }

/* Responsive (móvil / tablet) */
@media (max-width: 720px) {
  html, body { overflow-x: hidden; }       /* nada debe desbordar a la derecha */
  .editor-grid { grid-template-columns: 1fr; }
  .grid-2 { grid-template-columns: 1fr; }
  .view { padding: 18px 14px 80px; }

  /* Barra superior: ALTURA AUTOMÁTICA (antes 62px fija → al envolver, las filas
     se salían y se solapaban con el contenido). Marca + botones en la fila 1;
     la navegación en su propia fila, deslizable. El nombre/rol se ocultan en
     móvil para que quepa sin desbordar. */
  .topbar { flex-wrap: wrap; height: auto; min-height: 54px; padding: 9px 12px; gap: 6px 8px; }
  .brand { font-size: 14.5px; gap: 8px; }
  .brand-mark { width: 28px; height: 28px; }
  .topbar-right { margin-left: auto; gap: 4px; }
  .topbar-right .btn { padding: 6px 10px; font-size: 13px; }
  .user-info { display: none; }
  .topnav { order: 3; width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; gap: 4px; }

  /* Cabeceras y barras que envuelven en vez de desbordar. */
  .view-head { flex-wrap: wrap; gap: 10px; }
  .editor-head-info { flex-wrap: wrap; }
  .row-actions { flex-wrap: wrap; }

  /* Tablas anchas: scroll horizontal dentro de su contenedor. */
  #reports-container, #users-container, #surgeons-container { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .reports-table { min-width: 560px; }

  /* Modales y login a casi todo el ancho. */
  .modal { width: 92vw; max-width: 92vw; }
  .login-card { width: 92vw; max-width: 360px; }
}

/* Read-only editor */
.readonly input, .readonly select, .readonly textarea { background: var(--surface-2); color: var(--ink); cursor: default; border-color: var(--line); }
.readonly input:hover, .readonly select:hover, .readonly textarea:hover { border-color: var(--line); }

/* Historial de auditoría */
#audit-history { margin-top: 16px; }
.audit-list { list-style: none; margin: 6px 0 0; padding: 0; }
.audit-list li { display: grid; grid-template-columns: 150px 1fr auto; gap: 10px; align-items: baseline; padding: 8px 0; border-top: 1px solid var(--line); font-size: 0.92em; }
.audit-list li:first-child { border-top: 0; }
.audit-when { color: var(--muted); white-space: nowrap; }
.audit-who { color: var(--muted); }
@media (max-width: 720px) {
  .audit-list li { grid-template-columns: 1fr; gap: 2px; }
}

/* Botón admin "Devolver a borrador" sobre informe firmado */
.admin-revert-wrap { display: flex; justify-content: flex-end; margin-bottom: 18px; }

/* Acciones por fila en tablas de gestión (cirujanos, usuarios) */
.row-actions { display: flex; gap: 6px; white-space: nowrap; }

/* Campo con botón al lado (p. ej. fecha + "Hoy") */
.field-with-btn { display: flex; gap: 8px; align-items: center; }
.field-with-btn input { flex: 1; min-width: 0; }
.field-with-btn .btn { margin-top: 6px; border-color: var(--line-strong); color: var(--primary-dark); }
.field-with-btn .btn:hover { background: var(--primary-soft); border-color: var(--primary-soft); }
.btn-sm { padding: 8px 13px; font-size: 0.85em; white-space: nowrap; }
.btn-sm:disabled { opacity: 0.5; cursor: default; }

/* Imágenes del informe */
#images-upload-wrap { margin-bottom: 14px; }
#images-upload-wrap input[type="file"] { width: auto; border: none; padding: 0; margin-top: 0; font-size: 13.5px; }
#images-upload-wrap input[type="file"]::file-selector-button {
  font: inherit; font-weight: 600; cursor: pointer; margin-right: 12px;
  padding: 8px 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--primary-dark);
  transition: background .15s, border-color .15s;
}
#images-upload-wrap input[type="file"]::file-selector-button:hover { background: var(--primary-soft); border-color: var(--primary-soft); }
#images-hint { margin: 8px 0 0; }
.images-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; }
.image-item { margin: 0; border: 1px solid var(--line); border-radius: var(--radius); padding: 8px; background: var(--surface); box-shadow: var(--shadow-sm); }
.image-item img { width: 100%; height: 140px; object-fit: cover; border-radius: 7px; background: var(--surface-2); display: block; }
.image-item .image-caption { margin-top: 6px; font-size: 13px; }
.image-item figcaption { margin-top: 6px; font-size: 13px; color: var(--muted); min-height: 1em; }
.image-item .image-del { margin-top: 4px; color: var(--danger); padding: 4px 8px; }
