:root {
  --ink: #16233a;
  --ink-soft: #4b5a70;
  --desk: #e9edf2;
  --paper: #ffffff;
  --line: #cfd7e1;
  --pen: #2340a8;
  --pen-soft: #e3e8f8;
  --dawn: #d98a1c;
  --dusk: #c2571a;
  --night: #36437f;
  --warn: #a8261c;
  --warn-soft: #fbeceb;
  --ok: #1d6b45;
  --radius: 10px;
  --sans: 'Atkinson Hyperlegible Next', system-ui, sans-serif;
  --mono: 'Atkinson Hyperlegible Mono', ui-monospace, monospace;
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--desk);
  color: var(--ink);
  font: 400 1rem/1.5 var(--sans);
}

button, input, textarea { font: inherit; color: inherit; }

:focus-visible { outline: 3px solid var(--pen); outline-offset: 2px; }

/* ---------- barra superior ---------- */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px 24px;
  padding: 14px max(16px, calc((100vw - 1120px) / 2));
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { margin: 0; font-size: 1.25rem; font-weight: 800; letter-spacing: -0.01em; }
.topbar-main { display: grid; gap: 2px; min-width: 0; flex: 1 1 360px; }
.scheme-info { margin: 0; color: var(--ink-soft); font-size: 0.95rem; }
.scheme-info strong { font-family: var(--mono); color: var(--pen); margin-right: 8px; }

.load { display: grid; gap: 6px; margin-bottom: 10px; padding-bottom: 14px; border-bottom: 1px dashed var(--line); }
.load label { font-size: 0.9rem; color: var(--ink-soft); }
.load-row { display: flex; gap: 8px; }
.load input {
  width: 10ch;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-family: var(--mono);
  font-size: 1.05rem;
  letter-spacing: 0.05em;
  text-align: center;
}

/* ---------- botones ---------- */
.btn {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 16px;
  font-weight: 600;
  cursor: pointer;
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--pen); color: #fff; padding: 12px 28px; font-size: 1.05rem; }
.btn-primary:not(:disabled):hover { background: #1a3189; }
.btn-quiet { background: var(--paper); border-color: var(--line); }
.btn-quiet:hover { border-color: var(--pen); color: var(--pen); }
.btn-small { padding: 4px 10px; font-size: 0.875rem; }

/* ---------- área de trabajo ---------- */
.workspace {
  max-width: 1120px;
  margin: 0 auto;
  padding: 20px 16px 48px;
  display: grid;
  gap: 18px;
}

.banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  padding: 12px 16px;
  border-left: 4px solid var(--pen);
  background: var(--pen-soft);
  border-radius: 0 var(--radius) var(--radius) 0;
}
.banner strong { font-family: var(--mono); }

.pro {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0 16px;
}
.pro summary {
  display: flex;
  gap: 12px;
  align-items: baseline;
  padding: 12px 0;
  cursor: pointer;
  font-weight: 600;
}
.pro-summary { font-weight: 400; color: var(--ink-soft); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pro-fields { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.pro-fields label { display: grid; gap: 4px; font-size: 0.9rem; color: var(--ink-soft); }
.pro-fields input {
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
}
.hint { margin: 10px 0 14px; font-size: 0.85rem; color: var(--ink-soft); }

.editors { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.editor { display: grid; gap: 6px; align-content: start; }
.editor-clean { grid-template-rows: auto 1fr; }
.editor-clean textarea { height: 100%; }
.editor label { font-weight: 600; }
.editor-head { display: flex; justify-content: space-between; align-items: center; }
.editor textarea {
  width: 100%;
  resize: vertical;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  font-family: var(--mono);
  font-size: 0.98rem;
  line-height: 1.6;
}
#entrada { border-color: #9fb0c4; }
#limpio { background: #f6f8fa; color: var(--ink-soft); }

.errors {
  margin: 0;
  padding: 10px 16px 10px 36px;
  background: var(--warn-soft);
  color: var(--warn);
  border-radius: var(--radius);
  font-size: 0.92rem;
}
.errors code { font-family: var(--mono); color: var(--ink); }

.actions { display: flex; justify-content: flex-end; gap: 12px; flex-wrap: wrap; }
.status { margin: -6px 0 0; text-align: right; min-height: 1.5em; color: var(--ok); }
.status.is-error { color: var(--warn); }

/* ---------- la hoja (vista previa = impresión) ---------- */
.sheet-wrap { overflow-x: auto; }
/* En pantallas anchas la vista previa mide exactamente una hoja carta (8,5 x 11 in)
   con los mismos márgenes que la impresión, y marca dónde se corta cada página. */
.sheet {
  position: relative;
  width: 100%;
  max-width: 820px;
  min-height: 420px;
  margin: 0 auto;
  padding: 36px 40px 28px;
  background: var(--paper);
  box-shadow: 0 1px 2px rgba(22, 35, 58, 0.12), 0 12px 32px -12px rgba(22, 35, 58, 0.25);
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.sheet-head { border-bottom: 2px solid var(--ink); padding-bottom: 10px; }
.sheet-head h2 { margin: 0; font-size: 1.6rem; font-weight: 800; letter-spacing: -0.015em; }
.sheet-head time { color: var(--ink-soft); font-size: 0.95rem; text-align: right; }

.empty { margin: auto; color: var(--ink-soft); text-align: center; max-width: 34ch; }

.plan { width: 100%; border-collapse: collapse; }
.plan th, .plan td { padding: 12px 8px; }
.plan thead th { font-weight: 600; font-size: 0.95rem; vertical-align: bottom; }
.plan thead th:first-child { text-align: left; }
.plan .moment { display: grid; justify-items: center; gap: 2px; }
.plan .moment svg { width: 46px; height: 46px; }
.plan tbody tr { border-top: 1px solid var(--line); }
.plan tbody th { text-align: left; font-size: 1.35rem; font-weight: 600; line-height: 1.2; }
.plan tbody td { text-align: center; width: 22%; }
.med-detail { display: block; font-size: 0.95rem; font-weight: 400; color: var(--ink-soft); }

.dose { display: inline-flex; flex-wrap: wrap; justify-content: center; gap: 4px; max-width: 120px; }
.dose svg { width: 30px; height: 30px; }
.dose-none { color: #9aa6b5; font-size: 1.4rem; }
.dose-units { font-size: 1.45rem; font-weight: 800; }
.dose-units small { font-size: 0.85rem; font-weight: 600; margin-left: 2px; }
.dose-text { font-size: 1.15rem; font-weight: 600; }

/* Secciones aparte de la tabla principal: banda oscura para que la separación se note (también en B/N). */
.timed { margin-top: 16px; }
.timed h3 {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 4px;
  padding: 9px 14px;
  background: var(--ink);
  color: #fff;
  border-radius: 8px;
  font-size: 1.15rem;
  font-weight: 800;
  letter-spacing: -0.005em;
  -webkit-print-color-adjust: exact;
  print-color-adjust: exact;
}
.timed h3 svg { width: 22px; height: 22px; flex: none; }
.timed h3 small { margin-left: auto; font-size: 0.85rem; font-weight: 400; opacity: 0.85; }
.timed-med:first-of-type { border-top: none; }
.timed-med { border-top: 1px solid var(--line); padding: 12px 0; display: grid; grid-template-columns: minmax(150px, 1fr) 3fr; gap: 12px; align-items: center; }
.timed-med > div:first-child { font-size: 1.35rem; font-weight: 600; line-height: 1.2; }
.timed-slots { display: flex; flex-wrap: wrap; gap: 10px; }
.slot {
  display: grid;
  justify-items: center;
  gap: 6px;
  min-width: 92px;
  padding: 8px 10px 10px;
  border: 1.5px solid var(--line);
  border-radius: 12px;
}
.slot-time { display: flex; align-items: center; gap: 4px; font-weight: 800; font-size: 1.1rem; }
.slot-time svg { width: 18px; height: 18px; }
.slot input {
  width: 78px;
  padding: 3px 6px;
  border: 1px dashed var(--pen);
  border-radius: 6px;
  text-align: center;
  font-family: var(--mono);
  font-size: 0.95rem;
  background: var(--pen-soft);
}
.slot input[aria-invalid='true'] { border: 1.5px solid var(--warn); background: var(--warn-soft); }

.sheet-foot {
  margin-top: auto;
  display: grid;
  grid-template-columns: auto minmax(0, 1.1fr) minmax(0, 1fr);
  gap: 22px;
  align-items: center;
  border-top: 2px solid var(--ink);
  padding-top: 16px;
}
.qr { display: grid; justify-items: center; gap: 4px; }
.qr svg, .qr .qr-pending { width: 116px; height: 116px; }
.qr .qr-pending {
  display: grid;
  place-items: center;
  border: 1.5px dashed var(--line);
  border-radius: 8px;
  padding: 10px;
  text-align: center;
  font-size: 0.78rem;
  color: var(--ink-soft);
}
.qr-code { font-family: var(--mono); font-weight: 600; font-size: 1.3rem; letter-spacing: 0.08em; }
.meta { display: grid; gap: 0; font-size: 0.95rem; line-height: 1.3; }
.meta .by-label { font-size: 0.85rem; color: var(--ink-soft); }
.meta .who { font-weight: 600; font-size: 1.05rem; }
.meta .muted { color: var(--ink-soft); }
.howto { margin: 0; padding: 0 20px; border-left: 1px solid var(--line); border-right: 1px solid var(--line); font-size: 0.88rem; line-height: 1.45; color: var(--ink-soft); }
.howto strong { color: var(--ink); font-weight: 600; }
.howto .host { display: block; margin: 2px 0; font-size: 0.8em; letter-spacing: -0.01em; }

.print-only { display: none; }

/* ---------- historial ---------- */
.history {
  width: min(720px, calc(100vw - 32px));
  max-height: calc(100vh - 64px);
  border: none;
  border-radius: 14px;
  padding: 0;
  color: var(--ink);
  box-shadow: 0 24px 60px -20px rgba(22, 35, 58, 0.45);
}
.history::backdrop { background: rgba(22, 35, 58, 0.45); }
.history-head {
  position: sticky;
  top: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}
.history-head h2 { margin: 0; font-size: 1.2rem; }
.history-list { list-style: none; margin: 0; padding: 8px 20px 20px; }
.version { position: relative; padding: 14px 0 14px 26px; border-left: 2px solid var(--line); margin-left: 6px; }
.version::before {
  content: '';
  position: absolute;
  left: -7px;
  top: 20px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--paper);
  border: 2px solid var(--pen);
}
.version.is-current::before { background: var(--pen); }
.version:last-child { border-left-color: transparent; }
.version-head { display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: baseline; }
.version-head .code { font-family: var(--mono); font-weight: 600; }
.version-head .when { color: var(--ink-soft); font-size: 0.9rem; }
.version .by { font-size: 0.95rem; }
.changes { list-style: none; margin: 8px 0; padding: 0; display: grid; gap: 4px; font-family: var(--mono); font-size: 0.88rem; }
.changes li { padding: 2px 8px; border-radius: 6px; background: #f3f5f8; }
.changes .tag { font-family: var(--sans); font-weight: 600; margin-right: 6px; }
.changes .add .tag { color: var(--ok); }
.changes .del { text-decoration: line-through; color: var(--ink-soft); }
.changes .del .tag { color: var(--warn); text-decoration: none; display: inline-block; }
.changes .mod .tag { color: var(--dawn); }

/* ---------- móvil ---------- */
@media screen and (max-width: 760px) {
  .editors, .pro-fields { grid-template-columns: 1fr; }
  .sheet { padding: 22px 16px; }
  .plan tbody th, .timed-med > div:first-child { font-size: 1.1rem; }
  .plan .moment svg { width: 34px; height: 34px; }
  .dose svg { width: 24px; height: 24px; }
  .timed-med { grid-template-columns: 1fr; }
  .sheet-foot { grid-template-columns: auto 1fr; }
  .sheet-foot .meta { grid-column: 1 / -1; }
  /* En la pantalla angosta del celular el aviso puede partirse; en la hoja impresa/PDF va en una línea. */
  .foot-bottom .disclaimer { white-space: normal; }
  .howto { padding: 0 0 0 14px; border-right: none; }
  .actions .btn { flex: 1; }
}

@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* ---------- impresión: solo la hoja ---------- */
@media print {
  @page { size: letter; margin: 12mm; }
  body { background: #fff; }
  .topbar, .pro, .editors, .errors, .actions, .status, .banner, .history { display: none !important; }
  .workspace { padding: 0; max-width: none; display: block; }
  .sheet { box-shadow: none; padding: 0; width: auto; max-width: none; min-height: 0; background: none; display: block; }
  /* Pie repetido en todas las hojas: fijo al fondo de cada página + espacio reservado (tfoot). */
  .page-frame { display: table; width: 100%; border-collapse: collapse; }
  .page-frame > tbody { display: table-row-group; }
  .page-frame > tfoot { display: table-footer-group; }
  .page-frame > * > tr { display: table-row; }
  .page-frame > * > tr > td { display: table-cell; padding: 0; }
  .foot-space { display: block; height: var(--foot-h, 45mm); }
  .sheet-foot { position: fixed; left: 0; right: 0; bottom: 0; background: #fff; margin: 0; }
  .sheet-body > * + * { margin-top: 18px; }
  /* Un poco más compacto en papel para que quepan más medicamentos por hoja. */
  .plan th, .plan td { padding-top: 7px; padding-bottom: 7px; }
  .plan tbody th, .timed-med > div:first-child { font-size: 1.2rem; }
  .plan .moment svg { width: 38px; height: 38px; }
  .timed-med { padding: 8px 0; }
  .tags { margin-top: 3px; }
  .page-mark { display: none !important; }
  .timed h3 { break-after: avoid; }
  .screen-only { display: none !important; }
  .print-only { display: inline; }
  .plan tbody tr, .timed-med { break-inside: avoid; }
  .sheet-foot { break-inside: avoid; }
}

.slot input::placeholder { color: #8d9ab8; font-style: italic; }
/* Fila a lo ancho bajo las tres columnas del pie: aviso a la izquierda, fecha a la derecha. */
.foot-bottom {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 14px;
}
.foot-bottom .disclaimer {
  flex: 1;
  min-width: 0;
  margin: 0;
  white-space: nowrap; /* siempre en una sola línea en la hoja */
  font-size: 0.7rem;
  letter-spacing: -0.005em;
  line-height: 1.4;
  color: var(--ink-soft);
  font-style: italic;
}
.foot-bottom .foot-date {
  flex: none;
  white-space: nowrap;
  text-align: right;
  font-size: 0.82rem;
  color: var(--ink-soft);
}
.foot-bottom .disclaimer + .foot-date { padding-left: 14px; border-left: 1px solid var(--line); }

/* comentarios del medicamento (bajo el nombre) */
.tags { display: flex; flex-wrap: wrap; gap: 4px 6px; margin-top: 6px; }
.tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 9px 2px 7px;
  border-radius: 999px;
  font-size: 0.88rem;
  font-weight: 600;
  line-height: 1.4;
}
.tag svg { width: 15px; height: 15px; flex: none; }
.tag-note { background: var(--pen-soft); color: var(--pen); }
.tag-duration { background: #fbefd9; color: #8a4b05; }
@media print {
  .tag { border: 1px solid currentColor; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

.assumed {
  margin: 0;
  padding: 10px 16px 10px 36px;
  background: #fbefd9;
  color: #6b3a04;
  border-radius: var(--radius);
  font-size: 0.92rem;
}
.slot-prn { min-width: 0; padding: 8px 12px; }

@media screen and (min-width: 900px) {
  .sheet {
    width: 8.5in;
    max-width: none;
    min-height: 11in;
    padding: 12mm;
  }
  .sheet-wrap { padding-bottom: 4px; }
}
.page-mark {
  position: absolute;
  left: 0;
  right: 0;
  height: 0;
  border-top: 2px dashed #9fb0c4;
  pointer-events: none;
}
.page-mark span {
  position: absolute;
  right: 8px;
  top: -11px;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--desk);
  color: var(--ink-soft);
  font-size: 0.78rem;
}

/* En pantalla el marco de página no es una tabla: la hoja sigue siendo una columna flexible. */
@media screen {
  .page-frame, .page-frame > tbody, .page-frame > tbody > tr { display: block; }
  .page-frame > tfoot { display: none; }
  .page-frame > tbody > tr > td { display: flex; flex-direction: column; gap: 22px; padding: 0; }
  .sheet-body { display: flex; flex-direction: column; gap: 22px; }
}
.page-mark span { max-width: 70%; text-align: right; }

/* Número de página en el margen inferior (navegadores que lo soportan, p. ej. Chrome). */
@page {
  @bottom-right {
    content: "Página " counter(page) " de " counter(pages);
    font-size: 8pt;
    color: #4b5a70;
  }
}

/* ---------- botón ocupado ---------- */
.btn.is-loading { position: relative; padding-left: 40px; opacity: 1; cursor: progress; }
.btn.is-loading::before {
  content: '';
  position: absolute;
  left: 14px;
  top: 50%;
  width: 16px;
  height: 16px;
  margin-top: -8px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .btn.is-loading::before { animation-duration: 2.4s; } }

/* ---------- copia de la hoja para generar el PDF (fuera de pantalla) ---------- */
.pdf-host { position: fixed; left: -10000px; top: 0; background: #fff; }
.sheet.pdf-mode {
  box-shadow: none;
  padding: 0;
  min-height: 0;
  max-width: none;
  background: #fff;
  display: block;
}
.pdf-mode .screen-only { display: none !important; }
.pdf-mode .print-only { display: inline; }
.pdf-mode .sheet-foot { grid-template-columns: auto minmax(0, 1.1fr) minmax(0, 1fr); margin-top: 0; }
.pdf-mode .sheet-foot .meta { grid-column: auto; }
.pdf-mode .howto { padding: 0 20px; border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
.pdf-mode .timed-med { grid-template-columns: minmax(150px, 1fr) 3fr; padding: 8px 0; }
.pdf-mode .plan th, .pdf-mode .plan td { padding-top: 7px; padding-bottom: 7px; }
.pdf-mode .plan tbody th, .pdf-mode .timed-med > div:first-child { font-size: 1.2rem; }
.pdf-mode .plan .moment svg { width: 38px; height: 38px; }
.pdf-mode .dose svg { width: 30px; height: 30px; }
.pdf-mode .sheet-body > * + * { margin-top: 18px; }
.pdf-mode .tags { margin-top: 3px; }

/* ---------- varias tomas al día: siempre en una sola fila ---------- */
.timed-slots { flex-wrap: nowrap; gap: 8px; }
.timed-slots .slot:not(.slot-prn) { flex: 1 1 0; min-width: 0; max-width: 104px; padding: 8px 6px 10px; }
.timed-slots .slot input { width: 100%; max-width: 78px; min-width: 0; }
/* Desde 6 tomas se quita el reloj para ahorrar espacio. */
.timed-slots.is-compact { gap: 6px; }
.timed-slots.is-compact .slot-time svg { display: none; }
.timed-slots.is-compact .slot-time { font-size: 1rem; }
@media screen and (max-width: 760px) {
  /* En el celular no caben en una fila: tres por fila. */
  .timed-slots { flex-wrap: wrap; }
  .timed-slots .slot:not(.slot-prn) { flex: 0 0 calc((100% - 16px) / 3); max-width: none; }
}
/* El PDF usa siempre el diseño de hoja carta, también si se genera desde el celular. */
.pdf-mode .timed-slots { flex-wrap: nowrap; }
.pdf-mode .timed-slots .slot:not(.slot-prn) { flex: 1 1 0; max-width: 104px; }
.timed-slots.is-compact .slot input { padding: 3px 2px; font-size: 0.8rem; }

/* Tabla con columna Atardecer (4 tomas al día). */
.plan-4 tbody td { width: 17%; }

/* Aviso de privacidad junto a los botones (no se imprime). */
.actions { align-items: center; }
.privacy {
  flex: 1 1 320px;
  margin: 0;
  max-width: 62ch;
  margin-right: auto;
  font-size: 0.82rem;
  line-height: 1.4;
  color: var(--ink-soft);
}
