/* ---------- base ---------- */
:root {
  --bg: #0a0a0b;
  --bg-elev: #15151a;
  --bg-elev-2: #1d1d24;
  --fg: #e7e7ea;
  --fg-muted: #9094a0;
  --fg-dim: #5b5e6a;
  --accent: #6da6ff;
  --accent-dim: #3b6fc4;
  --good: #4ad295;
  --warn: #f6b352;
  --bad: #ff6b6b;
  --critical: #ff4655;
  --border: #25252e;
  --radius: 10px;
  --gap: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 4px 12px rgba(0,0,0,0.2);
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", sans-serif;
  --font-mono: ui-monospace, "SF Mono", "Cascadia Code", "Roboto Mono", monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-sans);
  line-height: 1.5;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--fg-muted); font-size: 0.9em; }
code { font-family: var(--font-mono); background: var(--bg-elev-2); padding: 0.1em 0.4em; border-radius: 4px; font-size: 0.9em; }
button, .link-btn {
  background: none; border: none; color: var(--accent); cursor: pointer; padding: 0; font: inherit;
}
button:hover, .link-btn:hover { text-decoration: underline; }

/* ---------- topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: rgba(10,10,11,0.85);
  backdrop-filter: saturate(140%) blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1200px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.brand-mark { color: var(--accent); }
.topnav { display: flex; gap: 18px; align-items: center; }
.topnav a { color: var(--fg-muted); font-weight: 500; padding: 6px 0; border-bottom: 2px solid transparent; }
.topnav a.active { color: var(--fg); border-color: var(--accent); }
.topnav a.muted { color: var(--fg-dim); cursor: not-allowed; }
.topnav .logout { margin-left: 12px; color: var(--fg-dim); }

/* ---------- container ---------- */
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 20px 60px;
  display: grid;
  gap: var(--gap);
}

/* ---------- hero ---------- */
.hero {
  display: flex; justify-content: space-between; align-items: flex-end; gap: 20px;
  padding: 8px 4px 4px;
}
.hero-greeting h1 { margin: 0 0 4px; font-size: 26px; }
.hero-meta { display: flex; gap: 12px; }
.meta-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 14px;
  text-align: right;
  min-width: 100px;
}
.meta-label { font-size: 0.75em; color: var(--fg-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.meta-value { font-size: 22px; font-weight: 600; }
.meta-card--alert { border-color: var(--critical); text-decoration: none; }
.meta-card--alert .meta-value { color: var(--critical); }
.meta-pulse { color: var(--critical); animation: pulse 1.6s ease-in-out infinite; font-size: 0.7em; vertical-align: middle; }
@keyframes pulse { 0%, 100% { opacity: 0.4; } 50% { opacity: 1; } }

/* ---------- card ---------- */
.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow);
}
.card-head {
  display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 12px;
  flex-wrap: wrap; gap: 8px;
}
.card-head h2 { font-size: 16px; margin: 0; font-weight: 600; }
.card-head h2 .icon { margin-right: 6px; }
.card-sub { font-size: 0.85em; color: var(--fg-muted); }

/* ---------- alerts ---------- */
.alert-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 12px; }
.alert {
  border-left: 3px solid var(--border);
  padding: 10px 14px;
  background: var(--bg-elev-2);
  border-radius: 6px;
}
.alert-headline { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.alert-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--fg-muted); flex-shrink: 0; }
.alert--critical { border-left-color: var(--critical); }
.alert--critical .alert-dot { background: var(--critical); box-shadow: 0 0 0 4px rgba(255,70,85,0.18); }
.alert--watch { border-left-color: var(--warn); }
.alert--watch .alert-dot { background: var(--warn); }
.alert--adherence { border-left-color: var(--accent); }
.alert--adherence .alert-dot { background: var(--accent); }
.alert-rule { font-size: 0.9em; color: var(--fg-muted); margin-top: 2px; }
.alert-action { font-size: 0.95em; margin-top: 6px; }
.alert-source { font-size: 0.8em; margin-top: 4px; }
.alerts-empty { background: var(--bg-elev); border-color: var(--good); }
.alerts-empty .icon { color: var(--good); }

/* ---------- insights ---------- */
.insights-md { line-height: 1.65; white-space: pre-wrap; }
.insights-status { margin-top: 10px; }
.insights-disclaimer { margin-top: 12px; font-size: 0.8em; padding-top: 10px; border-top: 1px solid var(--border); }

/* ---------- tasks ---------- */
.task-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.task-item {
  display: grid; grid-template-columns: 220px 1fr; gap: 12px; align-items: baseline;
  padding: 8px 0; border-bottom: 1px dashed var(--border);
}
.task-item:last-child { border-bottom: 0; }
.task-section { font-size: 0.75em; color: var(--fg-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.task-text { font-size: 0.95em; }

/* ---------- weekstrip ---------- */
.weekstrip {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px;
}
.metric-tile {
  background: var(--bg-elev-2);
  border-radius: 8px;
  padding: 12px 14px;
  position: relative;
}
.metric-label { font-size: 0.8em; color: var(--fg-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.metric-value { font-size: 26px; font-weight: 600; line-height: 1.1; margin-top: 2px; }
.metric-unit { font-size: 14px; color: var(--fg-muted); font-weight: 400; }
.metric-delta { font-size: 0.85em; margin-top: 4px; min-height: 1em; }
.metric-delta.up { color: var(--good); }
.metric-delta.down { color: var(--bad); }
.metric-delta.neutral { color: var(--fg-muted); }
.spark-wrap { position: relative; height: 44px; margin-top: 6px; }
.spark-wrap > canvas { position: absolute; inset: 0; width: 100% !important; height: 100% !important; }

/* Chart cards need a bounded height so Chart.js can size against it */
.chart-card > canvas, .lab-grid > div > canvas {
  display: block;
  max-width: 100%;
}
.chart-card { position: relative; }
.chart-card > canvas { max-height: 280px; }
.lab-grid > div { position: relative; }
.lab-grid > div > canvas { max-height: 220px; }

/* ---------- charts ---------- */
.chart-card canvas { display: block; width: 100% !important; }
.lab-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 18px;
}
.lab-grid h3 { font-size: 13px; margin: 0 0 6px; color: var(--fg-muted); font-weight: 500; }

/* ---------- meds ---------- */
.med-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.med-item { display: flex; justify-content: space-between; align-items: baseline; }
.med-name { font-weight: 500; }
.med-tags { font-size: 0.8em; }

/* ---------- lab table ---------- */
.lab-table { width: 100%; border-collapse: collapse; font-size: 0.9em; }
.lab-table th, .lab-table td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.lab-table th { color: var(--fg-muted); font-weight: 500; font-size: 0.8em; text-transform: uppercase; letter-spacing: 0.04em; }
.lab-row--flag { background: rgba(255,107,107,0.06); }
.lab-row--flag .lab-value { color: var(--bad); font-weight: 600; }
.lab-panel { white-space: nowrap; max-width: 180px; overflow: hidden; text-overflow: ellipsis; font-size: 0.8em; }
.lab-test { font-weight: 500; }
.lab-value { font-family: var(--font-mono); }
.lab-ref { font-family: var(--font-mono); font-size: 0.85em; }
.flag-chip { background: var(--bad); color: #0a0a0b; padding: 1px 7px; border-radius: 10px; font-size: 0.7em; font-weight: 700; letter-spacing: 0.04em; }

/* ---------- conditions ---------- */
.condition-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.condition-card { background: var(--bg-elev-2); padding: 12px 14px; border-radius: 8px; border-left: 3px solid var(--accent); }
.condition-title { font-weight: 600; }
.condition-tags { font-size: 0.8em; margin-top: 4px; }

/* ---------- visit timeline ---------- */
.visit-timeline { display: grid; gap: 10px; }
.visit-card { display: grid; grid-template-columns: 110px 1fr; gap: 14px; padding: 14px 16px; background: var(--bg-elev-2); border-radius: 8px; }
.visit-card--link { text-decoration: none; color: inherit; transition: background 0.15s, transform 0.15s; }
.visit-card--link:hover { background: #23232c; text-decoration: none; transform: translateX(2px); }
.visit-date { font-family: var(--font-mono); color: var(--accent); font-weight: 600; line-height: 1.15; }
.visit-time { font-size: 0.78em; font-family: var(--font-mono); margin-top: 2px; }
.visit-title { font-weight: 600; font-size: 0.95em; }
.visit-provider, .visit-tags, .visit-file { font-size: 0.85em; margin-top: 3px; }
.visit-summary { font-size: 0.92em; color: var(--fg); margin: 8px 0; line-height: 1.55; }
.visit-sections { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 8px; }
.section-chip { display: inline-block; background: rgba(109,166,255,0.10); color: var(--accent); padding: 1px 8px; border-radius: 10px; font-size: 0.72em; font-weight: 500; }

/* ---------- card link headings (e.g., "Latest panel →") ---------- */
.card-link { color: var(--fg); text-decoration: none; }
.card-link:hover { color: var(--accent); text-decoration: none; }

/* ---------- lab prev/next navigation ---------- */
.lab-nav { display: flex; justify-content: space-between; gap: 12px; margin: 6px 0 12px; font-size: 0.85em; }
.lab-nav-link { color: var(--fg-muted); padding: 6px 10px; background: var(--bg-elev-2); border-radius: 6px; }
.lab-nav-link:hover { color: var(--accent); background: var(--bg-elev); text-decoration: none; }
.lab-nav-next { margin-left: auto; }

/* ---------- visit detail page ---------- */
.visit-detail-hero { padding: 8px 4px 0; }
.visit-detail-hero h1 { margin: 6px 0 6px; font-size: 24px; }
.visit-detail-meta { display: flex; flex-wrap: wrap; gap: 6px; font-size: 0.9em; }
.visit-detail-meta span:first-child { color: var(--accent); font-family: var(--font-mono); font-weight: 500; }
.back-link { font-size: 0.85em; color: var(--fg-muted); }
.back-link:hover { color: var(--accent); }
.visit-content { padding: 20px 26px; }
.visit-toc { display: flex; flex-wrap: wrap; gap: 8px 12px; padding-bottom: 14px; margin-bottom: 18px; border-bottom: 1px solid var(--border); align-items: baseline; }
.toc-link { font-size: 0.82em; color: var(--accent); padding: 2px 8px; background: rgba(109,166,255,0.08); border-radius: 4px; }
.toc-link:hover { background: rgba(109,166,255,0.18); text-decoration: none; }

/* Markdown content rendering */
.md-content { line-height: 1.65; }
.md-content h2 { font-size: 17px; margin: 26px 0 10px; padding-top: 4px; border-top: 1px solid var(--border); padding-top: 18px; }
.md-content h2:first-child { border-top: 0; padding-top: 0; margin-top: 0; }
.md-content h3 { font-size: 14px; margin: 16px 0 8px; color: var(--accent); font-weight: 600; }
.md-content h4 { font-size: 13px; margin: 12px 0 6px; color: var(--fg-muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.md-content p { margin: 8px 0; }
.md-content ul, .md-content ol { margin: 8px 0; padding-left: 1.5em; }
.md-content li { margin: 3px 0; }
.md-content strong { color: var(--fg); font-weight: 600; }
.md-content em { color: var(--fg-muted); }
.md-content code { background: var(--bg-elev-2); }
.md-content blockquote { border-left: 3px solid var(--accent); padding: 4px 14px; margin: 10px 0; background: var(--bg-elev-2); color: var(--fg-muted); border-radius: 0 6px 6px 0; }
.md-content table { width: 100%; border-collapse: collapse; margin: 12px 0; font-size: 0.9em; }
.md-content th, .md-content td { padding: 6px 10px; border-bottom: 1px solid var(--border); text-align: left; }
.md-content th { color: var(--fg-muted); font-weight: 500; font-size: 0.82em; text-transform: uppercase; letter-spacing: 0.04em; }
.md-content hr { border: 0; border-top: 1px solid var(--border); margin: 20px 0; }

/* ---------- footer ---------- */
.footer {
  max-width: 1200px; margin: 0 auto; padding: 16px 20px 30px;
  display: flex; justify-content: space-between; gap: 16px;
  font-size: 0.85em; color: var(--fg-muted);
  border-top: 1px solid var(--border);
}

/* ---------- login ---------- */
.login-page {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
}
.login-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 28px;
  width: 100%;
  max-width: 360px;
  box-shadow: var(--shadow);
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card label { display: block; margin: 16px 0 6px; font-size: 0.85em; color: var(--fg-muted); }
.login-card input[type="password"] {
  width: 100%; padding: 10px 12px; background: var(--bg-elev-2); border: 1px solid var(--border);
  color: var(--fg); border-radius: 6px; font: inherit;
}
.login-card button[type="submit"] {
  width: 100%; padding: 11px; margin-top: 18px;
  background: var(--accent); color: #0a0a0b; border-radius: 6px; font-weight: 600;
  cursor: pointer; border: none;
}
.login-card button[type="submit"]:hover { text-decoration: none; background: #88b9ff; }
.login-error {
  background: rgba(255,70,85,0.12); color: var(--critical); padding: 8px 12px; border-radius: 6px;
  font-size: 0.9em; margin-bottom: 4px;
}
.login-foot { margin-top: 18px; font-size: 0.8em; }

/* ---------- responsive ---------- */
@media (max-width: 720px) {
  .hero { flex-direction: column; align-items: stretch; }
  .hero-meta { justify-content: stretch; }
  .meta-card { flex: 1; text-align: left; }
  .weekstrip { grid-template-columns: repeat(2, 1fr); }
  .lab-grid { grid-template-columns: 1fr; }
  .task-item { grid-template-columns: 1fr; }
  .task-section { margin-bottom: 2px; }
}
