/* ============================================================
   ChurnShield — App UI
   Dashboard / Settings / Audit shared styles. Replaces ~750+
   lines of duplicated inline CSS across app pages.
   Depends on tokens.css, base.css, components.css.
   ============================================================ */

/* ── App topbar ────────────────────────────────────── */
.app-topbar {
  /* Stationary topbar — matches marketing nav behavior. Scrolls away
     with the page rather than sticking. */
  position: relative;
  z-index: var(--z-nav);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-4) var(--space-6);
  background: rgba(10, 10, 15, 0.88);
  backdrop-filter: blur(12px) saturate(1.4);
  -webkit-backdrop-filter: blur(12px) saturate(1.4);
  border-bottom: 1px solid var(--color-border-subtle);
}

.app-topbar-brand {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-weight: var(--weight-bold);
  font-size: var(--text-lg);
  color: var(--color-text);
  text-decoration: none;
  letter-spacing: var(--tracking-tight);
}
.app-topbar-brand img { height: 24px; width: auto; }

.app-topbar-right {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  justify-content: flex-end;
  flex-wrap: wrap;
}

/* Primary action slot — stays visible at every viewport. The
   dashboard places its Refresh button here so it's reachable in
   one tap on mobile while secondary actions collapse into the
   hamburger dropdown. */
.app-topbar-primary {
  display: inline-flex;
  align-items: center;
}
.app-topbar-primary .btn { padding: 8px 16px; font-size: 13px; min-height: 36px; }

.app-topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
  justify-content: flex-end;
}

/* Hamburger toggle — hidden on desktop, replaces inline actions on
   mobile so the topbar stays clean instead of wrapping into 2 rows. */
.app-topbar-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  background: transparent;
  border: 1px solid var(--border);
  color: var(--fg);
  cursor: pointer;
  padding: 0;
}
.app-topbar-toggle:hover { background: var(--surface-alt); }
.app-topbar-toggle svg { width: 20px; height: 20px; }

@media (max-width: 720px) {
  .app-topbar-toggle { display: inline-flex; }
  .app-topbar-actions {
    display: none;
    position: absolute;
    top: calc(100% + var(--space-2));
    right: 0;
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-2);
    min-width: 220px;
    padding: var(--space-3);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    z-index: var(--z-overlay);
  }
  .app-topbar-actions.is-open { display: flex; }
  .app-topbar-actions .btn,
  .app-topbar-actions a.btn {
    width: 100%;
    justify-content: flex-start;
    text-align: left;
  }
  /* The Export CSV dropdown wrapper that lives inside the menu. */
  .app-topbar-actions .export-wrap { width: 100%; }
}

.app-status {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}
.app-status .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-success);
  box-shadow: 0 0 10px var(--color-success);
}
@media (prefers-reduced-motion: no-preference) {
  .app-status .dot { animation: pulse 2s var(--ease-in-out) infinite; }
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.4; }
}

/* ── App page shell ────────────────────────────────── */
.app-shell {
  max-width: 1360px;
  margin: 0 auto;
  padding: var(--space-8) var(--space-6);
}

.app-page-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  gap: var(--space-4);
  margin-bottom: var(--space-8);
}
.app-page-title {
  font-size: var(--text-3xl);
  letter-spacing: var(--tracking-tight);
  font-weight: var(--weight-semibold);
}
.app-page-subtitle {
  color: var(--color-text-muted);
  margin-top: var(--space-2);
  font-size: var(--text-sm);
}

/* ── KPI grid ──────────────────────────────────────── */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: var(--space-4);
  margin-bottom: var(--space-8);
}

.kpi-card {
  padding: var(--space-6);
  background: var(--color-surface-2);
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-inset);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}
.kpi-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-widest);
  text-transform: uppercase;
  color: var(--color-text-muted);
}
.kpi-value {
  font-size: var(--text-3xl);
  letter-spacing: var(--tracking-tighter);
  font-weight: var(--weight-bold);
  line-height: 1;
  color: var(--color-text);
}
.kpi-value--accent { color: var(--color-accent); }
.kpi-value--danger { color: var(--color-danger); }
.kpi-value--warning { color: var(--color-warning); }
.kpi-sub {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

/* ── App section card (dashboard / settings panels) ── */
.app-section {
  background: var(--color-surface-2);
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-lg);
  overflow: hidden;
  margin-bottom: var(--space-6);
}
.app-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-5) var(--space-6);
  border-bottom: 1px solid var(--color-border-subtle);
}
.app-section-title {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.app-section-body {
  padding: var(--space-6);
}
.app-section-body--flush {
  padding: 0;
}

/* Settings form groups */
.settings-group {
  padding: var(--space-5) var(--space-6);
  border-bottom: 1px solid var(--color-border-subtle);
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: start;
  gap: var(--space-6);
}
.settings-group:last-child { border-bottom: 0; }

.settings-group-info {
  min-width: 0;
}
.settings-group-title {
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--color-text);
  margin-bottom: var(--space-1);
}
.settings-group-desc {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  max-width: 52ch;
  line-height: var(--leading-sm);
}

@media (max-width: 640px) {
  .settings-group {
    grid-template-columns: 1fr;
    gap: var(--space-3);
  }
}

/* ── Data table ────────────────────────────────────── */
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}
.data-table th {
  text-align: left;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-widest);
  text-transform: uppercase;
  color: var(--color-text-muted);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--color-border-subtle);
}
.data-table td {
  padding: var(--space-4);
  border-bottom: 1px solid var(--color-border-subtle);
  color: var(--color-text);
  vertical-align: middle;
}
.data-table tr:hover td { background: rgba(255, 255, 255, 0.02); }
.data-table tr:last-child td { border-bottom: 0; }

.table-wrap {
  overflow-x: auto;
}

/* ── Activity feed ─────────────────────────────────── */
.activity-list {
  list-style: none;
  padding: 0;
  margin: 0;
}
.activity-item {
  display: flex;
  gap: var(--space-4);
  padding: var(--space-4) var(--space-6);
  border-bottom: 1px solid var(--color-border-subtle);
}
.activity-item:last-child { border-bottom: 0; }
.activity-icon {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-surface-3);
  color: var(--color-text-muted);
}
.activity-icon--success { background: var(--color-success-soft); color: var(--color-success); }
.activity-icon--warning { background: var(--color-warning-soft); color: var(--color-warning); }
.activity-icon--danger  { background: var(--color-danger-soft);  color: var(--color-danger); }
.activity-icon svg { width: 16px; height: 16px; }

.activity-body { flex: 1; min-width: 0; }
.activity-title {
  font-size: var(--text-sm);
  color: var(--color-text);
  margin-bottom: var(--space-1);
}
.activity-meta {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

/* ── Empty / loading / error state ─────────────────── */
.state-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: var(--space-16) var(--space-6);
  gap: var(--space-4);
}
.state-icon {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--color-surface-3);
  border: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-muted);
}
.state-icon svg { width: 28px; height: 28px; }
.state-title {
  font-size: var(--text-lg);
  font-weight: var(--weight-semibold);
  color: var(--color-text);
}
.state-desc {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  max-width: 42ch;
}

/* Loading skeleton */
.skeleton {
  background: linear-gradient(
    90deg,
    var(--color-surface-2) 0%,
    var(--color-surface-3) 50%,
    var(--color-surface-2) 100%
  );
  background-size: 200% 100%;
  border-radius: var(--radius-sm);
  color: transparent;
  user-select: none;
}
@media (prefers-reduced-motion: no-preference) {
  .skeleton { animation: shimmer 1.4s var(--ease-in-out) infinite; }
}
@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
.skeleton-line { height: 12px; border-radius: 6px; }

/* ── Step progress (onboarding) ───────────────────── */
.step-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  list-style: none;
  padding: 0;
  margin: 0;
}
.step-item {
  display: flex;
  gap: var(--space-4);
  align-items: flex-start;
  padding: var(--space-4);
  border-radius: var(--radius-md);
  background: var(--color-surface-2);
  border: 1px solid var(--color-border-subtle);
}
.step-item[aria-current="step"] {
  border-color: var(--color-accent);
  background: var(--color-accent-soft);
}
.step-item[data-done="true"] {
  opacity: 0.7;
}
.step-marker {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--color-surface-3);
  border: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--color-text-muted);
}
.step-item[aria-current="step"] .step-marker {
  background: var(--color-accent);
  color: var(--color-bg);
  border-color: var(--color-accent);
}
.step-item[data-done="true"] .step-marker {
  background: var(--color-success-soft);
  color: var(--color-success);
  border-color: var(--color-accent);
}
.step-body { flex: 1; }
.step-title {
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  color: var(--color-text);
  margin-bottom: var(--space-1);
}
.step-desc {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  line-height: var(--leading-sm);
}

/* ── Chart wrapper ────────────────────────────────── */
.chart-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 7;
  padding: var(--space-6);
  background: var(--color-surface-1);
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-lg);
}

/* ── Code sample (docs, audit report) ─────────────── */
.code-sample {
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  background: var(--color-surface-1);
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-md);
  padding: var(--space-4);
  overflow-x: auto;
  color: var(--color-text);
}
.code-sample .k { color: var(--color-accent); }     /* keyword */
.code-sample .s { color: #f5b846; }                  /* string */
.code-sample .c { color: var(--color-text-dim); }   /* comment */
.code-sample .n { color: var(--color-info); }       /* number */
