/* =============================================================================
   Kivo APP — Shell (reset + layout grid + topbar + sidebar + content + drawer)
   ============================================================================= */

/* --- Reset léger --- */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-body);
  font-size: var(--fs-base);
  line-height: 1.5;
  color: var(--text);
  background: var(--app-bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
h1, h2, h3, h4, p, ul, ol, figure { margin: 0; }
ul, ol { list-style: none; padding: 0; }
a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; cursor: pointer; background: none; border: none; }
input, select, textarea { font: inherit; color: inherit; }
img, svg { display: block; max-width: 100%; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--r-xs);
}

/* Lien d'évitement (a11y) */
.skip-link {
  position: absolute; left: var(--sp-2); top: -60px;
  background: var(--accent); color: var(--text-on-accent);
  padding: var(--sp-2) var(--sp-4); border-radius: var(--r-sm);
  z-index: 200; transition: top var(--t-fast) var(--ease);
}
.skip-link:focus { top: var(--sp-2); }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

/* =============================================================================
   LAYOUT GRID
   ============================================================================= */
.app {
  display: grid;
  grid-template:
    "topbar  topbar" var(--topbar-h)
    "sidebar main"   1fr
    / var(--sidebar-w) 1fr;
  height: 100vh;
  overflow: hidden;
  transition: grid-template-columns var(--t) var(--ease);
}
.app--rail { grid-template-columns: var(--sidebar-rail-w) 1fr; }

/* =============================================================================
   TOPBAR (56px)
   ============================================================================= */
.topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 0 var(--sp-4);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: var(--z-topbar);
}
.topbar__left { display: flex; align-items: center; gap: var(--sp-2); }
.topbar__center { flex: 1; display: flex; justify-content: center; }
.topbar__right { display: flex; align-items: center; gap: var(--sp-1); }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; border-radius: var(--r-sm);
  color: var(--text-soft); position: relative;
  transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 20px; height: 20px; }

.brand {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: 0 var(--sp-2); font-family: var(--font-title);
  font-weight: var(--fw-bold); font-size: var(--fs-lg); letter-spacing: -.02em;
}
.brand__mark {
  width: 28px; height: 28px; border-radius: var(--r-sm);
  background: var(--brand-grad); display: grid; place-items: center;
  color: #fff; font-weight: var(--fw-bold); box-shadow: 0 0 18px -4px var(--accent);
}
.brand__mark svg { width: 18px; height: 18px; }

/* Switch d'organisation */
.org-switch {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3); border-radius: var(--r-sm);
  border: 1px solid var(--border); background: var(--surface-2);
  color: var(--text); font-weight: var(--fw-medium); max-width: 220px;
  transition: border-color var(--t-fast) var(--ease);
}
.org-switch:hover { border-color: var(--border-strong); }
.org-switch__logo {
  width: 22px; height: 22px; border-radius: var(--r-xs); flex: none;
  background: var(--accent-soft-2); display: grid; place-items: center;
  font-size: var(--fs-xs); font-weight: var(--fw-bold); color: var(--accent);
}
.org-switch__name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.org-switch svg { width: 14px; height: 14px; color: var(--text-dim); flex: none; }

/* Déclencheur recherche ⌘K */
.search-trigger {
  display: flex; align-items: center; gap: var(--sp-2);
  width: min(420px, 40vw); padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-sm); border: 1px solid var(--border);
  background: var(--surface-inset); color: var(--text-dim);
  transition: border-color var(--t-fast) var(--ease), background var(--t-fast) var(--ease);
}
.search-trigger:hover { border-color: var(--border-strong); background: var(--surface-2); }
.search-trigger svg { width: 16px; height: 16px; flex: none; }
.search-trigger__label { flex: 1; text-align: left; }
.kbd {
  display: inline-flex; align-items: center; gap: 2px; padding: 2px 6px;
  font-family: var(--font-mono); font-size: var(--fs-xs);
  border: 1px solid var(--border); border-radius: var(--r-xs);
  background: var(--surface); color: var(--text-soft);
}

/* Indicateur d'état Kivo */
.kivo-state {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: var(--sp-1) var(--sp-2) var(--sp-1) var(--sp-3);
  border-radius: var(--r-pill); border: 1px solid var(--border);
  background: var(--surface-2); margin-right: var(--sp-1);
}
.kivo-state__dot {
  width: 9px; height: 9px; border-radius: 50%; flex: none;
  background: var(--success); box-shadow: 0 0 0 0 var(--success);
  animation: pulse-dot 2.4s var(--ease) infinite;
}
.kivo-state__label { font-size: var(--fs-sm); font-weight: var(--fw-medium); white-space: nowrap; }
.kivo-state__pause {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 8px; border-radius: var(--r-pill); font-size: var(--fs-xs);
  font-weight: var(--fw-semibold); background: var(--neutral-bg); color: var(--text-soft);
  transition: background var(--t-fast) var(--ease);
}
.kivo-state__pause:hover { background: var(--surface-3); }
.kivo-state__pause svg { width: 12px; height: 12px; }
/* État pause */
[data-kivo="paused"] .kivo-state { border-color: var(--warn-border); background: var(--warn-bg); }
[data-kivo="paused"] .kivo-state__dot { background: var(--warn); animation: none; }
[data-kivo="incident"] .kivo-state { border-color: var(--danger-border); background: var(--danger-bg); }
[data-kivo="incident"] .kivo-state__dot { background: var(--danger); animation: none; }

@keyframes pulse-dot {
  0% { box-shadow: 0 0 0 0 rgba(34,197,94,.5); }
  70% { box-shadow: 0 0 0 7px rgba(34,197,94,0); }
  100% { box-shadow: 0 0 0 0 rgba(34,197,94,0); }
}

/* Badge compteur (cloche / nav) */
.count-badge {
  position: absolute; top: 3px; right: 3px; min-width: 16px; height: 16px;
  padding: 0 4px; border-radius: var(--r-pill); background: var(--danger);
  color: #fff; font-size: 10px; font-weight: var(--fw-bold);
  display: inline-flex; align-items: center; justify-content: center;
  border: 2px solid var(--surface);
}

/* Menu profil (avatar) */
.avatar-btn { padding: 0; width: 34px; height: 34px; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center;
  background: var(--brand-grad); color: #fff; font-weight: var(--fw-bold);
  font-size: var(--fs-sm);
}

/* =============================================================================
   SIDEBAR (240px → rail 64px)
   ============================================================================= */
.sidebar {
  grid-area: sidebar;
  display: flex; flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto; overflow-x: hidden;
  z-index: var(--z-sidebar);
}
.sidebar__nav { flex: 1; padding: var(--sp-3) var(--sp-2); }
.sidebar__group + .sidebar__group { margin-top: var(--sp-2); padding-top: var(--sp-2); border-top: 1px solid var(--border); }
.sidebar__group-label {
  font-size: var(--fs-xs); font-weight: var(--fw-semibold); text-transform: uppercase;
  letter-spacing: .06em; color: var(--text-faint);
  padding: var(--sp-2) var(--sp-3) var(--sp-1);
}
.app--rail .sidebar__group-label { opacity: 0; height: 8px; padding: 0; }

.nav-item {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-3); border-radius: var(--r-sm);
  color: var(--text-soft); font-weight: var(--fw-medium);
  position: relative; white-space: nowrap;
  transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease);
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item svg { width: 19px; height: 19px; flex: none; }
.nav-item__label { flex: 1; overflow: hidden; }
.nav-item[aria-current="page"] {
  background: var(--accent-soft); color: var(--text);
  box-shadow: inset 3px 0 0 var(--accent);
}
.nav-item[aria-current="page"] svg { color: var(--accent); }
.app--rail .nav-item__label,
.app--rail .nav-item .badge { display: none; }
.app--rail .nav-item { justify-content: center; padding: var(--sp-2); }

/* Bandeau onboarding bas de sidebar */
.sidebar__onboarding {
  margin: var(--sp-2); padding: var(--sp-3); border-radius: var(--r-md);
  background: var(--surface-2); border: 1px solid var(--border);
}
.app--rail .sidebar__onboarding { display: none; }
.sidebar__onboarding-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--sp-2); }
.sidebar__onboarding-title { font-size: var(--fs-sm); font-weight: var(--fw-semibold); }
.sidebar__onboarding-count { font-size: var(--fs-xs); color: var(--text-dim); }
.progress-track { height: 6px; border-radius: var(--r-pill); background: var(--surface-inset); overflow: hidden; }
.progress-fill { height: 100%; border-radius: inherit; background: var(--brand-grad); transition: width var(--t-slow) var(--ease); }

/* =============================================================================
   CONTENU (seule zone qui scrolle)
   ============================================================================= */
.content {
  grid-area: main;
  overflow-y: auto;
  background: var(--app-bg);
  position: relative;
}
.content__inner { max-width: var(--content-max); margin: 0 auto; padding: var(--sp-5) var(--sp-6) var(--sp-10); }
/* Écrans pleine largeur (inbox, builder) opt-out du max-width */
.content--full .content__inner { max-width: none; padding: 0; height: 100%; }

/* Breadcrumbs */
.breadcrumb { display: flex; align-items: center; gap: var(--sp-2); font-size: var(--fs-sm); color: var(--text-dim); margin-bottom: var(--sp-3); flex-wrap: wrap; }
.breadcrumb a:hover { color: var(--text); }
.breadcrumb__sep { color: var(--text-faint); }
.breadcrumb [aria-current="page"] { color: var(--text-soft); }

/* Page header : titre + actions */
.page-header { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--sp-4); margin-bottom: var(--sp-5); flex-wrap: wrap; }
.page-header__titles { min-width: 0; }
.page-title { font-family: var(--font-title); font-size: var(--fs-2xl); font-weight: var(--fw-bold); letter-spacing: -.02em; line-height: 1.2; }
.page-subtitle { color: var(--text-dim); font-size: var(--fs-md); margin-top: var(--sp-1); }
.page-header__actions { display: flex; align-items: center; gap: var(--sp-2); flex: none; }

/* =============================================================================
   DRAWER (panneau latéral droit, hors flux)
   ============================================================================= */
.drawer {
  position: fixed; top: 0; right: 0; height: 100vh; width: var(--drawer-w);
  max-width: 92vw; background: var(--surface); border-left: 1px solid var(--border);
  box-shadow: var(--shadow-drawer); z-index: var(--z-drawer);
  transform: translateX(100%); transition: transform var(--t) var(--ease);
  display: flex; flex-direction: column; visibility: hidden;
}
.drawer.is-open { transform: translateX(0); visibility: visible; }
.drawer__header { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); padding: var(--sp-4); border-bottom: 1px solid var(--border); }
.drawer__title { font-family: var(--font-title); font-size: var(--fs-lg); font-weight: var(--fw-semibold); }
.drawer__body { flex: 1; overflow-y: auto; padding: var(--sp-4); }
.drawer__footer { padding: var(--sp-3) var(--sp-4); border-top: 1px solid var(--border); display: flex; gap: var(--sp-2); justify-content: flex-end; }
.scrim {
  position: fixed; inset: 0; background: rgba(3, 6, 15, .55); backdrop-filter: blur(2px);
  z-index: calc(var(--z-drawer) - 1); opacity: 0; visibility: hidden;
  transition: opacity var(--t) var(--ease), visibility var(--t) var(--ease);
}
.scrim.is-open { opacity: 1; visibility: visible; }

/* =============================================================================
   MOBILE — sidebar → onglets bas
   ============================================================================= */
.mobile-tabs { display: none; }

@media (max-width: 768px) {
  .app {
    grid-template:
      "topbar" var(--topbar-h)
      "main"   1fr
      / 1fr;
    padding-bottom: 60px; /* place pour la tab bar */
  }
  .sidebar { display: none; }
  .topbar__center { display: none; }
  .org-switch__name { display: none; }
  .content__inner { padding: var(--sp-4); }

  .mobile-tabs {
    position: fixed; bottom: 0; left: 0; right: 0; height: 60px;
    display: flex; background: var(--surface); border-top: 1px solid var(--border);
    z-index: var(--z-sidebar); padding-bottom: env(safe-area-inset-bottom, 0);
  }
  .mobile-tab {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 2px; color: var(--text-dim); font-size: 10px; font-weight: var(--fw-medium);
    position: relative; min-height: 44px;
  }
  .mobile-tab svg { width: 22px; height: 22px; }
  .mobile-tab[aria-current="page"] { color: var(--accent); }
  .mobile-tab .count-badge { top: 4px; right: 50%; margin-right: -22px; }
}

/* Réduction d'animation */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}
