/* TabaIA B2B SPA — NotebookLM-inspired light theme */
@import url('https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,400;0,9..40,500;0,9..40,600;0,9..40,700;1,9..40,400&display=swap');

:root {
  --bg: #f9fafb;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --primary: #4286f3;
  --primary-text: #ffffff;
  --success: #16a34a;
  --danger: #ef4444;
  --border: #e5e7eb;
  --sidebar-w: 260px;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(17, 24, 39, 0.06), 0 8px 24px rgba(17, 24, 39, 0.04);
  --font: 'DM Sans', system-ui, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body.app-body {
  margin: 0;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1.25rem 1rem;
  position: sticky;
  top: 0;
  height: 100vh;
}

.sidebar-brand {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 0.25rem 0.5rem 1.25rem;
}
.sidebar-brand .logo {
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}
.sidebar-brand .meta {
  font-size: 0.8rem;
  color: var(--muted);
}

.nav-links {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  flex: 1;
  overflow-y: auto;
}
.nav-links a {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.7rem 0.85rem;
  border-radius: 10px;
  color: var(--text);
  font-weight: 500;
  text-decoration: none;
  border: none;
  background: transparent;
  cursor: pointer;
  font: inherit;
  text-align: left;
  width: 100%;
}
.nav-links a:hover { background: #f3f4f6; text-decoration: none; }
.nav-links a.active {
  background: rgba(66, 134, 243, 0.12);
  color: var(--primary);
}

.sidebar-foot {
  border-top: 1px solid var(--border);
  padding-top: 1rem;
  margin-top: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.ocr-bar-label {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}
.ocr-bar {
  height: 8px;
  background: #eef2f7;
  border-radius: 999px;
  overflow: hidden;
}
.ocr-bar > span {
  display: block;
  height: 100%;
  background: var(--primary);
  border-radius: inherit;
  width: 0%;
  transition: width 0.3s ease;
}

.main {
  padding: 1.75rem 2rem 3rem;
  min-width: 0;
}

.page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.page-header h1 {
  margin: 0;
  font-size: 1.65rem;
  font-weight: 700;
  letter-spacing: -0.03em;
}
.page-header .sub {
  margin: 0.35rem 0 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
  margin-bottom: 1.25rem;
}
.toolbar .input { max-width: 360px; }

.home-actions { margin-bottom: 1.25rem; }

.patient-list {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}
.patient-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.35rem;
  width: 100%;
  text-align: left;
  padding: 0.9rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  cursor: pointer;
  font: inherit;
  color: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.patient-card:hover,
.patient-card:focus-visible {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(66, 134, 243, 0.12);
  outline: none;
}
.patient-card-name {
  font-weight: 650;
  font-size: 1rem;
  letter-spacing: -0.01em;
}
.patient-card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 0.85rem;
  font-size: 0.82rem;
  color: var(--muted);
}
.patient-empty { margin: 0.5rem 0; }

.ficha-header {
  width: 100%;
}
.ficha-header-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.65rem;
  width: 100%;
}
.ficha-header-text {
  min-width: 0;
  flex: 1;
}
.ficha-header-text h1 {
  margin: 0;
  word-break: break-word;
}
.ficha-patient-actions {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  flex-shrink: 0;
  padding-top: 0.15rem;
}
.ficha-patient-actions .btn-edit-patient,
.ficha-patient-actions .btn-del-patient {
  padding: 0.28rem 0.4rem;
  min-width: 1.85rem;
  min-height: 1.85rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.ficha-patient-actions .btn-tl svg {
  display: block;
}
.ficha-actions {
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 1.25rem;
}
.ficha-actions .btn-sm { padding: 0.35rem 0.7rem; font-size: 0.82rem; }

/* Topbar / bottom-nav: só mobile */
.topbar,
.sidebar-backdrop,
.bottom-nav { display: none; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.65rem 1.1rem;
  border-radius: 10px;
  border: 1px solid transparent;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary {
  background: var(--primary);
  color: var(--primary-text);
}
.btn-primary:hover:not(:disabled) { filter: brightness(0.96); }
.btn-secondary {
  background: var(--surface);
  color: var(--primary);
  border-color: var(--primary);
}
.btn-secondary:hover:not(:disabled) { background: rgba(66, 134, 243, 0.06); }
.btn-ghost {
  background: transparent;
  color: var(--muted);
  border-color: var(--border);
}
.btn-danger {
  background: var(--surface);
  color: var(--danger);
  border-color: var(--danger);
}
.btn-sm { padding: 0.4rem 0.75rem; font-size: 0.85rem; }
.btn-xs {
  padding: 0.22rem 0.55rem;
  font-size: 0.75rem;
  font-weight: 600;
  border-radius: 6px;
  line-height: 1.25;
}

.input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.85rem;
  width: 100%;
}
.input:focus, select:focus, textarea:focus {
  outline: 2px solid rgba(66, 134, 243, 0.35);
  border-color: var(--primary);
}
label.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--muted);
}
.stack { display: flex; flex-direction: column; gap: 0.85rem; }
.row { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center; }

.grid-kpi {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem;
  box-shadow: var(--shadow);
}
.kpi .label { font-size: 0.8rem; color: var(--muted); margin-bottom: 0.35rem; }
.kpi .value { font-size: 1.55rem; font-weight: 700; letter-spacing: -0.02em; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.15rem 1.25rem;
  box-shadow: var(--shadow);
}
.card + .card { margin-top: 0.85rem; }
.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
}
.card.clickable { cursor: pointer; transition: border-color 0.15s, box-shadow 0.15s; }
.card.clickable:hover { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(66, 134, 243, 0.12); }
.card h3 { margin: 0 0 0.35rem; font-size: 1.05rem; }
.card .muted { color: var(--muted); font-size: 0.875rem; }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
}
.badge-ok { background: rgba(22, 163, 74, 0.12); color: var(--success); }
.badge-err { background: rgba(239, 68, 68, 0.12); color: var(--danger); }
.badge-muted { background: #f3f4f6; color: var(--muted); }
.badge-chat-date {
  display: inline-block;
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid rgba(29, 78, 216, 0.28);
  background: rgba(66, 134, 243, 0.12);
  color: #1d4ed8;
  font: inherit;
  font-size: 0.82em;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 1.35;
  padding: 0.12rem 0.45rem;
  border-radius: 999px;
  vertical-align: baseline;
  margin: 0 0.12rem;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
  text-decoration-color: rgba(29, 78, 216, 0.45);
}
.badge-chat-date:hover,
.badge-chat-date:focus-visible {
  background: rgba(66, 134, 243, 0.22);
  outline: none;
  box-shadow: 0 0 0 2px rgba(66, 134, 243, 0.35);
}
.tl-row.is-cite-focus {
  animation: citeFocusPulse 2.6s ease;
}
.tl-row.is-cite-focus .tl-content {
  outline: 2px solid var(--tl-ink);
  outline-offset: 2px;
  background: var(--tl-soft);
  border-radius: 10px;
}
@keyframes citeFocusPulse {
  0% { background: transparent; }
  12% { background: color-mix(in srgb, var(--tl-soft) 85%, transparent); }
  100% { background: transparent; }
}

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}
table.data th, table.data td {
  text-align: left;
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border);
}
table.data th { color: var(--muted); font-weight: 600; font-size: 0.8rem; }
table.data tr:last-child td { border-bottom: none; }
table.data tr.clickable { cursor: pointer; }
table.data tr.clickable:hover td { background: #f8fafc; }

.pricing-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1rem;
}
.price-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.35rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.price-card.featured { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(66, 134, 243, 0.12); }
.price-card .price { font-size: 1.75rem; font-weight: 700; }
.price-card ul { margin: 0; padding-left: 1.1rem; color: var(--muted); font-size: 0.9rem; }

.timeline-year {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  margin-bottom: 0.75rem;
  overflow: hidden;
}
.hist-filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem 0.75rem;
  margin: 0 0 0.85rem;
}
.hist-filters-main {
  display: flex;
  align-items: center;
  gap: 0.45rem 0.55rem;
  flex: 1 1 auto;
  min-width: 0;
}
.hist-filters-scroll {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  min-width: 0;
}
.hist-filters-right {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  flex-shrink: 0;
  margin-left: auto;
}
.hist-filters-label {
  flex-shrink: 0;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--muted);
}
.hist-filter .hist-filter-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.15rem;
  padding: 0 0.2rem;
  margin-right: 0.15rem;
  border-radius: 4px;
  background: rgba(17, 24, 39, 0.06);
  font-variant-numeric: tabular-nums;
}
.hist-filter.is-active {
  background: rgba(66, 134, 243, 0.12);
  border-color: var(--primary);
  color: #1d4ed8;
}
.hist-filter.is-active .hist-filter-count {
  background: rgba(66, 134, 243, 0.18);
}
.hist-filter[data-filter="rxm"].is-active {
  background: rgba(107, 91, 138, 0.14);
  border-color: #6b5b8a;
  color: #5a4a78;
}
.hist-filter[data-filter="rxm"].is-active .hist-filter-count {
  background: rgba(107, 91, 138, 0.18);
}
.timeline-year > summary {
  list-style: none;
  cursor: pointer;
  padding: 0.9rem 1.1rem;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid transparent;
}
.timeline-year[open] > summary { border-bottom-color: var(--border); }
.timeline-year > summary::-webkit-details-marker { display: none; }

/* Timeline vertical (data | eixo | conteúdo) — ações no cabeçalho do card */
.tl-track {
  list-style: none;
  margin: 0;
  padding: 0.35rem 1rem 0.85rem;
}
.tl-row {
  --tl-pastel: #c5d9f5;
  --tl-ink: #3b6ea5;
  --tl-soft: #f3f7fd;
  display: grid;
  grid-template-columns: 6.25rem 1.35rem minmax(0, 1fr);
  gap: 0.35rem 0.65rem;
  align-items: start;
  padding: 0.95rem 0;
  position: relative;
}
.tl-rx { --tl-pastel: #b7d0f5; --tl-ink: #3d6fb0; --tl-soft: #eef4fc; }
.tl-rxm { --tl-pastel: #d4c8e8; --tl-ink: #6b5b8a; --tl-soft: #f3eef8; }
.tl-sy { --tl-pastel: #f5c9a0; --tl-ink: #c07838; --tl-soft: #fff6eb; }
.tl-cs { --tl-pastel: #b5dfc2; --tl-ink: #3f8f5c; --tl-soft: #eef8f1; }

.tl-when {
  text-align: right;
  padding-top: 0.15rem;
}
.tl-date {
  display: block;
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--tl-ink);
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
}
.tl-time {
  display: block;
  margin-top: 0.2rem;
  font-size: 0.78rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.tl-axis {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 100%;
  padding-top: 0.35rem;
}
.tl-axis::before {
  content: '';
  position: absolute;
  left: 50%;
  top: -0.95rem;
  bottom: -0.95rem;
  width: 4px;
  transform: translateX(-50%);
  background: var(--tl-pastel);
  border-radius: 999px;
}
.tl-row:first-child .tl-axis::before { top: 0.55rem; }
.tl-row:last-child .tl-axis::before { bottom: calc(100% - 0.95rem); }
.tl-row:only-child .tl-axis::before { display: none; }
.tl-dot {
  position: relative;
  z-index: 1;
  width: 0.85rem;
  height: 0.85rem;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--tl-ink);
  box-shadow: 0 0 0 3px var(--tl-soft);
  flex-shrink: 0;
}

.tl-content {
  min-width: 0;
  background: var(--tl-soft);
  border-radius: 10px;
  padding: 0.7rem 0.85rem;
  border: 1px solid var(--tl-pastel);
}
.tl-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.35rem;
}
.tl-title {
  display: block;
  color: var(--tl-ink);
  font-size: 0.92rem;
  letter-spacing: 0.01em;
  flex: 1;
  min-width: 0;
  line-height: 1.35;
}
.tl-head-actions {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  flex-shrink: 0;
}
.btn-tl {
  background: #fff !important;
  border: 1px solid rgba(17, 24, 39, 0.22) !important;
  color: #1f2937 !important;
  box-shadow: 0 1px 2px rgba(17, 24, 39, 0.06);
  font-weight: 600;
  line-height: 1;
}
.btn-tl.btn-edit-item,
.btn-tl.btn-del-item {
  padding: 0.28rem 0.4rem;
  min-width: 1.85rem;
  min-height: 1.85rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn-tl:hover {
  background: #f8fafc !important;
  border-color: rgba(17, 24, 39, 0.35) !important;
  color: #111827 !important;
}
.btn-tl-danger {
  color: #b91c1c !important;
  border-color: rgba(185, 28, 28, 0.35) !important;
}
.btn-tl-danger:hover {
  background: #fef2f2 !important;
  color: #991b1b !important;
}
.btn-tl svg {
  display: block;
}
.tl-body { font-size: 0.9rem; color: var(--text); }
.tl-body p { margin: 0 0 0.35rem; }
.tl-body p:last-child { margin-bottom: 0; }
.tl-row.is-collapsed .tl-body { display: none; }
.tl-row.is-collapsed .tl-head { margin-bottom: 0; }

.insert-menu { gap: 0.85rem; }
.insert-section { display: flex; flex-direction: column; gap: 0.45rem; }
.insert-section-title {
  margin: 0;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
}
.insert-btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}
.insert-full { width: 100%; justify-content: center; }
.insert-chevron { margin-left: 0.25rem; opacity: 0.7; }
.insert-forms-list {
  margin-top: 0.35rem;
  max-height: 40vh;
  overflow: auto;
}

@media (max-width: 720px) {
  .tl-row {
    grid-template-columns: 1.2rem minmax(0, 1fr);
    grid-template-areas:
      "axis when"
      "axis body";
    gap: 0.2rem 0.55rem;
    padding: 0.7rem 0;
  }
  .tl-when { grid-area: when; text-align: left; display: flex; align-items: baseline; gap: 0.5rem; }
  .tl-time { margin-top: 0; }
  .tl-axis { grid-area: axis; }
  .tl-content { grid-area: body; padding: 0.65rem 0.7rem; }
  .tl-head { flex-wrap: wrap; gap: 0.45rem; }
  .tl-head-actions { width: 100%; justify-content: flex-end; }
  .tl-title { font-size: 0.88rem; }
  .tl-track { padding: 0.25rem 0.35rem 0.65rem; }
  .btn-tl.btn-edit-item,
  .btn-tl.btn-del-item,
  .btn-tl.btn-card-toggle {
    min-width: 2.4rem;
    min-height: 2.4rem;
  }
}

.med-list { margin: 0.35rem 0 0; padding-left: 1.1rem; color: var(--text); }
.med-list li { margin: 0.2rem 0; }
.med-list .med-meta { color: var(--muted); font-size: 0.85rem; }
.rx-manip-table { margin-top: 0.35rem; border-radius: 8px; }
.rx-manip-table table.data {
  font-size: 0.82rem;
  table-layout: fixed;
  width: 100%;
}
.rx-manip-table th, .rx-manip-table td { padding: 0.45rem 0.65rem; vertical-align: top; }
.rx-manip-table .rx-code {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--tl-ink, #6b5b8a);
  white-space: nowrap;
  width: 4.5rem;
}
.rx-manip-table .rx-qty {
  white-space: nowrap;
  width: 5.5rem;
  color: var(--muted);
}
.rx-manip-table .rx-ing {
  white-space: normal;
  overflow-wrap: anywhere;
  word-break: break-word;
  line-height: 1.35;
}
.rx-manip-block + .rx-manip-block { margin-top: 0.75rem; padding-top: 0.65rem; border-top: 1px dashed rgba(107, 91, 138, 0.25); }
.rx-manip-toolbar {
  display: flex;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 0.25rem 0.5rem;
  margin: 0 0 0.55rem;
}
.rx-manip-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.35rem;
  color: #6b5b8a;
}
.rx-manip-title-main {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  min-width: 0;
}
.rx-manip-title strong { font-size: 0.98rem; color: #5a4a78; }
.rx-comp-meta { font-size: 0.75rem; margin-left: 0.15rem; }
.rx-manip-block.is-collapsed .rx-manip-detail { display: none; }
.rx-manip-block.is-collapsed { margin-bottom: 0; }
.rx-manip-block.is-collapsed + .rx-manip-block { margin-top: 0.45rem; padding-top: 0.45rem; }
.rx-potion {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.7rem;
  height: 1.7rem;
  border-radius: 8px;
  background: rgba(107, 91, 138, 0.12);
  color: #6b5b8a;
  flex-shrink: 0;
}
.rx-manip-posologia {
  font-size: 0.88rem;
  margin: 0 0 0.4rem;
  color: var(--text);
}
.rx-manip-k {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: #6b5b8a;
  margin-right: 0.35rem;
}
.rx-manip-obs { font-size: 0.85rem; margin-top: 0.35rem; color: var(--text); }
.rx-manip-edit { margin: 0.25rem 0; }
.rx-manip-edit .input-sm {
  padding: 0.3rem 0.45rem;
  font-size: 0.82rem;
  width: 100%;
}
.rx-manip-edit .rx-row-act { width: 2rem; text-align: center; vertical-align: middle; }
.rx-manip-edit td { vertical-align: middle; }
.rx-manip-edit-card {
  border: 1px solid #e4dced;
  background: #faf8fc;
  border-radius: 10px;
  padding: 0.75rem 0.85rem;
  margin-bottom: 0.65rem;
}
.rx-manip-edit-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.45rem;
}

.chat-drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, 0.28);
  z-index: 40;
}
.chat-drawer {
  position: fixed;
  top: 0;
  right: 0;
  width: min(400px, 100vw);
  height: 100vh;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: -12px 0 40px rgba(17, 24, 39, 0.12);
  z-index: 45;
  display: flex;
  flex-direction: column;
  animation: chatSlideIn 0.2s ease;
}
@keyframes chatSlideIn {
  from { transform: translateX(12px); opacity: 0.6; }
  to { transform: translateX(0); opacity: 1; }
}
.chat-drawer-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 1rem 1.1rem 0.75rem;
  border-bottom: 1px solid var(--border);
}
.chat-drawer-head strong { font-size: 1rem; }
.chat-drawer .chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 0.85rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  min-height: 0;
}
.chat-empty { margin: 0; padding: 0.5rem 0; font-size: 0.85rem; }
.chat-bubble {
  max-width: 92%;
  padding: 0.6rem 0.8rem;
  border-radius: 12px;
  font-size: 0.88rem;
  white-space: pre-wrap;
  line-height: 1.4;
}
.chat-bubble.user { align-self: flex-end; background: rgba(66, 134, 243, 0.12); }
.chat-bubble.bot { align-self: flex-start; background: #f3f4f6; }
.chat-drawer .chat-input {
  display: flex;
  gap: 0.45rem;
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.chat-drawer .chat-input .input { flex: 1; }
@media (max-width: 520px) {
  .chat-drawer {
    width: 100vw;
    height: 100dvh;
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
  .chat-drawer .chat-input {
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px));
  }
  .chat-bubble { max-width: 96%; }
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 50;
}
.modal {
  background: var(--surface);
  border-radius: 16px;
  width: min(520px, 100%);
  max-height: 90vh;
  overflow: auto;
  padding: 1.35rem 1.4rem;
  box-shadow: 0 20px 50px rgba(17, 24, 39, 0.18);
}
.modal.modal-wide { width: min(640px, 100%); }
.form-pick-list {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  max-height: 55vh;
  overflow-y: auto;
  margin: 0.5rem 0 0;
}
.form-pick-item {
  display: grid;
  grid-template-columns: 2rem 1fr;
  gap: 0.55rem;
  align-items: baseline;
  text-align: left;
  width: 100%;
  padding: 0.45rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
  font: inherit;
  line-height: 1.25;
}
.form-pick-item:hover { border-color: var(--primary); background: rgba(66, 134, 243, 0.06); }
.form-pick-num {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--primary);
  font-variant-numeric: tabular-nums;
}
.form-pick-item .pick-title {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
}
.form-pick-item .pick-desc {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 400;
  margin-top: 0.1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.stack-form { display: flex; flex-direction: column; gap: 0.45rem; }
.stack-form > .field,
.stack-form > fieldset.field {
  gap: 0.2rem;
  font-size: 0.8rem;
}
.stack-form .input,
.stack-form select.input,
.stack-form textarea.input {
  padding: 0.45rem 0.65rem;
  border-radius: 8px;
  font-size: 0.9rem;
}
.stack-form textarea.input { min-height: 2.4rem; }
.bool-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem 0.75rem;
  padding: 0.35rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fafbfc;
}
.bool-row .bool-label {
  flex: 1 1 12rem;
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--text);
  line-height: 1.3;
}
.bool-toggle {
  display: inline-flex;
  flex-shrink: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
}
.bool-toggle label {
  margin: 0;
  cursor: pointer;
}
.bool-toggle input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.bool-toggle span {
  display: block;
  padding: 0.35rem 0.85rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  min-width: 3.2rem;
  text-align: center;
  transition: background 0.12s, color 0.12s;
}
.bool-toggle label + label span { border-left: 1px solid var(--border); }
.bool-toggle input:checked + span {
  background: var(--primary);
  color: #fff;
}
.bool-row .measure-slot {
  flex: 1 1 100%;
  margin-top: 0.15rem;
}
.measure-slot.hidden { display: none !important; }
.measure-field {
  margin: 0;
  gap: 0.2rem;
}
.measure-field .measure-label {
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--muted);
}
.measure-field .input {
  max-width: 16rem;
  padding: 0.35rem 0.55rem;
  font-size: 0.85rem;
}
.multi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 0.45rem 0.75rem;
  padding: 0.35rem 0.5rem;
}
.multi-option {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}
.multi-grid label {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.8rem;
  font-weight: 400;
  color: var(--text);
}
.clinical-summary { margin: 0.35rem 0 0; padding-left: 1.1rem; }
.clinical-summary li { margin: 0.15rem 0; font-size: 0.9rem; }
.clinical-summary .k { color: var(--muted); }
.modal h2 { margin: 0 0 0.65rem; font-size: 1.05rem; letter-spacing: -0.01em; }
.modal-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 1.1rem; }

.toast {
  position: fixed;
  bottom: 1.25rem;
  right: 1.25rem;
  background: var(--text);
  color: #fff;
  padding: 0.75rem 1rem;
  border-radius: 10px;
  z-index: 60;
  font-size: 0.9rem;
  max-width: min(360px, 90vw);
}
.error-text { color: var(--danger); font-size: 0.9rem; }
.muted { color: var(--muted); }
.hidden { display: none !important; }

.auth-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(66, 134, 243, 0.12), transparent 60%),
    radial-gradient(900px 500px at 100% 0%, rgba(22, 163, 74, 0.08), transparent 55%),
    var(--bg);
}
.auth-card {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.75rem;
  box-shadow: var(--shadow);
}
.auth-card .brand { font-weight: 700; font-size: 1.25rem; margin: 0 0 0.25rem; }
.auth-card h1 { margin: 0 0 0.5rem; font-size: 1.5rem; letter-spacing: -0.02em; }

/* ========== Mobile / tablet shell ========== */
@media (max-width: 900px) {
  :root { --topbar-h: 56px; --bottom-nav-h: 64px; }

  .topbar {
    display: flex;
    align-items: center;
    gap: 0.55rem;
    position: sticky;
    top: 0;
    z-index: 35;
    height: calc(var(--topbar-h) + env(safe-area-inset-top, 0px));
    padding: env(safe-area-inset-top, 0px) 0.65rem 0;
    background: rgba(255, 255, 255, 0.92);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border);
  }
  .topbar-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.65rem;
    height: 2.65rem;
    border: none;
    border-radius: 10px;
    background: transparent;
    color: var(--text);
    cursor: pointer;
    flex-shrink: 0;
  }
  .topbar-btn:hover,
  .topbar-btn:focus-visible {
    background: #f3f4f6;
    outline: none;
  }
  .topbar-title {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    line-height: 1.15;
  }
  .topbar-title .logo {
    font-weight: 700;
    font-size: 1.05rem;
    letter-spacing: -0.02em;
  }
  .topbar-title .meta {
    font-size: 0.72rem;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(17, 24, 39, 0.4);
    z-index: 40;
    border: none;
    padding: 0;
  }
  .sidebar-backdrop[hidden] { display: none !important; }

  .app-shell {
    display: block;
    min-height: 100dvh;
  }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 45;
    width: min(300px, 86vw);
    height: 100dvh;
    height: 100vh;
    padding: calc(0.85rem + env(safe-area-inset-top, 0px)) 0.85rem calc(1rem + env(safe-area-inset-bottom, 0px));
    border-right: 1px solid var(--border);
    border-bottom: none;
    transform: translateX(-105%);
    transition: transform 0.22s ease;
    box-shadow: none;
  }
  .app-shell.nav-open .sidebar {
    transform: translateX(0);
    box-shadow: 12px 0 40px rgba(17, 24, 39, 0.18);
  }
  body.nav-drawer-open { overflow: hidden; }

  .nav-links {
    flex-direction: column;
    flex-wrap: nowrap;
  }
  .nav-links a {
    width: 100%;
    min-height: 2.75rem;
    font-size: 0.98rem;
  }

  .main {
    padding: 1rem 0.85rem calc(1.5rem + env(safe-area-inset-bottom, 0px));
  }
  .app-shell.has-bottom-nav .main {
    padding-bottom: calc(1.25rem + var(--bottom-nav-h) + env(safe-area-inset-bottom, 0px));
  }

  .bottom-nav {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    height: calc(var(--bottom-nav-h) + env(safe-area-inset-bottom, 0px));
    padding: 0.35rem 0.5rem env(safe-area-inset-bottom, 0px);
    background: rgba(255, 255, 255, 0.96);
    backdrop-filter: blur(10px);
    border-top: 1px solid var(--border);
    justify-content: space-around;
    gap: 0.25rem;
  }
  .bottom-nav[hidden] { display: none !important; }
  .bottom-nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.15rem;
    min-height: 3rem;
    border-radius: 10px;
    color: var(--muted);
    font-size: 0.72rem;
    font-weight: 600;
    text-decoration: none;
    -webkit-tap-highlight-color: transparent;
  }
  .bottom-nav-item:hover { text-decoration: none; color: var(--text); }
  .bottom-nav-item.active {
    color: var(--primary);
    background: rgba(66, 134, 243, 0.1);
  }
  .bottom-nav-ico {
    display: inline-flex;
    line-height: 0;
  }

  .page-header {
    margin-bottom: 1rem;
    gap: 0.65rem;
  }
  .page-header h1 {
    font-size: 1.35rem;
    word-break: break-word;
  }
  .page-header .sub { font-size: 0.88rem; }

  .home-actions {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.55rem;
  }
  .home-actions .btn {
    width: 100%;
    min-height: 2.85rem;
  }
  .toolbar .input {
    max-width: none;
    width: 100%;
    min-height: 2.85rem;
  }

  .ficha-header {
    flex-direction: column;
    align-items: stretch;
  }
  .ficha-header-top {
    align-items: flex-start;
  }
  .ficha-header-text h1 {
    font-size: 1.25rem;
    padding-right: 0.15rem;
  }
  .ficha-patient-actions {
    padding-top: 0.05rem;
  }
  .ficha-patient-actions .btn-edit-patient,
  .ficha-patient-actions .btn-del-patient {
    min-width: 2rem;
    min-height: 2rem;
  }
  .ficha-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
    margin-bottom: 1rem;
    position: sticky;
    top: calc(var(--topbar-h) + env(safe-area-inset-top, 0px));
    z-index: 20;
    padding: 0.45rem 0;
    background: linear-gradient(var(--bg) 70%, transparent);
  }
  .ficha-actions .btn {
    width: 100%;
    min-height: 2.75rem;
    font-size: 0.92rem;
    padding: 0.55rem 0.75rem;
  }

  .hist-filters {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
  }
  .hist-filters-main {
    width: 100%;
  }
  .hist-filters-label {
    line-height: 2.2rem;
  }
  .hist-filters-scroll {
    flex: 1 1 auto;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 0.1rem;
    scrollbar-width: none;
  }
  .hist-filters-scroll::-webkit-scrollbar { display: none; }
  .hist-filter {
    flex: 0 0 auto;
    min-height: 2.2rem;
    padding: 0.35rem 0.65rem;
    font-size: 0.8rem;
  }
  .hist-filters-right {
    width: 100%;
    justify-content: flex-end;
    margin-left: 0;
  }

  .rx-manip-table .rx-code { display: none; }
  .rx-manip-table .rx-qty { width: 4.75rem; }
  .rx-manip-table table.data { min-width: 0; }
  .rx-manip-table {
    overflow-x: visible;
  }

  .modal-backdrop {
    align-items: stretch;
    justify-content: flex-end;
    padding: 0;
  }
  .modal,
  .modal.modal-wide {
    width: 100%;
    max-width: none;
    max-height: min(92dvh, 100%);
    border-radius: 16px 16px 0 0;
    padding: 1.1rem 1rem calc(1.1rem + env(safe-area-inset-bottom, 0px));
  }
  .modal-actions {
    flex-wrap: wrap;
  }
  .modal-actions .btn {
    flex: 1 1 auto;
    min-height: 2.75rem;
  }
  .insert-btn-row .btn {
    flex: 1 1 calc(50% - 0.25rem);
    min-height: 2.6rem;
  }
  .insert-full { min-height: 2.75rem; }

  .table-wrap {
    margin: 0 -0.15rem;
    border-radius: 10px;
  }
  table.data th,
  table.data td {
    padding: 0.7rem 0.75rem;
    font-size: 0.86rem;
  }

  .grid-kpi {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.65rem;
  }
  .kpi { padding: 0.85rem 0.9rem; }
  .kpi .value { font-size: 1.25rem; }

  .pricing-grid {
    grid-template-columns: 1fr;
  }

  .cards-grid {
    grid-template-columns: 1fr;
  }

  .toast {
    left: 50%;
    right: auto;
    bottom: calc(1rem + var(--bottom-nav-h, 0px) + env(safe-area-inset-bottom, 0px));
    transform: translateX(-50%);
  }
  .app-shell:not(.has-bottom-nav) .toast {
    bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
  }
}

@media (max-width: 420px) {
  .page-header h1 { font-size: 1.2rem; }
  .ficha-header-text h1 { font-size: 1.15rem; }
  .grid-kpi {
    grid-template-columns: 1fr;
  }
}
