/* ==============================================
         DESIGN TOKENS — Dark mode (default)
      =============================================== */
      :root {
        --bg:           #0f1923;
        --bg-soft:      #131f2e;
        --surface:      #182333;
        --surface-2:    #1f2e42;
        --surface-3:    #263650;
        --border:       rgba(77, 140, 255, 0.16);
        --border-hard:  #2d4260;

        --text:         #e6f0ff;
        --text-2:       #a8c0d8;
        --text-muted:   #6a8aa8;
        --link:         #7fc0ff;
        --link-hover:   #a8d7ff;
        --link-visited: #9db8ff;

        --accent:       #4da6ff;
        --accent-dim:   rgba(77, 166, 255, 0.13);
        --accent-strong:#2e88ee;

        --success:      #3dd4a0;
        --success-dim:  rgba(61, 212, 160, 0.13);
        --warn:         #f5c84a;
        --warn-dim:     rgba(245, 200, 74, 0.12);
        --warn-bg:      #231d08;
        --warn-text:    #f5d87a;
        --error:        #ff6060;
        --error-dim:    rgba(255, 96, 96, 0.13);
        --brand-tile:   #132949;
        --brand-mark:   #55c7a8;
        --brand-line:   #f8fbff;
        --brand-live:   #e6f0ff;
        --brand-guide:  #55c7a8;

        --nav-bg:       rgba(9, 15, 27, 0.97);
        --sheet-bg:     #131f2e;

        --font-ui:      "Inter", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
        --font-content: "Inter", system-ui, -apple-system, sans-serif;

        --font-scale:   1;
        --text-xs:      calc(0.69rem  * var(--font-scale));
        --text-sm:      calc(0.8rem   * var(--font-scale));
        --text-base:    calc(0.9rem   * var(--font-scale));
        --text-md:      calc(0.97rem  * var(--font-scale));
        --text-lg:      calc(1.08rem  * var(--font-scale));
        --text-xl:      calc(1.22rem  * var(--font-scale));

        --topbar-h:     calc(56px + env(safe-area-inset-top, 0px));
        --safe-bottom:  env(safe-area-inset-bottom, 0px);
        --bottom-nav-clearance: calc(76px + var(--safe-bottom));
        --keyboard-offset: 0px;

        --r-sm:  8px;
        --r-md:  12px;
        --r-lg:  16px;
        --r-xl:  20px;
        --r-full:999px;
      }

      /* ==============================================
         LIGHT MODE
      =============================================== */
      [data-theme="light"] {
        --bg:           #f0f4f9;
        --bg-soft:      #e5ecf5;
        --surface:      #ffffff;
        --surface-2:    #eaf0f8;
        --surface-3:    #dde6f2;
        --border:       rgba(0, 80, 180, 0.12);
        --border-hard:  #c4d2e4;

        --text:         #1a2535;
        --text-2:       #3a5572;
        --text-muted:   #6a8aa8;
        --link:         #005fca;
        --link-hover:   #004eaa;
        --link-visited: #5c4fc9;

        --accent:       #0060cc;
        --accent-dim:   rgba(0, 96, 204, 0.09);
        --accent-strong:#004eaa;

        --success:      #1a9e72;
        --success-dim:  rgba(26, 158, 114, 0.1);
        --warn:         #b87d00;
        --warn-dim:     rgba(184, 125, 0, 0.1);
        --warn-bg:      #fff8e0;
        --warn-text:    #7a5200;
        --error:        #cc2222;
        --error-dim:    rgba(204, 34, 34, 0.09);
        --brand-tile:   #14284a;
        --brand-mark:   #42b89a;
        --brand-line:   #ffffff;
        --brand-live:   #14284a;
        --brand-guide:  #42b89a;

        --nav-bg:       rgba(240, 244, 249, 0.97);
        --sheet-bg:     #ffffff;
      }

      @media (prefers-color-scheme: light) {
        :root:not([data-theme="dark"]) {
          --bg:           #f0f4f9;
          --bg-soft:      #e5ecf5;
          --surface:      #ffffff;
          --surface-2:    #eaf0f8;
          --surface-3:    #dde6f2;
          --border:       rgba(0, 80, 180, 0.12);
          --border-hard:  #c4d2e4;
          --text:         #1a2535;
          --text-2:       #3a5572;
          --text-muted:   #6a8aa8;
          --link:         #005fca;
          --link-hover:   #004eaa;
          --link-visited: #5c4fc9;
          --accent:       #0060cc;
          --accent-dim:   rgba(0, 96, 204, 0.09);
          --accent-strong:#004eaa;
          --success:      #1a9e72;
          --success-dim:  rgba(26, 158, 114, 0.1);
          --warn:         #b87d00;
          --warn-dim:     rgba(184, 125, 0, 0.1);
          --warn-bg:      #fff8e0;
          --warn-text:    #7a5200;
          --error:        #cc2222;
          --error-dim:    rgba(204, 34, 34, 0.09);
          --brand-tile:   #14284a;
          --brand-mark:   #42b89a;
          --brand-line:   #ffffff;
          --brand-live:   #14284a;
          --brand-guide:  #42b89a;
          --nav-bg:       rgba(240, 244, 249, 0.97);
          --sheet-bg:     #ffffff;
        }
      }

      /* ==============================================
         FONT FAMILIES
      =============================================== */
      [data-font="system"]   { --font-ui: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
      [data-font="atkinson"] { --font-ui: "Atkinson Hyperlegible", sans-serif; }
      [data-font="serif"]    { --font-ui: "Source Serif 4", Georgia, serif; }

      /* ==============================================
         FONT SIZES
      =============================================== */
      [data-fontsize="sm"] { --font-scale: 0.88; }
      [data-fontsize="md"] { --font-scale: 1; }
      [data-fontsize="lg"] { --font-scale: 1.13; }
      [data-fontsize="xl"] { --font-scale: 1.28; }

      /* ==============================================
         RESET & BASE
      =============================================== */
      *, *::before, *::after { box-sizing: border-box; }
      html, body { margin: 0; padding: 0; }
      html {
        height: 100%;
        overscroll-behavior: none;
        touch-action: manipulation;
      }

      body {
        font-family: var(--font-ui);
        font-size: var(--text-base);
        line-height: 1.5;
        background: var(--bg);
        color: var(--text);
        min-height: -webkit-fill-available;
        min-height: 100dvh;
        -webkit-tap-highlight-color: transparent;
        overscroll-behavior-y: none;
        touch-action: manipulation;
      }

      a {
        color: var(--link);
        text-decoration-color: color-mix(in srgb, currentColor 55%, transparent);
        text-underline-offset: 0.15em;
      }

      a:visited { color: var(--link-visited); }
      a:hover { color: var(--link-hover); }
      a:active { color: var(--link-hover); }
      a:focus-visible {
        outline: 2px solid var(--accent);
        outline-offset: 2px;
        border-radius: 2px;
      }

      body::before {
        content: "";
        position: fixed;
        inset: 0;
        pointer-events: none;
        z-index: 0;
        background:
          radial-gradient(ellipse 90% 50% at 5% -5%,  rgba(60, 120, 255, 0.07) 0%, transparent 55%),
          radial-gradient(ellipse 60% 40% at 95% 5%,  rgba(61, 212, 160, 0.05) 0%, transparent 50%);
      }

      [data-theme="light"] body::before,
      @media (prefers-color-scheme: light) { :root:not([data-theme="dark"]) body::before { display: none; } }

      .app {
        position: relative;
        z-index: 1;
        width: min(980px, 100%);
        margin: 0 auto;
        min-height: 100dvh;
        padding-bottom: calc(80px + var(--safe-bottom));
      }

      input, select, button, textarea { font: inherit; }
      button,
      [role="button"],
      [data-open-card],
      [data-open-calculator],
      [data-collection],
      [data-group-toggle],
      .nav-btn,
      .icon-btn,
      .card-item-body,
      .collection-tile {
        touch-action: manipulation;
      }

      /* ==============================================
         STATUS BAR FILL — sits behind topbar, covers inset area
         Must be outside topbar's stacking context so it never
         overlaps the topbar's own flex children.
      =============================================== */
      .status-bar-fill {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: env(safe-area-inset-top, 0px);
        background: var(--bg);
        z-index: 49; /* below topbar (z-index: 50) */
        pointer-events: none;
      }

      /* ==============================================
         TOPBAR
      =============================================== */
      .topbar {
        position: sticky;
        top: 0;
        z-index: 50;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
        /* Push content below iOS/Android status bar */
        padding: calc(9px + env(safe-area-inset-top, 0px)) 12px 9px;
        min-height: var(--topbar-h);
        background: color-mix(in srgb, var(--bg) 88%, transparent);
        backdrop-filter: blur(14px) saturate(1.4);
        -webkit-backdrop-filter: blur(14px) saturate(1.4);
        border-bottom: 1px solid var(--border);
      }

      .brand-wrap {
        display: flex;
        align-items: center;
        gap: 8px;
        min-width: 0;
        flex: 1;
      }

      .brand {
        display: flex;
        align-items: center;
        gap: 8px;
        min-width: 0;
        flex: 1;
      }

      .brand-logo {
        width: 34px;
        height: 34px;
        flex-shrink: 0;
      }

      .brand-logo svg {
        display: block;
        width: 100%;
        height: 100%;
      }

      .brand-name {
        font-size: var(--text-lg);
        font-weight: 700;
        letter-spacing: -0.025em;
        white-space: nowrap;
        color: var(--brand-live);
      }

      .brand-name-live {
        color: var(--brand-live);
      }

      .brand-name-guide {
        color: var(--brand-guide);
      }

      .brand-mark-tile {
        fill: var(--brand-tile);
      }

      .brand-mark-accent {
        fill: var(--brand-mark);
      }

      .brand-mark-check,
      .brand-mark-lines {
        fill: none;
        stroke-linecap: round;
        stroke-linejoin: round;
      }

      .brand-mark-check {
        stroke: var(--brand-mark);
        stroke-width: 3;
      }

      .brand-mark-lines {
        stroke: var(--brand-line);
        stroke-width: 2.3;
      }

      .brand-context {
        font-size: var(--text-xs);
        color: var(--text-muted);
        font-weight: 500;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        min-width: 0;
      }

      .top-actions {
        display: flex;
        align-items: center;
        gap: 6px;
        flex-shrink: 0;
      }

      /* ==============================================
         ICON BUTTONS
      =============================================== */
      .icon-btn {
        display: grid;
        place-items: center;
        width: 40px;
        height: 40px;
        border: 1px solid var(--border-hard);
        border-radius: var(--r-md);
        background: var(--surface);
        color: var(--text-2);
        cursor: pointer;
        flex-shrink: 0;
        transition: background 0.15s, color 0.15s, border-color 0.15s;
      }

      .icon-btn:hover  { background: var(--surface-2); }
      .icon-btn.active { border-color: var(--accent); color: var(--accent); background: var(--accent-dim); }
      .icon-btn svg    { width: 20px; height: 20px; flex-shrink: 0; }

      .icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

      .icon-btn.with-count {
        width: auto;
        min-width: 40px;
        padding: 0 10px;
        grid-auto-flow: column;
        gap: 6px;
      }

      .icon-btn-count {
        font-size: var(--text-xs);
        font-weight: 700;
        line-height: 1;
      }

      /* ==============================================
         SYNC BADGE
      =============================================== */
      .sync-badge {
        display: inline-flex;
        align-items: center;
        gap: 5px;
        padding: 4px 10px;
        border-radius: var(--r-full);
        font-size: var(--text-xs);
        font-weight: 600;
        border: 1px solid var(--border-hard);
        background: var(--surface);
        color: var(--text-muted);
        white-space: nowrap;
        transition: all 0.2s;
        cursor: default;
      }

      .sync-current { color: var(--success); background: var(--success-dim); border-color: rgba(61,212,160,0.3); }
      .sync-syncing { color: var(--accent);  background: var(--accent-dim);  border-color: rgba(77,166,255,0.3); }
      .sync-stale,
      .sync-offline { color: var(--warn);    background: var(--warn-dim);    border-color: rgba(245,200,74,0.3); }
      .sync-failed  { color: var(--error);   background: var(--error-dim);   border-color: rgba(255,96,96,0.3); }
      .sync-ready   { color: #c4a0ff; background: rgba(160,100,255,0.13); border-color: rgba(160,100,255,0.3); }

      /* ==============================================
         SCREENS & LAYOUT
      =============================================== */
      .screen { padding: 8px 10px 0; }
      .hidden { display: none !important; }

      /* ==============================================
         PATIENT PANEL
      =============================================== */
      .patient-panel {
        margin: 0 10px 6px;
        background: var(--surface);
        border: 1px solid var(--border-hard);
        border-radius: var(--r-lg);
        padding: 8px 12px;
      }

      .patient-grid {
        display: flex;
        align-items: center;
        gap: 10px;
        flex-wrap: wrap;
      }

      .patient-field {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        white-space: nowrap;
      }

      .patient-field label {
        font-size: var(--text-xs);
        color: var(--text-muted);
        font-weight: 500;
      }

      .patient-input {
        width: 76px;
        padding: 5px 8px;
        border: 1px solid var(--border-hard);
        border-radius: var(--r-sm);
        background: var(--bg);
        color: var(--text);
        font-size: var(--text-sm);
      }

      .switch {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        color: var(--text-muted);
        font-size: var(--text-sm);
        white-space: nowrap;
        cursor: pointer;
      }

      .btn {
        border: 1px solid var(--border-hard);
        background: var(--surface-2);
        color: var(--text);
        border-radius: var(--r-sm);
        padding: 7px 12px;
        cursor: pointer;
        font-size: var(--text-sm);
        white-space: nowrap;
        transition: background 0.15s;
      }
      .btn:hover { background: var(--surface-3); }

      .patient-reminder {
        margin: 0 10px 6px;
        background: var(--accent-dim);
        border: 1px solid rgba(77,166,255,0.3);
        color: var(--accent);
        border-radius: var(--r-md);
        padding: 8px 12px;
        font-size: var(--text-sm);
      }

      /* ==============================================
         STATUS / INFO TEXT
      =============================================== */
      .status-line {
        color: var(--text-muted);
        font-size: var(--text-sm);
        margin-bottom: 6px;
        line-height: 1.4;
      }

      /* ==============================================
         SECTION HEADERS (bookmarks, etc.)
      =============================================== */
      .bookmarks-strip { margin-bottom: 10px; }

      .section-head {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 8px;
        margin: 0 0 6px;
        padding: 0 2px;
      }

      .section-head h2 {
        margin: 0;
        font-size: var(--text-md);
        font-weight: 600;
        color: var(--text);
      }

      .ghost-btn {
        border: 1px solid var(--border-hard);
        background: transparent;
        color: var(--text-2);
        border-radius: var(--r-sm);
        padding: 5px 10px;
        min-height: 32px;
        cursor: pointer;
        font-size: var(--text-sm);
        transition: background 0.15s;
      }
      .ghost-btn:hover { background: var(--surface-2); }
      .ghost-btn.active {
        background: rgba(245,200,74,0.15);
        border-color: rgba(245,200,74,0.5);
        color: var(--warn);
      }

      /* ==============================================
         UPDATE INDICATOR DOT
      =============================================== */
      .update-dot {
        display: inline-block;
        width: 7px;
        height: 7px;
        border-radius: 50%;
        background: var(--accent);
        flex-shrink: 0;
        margin-right: 6px;
        margin-bottom: 1px;
        vertical-align: middle;
        box-shadow: 0 0 0 2px var(--accent-dim);
      }

      /* ==============================================
         CARD ITEMS — compact, accessible, touch-safe
      =============================================== */
      .cards-stage { padding: 0; }

      .card-item {
        width: 100%;
        display: flex;
        align-items: stretch;
        background: var(--surface);
        border: 1px solid var(--border);
        border-left: 3px solid var(--accent);
        border-radius: var(--r-md);
        margin-bottom: 4px;
        min-height: 50px;
        overflow: hidden;
        transition: background 0.1s;
        cursor: default;
      }

      .card-item-body {
        flex: 1;
        padding: 9px 8px 9px 11px;
        min-width: 0;
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
        display: flex;
        flex-direction: column;
        justify-content: center;
      }

      .card-item-body:active { background: var(--surface-2); }

      .card-title {
        font-size: var(--text-md);
        font-weight: 600;
        color: var(--text);
        line-height: 1.3;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      .card-meta {
        font-size: var(--text-xs);
        color: var(--text-muted);
        margin-top: 3px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      .card-item-actions {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: 4px 6px 4px 2px;
        gap: 2px;
        flex-shrink: 0;
      }

      .card-act-btn {
        display: grid;
        place-items: center;
        width: 32px;
        height: 26px;
        border: none;
        background: transparent;
        color: var(--text-muted);
        border-radius: 7px;
        cursor: pointer;
        font-size: 0.85rem;
        transition: color 0.12s, background 0.12s;
        flex-shrink: 0;
      }

      .card-act-btn:active { background: var(--surface-3); }
      .card-act-btn svg { width: 14px; height: 14px; }
      .card-act-btn.bookmarked { color: var(--warn); }

      /* ── Attachment list at bottom of card detail ── */
      .card-attachments-section {
        padding: 12px 16px 0;
        display: flex;
        flex-direction: column;
        gap: 6px;
      }
      .attach-file-row {
        display: flex;
        align-items: center;
        gap: 10px;
        padding: 10px 12px;
        background: var(--surface-2);
        border-radius: 10px;
        transition: background 0.12s;
      }
      .attach-file-row:active { background: var(--surface-3); }
      .attach-file-icon { font-size: 1.2rem; flex-shrink: 0; }
      .attach-file-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
      .attach-file-name {
        display: flex;
        align-items: baseline;
        min-width: 0;
        max-width: 100%;
        font-size: 0.88rem;
        font-weight: 500;
      }
      .attach-file-name-base {
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .attach-file-name-ext {
        flex-shrink: 0;
        white-space: nowrap;
      }
      .attach-file-meta { font-size: 0.75rem; color: var(--text-muted); }

      /* ── Inline image figures in card content ── */
      figure.lg-image { margin: 12px 0; text-align: center; }
      figure.lg-image img { max-width: 100%; border-radius: 8px; cursor: zoom-in; }

      /* ── Fullscreen image lightbox ── */
      #lg-lightbox {
        position: fixed;
        inset: 0;
        z-index: 9999;
        background: rgba(0,0,0,0.92);
        display: flex;
        align-items: center;
        justify-content: center;
        padding: calc(16px + env(safe-area-inset-top, 0px)) 16px 16px;
        animation: lg-fade-in 0.15s ease;
      }
      @keyframes lg-fade-in { from { opacity: 0; } to { opacity: 1; } }
      .lg-lightbox-img {
        max-width: 100%;
        max-height: 100%;
        border-radius: 8px;
        object-fit: contain;
        touch-action: pinch-zoom;
      }
      .lg-lightbox-close {
        position: fixed;
        top: calc(16px + env(safe-area-inset-top, 0px));
        right: 16px;
        width: 40px;
        height: 40px;
        border-radius: 50%;
        border: none;
        background: rgba(255,255,255,0.15);
        color: #fff;
        font-size: 1.2rem;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 10000;
      }
      .lg-lightbox-close:hover { background: rgba(255,255,255,0.25); }

      /* ── Fullscreen PDF viewer ── */
      #lg-pdf-modal {
        position: fixed;
        inset: 0;
        z-index: 9999;
        background: rgba(7,11,18,0.98);
        display: flex;
        flex-direction: column;
        animation: lg-fade-in 0.15s ease;
      }
      .lg-pdf-modal-header {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: calc(14px + env(safe-area-inset-top, 0px)) 16px 14px;
        border-bottom: 1px solid rgba(255,255,255,0.08);
        background: rgba(7,11,18,0.9);
        backdrop-filter: blur(10px);
      }
      #lg-pdf-modal .lg-lightbox-close {
        position: static;
        top: auto;
        right: auto;
        flex-shrink: 0;
        margin-left: auto;
      }
      .lg-pdf-modal-title {
        min-width: 0;
        flex: 1 1 auto;
        color: #fff;
        font-size: 0.95rem;
        font-weight: 600;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .lg-pdf-modal-body {
        flex: 1;
        overflow: auto;
        padding: 16px max(12px, env(safe-area-inset-right, 0px)) calc(16px + env(safe-area-inset-bottom, 0px)) max(12px, env(safe-area-inset-left, 0px));
        -webkit-overflow-scrolling: touch;
      }
      .lg-pdf-status {
        min-height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        text-align: center;
        color: rgba(255,255,255,0.82);
        font-size: 0.95rem;
        padding: 24px;
      }
      .lg-pdf-pages {
        display: flex;
        flex-direction: column;
        gap: 16px;
        align-items: center;
      }
      .lg-pdf-page {
        width: 100%;
        max-width: 960px;
      }
      .lg-pdf-page canvas {
        display: block;
        width: 100%;
        height: auto;
        background: #fff;
        border-radius: 12px;
        box-shadow: 0 20px 60px rgba(0,0,0,0.28);
      }
      .lg-pdf-page-label {
        margin: 0 0 8px;
        color: rgba(255,255,255,0.6);
        font-size: 0.75rem;
        text-transform: uppercase;
        letter-spacing: 0.06em;
      }

      /* ==============================================
         GROUP HEADERS — sticky, compact
      =============================================== */
      .group-block { margin-bottom: 2px; }
      .group-block + .group-block { margin-top: 10px; }

      .group-controls {
        display: flex;
        justify-content: flex-end;
        margin: 0 0 8px;
      }

      .group-control-btn {
        min-height: 36px;
      }

      .group-header {
        position: sticky;
        top: var(--topbar-h, 56px);
        z-index: 10;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 5px 4px 4px 2px;
        margin-bottom: 4px;
        background: var(--bg);
      }

      .group-block.group-collapsible .group-header {
        min-height: 52px;
        padding: 12px 8px 11px 6px;
        cursor: pointer;
        touch-action: manipulation;
      }

      .group-title {
        margin: 0;
        font-size: var(--text-xs);
        font-weight: 700;
        color: var(--text-muted);
        text-transform: uppercase;
        letter-spacing: 0.07em;
        line-height: 1;
      }

      .group-toggle-btn {
        border: none;
        background: transparent;
        color: var(--text-muted);
        pointer-events: none;
        padding: 3px 5px;
        border-radius: 6px;
        display: flex;
        align-items: center;
        gap: 4px;
        font-size: var(--text-xs);
        transition: color 0.12s;
        white-space: nowrap;
      }

      .group-toggle-btn svg {
        width: 11px;
        height: 11px;
        transition: transform 0.18s;
      }

      .group-status-badges {
        display: inline-flex;
        align-items: center;
        gap: 4px;
      }

      .group-status-badge {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 4px;
        height: 18px;
        min-width: 31px;
        padding: 0 7px 0 5px;
        border: 1px solid color-mix(in srgb, var(--border-hard) 72%, transparent);
        border-radius: var(--r-full);
        background: color-mix(in srgb, var(--surface) 82%, transparent);
        box-shadow: 0 1px 3px rgba(0,0,0,0.12);
        font-size: var(--text-xs);
        font-weight: 700;
        color: var(--text);
        line-height: 1;
      }

      .group-status-dot {
        width: 7px;
        height: 7px;
        border-radius: 50%;
        flex: 0 0 auto;
      }

      .group-status-dot.notify {
        background: var(--accent);
        box-shadow: 0 0 0 2px var(--accent-dim);
      }

      .group-status-dot.ack-dot {
        background: var(--warn);
      }

      .group-block.collapsed .group-toggle-btn svg { transform: rotate(-90deg); }
      .group-block.collapsed .group-cards { display: none; }

      /* ==============================================
         SEARCH INPUT (in bottom nav)
      =============================================== */
      .search-empty {
        color: var(--text-muted);
        font-size: var(--text-sm);
        text-align: center;
        padding: 28px 0;
      }

      .bottom-search {
        width: 100%;
        background: var(--surface);
        border: 1px solid var(--border-hard);
        border-radius: var(--r-xl);
        padding: 5px 5px 5px 14px;
        display: flex;
        align-items: center;
        gap: 6px;
      }

      .search-icon-btn {
        border: none;
        background: transparent;
        color: var(--text-muted);
        width: 36px;
        height: 36px;
        min-width: 36px;
        border-radius: var(--r-sm);
        display: grid;
        place-items: center;
        cursor: pointer;
        flex-shrink: 0;
      }
      .search-icon-btn svg { width: 18px; height: 18px; }

      .bottom-search-input {
        flex: 1;
        background: transparent;
        border: none;
        color: var(--text);
        font-size: max(16px, var(--text-base));
        padding: 6px 0;
        outline: none;
        width: auto;
      }
      .bottom-search-input::placeholder { color: var(--text-muted); }

      /* ==============================================
         BOTTOM NAV
      =============================================== */
      .bottom-nav {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 40;
        display: flex;
        justify-content: center;
        padding: 6px 10px calc(var(--safe-bottom) + 4px);
        background: linear-gradient(to top, var(--nav-bg) 55%, transparent);
        transform: translateY(calc(-1 * var(--keyboard-offset)));
        transition: transform 110ms ease-out;
        -webkit-user-select: none;
        user-select: none;
      }

      /* In standalone mode (home screen app) the browser chrome is gone,
         so the home-indicator safe area should blend with the page background
         rather than showing the nav overlay color as a phantom "control bar". */
      @media (display-mode: standalone) {
        .bottom-nav {
          background: linear-gradient(to top,
            transparent 0px,
            transparent env(safe-area-inset-bottom, 0px),
            var(--nav-bg) calc(env(safe-area-inset-bottom, 0px) + 44px),
            transparent 100%
          );
        }
      }

      .bottom-nav-inner {
        width: min(700px, 100%);
        background: var(--surface);
        border: 1px solid var(--border-hard);
        border-radius: var(--r-xl);
        display: flex;
        gap: 2px;
        padding: 5px;
        overflow-x: auto;
        justify-content: flex-start;
        scrollbar-width: none;
        box-shadow: 0 4px 24px rgba(0,0,0,0.45);
        -webkit-user-select: none;
        user-select: none;
      }

      .bottom-nav-inner::-webkit-scrollbar { display: none; }

      .nav-btn {
        border: none;
        background: transparent;
        color: var(--text-muted);
        border-radius: var(--r-md);
        padding: 6px 10px;
        min-width: 58px;
        min-height: 48px;
        cursor: pointer;
        display: inline-flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 3px;
        flex: 1 0 58px;
        transition: background 0.13s, color 0.13s;
        -webkit-tap-highlight-color: transparent;
        -webkit-user-select: none;
        user-select: none;
      }

      .nav-btn.active { background: var(--accent-dim); color: var(--accent); }
      .nav-btn svg    { width: 20px; height: 20px; }
      .nav-btn .label {
        font-size: var(--text-xs);
        font-weight: 500;
        line-height: 1;
        white-space: nowrap;
        -webkit-user-select: none;
        user-select: none;
      }
      .nav-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

      /* ==============================================
         ACKNOWLEDGEMENTS
      =============================================== */
      .detail-ack {
        margin: 0 0 10px;
        display: grid;
        gap: 8px;
      }

      .detail-ack .ghost-btn {
        border-color: var(--warn);
        color: var(--warn-text);
        background: rgba(245,200,74,0.15);
        flex-shrink: 0;
      }

      .ack-actions {
        display: flex;
        justify-content: flex-end;
      }

      .ack-list {
        display: grid;
        gap: 8px;
      }

      .ack-entry {
        background: rgba(245, 200, 74, 0.08);
        border: 1px solid rgba(245, 200, 74, 0.18);
        border-radius: 10px;
        padding: 10px;
      }

      .ack-entry-row {
        display: flex;
        align-items: center;
        gap: 8px;
        flex-wrap: wrap;
        margin-bottom: 6px;
      }

      .ack-entry-row .ghost-btn {
        margin-left: auto;
      }

      .ack-entry-summary {
        color: var(--text);
        font-weight: 700;
        line-height: 1.45;
      }

      .ack-entry-description {
        color: var(--text-2);
        line-height: 1.55;
        margin-top: 4px;
        white-space: pre-wrap;
      }

      /* ==============================================
         CARD DETAIL SCREEN
      =============================================== */
      .card-detail-header {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        gap: 8px;
        margin-bottom: 2px;
      }

      .card-detail-title {
        margin: 0;
        font-size: var(--text-xl);
        font-weight: 700;
        line-height: 1.25;
        color: var(--text);
        flex: 1;
        min-width: 0;
      }

      .card-detail-actions {
        display: flex;
        gap: 6px;
        flex-shrink: 0;
        padding-top: 3px;
      }

      /* ==============================================
         TEMPLATE RENDER (card content)
      =============================================== */
      .template-render {
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: var(--r-md);
        padding: 12px 14px;
        color: var(--text);
        line-height: 1.65;
        font-size: var(--text-base);
        font-family: var(--font-content);
      }

      #detailBody {
        font-size: var(--text-base);
      }

      .template-render,
      .template-render table,
      .template-render tbody,
      .template-render thead,
      .template-render tfoot {
        font-size: var(--text-base);
      }

      .template-render :where(
        p, li, span, div, td, th, a, strong, em, small, label, blockquote
      ) {
        font-size: inherit;
      }

      .template-render a { color: var(--link); }

      .template-render a:visited { color: var(--link-visited); }
      .template-render a:hover,
      .template-render a:active {
        color: var(--link-hover);
      }

      .template-render :where(table) {
        width: 100%;
        max-width: 100%;
      }

      [data-theme="light"] .template-render {
        background: #ffffff;
        color: #1a2535;
        border-color: #d4dfe8;
      }

      .template-render h1,
      .template-render h2,
      .template-render h3 { line-height: 1.3; margin-top: 1em; margin-bottom: 0.35em; }
      .template-render p  { margin: 0 0 0.7em; }
      .template-render ol,
      .template-render ul { margin: 0 0 0.7em; padding-left: 1.4em; }
      .template-render li + li { margin-top: 0.3em; }

      /* ==============================================
         STRUCTURED MEDICATION CARDS
      =============================================== */
      .med-card-render {
        --med-card-bg: color-mix(in srgb, var(--surface) 88%, transparent);
        --med-card-bg-2: color-mix(in srgb, var(--surface-2) 72%, transparent);
        --med-accent-soft: color-mix(in srgb, var(--accent) 16%, transparent);
        --med-danger-soft: color-mix(in srgb, var(--error) 13%, transparent);
        --med-warn-soft: color-mix(in srgb, var(--warn) 15%, transparent);
        display: grid;
        gap: 8px;
        width: 100%;
        max-width: 100%;
        min-width: 0;
        color: var(--text);
        font-family: var(--font-content);
      }

      .med-card-render *,
      .med-card-render *::before,
      .med-card-render *::after {
        min-width: 0;
      }

      .med-card-render > * {
        max-width: 100%;
      }

      .med-topbar {
        position: sticky;
        top: calc(env(safe-area-inset-top) + 58px);
        z-index: 2;
        margin: -2px -2px 0;
        padding: 8px;
        border: 1px solid var(--border);
        border-radius: var(--r-md);
        background: color-mix(in srgb, var(--surface) 92%, transparent);
        box-shadow: var(--shadow-soft);
        backdrop-filter: blur(14px);
      }

      .med-title-row {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 8px;
        min-width: 0;
      }

      .med-title-row > div:first-child {
        flex: 1 1 auto;
      }

      .med-title-row h1 {
        margin: 0;
        font-size: 1.28rem;
        line-height: 1.12;
        letter-spacing: 0;
        overflow-wrap: anywhere;
      }

      .med-subtitle {
        margin: 3px 0 0;
        color: var(--text-muted);
        font-size: 0.82rem;
        line-height: 1.25;
      }

      .med-status {
        flex: 0 1 auto;
        max-width: 42%;
        padding: 4px 7px;
        border: 1px solid var(--border);
        border-radius: 999px;
        background: var(--success-dim);
        font-size: 0.72rem;
        font-weight: 800;
        line-height: 1.1;
        text-align: center;
      }

      .med-quick-grid {
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 5px;
        margin-top: 8px;
      }

      .med-quick {
        min-width: 0;
        min-height: 52px;
        padding: 6px;
        border: 1px solid var(--border);
        border-radius: 10px;
        background: var(--med-card-bg);
      }

      .med-quick strong {
        display: block;
        color: var(--text-muted);
        font-size: 0.68rem;
        font-weight: 800;
        text-transform: uppercase;
        letter-spacing: 0.04em;
      }

      .med-quick span {
        display: block;
        margin-top: 2px;
        overflow-wrap: anywhere;
        font-size: 0.94rem;
        font-weight: 850;
        line-height: 1.12;
      }

      .med-tabs,
      .med-regime-switch {
        display: flex;
        gap: 5px;
        overflow-x: auto;
        padding: 2px 0;
        margin-top: 7px;
        scrollbar-width: none;
      }

      .med-tabs a,
      .med-regime-switch a {
        flex: 0 0 auto;
        color: var(--text);
        text-decoration: none;
        white-space: nowrap;
        padding: 6px 9px;
        border: 1px solid var(--border);
        border-radius: 999px;
        background: var(--med-card-bg);
        font-size: 0.8rem;
        font-weight: 750;
      }

      .med-regime-switch a:first-child {
        background: var(--med-accent-soft);
      }

      .med-prototype-notice,
      .med-age-missing {
        padding: 7px 8px;
        border: 1px dashed var(--border-hard);
        border-radius: 10px;
        background: color-mix(in srgb, var(--surface-2) 38%, transparent);
        color: var(--text-2);
        font-size: 0.84rem;
        line-height: 1.35;
      }

      .med-card,
      .med-patient-dose,
      .med-dose-panel {
        overflow: hidden;
        border: 1px solid var(--border);
        border-radius: var(--r-md);
        background: var(--med-card-bg);
        box-shadow: var(--shadow-soft);
      }

      .med-card h2,
      .med-patient-dose h3 {
        margin: 0;
        padding: 8px 10px;
        border-bottom: 1px solid var(--border);
        background: var(--med-card-bg-2);
        font-size: 1rem;
        line-height: 1.2;
        letter-spacing: 0;
      }

      .med-card-render .med-section-danger .med-content {
        background: var(--med-danger-soft);
      }

      .med-card-render .med-section-warn .med-content {
        background: var(--med-warn-soft);
      }

      .med-content {
        padding: 8px 10px;
        max-width: 100%;
        overflow-wrap: anywhere;
      }

      .med-content p {
        margin: 0 0 6px;
      }

      .med-content p:last-child {
        margin-bottom: 0;
      }

      .med-content > .med-section-table:first-child {
        margin-top: -8px;
      }

      .med-compact {
        margin: 0;
        padding-left: 18px;
      }

      .med-compact li {
        margin: 2px 0;
      }

      .med-note-block {
        margin: 8px 0 0;
        padding: 8px 10px;
        border: 1px solid var(--border);
        border-radius: 10px;
        background: var(--med-card-bg-2);
        color: var(--text-2);
        font-size: 0.86rem;
      }

      .med-note-block > strong {
        display: block;
        margin-bottom: 4px;
        color: var(--text);
        font-size: 0.78rem;
        text-transform: uppercase;
        letter-spacing: 0.04em;
      }

      .med-note-block.med-section-danger {
        border-color: color-mix(in srgb, #dc2626 35%, var(--border));
        background: color-mix(in srgb, #fee2e2 35%, var(--med-card-bg));
      }

      .med-note-block.med-section-warn {
        border-color: color-mix(in srgb, #d97706 35%, var(--border));
        background: color-mix(in srgb, #fef3c7 35%, var(--med-card-bg));
      }

      .med-section-title {
        margin: 10px 0 5px;
        color: var(--text-muted);
        font-size: 0.78rem;
        font-weight: 850;
        text-transform: uppercase;
        letter-spacing: 0.05em;
      }

      .med-section-title:first-child {
        margin-top: 0;
      }

      .med-patient-dose {
        margin-bottom: 8px;
        border-color: color-mix(in srgb, var(--accent) 38%, var(--border));
        background: var(--med-accent-soft);
      }

      .med-patient-dose h3 {
        background: color-mix(in srgb, var(--accent) 22%, transparent);
      }

      .med-patient-meta {
        display: flex;
        flex-wrap: wrap;
        gap: 5px;
        padding: 7px 8px 0;
      }

      .med-patient-meta span,
      .med-route-pill,
      .med-age-pill {
        padding: 3px 7px;
        border: 1px solid var(--border);
        border-radius: 999px;
        background: color-mix(in srgb, var(--surface) 48%, transparent);
        font-size: 0.76rem;
        font-weight: 800;
        line-height: 1.1;
        white-space: normal;
      }

      .med-route-pill {
        background: color-mix(in srgb, var(--accent) 26%, transparent);
      }

      .med-age-pill {
        background: var(--success-dim);
      }

      .med-scroll {
        overflow-x: auto;
        overflow-y: hidden;
        width: 100%;
        max-width: 100%;
        -webkit-overflow-scrolling: touch;
      }

      .med-patient-table,
      .med-dose-table,
      .med-weight-table {
        width: 100%;
        border-collapse: collapse;
        font-size: 0.86rem;
        table-layout: fixed;
      }

      .med-patient-table th,
      .med-patient-table td,
      .med-dose-table th,
      .med-dose-table td,
      .med-weight-table th,
      .med-weight-table td {
        padding: 6px 7px;
        border-top: 1px solid var(--border);
        text-align: left;
        vertical-align: top;
        overflow-wrap: anywhere;
        word-break: normal;
      }

      .med-patient-table th,
      .med-weight-table th {
        color: var(--text-muted);
        font-size: 0.7rem;
        font-weight: 850;
        text-transform: uppercase;
        letter-spacing: 0.04em;
      }

      .med-patient-table td strong {
        display: block;
        font-size: 0.98rem;
        line-height: 1.16;
      }

      .med-patient-table small {
        display: block;
        margin-top: 2px;
        color: var(--text-muted);
        line-height: 1.25;
      }

      .med-calc-details,
      .med-lookup,
      .med-section-details {
        border-top: 1px solid var(--border);
        background: color-mix(in srgb, var(--surface-2) 30%, transparent);
      }

      .med-dose-actions {
        display: flex;
        flex-wrap: wrap;
        gap: 5px;
        padding: 7px;
        border-top: 1px solid var(--border);
        background: color-mix(in srgb, var(--surface) 48%, transparent);
      }

      .med-action-btn {
        flex: 1 1 130px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-height: 32px;
        padding: 6px 9px;
        border: 1px solid var(--border);
        border-radius: 999px;
        background: color-mix(in srgb, var(--surface-2) 48%, transparent);
        color: var(--text);
        cursor: pointer;
        font-size: 0.8rem;
        font-weight: 800;
        line-height: 1.1;
        text-decoration: none;
        text-align: center;
        white-space: normal;
      }

      .med-calc-details summary,
      .med-lookup summary,
      .med-section-details summary {
        cursor: pointer;
        padding: 8px 10px;
        font-size: 0.86rem;
        font-weight: 800;
      }

      .med-section-details {
        margin: 8px -10px -8px;
        border-top-color: color-mix(in srgb, var(--accent) 35%, var(--border));
        background: var(--med-accent-soft);
      }

      .med-section-details summary {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
        color: var(--text);
        list-style: none;
        overflow-wrap: anywhere;
      }

      .med-section-details summary::-webkit-details-marker {
        display: none;
      }

      .med-section-details summary::before {
        content: "";
        flex: 0 0 auto;
        width: 9px;
        height: 9px;
        border-right: 2px solid var(--accent);
        border-bottom: 2px solid var(--accent);
        transform: rotate(-45deg);
        transition: transform 0.16s ease;
      }

      .med-section-details[open] summary::before {
        transform: rotate(45deg);
      }

      .med-section-details summary span {
        flex: 1 1 auto;
        min-width: 0;
      }

      .med-section-details summary small {
        flex: 0 0 auto;
        padding: 3px 7px;
        border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--border));
        border-radius: 999px;
        background: color-mix(in srgb, var(--surface) 55%, transparent);
        color: var(--text-2);
        font-size: 0.72rem;
        font-weight: 850;
        line-height: 1.1;
      }

      .med-section-details[open] summary small {
        color: var(--accent);
      }

      .med-section-details .med-content {
        border-top: 1px solid color-mix(in srgb, var(--accent) 24%, var(--border));
        background: color-mix(in srgb, var(--surface) 54%, transparent);
      }

      .med-link-row {
        display: grid;
        gap: 6px;
      }

      .med-link-row a {
        display: block;
        padding: 8px;
        border: 1px solid var(--border);
        border-radius: 10px;
        background: color-mix(in srgb, var(--surface-2) 34%, transparent);
        color: var(--text);
        font-weight: 800;
        line-height: 1.2;
        text-decoration: none;
        overflow-wrap: anywhere;
      }

      .med-dose-panel {
        margin-bottom: 8px;
        background: color-mix(in srgb, var(--surface) 82%, transparent);
      }

      .med-dose-head {
        display: flex;
        justify-content: space-between;
        gap: 6px;
        padding: 7px 8px;
        border-bottom: 1px solid var(--border);
        background: var(--med-accent-soft);
        font-weight: 850;
        line-height: 1.22;
        min-width: 0;
        overflow-wrap: anywhere;
      }

      .med-dose-head > span:first-child {
        flex: 1 1 auto;
      }

      .med-dose-head > span:last-child {
        flex: 0 1 auto;
        display: flex;
        flex-wrap: wrap;
        justify-content: flex-end;
        gap: 4px;
      }

      .med-dose-table th {
        width: 38%;
        color: var(--text-muted);
        font-weight: 800;
      }

      .med-dose-table tr:first-child th,
      .med-dose-table tr:first-child td {
        border-top: 0;
      }

      .med-weight-table {
        min-width: min(520px, calc(100vw - 36px));
      }

      .med-weight-table td {
        white-space: nowrap;
      }

      .med-modal-toggle {
        position: absolute;
        opacity: 0;
        pointer-events: none;
      }

      .med-modal {
        position: fixed;
        left: 0;
        right: 0;
        top: 0;
        bottom: var(--bottom-nav-clearance);
        z-index: 120;
        display: none;
        align-items: flex-end;
        background: color-mix(in srgb, #000 42%, transparent);
        padding: 0 8px;
      }

      .med-modal-toggle:checked + .med-modal {
        display: flex;
      }

      .med-modal-box {
        width: min(100%, 560px);
        max-width: 100%;
        max-height: min(84dvh, calc(100dvh - var(--bottom-nav-clearance) - env(safe-area-inset-top, 0px) - 12px));
        margin: 0 auto;
        overflow: auto;
        border: 1px solid var(--border);
        border-radius: 18px 18px 0 0;
        background: color-mix(in srgb, var(--surface) 96%, transparent);
        box-shadow: 0 -14px 42px rgba(0, 0, 0, 0.28);
      }

      .med-modal-head {
        position: sticky;
        top: 0;
        z-index: 1;
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        gap: 8px;
        padding: 10px;
        border-bottom: 1px solid var(--border);
        background: color-mix(in srgb, var(--surface) 92%, transparent);
        backdrop-filter: blur(12px);
        min-width: 0;
      }

      .med-modal-head > div {
        flex: 1 1 auto;
      }

      .med-modal-head h3 {
        margin: 0;
        font-size: 1rem;
        line-height: 1.2;
      }

      .med-modal-close {
        flex: 0 0 auto;
        padding: 5px 10px;
        border: 1px solid var(--border);
        border-radius: 999px;
        background: color-mix(in srgb, var(--surface-2) 55%, transparent);
        cursor: pointer;
        font-size: 0.82rem;
        font-weight: 850;
      }

      .med-calc-grid {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 6px;
      }

      .med-calc-card {
        min-width: 0;
        padding: 8px;
        border: 1px solid var(--border);
        border-radius: 11px;
        background: color-mix(in srgb, var(--surface-2) 34%, transparent);
      }

      .med-calc-card strong,
      .med-calc-card small,
      .med-calc-card em {
        display: block;
      }

      .med-calc-card strong {
        color: var(--text-muted);
        font-size: 0.74rem;
        font-weight: 850;
        line-height: 1.15;
      }

      .med-calc-card span {
        display: block;
        margin-top: 2px;
        font-size: 1.02rem;
        font-weight: 900;
        line-height: 1.12;
      }

      .med-calc-card small,
      .med-calc-card em {
        margin-top: 2px;
        color: var(--text-2);
        font-size: 0.78rem;
        font-style: normal;
        line-height: 1.25;
      }

      .med-note {
        margin: 8px 0 0;
        padding: 7px 8px;
        border: 1px solid var(--border);
        border-radius: 10px;
        background: var(--med-accent-soft);
        color: var(--text-2);
        font-size: 0.84rem;
      }

      @media (max-width: 390px) {
        .med-topbar {
          top: calc(env(safe-area-inset-top) + 50px);
        }

        .med-quick-grid {
          grid-template-columns: repeat(2, minmax(0, 1fr));
        }

        .med-title-row {
          align-items: flex-start;
        }

        .med-status {
          max-width: 38%;
          font-size: 0.68rem;
        }

        .med-dose-head {
          display: grid;
        }

        .med-dose-head > span:last-child {
          justify-content: flex-start;
        }

        .med-dose-table th {
          width: 42%;
        }

        .med-calc-grid {
          grid-template-columns: 1fr;
        }
      }

      pre {
        background: var(--surface);
        border: 1px solid var(--border-hard);
        border-radius: var(--r-md);
        padding: 12px;
        white-space: pre-wrap;
        margin: 0;
        font-size: var(--text-sm);
        color: var(--text-2);
        font-family: "SFMono-Regular", "Consolas", monospace;
      }

      /* ==============================================
         SHEET OVERLAY — slides up from bottom
      =============================================== */
      .sheet-overlay {
        position: fixed;
        inset: 0;
        z-index: 100;
        display: flex;
        flex-direction: column;
        justify-content: flex-end;
      }

      .sheet-backdrop {
        position: absolute;
        inset: 0;
        background: rgba(0,0,0,0.6);
        backdrop-filter: blur(3px);
        -webkit-backdrop-filter: blur(3px);
      }

      .sheet {
        position: relative;
        z-index: 1;
        background: var(--sheet-bg);
        border: 1px solid var(--border-hard);
        border-bottom: none;
        border-radius: var(--r-xl) var(--r-xl) 0 0;
        max-height: 86dvh;
        overflow-y: auto;
        overscroll-behavior: contain;
        padding-bottom: calc(var(--safe-bottom) + 18px);
      }

      .sheet-handle {
        width: 36px;
        height: 4px;
        border-radius: 2px;
        background: var(--border-hard);
        margin: 12px auto 0;
        flex-shrink: 0;
      }

      .sheet-header {
        position: sticky;
        top: 0;
        z-index: 2;
        background: var(--sheet-bg);
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 14px 16px 12px;
        border-bottom: 1px solid var(--border);
        -webkit-user-select: none;
        user-select: none;
      }

      .sheet-title {
        margin: 0;
        font-size: var(--text-lg);
        font-weight: 700;
        color: var(--text);
      }

      .sheet-close {
        border: 1px solid var(--border-hard);
        background: var(--surface-2);
        color: var(--text-muted);
        border-radius: var(--r-md);
        width: 36px;
        height: 36px;
        display: grid;
        place-items: center;
        cursor: pointer;
        flex-shrink: 0;
        touch-action: manipulation;
        -webkit-user-select: none;
        user-select: none;
      }
      .sheet-close svg { width: 18px; height: 18px; }

      .sheet-body { padding: 10px 16px; }

      /* ==============================================
         MENU ITEMS (inside hamburger sheet)
      =============================================== */
      .menu-item {
        display: flex;
        align-items: center;
        gap: 14px;
        width: 100%;
        padding: 13px 4px;
        border: none;
        background: transparent;
        color: var(--text);
        cursor: pointer;
        font-size: var(--text-md);
        border-bottom: 1px solid var(--border);
        text-align: left;
        transition: color 0.12s;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
        -webkit-user-select: none;
        user-select: none;
      }
      .menu-item:last-child { border-bottom: none; }
      .menu-item:active { color: var(--accent); }
      .menu-item.danger:active { color: var(--error); }

      .menu-icon {
        width: 40px;
        height: 40px;
        border-radius: var(--r-md);
        background: var(--surface-2);
        display: grid;
        place-items: center;
        flex-shrink: 0;
      }
      .menu-item.danger .menu-icon { background: var(--error-dim); }
      .menu-icon svg { width: 20px; height: 20px; }

      .menu-item-text { flex: 1; min-width: 0; }
      .menu-item-label { font-weight: 600; line-height: 1.2; }

      .menu-sync-row {
        padding: 10px 4px 12px;
        border-bottom: 1px solid var(--border);
        display: flex;
        align-items: center;
        gap: 10px;
      }

      .menu-sync-label {
        font-size: var(--text-xs);
        color: var(--text-muted);
      }

      .menu-sync-value {
        margin-left: auto;
        display: flex;
        flex-direction: column;
        align-items: flex-end;
        gap: 3px;
        min-width: 0;
      }

      .menu-sync-version {
        color: var(--text-muted);
        font-size: var(--text-xs);
        line-height: 1.1;
        white-space: nowrap;
      }

      /* ==============================================
         SETTINGS (inside settings sheet)
      =============================================== */
      .settings-section { margin-bottom: 22px; }

      .settings-section-title {
        font-size: var(--text-xs);
        font-weight: 700;
        color: var(--text-muted);
        text-transform: uppercase;
        letter-spacing: 0.07em;
        margin: 0 0 10px;
      }

      .settings-row {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        padding: 11px 0;
        border-bottom: 1px solid var(--border);
      }
      .settings-row:last-child { border-bottom: none; }
      .settings-row-label { font-size: var(--text-base); font-weight: 500; color: var(--text); }
      .settings-row-desc  { font-size: var(--text-xs); color: var(--text-muted); margin-top: 2px; }

      .seg-control {
        display: flex;
        gap: 2px;
        background: var(--surface-2);
        border-radius: var(--r-md);
        padding: 3px;
        flex-shrink: 0;
      }

      .seg-btn {
        border: none;
        background: transparent;
        color: var(--text-muted);
        border-radius: 9px;
        padding: 5px 11px;
        font-size: var(--text-sm);
        font-weight: 500;
        cursor: pointer;
        transition: all 0.14s;
        white-space: nowrap;
      }
      .seg-btn.active {
        background: var(--surface);
        color: var(--text);
        box-shadow: 0 1px 4px rgba(0,0,0,0.25);
      }

      .font-option {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 2px;
        border: 1px solid var(--border-hard);
        background: var(--surface-2);
        border-radius: var(--r-md);
        padding: 8px 12px;
        cursor: pointer;
        font-size: var(--text-sm);
        font-weight: 500;
        color: var(--text-2);
        transition: all 0.14s;
        white-space: nowrap;
      }
      .font-option.active {
        border-color: var(--accent);
        background: var(--accent-dim);
        color: var(--accent);
      }
      .font-option-preview {
        font-size: var(--text-xs);
        color: var(--text-muted);
        font-weight: 400;
        margin-top: 1px;
      }

      .font-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
      }

      .toggle-switch {
        position: relative;
        width: 44px;
        height: 26px;
        flex-shrink: 0;
      }
      .toggle-switch input {
        opacity: 0;
        width: 0;
        height: 0;
        position: absolute;
      }
      .toggle-track {
        position: absolute;
        inset: 0;
        background: var(--border-hard);
        border-radius: var(--r-full);
        cursor: pointer;
        transition: background 0.2s;
      }
      .toggle-track::after {
        content: "";
        position: absolute;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background: #fff;
        top: 3px;
        left: 3px;
        transition: transform 0.2s;
        box-shadow: 0 1px 3px rgba(0,0,0,0.3);
      }
      .toggle-switch input:checked + .toggle-track { background: var(--accent); }
      .toggle-switch input:checked + .toggle-track::after { transform: translateX(18px); }

      /* ==============================================
         VERSION NOTES (inside sheet)
      =============================================== */
      .version-item {
        padding: 12px 0;
        border-bottom: 1px solid var(--border);
      }
      .version-item:last-child { border-bottom: none; }

      .version-row {
        display: flex;
        align-items: center;
        gap: 8px;
        margin-bottom: 6px;
        flex-wrap: wrap;
      }

      .version-badge {
        background: var(--accent-dim);
        color: var(--accent);
        border-radius: var(--r-full);
        padding: 2px 9px;
        font-size: var(--text-xs);
        font-weight: 700;
      }

      .version-date {
        color: var(--text-muted);
        font-size: var(--text-xs);
      }

      .version-change-badge {
        margin-left: auto;
        border-radius: var(--r-full);
        padding: 2px 9px;
        font-size: var(--text-xs);
        font-weight: 600;
        background: var(--warn-dim);
        color: var(--warn);
      }
      .version-change-badge.ack { background: var(--error-dim); color: var(--error); }

      .version-notes-text {
        color: var(--text-2);
        font-size: var(--text-sm);
        line-height: 1.55;
        white-space: pre-wrap;
      }

      .version-notes-summary {
        color: var(--text-1);
        font-size: var(--text-sm);
        font-weight: 700;
        line-height: 1.45;
        margin-bottom: 4px;
        white-space: pre-wrap;
      }

      .version-empty {
        color: var(--text-muted);
        font-size: var(--text-sm);
        text-align: center;
        padding: 20px 0;
      }

      input[type="number"],
      select {
        width: 100%;
        border: 1px solid var(--border-hard);
        border-radius: var(--r-sm);
        background: var(--bg);
        color: var(--text);
        padding: 8px 10px;
        font-size: var(--text-sm);
      }

      /* ==============================================
         COLLECTION TILES (home)
      =============================================== */
      .collection-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
        gap: 8px;
        margin-bottom: 14px;
      }

      .collection-tile {
        border: 1px solid var(--border-hard);
        border-radius: var(--r-lg);
        padding: 12px;
        background: var(--surface);
        cursor: pointer;
        text-align: left;
        min-height: 44px;
        transition: background 0.14s, border-color 0.14s;
      }
      .collection-tile:active { background: var(--surface-2); }
      .collection-tile.active { border-color: var(--accent); background: var(--accent-dim); }
      .collection-tile-name  { font-weight: 600; font-size: var(--text-base); }
      .collection-tile-count { color: var(--text-muted); font-size: var(--text-xs); margin-top: 4px; }

      /* ==============================================
         PEDIATRIC AGES SCREEN
      =============================================== */
      .ages-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
        padding: 0 0 8px;
      }

      .age-tile {
        background: var(--surface);
        border: 1px solid var(--border-hard);
        border-left: 3px solid var(--accent);
        border-radius: var(--r-md);
        padding: 12px 14px;
        cursor: pointer;
        text-align: left;
        min-height: 64px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        gap: 3px;
        transition: background 0.12s, border-color 0.12s;
        -webkit-tap-highlight-color: transparent;
      }

      .age-tile:active { background: var(--surface-2); border-color: var(--accent); }

      .age-tile-label  { font-size: var(--text-md); font-weight: 700; color: var(--text); }
      .age-tile-weight { font-size: var(--text-sm); color: var(--text-muted); }

      /* Age detail screen */
      .age-detail-header {
        position: sticky;
        top: calc(var(--topbar-h, 56px) + 6px);
        z-index: 12;
        background: var(--surface);
        border: 1px solid var(--border);
        border-left: 3px solid var(--accent);
        border-radius: var(--r-md);
        padding: 14px 16px;
        margin-bottom: 12px;
        box-shadow: 0 8px 20px rgba(0,0,0,0.08);
      }

      .age-detail-title { margin: 0 0 4px; font-size: var(--text-xl); font-weight: 700; }
      .age-detail-sub   { color: var(--text-muted); font-size: var(--text-sm); }

      .age-section { margin-bottom: 14px; }

      .age-section-title {
        font-size: var(--text-xs);
        font-weight: 700;
        color: var(--text-muted);
        text-transform: uppercase;
        letter-spacing: 0.07em;
        margin: 0 0 8px;
        padding-bottom: 5px;
        border-bottom: 1px solid var(--border);
      }

      .age-value-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
        gap: 6px;
      }

      .age-value-row {
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: var(--r-sm);
        padding: 8px 10px;
        display: flex;
        flex-direction: column;
        gap: 2px;
      }

      .age-value-name  { font-size: var(--text-xs); color: var(--text-muted); font-weight: 500; }
      .age-value-val   { font-size: var(--text-md); font-weight: 700; color: var(--text); }
      .age-value-unit  { font-size: var(--text-xs); color: var(--text-muted); }

      .age-dose-row {
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: var(--r-sm);
        padding: 9px 12px;
        margin-bottom: 5px;
        display: flex;
        justify-content: space-between;
        align-items: baseline;
        gap: 8px;
      }

      .age-dose-drug   { font-size: var(--text-base); font-weight: 600; color: var(--text); }
      .age-dose-amount { font-size: var(--text-md); font-weight: 700; color: var(--accent); white-space: nowrap; }
      .age-dose-note   { font-size: var(--text-xs); color: var(--text-muted); margin-top: 2px; }
      .age-dose-left   { flex: 1; min-width: 0; }

      .age-quick-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
        gap: 6px;
      }

      .calculator-pill {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 2px 8px;
        border-radius: 999px;
        background: var(--accent-dim);
        color: var(--accent-strong);
        font-size: var(--text-xs);
        font-weight: 700;
        margin-right: 8px;
      }

      .calculator-grid {
        display: grid;
        gap: 10px;
      }

      .calculator-detail-head,
      .calculator-group,
      .calculator-footer,
      .calculator-score-card {
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: var(--r-md);
      }

      .calculator-detail-head {
        padding: 14px 16px;
        margin-bottom: 10px;
      }

      .calculator-heading-row {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        gap: 8px;
      }

      .calculator-title {
        margin: 0;
        font-size: var(--text-xl);
      }

      .calculator-subtitle {
        margin: 8px 0 0;
        color: var(--text-muted);
        font-size: var(--text-sm);
      }

      .calculator-progress-bar {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
        margin: 0 0 10px;
        flex-wrap: wrap;
      }

      .calculator-progress-copy {
        color: var(--text-muted);
        font-size: var(--text-sm);
      }

      .calculator-progress-actions {
        display: flex;
        align-items: center;
        gap: 8px;
      }

      .calculator-session-strip {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
        margin: 0 0 10px;
      }

      .calculator-birth-row {
        display: flex;
        gap: 8px;
        justify-content: flex-start;
        margin: 0 0 8px;
        flex-wrap: wrap;
      }

      .calculator-session-chip {
        border: 1px solid var(--border-hard);
        border-radius: var(--r-md);
        background: var(--surface-2);
        color: var(--text);
        padding: 10px 12px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;
        font: inherit;
      }

      .calculator-session-chip span {
        font-size: var(--text-sm);
        color: var(--text-muted);
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        gap: 2px;
      }

      .calculator-session-chip small {
        font-size: var(--text-xs);
        color: var(--text);
      }

      .calculator-session-chip strong {
        font-size: var(--text-base);
      }

      .calculator-session-chip.active {
        border-color: var(--accent);
        background: var(--accent-dim);
      }

      .calculator-group {
        padding: 12px;
        margin-bottom: 10px;
      }

      .calculator-group-head h3 {
        margin: 0;
        font-size: var(--text-lg);
      }

      .calculator-group-head p {
        margin: 6px 0 0;
        color: var(--text-muted);
        font-size: var(--text-sm);
      }

      .calculator-field {
        margin-top: 12px;
        padding-top: 12px;
        border-top: 1px solid var(--border);
      }

      .calculator-field:first-of-type {
        border-top: 0;
        padding-top: 0;
      }

      .calculator-field-head {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
      }

      .calculator-field-label {
        font-weight: 700;
        font-size: var(--text-base);
      }

      .calculator-field-desc,
      .calculator-missing,
      .calculator-required {
        color: var(--text-muted);
        font-size: var(--text-sm);
      }

      .calculator-missing {
        margin-top: 8px;
        color: var(--warn);
      }

      .calculator-choice-grid {
        display: grid;
        grid-template-columns: 1fr;
        gap: 8px;
        margin-top: 10px;
      }

      .calculator-choice {
        width: 100%;
        min-height: 54px;
        border: 1px solid var(--border-hard);
        border-radius: var(--r-md);
        background: var(--surface-2);
        color: var(--text);
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
        padding: 12px 14px;
        font: inherit;
        text-align: left;
      }

      .calculator-choice.active {
        border-color: var(--accent);
        background: var(--accent-dim);
      }

      .calculator-select,
      .calculator-number,
      .calculator-note {
        width: 100%;
        margin-top: 10px;
        border: 1px solid var(--border-hard);
        border-radius: var(--r-md);
        background: var(--surface-2);
        color: var(--text);
        padding: 12px 14px;
        font: inherit;
      }

      .calculator-note {
        min-height: 88px;
        resize: vertical;
      }

      .calculator-footer {
        position: sticky;
        bottom: calc(var(--safe-bottom) + 72px);
        padding: 8px;
        display: grid;
        grid-template-columns: 1fr;
        gap: 0;
        margin: 10px 0 18px;
      }

      .calculator-score-card {
        padding: 8px 10px;
        display: flex;
        flex-direction: column;
        gap: 2px;
        min-width: 0;
      }

      .calculator-score-card span,
      .calculator-score-card small {
        color: var(--text-muted);
        font-size: var(--text-xs);
      }

      .calculator-score-card strong {
        font-size: var(--text-base);
      }

      .calculator-score-inline {
        color: var(--text);
        font-size: var(--text-xs);
        line-height: 1.35;
        font-weight: 500;
        white-space: normal;
      }

      .age-quick-card {
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: var(--r-sm);
        padding: 9px 10px;
        display: flex;
        flex-direction: column;
        gap: 3px;
        min-width: 0;
      }

      .age-quick-label {
        font-size: var(--text-xs);
        color: var(--text-muted);
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.05em;
      }

      .age-quick-value {
        font-size: var(--text-md);
        color: var(--text);
        font-weight: 700;
        line-height: 1.35;
      }

      .age-quick-note {
        font-size: var(--text-xs);
        color: var(--text-muted);
        line-height: 1.4;
      }

      .placeholder-notice {
        background: var(--warn-dim);
        border: 1px solid rgba(245,200,74,0.3);
        border-radius: var(--r-md);
        padding: 10px 14px;
        color: var(--warn);
        font-size: var(--text-sm);
        margin-bottom: 12px;
      }

      /* ==============================================
         RESPONSIVE
      =============================================== */
      @media (max-width: 480px) {
        :root { --bottom-nav-clearance: calc(66px + var(--safe-bottom)); }
        .screen   { padding: 6px 8px 0; }
        .topbar   { padding: calc(7px + env(safe-area-inset-top, 0px)) 10px 7px; }
        #syncBadge { display: none; }
        .menu-sync-badge { display: inline-flex; }
        .bottom-nav { padding: 5px 8px calc(var(--safe-bottom) + 3px); }
        .bottom-nav-inner { gap: 1px; padding: 4px; }
        .nav-btn  { min-width: 50px; min-height: 46px; padding: 5px 8px; }
        .nav-btn svg { width: 18px; height: 18px; }
        .calculator-footer { bottom: calc(var(--safe-bottom) + 68px); }
        .age-detail-header { top: calc(var(--topbar-h, 56px) + 4px); }
      }

      @media (min-width: 600px) {
        .nav-btn { min-width: 70px; }
      }
