/* ────────────────────────────────────────────────────────────────────────────
 * MedConnekt Design System — Foundation Stylesheet
 * Apothecary Modern / Clinical Editorial
 * ────────────────────────────────────────────────────────────────────────────
 */

@import url('https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,400;0,500;0,600;0,700;0,800;1,400;1,600&family=Montserrat:wght@300;400;500;600;700;800&family=Caveat:wght@500;600;700&family=JetBrains+Mono:wght@400;500;600&display=swap');

/* ── Tokens (mirrored from /design/tokens/tokens.css) ─────────────────────── */
:root {
  --berry-50:#FBE6EA; --berry-100:#F5C9D3; --berry-200:#ECA8B8;
  --berry-300:#DD7F95; --berry-400:#CC5876; --berry-500:#B93352;
  --berry-600:#A02945; --berry-700:#821F36; --berry-800:#62182A; --berry-900:#3D1019;
  --olive-50:#EAEEDA; --olive-100:#D8E0BD; --olive-300:#8B9A6E;
  --olive-500:#6B7A4E; --olive-700:#586642; --olive-900:#2F3923;
  --blush:#F5DDD8; --pink:#F2C4C4;
  --cream-0:#FFFFFF; --cream-50:#FDFBF8; --cream-100:#FAF6F1;
  --cream-200:#F9F5F0; --cream-300:#F5F0EA; --cream-400:#EADFD8;
  --paper:var(--cream-0); --cream:var(--cream-200); --line:var(--cream-400);
  --ink:#2A2420; --muted:#7A6E68; --on-berry:var(--cream-0); --on-olive:var(--cream-0);

  --success:#22A06B; --success-bg:var(--olive-50); --success-text:var(--olive-700); --success-line:#B5C490;
  --warning:#F5A524; --warning-bg:#FFF3D9; --warning-text:#8A5B00; --warning-line:#F0CC72;
  --danger:#D92D20;  --danger-bg:#FBE6E6;  --danger-text:#8B1A12;  --danger-line:#F2B6B2;
  --info:#7A5C8D;    --info-bg:#F4EBF7;    --info-text:#7A5C8D;    --info-line:#C7B5D9;

  --serif:'Playfair Display', Georgia, serif;
  --sans :'Montserrat', system-ui, sans-serif;
  --hand :'Caveat', cursive;
  --mono :'JetBrains Mono', ui-monospace, monospace;

  --radius-xs:8px; --radius-sm:14px; --radius-md:18px;
  --radius-lg:22px; --radius-xl:28px; --radius-2xl:36px; --radius-pill:9999px;

  --space-1:4px;  --space-2:8px;  --space-3:12px; --space-4:16px;
  --space-5:20px; --space-6:24px; --space-8:32px; --space-10:40px;
  --space-12:48px; --space-16:64px; --space-20:80px; --space-24:96px;

  --shadow-xs:0 1px 2px rgba(42,36,32,.04);
  --shadow-sm:0 1px 2px rgba(42,36,32,.04), 0 6px 20px rgba(42,36,32,.06);
  --shadow-md:0 4px 12px rgba(42,36,32,.08), 0 12px 32px rgba(42,36,32,.06);
  --shadow-lg:0 14px 26px rgba(139,35,62,.16);
  --shadow-xl:0 34px 90px rgba(42,36,32,.18);
  --ring-focus:0 0 0 4px rgba(185,51,82,.18);
  --ring-danger:0 0 0 4px rgba(217,45,32,.18);

  --ease-out:cubic-bezier(0.16, 1, 0.3, 1);
  --ease-in-out:cubic-bezier(0.65, 0, 0.35, 1);
  --dur-fast:140ms; --dur-med:240ms; --dur-slow:360ms;

  --container:1240px; --container-narrow:880px;
  --sidebar-w:288px; --topbar-h:72px;

  /* paper grain SVG noise */
  --grain:url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='1.4' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.165 0 0 0 0 0.141 0 0 0 0 0.125 0 0 0 0.05 0'/></filter><rect width='160' height='160' filter='url(%23n)'/></svg>");
}

[data-theme="dark"] {
  --cream-0:#1A1614; --cream-50:#1F1B18; --cream-100:#23201D;
  --cream-200:#221C19; --cream-300:#2A2421; --cream-400:#3A2E27;
  --paper:#26211D; --cream:#221C19; --line:#3A2E27;
  --ink:#F5F0EA; --muted:#B8A89E;
  --berry-500:#D24D6E; --olive-500:#95A56C;
  --shadow-sm:0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.3);
  --shadow-md:0 4px 12px rgba(0,0,0,.5), 0 12px 32px rgba(0,0,0,.35);
}

/* ── Reset / Base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body {
  font-family: var(--sans);
  font-size: clamp(15px, 1rem, 16px);
  line-height: 1.55;
  color: var(--ink);
  background: var(--cream);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  font-feature-settings: 'kern' 1, 'liga' 1, 'cv11' 1;
}
body::before {
  content:'';
  position: fixed; inset: 0;
  background-image: var(--grain);
  pointer-events: none;
  opacity: .35;
  z-index: 0;
  mix-blend-mode: multiply;
}
[data-theme="dark"] body::before { opacity: .12; mix-blend-mode: screen; }

img, svg, video { display: block; max-width: 100%; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
a { color: var(--ink); text-underline-offset: .25em; text-decoration-thickness: 1px; text-decoration-color: var(--berry-300); }
a:hover { color: var(--berry-600); text-decoration-color: var(--berry-500); }
input, select, textarea { font: inherit; color: inherit; }
::selection { background: var(--berry-100); color: var(--berry-900); }

:focus-visible {
  outline: none;
  box-shadow: var(--ring-focus);
  border-radius: var(--radius-xs);
}

/* ── Typography Scale ─────────────────────────────────────────────────────── */
.display, .h-display {
  font-family: var(--serif);
  font-weight: 700;
  font-size: clamp(40px, 6vw, 88px);
  line-height: 0.98;
  letter-spacing: -0.025em;
  color: var(--ink);
}
.h1 { font-family: var(--serif); font-weight: 700; font-size: clamp(32px, 4.2vw, 56px); line-height: 1.05; letter-spacing: -0.02em; }
.h2 { font-family: var(--serif); font-weight: 700; font-size: clamp(26px, 2.8vw, 36px); line-height: 1.12; letter-spacing: -0.015em; }
.h3 { font-family: var(--serif); font-weight: 600; font-size: clamp(20px, 1.8vw, 24px); line-height: 1.2;  letter-spacing: -0.01em; }
.h4 { font-family: var(--sans);  font-weight: 700; font-size: 18px; line-height: 1.3;  letter-spacing: -0.005em; }
.h5 { font-family: var(--sans);  font-weight: 700; font-size: 15px; line-height: 1.35; letter-spacing: 0.005em; }
.body-lg { font-size: 18px; line-height: 1.6; color: var(--ink); }
.body    { font-size: 15px; line-height: 1.6; }
.body-sm { font-size: 13.5px; line-height: 1.55; color: var(--muted); }
.eyebrow {
  font-family: var(--sans);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--olive-700);
}
.hand {
  font-family: var(--hand);
  font-weight: 600;
  color: var(--berry-600);
}
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.tabular { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.italic { font-style: italic; }
.serif { font-family: var(--serif); }
.center { text-align: center; }
.right { text-align: right; }
.numerals {
  font-family: var(--serif);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

/* ── Layout primitives ────────────────────────────────────────────────────── */
.container       { max-width: var(--container);        margin: 0 auto; padding: 0 var(--space-6); position: relative; z-index: 1; }
.container-narrow{ max-width: var(--container-narrow); margin: 0 auto; padding: 0 var(--space-6); position: relative; z-index: 1; }
.row { display: flex; gap: var(--space-4); align-items: center; }
.col { display: flex; flex-direction: column; gap: var(--space-4); }
.spread { display: flex; justify-content: space-between; align-items: center; gap: var(--space-4); }
.stack > * + * { margin-top: var(--space-4); }
.stack-2 > * + * { margin-top: var(--space-2); }
.stack-6 > * + * { margin-top: var(--space-6); }
.stack-10 > * + * { margin-top: var(--space-10); }
.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: var(--space-6); }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--space-6); }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--space-4); }
@media (max-width: 880px) {
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
}

/* ── Decorative elements ──────────────────────────────────────────────────── */
.rule {
  display: block;
  height: 1px;
  background: var(--line);
  border: 0;
  margin: var(--space-8) 0;
}
.rule-double { border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); height: 5px; background: transparent; margin: var(--space-8) 0; }
.rule-fancy {
  display: flex; align-items: center; gap: var(--space-3);
  color: var(--olive-700); margin: var(--space-10) 0;
  font-size: 12px; letter-spacing: 0.16em; text-transform: uppercase; font-weight: 700;
}
.rule-fancy::before, .rule-fancy::after { content:''; flex: 1; height: 1px; background: var(--line); }
.ornament { color: var(--berry-500); font-size: 14px; }
.ornament::before { content:'✦'; }

.section { padding: var(--space-16) 0; position: relative; z-index: 1; }
.section-tight { padding: var(--space-10) 0; }

.eyebrow-block {
  display: inline-flex; align-items: center; gap: var(--space-2);
  font-size: 11px; font-weight: 700; letter-spacing: 0.18em; text-transform: uppercase;
  color: var(--olive-700);
}
.eyebrow-block::before {
  content:''; display:inline-block; width:18px; height:1px; background: var(--olive-500);
}

/* page header pattern: huge title + meta row + rule */
.page-head { padding: var(--space-12) 0 var(--space-8); }
.page-head .meta {
  display: grid;
  grid-template-columns: max-content 1fr max-content;
  gap: var(--space-4);
  align-items: end;
  border-bottom: 1px solid var(--line);
  padding-bottom: var(--space-6);
  margin-top: var(--space-6);
}
.page-head .meta .item {
  display:flex; flex-direction:column; gap: 2px;
  font-size: 12px; letter-spacing: 0.06em;
}
.page-head .meta .item strong { font-family: var(--mono); font-weight: 500; color: var(--ink); }
.page-head .meta .item span { color: var(--muted); text-transform: uppercase; font-size: 10px; letter-spacing: 0.16em; }

.crumbs {
  font-size: 12px; color: var(--muted); letter-spacing: 0.04em;
  display: flex; align-items: center; gap: var(--space-2);
  font-family: var(--mono);
}
.crumbs a { color: var(--muted); text-decoration: none; }
.crumbs a:hover { color: var(--ink); }
.crumbs .sep { color: var(--muted); font-size: 11px; opacity: .7; }

/* stamp — small badge in corners */
.stamp {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 10px;
  border: 1px solid var(--berry-500);
  color: var(--berry-600);
  font-family: var(--mono); font-size: 10px;
  letter-spacing: 0.15em; text-transform: uppercase;
  border-radius: var(--radius-xs);
  background: var(--cream-50);
  transform: rotate(-1.5deg);
}
.stamp.olive { border-color: var(--olive-500); color: var(--olive-700); }

/* ── Page chrome (system pages) ───────────────────────────────────────────── */
.ds-topbar {
  position: sticky; top: 0; z-index: 50;
  height: var(--topbar-h);
  background: rgba(249,245,240,.85);
  backdrop-filter: blur(14px) saturate(140%);
  -webkit-backdrop-filter: blur(14px) saturate(140%);
  border-bottom: 1px solid var(--line);
}
[data-theme="dark"] .ds-topbar { background: rgba(34,28,25,.85); }
.ds-topbar .inner {
  display:flex; align-items: center; justify-content: space-between; gap: var(--space-6);
  height: 100%;
  max-width: var(--container);
  margin: 0 auto;
  padding: 0 var(--space-6);
}
.brand {
  display:inline-flex; align-items:baseline; gap: 10px;
  font-family: var(--serif); font-weight: 700; font-size: 22px;
  letter-spacing: -0.01em;
  color: var(--ink);
  text-decoration: none;
}
.brand .mark {
  display:inline-block; width:10px; height:10px; background: var(--berry-500); border-radius: 2px;
  transform: rotate(45deg) translateY(-1px);
}
.brand small {
  font-family: var(--mono); font-size: 10px; font-weight: 500;
  letter-spacing: 0.18em; text-transform: uppercase;
  color: var(--muted);
}
.nav-pill { display: inline-flex; gap: 2px; padding: 4px; background: var(--cream-100); border-radius: var(--radius-pill); border: 1px solid var(--line); }
.nav-pill a {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 14px; font-size: 12px; font-weight: 600; letter-spacing: 0.04em;
  color: var(--muted); text-decoration: none; border-radius: var(--radius-pill);
  transition: color var(--dur-fast), background var(--dur-fast);
}
.nav-pill a[aria-current="page"], .nav-pill a:hover { background: var(--paper); color: var(--ink); box-shadow: var(--shadow-xs); }

/* system layout: sidebar + main */
.ds-app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: calc(100vh - var(--topbar-h));
  gap: 0;
}
@media (max-width: 1024px) { .ds-app { grid-template-columns: 1fr; } .ds-sidebar { display: none; } }

.ds-sidebar {
  position: sticky; top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h));
  overflow-y: auto;
  padding: var(--space-8) var(--space-6);
  border-right: 1px solid var(--line);
  background: linear-gradient(180deg, var(--cream-100) 0%, var(--cream-200) 100%);
}
.ds-sidebar .nav-section { margin-bottom: var(--space-8); }
.ds-sidebar .nav-section h6 {
  font-family: var(--mono); font-size: 10px; font-weight: 600;
  letter-spacing: 0.2em; text-transform: uppercase; color: var(--muted);
  margin-bottom: var(--space-3);
  display: flex; align-items: center; gap: var(--space-2);
}
.ds-sidebar .nav-section h6::before {
  content:''; display:inline-block; width: 8px; height: 1px; background: var(--olive-500);
}
.ds-sidebar a {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px; border-radius: var(--radius-xs);
  font-size: 14px; font-weight: 500; color: var(--ink);
  text-decoration: none;
  letter-spacing: -0.005em;
  transition: background var(--dur-fast), color var(--dur-fast);
}
.ds-sidebar a:hover { background: var(--paper); }
.ds-sidebar a[aria-current="page"] { background: var(--paper); color: var(--berry-700); box-shadow: var(--shadow-xs); }
.ds-sidebar a[aria-current="page"]::before {
  content:''; width: 3px; height: 16px; background: var(--berry-500); border-radius: 2px; margin-left: -10px; margin-right: 7px;
}

.ds-main { padding: var(--space-10) var(--space-12); max-width: 1080px; }
@media (max-width: 1024px) { .ds-main { padding: var(--space-8) var(--space-6); } }

/* ── Utility classes ──────────────────────────────────────────────────────── */
.surface { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius-lg); }
.surface-soft { background: var(--cream-50); border: 1px solid var(--line); border-radius: var(--radius-lg); }
.shadow-sm { box-shadow: var(--shadow-sm); }
.shadow-md { box-shadow: var(--shadow-md); }
.shadow-lg { box-shadow: var(--shadow-lg); }

.text-berry { color: var(--berry-600); }
.text-olive { color: var(--olive-700); }
.text-muted { color: var(--muted); }
.text-danger { color: var(--danger-text); }

.bg-cream { background: var(--cream); }
.bg-paper { background: var(--paper); }
.bg-blush { background: var(--blush); }
.bg-olive { background: var(--olive-50); }

.no-wrap { white-space: nowrap; }
.full { width: 100%; }
.hide { display: none !important; }
@media (max-width: 768px) { .md-hide { display: none !important; } }

/* ── Animations ───────────────────────────────────────────────────────────── */
@keyframes fadeUp { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes pulse-dot { 0%, 100% { opacity: 1; } 50% { opacity: .4; } }
@keyframes ring-pulse { 0% { box-shadow: 0 0 0 0 rgba(185,51,82,.4); } 70% { box-shadow: 0 0 0 14px rgba(185,51,82,0); } 100% { box-shadow: 0 0 0 0 rgba(185,51,82,0); } }
@keyframes shimmer { 0% { background-position: -200px 0; } 100% { background-position: calc(200px + 100%) 0; } }

.reveal { animation: fadeUp var(--dur-slow) var(--ease-out) both; }
.reveal.d1 { animation-delay: 80ms; }
.reveal.d2 { animation-delay: 160ms; }
.reveal.d3 { animation-delay: 240ms; }
.reveal.d4 { animation-delay: 320ms; }
.reveal.d5 { animation-delay: 400ms; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0ms !important;
    transition-duration: 0ms !important;
  }
}

/* ── Mobile rescue (≤ 768px) ──────────────────────────────────────────────── */
@media (max-width: 768px) {
  /* layout containers */
  .container,
  .container-narrow { padding: 0 var(--space-4); }

  /* system topbar */
  .ds-topbar { height: auto; }
  .ds-topbar .inner {
    flex-wrap: wrap;
    padding: var(--space-3) var(--space-4);
    gap: var(--space-2);
    min-height: 60px;
  }
  .ds-topbar .brand small { display: none; }
  .ds-topbar .nav-pill { display: none; }

  /* system subpage main */
  .ds-main { padding: var(--space-6) var(--space-4); }
  .page-head { padding: var(--space-6) 0 var(--space-4); }
  .page-head .meta { grid-template-columns: 1fr; gap: var(--space-2); }
  .page-head .meta .item.right { text-align: left; }

  /* generic grids collapse */
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr !important; gap: var(--space-4); }

  /* type smaller */
  .display, .h-display { font-size: clamp(30px, 9vw, 44px); }
  .h1 { font-size: clamp(26px, 7vw, 36px); }
  .h2 { font-size: clamp(22px, 5vw, 28px); }
  .h3 { font-size: clamp(18px, 4.5vw, 22px); }

  /* component density */
  .card { padding: var(--space-4); }
  .stat { padding: var(--space-4); }
  .stat .stat__value { font-size: 40px; }
  .modal { padding: var(--space-5); border-radius: var(--radius-lg); }
  .empty { padding: var(--space-10) var(--space-4); }
  .rule-fancy { margin: var(--space-8) 0; }

  /* cabinet shell */
  .cab-shell { grid-template-columns: 1fr; }
  .cab-side { display: none; }
  .cab-top {
    padding: var(--space-3) var(--space-4);
    gap: var(--space-3);
    flex-wrap: wrap;
    min-height: 60px;
  }
  .cab-top .crumbs {
    font-size: 11px;
    max-width: 70vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .cab-content { padding: var(--space-5) var(--space-4) 100px !important; max-width: 100% !important; }
  .bot-nav { display: block; }

  /* tabs scrollable */
  .tabs, .tabs-pill {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap !important;
    scrollbar-width: none;
  }
  .tabs::-webkit-scrollbar,
  .tabs-pill::-webkit-scrollbar { display: none; }
  .tabs button, .tabs-pill button { white-space: nowrap; flex-shrink: 0; }

  /* domain mc-rec collapses */
  .mc-rec { grid-template-columns: 1fr !important; }
  .mc-rec__thumb { aspect-ratio: 16/9; }

  /* inline grid layouts in templates — catch-all */
  [style*="grid-template-columns"] { grid-template-columns: 1fr !important; }

  /* sticky aside un-stickies */
  aside[style*="sticky"],
  aside .surface[style*="sticky"] { position: static !important; }

  /* surface with big padding */
  .surface[style*="padding: 40px"],
  .surface[style*="padding: 32px"] { padding: var(--space-5) !important; }
  form[style*="padding: 40px"] { padding: var(--space-5) !important; }

  /* hero stat cards in dashboard */
  .stat .stat__value { font-size: clamp(36px, 11vw, 56px) !important; }

  /* numerals don't overflow */
  .numerals { word-break: break-all; }

  /* spread rows wrap by default */
  .spread { flex-wrap: wrap; }

  /* page header h1 with margin */
  .h1[style*="max-width"] { max-width: 100% !important; }

  /* cab-top actions */
  .cab-top .actions { margin-left: auto; }
}

/* ── Smaller phones (≤ 420px) ──────────────────────────────────────────── */
@media (max-width: 420px) {
  .ds-topbar .brand { font-size: 18px; }
  .stat .stat__value { font-size: 32px !important; }
  .display { font-size: clamp(26px, 9vw, 36px); }
  .h1 { font-size: clamp(24px, 7vw, 30px); }
  .btn { padding: 10px 16px; font-size: 13px; }
  .btn--lg { padding: 14px 20px; font-size: 14px; min-height: 48px; }
  .modal__title { font-size: 22px; }
}

/* ── Footer (system) ──────────────────────────────────────────────────────── */
.ds-foot {
  border-top: 1px solid var(--line);
  margin-top: var(--space-20);
  padding: var(--space-10) 0;
  color: var(--muted);
  font-size: 12px;
}
.ds-foot .inner {
  max-width: var(--container);
  margin: 0 auto;
  padding: 0 var(--space-6);
  display: flex; flex-wrap: wrap; justify-content: space-between; gap: var(--space-4);
  font-family: var(--mono);
  letter-spacing: 0.05em;
}
