:root {
    --bg: #f5f5f7;
    --surface: rgba(255,255,255,.92);
    --surface-solid: #ffffff;
    --text: #1d1d1f;
    --muted: #6e6e73;
    --line: rgba(0,0,0,.08);
    --primary: #077fbe;
    --primary-dark: #03679e;
    --primary-soft: #eaf6fc;
    --success: #177245;
    --success-soft: #eaf7ef;
    --warning: #9a6700;
    --warning-soft: #fff6df;
    --danger: #b42318;
    --danger-soft: #fef0ee;
    --shadow: 0 10px 35px rgba(0,0,0,.07);
    --radius-xl: 24px;
    --radius-lg: 18px;
    --radius-md: 14px;
    --sidebar: 268px;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Tahoma, Arial, sans-serif;
    background:
        radial-gradient(circle at top right, rgba(7,127,190,.08), transparent 35%),
        var(--bg);
    color: var(--text);
    direction: rtl;
    min-height: 100vh;
}
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
img { max-width: 100%; }
.hidden { display: none !important; }

.app-shell { min-height: 100vh; }
.sidebar {
    position: fixed;
    inset: 0 0 0 auto;
    width: var(--sidebar);
    background: rgba(255,255,255,.82);
    backdrop-filter: blur(22px);
    border-left: 1px solid var(--line);
    padding: 20px 16px;
    overflow-y: auto;
    z-index: 30;
}
.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 8px 22px;
    border-bottom: 1px solid var(--line);
    margin-bottom: 18px;
}
.brand-avatar {
    width: 52px;
    height: 52px;
    border-radius: 17px;
    object-fit: cover;
    box-shadow: 0 6px 20px rgba(7,127,190,.18);
    border: 2px solid white;
}
.brand-name { font-weight: 800; font-size: 14px; line-height: 1.5; }
.brand-sub { color: var(--muted); font-size: 11px; }
.nav-section { margin: 18px 0; }
.nav-title {
    color: #929297;
    font-size: 11px;
    font-weight: 700;
    padding: 0 13px 8px;
}
.nav-link {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 11px 13px;
    margin: 4px 0;
    border-radius: 13px;
    color: #424245;
    font-size: 13px;
    font-weight: 600;
    transition: .2s ease;
}
.nav-link:hover { background: rgba(7,127,190,.07); color: var(--primary-dark); }
.nav-link.active {
    background: linear-gradient(135deg, var(--primary), #34a6d9);
    color: white;
    box-shadow: 0 8px 24px rgba(7,127,190,.22);
}
.nav-icon {
    width: 28px;
    height: 28px;
    display: grid;
    place-items: center;
    border-radius: 9px;
    background: rgba(0,0,0,.035);
    font-size: 14px;
}
.nav-link.active .nav-icon { background: rgba(255,255,255,.18); }

.main {
    margin-right: var(--sidebar);
    min-height: 100vh;
}
.topbar {
    min-height: 76px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 30px;
    position: sticky;
    top: 0;
    z-index: 20;
    background: rgba(245,245,247,.78);
    backdrop-filter: blur(22px);
    border-bottom: 1px solid rgba(0,0,0,.04);
}
.topbar-title h1 { margin: 0; font-size: 22px; letter-spacing: -.4px; }
.topbar-title p { margin: 3px 0 0; color: var(--muted); font-size: 12px; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.user-chip {
    display: flex; align-items: center; gap: 9px;
    padding: 7px 10px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: white;
}
.user-circle {
    width: 34px; height: 34px; border-radius: 50%;
    background: var(--primary-soft); color: var(--primary);
    display: grid; place-items: center; font-weight: 800;
}
.mobile-toggle { display: none; }

.content { padding: 26px 30px 44px; max-width: 1680px; margin: 0 auto; }
.page-head {
    display: flex; align-items: center; justify-content: space-between;
    gap: 14px; margin-bottom: 22px;
}
.page-head h2 { margin: 0; font-size: 24px; }
.page-head p { color: var(--muted); margin: 5px 0 0; font-size: 13px; }

.grid { display: grid; gap: 18px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0,1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0,1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0,1fr)); }
.span-2 { grid-column: span 2; }
.span-3 { grid-column: span 3; }

.card {
    background: var(--surface);
    border: 1px solid rgba(255,255,255,.9);
    box-shadow: var(--shadow);
    border-radius: var(--radius-xl);
    padding: 20px;
}
.card-header {
    display: flex; align-items: center; justify-content: space-between;
    gap: 12px; margin-bottom: 16px;
}
.card-title { font-size: 16px; font-weight: 800; margin: 0; }
.card-subtitle { color: var(--muted); font-size: 12px; margin-top: 3px; }
.metric {
    min-height: 142px;
    position: relative;
    overflow: hidden;
}
.metric::after {
    content: "";
    position: absolute;
    width: 90px; height: 90px;
    border-radius: 50%;
    left: -18px; bottom: -28px;
    background: rgba(7,127,190,.08);
}
.metric-label { color: var(--muted); font-size: 12px; font-weight: 600; }
.metric-value { font-size: 30px; font-weight: 800; margin-top: 14px; letter-spacing: -.8px; }
.metric-note { font-size: 11px; color: var(--muted); margin-top: 6px; }
.metric-icon {
    position: absolute; left: 18px; top: 18px;
    width: 42px; height: 42px; border-radius: 14px;
    display: grid; place-items: center; background: var(--primary-soft); color: var(--primary);
}

.btn {
    border: 0;
    border-radius: 13px;
    padding: 10px 15px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-weight: 700;
    font-size: 13px;
    transition: .18s ease;
    white-space: nowrap;
}
.btn:hover { transform: translateY(-1px); }
.btn-primary { background: var(--primary); color: #fff; box-shadow: 0 8px 18px rgba(7,127,190,.18); }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: #fff; border: 1px solid var(--line); color: var(--text); }
.btn-soft { background: var(--primary-soft); color: var(--primary-dark); }
.btn-danger { background: var(--danger-soft); color: var(--danger); }
.btn-success { background: var(--success-soft); color: var(--success); }
.btn-sm { padding: 7px 10px; border-radius: 10px; font-size: 11px; }
.btn-block { width: 100%; }

.input, .select, .textarea {
    width: 100%;
    border: 1px solid rgba(0,0,0,.10);
    background: rgba(255,255,255,.88);
    border-radius: 13px;
    padding: 11px 13px;
    color: var(--text);
    outline: none;
    transition: .18s;
}
.input:focus, .select:focus, .textarea:focus {
    border-color: rgba(7,127,190,.7);
    box-shadow: 0 0 0 4px rgba(7,127,190,.08);
}
.textarea { min-height: 110px; resize: vertical; }
.form-group { margin-bottom: 15px; }
.label { display: block; font-size: 12px; font-weight: 700; margin-bottom: 7px; }
.hint { font-size: 10px; color: var(--muted); margin-top: 6px; }
.required::after { content: " *"; color: var(--danger); }
.form-actions { display: flex; gap: 10px; justify-content: flex-start; margin-top: 20px; }
.checkbox-row { display:flex; align-items:center; gap:8px; font-size:12px; }
.checkbox { width: 18px; height: 18px; accent-color: var(--primary); }

.table-wrap {
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: 18px;
    background: white;
}
.table {
    width: 100%;
    border-collapse: collapse;
    min-width: 760px;
}
.table th {
    text-align: right;
    color: var(--muted);
    background: #fafafa;
    font-size: 11px;
    padding: 12px 14px;
    border-bottom: 1px solid var(--line);
    white-space: nowrap;
}
.table td {
    padding: 13px 14px;
    border-bottom: 1px solid rgba(0,0,0,.055);
    font-size: 12px;
    vertical-align: middle;
}
.table tr:last-child td { border-bottom: 0; }
.table tr:hover td { background: #fcfcfd; }
.cell-title { font-weight: 700; }
.cell-sub { font-size: 10px; color: var(--muted); margin-top: 2px; }

.badge {
    display: inline-flex; align-items: center; gap: 5px;
    border-radius: 999px; padding: 5px 9px;
    font-size: 10px; font-weight: 800;
}
.badge-success { color: var(--success); background: var(--success-soft); }
.badge-info { color: var(--primary-dark); background: var(--primary-soft); }
.badge-warning { color: var(--warning); background: var(--warning-soft); }
.badge-danger { color: var(--danger); background: var(--danger-soft); }
.badge-neutral { color: #555; background: #f0f0f2; }

.alert {
    border-radius: 15px;
    padding: 13px 15px;
    margin-bottom: 17px;
    font-size: 12px;
    border: 1px solid transparent;
}
.alert-success { background: var(--success-soft); color: var(--success); border-color: rgba(23,114,69,.12); }
.alert-danger { background: var(--danger-soft); color: var(--danger); border-color: rgba(180,35,24,.12); }
.alert-warning { background: var(--warning-soft); color: var(--warning); border-color: rgba(154,103,0,.12); }

.filters {
    display: flex; gap: 10px; align-items: end; flex-wrap: wrap;
    margin-bottom: 16px;
}
.filters .form-group { margin: 0; min-width: 150px; flex: 1; }
.filters .search-wide { flex: 2; min-width: 240px; }

.empty {
    text-align: center;
    padding: 48px 20px;
    color: var(--muted);
}
.empty-icon {
    width: 62px; height: 62px; border-radius: 20px;
    display: grid; place-items: center; margin: 0 auto 12px;
    background: var(--primary-soft); color: var(--primary); font-size: 26px;
}

.pagination { display: flex; gap: 5px; margin-top: 15px; flex-wrap: wrap; }
.pagination a, .pagination span {
    padding: 7px 11px; border-radius: 9px;
    background: white; border: 1px solid var(--line); font-size: 11px;
}
.pagination .active { background: var(--primary); color: white; border-color: var(--primary); }

.detail-list { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 14px; }
.detail-item {
    padding: 13px;
    border-radius: 14px;
    background: #fafafa;
    border: 1px solid var(--line);
}
.detail-key { color: var(--muted); font-size: 10px; margin-bottom: 5px; }
.detail-value { font-size: 13px; font-weight: 700; white-space: pre-wrap; }

.timeline { position: relative; padding-right: 19px; }
.timeline::before { content: ""; position:absolute; right:5px; top:5px; bottom:5px; width:2px; background:#e7e7ea; }
.timeline-item { position:relative; margin-bottom:14px; }
.timeline-dot { position:absolute; right:-19px; top:5px; width:12px; height:12px; border-radius:50%; background:var(--primary); border:3px solid white; box-shadow:0 0 0 1px rgba(7,127,190,.25); }
.timeline-title { font-size:12px; font-weight:800; }
.timeline-meta { font-size:10px; color:var(--muted); margin-top:2px; }

.chart-bars {
    height: 210px;
    display: flex;
    align-items: flex-end;
    gap: 12px;
    padding-top: 24px;
}
.bar-item { flex: 1; text-align: center; min-width: 30px; }
.bar {
    width: 100%;
    max-width: 46px;
    margin: 0 auto 8px;
    background: linear-gradient(180deg, #39a7da, var(--primary));
    border-radius: 10px 10px 5px 5px;
    min-height: 5px;
    position: relative;
}
.bar:hover::after {
    content: attr(data-value);
    position: absolute;
    bottom: calc(100% + 7px);
    right: 50%;
    transform: translateX(50%);
    background: #1d1d1f; color: white; padding: 4px 7px; border-radius: 7px; font-size: 9px;
    white-space: nowrap;
}
.bar-label { font-size: 10px; color: var(--muted); }

.ai-layout { display:grid; grid-template-columns: 250px minmax(0,1fr); gap:18px; min-height: calc(100vh - 150px); }
.ai-sidebar { background:white; border-radius:22px; padding:13px; border:1px solid var(--line); }
.ai-thread {
    display:block; padding:10px; border-radius:12px; margin:4px 0;
    font-size:11px; color:#444; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;
}
.ai-thread:hover, .ai-thread.active { background:var(--primary-soft); color:var(--primary-dark); }
.ai-chat { display:flex; flex-direction:column; background:white; border-radius:22px; border:1px solid var(--line); overflow:hidden; }
.ai-messages { flex:1; padding:22px; overflow:auto; min-height:460px; max-height:calc(100vh - 270px); }
.ai-message { max-width:82%; margin-bottom:14px; padding:13px 15px; border-radius:18px; font-size:12px; line-height:1.9; white-space:pre-wrap; }
.ai-message.user { margin-right:auto; background:var(--primary); color:white; border-bottom-left-radius:6px; }
.ai-message.assistant { margin-left:auto; background:#f3f3f5; color:#242426; border-bottom-right-radius:6px; }
.ai-compose { padding:14px; border-top:1px solid var(--line); display:flex; gap:9px; background:#fafafa; }
.ai-compose textarea { min-height:52px; max-height:150px; }

.login-page {
    min-height:100vh; display:grid; place-items:center; padding:24px;
    background:
      radial-gradient(circle at 80% 20%, rgba(7,127,190,.18), transparent 28%),
      radial-gradient(circle at 20% 80%, rgba(67,171,218,.14), transparent 30%),
      #f5f5f7;
}
.login-card {
    width:min(100%, 440px);
    background:rgba(255,255,255,.88);
    backdrop-filter:blur(24px);
    border:1px solid rgba(255,255,255,.9);
    border-radius:30px;
    box-shadow:0 25px 70px rgba(0,0,0,.10);
    padding:30px;
}
.login-logo { text-align:center; margin-bottom:24px; }
.login-logo img { width:78px; height:78px; object-fit:cover; border-radius:24px; box-shadow:0 12px 28px rgba(7,127,190,.18); }
.login-logo h1 { font-size:20px; margin:14px 0 4px; }
.login-logo p { color:var(--muted); font-size:11px; margin:0; }

.public-shell { max-width:760px; margin:35px auto; padding:0 16px; }
.public-header { text-align:center; margin-bottom:25px; }
.public-header img { width:76px; height:76px; border-radius:24px; object-fit:cover; }
.public-header h1 { margin:12px 0 3px; font-size:22px; }
.public-header p { color:var(--muted); font-size:12px; }

.prescription-paper {
    width: 210mm;
    min-height: 297mm;
    margin: 20px auto;
    background:white;
    padding:16mm;
    box-shadow:0 12px 35px rgba(0,0,0,.08);
    direction:rtl;
}
.rx-header { display:flex; justify-content:space-between; gap:20px; border-bottom:2px solid var(--primary); padding-bottom:14px; }
.rx-doctor h1 { margin:0; color:var(--primary); font-size:22px; }
.rx-doctor p { margin:4px 0; font-size:11px; }
.rx-logo img { width:70px; height:70px; object-fit:cover; border-radius:20px; }
.rx-patient { display:grid; grid-template-columns:repeat(3,1fr); gap:10px; padding:15px 0; border-bottom:1px solid #ddd; }
.rx-symbol { font-family:Georgia,serif; font-size:44px; color:var(--primary); margin:14px 0 0; }
.rx-item { margin:15px 0; padding:11px 14px; border-right:4px solid var(--primary); background:#fafafa; }
.rx-med { font-size:17px; font-weight:800; }
.rx-details { font-size:11px; margin-top:6px; color:#444; }
.rx-footer { margin-top:30px; border-top:1px solid #ddd; padding-top:15px; display:flex; justify-content:space-between; font-size:10px; }

@media print {
    body { background:white; }
    .print-hide { display:none !important; }
    .prescription-paper { margin:0; box-shadow:none; width:auto; min-height:auto; padding:10mm; }
}

@media (max-width: 1100px) {
    .grid-4 { grid-template-columns: repeat(2,minmax(0,1fr)); }
    .grid-3 { grid-template-columns: repeat(2,minmax(0,1fr)); }
}
@media (max-width: 820px) {
    .sidebar { transform: translateX(110%); transition:.25s; box-shadow:-20px 0 50px rgba(0,0,0,.15); }
    body.sidebar-open .sidebar { transform: translateX(0); }
    .main { margin-right:0; }
    .mobile-toggle { display:inline-flex; }
    .content, .topbar { padding-left:16px; padding-right:16px; }
    .topbar-title p { display:none; }
    .user-chip .user-meta { display:none; }
    .grid-2,.grid-3,.grid-4 { grid-template-columns:1fr; }
    .span-2,.span-3 { grid-column:auto; }
    .detail-list { grid-template-columns:1fr; }
    .page-head { align-items:flex-start; flex-direction:column; }
    .ai-layout { grid-template-columns:1fr; }
    .ai-sidebar { max-height:180px; overflow:auto; }
}
@media (max-width: 520px) {
    .content { padding-top:18px; }
    .card { padding:16px; border-radius:20px; }
    .metric-value { font-size:25px; }
    .form-actions { flex-direction:column; }
    .form-actions .btn { width:100%; }
    .topbar { min-height:66px; }
}

/* OpenAI settings status */
.api-key-status {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 14px;
    border-radius: 16px;
    border: 1px solid var(--line);
    background: #f7f7f8;
}
.api-key-status.is-ready { background: var(--success-soft); border-color: rgba(23,114,69,.15); }
.api-key-status.is-error { background: var(--danger-soft); border-color: rgba(180,35,24,.15); }
.api-key-status-icon {
    flex: 0 0 34px;
    width: 34px;
    height: 34px;
    display: grid;
    place-items: center;
    border-radius: 11px;
    background: white;
    font-weight: 900;
    box-shadow: 0 5px 15px rgba(0,0,0,.05);
}
.api-key-status.is-ready .api-key-status-icon { color: var(--success); }
.api-key-status.is-error .api-key-status-icon { color: var(--danger); }

/* Floating context-aware clinic assistant */
.ai-widget-root {
    position: fixed;
    left: 24px;
    bottom: 24px;
    z-index: 100;
    direction: rtl;
}
.ai-fab {
    border: 0;
    min-height: 54px;
    padding: 9px 12px 9px 18px;
    border-radius: 999px;
    display: flex;
    align-items: center;
    gap: 10px;
    background: rgba(29,29,31,.94);
    color: white;
    box-shadow: 0 18px 45px rgba(0,0,0,.22);
    backdrop-filter: blur(18px);
    font-weight: 800;
    transition: transform .2s ease, box-shadow .2s ease, opacity .2s ease;
}
.ai-fab:hover { transform: translateY(-2px); box-shadow: 0 22px 55px rgba(0,0,0,.28); }
.ai-fab-spark {
    width: 36px;
    height: 36px;
    display: grid;
    place-items: center;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), #42b9e6);
    box-shadow: inset 0 0 0 1px rgba(255,255,255,.22);
}
.ai-fab-label { font-size: 12px; }
.ai-widget-root.is-open .ai-fab { opacity: 0; pointer-events: none; transform: translateY(10px) scale(.95); }
.ai-widget {
    position: absolute;
    left: 0;
    bottom: 0;
    width: min(420px, calc(100vw - 32px));
    height: min(680px, calc(100vh - 40px));
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border-radius: 28px;
    border: 1px solid rgba(255,255,255,.9);
    background: rgba(255,255,255,.96);
    box-shadow: 0 28px 80px rgba(0,0,0,.22);
    backdrop-filter: blur(28px);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transform: translateY(18px) scale(.97);
    transform-origin: bottom left;
    transition: opacity .22s ease, transform .22s ease, visibility .22s ease;
}
.ai-widget-root.is-open .ai-widget {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transform: translateY(0) scale(1);
}
.ai-widget-header {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 72px;
    padding: 14px 16px;
    background: rgba(250,250,251,.92);
    border-bottom: 1px solid var(--line);
}
.ai-widget-avatar {
    flex: 0 0 42px;
    width: 42px;
    height: 42px;
    display: grid;
    place-items: center;
    border-radius: 15px;
    color: white;
    background: linear-gradient(135deg, var(--primary), #40b4e2);
    font-size: 12px;
    font-weight: 900;
    box-shadow: 0 8px 22px rgba(7,127,190,.22);
}
.ai-widget-heading { min-width: 0; flex: 1; display: flex; flex-direction: column; gap: 3px; }
.ai-widget-heading strong { font-size: 13px; }
.ai-widget-heading span { color: var(--muted); font-size: 10px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ai-widget-icon-button {
    border: 0;
    width: 34px;
    height: 34px;
    border-radius: 11px;
    background: #eeeef0;
    color: #424245;
    font-size: 18px;
}
.ai-widget-icon-button:hover { background: var(--primary-soft); color: var(--primary-dark); }
.ai-context-chip {
    display: flex;
    align-items: center;
    gap: 7px;
    align-self: flex-start;
    max-width: calc(100% - 32px);
    margin: 12px 16px 0;
    padding: 7px 10px;
    border-radius: 999px;
    background: var(--primary-soft);
    color: var(--primary-dark);
    font-size: 10px;
    font-weight: 800;
}
.ai-context-chip span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ai-widget-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    scroll-behavior: smooth;
}
.ai-widget-empty {
    min-height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    color: var(--muted);
    padding: 24px 8px;
}
.ai-widget-empty-icon {
    width: 58px;
    height: 58px;
    display: grid;
    place-items: center;
    border-radius: 20px;
    color: var(--primary);
    background: var(--primary-soft);
    font-size: 24px;
    margin-bottom: 12px;
}
.ai-widget-empty strong { color: var(--text); font-size: 15px; }
.ai-widget-empty p { max-width: 290px; font-size: 11px; line-height: 1.8; margin: 7px 0 14px; }
.ai-suggestions { display: flex; flex-wrap: wrap; justify-content: center; gap: 7px; }
.ai-suggestions button {
    border: 1px solid var(--line);
    background: white;
    color: #48484a;
    border-radius: 999px;
    padding: 7px 10px;
    font-size: 10px;
    font-weight: 700;
}
.ai-suggestions button:hover { border-color: rgba(7,127,190,.35); color: var(--primary-dark); }
.ai-widget-message { display: flex; margin: 0 0 12px; }
.ai-widget-message.user { justify-content: flex-start; }
.ai-widget-message.assistant { justify-content: flex-end; }
.ai-widget-bubble {
    max-width: 86%;
    padding: 11px 13px;
    border-radius: 18px;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    font-size: 11.5px;
    line-height: 1.85;
}
.ai-widget-message.user .ai-widget-bubble {
    background: var(--primary);
    color: white;
    border-bottom-left-radius: 6px;
}
.ai-widget-message.assistant .ai-widget-bubble {
    background: #f1f1f3;
    color: #252527;
    border-bottom-right-radius: 6px;
}
.ai-widget-message.error .ai-widget-bubble { background: var(--danger-soft); color: var(--danger); }
.ai-widget-typing {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 0 18px 10px;
    color: var(--muted);
    font-size: 9px;
}
.ai-widget-typing span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--primary);
    animation: aiTyping 1.2s infinite ease-in-out;
}
.ai-widget-typing span:nth-child(2) { animation-delay: .15s; }
.ai-widget-typing span:nth-child(3) { animation-delay: .3s; }
.ai-widget-typing em { font-style: normal; margin-right: 5px; }
@keyframes aiTyping { 0%, 70%, 100% { transform: translateY(0); opacity: .35; } 35% { transform: translateY(-4px); opacity: 1; } }
.ai-widget-compose {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 12px 14px;
    border-top: 1px solid var(--line);
    background: rgba(250,250,251,.94);
}
.ai-widget-compose textarea {
    flex: 1;
    min-height: 44px;
    max-height: 120px;
    resize: none;
    border: 1px solid rgba(0,0,0,.09);
    border-radius: 16px;
    padding: 11px 12px;
    outline: none;
    background: white;
    line-height: 1.5;
    font-size: 11px;
}
.ai-widget-compose textarea:focus { border-color: rgba(7,127,190,.55); box-shadow: 0 0 0 3px rgba(7,127,190,.07); }
.ai-widget-compose button {
    flex: 0 0 42px;
    width: 42px;
    height: 42px;
    border: 0;
    border-radius: 14px;
    background: var(--primary);
    color: white;
    font-size: 21px;
    font-weight: 900;
}
.ai-widget-compose button:disabled { opacity: .5; cursor: not-allowed; }
.ai-widget-disclaimer {
    padding: 0 14px 10px;
    background: rgba(250,250,251,.94);
    color: #8a8a8f;
    font-size: 8.5px;
    text-align: center;
}

@media (max-width: 520px) {
    .ai-widget-root { left: 12px; bottom: 12px; }
    .ai-widget { width: calc(100vw - 24px); height: calc(100vh - 24px); border-radius: 24px; }
    .ai-fab-label { display: none; }
    .ai-fab { padding: 9px; }
}

/* Mobile refinements for the floating assistant and application chrome */
@media (max-width: 820px) {
    .topbar { align-items: flex-start; gap: 10px; }
    .topbar-actions { flex-wrap: wrap; justify-content: flex-end; gap: 7px; }
    .topbar-actions .user-chip { order: -1; }
    .content { padding-bottom: 92px; }
}

@media (max-width: 520px) {
    body.ai-widget-open { overflow: hidden; overscroll-behavior: none; }
    .ai-widget-root.is-open { inset: 0; }
    .ai-widget-root.is-open .ai-widget {
        position: fixed;
        inset: 0;
        width: 100vw;
        height: 100dvh;
        max-height: none;
        border-radius: 0;
        border: 0;
        transform: none;
    }
    .ai-widget-root.is-open .ai-fab { display: none; }
    .ai-widget-header {
        padding-top: max(14px, env(safe-area-inset-top));
        padding-left: max(16px, env(safe-area-inset-left));
        padding-right: max(16px, env(safe-area-inset-right));
    }
    .ai-widget-compose {
        padding-bottom: max(12px, env(safe-area-inset-bottom));
        padding-left: max(14px, env(safe-area-inset-left));
        padding-right: max(14px, env(safe-area-inset-right));
    }
    .ai-widget-disclaimer { padding-bottom: max(9px, env(safe-area-inset-bottom)); }
    .topbar { padding: 12px; }
    .topbar-title h1 { font-size: 18px; }
    .topbar-title p { max-width: 230px; }
    .topbar-actions { width: 100%; justify-content: space-between; }
    .topbar-actions > .btn-primary { flex: 1 1 100%; }
    .user-meta { display: none; }
    .content { padding: 14px 12px 96px; }
    .card { border-radius: 20px; }
    .table-wrap { margin-inline: -4px; }
}

/* V3 — reliable mobile navigation */
.sidebar-close,
.sidebar-backdrop { display: none; }

.brand-copy { min-width: 0; flex: 1; }
.sidebar-close {
    width: 38px;
    height: 38px;
    flex: 0 0 38px;
    border: 0;
    border-radius: 12px;
    background: #f2f2f4;
    color: var(--text);
    font-size: 25px;
    line-height: 1;
    align-items: center;
    justify-content: center;
}

/* Prescription medicine history/autocomplete */
.medication-row {
    position: relative;
    background: #fafafa;
    box-shadow: none;
    margin-bottom: 12px;
    padding: 14px;
    overflow: visible;
}
.medication-autocomplete { position: relative; z-index: 4; }
.medication-suggestions {
    position: absolute;
    z-index: 80;
    top: calc(100% - 8px);
    right: 0;
    left: 0;
    max-height: 310px;
    overflow-y: auto;
    background: #fff;
    border: 1px solid rgba(0,0,0,.1);
    border-radius: 14px;
    box-shadow: 0 18px 42px rgba(0,0,0,.14);
    padding: 6px;
}
.medication-suggestion {
    display: flex;
    align-items: stretch;
    gap: 6px;
    border-bottom: 1px solid rgba(0,0,0,.055);
}
.medication-suggestion:last-child { border-bottom: 0; }
.medication-suggestion-main {
    border: 0;
    background: transparent;
    text-align: right;
    padding: 10px;
    min-width: 0;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;
    border-radius: 10px;
}
.medication-suggestion-main:hover { background: var(--primary-soft); }
.medication-suggestion-main strong { font-size: 12px; color: var(--text); }
.medication-suggestion-main span { font-size: 10px; color: var(--muted); white-space: normal; }
.medication-suggestion-remove {
    border: 0;
    background: transparent;
    color: var(--danger);
    font-size: 10px;
    padding: 8px 9px;
    border-radius: 9px;
    align-self: center;
}
.medication-suggestion-remove:hover { background: var(--danger-soft); }
.medication-row-actions {
    display: flex;
    gap: 12px;
    align-items: center;
    justify-content: space-between;
}
.prescription-medications-head { align-items: flex-start; }
.key-action-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

@media (max-width: 820px) {
    body.sidebar-lock { overflow: hidden; }

    .sidebar {
        width: min(88vw, 320px);
        max-width: 320px;
        z-index: 110;
        transform: translateX(105%);
        transition: transform .24s ease;
        box-shadow: -22px 0 55px rgba(0,0,0,.18);
        background: rgba(255,255,255,.98);
        padding-top: max(14px, env(safe-area-inset-top));
        padding-bottom: max(18px, env(safe-area-inset-bottom));
    }
    body.sidebar-open .sidebar { transform: translateX(0); }

    .sidebar-close { display: inline-flex; }
    .sidebar-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 100;
        border: 0;
        padding: 0;
        margin: 0;
        background: rgba(17,24,39,.34);
        backdrop-filter: blur(2px);
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
        transition: opacity .2s ease, visibility .2s ease;
    }
    body.sidebar-open .sidebar-backdrop {
        opacity: 1;
        visibility: visible;
        pointer-events: auto;
    }

    .mobile-toggle {
        display: inline-flex !important;
        width: 42px;
        height: 42px;
        padding: 0;
        flex: 0 0 42px;
        font-size: 19px;
        position: relative;
        z-index: 25;
    }

    .topbar > div:first-child { min-width: 0; flex: 1; }
    .topbar-title { min-width: 0; }
    .topbar-title h1 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

    .filters .form-group,
    .filters .search-wide { min-width: min(100%, 220px); }

    .medication-row-actions { align-items: flex-start; flex-direction: column; }
    .medication-row-actions .btn { width: 100%; }
    .prescription-medications-head { flex-direction: column; }
    .prescription-medications-head .btn { width: 100%; }
}

@media (max-width: 520px) {
    html, body { max-width: 100%; overflow-x: hidden; }
    .app-shell, .main, .content, .card { min-width: 0; max-width: 100%; }
    .topbar { align-items: center; flex-wrap: wrap; }
    .topbar > div:first-child { width: 100%; }
    .topbar-actions { width: 100%; display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
    .topbar-actions > .btn-primary { grid-column: 1 / -1; width: 100%; }
    .topbar-actions .user-chip { min-width: 0; width: 100%; justify-content: center; }
    .topbar-actions form { width: 100%; }
    .topbar-actions form .btn,
    .topbar-actions > .btn-secondary { width: 100%; }

    .filters { display: grid; grid-template-columns: 1fr; align-items: stretch; }
    .filters .form-group,
    .filters .search-wide { width: 100%; min-width: 0; }
    .filters > .btn { width: 100%; }

    .table-wrap { width: 100%; max-width: 100%; -webkit-overflow-scrolling: touch; }
    .table { min-width: 700px; }

    .key-action-grid { grid-template-columns: 1fr; }
    .medication-suggestions {
        position: fixed;
        top: auto;
        right: 10px;
        left: 10px;
        bottom: max(10px, env(safe-area-inset-bottom));
        max-height: 48vh;
        border-radius: 18px;
        z-index: 140;
    }

    .btn { min-height: 42px; }
    .btn-sm { min-height: 36px; }
    .input, .select, .textarea { font-size: 16px; } /* avoids iOS zoom */
}

/* The menu button also collapses the desktop sidebar. */
.mobile-toggle { display:inline-flex; min-width:44px; min-height:44px; }
@media (min-width:821px) {
    .sidebar { transition:transform .24s ease; }
    body.sidebar-collapsed .sidebar { transform:translateX(110%); visibility:hidden; }
    body.sidebar-collapsed .main { margin-right:0; }
}
@media (prefers-reduced-motion:reduce) {
    .sidebar, .sidebar-backdrop { transition:none; }
}
