/* ==========================================================================
   Wafi CRM - Dashboard (index.php)
   Uses the app tokens from variables.css, works in light and dark themes.
   ========================================================================== */

.dash { display: flex; flex-direction: column; gap: 22px; }

/* ---------- Welcome ---------- */
.dash-hero { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; flex-wrap: wrap; padding: 4px 0 2px; }
.dash-date { font-size: 13px; font-weight: 600; color: var(--primary); letter-spacing: .01em; }
.dash-title { font-size: clamp(1.6rem, 2.4vw, 2.1rem); letter-spacing: -0.04em; font-weight: 700; line-height: 1.1; margin-top: 4px; }
.dash-lead { color: var(--text-secondary); font-size: 14.5px; margin-top: 8px; max-width: 62ch; }
.dash-lead strong { color: var(--text-primary); font-weight: 650; }
.dash-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.dash-actions .btn { height: 40px; }

/* ---------- KPI tiles ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; }
.kpi {
  display: flex; flex-direction: column; gap: 6px; padding: 20px 20px 18px;
  background: var(--bg-surface); border: 1px solid var(--border-subtle); border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card); color: var(--text-primary);
  transition: transform var(--transition-base), box-shadow var(--transition-base), border-color var(--transition-base);
}
.kpi:hover { transform: translateY(-2px); box-shadow: var(--shadow-card-hover); border-color: var(--primary-border); color: var(--text-primary); }
.kpi-label { font-size: 13px; font-weight: 600; color: var(--text-secondary); }
.kpi-value { font-size: clamp(1.5rem, 2.1vw, 1.95rem); font-weight: 700; letter-spacing: -0.035em; font-variant-numeric: tabular-nums; line-height: 1.15; }
.kpi-foot { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 12.5px; margin-top: auto; }
.kpi-note { color: var(--text-subdued); }
.kpi-trend { font-weight: 700; padding: 2px 8px; border-radius: 999px; font-variant-numeric: tabular-nums; }
.kpi-trend.up { background: var(--status-success-bg); color: var(--status-success-text); }
.kpi-trend.down { background: var(--status-critical-bg); color: var(--status-critical-text); }
.kpi-feature { background: linear-gradient(155deg, var(--primary-deep), var(--primary-darkest)); border-color: transparent; color: #fff; }
.kpi-feature:hover { color: #fff; border-color: transparent; }
.kpi-feature .kpi-label, .kpi-feature .kpi-note { color: rgba(255, 255, 255, .72); }
.kpi-feature .kpi-trend.up { background: rgba(255, 255, 255, .14); color: #b8f0d8; }
.kpi-feature .kpi-trend.down { background: rgba(255, 120, 110, .18); color: #ffc9c3; }

/* ---------- Grids & panels ---------- */
.dash-grid { display: grid; gap: 16px; grid-template-columns: minmax(0, 1fr); align-items: start; }
.dash-grid-main { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); }
.dash-grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.panel {
  background: var(--bg-surface); border: 1px solid var(--border-subtle); border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card); min-width: 0; display: flex; flex-direction: column; height: 100%;
}
.panel-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 18px 20px 12px; }
.panel-title { font-size: 15.5px; font-weight: 650; letter-spacing: -0.015em; }
.panel-sub { font-size: 12.5px; color: var(--text-subdued); margin-top: 2px; }
.panel-link { font-size: 13px; font-weight: 600; color: var(--primary); padding: 4px 10px; border-radius: 999px; white-space: nowrap; transition: background-color var(--transition-fast); }
.panel-link:hover { background: var(--primary-light); color: var(--primary-active); }
.panel-alert { border-color: var(--status-critical-border); height: auto; }

/* Segmented period control */
.segmented { display: inline-flex; padding: 3px; gap: 2px; background: var(--bg-app); border: 1px solid var(--border-subtle); border-radius: 999px; }
.segmented button { padding: 5px 12px; border-radius: 999px; font-size: 12.5px; font-weight: 600; color: var(--text-secondary); transition: background-color var(--transition-fast), color var(--transition-fast); }
.segmented button:hover { color: var(--text-primary); }
.segmented button.active { background: var(--bg-surface); color: var(--text-primary); box-shadow: var(--shadow-sm); }

/* Chart */
.chart-wrap { position: relative; height: 260px; padding: 4px 16px 16px 6px; }
.chart-wrap canvas { display: block; }
.chart-wrap.is-locked canvas { filter: blur(3px); opacity: .35; }
.locked { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; text-align: center; padding: 20px; }
.locked strong { font-size: 15px; }
.locked span { font-size: 13px; color: var(--text-secondary); max-width: 40ch; margin-bottom: 8px; }

/* Funnel */
.funnel { list-style: none; display: flex; flex-direction: column; gap: 16px; padding: 6px 20px 20px; }
.funnel-meta { display: flex; justify-content: space-between; gap: 10px; font-size: 13px; margin-bottom: 6px; }
.funnel-label { font-weight: 600; }
.funnel-label em { font-style: normal; color: var(--text-subdued); font-weight: 500; margin-left: 4px; }
.funnel-amount { font-weight: 650; font-variant-numeric: tabular-nums; }
.funnel-bar { display: block; height: 8px; border-radius: 999px; background: var(--bg-surface-hover); overflow: hidden; }
.funnel-bar span { display: block; height: 100%; width: var(--w); border-radius: inherit; background: linear-gradient(90deg, #2A6BC2, var(--primary)); transform-origin: left; }
.funnel-row.is-won .funnel-bar span { background: var(--primary-deep); }
html[data-theme="dark"] .funnel-row.is-won .funnel-bar span { background: #6FD3C6; }

/* Lists */
.list { list-style: none; padding: 0 8px 10px; }
.list-row { display: flex; align-items: center; gap: 12px; padding: 11px 12px; border-radius: 12px; color: var(--text-primary); transition: background-color var(--transition-fast); }
.list-row:hover { background: var(--bg-surface-hover); color: var(--text-primary); }
.list li + li .list-row { box-shadow: 0 -1px 0 var(--border-subtle); }
.list li + li .list-row:hover { box-shadow: none; }
.list-main { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.list-title { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-sub { font-size: 12.5px; color: var(--text-subdued); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-sub.mono { font-family: var(--font-mono); font-size: 12px; }
.list-end { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex: none; }
.list-amount { font-size: 13.5px; font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; }
.avatar { width: 36px; height: 36px; border-radius: 11px; flex: none; display: grid; place-items: center; font-size: 12px; font-weight: 700; background: var(--primary-light); color: var(--primary-active); }
html[data-theme="dark"] .avatar { color: var(--primary); }

.chip { font-size: 11.5px; font-weight: 650; padding: 3px 9px; border-radius: 999px; white-space: nowrap; flex: none; }
.chip-late { background: var(--status-critical-bg); color: var(--status-critical-text); }
.chip-today { background: var(--status-warning-bg); color: var(--status-warning-text); }
.chip-soon { background: var(--status-info-bg); color: var(--status-info-text); }
.chip-muted { background: var(--status-neutral-bg); color: var(--text-secondary); }

.prio { width: 10px; height: 10px; border-radius: 50%; flex: none; box-shadow: 0 0 0 4px var(--bg-surface-hover); }
.prio-haute { background: var(--status-critical-text); }
.prio-moyenne { background: var(--status-warning-text); }
.prio-basse { background: var(--text-subdued); }

.prob { display: inline-flex; align-items: center; gap: 6px; font-size: 11.5px; font-weight: 600; color: var(--text-secondary); }
.prob span { width: 38px; height: 5px; border-radius: 999px; background: var(--bg-surface-hover); position: relative; overflow: hidden; }
.prob span::after { content: ""; position: absolute; inset: 0 auto 0 0; width: var(--p); background: var(--primary); border-radius: inherit; }

/* Activity feed */
.feed { list-style: none; padding: 4px 20px 16px; }
.feed li { display: grid; grid-template-columns: 14px minmax(0, 1fr); gap: 12px; position: relative; padding-bottom: 16px; }
.feed li:not(:last-child)::before { content: ""; position: absolute; left: 6px; top: 16px; bottom: 0; width: 2px; background: var(--border-subtle); }
.feed-dot { width: 14px; height: 14px; border-radius: 50%; border: 3px solid var(--bg-surface); background: var(--border-strong); box-shadow: 0 0 0 1px var(--border-default); margin-top: 3px; position: relative; }
.feed li.is-won .feed-dot { background: var(--primary); box-shadow: 0 0 0 1px var(--primary); }
.feed-head { display: flex; justify-content: space-between; gap: 10px; font-size: 13.5px; }
.feed-head time { font-size: 12px; color: var(--text-subdued); white-space: nowrap; }
.feed-body p { font-size: 13px; color: var(--text-secondary); margin-top: 2px; overflow-wrap: anywhere; }
.feed-by { font-size: 12px; color: var(--text-subdued); }

/* Empty states */
.empty { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; padding: 8px 20px 22px; }
.empty strong { font-size: 14px; }
.empty span { font-size: 13px; color: var(--text-secondary); margin-bottom: 8px; }

/* ---------- Entrance motion ---------- */
@media (prefers-reduced-motion: no-preference) {
  .dash > * { animation: dash-in .6s cubic-bezier(.16, 1, .3, 1) both; }
  .dash > *:nth-child(2) { animation-delay: .05s; }
  .dash > *:nth-child(3) { animation-delay: .1s; }
  .dash > *:nth-child(4) { animation-delay: .15s; }
  .dash > *:nth-child(5) { animation-delay: .2s; }
  .funnel-bar span { animation: bar-grow 1s cubic-bezier(.16, 1, .3, 1) both .25s; }
  @keyframes dash-in { from { opacity: 0; transform: translateY(10px); } }
  @keyframes bar-grow { from { transform: scaleX(0); } }
}

/* ---------- Responsive ---------- */
@media (max-width: 1280px) {
  .dash-grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .dash-grid-3 > :last-child { grid-column: 1 / -1; }
}
@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .dash-grid-main { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 767px) {
  .dash { gap: 16px; }
  .dash-hero { align-items: stretch; flex-direction: column; }
  .dash-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .dash-actions .btn:first-child { grid-column: 1 / -1; }
  .kpis { gap: 10px; }
  .kpi { padding: 14px; border-radius: 14px; }
  .kpi-label { font-size: 12px; }
  .kpi-value { font-size: 1.3rem; }
  .kpi-foot { font-size: 11.5px; }
  .dash-grid-3 { grid-template-columns: minmax(0, 1fr); }
  .panel-head { padding: 16px 16px 10px; }
  .list { padding: 0 4px 6px; }
  .funnel, .feed { padding-left: 16px; padding-right: 16px; }
  .chart-wrap { height: 220px; padding: 0 8px 12px 0; }
}
@media (max-width: 380px) {
  .kpis { grid-template-columns: minmax(0, 1fr); }
}

/* Period control stays on one line; the panel header wraps under the title on small screens */
.panel-head { flex-wrap: wrap; }
.segmented button { white-space: nowrap; }

/* Objectif du mois (tuile « Facturé ce mois-ci ») */
.kpi .obj-bar { display: block; height: 6px; border-radius: 6px; background: rgba(21, 82, 163, .12); margin-top: 10px; overflow: hidden; }
.kpi .obj-bar i { display: block; height: 100%; border-radius: 6px; background: linear-gradient(90deg, #1552A3, #D69A3C); transition: width .6s cubic-bezier(.2, .8, .2, 1); }
.kpi .obj-set { display: inline-block; margin-top: 8px; padding: 0; border: 0; background: none; font: inherit; font-size: 12.5px; font-weight: 600; color: #1552A3; cursor: pointer; }
.kpi .obj-set:hover { text-decoration: underline; }
[data-theme="dark"] .kpi .obj-set { color: #8DB4EA; }

/* Argent qui dort : bandeau des impayés en retard */
.argent-dort { display: flex; align-items: center; gap: 14px; margin: 14px 0 4px; padding: 14px 18px; border-radius: 14px; text-decoration: none; color: var(--text-primary);
  background: linear-gradient(90deg, rgba(180, 35, 24, .09), rgba(180, 35, 24, .02)); border: 1px solid rgba(180, 35, 24, .25); transition: transform .2s cubic-bezier(.2,.7,.2,1), box-shadow .2s; }
.argent-dort:hover { transform: translateY(-1px); box-shadow: 0 6px 18px rgba(180, 35, 24, .12); }
.argent-dort:focus-visible { outline: 2px solid #B42318; outline-offset: 2px; }
.ad-icone { flex: none; display: grid; place-items: center; width: 42px; height: 42px; border-radius: 50%; background: rgba(180, 35, 24, .12); color: #B42318; }
.ad-texte { flex: 1; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.ad-texte strong { font-size: 1.1rem; color: #B42318; }
.ad-texte span { font-size: 13px; color: var(--text-secondary); }
.ad-cta { flex: none; padding: 9px 14px; border-radius: 10px; background: #128C7E; color: #fff; font-weight: 600; font-size: 13.5px; }
@media (max-width: 640px) { .argent-dort { flex-wrap: wrap; } .ad-cta { width: 100%; text-align: center; } }
@media (prefers-reduced-motion: reduce) { .argent-dort { transition: none; } }
[dir="rtl"] .ad-fleche { display: inline-block; transform: scaleX(-1); }
