/* ==========================================================================
   Al-Ayada — Design System (inline, single-file build)
   ========================================================================== */
@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@600;700;800&family=Tajawal:wght@400;500;700&family=IBM+Plex+Mono:wght@400;500;600&display=swap');

:root {
  --ink: #1F2A24; --ink-soft: #4B584F;
  --canvas: #F5F1E8; --panel: #FFFFFF;
  --primary: #2F5D50; --primary-d: #223F37;
  --accent: #C97064; --bronze: #8E6C46; --line: #E4DDCC;
  --st-waiting: #C9A227; --st-inclinic: #2F5D50; --st-done: #6B8F71; --st-cancelled: #A94442;
  --radius: 14px; --radius-sm: 8px; --shadow: 0 4px 20px rgba(31, 42, 36, .07);
}
* { box-sizing: border-box; }
html, body { margin:0; padding:0; background:var(--canvas); color:var(--ink); font-family:'Tajawal',sans-serif; direction:rtl; overflow-x:hidden; max-width:100vw; }
h1,h2,h3,h4,.display { font-family:'Cairo',sans-serif; font-weight:800; color:var(--primary-d); margin:0 0 .3em; }
h1 { font-size:clamp(1.15rem,4vw,1.6rem); }
h2 { font-size:clamp(1.1rem,3.5vw,1.4rem); }
h3 { font-size:clamp(1rem,3vw,1.15rem); }
h4 { font-size:1rem; }
.mono,.num { font-family:'IBM Plex Mono',monospace; }
a { color:var(--primary); }
button,input,select,textarea { font-family:inherit; font-size:inherit; }
:focus-visible { outline:3px solid var(--accent); outline-offset:2px; }
@media (prefers-reduced-motion: reduce){ *{animation:none!important;transition:none!important;} }

.app-shell { display:grid; grid-template-columns:240px 1fr; min-height:100vh; }
.sidebar { background:var(--primary-d); color:#F5F1E8; padding:28px 20px; display:flex; flex-direction:column; gap:28px; }
.brand { font-family:'Cairo',sans-serif; font-weight:800; font-size:clamp(1.05rem,4vw,1.4rem); letter-spacing:.02em; }
.brand span { color:var(--accent); }
.nav-list { list-style:none; margin:0; padding:0; display:flex; flex-direction:column; gap:4px; }
.nav-list button { width:100%; text-align:right; background:none; border:none; color:#E9E4D6; padding:12px 14px; border-radius:var(--radius-sm); cursor:pointer; font-size:.98rem; transition:background .15s; }
.nav-list button:hover,.nav-list button.active { background:rgba(245,241,232,.12); }
.user-card { margin-top:auto; padding-top:16px; border-top:1px solid rgba(245,241,232,.15); font-size:.85rem; color:#C9C2AD; }
.user-card strong { display:block; color:#F5F1E8; font-size:.95rem; margin-bottom:2px; }
.user-card button { margin-top:10px; background:none; border:1px solid rgba(245,241,232,.3); color:#F5F1E8; padding:6px 12px; border-radius:var(--radius-sm); cursor:pointer; }

/* ---- Sidebar search slot ----
   On pages that lead with a search box (finding/opening a case),
   the brand mark and the signed-in user's name are swapped out for
   a search field + "add new case" button, right inside the sidebar
   bar. This keeps the search reachable at all times while scrolling
   and frees up the vertical space at the top of the page for
   future content. Hidden by default; toggled on by showSection()
   via body.topbar-search-mode, and never while the mobile nav
   drawer is open (the drawer needs the normal brand/user card). */
.branch-switch { width:100%; margin-top:6px; padding:6px 8px; border-radius:var(--radius-sm);
  border:1px solid rgba(255,255,255,.35); background:rgba(255,255,255,.12); color:#F5F1E8; font-size:.82rem; }
.branch-switch option { color:#1B1B1B; }
.sidebar-search-slot { display:none; }
body.topbar-search-mode:not(.drawer-open) #sidebar-brand,
body.topbar-search-mode:not(.drawer-open) #sidebar-user-card { display:none; }
body.topbar-search-mode:not(.drawer-open) .sidebar-search-slot { display:flex; flex:1; min-width:0; }
.topbar-search-group { display:none; flex:1; min-width:0; gap:8px; align-items:center; }
.topbar-search-group .search-field-wrap { flex:1; min-width:0; position:relative; margin-bottom:0; }
.topbar-search-group .search-field-wrap input { width:100%; padding:9px 12px; font-size:16px; border-radius:var(--radius-sm); border:1px solid var(--line); background:#FBF9F4; color:var(--ink); }
.topbar-search-group .search-field-wrap input:focus { border-color:var(--primary); background:#fff; }
.topbar-search-group .search-dropdown { position:absolute; top:calc(100% + 4px); inset-inline:0; border:1px solid var(--line); border-radius:var(--radius-sm); background:#fff; box-shadow:var(--shadow); z-index:60; overflow:hidden; }
.topbar-search-group .btn { flex-shrink:0; white-space:nowrap; }

/* ---- Main search bar (desktop) ----
   On wide screens the sidebar stays a normal, full-height nav column
   (brand mark + user card always visible) — the search box for
   search-led pages moves here instead, at the very top of the page
   content, right under the page title. reparentSearchGroup() in the
   script below moves the actual box (#topbar-search-sec-patients or
   #topbar-search-doc-chart) in and out of this container depending
   on viewport width; on narrow/mobile screens this stays empty and
   hidden and the same box lives inside .sidebar-search-slot instead. */
.main-search-bar { display:none; }
.main-search-bar.is-active {
  display:flex; align-items:center; gap:12px; flex-wrap:wrap;
  background:var(--panel); border:1px solid var(--line); border-radius:var(--radius);
  box-shadow:var(--shadow); padding:16px 20px; margin-bottom:24px;
}
.main-search-bar .search-field-wrap { flex:1; min-width:220px; position:relative; margin-bottom:0; }
.main-search-bar .search-field-wrap input {
  width:100%; padding:12px 16px; font-size:1rem; border-radius:var(--radius-sm);
  border:1px solid var(--line); background:var(--canvas); color:var(--ink);
}
.main-search-bar .search-field-wrap input:focus { border-color:var(--primary); background:#fff; }
.main-search-bar .search-dropdown {
  position:absolute; top:calc(100% + 6px); inset-inline:0; border:1px solid var(--line);
  border-radius:var(--radius-sm); background:#fff; box-shadow:var(--shadow); z-index:60; overflow:hidden;
}
.main-search-bar .btn { flex-shrink:0; white-space:nowrap; }
@media (max-width:760px) { .main-search-bar.is-active { display:none; } }

.main { padding:32px 40px; overflow-y:auto; }
.main-header { display:flex; justify-content:space-between; align-items:baseline; margin-bottom:24px; flex-wrap:wrap; gap:12px; }
.main-header .date { color:var(--ink-soft); font-size:.95rem; }

/* Hamburger drawer toggle + overlay — mobile only (see media query
   below for the open/closed drawer behavior). Hidden on desktop
   since the sidebar is already fully visible there. */
.sidebar-toggle-btn { display:none; }
.sidebar-overlay { display:none; }

/* ---------------- Bottom nav (mobile only) ---------------- */
.bottom-nav { display:none; }

/* ---------------- Mobile layout ---------------- */
@media (max-width:760px) {
  /* Turn the shell into a fixed-height flex column: the green topbar
     (.sidebar) becomes a non-scrolling first child, and only .main
     scrolls beneath it. This keeps the topbar permanently visible
     no matter what — more robust across mobile browsers than
     position:sticky, which can misbehave depending on ancestor
     overflow/height quirks. */
  .app-shell {
    grid-template-columns:1fr;
    display:flex; flex-direction:column;
    height:100vh; height:100dvh; /* 100dvh accounts for mobile browser chrome; 100vh is the fallback */
    overflow:hidden;
  }

  .sidebar {
    /* flex-start (instead of space-between) keeps the toggle button and
       brand name grouped together at the leading edge; the user card is
       then pushed to the opposite edge on its own via margin-inline-start:auto
       below, so it doesn't pull the brand away from the toggle button. */
    flex-direction:row; align-items:center; justify-content:flex-start;
    padding:12px 16px; gap:0; flex-shrink:0; position:relative; z-index:20;
  }
  .sidebar .nav-list { display:none; } /* replaced by bottom-nav on mobile; reappears inside the drawer below */
  .sidebar .brand { font-size:1.15rem; }
  .user-card {
    margin-top:0; padding-top:0; border-top:none;
    margin-inline-start:auto; /* pushes the user card to the far edge, away from the toggle+brand group */
    display:flex; align-items:center; gap:10px; font-size:.72rem;
  }
  .user-card strong { display:inline; font-size:.85rem; }
  .user-card span { display:none; } /* branch id hidden to save space on mobile */
  .user-card button { display:none; } /* removed from the compact top bar — now lives only in the drawer (see body.drawer-open below) */

  .main {
    padding:18px 16px calc(90px + env(safe-area-inset-bottom));
    flex:1 1 auto; min-height:0; /* min-height:0 is required so a flex child can actually scroll instead of stretching the row */
    overflow-y:auto; -webkit-overflow-scrolling:touch;
  }
  .main-header h1 { font-size:1.25rem; }

  .grid-2 { grid-template-columns:1fr; }
  .field-row { grid-template-columns:1fr; }
  .panel { padding:18px; border-radius:12px; }
  .topbar-search-group .btn-sm { padding:6px 10px; font-size:.78rem; }

  /* Label + value side by side even on narrow screens — the label
     column is capped so a long value (or a long custom-field label)
     never crowds the other one out; text that doesn't fit simply
     wraps within its own column, staying next to its label instead of
     the row dropping to a whole separate full-width line. Keeps each
     field to roughly one compact row instead of the two lines a fully
     stacked label-above-value layout costs. */
  .info-table, .info-table tbody { display:block; width:auto; }
  .info-table tr {
    display:grid; grid-template-columns:minmax(min-content,42%) 1fr;
    column-gap:10px; align-items:baseline;
    padding:8px 0; border-bottom:1px solid var(--line);
  }
  .info-table tr:last-child { border-bottom:none; }
  .info-table th { display:block; width:auto; padding:0; border-bottom:none; font-size:.76rem; white-space:normal; }
  .info-table td { display:block; width:auto; padding:0; border-bottom:none; min-width:0; }

  .login-wrap { padding:16px; }
  .login-card { width:100%; max-width:400px; padding:32px 24px; }

  .bottom-nav {
    display:flex; position:fixed; bottom:0; inset-inline:0; z-index:30;
    background:var(--panel); border-top:1px solid var(--line);
    box-shadow:0 -6px 20px rgba(31,42,36,.10);
    padding:6px 6px calc(6px + env(safe-area-inset-bottom));
  }
  .bottom-nav button {
    flex:1; display:flex; flex-direction:column; align-items:center; gap:2px;
    background:none; border:none; padding:8px 4px; border-radius:var(--radius-sm);
    color:var(--ink-soft); font-size:.68rem; font-weight:600; cursor:pointer;
    white-space:nowrap; overflow:hidden; text-overflow:ellipsis;
  }
  .bottom-nav button .bn-icon { font-size:1.3rem; line-height:1; }
  .bottom-nav button.active { color:var(--primary); background:#EAF3EF; }

  /* ---- Hamburger nav drawer (mobile only) ----
     Same <aside class="sidebar"> element used for the desktop
     sidebar: while closed it's the compact sticky top bar (rules
     above); toggling body.drawer-open (in JS) expands it into a
     full-height slide-out panel with the nav list + logout, without
     touching the bottom-nav tab bar at all. */
  .sidebar-toggle-btn {
    display:inline-flex; align-items:center; justify-content:center;
    width:38px; height:38px; margin-inline-end:10px; border-radius:var(--radius-sm);
    background:rgba(245,241,232,.14); border:none; color:#F5F1E8;
    font-size:1.2rem; line-height:1; cursor:pointer;
  }
  .sidebar-overlay {
    display:none; position:fixed; inset:0; background:rgba(15,20,17,.45); z-index:40;
  }
  body.drawer-open .sidebar-overlay { display:block; }
  body.drawer-open .sidebar {
    position:fixed; inset-inline-start:0; top:0; bottom:0; width:78vw; max-width:300px;
    flex-direction:column; align-items:stretch; justify-content:flex-start;
    padding:22px 18px; gap:22px; z-index:50; overflow-y:auto;
    box-shadow:0 0 30px rgba(0,0,0,.28);
  }
  body.drawer-open .sidebar .nav-list { display:flex; }
  body.drawer-open .sidebar .user-card {
    margin-top:auto; padding-top:16px; border-top:1px solid rgba(245,241,232,.15);
    display:block; font-size:.85rem;
  }
  body.drawer-open .sidebar .user-card strong { display:block; font-size:.95rem; }
  body.drawer-open .sidebar .user-card span { display:block; }
  body.drawer-open .sidebar .user-card button { display:block; margin-top:10px; padding:6px 12px; font-size:.8rem; }

  /* Full-width, easy-to-tap primary actions (small/ghost/danger buttons stay their natural size) */
  .btn-primary:not(.btn-sm), .btn-accent:not(.btn-sm) { width:100%; justify-content:center; padding:13px 18px; }

  /* Tighter stat cards and near-full-height modals on small screens */
  .stats-grid { gap:10px; }
  .stat-card { padding:14px 16px; }
  .stat-card .stat-value { font-size:1.2rem; }
  .stat-card--primary .stat-value { font-size:1.35rem; }
  .modal-box { padding:20px; max-height:85vh; }
}

@media (max-width:380px) {
  .login-card { padding:26px 18px; }
  .panel { padding:14px; }
  .main { padding:14px 12px calc(90px + env(safe-area-inset-bottom)); }
  .bottom-nav button { font-size:.62rem; padding:7px 2px; }
  .bottom-nav button .bn-icon { font-size:1.15rem; }
  .topbar-search-group { gap:5px; }
  .topbar-search-group .search-field-wrap input { padding:8px 8px; }
  .topbar-search-group .btn-sm { padding:6px 8px; font-size:.72rem; }
}

.panel { background:var(--panel); border:1px solid var(--line); border-radius:var(--radius); padding:22px; box-shadow:var(--shadow); }

.panel + .panel { margin-top:20px; }
.grid-2 { display:grid; grid-template-columns:1.1fr .9fr; gap:20px; align-items:start; }

@media (max-width:980px){ .grid-2{grid-template-columns:1fr;} }

.btn { display:inline-flex; align-items:center; gap:6px; border:none; border-radius:var(--radius-sm); padding:10px 18px; font-weight:700; cursor:pointer; font-size:.93rem; transition:transform .1s,opacity .15s; }
.btn:active { transform:scale(.98); }
.btn-primary { background:var(--primary); color:#fff; }
.btn-primary:hover { background:var(--primary-d); }
.btn-accent { background:var(--accent); color:#fff; }
.btn-accent:hover { opacity:.9; }
.btn-ghost { background:transparent; color:var(--primary); border:1px solid var(--line); }
.btn-ghost:hover { background:#F0EBDD; }
.btn-sm { padding:6px 12px; font-size:.82rem; }
.btn-danger { background:var(--st-cancelled); color:#fff; }

.field { display:flex; flex-direction:column; gap:6px; margin-bottom:14px; }
.field label { font-size:.85rem; color:var(--ink-soft); font-weight:500; }
.field input,.field select,.field textarea { width:100%; border:1px solid var(--line); border-radius:var(--radius-sm); padding:10px 12px; background:#FBF9F4; color:var(--ink); font-size:16px; }
.field input:focus,.field select:focus,.field textarea:focus { border-color:var(--primary); background:#fff; }
.field-row { display:grid; grid-template-columns:1fr 1fr; gap:14px; }

/* ---- Custom field suggestion dropdown ----
   Used by the reusable "custom fields" widget (patients, invoices,
   visits — see customFieldsAddRow() in the script) to offer
   previously-typed labels/values as the user types, since these
   fields are almost always the same handful of recurring entries. */
.custom-field-input-wrap { position:relative; }
.cf-suggest-dropdown {
  position:absolute; top:100%; inset-inline:0; margin-top:4px; z-index:60;
  border:1px solid var(--line); border-radius:var(--radius-sm); background:#fff;
  box-shadow:var(--shadow); max-height:220px; overflow-y:auto;
}
.cf-suggest-item { padding:9px 12px; font-size:.88rem; cursor:pointer; border-bottom:1px solid var(--line); }
.cf-suggest-item:last-child { border-bottom:none; }
.cf-suggest-item:hover { background:var(--canvas); }
/* "تثبيت" checkbox on each custom-field row: pins that field's label +
   current value so it's pre-filled and ready next time the same form
   opens (see readPinnedCustomFields()/populatePinnedFieldRows()). */
.cf-pin-wrap { display:flex; align-items:center; gap:5px; font-size:.78rem; color:var(--ink-soft); white-space:nowrap; padding-bottom:11px; cursor:pointer; user-select:none; }
.cf-pin-wrap input { width:auto; margin:0; accent-color:var(--primary); }
@media (max-width:480px) { .cf-pin-wrap span { display:none; } }

.queue-list { list-style:none; margin:0; padding:0; display:flex; flex-direction:column; gap:10px; }
.queue-item { display:flex; justify-content:space-between; align-items:center; gap:12px; flex-wrap:wrap; background:#FBF9F4; border-radius:var(--radius-sm); padding:14px 16px; border-inline-start:5px solid var(--st-waiting); }
.queue-no { flex:0 0 auto; display:inline-flex; align-items:center; justify-content:center; min-width:34px; height:34px;
  border-radius:50%; background:var(--primary); color:#fff; font-weight:800; font-size:1rem; }
.queue-item.queue-soon { background:#FFF8E1; border-inline-start-color:var(--accent); }
.queue-item.queue-soon .queue-no { background:var(--accent); }
.queue-sent { color:var(--st-done); font-weight:800; }
.queue-item[data-status="in-clinic"] { border-inline-start-color:var(--st-inclinic); }
.queue-item[data-status="done"] { border-inline-start-color:var(--st-done); opacity:.7; }
.queue-item[data-status="cancelled"] { border-inline-start-color:var(--st-cancelled); opacity:.55; text-decoration:line-through; }

.badge { display:inline-block; padding:3px 10px; border-radius:999px; font-size:.78rem; font-weight:700; color:#fff; }
.badge-waiting { background:var(--st-waiting); }
.badge-in-clinic { background:var(--st-inclinic); }
.badge-done { background:var(--st-done); }
.badge-cancelled { background:var(--st-cancelled); }
.timeline { list-style:none; margin:0; padding:0; border-inline-start:2px solid var(--line); }
.timeline li { position:relative; padding:4px 22px 20px 0; }
.timeline li::before { content:""; position:absolute; right:-7px; top:8px; width:12px; height:12px; border-radius:50%; background:var(--primary); border:2px solid var(--panel); }
.timeline .t-date { font-size:.78rem; color:var(--bronze); font-weight:600; }
.timeline .t-body { margin-top:3px; line-height:1.6; word-break:break-word; }

/* ---- Generic label/value data table (patient header, clinical profile) ---- */
.info-table { width:100%; border-collapse:collapse; }
.info-table th, .info-table td { text-align:right; padding:10px 8px; border-bottom:1px solid var(--line); font-size:.92rem; line-height:1.5; vertical-align:top; word-break:break-word; }
.info-table th { width:130px; color:var(--ink-soft); font-weight:600; white-space:nowrap; }
.info-table tr:last-child th, .info-table tr:last-child td { border-bottom:none; }
.info-table .name-cell { font-family:'Cairo',sans-serif; font-weight:800; font-size:1.1rem; color:var(--primary-d); }

/* ---- Case-file header strip ---- */
.patient-strip { display:flex; flex-wrap:wrap; align-items:center; gap:10px 22px; }
.patient-strip .ps-name {
  font-family:'Cairo',sans-serif; font-weight:800; font-size:1.25rem; color:var(--primary-d);
  padding-inline-end:22px; border-inline-end:2px solid var(--line); line-height:1.3;
}
.patient-strip .ps-item { display:flex; align-items:baseline; gap:6px; font-size:.92rem; min-width:0; }
.patient-strip .ps-item > span { color:var(--ink-soft); font-size:.8rem; font-weight:600; white-space:nowrap; }
.patient-strip .ps-item > b { font-weight:700; word-break:break-word; }
#c-extra-fields { display:contents; }
#c-profile-fields {
  flex:1 1 100%; display:grid; grid-template-columns:repeat(auto-fill,minmax(105px,1fr));
  gap:10px 16px; margin-top:12px; padding-top:12px; border-top:1px dashed var(--line);
}
#c-profile-fields:empty { display:none; }
#c-profile-fields .ps-item { gap:7px; font-size:.95rem; }
#c-profile-fields .ps-item > span { font-size:.82rem; }
@media (min-width:700px) { #c-profile-fields { grid-template-columns:repeat(auto-fill,minmax(140px,1fr)); } }
.ps-edit-btn { flex:1 1 100%; justify-content:center; margin-top:12px; }
.ps-attach-btn { flex-shrink:0; gap:4px; }
.ps-attach-count { display:inline-flex; align-items:center; justify-content:center; min-width:19px; height:19px;
  padding:0 5px; border-radius:999px; background:rgba(255,255,255,.28); font-size:.75rem; font-weight:800; }
.ps-attach-count:empty { display:none; }
.ps-attach-count.pending { background:var(--accent); }
@media (max-width:620px) {
  .patient-strip .ps-name { width:100%; border-inline-end:none; padding-inline-end:0;
    border-bottom:1px solid var(--line); padding-bottom:8px; }
}

/* ---- البيانات الأولية grid (label -> value cards) ---- */
.metrics-hint { width:100%; margin:0; color:var(--ink-soft); font-size:.8rem; }
.cf-grid { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:6px; }
@media (min-width:700px) { .cf-grid { grid-template-columns:repeat(auto-fill,minmax(210px,1fr)); gap:8px; } }
.cf-grid .custom-field-row {
  display:flex !important; flex-wrap:nowrap; align-items:center; gap:3px !important; min-width:0;
  background:#FBF9F4; border:1px solid var(--line); border-radius:var(--radius-sm); padding:4px; margin:0;
}
.cf-grid .custom-field-row .field { margin-bottom:0; gap:0; min-width:0; }
.cf-grid .custom-field-row .field label { display:none; }
.cf-grid .cf-label, .cf-grid .cf-value { padding:5px 4px; font-size:.85rem; background:#fff; text-align:center; }
.cf-grid .custom-field-input-wrap:first-of-type { flex:1 1 50%; min-width:0; }
.cf-grid .custom-field-input-wrap:nth-of-type(2) { flex:1 1 50%; min-width:0; }
.cf-grid .cf-label { font-weight:600; }
.cf-grid .cf-value { font-weight:700; }
.cf-grid .cf-pin-wrap { padding-bottom:0; flex:0 0 auto; gap:0; font-size:.7rem; }
.cf-grid .cf-pin-wrap span { display:none; }
.cf-grid .cf-pin-wrap input { width:13px; height:13px; }
.cf-grid .cf-remove { flex:0 0 auto; padding:0 3px; font-size:.75rem; border:none; background:transparent; color:var(--st-cancelled); }
.cf-grid:empty { display:none; }
/* Roomy one-per-row layout inside the edit modal (phones), two columns on wide screens */
#cp-cf-list { grid-template-columns:1fr; gap:8px; }
@media (min-width:700px) { #cp-cf-list { grid-template-columns:repeat(2,minmax(0,1fr)); } }
#cp-cf-list .custom-field-row { gap:6px !important; padding:6px; }
#cp-cf-list .custom-field-input-wrap:first-of-type { flex:0 0 34%; }
#cp-cf-list .custom-field-input-wrap:nth-of-type(2) { flex:1 1 auto; }
#cp-cf-list .cf-label, #cp-cf-list .cf-value { padding:10px 8px; font-size:16px; }
#cp-cf-list .cf-value { text-align:start; }
#cp-cf-list .cf-pin-wrap input { width:17px; height:17px; }
#cp-cf-list .cf-remove { padding:0 6px; font-size:1rem; }
/* Per-معطى colouring */
.cf-style-btn { flex:0 0 auto; width:24px; height:24px; border-radius:6px; border:1px solid var(--line);
  background:#fff; cursor:pointer; font-weight:800; font-size:.8rem; line-height:1; padding:0; }
.style-preview { border:1px solid var(--line); border-radius:var(--radius-sm); padding:12px; text-align:center;
  font-weight:800; font-size:1.05rem; margin-bottom:14px; background:#fff; }
.style-color-row { display:flex; align-items:center; gap:10px; margin-bottom:12px; }
.style-color-row input[type="color"] { width:52px; height:36px; padding:0; border:1px solid var(--line);
  border-radius:var(--radius-sm); background:#fff; cursor:pointer; }
.style-color-row label { flex:1; font-size:.9rem; }

/* ---- Visit-timeline toolbar: sort arrow + Excel-style filter button ---- */
.timeline-head { display:flex; justify-content:space-between; align-items:center; gap:10px; margin-bottom:10px; }
.timeline-head h3 { margin:0; }
.timeline-tools { display:flex; gap:6px; }
.tl-icon-btn { display:inline-flex; align-items:center; justify-content:center; gap:2px;
  min-width:32px; height:30px; padding:0 7px; cursor:pointer; font-size:.9rem; font-weight:700;
  color:var(--ink-soft); background:#FBF9F4; border:1px solid var(--line); border-radius:var(--radius-sm); }
.tl-icon-btn:hover { background:#F0EBDD; color:var(--ink); }
.tl-icon-btn.active { background:var(--primary); border-color:var(--primary); color:#fff; }
.tl-caret { font-size:.65rem; line-height:1; }
.timeline-filter { border:1px solid var(--line); border-radius:var(--radius-sm); background:#FBF9F4; padding:12px; margin-bottom:12px; }
.tl-summary { margin:0 0 10px; font-size:.8rem; color:var(--ink-soft); }
.tl-summary:empty { display:none; }

/* Clinical profile summary block sitting above the visit timeline */
.clinical-profile-box { margin-bottom:16px; }
.clinical-profile-box .empty-state { padding:16px 10px; }

.login-wrap { min-height:100vh; display:flex; align-items:center; justify-content:center; background:radial-gradient(circle at 30% 20%, #35695B 0%, var(--primary-d) 55%, #16241F 100%); }
.login-card { background:var(--panel); border-radius:18px; padding:40px 36px; width:380px; box-shadow:0 20px 60px rgba(0,0,0,.35); }
.login-brand { text-align:center; margin-bottom:22px; }
.login-brand .display { font-size:clamp(1.5rem,8vw,1.9rem); }
.login-brand .tag { color:var(--ink-soft); font-size:.9rem; }
/* ---- Patient attachments (prescription / lab-result uploads) ----
   Deliberately built with flexbox + a padding-top square trick
   instead of CSS Grid/aspect-ratio: those are modern properties that
   some embedded/older WebViews render inconsistently (cards stacking
   on top of each other instead of wrapping into a row). Flexbox wrap
   has been reliably supported for years across every browser this
   app is likely to run in. */
.attachments-grid { display:flex; flex-wrap:wrap; gap:12px; }
.attachment-card { position:relative; flex:0 0 calc(33.333% - 8px); max-width:calc(33.333% - 8px); border:1px solid var(--line); border-radius:var(--radius-sm); overflow:hidden; background:var(--canvas); }
.attachment-thumb-wrap { position:relative; width:100%; padding-top:100%; background:#fff; }
.attachment-thumb { position:absolute; inset:0; width:100%; height:100%; object-fit:cover; display:block; }
.attachment-thumb-pdf { position:absolute; inset:0; display:flex; align-items:center; justify-content:center; font-size:2.1rem; color:var(--accent); }
.attachment-meta { padding:6px 8px 8px; }
.attachment-name { font-size:.76rem; font-weight:600; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.attachment-sub { font-size:.68rem; color:var(--ink-soft); margin-top:2px; }
.attachment-delete { position:absolute; top:5px; left:5px; width:22px; height:22px; border-radius:50%; border:none; background:rgba(31,42,36,.6); color:#fff; cursor:pointer; font-size:.72rem; line-height:1; display:flex; align-items:center; justify-content:center; z-index:2; }
.attachment-delete:hover { background:var(--st-cancelled); }
/* Image thumbnails open the in-app viewer (below) instead of a plain
   link, so they're wrapped in a <button> that's reset to look and
   behave exactly like the old clickable thumbnail did. */
.attachment-thumb-btn { display:block; width:100%; padding:0; margin:0; border:none; background:none; cursor:pointer; text-align:inherit; font:inherit; color:inherit; }
.attachment-thumb-btn:hover .attachment-thumb { filter:brightness(.92); }
@media (max-width: 480px) {
  .attachment-card { flex:0 0 calc(50% - 6px); max-width:calc(50% - 6px); }
}

/* ---- Attachments nav card (lives on the exam/كشف page) ----
   The exam page only shows a compact summary + button here; the
   actual upload UI (camera, file picker, grid) lives on its own
   page so the exam page stays uncluttered — see #doc-view-attachments. */
.attachments-nav-card { display:flex; align-items:center; justify-content:space-between; gap:14px; flex-wrap:wrap; }
.attachments-nav-info { display:flex; align-items:center; gap:12px; min-width:0; }
.attachments-nav-icon { font-size:1.6rem; flex-shrink:0; }
.attachments-nav-info h3 { white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }

/* ---- Local (device-stored, not-yet-synced) attachment state ----
   Files are saved to the device the instant they're picked, then
   uploaded to the cloud in the background — this styles that
   "queued" / "failed" state on its card until it syncs. */
.attachment-card.is-local { border-color:var(--st-waiting); }
.attachment-card.is-local.status-error { border-color:var(--st-cancelled); }
.attachment-status-badge { position:absolute; bottom:6px; right:6px; left:6px; z-index:2; background:rgba(31,42,36,.75); color:#fff; font-size:.66rem; font-weight:700; padding:3px 6px; border-radius:6px; text-align:center; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.attachment-status-badge.attachment-status-error { background:var(--st-cancelled); }
.attachment-retry-btn { margin-top:6px; width:100%; justify-content:center; }

.error-msg { color:var(--st-cancelled); font-size:.85rem; min-height:1.2em; margin-top:4px; }

.empty-state { text-align:center; color:var(--ink-soft); padding:28px 10px; }
.empty-state .icon { font-size:1.8rem; margin-bottom:6px; }

/* ---- Reserved-today appointments (secretary reminder cards) ---- */
.appt-card { display:flex; justify-content:space-between; align-items:center; gap:12px; flex-wrap:wrap; background:#FBF9F4; border-radius:var(--radius-sm); padding:14px 16px; border-inline-start:5px solid var(--st-waiting); }
.appt-card[data-confirmed="true"] { border-inline-start-color:var(--st-done); opacity:.7; }
.appt-contact { display:flex; align-items:center; gap:8px; }
.appt-contact-btn { display:inline-flex; align-items:center; justify-content:center; width:38px; height:38px; border-radius:50%; border:none; cursor:pointer; font-size:1.05rem; text-decoration:none; line-height:1; transition:transform .12s, opacity .15s; }
.appt-contact-btn:hover { transform:scale(1.08); opacity:.92; }
.appt-contact-btn.call { background:var(--primary); color:#fff; }
.appt-contact-btn.wa { background:#25D366; color:#fff; }
.appt-contact-none { font-size:.78rem; color:var(--ink-soft); }
.field-error { display:block; color:var(--st-cancelled); font-size:.78rem; margin-top:5px; font-weight:600; }

/* ---------------- Stat cards (dashboard KPIs) ---------------- */
/* Reusable anywhere a quick-glance metric is needed, not just finance. */
.stats-grid { display:grid; grid-template-columns:repeat(auto-fit,minmax(150px,1fr)); gap:14px; margin-bottom:20px; }
.stat-card { position:relative; background:var(--panel); border:1px solid var(--line); border-radius:var(--radius); padding:16px 18px; box-shadow:var(--shadow); overflow:hidden; }
.stat-card::before { content:""; position:absolute; inset-inline-start:0; top:0; bottom:0; width:4px; background:var(--bronze); }
.stat-card .stat-icon { display:block; font-size:1.2rem; margin-bottom:8px; }
.stat-card .stat-label { font-size:.78rem; color:var(--ink-soft); font-weight:600; margin-bottom:4px; }
.stat-card .stat-value { display:block; width:100%; border:none; background:transparent; padding:0; font-size:1.4rem; font-weight:800; color:var(--primary-d); font-family:'IBM Plex Mono',monospace; pointer-events:none; }
.stat-card--primary { background:linear-gradient(135deg,var(--primary) 0%,var(--primary-d) 100%); border-color:var(--primary-d); }
.stat-card--primary::before { display:none; }
.stat-card--primary .stat-label { color:rgba(245,241,232,.78); }
.stat-card--primary .stat-value { color:#fff; font-size:1.55rem; }

/* ---------------- Quick actions ---------------- */
/* Anything beyond the at-a-glance data lives behind one of these,
   opened as a focused modal — keeps the page from turning into a
   "سلطة" of every form shown at once. */
.finance-actions { display:flex; flex-wrap:wrap; gap:12px; margin:22px 0 4px; }

/* ---------------- Modal (generic, reusable overlay dialog) ---------------- */
.modal-overlay { display:none; position:fixed; inset:0; background:rgba(31,42,36,.55); z-index:100; align-items:center; justify-content:center; padding:20px; }

/* Floating action button (e.g. "extra invoice" shortcut on the case-file
   page). Sits above the bottom-nav on mobile and above the page content
   on desktop, always reachable regardless of scroll position. */
.fab {
  position:fixed; inset-inline-end:20px; bottom:calc(90px + env(safe-area-inset-bottom));
  width:56px; height:56px; border-radius:50%; border:none;
  background:var(--accent); color:#fff; box-shadow:0 6px 18px rgba(0,0,0,.25);
  display:flex; align-items:center; justify-content:center; cursor:pointer;
  z-index:15; transition:transform .15s ease, box-shadow .15s ease;
}
.fab:hover { transform:translateY(-2px); box-shadow:0 10px 22px rgba(0,0,0,.3); }
.fab:active { transform:translateY(0); }
.fab-icon { font-size:1.5rem; line-height:1; }
@media (min-width:761px) { .fab { bottom:28px; } }
.modal-box { background:var(--panel); border-radius:var(--radius); box-shadow:0 20px 60px rgba(0,0,0,.35); width:100%; max-width:540px; max-height:88vh; overflow-y:auto; padding:26px; }
.modal-box-header { display:flex; justify-content:space-between; align-items:center; gap:12px; margin-bottom:18px; padding-bottom:14px; border-bottom:1px solid var(--line); }
.modal-box-header h3 { margin:0; }
.modal-close-btn { flex-shrink:0; background:none; border:none; font-size:1.2rem; line-height:1; color:var(--ink-soft); cursor:pointer; padding:6px 10px; border-radius:var(--radius-sm); }
.modal-close-btn:hover { background:#F0EBDD; color:var(--ink); }

/* ---------------- Attachment image viewer (full-screen lightbox) ----------------
   Lets staff page through every photo attached to a patient (prescriptions,
   lab results...) without leaving the app and opening a new browser tab per
   image. Reuses the generic .modal-overlay plumbing (open/close, Escape,
   click-outside-to-close) already wired for every other modal — see
   openModal()/closeModal() and the .modal-overlay listeners in the script. */
.image-viewer-overlay { z-index:120; background:rgba(15,18,16,.92); flex-direction:column; gap:14px; }
.image-viewer-topbar { width:100%; max-width:960px; display:flex; align-items:center; justify-content:space-between; gap:12px; color:#F5F1E8; }
.image-viewer-counter { font-family:'IBM Plex Mono',monospace; font-size:.85rem; color:#E9E4D6; background:rgba(245,241,232,.12); padding:5px 12px; border-radius:999px; }
.image-viewer-overlay .modal-close-btn { color:#F5F1E8; background:rgba(245,241,232,.12); }
.image-viewer-overlay .modal-close-btn:hover { background:rgba(245,241,232,.22); color:#fff; }
.image-viewer-stage { position:relative; width:100%; max-width:960px; flex:1; min-height:0; display:flex; align-items:center; justify-content:center; touch-action:pan-y; overflow:hidden; }
.image-viewer-img { max-width:100%; max-height:78vh; object-fit:contain; border-radius:10px; box-shadow:0 20px 60px rgba(0,0,0,.5); background:#111; opacity:0; transition:opacity .18s ease, transform .12s ease; transform-origin:center center; cursor:zoom-in; touch-action:none; user-select:none; -webkit-user-drag:none; }
.image-viewer-img.is-loaded { opacity:1; }
.image-viewer-img.is-zoomed { cursor:grab; }
.image-viewer-img.is-zoomed.is-dragging { cursor:grabbing; }
.image-viewer-img.is-dragging, .image-viewer-img.is-pinching { transition:opacity .18s ease; } /* no transform easing while actively gesturing — keeps drag/pinch 1:1 with the finger/cursor */
.image-viewer-nav { position:absolute; top:50%; transform:translateY(-50%); width:44px; height:44px; border-radius:50%; border:none; background:rgba(245,241,232,.14); color:#fff; font-size:1.5rem; line-height:1; cursor:pointer; display:flex; align-items:center; justify-content:center; transition:background .15s ease, transform .15s ease; z-index:2; }
.image-viewer-nav:hover { background:rgba(245,241,232,.28); }
.image-viewer-nav:active { transform:translateY(-50%) scale(.94); }
/* Physical (not RTL-logical) left/right placement: on this RTL page, "next"
   reads visually leftward and "previous" reads visually rightward — see the
   matching keyboard/swipe mapping in imageViewerStep() usage in the script. */
.image-viewer-prev { right:8px; }
.image-viewer-next { left:8px; }
.image-viewer-nav[hidden] { display:none; }
/* ---- Zoom / rotate / open-original toolbar ----
   A pill-shaped control bar under the image. Buttons reuse the same
   translucent-on-dark treatment as the nav arrows above for visual
   consistency within the lightbox. */
.image-viewer-toolbar { display:flex; align-items:center; justify-content:center; gap:6px; background:rgba(245,241,232,.10); padding:6px 10px; border-radius:999px; flex-wrap:wrap; }
.image-viewer-toolbar button, .image-viewer-toolbar a { width:36px; height:36px; border-radius:50%; border:none; background:rgba(245,241,232,.14); color:#fff; font-size:1.05rem; line-height:1; cursor:pointer; display:flex; align-items:center; justify-content:center; text-decoration:none; transition:background .15s ease, transform .15s ease, opacity .15s ease; }
.image-viewer-toolbar button:hover, .image-viewer-toolbar a:hover { background:rgba(245,241,232,.28); }
.image-viewer-toolbar button:active, .image-viewer-toolbar a:active { transform:scale(.94); }
.image-viewer-toolbar button:disabled { opacity:.35; cursor:not-allowed; background:rgba(245,241,232,.08); }
.image-viewer-toolbar .image-viewer-divider { width:1px; height:22px; background:rgba(245,241,232,.2); margin:0 2px; }
.image-viewer-zoom-level { min-width:50px; text-align:center; font-family:'IBM Plex Mono',monospace; font-size:.78rem; color:#E9E4D6; user-select:none; }
.image-viewer-name { max-width:960px; width:100%; text-align:center; color:#C9C2AD; font-size:.82rem; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
@media (max-width:760px) {
  .image-viewer-overlay { padding:14px; gap:10px; }
  .image-viewer-nav { width:38px; height:38px; font-size:1.25rem; }
  .image-viewer-img { max-height:64vh; }
  .image-viewer-toolbar button, .image-viewer-toolbar a { width:34px; height:34px; font-size:.95rem; }
}

/* ===== Al-Ayada v2 additions: allergy banner, vitals, Rx, print ===== */
.allergy-banner{background:#FDECEA;border:2px solid #A94442;border-right:6px solid #A94442;color:#7A2F2C;
  border-radius:var(--radius-sm);padding:10px 14px;margin:0 0 12px;font-weight:700;font-size:.95rem;display:none;}
.allergy-banner .ab-title{display:block;font-family:'Cairo',sans-serif;font-size:.8rem;letter-spacing:.03em;margin-bottom:2px;}
.vitals-grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(96px,1fr));gap:8px;margin-bottom:10px;}
.vitals-grid .vf{display:flex;flex-direction:column;gap:3px;}
.vitals-grid .vf label{font-size:.72rem;color:var(--ink-soft);}
.vitals-grid .vf input{width:100%;padding:7px 8px;border:1px solid var(--line);border-radius:var(--radius-sm);
  background:var(--panel);font-family:'IBM Plex Mono',monospace;text-align:center;}
.vitals-grid .vf input.out-of-range{border-color:#A94442;background:#FDECEA;color:#7A2F2C;font-weight:600;}
.vitals-grid .vf .hint{font-size:.62rem;color:#9A9280;text-align:center;}
.rx-row{display:grid;grid-template-columns:1.4fr 1fr 1fr .9fr auto;gap:6px;margin-bottom:6px;align-items:start;}
/* Prescription row, split into quick numeric cells: اسم الدواء on its own
   line, then وحدة/قيمة الجرعة, then عدد المرات + عدد الأيام as four
   columns so the doctor only types digits. */
.rx-row.rx-row-split{grid-template-columns:repeat(4,minmax(0,1fr));gap:6px;
  border:1px solid var(--line);border-radius:var(--radius-sm);padding:8px;background:#FBF9F4;align-items:center;}
.rx-row-split .rx-drug{grid-column:1 / -1;}
.rx-row-split .rx-drug input{font-weight:700;}
.rx-row-split select{padding:8px 6px;border:1px solid var(--line);border-radius:var(--radius-sm);background:#fff;width:100%;font-size:.85rem;}
.rx-row-split .rx-cell{grid-column:span 2;display:flex;align-items:center;gap:5px;min-width:0;}
.rx-row-split .rx-cell > *{min-width:0;}
.rx-row-split .rx-tag{flex:0 0 auto;font-size:.75rem;color:var(--ink-soft);font-weight:700;white-space:nowrap;}
.rx-row-split .rx-num{flex:1 1 auto;text-align:center;font-weight:700;background:#fff;}
.rx-row-split .rx-del{grid-column:1 / -1;width:100%;justify-self:stretch;padding:6px;margin-top:2px;
  background:#fff;border:1px dashed var(--line);}
.vitals-collapsed{display:none !important;}
.rx-row input{padding:7px 8px;border:1px solid var(--line);border-radius:var(--radius-sm);background:var(--panel);width:100%;}
.rx-row .rx-del{background:none;border:1px solid var(--line);border-radius:var(--radius-sm);color:#A94442;cursor:pointer;padding:6px 9px;}
@media(max-width:640px){.rx-row{grid-template-columns:1fr 1fr;}.rx-row .rx-del{grid-column:span 2;}}
.rx-suggest{position:relative;}
.rx-suggest-list{position:absolute;z-index:60;top:100%;right:0;left:0;background:var(--panel);border:1px solid var(--line);
  border-radius:var(--radius-sm);box-shadow:var(--shadow);max-height:170px;overflow:auto;display:none;}
.rx-suggest-list div{padding:7px 10px;cursor:pointer;font-size:.9rem;}
.rx-suggest-list div:hover{background:var(--canvas);}
.sub-head{font-family:'Cairo',sans-serif;font-weight:700;color:var(--primary-d);font-size:.92rem;
  border-top:1px dashed var(--line);padding-top:12px;margin:14px 0 8px;display:flex;justify-content:space-between;align-items:center;gap:8px;flex-wrap:wrap;}
.trend-box{border:1px solid var(--line);border-radius:var(--radius-sm);padding:8px;margin-bottom:10px;background:var(--panel);}
.trend-box h5{margin:0 0 4px;font-family:'Cairo',sans-serif;font-size:.78rem;color:var(--ink-soft);}
.trend-box svg{width:100%;height:70px;display:block;}
.vitals-chips{display:flex;flex-wrap:wrap;gap:5px;margin-top:6px;}
.vitals-chips span{background:var(--canvas);border:1px solid var(--line);border-radius:20px;padding:2px 9px;
  font-size:.75rem;font-family:'IBM Plex Mono',monospace;}
.rx-view{margin-top:6px;padding:7px 10px;background:#F3F7F4;border-right:3px solid var(--st-done);border-radius:var(--radius-sm);font-size:.85rem;}
#print-area{display:none;}
@media print{
  body>*:not(#print-area){display:none!important;}
  #print-area{display:block!important;padding:0;}
  @page{size:A4;margin:14mm;}
}
.pr-sheet{font-family:'Tajawal',sans-serif;color:#000;direction:rtl;}
.pr-head{display:flex;justify-content:space-between;align-items:flex-start;border-bottom:2px solid #2F5D50;padding-bottom:8px;margin-bottom:10px;}
.pr-head h1{font-size:19pt;margin:0;color:#2F5D50;}
.pr-head p{margin:2px 0;font-size:9.5pt;color:#333;}
.pr-meta{display:flex;gap:22px;flex-wrap:wrap;font-size:10.5pt;margin-bottom:12px;border-bottom:1px dashed #999;padding-bottom:8px;}
.pr-title{font-family:'Cairo',sans-serif;font-size:13pt;margin:12px 0 8px;color:#2F5D50;}
.pr-table{width:100%;border-collapse:collapse;font-size:10.5pt;}
.pr-table th,.pr-table td{border:1px solid #666;padding:6px 8px;text-align:right;}
.pr-table th{background:#EFEDE4;}
.pr-sign{margin-top:38px;text-align:left;font-size:10.5pt;}

