/* ============================================================
   Clinic Ledger — shared design foundation
   Single source of truth for design tokens (see DESIGN.md) and the
   base reset. Linked by every page BEFORE its page-specific <style>,
   so page rules can still override when a screen genuinely needs to.
   Shared components (.btn, .pill, .avatar, .segmented, .toast, and the
   app shell) live below as the single source of truth. Pages keep only
   genuinely per-screen overrides inline (e.g. .main width, the patient
   and add-visit detail topbars, the add-visit toast position).
   ============================================================ */
:root {
  /* Surfaces & ink */
  --bg:          oklch(1 0 0);
  --surface:     oklch(0.985 0.004 160);
  --surface-2:   oklch(0.962 0.006 160);
  --ink:         oklch(0.26 0.012 160);
  --ink-muted:   oklch(0.46 0.012 160);
  --hairline:    oklch(0.915 0.005 160);

  /* Brand accent — muted sage (carries white text on fills) */
  --primary:        oklch(0.47 0.085 158);
  --primary-strong: oklch(0.41 0.085 158);
  --primary-tint:   oklch(0.955 0.025 158);

  /* Status — the only loud voices */
  --amber-ink:  oklch(0.45 0.10 72);
  --amber-tint: oklch(0.95 0.05 82);
  --clay:       oklch(0.50 0.15 28);
  --clay-ink:   oklch(0.47 0.15 28);
  --clay-tint:  oklch(0.955 0.035 32);
  --clay-edge:  oklch(0.55 0.15 28 / 0.28);

  /* Spacing — 4pt scale */
  --s-1: 4px;  --s-2: 8px;  --s-3: 12px; --s-4: 16px;
  --s-5: 24px; --s-6: 32px; --s-7: 48px; --s-8: 64px;

  /* Radius */
  --r-sm: 8px; --r-md: 12px; --r-lg: 16px; --r-pill: 999px;

  /* Type scale (fixed rem, ~1.2 ratio) */
  --fs-xs: 0.75rem; --fs-sm: 0.875rem; --fs-base: 1rem;
  --fs-title: 1.0625rem; --fs-section: 1.1875rem; --fs-page: 1.5rem;

  /* Elevation — flat by default, lift on touch */
  --sh-1: 0 1px 2px oklch(0.26 0.02 160 / 0.06);
  --sh-2: 0 2px 8px oklch(0.26 0.02 160 / 0.10);
  --sh-pop: 0 10px 30px oklch(0.26 0.02 160 / 0.16);

  /* Z scale (semantic, low → high) */
  --z-letter: 20; --z-pop: 40; --z-bar: 50; --z-nav: 50; --z-toast: 80;

  /* Motion */
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --dur: 180ms;

  --maxw: 980px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: var(--fs-base);
  line-height: 1.5;
  color: var(--ink);
  background: var(--bg);
  font-kerning: normal;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
.num { font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1; }
button { font: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }
:focus-visible { outline: 2.5px solid var(--primary); outline-offset: 2px; border-radius: var(--r-sm); }

/* Visually hidden but read by screen readers (e.g. severity cues on alert chips). */
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
/* Keep the long clinic sub-title tidy in the narrow sidebar / phone topbar. */
.brand-sub { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

/* ============================================================
   Shared components — single source of truth.
   ============================================================ */

/* Buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: var(--s-2);
  min-height: 46px; padding: 0 var(--s-4); border-radius: var(--r-md); font-weight: 600; font-size: var(--fs-base);
  cursor: pointer; border: 1px solid transparent;
  transition: background var(--dur) var(--ease), transform var(--dur) var(--ease), border-color var(--dur) var(--ease); }
.btn svg { width: 19px; height: 19px; }
.btn-primary { background: var(--primary); color: oklch(1 0 0); box-shadow: var(--sh-1); }
.btn-primary:hover { background: var(--primary-strong); }
.btn-primary:active { transform: translateY(1px); }
.btn-ghost { background: var(--bg); color: var(--ink); border-color: var(--hairline); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-block { width: 100%; }

/* Pills */
.pill { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap;
  padding: 3px var(--s-2); border-radius: var(--r-pill); font-size: var(--fs-xs); font-weight: 600; letter-spacing: 0.005em; }
.pill.clay { background: var(--clay-tint); color: var(--clay-ink); }
.pill.amber { background: var(--amber-tint); color: var(--amber-ink); }
.pill.sage { background: var(--primary-tint); color: var(--primary-strong); }
.pill.neutral { background: var(--surface-2); color: var(--ink-muted); }

/* Avatar (initials in a circle) */
.avatar { width: 44px; height: 44px; border-radius: 50%; flex: none; display: grid; place-items: center;
  font-size: var(--fs-sm); font-weight: 600; background: var(--surface-2); color: var(--ink); border: 1px solid var(--hairline); }

/* Segmented control */
.segmented { display: flex; gap: 3px; padding: 3px; background: var(--surface-2); border: 1px solid var(--hairline); border-radius: var(--r-md); }
.segmented button { flex: 1; min-height: 44px; border: none; background: none; border-radius: calc(var(--r-md) - 3px);
  padding: 0 var(--s-3); font-weight: 500; color: var(--ink-muted); cursor: pointer; transition: background var(--dur) var(--ease), color var(--dur) var(--ease); }
.segmented button[aria-pressed="true"] { background: var(--bg); color: var(--ink); font-weight: 600; box-shadow: var(--sh-1); }

/* Toast (pages may override `bottom` — e.g. the bare add-visit flow) */
.toast { position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translate(-50%, 12px); z-index: var(--z-toast);
  background: var(--ink); color: oklch(0.98 0 0); padding: var(--s-3) var(--s-4); border-radius: var(--r-pill);
  font-size: var(--fs-sm); font-weight: 500; box-shadow: var(--sh-pop); opacity: 0; pointer-events: none;
  transition: opacity var(--dur) var(--ease), transform var(--dur) var(--ease); max-width: calc(100vw - var(--s-7)); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- App shell (sidebar on tablet, bottom bar on phone) ---------- */
.shell { min-height: 100dvh; }
.sidebar { display: none; }
.mark { width: 34px; height: 34px; flex: none; border-radius: 10px; background: var(--primary); color: oklch(1 0 0);
  display: grid; place-items: center; box-shadow: var(--sh-1); }
.mark svg { width: 20px; height: 20px; }
.brand { display: flex; align-items: center; gap: var(--s-3); }
.brand-name { font-weight: 600; font-size: var(--fs-title); letter-spacing: -0.01em; }
.brand-sub { font-size: var(--fs-xs); color: var(--ink-muted); margin-top: -2px; }

.bottomnav { position: fixed; left: 0; right: 0; bottom: 0; z-index: var(--z-nav);
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; align-items: end; background: oklch(1 0 0 / 0.92);
  backdrop-filter: blur(10px); border-top: 1px solid var(--hairline); padding: var(--s-2) var(--s-2) max(var(--s-2), env(safe-area-inset-bottom)); }
.nav-item { display: flex; flex-direction: column; align-items: center; gap: 3px; background: none; border: none; cursor: pointer;
  padding: var(--s-2) 0; color: var(--ink-muted); font-size: var(--fs-xs); font-weight: 500; min-height: 52px; border-radius: var(--r-sm); text-decoration: none; }
.nav-item svg { width: 24px; height: 24px; }
.nav-item[aria-current="page"] { color: var(--primary-strong); font-weight: 600; }
.nav-add { transform: translateY(-2px); }
.nav-add .fab { width: 50px; height: 50px; border-radius: 16px; background: var(--primary); color: oklch(1 0 0);
  display: grid; place-items: center; box-shadow: var(--sh-2); transition: background var(--dur) var(--ease), transform var(--dur) var(--ease); }
.nav-add:hover .fab { background: var(--primary-strong); }
.nav-add:active .fab { transform: scale(0.96); }
.nav-add .fab svg { width: 26px; height: 26px; }

@media (min-width: 768px) {
  .shell { display: grid; grid-template-columns: 248px 1fr; }
  .sidebar { display: flex; flex-direction: column; gap: var(--s-2); position: sticky; top: 0; align-self: start;
    height: 100dvh; padding: var(--s-5) var(--s-4); border-right: 1px solid var(--hairline); }
  .sidebar .brand { padding: 0 var(--s-2) var(--s-2); }
  .side-nav { display: flex; flex-direction: column; gap: 2px; margin-top: var(--s-4); }
  .side-item { display: flex; align-items: center; gap: var(--s-3); padding: var(--s-3); border-radius: var(--r-md);
    background: none; border: none; cursor: pointer; color: var(--ink-muted); font-size: var(--fs-base); font-weight: 500; text-align: left;
    text-decoration: none; transition: background var(--dur) var(--ease), color var(--dur) var(--ease); }
  .side-item svg { width: 22px; height: 22px; flex: none; }
  .side-item:hover { background: var(--surface); color: var(--ink); }
  .side-item[aria-current="page"] { background: var(--primary-tint); color: var(--primary-strong); font-weight: 600; }
  .side-cta { margin-top: var(--s-4); }
  .side-cta .btn { width: 100%; }
  .bottomnav { display: none; }
  .toast { bottom: var(--s-6); }
}
