/* ─── CSS Variables ─────────────────────────────────────── */
:root {
    --primary: #4f46e5;
    --primary-light: #818cf8;
    --primary-dark: #3730a3;
    --secondary: #7c3aed;
    --bg: #f8fafc;
    --sidebar-bg: #1e293b;
    --text: #1e293b;
    --text-light: #64748b;
    --btn: #4f46e5;
    --radius: 8px;
    --font: 'Inter', sans-serif;
    --card-bg: #ffffff;
    --border: #e2e8f0;
    --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
    --shadow-lg: 0 10px 15px -3px rgba(0,0,0,.1);
    --success: #10b981;
    --danger: #ef4444;
    --warning: #f59e0b;
    --info: #3b82f6;
    --sidebar-text: #cbd5e1;
    --sidebar-hover: #334155;
    --sidebar-active: var(--primary);
}

[data-theme="dark"] {
    --bg: #0f172a;
    --text: #e2e8f0;
    --text-light: #94a3b8;
    --card-bg: #1e293b;
    --border: #334155;
    --shadow: 0 1px 3px rgba(0,0,0,.3);
    --shadow-lg: 0 10px 15px -3px rgba(0,0,0,.4);
    --sidebar-bg: #0f172a;
    --sidebar-text: #94a3b8;
    --sidebar-hover: #1e293b;
}

/* ─── Reset ─────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 15px; scroll-behavior: smooth; }
body { font-family: var(--font); background: var(--bg); color: var(--text); line-height: 1.6; min-height: 100vh; }
a { color: var(--primary); text-decoration: none; }
ul { list-style: none; }
img { max-width: 100%; height: auto; }

/* ─── Layout ────────────────────────────────────────────── */
.app-wrapper { display: flex; min-height: 100vh; }
.sidebar {
    width: 260px; background: var(--sidebar-bg); color: var(--sidebar-text);
    display: flex; flex-direction: column; position: fixed; top: 0; left: 0; bottom: 0;
    z-index: 100; transition: transform .3s ease; overflow-y: auto;
}
.sidebar-header {
    padding: 20px; border-bottom: 1px solid rgba(255,255,255,.1);
}
.logo { display: flex; align-items: center; gap: 10px; color: #fff; font-size: 1.2rem; font-weight: 700; }
.logo i { font-size: 1.5rem; color: var(--primary-light); }
.sidebar-nav { flex: 1; padding: 10px 0; }
.nav-label { padding: 15px 20px 5px; font-size: .7rem; text-transform: uppercase; letter-spacing: 1px; opacity: .5; color: #fff; }
.sidebar-nav ul li a {
    display: flex; align-items: center; gap: 12px; padding: 10px 20px;
    color: var(--sidebar-text); font-size: .9rem; transition: all .2s; border-left: 3px solid transparent;
}
.sidebar-nav ul li a:hover { background: var(--sidebar-hover); color: #fff; }
.sidebar-nav ul li a.active { background: var(--sidebar-hover); color: #fff; border-left-color: var(--primary-light); }
.sidebar-nav ul li a i { width: 20px; text-align: center; font-size: 1.1rem; }
.sidebar-footer {
    padding: 15px 20px; border-top: 1px solid rgba(255,255,255,.1);
    display: flex; align-items: center; justify-content: space-between;
}
.user-info { display: flex; align-items: center; gap: 10px; font-size: .85rem; }
.user-info i { font-size: 1.5rem; }
.logout-btn { color: var(--sidebar-text); font-size: 1.2rem; }
.logout-btn:hover { color: var(--danger); }

.main-content {
    flex: 1; margin-left: 260px; display: flex; flex-direction: column; min-height: 100vh;
}
.topbar {
    background: var(--card-bg); padding: 15px 30px; display: flex; align-items: center;
    gap: 15px; border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 50;
}
.topbar h2 { font-size: 1.1rem; font-weight: 600; flex: 1; }
.topbar-actions { display: flex; gap: 10px; }
.sidebar-toggle { display: none; background: none; border: none; font-size: 1.3rem; color: var(--text); cursor: pointer; }

.content-area { flex: 1; padding: 25px 30px; }
.app-footer {
    background: var(--card-bg); border-top: 1px solid var(--border);
    padding: 12px 30px; text-align: center; font-size: .8rem; color: var(--text-light);
}

/* ─── Login ─────────────────────────────────────────────── */
.login-wrapper {
    display: flex; align-items: center; justify-content: center; min-height: 100vh;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
}
.login-card {
    background: var(--card-bg); border-radius: var(--radius); padding: 40px;
    width: 100%; max-width: 400px; box-shadow: var(--shadow-lg);
}
.login-header { text-align: center; margin-bottom: 30px; }
.login-header i { font-size: 3rem; color: var(--primary); margin-bottom: 10px; }
.login-header h1 { font-size: 1.5rem; font-weight: 700; }
.login-header p { color: var(--text-light); font-size: .9rem; }
.login-form .form-group { margin-bottom: 20px; }
.login-form .form-group label { display: flex; align-items: center; gap: 8px; font-weight: 500; margin-bottom: 5px; font-size: .85rem; }

/* ─── Page Header ───────────────────────────────────────── */
.page-header {
    display: flex; align-items: center; justify-content: space-between;
    margin-bottom: 25px; flex-wrap: wrap; gap: 10px;
}
.page-header h1 { font-size: 1.4rem; font-weight: 700; display: flex; align-items: center; gap: 10px; }
.page-header h1 i { color: var(--primary); }

/* ─── Cards ─────────────────────────────────────────────── */
.card {
    background: var(--card-bg); border-radius: var(--radius); box-shadow: var(--shadow);
    margin-bottom: 20px; border: 1px solid var(--border);
}
.card-header {
    padding: 15px 20px; border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between;
}
.card-header h3 { font-size: 1rem; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.card-body { padding: 20px; }
.card-body.p-0 { padding: 0; }

/* ─── Stats Grid ────────────────────────────────────────── */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px; margin-bottom: 30px; }
.stat-card {
    background: var(--card-bg); border-radius: var(--radius); box-shadow: var(--shadow);
    padding: 20px; display: flex; align-items: center; gap: 15px; border: 1px solid var(--border);
    transition: transform .2s, box-shadow .2s;
}
.stat-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.stat-icon {
    width: 50px; height: 50px; border-radius: var(--radius);
    display: flex; align-items: center; justify-content: center; font-size: 1.4rem; color: #fff;
}
.stat-primary .stat-icon { background: var(--primary); }
.stat-info .stat-icon { background: var(--info); }
.stat-success .stat-icon { background: var(--success); }
.stat-danger .stat-icon { background: var(--danger); }
.stat-warning .stat-icon { background: var(--warning); }
.stat-secondary .stat-icon { background: var(--secondary); }
.stat-info .stat-value { color: var(--text); font-size: 1.5rem; font-weight: 700; }
.stat-label { display: block; font-size: .8rem; color: var(--text-light); margin-top: 2px; }

/* ─── Dashboard Grid ────────────────────────────────────── */
.dashboard-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
@media (max-width: 900px) { .dashboard-grid { grid-template-columns: 1fr; } }

/* ─── Tables ────────────────────────────────────────────── */
.table-responsive { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 10px 15px; text-align: left; border-bottom: 1px solid var(--border); }
.table th { background: var(--bg); font-size: .8rem; text-transform: uppercase; letter-spacing: .5px; color: var(--text-light); font-weight: 600; }
.table tbody tr:hover { background: color-mix(in srgb, var(--primary) 5%, var(--card-bg)); }
.table tfoot th { background: var(--bg); padding: 10px 15px; }
.table-total th { color: var(--primary); font-size: 1rem; }
.actions { display: flex; gap: 5px; }
.text-center { text-align: center; }

/* ─── Badges ────────────────────────────────────────────── */
.badge {
    display: inline-block; padding: 3px 10px; border-radius: 20px;
    font-size: .75rem; font-weight: 600; text-transform: capitalize;
}
.badge-paid, .badge-accepted { background: #d1fae5; color: #065f46; }
.badge-unpaid, .badge-refused { background: #fee2e2; color: #991b1b; }
.badge-draft { background: #fef3c7; color: #92400e; }
.badge-sent { background: #dbeafe; color: #1e40af; }
.badge-cancelled { background: #e5e7eb; color: #374151; }
.badge-info { background: #dbeafe; color: #1e40af; }
.badge-danger { background: #fee2e2; color: #991b1b; }
.badge-primary { background: #e0e7ff; color: #3730a3; }

/* ─── Buttons ───────────────────────────────────────────── */
.btn {
    display: inline-flex; align-items: center; gap: 6px; padding: 8px 16px;
    border-radius: var(--radius); font-size: .85rem; font-weight: 500;
    border: 1px solid transparent; cursor: pointer; transition: all .2s;
    font-family: var(--font); white-space: nowrap;
}
.btn-primary { background: var(--btn); color: #fff; border-color: var(--btn); }
.btn-primary:hover { filter: brightness(1.1); }
.btn-outline { background: transparent; color: var(--text); border-color: var(--border); }
.btn-outline:hover { border-color: var(--primary); color: var(--primary); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(1.1); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { filter: brightness(1.1); }
.btn-sm { padding: 5px 10px; font-size: .8rem; }
.btn-block { width: 100%; justify-content: center; }
.btn-group { display: flex; gap: 8px; flex-wrap: wrap; }

/* ─── Forms ─────────────────────────────────────────────── */
.form-group { margin-bottom: 15px; }
.form-group label { display: block; font-size: .85rem; font-weight: 500; margin-bottom: 5px; color: var(--text); }
.form-group input, .form-group select, .form-group textarea {
    width: 100%; padding: 8px 12px; border: 1px solid var(--border); border-radius: var(--radius);
    font-size: .9rem; font-family: var(--font); background: var(--card-bg); color: var(--text);
    transition: border-color .2s;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
    outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 20%, transparent);
}
.form-group input[type="color"] { height: 40px; padding: 3px; cursor: pointer; }
.form-group input[type="file"] { padding: 5px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 20px; }
.form-grid .full-width, .form-actions.full-width { grid-column: 1 / -1; }
.form-inline { display: flex; align-items: center; gap: 15px; flex-wrap: wrap; }
.form-inline .form-group { margin-bottom: 0; }
.form-actions { margin-top: 20px; display: flex; gap: 10px; }
.checkbox-label { display: flex; align-items: center; gap: 8px; font-size: .9rem; cursor: pointer; }
.checkbox-label input[type="checkbox"] { width: auto; }

/* ─── Alert ─────────────────────────────────────────────── */
.alert {
    padding: 12px 20px; border-radius: var(--radius); margin-bottom: 20px;
    font-size: .9rem; border: 1px solid transparent;
}
.alert-success { background: #d1fae5; color: #065f46; border-color: #a7f3d0; }
.alert-danger { background: #fee2e2; color: #991b1b; border-color: #fecaca; }
.alert-info { background: #dbeafe; color: #1e40af; border-color: #bfdbfe; }
.alert-warning { background: #fef3c7; color: #92400e; border-color: #fde68a; }

/* ─── Section Title ─────────────────────────────────────── */
.section-title {
    font-size: 1rem; font-weight: 600; margin: 20px 0 10px; padding-top: 15px;
    border-top: 1px solid var(--border);
}

/* ─── Invoice Summary ───────────────────────────────────── */
.invoice-summary {
    width: 300px; margin-left: auto; margin-top: 15px; padding: 15px;
    background: var(--bg); border-radius: var(--radius);
}
.summary-row { display: flex; justify-content: space-between; padding: 5px 0; font-size: .9rem; }
.summary-total { border-top: 2px solid var(--primary); padding-top: 10px; margin-top: 5px; font-size: 1.1rem; font-weight: 700; color: var(--primary); }

/* ─── Invoice Preview ───────────────────────────────────── */
.invoice-preview-wrapper { background: var(--card-bg); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.invoice-iframe { width: 100%; height: 80vh; border: none; }

/* ─── AI Chat ───────────────────────────────────────────── */
.ai-chat-container { display: flex; flex-direction: column; height: 500px; }
.ai-chat-messages { flex: 1; overflow-y: auto; padding: 15px; display: flex; flex-direction: column; gap: 15px; }
.ai-message { display: flex; gap: 10px; align-items: flex-start; }
.ai-message.ai-user { flex-direction: row-reverse; }
.ai-avatar { width: 36px; height: 36px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 1rem; background: #e0e7ff; color: var(--primary); flex-shrink: 0; }
.user-avatar { background: #d1fae5; color: var(--success); }
.ai-bubble {
    max-width: 70%; padding: 10px 15px; border-radius: 12px; background: var(--bg);
    font-size: .9rem; line-height: 1.5;
}
.ai-bubble p { margin: 0; }
.ai-bubble ul { margin: 5px 0; padding-left: 20px; list-style: disc; }
.user-bubble { background: var(--primary); color: #fff; border-bottom-right-radius: 4px; }
.ai-bubble:not(.user-bubble) { border-bottom-left-radius: 4px; }
.ai-welcome { justify-content: center; }
.ai-welcome .ai-bubble { max-width: 80%; }
.ai-chat-input {
    display: flex; gap: 10px; padding: 15px; border-top: 1px solid var(--border);
    background: var(--card-bg);
}
.ai-chat-input input[type="text"] { flex: 1; padding: 10px 15px; border: 1px solid var(--border); border-radius: var(--radius); font-size: .9rem; font-family: var(--font); background: var(--bg); color: var(--text); }
.ai-chat-input input[type="text"]:focus { outline: none; border-color: var(--primary); }
.ai-chat-input .btn { border-radius: 50%; width: 42px; height: 42px; padding: 0; justify-content: center; }

/* ─── Responsive ────────────────────────────────────────── */
@media (max-width: 768px) {
    .sidebar { transform: translateX(-100%); }
    .sidebar.open { transform: translateX(0); }
    .main-content { margin-left: 0; }
    .sidebar-toggle { display: block; }
    .topbar { padding: 12px 15px; }
    .content-area { padding: 15px; }
    .stats-grid { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
    .form-grid { grid-template-columns: 1fr; }
    .invoice-summary { width: 100%; }
    .page-header h1 { font-size: 1.2rem; }
    .table { font-size: .8rem; }
    .table th, .table td { padding: 8px 10px; }
    .btn-group { flex-wrap: wrap; }
}

/* ─── Scrollbar ─────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-light); }

/* ─── Print ─────────────────────────────────────────────── */
@media print {
    .sidebar, .topbar, .app-footer, .btn, .page-header .btn-group, .sidebar-toggle { display: none !important; }
    .main-content { margin-left: 0 !important; }
    .content-area { padding: 0 !important; }
    .card { box-shadow: none !important; border: none !important; }
}
/* DATAZAD fixes */
.login-wrapper {
    position: fixed;
    inset: 0;
    width: 100vw;
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    padding: 20px;
}
.login-card { margin: 0 auto; }
@media print {
    .invoice-preview-wrapper, .invoice-iframe { height: auto !important; }
}

/* DATAZAD SaaS V2: RTL + theme helpers */
html[dir="rtl"] body { direction: rtl; text-align: right; }
html[dir="rtl"] .sidebar { left: auto; right: 0; }
html[dir="rtl"] .main-content { margin-left: 0; margin-right: 260px; }
html[dir="rtl"] .sidebar-nav ul li a { border-left: 0; border-right: 3px solid transparent; }
html[dir="rtl"] .sidebar-nav ul li a.active { border-right-color: var(--primary-light); }
[data-theme="company"] { --bg: color-mix(in srgb, var(--primary) 5%, #f8fafc); }
@media(max-width:768px){ html[dir="rtl"] .main-content{margin-right:0;} }


/* Correctifs DATAZAD SaaS V2.1 */
html[dir="rtl"], html[dir="rtl"] body { direction: rtl; text-align: right; }
html[dir="rtl"] .sidebar { left: auto; right: 0; }
html[dir="rtl"] .main-content { margin-left: 0; margin-right: 260px; }
html[dir="rtl"] .sidebar-nav ul li a { border-left: 0; border-right: 3px solid transparent; }
html[dir="rtl"] .sidebar-nav ul li a.active { border-right-color: var(--primary-light); }
html[dir="rtl"] .topbar-actions { margin-left: 0; margin-right: auto; }
.document-preview-frame { width: 100%; height: 82vh; border: 0; background:#fff; border-radius: var(--radius); }
.form-saving { opacity: .65; pointer-events: none; }
@media (max-width: 768px) {
  html[dir="rtl"] .sidebar { transform: translateX(100%); }
  html[dir="rtl"] .sidebar.open { transform: translateX(0); }
  html[dir="rtl"] .main-content { margin-right: 0; }
}
@media print {
  .document-preview-frame { height: auto !important; }
}


/* DATAZAD SaaS V2.2 - Aperçu documents A4 centré */
.invoice-preview-wrapper {
    min-height: calc(100vh - 210px) !important;
    padding: 24px !important;
    display: flex !important;
    justify-content: center !important;
    align-items: flex-start !important;
    overflow: auto !important;
    background: #eef2f7 !important;
    border: 1px solid var(--border) !important;
}
.document-preview-frame,
.invoice-iframe {
    width: 210mm !important;
    max-width: 100% !important;
    height: 297mm !important;
    min-height: 297mm !important;
    border: 0 !important;
    background: #fff !important;
    border-radius: 0 !important;
    box-shadow: 0 12px 35px rgba(15,23,42,.18) !important;
    flex: 0 0 auto !important;
}
@media (max-width: 980px) {
    .invoice-preview-wrapper { padding: 12px !important; justify-content: flex-start !important; }
    .document-preview-frame, .invoice-iframe { width: 794px !important; max-width: none !important; }
}
@media print {
    .invoice-preview-wrapper { display:block !important; padding:0 !important; background:#fff !important; border:0 !important; }
    .document-preview-frame, .invoice-iframe { width:100% !important; height:auto !important; min-height:0 !important; box-shadow:none !important; }
}

/* Document viewer final: facture/devis en grand sous les boutons */
.invoice-preview-wrapper{
    width:100%;
    min-height:calc(100vh - 190px);
    background:#f3f4f6;
    border:1px solid var(--border);
    border-radius:10px;
    padding:18px;
    overflow:auto;
}
.document-preview-frame{
    display:block;
    width:100%;
    min-height:calc(100vh - 230px);
    height:calc(100vh - 230px);
    border:0;
    background:#fff;
    border-radius:8px;
}
@media (max-width: 768px){
    .invoice-preview-wrapper{padding:8px;min-height:75vh;}
    .document-preview-frame{height:75vh;min-height:75vh;}
}

/* DATAZAD - Achats, TVA & Chèques */
@media print {
    .no-print, .sidebar, .topbar, .app-footer, .page-header .btn, .form-inline, .btn { display:none !important; }
    .main-content { margin-left:0 !important; margin-right:0 !important; }
    .content-area { padding:10px !important; }
    .card { box-shadow:none !important; border:1px solid #ddd !important; }
    body { background:#fff !important; color:#000 !important; }
}
/* DATAZAD - AI */
.ai-quick-grid{
    display:grid;
    grid-template-columns:repeat(auto-fit,minmax(190px,1fr));
    gap:10px;
    margin-top:15px;
}

.ai-quick-grid{
    display:flex;
    flex-wrap:wrap;
    gap:8px;
    margin-top:12px;
}

.ai-quick-grid button{
    background:#4f46e5;
    color:#fff;
    border:none;
    border-radius:20px;
    padding:8px 14px;
    cursor:pointer;
    font-size:13px;
    transition:.2s;
}

.ai-quick-grid button:hover{
    background:#3730a3;
}