/* ============================================================
   FONTS (self-hosted, no Google dependency)
   ============================================================ */
@font-face {
    font-family: 'Inter';
    font-style: normal;
    font-weight: 300;
    font-display: swap;
    src: url('/assets/fonts/inter-300.woff2') format('woff2');
}
@font-face {
    font-family: 'Inter';
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url('/assets/fonts/inter-400.woff2') format('woff2');
}
@font-face {
    font-family: 'Inter';
    font-style: normal;
    font-weight: 500;
    font-display: swap;
    src: url('/assets/fonts/inter-500.woff2') format('woff2');
}
@font-face {
    font-family: 'Inter';
    font-style: normal;
    font-weight: 600;
    font-display: swap;
    src: url('/assets/fonts/inter-600.woff2') format('woff2');
}
@font-face {
    font-family: 'Inter';
    font-style: normal;
    font-weight: 700;
    font-display: swap;
    src: url('/assets/fonts/inter-700.woff2') format('woff2');
}
@font-face {
    font-family: 'Noto Nastaliq Urdu';
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url('/assets/fonts/noto-nastaliq-urdu-400.woff2') format('woff2');
}
@font-face {
    font-family: 'Noto Nastaliq Urdu';
    font-style: normal;
    font-weight: 700;
    font-display: swap;
    src: url('/assets/fonts/noto-nastaliq-urdu-700.woff2') format('woff2');
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
.is-hidden { display: none !important; }
:root {
    --bg: #f8fafc; --surface: #ffffff; --border: #e2e8f0;
    --text: #0f172a; --text-secondary: #64748b; --text-muted: #94a3b8;
    --slate: #334155;
    --primary: #2563eb; --primary-hover: #1d4ed8; --primary-light: #dbeafe;
    --danger: #dc2626; --danger-hover: #b91c1c; --danger-light: #fee2e2;
    --success: #16a34a; --success-light: #dcfce7;
    --warn: #d97706; --warn-light: #fef3c7;
    --gold: #ca8a04; --gold-bg: #fef9c3; --gold-border: #ca8a04;
    --silver: #64748b; --silver-bg: #f1f5f9;
    --red-border: #dc2626;
    --shadow: 0 1px 3px rgba(0,0,0,.1); --shadow-lg: 0 4px 12px rgba(0,0,0,.15);
    --radius: 8px; --radius-sm: 4px;
    --sidebar-w: 280px;
    --font: 'Inter', -apple-system, sans-serif;
    --login-bg: #0f172a;
}
html, body { height: 100%; font-family: var(--font); background: var(--bg); color: var(--text); }

/* ============================================================
   LOGIN PAGE: full-screen, centered card
   ============================================================ */
.login-page {
    position: fixed; inset: 0; z-index: 200;
    display: flex; align-items: center; justify-content: center;
    background: var(--login-bg);
    background-image:
        radial-gradient(ellipse at 20% 50%, rgba(37,99,235,.15) 0%, transparent 50%),
        radial-gradient(ellipse at 80% 50%, rgba(99,102,241,.1) 0%, transparent 50%);
}
.login-card {
    width: 100%; max-width: 400px; padding: 40px 36px;
    background: var(--surface); border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0,0,0,.3);
    animation: loginFadeIn .4s ease;
}
@keyframes loginFadeIn { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }

.login-card-brand { text-align: center; margin-bottom: 32px; }
.login-card-icon { font-size: 48px; margin-bottom: 8px; }
.login-card-brand h1 { font-size: 24px; font-weight: 700; color: var(--primary); }
.login-card-brand p { font-size: 13px; color: var(--text-muted); margin-top: 4px; }

.login-card .form-group { margin-bottom: 18px; }
.login-card .form-group label {
    display: block; font-size: 13px; font-weight: 500; color: var(--text-secondary); margin-bottom: 6px;
}
.login-card .field {
    padding: 10px 14px; font-size: 14px;
}

.login-submit {
    width: 100%; padding: 12px; font-size: 15px; font-weight: 600;
    margin-top: 8px; border-radius: var(--radius);
}

.login-error {
    background: var(--danger-light); color: var(--danger); font-size: 13px;
    padding: 10px 14px; border-radius: var(--radius-sm); margin-bottom: 12px;
    text-align: center; font-weight: 500;
}

.login-card-lang {
    display: flex; gap: 6px; justify-content: center; margin-top: 24px;
    padding-top: 20px; border-top: 1px solid var(--border);
}
.login-card-lang .lang-btn {
    padding: 6px 16px; border-radius: var(--radius-sm);
    font-size: 12px; font-weight: 600;
}

/* ============================================================
   APP LAYOUT
   ============================================================ */
.app { display: flex; height: 100vh; overflow: hidden; }

/* ============================================================
   SIDEBAR
   ============================================================ */
.sidebar {
    width: var(--sidebar-w); min-width: var(--sidebar-w); background: var(--surface);
    border-right: 1px solid var(--border); display: flex; flex-direction: column;
    overflow-y: auto; z-index: 20;
    transition: width .25s ease, min-width .25s ease, transform .25s ease;
}
.sidebar-collapsed .sidebar {
    width: 0; min-width: 0; overflow: hidden; border-right: none;
}
.sidebar-collapsed .sidebar > * { opacity: 0; pointer-events: none; }

/* Brand */
.sidebar-brand {
    padding: 20px 20px 16px;
    border-bottom: 1px solid var(--border);
    background: linear-gradient(135deg, var(--primary-light) 0%, var(--surface) 100%);
}
.sidebar-brand h1 { font-size: 18px; font-weight: 700; color: var(--primary); }
.sidebar-brand p { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* Sidebar sections */
.sidebar-section { padding: 12px 16px; }
.sidebar-section-title {
    font-size: 11px; font-weight: 600; color: var(--text-muted);
    text-transform: uppercase; letter-spacing: .8px; margin-bottom: 8px;
}
.sidebar-action-btn { margin-bottom: 6px; }
.sidebar-action-btn:last-child { margin-bottom: 0; }

/* Caste / Section selector */
.caste-list { display: flex; flex-direction: column; gap: 2px; margin-bottom: 8px; max-height: 200px; overflow-y: auto; }
.caste-item {
    display: flex; align-items: center; justify-content: space-between;
    padding: 7px 10px; border-radius: var(--radius-sm);
    cursor: pointer; font-size: 13px; color: var(--text-secondary);
    transition: all .15s;
}
.caste-item:hover { background: var(--primary-light); color: var(--primary); }
.caste-item.active { background: var(--primary-light); color: var(--primary); font-weight: 600; }
.caste-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.caste-count { font-size: 11px; color: var(--text-muted); margin-left: 6px; background: var(--bg); padding: 1px 6px; border-radius: 8px; }
[dir="rtl"] .caste-count { margin-left: 0; margin-right: 6px; }

/* User profile */
.sidebar-profile {
    display: flex; align-items: center; gap: 10px;
    padding: 14px 16px;
    background: var(--bg);
}
.sidebar-profile-avatar {
    width: 36px; height: 36px; border-radius: 50%;
    background: var(--primary); color: white;
    display: flex; align-items: center; justify-content: center;
    font-weight: 700; font-size: 14px; flex-shrink: 0;
    text-transform: uppercase;
}
.sidebar-profile-info { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.sidebar-profile-name { font-size: 13px; font-weight: 600; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-profile-role { font-size: 11px; color: var(--text-muted); text-transform: capitalize; }
.sidebar-profile-logout { flex-shrink: 0; }

/* View-only banner */
.view-only-banner {
    background: var(--warn-light); color: var(--warn); font-size: 12px;
    font-weight: 600; padding: 8px 16px; text-align: center;
}

/* Sidebar dividers */
.sidebar-divider { height: 1px; background: var(--border); margin: 0; }

/* Sidebar footer (legend) */
.sidebar-footer { margin-top: auto; padding: 0 16px 16px; }
.sidebar-footer .sidebar-divider { margin-bottom: 12px; }

/* ============================================================
   SEARCH
   ============================================================ */
.search-box { position: relative; }
.search-results {
    position: absolute; top: 100%; left: 0; right: 0; background: var(--surface);
    border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-lg);
    max-height: 200px; overflow-y: auto; z-index: 30;
}
.search-result-item {
    padding: 8px 12px; cursor: pointer; font-size: 13px; border-bottom: 1px solid var(--border);
}
.search-result-item:last-child { border-bottom: none; }
.search-result-item:hover { background: var(--primary-light); }
.search-result-item .sr-title { color: var(--text-muted); font-size: 11px; }

/* ============================================================
   LEGEND
   ============================================================ */
.legend h4 { font-size: 11px; font-weight: 600; color: var(--text-muted); margin-bottom: 8px; text-transform: uppercase; letter-spacing: .5px; }
.legend-item { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--text-secondary); margin-bottom: 5px; }
.legend-dot { width: 12px; height: 12px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.legend-dot.heir { background: var(--gold); }
.legend-dot.branch { background: var(--silver); }
.legend-dot.root { border: 2px solid var(--red-border); background: white; }
.legend-dot.dead { background: #cbd5e1; border: 2px solid #94a3b8; }
.legend-dot.end { background: var(--danger-light); border: 2px dashed var(--danger); }

/* ============================================================
   MAIN CONTENT
   ============================================================ */
.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-width: 0; }
.topbar {
    display: flex; align-items: center; justify-content: space-between;
    padding: 12px 20px; background: var(--surface); border-bottom: 1px solid var(--border);
    min-height: 56px; flex-wrap: wrap; gap: 8px;
}
.topbar-left { display: flex; align-items: center; gap: 12px; flex: 1; min-width: 0; }
.topbar-actions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.menu-toggle {
    display: flex; align-items: center; justify-content: center;
    background: none; border: none; font-size: 20px; cursor: pointer;
    color: var(--text); padding: 4px; min-width: 36px; min-height: 36px;
    border-radius: var(--radius-sm); transition: background .15s; flex-shrink: 0;
}
.menu-toggle:hover { background: var(--bg); }
.selected-name { font-weight: 600; font-size: 14px; color: var(--primary); margin-right: 4px; white-space: nowrap; }
.topbar-img-btn { flex-shrink: 0; }
.topbar-compact-btn { flex-shrink: 0; }
.topbar-compact-btn.active { background: var(--primary-light); color: var(--primary); border-color: var(--primary); }

/* ============================================================
   BREADCRUMB
   ============================================================ */
.breadcrumb { display: flex; align-items: center; gap: 4px; font-size: 13px; color: var(--text-secondary); overflow: hidden; }
.breadcrumb-item { cursor: pointer; padding: 2px 6px; border-radius: var(--radius-sm); white-space: nowrap; }
.breadcrumb-item:hover { background: var(--primary-light); color: var(--primary); }
.breadcrumb-item.active { color: var(--text); font-weight: 500; cursor: default; }
.breadcrumb-item.active:hover { background: none; }
.breadcrumb-sep { color: var(--text-muted); }

/* ============================================================
   CANVAS
   ============================================================ */
.canvas-wrap { flex: 1; overflow: hidden; position: relative; background: var(--bg); }
.tree-canvas { width: 100%; height: 100%; }
.tree-canvas svg { width: 100%; height: 100%; }

.empty-state {
    position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
    text-align: center; color: var(--text-secondary); padding: 20px;
}
.empty-icon { font-size: 48px; margin-bottom: 12px; }
.empty-state h3 { font-size: 18px; margin-bottom: 8px; }
.empty-state p { font-size: 14px; line-height: 1.5; }

.loading-skeleton {
    position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
    display: flex; gap: 24px;
}
.skeleton-node {
    width: 120px; height: 60px; background: var(--border); border-radius: var(--radius);
    animation: pulse 1.5s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: .4; } 50% { opacity: 1; } }

/* ============================================================
   BUTTONS
   ============================================================ */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    padding: 8px 16px; border: none; border-radius: var(--radius-sm); font-size: 13px;
    font-weight: 500; cursor: pointer; transition: all .15s; font-family: var(--font);
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-danger { background: var(--danger); color: white; }
.btn-danger:hover:not(:disabled) { background: var(--danger-hover); }
.btn-subtle { background: var(--bg); color: var(--text-secondary); border: 1px solid var(--border); }
.btn-subtle:hover:not(:disabled) { background: var(--border); }
.btn-block { width: 100%; }
.btn-sm { padding: 5px 10px; font-size: 12px; }

/* ============================================================
   FORMS
   ============================================================ */
.field {
    width: 100%; padding: 8px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm);
    font-size: 13px; font-family: var(--font); background: var(--surface); color: var(--text);
    transition: border-color .15s;
}
.field:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); }
.field::placeholder { color: var(--text-muted); }
select.field { cursor: pointer; }

.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 12px; font-weight: 500; color: var(--text-secondary); margin-bottom: 4px; }
.form-actions { display: flex; gap: 8px; margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); }

/* ============================================================
   OVERLAY & MODAL
   ============================================================ */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 40; display: none; }
.overlay.show { display: block; }

.modal-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 50;
    display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal {
    background: var(--surface); border-radius: var(--radius); width: 100%; max-width: 480px;
    box-shadow: var(--shadow-lg); max-height: 90vh; overflow-y: auto;
}
.modal-head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.modal-head h3 { font-size: 16px; font-weight: 600; }
.btn-close { background: none; border: none; font-size: 22px; cursor: pointer; color: var(--text-muted); padding: 4px; }
.btn-close:hover { color: var(--text); }
.modal-body { padding: 20px; }

/* ============================================================
   TOAST
   ============================================================ */
.toast-stack { position: fixed; bottom: 20px; right: 20px; z-index: 100; display: flex; flex-direction: column; gap: 8px; }
.toast {
    padding: 10px 16px; border-radius: var(--radius-sm); font-size: 13px; font-weight: 500;
    color: white; box-shadow: var(--shadow-lg); animation: slideIn .3s ease;
    max-width: 360px;
}
.toast.success { background: var(--success); }
.toast.warn { background: var(--warn); }
.toast.error { background: var(--danger); }
.toast.info { background: var(--primary); }
.toast.hide { animation: slideOut .3s ease forwards; }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
@keyframes slideOut { from { transform: translateX(0); opacity: 1; } to { transform: translateX(100%); opacity: 0; } }

/* ============================================================
   D3 TREE
   ============================================================ */
.d3-node { cursor: pointer; }
.d3-node circle { transition: all .2s; }
.d3-node:hover circle { filter: brightness(1.15); }
.d3-node.selected circle { stroke-width: 3px !important; stroke: var(--primary) !important; }
.d3-node.selected text { fill: var(--primary) !important; font-weight: 700 !important; }
.d3-node text { font-family: var(--font); pointer-events: none; }
.d3-link { fill: none; stroke-linecap: round; transition: stroke .2s, stroke-width .2s; }

/* ============================================================
   TOOLTIP
   ============================================================ */
.tooltip {
    position: absolute; background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius-sm); padding: 8px 12px; font-size: 12px;
    box-shadow: var(--shadow-lg); pointer-events: none; z-index: 60; max-width: 250px;
}
.tooltip .tt-name { font-weight: 600; font-size: 13px; }
.tooltip .tt-detail { color: var(--text-secondary); margin-top: 2px; }

/* ============================================================
   BADGE
   ============================================================ */
.badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 500; }
.badge-gold { background: var(--gold-bg); color: var(--gold); }
.badge-red { background: var(--danger-light); color: var(--danger); }
.badge-silver { background: var(--silver-bg); color: var(--silver); }

/* ============================================================
   LANGUAGE SWITCHER
   ============================================================ */
.lang-switch { display: flex; gap: 4px; }
.lang-btn {
    flex: 1; padding: 6px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm);
    background: var(--surface); font-size: 12px; font-weight: 600; cursor: pointer;
    color: var(--text-secondary); transition: all .15s;
}
.lang-btn:hover { background: var(--bg); }
.lang-btn.active { background: var(--primary); color: white; border-color: var(--primary); }

/* ============================================================
   VIEW-ONLY MODE
   ============================================================ */
.view-only-topbar {
    font-size: 12px; color: var(--warn); font-weight: 500; padding: 4px 12px;
    background: var(--warn-light); border-radius: 10px; white-space: nowrap;
}
.view-mode .btn-primary { opacity: 0.4; pointer-events: none; }
.view-mode .topbar-actions { display: none !important; }

/* ============================================================
   NODE ACTIONS PANEL
   ============================================================ */
.node-actions {
    position: absolute; z-index: 15; background: var(--surface);
    border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: var(--shadow-lg); padding: 8px; min-width: 180px; max-width: 260px;
    pointer-events: auto; animation: fadeUp .15s ease;
    max-height: calc(100% - 16px); overflow-y: auto;
}
@keyframes fadeUp { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
.node-actions-name {
    font-size: 12px; font-weight: 600; color: var(--primary); padding: 4px 8px 6px;
    border-bottom: 1px solid var(--border); margin-bottom: 6px; white-space: nowrap;
    overflow: hidden; text-overflow: ellipsis;
}
.node-actions-btns { display: flex; flex-direction: column; gap: 4px; }
.node-btn {
    padding: 5px 10px; border: 1px solid var(--slate); border-radius: var(--radius-sm);
    background: var(--slate); color: #f8fafc; font-size: 11px; font-weight: 500;
    cursor: pointer; transition: all .12s; font-family: var(--font); white-space: nowrap;
}
.node-btn:hover { background: var(--primary); border-color: var(--primary); }
.node-btn-edit { background: var(--slate); border-color: var(--slate); color: #f8fafc; }
.node-btn-edit:hover { background: var(--primary); border-color: var(--primary); }
.node-btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.node-btn-danger:hover { background: var(--danger-hover); border-color: var(--danger-hover); }
.node-btn-back { background: var(--slate); border-color: var(--slate); color: #f8fafc; }
.node-btn-back:hover { background: var(--primary); border-color: var(--primary); border-style: solid; }
.node-btn:disabled { opacity: .4; pointer-events: none; }

/* ============================================================
   USER MANAGEMENT PANEL
   ============================================================ */
.um-list { display: flex; flex-direction: column; gap: 8px; max-height: 400px; overflow-y: auto; }
.um-row {
    display: flex; justify-content: space-between; align-items: center; gap: 12px;
    padding: 10px 12px; background: var(--bg); border-radius: var(--radius); border: 1px solid var(--border);
}
.um-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.um-name { font-weight: 600; color: var(--text); font-size: 13px; }
.um-date { font-size: 11px; color: var(--text-muted); }
.um-actions { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.um-role-select { padding: 4px 8px; font-size: 12px; border-radius: var(--radius-sm); min-width: 80px; }
.um-delete { font-size: 11px; }
.um-empty { text-align: center; padding: 20px; color: var(--text-muted); font-size: 13px; }

.auth-switch-msg { text-align: center; font-size: 12px; color: var(--text-secondary); margin-top: 8px; }

/* ============================================================
   IMPORT FILE INPUT
   ============================================================ */
.import-label { cursor: pointer; }
.import-file-input {
    position: absolute; width: 0; height: 0; opacity: 0; pointer-events: none;
}

/* ============================================================
   RTL
   ============================================================ */
[dir="rtl"] .sidebar { border-right: none; border-left: 1px solid var(--border); }
[dir="rtl"] .sidebar-brand { background: linear-gradient(-135deg, var(--primary-light) 0%, var(--surface) 100%); }
[dir="rtl"] .topbar-actions { flex-direction: row-reverse; }
[dir="rtl"] .breadcrumb { direction: rtl; }
[dir="rtl"] .legend-item { flex-direction: row-reverse; }
[dir="rtl"] .form-actions { flex-direction: row-reverse; }
[dir="rtl"] .modal-head { flex-direction: row-reverse; }
[dir="rtl"] .topbar-left { flex-direction: row-reverse; }
[dir="rtl"] .topbar { flex-direction: row-reverse; }
[dir="rtl"] .topbar-actions { justify-content: flex-start; }

/* ============================================================
   URDU FONT
   ============================================================ */
html[lang="ur"] body { font-family: 'Noto Nastaliq Urdu', 'Inter', sans-serif; }
html[lang="ur"] .sidebar-brand h1 { font-family: 'Noto Nastaliq Urdu', serif; }
html[lang="ur"] .sidebar-brand p { font-family: 'Noto Nastaliq Urdu', serif; }
html[lang="ur"] .legend-item span[data-key] { font-family: 'Noto Nastaliq Urdu', serif; }
html[lang="ur"] .d3-node text { font-family: 'Noto Nastaliq Urdu', 'Inter', sans-serif; }
html[lang="ur"] .login-card-brand h1 { font-family: 'Noto Nastaliq Urdu', serif; }
html[lang="ur"] .login-card-brand p { font-family: 'Noto Nastaliq Urdu', serif; }

/* ============================================================
   MOBILE: tablet (max-width: 1024px)
   ============================================================ */
@media (max-width: 1024px) {
    :root { --sidebar-w: 260px; }
    .topbar-actions { gap: 4px; }
    .topbar-actions .btn-sm { padding: 5px 8px; font-size: 11px; }
}

/* ============================================================
   MOBILE: phone (max-width: 768px)
   ============================================================ */
@media (max-width: 768px) {
    /* Sidebar becomes slide-over */
    .sidebar {
        position: fixed; top: 0; bottom: 0; left: 0;
        width: 85vw; max-width: 320px; min-width: 0;
        transform: translateX(-100%); transition: transform .3s ease;
        z-index: 50; box-shadow: none;
    }
    .sidebar.open {
        transform: translateX(0); box-shadow: 4px 0 20px rgba(0,0,0,.2);
    }
    [dir="rtl"] .sidebar { left: auto; right: 0; transform: translateX(100%); }
    [dir="rtl"] .sidebar.open { transform: translateX(0); }

    .topbar { padding: 10px 12px; min-height: 48px; }
    .topbar-left { gap: 8px; }
    .topbar-actions { display: none !important; }

    .node-actions { min-width: 150px; max-width: 220px; }
    .node-actions-btns { gap: 3px; }
    .node-btn { font-size: 12px; padding: 8px 10px; }

    .modal-overlay { padding: 8px; align-items: flex-end; }
    .modal { max-width: 100%; max-height: 85vh; border-radius: var(--radius) var(--radius) 0 0; }
    .modal-body { padding: 16px; }
    .modal-head { padding: 12px 16px; }

    .toast-stack { left: 12px; right: 12px; bottom: 12px; }
    .toast { max-width: 100%; }

    .form-actions { flex-direction: column; }
    .form-actions .btn { width: 100%; }

    .empty-state { padding: 16px; width: 90%; }
    .empty-state h3 { font-size: 16px; }
    .empty-state p { font-size: 13px; }

    .breadcrumb { font-size: 12px; }

    /* Login card smaller on mobile */
    .login-card { padding: 28px 24px; margin: 16px; }
    .login-card-brand h1 { font-size: 20px; }
}

/* ============================================================
   MOBILE: small phone (max-width: 480px)
   ============================================================ */
@media (max-width: 480px) {
    .topbar { padding: 8px 10px; }
    .topbar-left { gap: 6px; }
    .menu-toggle { font-size: 20px; }

    .sidebar-brand { padding: 16px; }
    .sidebar-brand h1 { font-size: 16px; }
    .sidebar-section { padding: 10px 12px; }

    .btn { padding: 7px 12px; font-size: 12px; }
    .btn-sm { padding: 4px 8px; font-size: 11px; }
    .field { padding: 7px 10px; font-size: 12px; }

    .legend-item { font-size: 11px; gap: 6px; }
    .legend-dot { width: 10px; height: 10px; }

    .empty-icon { font-size: 36px; }
    body:not(.compact-mode) .d3-node text { font-size: 10px !important; }

    .lang-btn { padding: 5px 8px; font-size: 11px; }

    .login-card { padding: 24px 20px; }
    .login-card-icon { font-size: 40px; }
    .login-card-brand h1 { font-size: 18px; }
    .login-card-brand p { font-size: 12px; }

    .um-row { flex-direction: column; align-items: stretch; gap: 8px; }
    .um-actions { justify-content: space-between; }
}

/* ============================================================
   MOBILE: landscape mode
   ============================================================ */
@media (max-height: 500px) and (orientation: landscape) {
    .topbar { min-height: 40px; padding: 6px 12px; }
    .modal-overlay { align-items: center; }
    .modal { max-height: 90vh; border-radius: var(--radius); }
}

/* ============================================================
   TOUCH: improve tap targets
   ============================================================ */
@media (pointer: coarse) {
    .btn { min-height: 40px; }
    .btn-sm { min-height: 32px; }
    .field { min-height: 40px; }
    .menu-toggle { min-width: 44px; min-height: 44px; display: flex; align-items: center; justify-content: center; }
    .btn-close { min-width: 44px; min-height: 44px; display: flex; align-items: center; justify-content: center; }
    .breadcrumb-item { padding: 6px 8px; }
    .search-result-item { padding: 12px; }
    .lang-btn { min-height: 36px; }
}

/* ============================================================
   SAFE AREA: notched phones
   ============================================================ */
@supports (padding: env(safe-area-inset-bottom)) {
    .toast-stack { bottom: calc(20px + env(safe-area-inset-bottom)); }
    .modal-overlay { padding-bottom: env(safe-area-inset-bottom); }
    .login-page { padding-bottom: env(safe-area-inset-bottom); }
}

/* ============================================================
   PDF LOADING OVERLAY
   ============================================================ */
#pdfOverlay {
    position: fixed;
    inset: 0;
    z-index: 9999;
    background: rgba(15, 23, 42, 0.7);
    backdrop-filter: blur(4px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
}
#pdfOverlay.visible {
    opacity: 1;
    pointer-events: auto;
}
.pdf-overlay-spinner {
    width: 48px;
    height: 48px;
    border: 4px solid rgba(255,255,255,0.2);
    border-top-color: #facc15;
    border-radius: 50%;
    animation: pdf-spin 0.8s linear infinite;
}
@keyframes pdf-spin {
    to { transform: rotate(360deg); }
}
.pdf-overlay-text {
    color: #f8fafc;
    font-size: 14px;
    font-weight: 500;
    margin-top: 16px;
    letter-spacing: 0.3px;
}
