:root {
  --color-bg: #F5F8F8;
  --color-surface: #FFFFFF;
  --color-primary: #0F5257;
  --color-primary-dark: #0A3B3F;
  --color-primary-tint: #E3EFEE;
  --color-text: #1C2B2E;
  --color-text-muted: #5B6B6D;
  --color-border: #DCE6E5;
  --color-risk: #B3261E;
  --color-risk-tint: #FBEAE9;
  --color-accent: #C97B2E;
  --color-accent-tint: #FBF1E6;
  --color-risk-border: #F0C9C6;
  --color-heading: #0A3B3F;
  --color-link: #0F5257;

  --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-display: Georgia, "Times New Roman", serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;

  --radius-sm: 8px;
  --radius-md: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-body);
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100vh;
  padding-bottom: calc(16px + var(--safe-bottom));
}

a { color: inherit; text-decoration: none; }
button { font-family: inherit; }

::selection { background: var(--color-primary-tint); }

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

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--color-primary);
  color: #fff;
  padding: calc(12px + var(--safe-top)) 16px 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: 0 2px 6px rgba(10,59,63,0.15);
}

.topbar__back {
  background: rgba(255,255,255,0.14);
  border: none;
  color: #fff;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.topbar__title {
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 0.2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar__subtitle {
  font-size: 12px;
  opacity: 0.8;
  margin-top: 1px;
}

.offline-pill {
  margin-left: auto;
  font-size: 11px;
  background: rgba(255,255,255,0.15);
  padding: 4px 9px;
  border-radius: 999px;
  white-space: nowrap;
}
.offline-pill[data-state="offline"] { background: var(--color-accent); }

/* ---------- Home ---------- */
.home {
  padding: 24px 20px 40px;
  max-width: 640px;
  margin: 0 auto;
}

.home__eyebrow {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--color-link);
  font-weight: 700;
  margin: 8px 0 4px;
}

.home__title {
  font-family: var(--font-display);
  font-size: 30px;
  line-height: 1.15;
  margin: 0 0 8px;
  color: var(--color-heading);
}

.home__desc {
  color: var(--color-text-muted);
  font-size: 15px;
  line-height: 1.5;
  margin: 0 0 28px;
}

.home__grid {
  display: grid;
  gap: 16px;
}

.mode-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: 0 1px 3px rgba(15,82,87,0.06);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.mode-card:active { transform: scale(0.98); }

.mode-card__icon {
  width: 52px;
  height: 52px;
  border-radius: 12px;
  background: var(--color-primary-tint);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--color-link);
}
.mode-card__icon svg { width: 26px; height: 26px; }

.mode-card--tarjetas .mode-card__icon {
  background: var(--color-accent-tint);
  color: var(--color-accent);
}

.mode-card__body h2 {
  margin: 0 0 4px;
  font-size: 17px;
  color: var(--color-heading);
}
.mode-card__body p {
  margin: 0;
  font-size: 13.5px;
  color: var(--color-text-muted);
  line-height: 1.4;
}

.home__footer {
  margin-top: 32px;
  font-size: 12.5px;
  color: var(--color-text-muted);
  text-align: center;
  line-height: 1.6;
}
.home__footer a { color: var(--color-link); font-weight: 600; }

/* ---------- Lista ---------- */
.list-controls {
  position: sticky;
  top: 0;
  z-index: 15;
  background: var(--color-bg);
  padding: 12px 16px 8px;
}

.search-box {
  position: relative;
  margin-bottom: 10px;
}
.search-box input {
  width: 100%;
  padding: 12px 14px 12px 38px;
  border-radius: 999px;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  font-size: 15px;
  color: var(--color-text);
}
.search-box input:focus { border-color: var(--color-link); }
.search-box::before {
  content: "";
  position: absolute;
  left: 14px;
  top: 50%;
  width: 14px;
  height: 14px;
  transform: translateY(-50%);
  border: 2px solid var(--color-text-muted);
  border-radius: 50%;
}
.search-box::after {
  content: "";
  position: absolute;
  left: 26px;
  top: 25px;
  width: 6px;
  height: 2px;
  background: var(--color-text-muted);
  transform: rotate(45deg);
}

.alphabet {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 4px;
  scrollbar-width: none;
}
.alphabet::-webkit-scrollbar { display: none; }

.alphabet__chip {
  flex-shrink: 0;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text-muted);
  border-radius: 8px;
  width: 30px;
  height: 30px;
  font-size: 13px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}
.alphabet__chip[data-active="true"] {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: #fff;
}
.alphabet__chip[disabled] {
  opacity: 0.35;
}

.drug-list {
  list-style: none;
  margin: 0;
  padding: 8px 16px 24px;
  max-width: 640px;
  margin: 0 auto;
}

.drug-list__item + .drug-list__item { margin-top: 8px; }

.drug-list__link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
}
.drug-list__link:active { background: var(--color-primary-tint); }

.drug-list__name {
  font-size: 15.5px;
  font-weight: 600;
  color: var(--color-heading);
}
.drug-list__meta {
  display: block;
  font-size: 12.5px;
  color: var(--color-text-muted);
  font-weight: 400;
  margin-top: 2px;
}

.risk-badge {
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.3px;
  color: var(--color-risk);
  background: var(--color-risk-tint);
  padding: 3px 8px;
  border-radius: 999px;
  white-space: nowrap;
  flex-shrink: 0;
}

.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--color-text-muted);
  font-size: 14.5px;
}

/* ---------- Detalle ---------- */
.detail {
  max-width: 680px;
  margin: 0 auto;
  padding: 4px 0 40px;
}

.detail__hero {
  padding: 18px 20px 16px;
}
.detail__hero-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--color-link);
  font-weight: 700;
}
.detail__hero h1 {
  font-family: var(--font-display);
  font-size: 24px;
  margin: 4px 0 0;
  color: var(--color-heading);
}

.risk-banner {
  margin: 0 20px 4px;
  background: var(--color-risk-tint);
  border: 1px solid var(--color-risk-border);
  color: var(--color-risk);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  display: flex;
  gap: 8px;
  align-items: flex-start;
}

.detail__toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 4px 20px 4px;
  font-size: 12.5px;
}
.detail__toolbar-sep { color: var(--color-border); }
.btn-link {
  background: none;
  border: none;
  padding: 6px 2px;
  color: var(--color-link);
  font-weight: 600;
  font-size: 12.5px;
  cursor: pointer;
}
.btn-link:active { opacity: 0.7; }

.section {
  margin: 18px 16px 0;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.section__header {
  width: 100%;
  padding: 13px 16px;
  font-size: 14px;
  font-weight: 700;
  color: var(--color-heading);
  background: var(--color-primary-tint);
  border-bottom: 1px solid var(--color-border);
  letter-spacing: 0.2px;
  border: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  text-align: left;
  cursor: pointer;
}

.section[data-collapsed="true"] .section__header {
  border-bottom: none;
}

.section__chevron {
  flex-shrink: 0;
  transition: transform 0.15s ease;
  color: var(--color-link);
  font-size: 13px;
}

.section[data-collapsed="true"] .section__chevron {
  transform: rotate(-90deg);
}

.section__body { padding: 4px 16px 4px; }

.field {
  padding: 11px 0;
  border-bottom: 1px solid var(--color-border);
}
.field:last-child { border-bottom: none; }

.field__label {
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--color-text-muted);
  font-weight: 700;
  margin-bottom: 3px;
}
.field__value {
  font-size: 14.5px;
  line-height: 1.5;
  color: var(--color-text);
}

.field__value.mono {
  font-family: var(--font-mono);
  font-size: 13.5px;
}

.toc {
  margin: 16px 16px 0;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.toc a {
  font-size: 12px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  color: var(--color-heading);
  padding: 6px 10px;
  border-radius: 999px;
}

/* ---------- Panel de carga (uso solo local) ---------- */
.admin {
  max-width: 720px;
  margin: 0 auto;
  padding: 20px 16px 60px;
}
.admin h1 { font-family: var(--font-display); color: var(--color-heading); }
.admin fieldset {
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  margin-bottom: 16px;
  padding: 12px 14px 16px;
}
.admin legend {
  font-weight: 700;
  color: var(--color-link);
  padding: 0 6px;
  font-size: 13px;
  text-transform: uppercase;
}
.admin label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--color-text-muted);
  margin: 10px 0 4px;
}
.admin input[type="text"],
.admin input[type="password"],
.admin textarea,
.admin select {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  background: var(--color-surface);
}
.admin textarea { min-height: 56px; resize: vertical; }
.admin__actions {
  position: sticky;
  bottom: 0;
  background: var(--color-bg);
  padding: 12px 0;
  display: flex;
  gap: 10px;
}
.btn {
  border: none;
  border-radius: 999px;
  padding: 12px 20px;
  font-size: 14.5px;
  font-weight: 700;
  cursor: pointer;
}
.btn--primary { background: var(--color-primary); color: #fff; }
.btn--ghost { background: var(--color-surface); color: var(--color-heading); border: 1px solid var(--color-border); }

@media (min-width: 700px) {
  .home__grid { grid-template-columns: 1fr 1fr; }
}

/* ---------- Footer global ---------- */
.app-footer {
  max-width: 680px;
  margin: 24px auto 0;
  padding: 16px 20px calc(20px + var(--safe-bottom));
  border-top: 1px solid var(--color-border);
  text-align: center;
}
.app-footer p {
  margin: 2px 0;
  font-size: 11.5px;
  color: var(--color-text-muted);
  line-height: 1.6;
}

/* ---------- Tema: oscuro automático (preferencia del sistema) o elegido por la persona ---------- */
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] {
    --color-bg: #0E1617;
    --color-surface: #162224;
    --color-primary: #0F5257;
    --color-primary-dark: #0A3B3F;
    --color-primary-tint: #1D3A3C;
    --color-text: #E6EEEE;
    --color-text-muted: #9FB1B3;
    --color-border: #2A3D3F;
    --color-risk: #FF8A80;
    --color-risk-tint: #3A1D1B;
    --color-risk-border: #6B2F2B;
    --color-accent-tint: #3A2A18;
    --color-heading: #A9E0DC;
    --color-link: #7CCBC6;
    color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --color-bg: #0E1617;
    --color-surface: #162224;
    --color-primary: #0F5257;
    --color-primary-dark: #0A3B3F;
    --color-primary-tint: #1D3A3C;
    --color-text: #E6EEEE;
    --color-text-muted: #9FB1B3;
    --color-border: #2A3D3F;
    --color-risk: #FF8A80;
    --color-risk-tint: #3A1D1B;
    --color-risk-border: #6B2F2B;
    --color-accent-tint: #3A2A18;
    --color-heading: #A9E0DC;
    --color-link: #7CCBC6;
    color-scheme: dark;
  }
}

/* ---------- Accesibilidad y listas ---------- */
.sr-only {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
.section__heading { margin: 0; font: inherit; }
.topbar__title:focus { outline: none; }
.field__list { margin: 0; padding-left: 20px; }
.field__list li { margin: 2px 0; }
.topbar__theme {
  background: rgba(255,255,255,0.14);
  border: none;
  color: #fff;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  cursor: pointer;
}
.topbar__theme svg { width: 20px; height: 20px; }
.offline-pill { margin-left: auto; }
.topbar__title { min-width: 0; }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; scroll-behavior: auto !important; }
}
