*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --text-2: #6b7280;
  --accent: #1B4FD8;
  --accent-light: #eff2ff;
  --green: #059669;
  --green-light: #ecfdf5;
  --orange: #d97706;
  --orange-light: #fffbeb;
  --red: #dc2626;
  --red-light: #fef2f2;
  --sidebar-w: 220px;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
}

body { font-family: 'Inter', system-ui, -apple-system, sans-serif; font-size: 14px; background: var(--bg); color: var(--text); height: 100vh; overflow: hidden; }

#app { display: flex; height: 100vh; }

/* SIDEBAR */
#sidebar { width: var(--sidebar-w); background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 20px 0; flex-shrink: 0; }

.brand { display: flex; align-items: center; gap: 10px; padding: 0 16px 20px; border-bottom: 1px solid var(--border); margin-bottom: 12px; }
.brand-icon { width: 32px; height: 32px; background: var(--accent); border-radius: 8px; display: flex; align-items: center; justify-content: center; color: #fff; font-size: 11px; font-weight: 800; letter-spacing: 0.5px; }
.brand span { font-weight: 700; font-size: 13px; }

.societe-select { padding: 0 12px 16px; border-bottom: 1px solid var(--border); margin-bottom: 8px; }
.societe-select label { display: block; font-size: 10px; letter-spacing: 1.5px; text-transform: uppercase; color: var(--text-2); margin-bottom: 6px; font-weight: 600; }
.societe-select select { width: 100%; padding: 7px 10px; border: 1px solid var(--border); border-radius: var(--radius); font-size: 13px; font-weight: 600; background: var(--bg); cursor: pointer; }

nav { padding: 0 8px; flex: 1; }
.nav-link { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: var(--radius); text-decoration: none; color: var(--text-2); font-size: 13px; font-weight: 500; margin-bottom: 2px; transition: all .15s; }
.nav-link svg { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
.nav-link:hover { background: var(--bg); color: var(--text); }
.nav-link.active { background: var(--accent-light); color: var(--accent); font-weight: 600; }

/* MAIN */
#main { flex: 1; overflow-y: auto; padding: 28px 32px; }

.view { max-width: 1100px; }
.hidden { display: none !important; }

/* PAGE HEADER */
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; }
.page-title { font-size: 22px; font-weight: 800; }
.page-sub { font-size: 13px; color: var(--text-2); margin-top: 2px; }

/* CARDS / STATS */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-bottom: 28px; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.stat-label { font-size: 11px; letter-spacing: 1.5px; text-transform: uppercase; color: var(--text-2); font-weight: 600; margin-bottom: 8px; }
.stat-value { font-size: 28px; font-weight: 800; color: var(--text); }
.stat-sub { font-size: 12px; color: var(--text-2); margin-top: 4px; }

/* TABLE */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.card-header { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.card-title { font-size: 14px; font-weight: 700; }

table { width: 100%; border-collapse: collapse; }
th { padding: 10px 16px; text-align: left; font-size: 11px; letter-spacing: 1px; text-transform: uppercase; color: var(--text-2); font-weight: 600; border-bottom: 1px solid var(--border); background: #fafafa; }
td { padding: 12px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: #fafafa; }

/* BADGES */
.badge { display: inline-flex; align-items: center; padding: 3px 10px; border-radius: 20px; font-size: 11px; font-weight: 600; letter-spacing: 0.3px; }
.badge-gray { background: #f3f4f6; color: #6b7280; }
.badge-blue { background: var(--accent-light); color: var(--accent); }
.badge-green { background: var(--green-light); color: var(--green); }
.badge-orange { background: var(--orange-light); color: var(--orange); }
.badge-red { background: var(--red-light); color: var(--red); }

/* BUTTONS */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 16px; border-radius: var(--radius); font-size: 13px; font-weight: 600; border: none; cursor: pointer; transition: all .15s; text-decoration: none; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: #1640b0; }
.btn-secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-success { background: var(--green); color: #fff; }
.btn-success:hover { background: #047857; }
.btn-danger { background: var(--red); color: #fff; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn svg { width: 14px; height: 14px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* FORMS */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.form-grid.cols-1 { grid-template-columns: 1fr; }
.form-grid.cols-3 { grid-template-columns: 1fr 1fr 1fr; }
.form-group { display: flex; flex-direction: column; gap: 5px; }
.form-group.full { grid-column: 1 / -1; }
label { font-size: 12px; font-weight: 600; color: var(--text-2); }
input, select, textarea { padding: 8px 12px; border: 1px solid var(--border); border-radius: var(--radius); font-size: 13px; font-family: inherit; background: #fff; transition: border-color .15s; width: 100%; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(27,79,216,.1); }
textarea { resize: vertical; min-height: 80px; }
.form-section { margin: 20px 0 10px; font-size: 11px; letter-spacing: 1.5px; text-transform: uppercase; color: var(--text-2); font-weight: 700; padding-bottom: 6px; border-bottom: 1px solid var(--border); }

/* LIGNES */
.lignes-table { width: 100%; border-collapse: collapse; margin-bottom: 12px; }
.lignes-table th { font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: var(--text-2); padding: 6px 8px; text-align: left; }
.lignes-table td { padding: 4px; }
.lignes-table input { padding: 6px 8px; font-size: 13px; }
.lignes-table .col-desc { width: 40%; }
.lignes-table .col-qte { width: 10%; }
.lignes-table .col-unite { width: 12%; }
.lignes-table .col-pu { width: 14%; }
.lignes-table .col-tva { width: 10%; }
.lignes-table .col-ht { width: 10%; text-align: right; padding-right: 12px; font-weight: 600; }
.lignes-table .col-del { width: 4%; text-align: center; }
.del-btn { background: none; border: none; cursor: pointer; color: var(--text-2); padding: 4px; border-radius: 4px; }
.del-btn:hover { color: var(--red); background: var(--red-light); }

/* TOTAUX */
.totaux-bloc { display: flex; justify-content: flex-end; margin: 12px 0 20px; }
.totaux-inner { min-width: 260px; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.totaux-row { display: flex; justify-content: space-between; padding: 8px 14px; font-size: 13px; border-bottom: 1px solid var(--border); }
.totaux-row:last-child { border-bottom: none; font-weight: 800; font-size: 15px; background: var(--accent-light); color: var(--accent); }

/* MODAL */
#modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 100; display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; overflow-y: auto; }
#modal { background: #fff; border-radius: 12px; width: 100%; max-width: 780px; box-shadow: 0 20px 60px rgba(0,0,0,.2); }
#modal-header { display: flex; align-items: center; justify-content: space-between; padding: 20px 24px; border-bottom: 1px solid var(--border); }
#modal-title { font-size: 16px; font-weight: 800; }
#modal-close { background: none; border: none; font-size: 22px; cursor: pointer; color: var(--text-2); line-height: 1; padding: 4px 8px; border-radius: 4px; }
#modal-close:hover { background: var(--bg); }
#modal-body { padding: 24px; }

/* SEARCH BAR */
.search-bar { position: relative; }
.search-bar input { padding-left: 32px; }
.search-bar svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); width: 14px; height: 14px; stroke: var(--text-2); fill: none; stroke-width: 2; }

/* FILTERS */
.filters { display: flex; gap: 10px; align-items: center; margin-bottom: 16px; flex-wrap: wrap; }
.filters select { width: auto; padding: 7px 10px; font-size: 12px; }

/* EMPTY STATE */
.empty { text-align: center; padding: 60px 20px; color: var(--text-2); }
.empty svg { width: 40px; height: 40px; stroke: #d1d5db; fill: none; stroke-width: 1.5; margin-bottom: 12px; }
.empty p { font-size: 14px; margin-bottom: 16px; }

/* ACTIONS TABLE */
.actions { display: flex; gap: 6px; }

/* LOGO UPLOAD */
.logo-upload { border: 2px dashed var(--border); border-radius: var(--radius); padding: 20px; text-align: center; cursor: pointer; transition: border-color .15s; }
.logo-upload:hover { border-color: var(--accent); }
.logo-preview { max-height: 60px; max-width: 180px; }

/* ALERT */
.alert { padding: 10px 14px; border-radius: var(--radius); font-size: 13px; margin-bottom: 16px; }
.alert-success { background: var(--green-light); color: var(--green); border: 1px solid #a7f3d0; }
.alert-error { background: var(--red-light); color: var(--red); border: 1px solid #fecaca; }

/* MOBILE HEADER */
#mobile-header { display: none; }
#sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 90; }

@media (max-width: 768px) {
  /* Reset body/app pour mobile natif */
  body { height: auto; overflow: auto; -webkit-overflow-scrolling: touch; }
  #app { flex-direction: column; height: auto; min-height: 100dvh; }

  /* Barre du haut mobile */
  #mobile-header {
    display: flex; align-items: center; gap: 12px;
    padding: 14px 16px; background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 80;
    box-shadow: 0 1px 4px rgba(0,0,0,.06);
  }
  #hamburger { background: none; border: none; cursor: pointer; padding: 6px; color: var(--text); display: flex; border-radius: 8px; }
  #hamburger svg { width: 22px; height: 22px; }
  .mobile-title { font-weight: 800; font-size: 15px; flex: 1; }

  /* Sidebar tiroir */
  #sidebar {
    position: fixed; left: -260px; top: 0; bottom: 0; z-index: 100;
    width: 260px; transition: transform .28s cubic-bezier(.4,0,.2,1);
    box-shadow: none; overflow-y: auto;
  }
  #sidebar.open { transform: translateX(260px); box-shadow: 8px 0 24px rgba(0,0,0,.18); }
  #sidebar-overlay.open { display: block; }

  /* Contenu principal */
  #main { padding: 16px 14px; overflow-y: visible; flex: 1; }
  .view { max-width: 100%; }

  /* En-têtes de page */
  .page-header { flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
  .page-header > div:last-child { display: flex; gap: 8px; flex-wrap: wrap; }
  .page-title { font-size: 18px; }

  /* Stats */
  .stats-grid { grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 16px; }
  .stat-card { padding: 14px; }
  .stat-value { font-size: 22px; }

  /* Tables avec scroll horizontal */
  .card { overflow: hidden; }
  .card > .card-header { padding: 12px 14px; }
  .table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  table { min-width: 560px; }
  th, td { padding: 10px 12px; }

  /* Formulaires */
  .form-grid { grid-template-columns: 1fr !important; }
  .form-group.full { grid-column: 1; }
  input, select, textarea { font-size: 16px; } /* évite zoom iOS */

  /* Modal plein écran natif */
  #modal-overlay {
    padding: 0; align-items: flex-end;
    background: rgba(0,0,0,.5);
  }
  #modal {
    border-radius: 20px 20px 0 0;
    max-width: 100%; width: 100%;
    max-height: 94dvh;
    display: flex; flex-direction: column;
  }
  #modal-header {
    position: sticky; top: 0; z-index: 10;
    background: #fff; border-radius: 20px 20px 0 0;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
  }
  /* Trait de tirage */
  #modal-header::before {
    content: ''; display: block;
    width: 36px; height: 4px;
    background: var(--border); border-radius: 2px;
    margin: 0 auto 12px;
  }
  #modal-body { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 16px 20px; flex: 1; }

  /* Lignes produits */
  .lignes-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .lignes-table { min-width: 500px; }

  /* Totaux */
  .totaux-bloc { justify-content: stretch; }
  .totaux-inner { min-width: unset; width: 100%; }

  /* Filtres */
  .filters { gap: 8px; }
  .filters select { font-size: 14px; }

  /* Actions */
  .actions { gap: 6px; }
  .btn { padding: 9px 14px; font-size: 13px; }
  .btn-sm { padding: 7px 12px; font-size: 12px; }
}

@media (max-width: 420px) {
  .stats-grid { grid-template-columns: 1fr; }
  #main { padding: 12px 10px; }
  .page-title { font-size: 16px; }
}

/* LOGIN */
#login-screen { position: fixed; inset: 0; background: var(--bg); z-index: 999; display: flex; align-items: center; justify-content: center; }
.login-box { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 40px; width: 100%; max-width: 360px; box-shadow: 0 8px 32px rgba(0,0,0,.1); }
.login-logo { display: flex; align-items: center; gap: 10px; margin-bottom: 32px; justify-content: center; }
.login-logo .brand-icon { width: 40px; height: 40px; font-size: 13px; }
.login-logo span { font-size: 16px; font-weight: 800; }
.login-box h2 { font-size: 20px; font-weight: 800; margin-bottom: 6px; text-align: center; }
.login-sub { font-size: 13px; color: var(--text-2); text-align: center; margin-bottom: 28px; }
.login-box .form-group { margin-bottom: 16px; }
.login-btn { width: 100%; padding: 11px; font-size: 14px; margin-top: 8px; justify-content: center; }
.login-error { display: none; }
