@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;800&family=Tajawal:wght@400;500;700&family=Almarai:wght@400;700;800&family=IBM+Plex+Sans+Arabic:wght@400;500;600;700&family=Readex+Pro:wght@400;500;600;700&family=Noto+Kufi+Arabic:wght@400;600;700&family=Amiri:wght@400;700&family=Poppins:wght@400;500;600;700&family=Inter:wght@400;500;600;700;800&family=Montserrat:wght@400;500;600;700&family=Playfair+Display:wght@500;700;800&family=Merriweather:wght@400;700&display=swap');

:root {
    --primary: #101828;
    --primary-soft: #1d2939;
    --accent: #4f46e5;
    --accent-dark: #4338ca;
    --accent-soft: #eef2ff;
    --bg: #f5f6fa;
    --surface: #ffffff;
    --border: #e5e7eb;
    --text: #101828;
    --muted: #667085;
    --muted-2: #98a2b3;
    --success: #059669;
    --success-soft: #ecfdf5;
    --warning: #d97706;
    --warning-soft: #fffbeb;
    --danger: #dc2626;
    --danger-soft: #fef2f2;
    --radius-sm: 8px;
    --radius: 12px;
    --radius-lg: 18px;
    --shadow-sm: 0 1px 2px rgba(16,24,40,.06);
    --shadow-md: 0 4px 14px rgba(16,24,40,.08);
    --shadow-lg: 0 16px 44px rgba(16,24,40,.16);
    --topbar-h: 56px;
    --bottomnav-h: 62px;
    --sidebar-w: 250px;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    overflow-x: hidden;
}
body[dir="rtl"] { font-family: 'Cairo', 'Inter', sans-serif; }
a { color: var(--accent); text-decoration: none; }
h1, h2, h3 { margin: 0 0 12px; letter-spacing: -.01em; }
h1 { font-size: 24px; font-weight: 800; }
h2 { font-size: 17px; font-weight: 700; }
p { line-height: 1.6; }
::selection { background: var(--accent-soft); color: var(--accent-dark); }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #d0d5dd; border-radius: 10px; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    padding: 10px 18px; border-radius: var(--radius-sm); border: 1px solid transparent;
    cursor: pointer; font-size: 14px; font-weight: 600; text-align: center; font-family: inherit;
    transition: transform .12s, box-shadow .12s, background .15s, border-color .15s, opacity .15s;
    white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); color: #fff; box-shadow: 0 1px 2px rgba(79,70,229,.35); }
.btn-primary:hover { background: var(--accent-dark); box-shadow: 0 6px 18px rgba(79,70,229,.35); }
.btn-secondary { background: #fff; color: var(--text); border-color: var(--border); }
.btn-secondary:hover { border-color: #c7cbd4; background: #fafafc; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #b91c1c; }
.btn-outline { background: transparent; border-color: var(--accent); color: var(--accent); }
.btn-outline:hover { background: var(--accent-soft); }
.btn-lg { padding: 14px 26px; font-size: 15px; }
.btn-sm { padding: 6px 12px; font-size: 12.5px; }
.btn-block { width: 100%; }
.btn-ai { background: linear-gradient(135deg,#7c3aed 0%, #4f46e5 100%); color: #fff; border: none; }
.btn-ai:hover { filter: brightness(1.05); box-shadow: 0 8px 22px rgba(124,58,237,.35); }

/* ---------- Landing / Auth ---------- */
.landing-body { background: linear-gradient(180deg, #f8f9ff 0%, var(--bg) 320px); }
.landing-header { display: flex; justify-content: space-between; align-items: center; padding: 18px 24px; background: rgba(255,255,255,.85); backdrop-filter: blur(8px); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20; }
.landing-brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 18px; }
.landing-logo { height: 34px; border-radius: 6px; }
.landing-hero { padding: 80px 20px 60px; text-align: center; }
.landing-hero-inner { max-width: 780px; margin: 0 auto; }
.landing-hero h1 { font-size: clamp(28px, 5vw, 42px); line-height: 1.15; }
.landing-subtitle { color: var(--muted); font-size: 16.5px; line-height: 1.7; margin: 18px 0 32px; }
.landing-features { display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; margin-top: 60px; text-align: left; }
.feature-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 26px; box-shadow: var(--shadow-sm); }
.feature-card::before { content: ''; display: block; width: 40px; height: 40px; border-radius: 10px; background: var(--accent-soft); margin-bottom: 14px; }
.feature-card h3 { font-size: 15.5px; }
.feature-card p { color: var(--muted); font-size: 13.5px; margin: 0; }
.landing-footer { text-align: center; padding: 30px; color: var(--muted-2); font-size: 13px; }

.auth-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; background: radial-gradient(circle at 20% 20%, #eef1ff 0%, var(--bg) 55%); }
.auth-card { background: var(--surface); padding: 34px 26px; border-radius: var(--radius-lg); width: 100%; max-width: 400px; box-shadow: var(--shadow-lg); border: 1px solid var(--border); }
.auth-card h2 { font-size: 21px; }
.auth-subtitle { color: var(--muted); font-size: 13.5px; margin-bottom: 22px; line-height: 1.6; }
.auth-links { display: flex; justify-content: space-between; margin-top: 18px; font-size: 13px; flex-wrap: wrap; gap: 8px; }
.auth-links a { color: var(--muted); font-weight: 500; }
.auth-links a:hover { color: var(--accent); }

form label { display: block; font-size: 13px; font-weight: 600; margin: 14px 0 6px; color: #344054; }
form input[type=text], form input[type=email], form input[type=password],
form input[type=date], form input[type=number], form input[type=file], form select, form textarea {
    width: 100%; padding: 11px 13px; border: 1.5px solid var(--border); border-radius: var(--radius-sm);
    font-size: 14px; font-family: inherit; background: #fff; color: var(--text);
    transition: border-color .15s, box-shadow .15s;
}
form input:focus, form select:focus, form textarea:focus {
    outline: none; border-color: var(--accent); box-shadow: 0 0 0 3.5px var(--accent-soft);
}
form input[type=color] { width: 60px; height: 40px; padding: 2px; border: 1.5px solid var(--border); border-radius: var(--radius-sm); cursor: pointer; }
form input[type=range] { width: 100%; }
form small { color: var(--muted-2); font-size: 12px; }

.alert { padding: 12px 14px; border-radius: var(--radius-sm); font-size: 13.5px; margin-bottom: 16px; border: 1px solid transparent; line-height: 1.6; }
.alert-error { background: var(--danger-soft); color: #991b1b; border-color: #fecaca; }
.alert-success { background: var(--success-soft); color: #065f46; border-color: #a7f3d0; }
.alert-info { background: var(--accent-soft); color: #3730a3; border-color: #c7d2fe; }
.alert a { word-break: break-all; }

/* ==========================================================
   APP SHELL – MOBILE-FIRST
========================================================== */
.app-shell { display: block; min-height: 100vh; }

/* Mobile topbar (default visible on all sizes; hidden on desktop) */
.mobile-topbar {
    display: flex; align-items: center; gap: 10px;
    height: var(--topbar-h); padding: 0 14px;
    background: var(--surface); border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 60; box-shadow: var(--shadow-sm);
}
.hamburger-btn {
    display: flex; align-items: center; justify-content: center;
    width: 40px; height: 40px; border-radius: var(--radius-sm);
    border: 1px solid var(--border); background: #fff; color: var(--text);
    cursor: pointer; flex-shrink: 0; -webkit-tap-highlight-color: transparent;
}
.hamburger-btn:active { background: #f2f3f7; transform: scale(.96); }
.mobile-topbar-brand { display: flex; align-items: center; gap: 8px; font-weight: 800; font-size: 15px; overflow: hidden; min-width: 0; flex: 1; }
.mobile-topbar-brand img { height: 26px; border-radius: 5px; flex-shrink: 0; }
.mobile-topbar-brand span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mobile-topbar-spacer { flex: 0; }
.mobile-topbar-avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 14px; flex-shrink: 0; }

/* Off-canvas sidebar (drawer) - hidden by default; opens via .is-open */
.app-sidebar {
    position: fixed; top: 0; left: 0; height: 100dvh; width: var(--sidebar-w);
    background: linear-gradient(180deg, var(--primary) 0%, var(--primary-soft) 100%);
    color: #fff; display: flex; flex-direction: column;
    padding: 14px 12px 14px; z-index: 100;
    transform: translateX(-105%); transition: transform .28s cubic-bezier(.4,0,.2,1);
    box-shadow: var(--shadow-lg); overflow-y: auto; overscroll-behavior: contain;
}
.app-sidebar.is-open { transform: translateX(0); }
body[dir="rtl"] .app-sidebar { left: auto; right: 0; transform: translateX(105%); }
body[dir="rtl"] .app-sidebar.is-open { transform: translateX(0); }

.app-brand { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 6px 8px 18px; font-weight: 800; font-size: 15px; }
.app-brand > div { display: flex; align-items: center; gap: 8px; overflow: hidden; min-width: 0; }
.app-brand span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.app-brand-logo { height: 30px; border-radius: 6px; flex-shrink: 0; }
.sidebar-close-btn { display: flex; align-items: center; justify-content: center; width: 32px; height: 32px; border-radius: 8px; background: rgba(255,255,255,.08); color: #fff; border: none; cursor: pointer; flex-shrink: 0; }
.sidebar-close-btn:active { background: rgba(255,255,255,.18); }

.nav-link {
    display: flex; align-items: center; gap: 11px; color: #c3cbe6;
    padding: 11px 12px; border-radius: var(--radius-sm); margin-bottom: 3px;
    font-size: 14px; font-weight: 500; position: relative;
    transition: background .15s, color .15s;
}
.nav-link svg { flex-shrink: 0; opacity: .85; }
.nav-link:hover, .nav-link:active { background: rgba(255,255,255,.08); color: #fff; }
.nav-link.active { background: rgba(255,255,255,.12); color: #fff; font-weight: 600; }
.nav-spacer { flex: 1; }

.sidebar-lang { padding: 10px 8px 12px; border-top: 1px solid rgba(255,255,255,.08); margin-top: 8px; }
.sidebar-lang label { display: block; font-size: 11px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: #8a95c1; margin-bottom: 6px; }
.sidebar-lang select {
    width: 100%; background: rgba(255,255,255,.08); color: #fff; border: 1px solid rgba(255,255,255,.14);
    padding: 9px 10px; border-radius: var(--radius-sm); font-size: 13.5px; font-family: inherit;
}
.sidebar-lang select option { color: #111; }

.nav-user { display: flex; align-items: center; gap: 10px; padding: 12px 8px 6px; font-size: 13px; color: #aab4d6; border-top: 1px solid rgba(255,255,255,.08); margin-top: 4px; }
.nav-user-avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 13px; flex-shrink: 0; }
.nav-user-name { color: #e4e7f5; font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-logout { color: #ffb4b4 !important; }
.nav-logout:hover { background: rgba(239,68,68,.14) !important; color: #ff8f8f !important; }

.sidebar-overlay {
    position: fixed; inset: 0; background: rgba(16,24,40,.55);
    opacity: 0; pointer-events: none; transition: opacity .22s ease;
    z-index: 90; backdrop-filter: blur(2px);
}
.sidebar-overlay.is-visible { opacity: 1; pointer-events: auto; }

/* Main content – mobile default */
.app-content { padding: 16px 14px calc(var(--bottomnav-h) + 22px); max-width: 100%; }
.page-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 18px; gap: 12px; flex-wrap: wrap; }
.page-header h1 { margin: 0; font-size: 21px; }
.page-header .btn { font-size: 13.5px; padding: 9px 14px; }

/* Bottom nav (mobile) */
.mobile-bottom-nav {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0;
    height: calc(var(--bottomnav-h) + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: var(--surface); border-top: 1px solid var(--border);
    z-index: 55; box-shadow: 0 -6px 20px rgba(16,24,40,.08);
}
.bottom-nav-link {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 3px; color: var(--muted-2); font-size: 10.5px; font-weight: 600; position: relative;
    -webkit-tap-highlight-color: transparent;
}
.bottom-nav-link svg { width: 20px; height: 20px; }
.bottom-nav-link.active { color: var(--accent); }
.bottom-nav-fab { margin-top: -20px; }
.bottom-nav-fab .fab-circle {
    width: 44px; height: 44px; border-radius: 50%; background: var(--accent); color: #fff;
    display: flex; align-items: center; justify-content: center;
    box-shadow: 0 8px 18px rgba(79,70,229,.45); margin-bottom: 2px;
}

/* ---------- Stats ---------- */
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin-bottom: 22px; }
.stat-card {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 14px; box-shadow: var(--shadow-sm);
}
.stat-card-wide { grid-column: 1 / -1; background: linear-gradient(135deg, var(--primary) 0%, var(--primary-soft) 100%); border: none; }
.stat-card-wide .stat-label { color: #aab4d6; }
.stat-card-wide .stat-value { color: #fff; }
.stat-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; font-weight: 700; }
.stat-value { font-size: 20px; font-weight: 800; margin-top: 6px; letter-spacing: -.02em; }

/* ---------- Panels / Tables ---------- */
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; box-shadow: var(--shadow-sm); }
.panel-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; gap: 10px; flex-wrap: wrap; }
.panel-header h2 { margin: 0; }
.panel-header a { font-size: 13.5px; font-weight: 600; }

.data-table { width: 100%; border-collapse: collapse; }
.data-table th { text-align: left; font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 700; padding: 10px 8px; border-bottom: 1.5px solid var(--border); }
.data-table td { padding: 12px 8px; border-bottom: 1px solid #f0f1f5; font-size: 14px; vertical-align: middle; }
.data-table tbody tr:last-child td { border-bottom: none; }
.row-actions { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.row-actions a, .row-actions button {
    font-size: 12.5px; font-weight: 600; padding: 5px 9px; border-radius: 7px; color: var(--muted);
    background: transparent; border: none; cursor: pointer; font-family: inherit; transition: background .12s, color .12s;
}
.row-actions a:hover { background: var(--accent-soft); color: var(--accent-dark); }
.empty-state { text-align: center; color: var(--muted); padding: 40px 20px !important; font-size: 14px; }

/* ---------- Badges ---------- */
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 4px 11px; border-radius: 20px; font-size: 12px; font-weight: 700; border: 1px solid transparent; }
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge-draft { background: #f2f4f7; color: #475467; }
.badge-sent { background: var(--accent-soft); color: #3730a3; }
.badge-paid { background: var(--success-soft); color: #047857; }
.badge-overdue { background: var(--danger-soft); color: #b91c1c; }
.badge-cancelled { background: #f2f4f7; color: #98a2b3; }
select.badge { appearance: none; -webkit-appearance: none; cursor: pointer; padding-right: 11px; }

/* ---------- Filters ---------- */
.filter-bar { display: flex; flex-direction: column; gap: 10px; margin-bottom: 18px; background: var(--surface); padding: 14px; border-radius: var(--radius); border: 1px solid var(--border); box-shadow: var(--shadow-sm); }
.filter-bar input, .filter-bar select { padding: 10px 12px; border: 1.5px solid var(--border); border-radius: var(--radius-sm); font-size: 13.5px; font-family: inherit; background: #fff; width: 100%; }
.filter-bar-btns { display: flex; gap: 8px; }
.filter-bar-btns .btn { flex: 1; }

/* ---------- Settings Form ---------- */
.settings-form { display: block; }
.form-grid { display: grid; grid-template-columns: 1fr; gap: 6px 20px; }
.form-group-full { grid-column: 1 / -1; }
.logo-preview { max-height: 60px; margin-top: 8px; display: block; border-radius: 6px; border: 1px solid var(--border); padding: 4px; background: #fff; }
.panel h2 { padding-bottom: 12px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }

/* Slider readout */
.range-with-value { display: flex; align-items: center; gap: 12px; }
.range-with-value input[type=range] { flex: 1; }
.range-with-value .range-value { font-weight: 700; font-size: 13px; color: var(--accent-dark); min-width: 46px; text-align: right; }

/* ---------- Invoice builder (form + preview) ---------- */
.invoice-builder { display: grid; grid-template-columns: 1fr; gap: 18px; align-items: start; }
.invoice-form-col { padding-right: 0; }
.invoice-preview-col { }
.preview-wrap { background: #dde1ea; padding: 12px; border-radius: var(--radius); display: flex; justify-content: center; overflow-x: auto; }
.preview-toolbar { display: flex; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; align-items: center; }

.form-section { margin-bottom: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 0 14px; overflow: hidden; }
.form-section summary { cursor: pointer; font-weight: 700; padding: 14px 2px; font-size: 14.5px; list-style: none; user-select: none; display: flex; align-items: center; }
.form-section summary::-webkit-details-marker { display: none; }
.form-section summary::before { content: '▸'; margin-right: 10px; color: var(--accent); font-size: 12px; transition: transform .15s; }
body[dir="rtl"] .form-section summary::before { margin-right: 0; margin-left: 10px; }
.form-section[open] summary::before { transform: rotate(90deg); }
.form-section[open] summary { border-bottom: 1px solid var(--border); }
.form-section-body { padding: 14px 2px; }
.section-toolbar { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }

.item-row-actions { text-align: right; margin-top: 8px; }
.link-btn { background: none; border: none; color: var(--accent); cursor: pointer; font-size: 13px; padding: 4px 0; font-weight: 600; font-family: inherit; }
.link-btn:hover { text-decoration: underline; }
.link-btn-danger { color: var(--danger); }
.inline-fields { display: grid; grid-template-columns: 1fr; gap: 0 16px; }
.calc-readout { background: #f7f8fc; border: 1px dashed var(--border); border-radius: var(--radius-sm); padding: 10px 12px; font-size: 13px; margin-top: 8px; color: var(--muted); word-break: break-word; }
#itemsContainer .panel { padding: 12px; margin-bottom: 10px; background: #fafbfd; box-shadow: none; }

.form-actions-bar { display: flex; gap: 10px; flex-wrap: wrap; }
.form-actions-bar .btn { flex: 1; min-width: 140px; }

/* ---------- Invoice A4 render ---------- */
.invoice-page {
    background: #fff; width: 210mm; min-height: 297mm; padding: 14mm;
    box-shadow: 0 4px 24px rgba(16,24,40,.14); font-size: 13px; line-height: 1.5; color: #222;
}
.invoice-page h1 { color: var(--inv-primary, var(--primary)); font-size: 26px; margin: 0; }
.invoice-custom-header { text-align: center; font-weight: 700; margin-bottom: 14px; color: var(--inv-primary, var(--primary)); }
.invoice-custom-footer { text-align: center; color: #888; font-size: 11px; margin-top: 30px; border-top: 1px solid #eee; padding-top: 10px; }
.invoice-top { display: flex; justify-content: space-between; margin-bottom: 24px; gap: 16px; flex-wrap: wrap; }
.invoice-logo { max-height: 55px; margin-bottom: 8px; }
.invoice-issuer-name { font-weight: 700; font-size: 15px; }
.invoice-issuer-meta { color: #555; font-size: 12px; margin-top: 4px; }
.invoice-meta { text-align: right; }
.invoice-meta-table { margin-left: auto; margin-top: 8px; }
.invoice-meta-table td { padding: 2px 0 2px 14px; font-size: 12px; text-align: right; }
.invoice-meta-table td:first-child { color: #888; }
.section-label { text-transform: uppercase; font-size: 11px; letter-spacing: .05em; color: #888; margin-bottom: 6px; font-weight: 700; }
.invoice-parties { margin-bottom: 20px; }
.party-name { font-weight: 700; }
.party-meta { color: #555; font-size: 12px; margin-top: 4px; }
.invoice-items { width: 100%; border-collapse: collapse; margin-bottom: 16px; }
.invoice-items th { background: var(--inv-primary, var(--primary)); color: #fff; text-align: left; padding: 9px 10px; font-size: 12px; }
.invoice-items th:first-child { border-radius: 6px 0 0 0; }
.invoice-items th:last-child { border-radius: 0 6px 0 0; }
.invoice-items td { padding: 10px; border-bottom: 1px solid #eee; vertical-align: top; }
.text-right { text-align: right; }
.invoice-totals-wrap { display: flex; justify-content: flex-end; margin-bottom: 20px; }
.invoice-totals { width: 280px; }
.invoice-totals td { padding: 5px 0; font-size: 13px; }
.invoice-totals .total-row td { font-weight: 700; font-size: 15px; border-top: 2px solid var(--inv-primary, var(--primary)); padding-top: 8px; color: var(--inv-primary, var(--primary)); }
.invoice-bottom-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 20px; }
.invoice-payment div, .invoice-notes div { font-size: 12px; margin-bottom: 2px; }
.invoice-terms { margin-bottom: 30px; font-size: 12px; color: #444; }
.invoice-signature { width: 220px; margin-left: auto; text-align: center; }
.signature-img { max-height: 60px; margin-bottom: 6px; }
.signature-space { height: 60px; border-bottom: 1px solid #999; margin-bottom: 6px; }
.signature-name { font-weight: 700; font-size: 13px; border-top: 1px solid #999; padding-top: 4px; }
.signature-title { font-size: 11px; color: #888; }
/* ---------- Template: Official / Formal (Arabic-style document) ---------- */
.template-modern .invoice-items th { background: var(--inv-accent, var(--accent)); }
.template-modern .invoice-page { border-top: 8px solid var(--inv-accent, var(--accent)); }
.template-modern h1 { color: var(--inv-accent, var(--accent)); }

/* ---------- Template EGX New (dokumen resmi ringkas 1 halaman) ---------- */
.template-egxnew, .template-official { border-top: none; color: #1f2937; padding: 10mm 11mm; }
.template-egxnew h1, .template-official h1 { font-size: 19px; text-align: center; color: #111827; margin: 4px 0 6px; letter-spacing: 0; font-weight: 800; line-height: 1.25; }
.template-egxnew .official-brand, .template-official .official-brand { text-align: center; font-size: 11px; letter-spacing: .22em; font-weight: 700; color: #374151; margin-bottom: 4px; }
.template-egxnew .official-sub, .template-official .official-sub { text-align: center; font-size: 10.5px; color: #6b7280; margin-bottom: 8px; }
.template-egxnew .official-badges, .template-official .official-badges { text-align: center; margin-bottom: 8px; display: flex; justify-content: center; gap: 6px; flex-wrap: wrap; }
.template-egxnew .official-badges .b-pending, .template-official .official-badges .b-pending { background: #111827; color: #fff; padding: 3px 12px; font-size: 10px; font-weight: 700; letter-spacing: .06em; border-radius: 3px; }
.template-egxnew .official-badges .b-vip, .template-official .official-badges .b-vip { background: #6d28d9; color: #fff; padding: 3px 12px; font-size: 10px; font-weight: 700; letter-spacing: .06em; border-radius: 3px; }
.template-egxnew .official-divider, .template-official .official-divider { border: 0; border-top: 1px solid #d1d5db; margin: 6px 0 12px; }
.egx-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; align-items: start; }
.template-egxnew .official-section, .template-official .official-section { margin-bottom: 12px; break-inside: avoid; page-break-inside: avoid; }
.template-egxnew .official-section h2, .template-official .official-section h2 { font-size: 12.5px; font-weight: 800; color: #111827; margin: 0 0 5px; padding-bottom: 3px; border-bottom: 2px solid #111827; display: inline-block; }
.template-egxnew .official-kv, .template-official .official-kv { width: 100%; border-collapse: collapse; border: 1px solid #d1d5db; }
.template-egxnew .official-kv tr, .template-official .official-kv tr { border-bottom: 1px solid #e5e7eb; }
.template-egxnew .official-kv tr:last-child, .template-official .official-kv tr:last-child { border-bottom: none; }
.template-egxnew .official-kv td, .template-official .official-kv td { padding: 5px 9px; font-size: 10.5px; vertical-align: middle; line-height: 1.35; }
.template-egxnew .official-kv td.k, .template-official .official-kv td.k { background: #f3f4f6; font-weight: 700; color: #374151; width: 42%; border-left: 1px solid #d1d5db; }
.template-egxnew .official-kv td.v, .template-official .official-kv td.v { color: #111827; word-break: break-word; }
.template-egxnew .official-kv td.total-k, .template-official .official-kv td.total-k { background: #111827; color: #fff; }
.template-egxnew .official-kv td.total-v, .template-official .official-kv td.total-v { background: #f9fafb; font-weight: 800; }
.template-egxnew[dir="rtl"] .official-kv td.k, .template-official[dir="rtl"] .official-kv td.k { border-left: none; border-right: 1px solid #d1d5db; }
.template-egxnew .official-note, .template-official .official-note { font-size: 9.7px; line-height: 1.5; color: #374151; margin-top: 4px; text-align: justify; }
.template-egxnew .official-note b, .template-official .official-note b, .template-egxnew .official-note strong, .template-official .official-note strong { color: #111827; }
.template-egxnew .official-footer, .template-official .official-footer { border-top: 1px solid #d1d5db; margin-top: 12px; padding-top: 8px; font-size: 9.5px; color: #6b7280; line-height: 1.65; }
.template-egxnew .official-footer .ref, .template-official .official-footer .ref { color: #374151; font-weight: 700; }
.template-egxnew .invoice-signature, .template-official .invoice-signature { margin-top: 14px; }

/* ---------- Public page ---------- */
.public-page { background: var(--bg); min-height: 100vh; padding: 20px 12px; }
.public-toolbar { max-width: 210mm; margin: 0 auto 14px; display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.public-invoice-wrap { display: flex; justify-content: center; }

/* ---------- Success box ---------- */
.success-box { background: var(--surface); border-radius: var(--radius-lg); padding: 26px 20px; max-width: 480px; margin: 32px auto; text-align: center; box-shadow: var(--shadow-lg); border: 1px solid var(--border); }
.success-box .icon-ok { font-size: 34px; color: #fff; margin: 0 auto 14px; width: 60px; height: 60px; border-radius: 50%; background: var(--success); display: flex; align-items: center; justify-content: center; }
.share-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 20px; }
.share-actions .btn { width: 100%; }
.copy-input-row { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
.copy-input-row input { flex: 1; background: #f7f8fc; min-width: 200px; }

/* ---------- Data table cards (mobile) ---------- */
@media (max-width: 640px) {
    .data-table thead { display: none; }
    .data-table, .data-table tbody, .data-table tr, .data-table td { display: block; width: 100%; }
    .data-table tbody tr {
        margin-bottom: 12px; border: 1px solid var(--border); border-radius: var(--radius);
        padding: 4px 14px; background: #fff; box-shadow: var(--shadow-sm);
    }
    .data-table td {
        display: flex; justify-content: space-between; align-items: center; gap: 10px;
        padding: 10px 0; border-bottom: 1px solid #f1f2f6; text-align: right;
    }
    .data-table td:last-child { border-bottom: none; }
    .data-table td::before {
        content: attr(data-label); font-weight: 700; color: var(--muted); font-size: 11.5px;
        text-transform: uppercase; letter-spacing: .04em; text-align: left; margin-right: 12px; flex-shrink: 0;
    }
    .data-table td.row-actions { flex-direction: column; align-items: stretch; text-align: left; }
    .data-table td.row-actions::before { margin-bottom: 8px; }
    .row-actions { justify-content: flex-start; }
    .row-actions a, .row-actions button { background: #f5f6fa; }
    .invoice-bottom-grid { grid-template-columns: 1fr; }
}

/* ---------- Tablet ---------- */
@media (min-width: 640px) {
    .stat-grid { grid-template-columns: repeat(3, 1fr); }
    .stat-card-wide { grid-column: span 3; }
    .form-grid, .inline-fields { grid-template-columns: 1fr 1fr; }
    .form-group-full { grid-column: 1 / -1; }
    .filter-bar { flex-direction: row; flex-wrap: wrap; align-items: center; }
    .filter-bar input, .filter-bar select { width: auto; flex: 1 1 160px; min-width: 140px; }
    .filter-bar-btns { flex: 0 0 auto; }
    .filter-bar-btns .btn { flex: 0 0 auto; }
    .app-content { padding: 22px 20px calc(var(--bottomnav-h) + 22px); }
    .landing-features { grid-template-columns: 1fr 1fr; }
}

/* ---------- Desktop (≥ 960px): fixed sidebar layout ---------- */
@media (min-width: 960px) {
    .app-shell { display: flex; }
    .mobile-topbar { display: none; }
    .mobile-bottom-nav { display: none; }
    .sidebar-overlay { display: none; }
    .app-sidebar {
        position: sticky; top: 0; height: 100vh; transform: none !important;
        box-shadow: none; border-right: 1px solid rgba(255,255,255,.05);
        flex-shrink: 0;
    }
    body[dir="rtl"] .app-sidebar { border-right: none; border-left: 1px solid rgba(255,255,255,.05); }
    .sidebar-close-btn { display: none; }
    .app-content { flex: 1; padding: 30px 40px; max-width: 1280px; width: 100%; margin: 0 auto; }
    .page-header h1 { font-size: 24px; }
    .stat-grid { grid-template-columns: repeat(6, 1fr); }
    .stat-card-wide { grid-column: span 2; }
    .invoice-builder { grid-template-columns: 1fr 1fr; }
    .invoice-form-col { max-height: calc(100vh - 40px); overflow-y: auto; padding-right: 6px; }
    .invoice-preview-col { position: sticky; top: 20px; }
    .landing-features { grid-template-columns: repeat(3, 1fr); }
}

/* Prevent horizontal overflow on very small screens */
@media (max-width: 400px) {
    .app-content { padding-left: 12px; padding-right: 12px; }
    .panel { padding: 14px 12px; }
    .form-section { padding: 0 10px; }
    h1 { font-size: 19px; }
    .btn { padding: 9px 14px; font-size: 13.5px; }
    .invoice-page { padding: 8mm; font-size: 12px; }
    .invoice-totals { width: 100%; }
}

/* Mobile: invoice preview scales to fit */
@media screen and (max-width: 959px) {
    .invoice-page { width: 100%; min-height: auto; box-shadow: 0 2px 10px rgba(0,0,0,.08); }
    .preview-wrap { padding: 8px; }
    /* EGX New: susun bertumpuk di layar HP + font sedikit lebih besar agar terbaca */
    .egx-grid { grid-template-columns: 1fr; gap: 0; }
    .template-egxnew, .template-official { padding: 6mm 5mm; }
    .template-egxnew .official-kv td, .template-official .official-kv td { font-size: 12px; padding: 6px 10px; }
    .template-egxnew .official-note, .template-official .official-note { font-size: 11.5px; line-height: 1.6; }
    .template-egxnew .official-section h2, .template-official .official-section h2 { font-size: 14px; }
    .template-egxnew h1, .template-official h1 { font-size: 18px; }
}

/* ---------- AI Modal ---------- */
.ai-modal-overlay {
    position: fixed; inset: 0; background: rgba(16,24,40,.55); z-index: 200;
    display: none; align-items: flex-end; justify-content: center;
    backdrop-filter: blur(3px);
}
.ai-modal-overlay.is-visible { display: flex; }
.ai-modal {
    background: #fff; width: 100%; max-width: 520px; border-radius: 20px 20px 0 0;
    padding: 20px; box-shadow: var(--shadow-lg); max-height: 92vh; overflow-y: auto;
    animation: aiSlideUp .28s cubic-bezier(.4,0,.2,1);
}
@keyframes aiSlideUp { from { transform: translateY(30px); opacity: .7; } to { transform: translateY(0); opacity: 1; } }
@media (min-width: 640px) {
    .ai-modal-overlay { align-items: center; }
    .ai-modal { border-radius: 18px; max-height: 86vh; }
}
.ai-modal h3 { margin: 0 0 4px; font-size: 17px; }
.ai-modal .ai-sub { color: var(--muted); font-size: 13px; margin-bottom: 14px; line-height: 1.55; }
.ai-modal textarea { min-height: 130px; resize: vertical; }
.ai-modal-actions { display: flex; gap: 10px; margin-top: 14px; flex-wrap: wrap; }
.ai-modal-actions .btn { flex: 1; min-width: 130px; }
.ai-status { font-size: 13px; margin-top: 10px; min-height: 20px; }
.ai-status.err { color: var(--danger); }
.ai-status.ok { color: var(--success); }

/* Sidebar bg image field preview */
.bg-preview { max-height: 100px; margin-top: 8px; border-radius: 8px; border: 1px solid var(--border); display: block; }
.bg-remove-row { display: flex; align-items: center; gap: 8px; margin-top: 8px; font-size: 13px; color: var(--muted); }

/* ---------- Print ---------- */
@media print {
    body * { visibility: hidden; }
    #printArea, #printArea * { visibility: visible; }
    #printArea { position: absolute; top: 0; left: 0; width: 100%; }
    .invoice-page { box-shadow: none; margin: 0; width: 100%; }
    .no-print { display: none !important; }
    body::before { display: none !important; }
    /* Background pada invoice-page tetap tampil saat print/PDF */
    .invoice-page::before { visibility: visible !important; display: block !important; }
    @page { size: A4; margin: 0; }
}
