:root {
  --bg: #f2f6f7;
  --card: #ffffff;
  --ink: #17333a;
  --muted: #6b858b;
  --line: #dbe6e8;
  --accent: #0d7f83;
  --accent-dark: #096569;
  --danger: #b4232b;
  --warn: #9a6700;
  --shadow: 0 8px 24px rgba(18, 62, 68, 0.08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

[hidden] { display: none !important; }

.wrap { max-width: 1040px; margin: 0 auto; padding: 28px 18px 64px; }

h1, h2 { margin: 0; line-height: 1.25; }
h1 { font-size: 21px; }
h2 { font-size: 17px; margin-bottom: 14px; }
p { margin: 0; }
.muted { color: var(--muted); font-size: 13px; }
.hint { margin: -8px 0 12px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
  box-shadow: var(--shadow);
}

.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-size: 28px;
  font-weight: 600;
}
.brand.compact .brand-mark { width: 38px; height: 38px; font-size: 24px; }

.login-card { max-width: 420px; margin: 12vh auto 0; }
.login-card form { display: grid; gap: 14px; margin-top: 24px; }
.fineprint { margin-top: 18px; text-align: center; color: var(--muted); font-size: 12px; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}
.top-actions { display: flex; align-items: center; gap: 12px; }

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 20px;
}
.tabs button {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 10px 16px;
  border-radius: 8px 8px 0 0;
  cursor: pointer;
  font: inherit;
  font-weight: 600;
}
.tabs button:hover { background: #e7f0f1; color: var(--ink); }
.tabs button.active { color: var(--accent-dark); box-shadow: inset 0 -3px 0 var(--accent); }

.panel { display: grid; gap: 18px; }
.panel > h2 { margin: 4px 0 -6px; }

.stats { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; }
.stat { display: grid; gap: 4px; }
.stat-num { font-size: 28px; font-weight: 700; color: var(--accent-dark); }
.stat-label { color: var(--muted); font-size: 13px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
label { display: grid; gap: 6px; font-weight: 600; font-size: 13px; }
.span-2 { grid-column: 1 / -1; }
input, textarea, select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-weight: 400;
}
.check { display: flex; align-items: center; gap: 8px; font-weight: 400; font-size: 13px; }
.check input { width: auto; }

input:focus, textarea:focus, select:focus {
  outline: 2px solid rgba(13, 127, 131, 0.25);
  border-color: var(--accent);
}
textarea { resize: vertical; }

button { font: inherit; font-weight: 600; cursor: pointer; }
.primary, .ghost, .danger, .small-btn {
  border-radius: 8px;
  padding: 10px 16px;
  border: 1px solid transparent;
}
.primary { background: var(--accent); color: #fff; }
.primary:hover { background: var(--accent-dark); }
.ghost { background: transparent; border-color: var(--line); color: var(--ink); }
.ghost:hover { background: #eef4f5; }
.danger { background: transparent; border-color: #efc9cb; color: var(--danger); }
.danger:hover { background: #fdf0f0; }
.small-btn { padding: 6px 10px; font-size: 13px; background: #eef5f5; border-color: var(--line); color: var(--accent-dark); }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; }

.list { display: grid; gap: 10px; }
.item {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px 16px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}
.item-main { min-width: 0; }
.item-title { font-weight: 700; overflow-wrap: anywhere; }
.item-meta { color: var(--muted); font-size: 13px; margin-top: 3px; }
.item-notes { margin-top: 6px; font-size: 13px; white-space: pre-wrap; overflow-wrap: anywhere; }

.exam-head { display: flex; align-items: flex-start; gap: 8px; cursor: pointer; }
.exam-head > div { min-width: 0; }
.caret { display: inline-block; color: var(--muted); font-size: 12px; padding-top: 3px; transition: transform 0.15s; }
.exam-item.expanded .caret { transform: rotate(90deg); }
.exam-details { display: grid; gap: 8px; margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); }
.exam-upload { display: grid; gap: 8px; margin-top: 4px; }
.exam-upload button { justify-self: start; }

.summary-head { display: flex; align-items: flex-start; gap: 8px; cursor: pointer; }
.summary-head > div { min-width: 0; }
.summary-item.expanded .caret { transform: rotate(90deg); }
.summary-details { display: grid; gap: 8px; margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); }
.detail-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px 14px; font-size: 13px; }
.detail-row { display: grid; gap: 2px; min-width: 0; }
.detail-label { color: var(--muted); font-weight: 600; }
.detail-value { overflow-wrap: anywhere; }

.item-actions { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.item-actions a { text-decoration: none; }

.doc-links { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.doc-chip {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  background: #eef5f5;
  border: 1px solid #c9e2e3;
  color: var(--accent-dark);
  font-size: 12px;
  font-weight: 600;
  text-decoration: none;
}
.doc-chip:hover { background: #dfeff0; }
.doc-chip-wrap { display: inline-flex; align-items: center; flex-wrap: wrap; gap: 4px; }
.doc-chip.subtle { background: #fff; color: var(--muted); border-color: var(--line); }
.doc-chip-meta { color: var(--muted); font-size: 11px; font-weight: 400; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  background: #eef2f3;
  color: var(--muted);
  margin-left: 6px;
}
.badge.programado { background: #e6f2f2; color: var(--accent-dark); }
.badge.realizado { background: #e8f4e9; color: #216b36; }
.badge.pendiente { background: #fff4dd; color: var(--warn); }
.badge.vencido { background: #fdebec; color: var(--danger); }

.empty { color: var(--muted); padding: 18px; text-align: center; border: 1px dashed var(--line); border-radius: 12px; }
.error { color: var(--danger); font-size: 13px; }
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 11px 18px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  z-index: 10;
  max-width: 90vw;
  text-align: center;
}

.section-heading { margin-top: 6px; }
.section-heading h2 { display: flex; align-items: center; gap: 8px; }
.section-count {
  display: inline-block;
  min-width: 22px;
  padding: 1px 7px;
  border-radius: 999px;
  background: #e6f2f2;
  color: var(--accent-dark);
  font-size: 12px;
  text-align: center;
}

.category-form { display: flex; flex-wrap: wrap; align-items: end; gap: 12px; }
.category-form label { flex: 1 1 260px; }
.category-item .item-main { flex: 1; }
.category-item > .item-main > .item-title {
  color: #064e52;
  font-size: 17px;
  font-weight: 800;
  letter-spacing: 0.2px;
}
.subcategory-list { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 6px; }
.subcategory-list li {
  display: block;
  padding: 8px 10px;
  border-left: 3px solid #0b78a8;
  background: #f5f9f9;
  border-radius: 6px;
  font-size: 14px;
}
.subcategory-head { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 8px; }
.subcategory-name { color: #0b78a8; font-weight: 800; font-size: 14px; }
.sub-actions { display: inline-flex; gap: 10px; }
.link-btn {
  border: 0;
  background: transparent;
  padding: 0;
  color: var(--accent-dark);
  font-size: 12px;
  text-decoration: underline;
}
.link-btn.danger-text { color: var(--danger); }
.subcategory-form { display: flex; gap: 8px; margin-top: 12px; max-width: 420px; }
.subcategory-form input { flex: 1; }

.cat-chip {
  display: inline-block;
  margin-left: 8px;
  padding: 2px 8px;
  border-radius: 999px;
  background: #ede7f6;
  border: 1px solid #d5c4f2;
  color: #5e35b1;
  font-size: 11px;
  font-weight: 700;
  vertical-align: middle;
}
.history { margin-top: 8px; }
.history summary {
  cursor: pointer;
  color: #5e35b1;
  font-size: 12px;
  font-weight: 800;
  list-style: none;
  user-select: none;
}
.history summary::-webkit-details-marker { display: none; }
.history summary::before { content: '▸ '; }
.history[open] summary::before { content: '▾ '; }
.history-count {
  display: inline-block;
  margin-left: 4px;
  padding: 1px 7px;
  border-radius: 999px;
  background: #ede7f6;
  color: #5e35b1;
  font-size: 11px;
}
.history-list { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 6px; }
.history-entry {
  padding: 7px 9px;
  border: 1px dashed #d5c4f2;
  border-radius: 8px;
  background: #fff;
  font-size: 12px;
}
.history-head { display: flex; align-items: flex-start; gap: 7px; cursor: pointer; }
.history-head > div { min-width: 0; }
.history-entry.expanded .caret { transform: rotate(90deg); }
.history-entry-details {
  display: grid;
  gap: 8px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px dashed var(--line);
}
.history-date { color: var(--muted); white-space: nowrap; font-size: 11px; }
.history-title { font-weight: 800; color: var(--ink); overflow-wrap: anywhere; font-size: 13px; }
.history-subline { color: #0b78a8; font-weight: 600; font-size: 11px; margin-top: 2px; overflow-wrap: anywhere; }
.history-empty { font-size: 12px; margin-top: 6px; }

.tab-count {
  display: inline-block;
  margin-left: 4px;
  padding: 0 6px;
  border-radius: 999px;
  background: #fdebec;
  color: var(--danger);
  font-size: 11px;
  font-weight: 800;
}
.alert-link { margin-top: 12px; }

.severity-chip {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
  margin-left: 8px;
  vertical-align: middle;
}
.severity-chip.sev-danger { background: #fdebec; color: var(--danger); }
.severity-chip.sev-warning { background: #fff4dd; color: var(--warn); }
.severity-chip.sev-info { background: #e6f2f2; color: var(--accent-dark); }
.alert-item { border-left: 4px solid var(--line); }
.alert-item.severity-danger { border-left-color: var(--danger); }
.alert-item.severity-warning { border-left-color: var(--warn); }
.alert-item.severity-info { border-left-color: var(--accent); }

.kind-chip, .type-chip {
  display: inline-block;
  margin-left: 6px;
  padding: 2px 8px;
  border-radius: 6px;
  font-size: 10px;
  font-weight: 800;
  vertical-align: middle;
}
.kind-chip.kind-cita { background: #e6f2f2; color: #064e52; }
.kind-chip.kind-medicamento { background: #e7f1fb; color: #0b78a8; }
.kind-chip.kind-tramite { background: #ede7f6; color: #5e35b1; }
.kind-chip.kind-examen { background: #eef4e9; color: #216b36; }
.type-chip { background: #f1f4f5; color: var(--muted); }

.med-head, .tramite-head { display: flex; align-items: flex-start; gap: 8px; cursor: pointer; }
.med-head > div, .tramite-head > div { min-width: 0; }
.med-item.expanded .caret, .tramite-item.expanded .caret { transform: rotate(90deg); }
.med-details, .tramite-details {
  display: grid;
  gap: 10px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed var(--line);
}
.mini-section { display: grid; gap: 6px; }
.mini-section > strong, .doc-block > strong { font-size: 12px; color: var(--muted); }
.mini-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 5px; }
.mini-list li {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
  padding: 6px 8px;
  background: #f6fafb;
  border: 1px solid var(--line);
  border-radius: 7px;
  font-size: 12px;
}
.delivery-form { display: grid; grid-template-columns: repeat(3, 1fr) auto; gap: 8px; align-items: end; }
.delivery-form button { justify-self: start; }
.detail-source { font-size: 12px; color: var(--muted); }
.doc-block { display: grid; gap: 4px; }
.history-top { display: flex; flex-wrap: wrap; align-items: center; }
.history-entry .detail-source { margin-top: 4px; }
select[multiple] { min-height: 88px; }
select[multiple]:disabled { background: #f1f4f5; opacity: 0.7; }

@media (max-width: 900px) {
  .stats { grid-template-columns: repeat(3, 1fr); }
  .delivery-form { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 700px) {
  .stats, .form-grid, .detail-grid { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .item { flex-direction: column; }
  .item-actions { justify-content: flex-start; }
  .topbar { align-items: flex-start; }
  .delivery-form { grid-template-columns: 1fr; }
}
