:root {
  --bg: #f4f6f9; --card: #fff; --text: #1d2733; --muted: #6b7785; --line: #e1e6ec;
  --accent: #0f3d5e; --accent2: #1b6fa8; --ok: #1e8a4c; --warn: #c27c0e; --err: #c0392b;
  --radius: 10px;
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--err); }
h1 { font-size: 22px; margin: 0 0 16px; }
h2 { font-size: 17px; margin: 22px 0 10px; }
a { color: var(--accent2); }

.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 10px; }
.login h1 { color: var(--accent); margin: 0; }

.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--accent); color: #fff; }
.topbar input { flex: 1; max-width: 420px; margin-left: auto; border: 0; }
.icon-btn { background: none; border: 0; color: inherit; font-size: 22px; cursor: pointer; display: none; }
.sidebar { position: fixed; top: 56px; left: 0; bottom: 0; width: 210px; background: var(--card); border-right: 1px solid var(--line); padding: 12px 8px; overflow-y: auto; z-index: 15; }
.sidebar a { display: block; padding: 9px 12px; border-radius: 8px; color: var(--text); text-decoration: none; }
.sidebar a:hover, .sidebar a.active { background: #e8f0f7; color: var(--accent); font-weight: 600; }
main { margin-left: 210px; padding: 20px; max-width: 1200px; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.grid { display: grid; gap: 12px; }
.stats { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.stat .v { font-size: 24px; font-weight: 700; color: var(--accent); }
.stat .l { color: var(--muted); font-size: 13px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.row.between { justify-content: space-between; }
.cols2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

input, select, textarea { width: 100%; padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px; font: inherit; background: #fff; color: var(--text); }
textarea { min-height: 80px; resize: vertical; }
label { display: block; font-size: 13px; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: 3px; }
.form { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; }
.form .full { grid-column: 1 / -1; }

.btn { display: inline-flex; align-items: center; gap: 6px; padding: 9px 14px; border-radius: 8px; border: 1px solid var(--line); background: #fff; color: var(--text); font: inherit; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { border-color: var(--accent2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.danger { color: var(--err); }
.btn.small { padding: 5px 9px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }

table { width: 100%; border-collapse: collapse; background: var(--card); }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; color: var(--muted); font-weight: 600; }
tr.click { cursor: pointer; }
tr.click:hover td { background: #f7fafc; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); }
.right { text-align: right; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 12px; background: #eef1f4; color: var(--muted); }
.badge.ok { background: #e3f4ea; color: var(--ok); }
.badge.warn { background: #fdf1dd; color: var(--warn); }
.badge.err { background: #fbe6e3; color: var(--err); }
.badge.info { background: #e3eef8; color: var(--accent2); }

.lignes td { padding: 4px; }
.lignes input { padding: 7px; }
.totaux { margin-left: auto; max-width: 300px; }
.totaux div { display: flex; justify-content: space-between; padding: 3px 0; }
.totaux .big { font-weight: 700; font-size: 18px; border-top: 1px solid var(--line); padding-top: 6px; }

.modal { position: fixed; inset: 0; background: rgba(10, 20, 30, .45); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal-box { background: var(--card); border-radius: var(--radius); padding: 18px; width: 100%; max-width: 680px; max-height: 92vh; overflow-y: auto; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: #fff; padding: 10px 18px; border-radius: 8px; z-index: 60; max-width: 90vw; }
.toast.err { background: var(--err); }

.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { padding: 8px 0; border-bottom: 1px solid var(--line); }

.wa { display: grid; grid-template-columns: 300px 1fr; height: calc(100vh - 190px); min-height: 420px; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: var(--card); }
.wa-list { border-right: 1px solid var(--line); overflow-y: auto; }
.wa-item { padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.wa-item:hover, .wa-item.active { background: #eef5ee; }
.wa-item .n { font-weight: 600; display: flex; justify-content: space-between; gap: 6px; }
.wa-item .p { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wa-chat { display: flex; flex-direction: column; min-width: 0; }
.wa-msgs { flex: 1; overflow-y: auto; padding: 14px; background: #ece5dd; display: flex; flex-direction: column; gap: 6px; }
.bubble { max-width: 75%; padding: 7px 10px; border-radius: 8px; background: #fff; white-space: pre-wrap; word-wrap: break-word; }
.bubble.me { align-self: flex-end; background: #d9fdd3; }
.bubble small { display: block; color: var(--muted); font-size: 11px; text-align: right; }
.wa-send { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); }
.tabs { display: flex; gap: 6px; margin-bottom: 12px; flex-wrap: wrap; }
.tabs .btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.nav-backdrop, .bottomnav { display: none; }
img, canvas { max-width: 100%; height: auto; }

@media (max-width: 820px) {
  html { -webkit-text-size-adjust: 100%; }
  body { overflow-x: hidden; }
  .icon-btn { display: block; padding: 4px 6px; }
  .topbar { padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top)); gap: 8px; }
  .topbar input { max-width: none; }
  .topbar strong { display: none; }
  .sidebar { top: 0; width: 250px; padding-top: calc(16px + env(safe-area-inset-top)); transform: translateX(-100%); transition: transform .2s; box-shadow: 2px 0 12px rgba(0,0,0,.15); z-index: 40; }
  .sidebar.open { transform: none; }
  .sidebar a { padding: 12px 14px; font-size: 16px; }
  .sidebar.open + .nav-backdrop { display: block; position: fixed; inset: 0; background: rgba(10,20,30,.4); z-index: 30; }
  main { margin-left: 0; padding: 14px 12px calc(84px + env(safe-area-inset-bottom)); }
  h1 { font-size: 20px; }

  /* champs à 16px : évite le zoom automatique sur iPhone */
  input, select, textarea { font-size: 16px; padding: 11px 10px; }
  .btn { padding: 11px 14px; }
  .btn.small { padding: 8px 11px; }
  .form { grid-template-columns: 1fr; }
  .cols2 { grid-template-columns: 1fr; }
  .row > .btn { flex: 1 1 auto; justify-content: center; }
  .stats { grid-template-columns: 1fr 1fr; }
  .stat .v { font-size: 20px; }
  .card { padding: 14px 12px; }

  th, td { padding: 8px 7px; }
  .table-wrap { -webkit-overflow-scrolling: touch; }
  .lignes { min-width: 560px; }
  .totaux { max-width: none; width: 100%; }

  .modal { padding: 0; place-items: end stretch; }
  .modal-box { max-width: none; max-height: 92vh; border-radius: 14px 14px 0 0; padding: 16px 14px calc(16px + env(safe-area-inset-bottom)); }
  .toast { bottom: calc(80px + env(safe-area-inset-bottom)); }

  .wa { grid-template-columns: 1fr; height: auto; }
  .wa-list { max-height: 260px; border-right: 0; border-bottom: 1px solid var(--line); }
  .wa-msgs { min-height: 340px; }
  .bubble { max-width: 88%; }
  .hide-m { display: none; }

  .bottomnav { display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 25; background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); box-shadow: 0 -2px 10px rgba(0,0,0,.05); }
  .bottomnav a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 1px; padding: 7px 2px 8px; font-size: 11px; color: var(--muted); text-decoration: none; }
  .bottomnav a span { font-size: 20px; line-height: 1.1; }
  .bottomnav a.active { color: var(--accent); font-weight: 600; }
}
