/* ============================================================
   A3P/RING Stats Dashboard — Design System
   Based on Ring landing page design tokens
   ============================================================ */

/* --- CSS Variables --- */
:root {
  /* Colors — default A3P purple, overridden by JS for RING */
  --c-primary: #7345FF;
  --c-primary-light: #9370FF;
  --c-primary-dark: #5A2ED4;
  --c-primary-50: #F3EFFF;
  --c-primary-100: #E4DBFF;
  --c-primary-900: #2A1066;
  --c-accent: #7345FF;
  --c-accent-light: #9370FF;
  --c-accent-dark: #5A2ED4;

  --c-bg-light: #F5F6FA;
  --c-bg-white: #FFFFFF;
  --c-border-light: #E8EAF0;
  --c-text: #1a1a2e;
  --c-text-secondary: #64748B;
  --c-text-muted: #94A3B8;

  /* Status colors */
  --c-success: #10B981;
  --c-warning: #F59E0B;
  --c-danger: #EF4444;
  --c-info: #3B82F6;

  /* Chart palette */
  --chart-1: var(--c-primary);
  --chart-2: var(--c-primary-light);
  --chart-3: #10B981;
  --chart-4: #F59E0B;
  --chart-5: #EF4444;
  --chart-6: #8B5CF6;
  --chart-7: #EC4899;
  --chart-8: #06B6D4;
  --chart-9: #84CC16;
  --chart-10: #F97316;

  /* Gradients */
  --g-primary: linear-gradient(135deg, var(--c-primary), var(--c-primary-dark));

  /* Shadows — softer for white sidebar */
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.05);
  --shadow-md: 0 2px 8px rgba(0,0,0,0.06), 0 4px 16px rgba(0,0,0,0.04);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.06), 0 8px 24px rgba(0,0,0,0.08);

  /* Typography */
  --font: 'Outfit', system-ui, -apple-system, sans-serif;
  --fs-xs: 12px;
  --fs-sm: 13px;
  --fs-base: 14px;
  --fs-lg: 16px;
  --fs-xl: 18px;
  --fs-2xl: 22px;
  --fs-3xl: 28px;
  --fs-4xl: 36px;

  /* Spacing */
  --sp-xs: 0.5rem;
  --sp-sm: 1rem;
  --sp-md: 1.5rem;
  --sp-lg: 2rem;
  --sp-xl: 3rem;

  /* Layout */
  --sidebar-width: 240px;
  --header-height: 64px;
  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;

  /* Transitions */
  --t-fast: 0.15s ease;
  --t-base: 0.25s ease;
}

/* --- Reset & Base --- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { font-size: 14px; }

body {
  font-family: var(--font);
  background: var(--c-bg-light);
  color: var(--c-text);
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
}

a { color: var(--c-accent); text-decoration: none; }
a:hover { color: var(--c-accent-dark); }

/* --- Layout: Sidebar + Main --- */
.sidebar {
  width: var(--sidebar-width);
  background: var(--c-bg-white);
  color: var(--c-text);
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  z-index: 100;
  transition: transform var(--t-base);
  border-right: 1px solid var(--c-border-light);
}

.sidebar__logo {
  padding: var(--sp-md);
  border-bottom: 1px solid var(--c-border-light);
  display: flex;
  align-items: center;
  gap: var(--sp-xs);
}

.sidebar__logo-text {
  font-size: var(--fs-xl);
  font-weight: 700;
  color: var(--c-primary);
}

.sidebar__logo-sub {
  font-size: var(--fs-xs);
  color: var(--c-text-muted);
  font-weight: 400;
}

.sidebar__nav {
  flex: 1;
  padding: var(--sp-sm) 0;
  overflow-y: auto;
}

.sidebar__link {
  display: flex;
  align-items: center;
  gap: var(--sp-sm);
  padding: 10px var(--sp-md);
  color: var(--c-text-secondary);
  font-size: var(--fs-sm);
  font-weight: 500;
  transition: all var(--t-fast);
  border-left: 3px solid transparent;
}

.sidebar__link:hover {
  color: var(--c-text);
  background: var(--c-bg-light);
}

.sidebar__link.active {
  color: var(--c-primary);
  background: var(--c-primary-50);
  border-left-color: var(--c-primary);
  font-weight: 600;
}

.sidebar__link svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  opacity: 0.5;
}

.sidebar__link:hover svg { opacity: 0.8; }
.sidebar__link.active svg { opacity: 1; color: var(--c-primary); }

.sidebar__footer {
  padding: var(--sp-sm) var(--sp-md);
  border-top: 1px solid var(--c-border-light);
  font-size: var(--fs-xs);
  color: var(--c-text-muted);
}

.sidebar__sync {
  display: flex;
  align-items: center;
  gap: 6px;
}

.sidebar__sync-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--c-success);
  flex-shrink: 0;
}

.sidebar__sync-dot.stale { background: var(--c-warning); }
.sidebar__sync-dot.error { background: var(--c-danger); }

/* --- Main content --- */
.main {
  margin-left: var(--sidebar-width);
  flex: 1;
  min-height: 100vh;
}

.main__header {
  background: var(--c-bg-white);
  border-bottom: 1px solid var(--c-border-light);
  padding: var(--sp-md) var(--sp-lg);
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.main__title {
  font-size: var(--fs-2xl);
  font-weight: 700;
}

.main__subtitle {
  font-size: var(--fs-sm);
  color: var(--c-text-secondary);
  margin-top: 2px;
}

.main__actions {
  display: flex;
  align-items: center;
  gap: var(--sp-sm);
}

.main__content {
  padding: var(--sp-lg);
}

/* Mobile hamburger */
.hamburger {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
}

.hamburger svg { width: 24px; height: 24px; color: var(--c-text); }

/* --- KPI Cards --- */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: var(--sp-md);
  margin-bottom: var(--sp-lg);
}

.kpi-card {
  background: var(--c-bg-white);
  border: 1px solid var(--c-border-light);
  border-radius: var(--radius-lg);
  padding: var(--sp-md);
  box-shadow: var(--shadow-sm);
  transition: box-shadow var(--t-base), transform var(--t-base);
}

.kpi-card:hover {
  box-shadow: var(--shadow-md);
}

.kpi-card__label {
  font-size: var(--fs-xs);
  color: var(--c-text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: 600;
  margin-bottom: 8px;
}

.kpi-card__value {
  font-size: var(--fs-4xl);
  font-weight: 700;
  line-height: 1.1;
  background: var(--g-primary);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.kpi-card__trend {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 8px;
  font-size: var(--fs-xs);
  font-weight: 500;
}

.kpi-card__trend.up { color: var(--c-success); }
.kpi-card__trend.down { color: var(--c-danger); }
.kpi-card__trend.neutral { color: var(--c-text-muted); }

.kpi-card--accent {
  background: var(--g-primary);
  border-color: transparent;
}

.kpi-card--accent .kpi-card__label {
  color: rgba(255,255,255,0.7);
}

.kpi-card--accent .kpi-card__value {
  background: none;
  -webkit-text-fill-color: white;
  color: white;
}

.kpi-card--accent .kpi-card__trend {
  color: rgba(255,255,255,0.8);
}

/* --- Section / Chart panels --- */
.section-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--sp-md);
  margin-bottom: var(--sp-lg);
}

.section-grid--3 {
  grid-template-columns: 1fr 1fr 1fr;
}

.section-grid--full {
  grid-template-columns: 1fr;
}

.panel {
  background: var(--c-bg-white);
  border: 1px solid var(--c-border-light);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}

.panel__header {
  padding: var(--sp-md);
  border-bottom: 1px solid var(--c-border-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.panel__title {
  font-size: var(--fs-lg);
  font-weight: 600;
}

.panel__badge {
  font-size: var(--fs-xs);
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--c-primary-50);
  color: var(--c-primary-dark);
  font-weight: 600;
}

.panel__body {
  padding: var(--sp-md);
  overflow: hidden;
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}

.panel__body--flush {
  padding: 0;
}

/* Chart container */
.chart-container {
  position: relative;
  width: 100%;
  min-height: 280px;
}

.chart-container--sm { min-height: 200px; }
.chart-container--lg { min-height: 380px; }

/* --- Data Table --- */
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-sm);
}

.data-table th {
  text-align: left;
  font-weight: 600;
  color: var(--c-text-secondary);
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 10px var(--sp-md);
  border-bottom: 2px solid var(--c-border-light);
}

.data-table td {
  padding: 10px var(--sp-md);
  border-bottom: 1px solid var(--c-border-light);
}

.data-table tr:last-child td { border-bottom: none; }

.data-table tr:hover td {
  background: var(--c-primary-50);
}

.data-table__rank {
  width: 40px;
  color: var(--c-text-muted);
  font-weight: 600;
}

.data-table__bar {
  height: 6px;
  border-radius: 3px;
  background: var(--g-primary);
  transition: width var(--t-base);
}

/* --- Rate Bars (completion rates) --- */
.rate-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--sp-md);
}

.rate-item {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.rate-item__label {
  font-size: var(--fs-sm);
  color: var(--c-text-secondary);
  font-weight: 500;
}

.rate-item__bar-bg {
  height: 8px;
  border-radius: 4px;
  background: var(--c-border-light);
  overflow: hidden;
}

.rate-item__bar {
  height: 100%;
  border-radius: 4px;
  background: var(--g-primary);
  transition: width 0.6s cubic-bezier(0.16, 1, 0.3, 1);
}

.rate-item__value {
  font-size: var(--fs-xl);
  font-weight: 700;
  color: var(--c-primary-dark);
}

/* --- Event cards --- */
.event-card {
  display: flex;
  align-items: center;
  gap: var(--sp-md);
  padding: var(--sp-md);
  border-bottom: 1px solid var(--c-border-light);
}

.event-card:last-child { border-bottom: none; }

.event-card__date {
  text-align: center;
  min-width: 56px;
  padding: 8px;
  background: var(--c-primary-50);
  border-radius: var(--radius-md);
}

.event-card__day {
  font-size: var(--fs-2xl);
  font-weight: 700;
  color: var(--c-primary-dark);
  line-height: 1;
}

.event-card__month {
  font-size: var(--fs-xs);
  color: var(--c-text-secondary);
  text-transform: uppercase;
  font-weight: 600;
}

.event-card__info { flex: 1; }

.event-card__name {
  font-weight: 600;
  font-size: var(--fs-base);
}

.event-card__meta {
  font-size: var(--fs-xs);
  color: var(--c-text-secondary);
  margin-top: 2px;
}

.event-card__status {
  font-size: var(--fs-xs);
  padding: 3px 10px;
  border-radius: 999px;
  font-weight: 600;
}

.event-card__status--published {
  background: #D1FAE5;
  color: #065F46;
}

.event-card__status--draft {
  background: #FEF3C7;
  color: #92400E;
}

/* --- Status badges --- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: var(--fs-xs);
  font-weight: 600;
}

.badge--success { background: #D1FAE5; color: #065F46; }
.badge--warning { background: #FEF3C7; color: #92400E; }
.badge--danger { background: #FEE2E2; color: #991B1B; }
.badge--info { background: var(--c-primary-50); color: var(--c-primary-dark); }

/* --- Loading state --- */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  color: var(--c-text-muted);
}

.loading__spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--c-border-light);
  border-top-color: var(--c-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.skeleton {
  background: linear-gradient(90deg, var(--c-border-light) 25%, #f1f5f9 50%, var(--c-border-light) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: var(--radius-sm);
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* --- Refresh button --- */
.btn-refresh {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: var(--c-bg-white);
  border: 1px solid var(--c-border-light);
  border-radius: var(--radius-md);
  font-size: var(--fs-sm);
  font-weight: 500;
  color: var(--c-text-secondary);
  cursor: pointer;
  transition: all var(--t-fast);
  font-family: var(--font);
}

.btn-refresh:hover {
  border-color: var(--c-primary);
  color: var(--c-primary-dark);
}

.btn-refresh svg {
  width: 14px;
  height: 14px;
}

.btn-refresh.refreshing svg {
  animation: spin 0.8s linear infinite;
}

/* --- Responsive --- */
@media (max-width: 1024px) {
  .section-grid { grid-template-columns: 1fr; }
  .section-grid--3 { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .main {
    margin-left: 0;
  }

  .hamburger { display: block; }

  .kpi-grid {
    grid-template-columns: 1fr 1fr;
  }

  .rate-grid { grid-template-columns: 1fr; }
}

@media (max-width: 480px) {
  .kpi-grid { grid-template-columns: 1fr; }
  .main__content { padding: var(--sp-sm); }
}

/* --- Info Tooltip --- */
.info-tip {
  position: relative;
  display: inline-flex;
  align-items: center;
  cursor: help;
  margin-left: 4px;
  vertical-align: middle;
}

.info-tip svg {
  width: 14px;
  height: 14px;
  color: var(--c-text-muted);
  transition: color var(--t-fast);
  flex-shrink: 0;
}

.info-tip:hover svg {
  color: var(--c-primary);
}

.info-tip::after {
  content: attr(data-tip);
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: #1E293B;
  color: #F8FAFC;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: var(--fs-xs);
  font-weight: 400;
  text-transform: none;
  letter-spacing: normal;
  white-space: pre-line;
  width: max-content;
  max-width: 360px;
  line-height: 1.4;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--t-fast);
  z-index: 1000;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.info-tip::before {
  content: '';
  position: absolute;
  bottom: calc(100% + 2px);
  left: 50%;
  transform: translateX(-50%);
  border: 5px solid transparent;
  border-top-color: #1E293B;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--t-fast);
  z-index: 1000;
}

.info-tip:hover::after,
.info-tip:hover::before {
  opacity: 1;
}

/* Elevate parent container so tooltip renders above siblings AND sticky header */
.kpi-card:has(.info-tip:hover),
.panel__header:has(.info-tip:hover),
.panel__body:has(.info-tip:hover),
.rate-item:has(.info-tip:hover),
h2:has(.info-tip:hover) {
  position: relative;
  z-index: 100;
}

/* Unclip panel body when tooltip is active inside it */
.panel__body:has(.info-tip:hover) {
  overflow: visible;
}

/* Lower sticky header when a tooltip is active anywhere in content */
.main:has(.info-tip:hover) .main__header {
  z-index: 1;
}

/* Accent card variant */
.kpi-card--accent .info-tip svg {
  color: rgba(255,255,255,0.5);
}
.kpi-card--accent .info-tip:hover svg {
  color: rgba(255,255,255,0.9);
}

/* --- Heatmap --- */
.heatmap {
  display: grid;
  grid-template-columns: 40px repeat(24, 1fr);
  gap: 2px;
  font-size: var(--fs-xs);
}

.heatmap__label {
  display: flex;
  align-items: center;
  color: var(--c-text-muted);
  font-weight: 500;
}

.heatmap__cell {
  aspect-ratio: 1;
  border-radius: 3px;
  background: var(--c-border-light);
  transition: background var(--t-fast);
}

.heatmap__cell[data-level="1"] { background: var(--c-primary-100); }
.heatmap__cell[data-level="2"] { background: var(--c-primary-light); }
.heatmap__cell[data-level="3"] { background: var(--c-primary); }
.heatmap__cell[data-level="4"] { background: var(--c-primary-dark); }
.heatmap__cell[data-level="5"] { background: var(--c-primary-900); }

.heatmap__hour-labels {
  display: contents;
}

.heatmap__hour {
  text-align: center;
  color: var(--c-text-muted);
  font-size: 10px;
}

/* --- Granularity Toggle --- */
.toggle-group {
  display: inline-flex;
  gap: 0;
  border: 1px solid var(--c-border-light);
  border-radius: var(--radius-md);
  overflow: hidden;
  background: var(--c-bg-white);
}

.toggle-group__btn {
  padding: 6px 14px;
  font-size: var(--fs-xs);
  font-weight: 500;
  font-family: var(--font);
  background: transparent;
  border: none;
  color: var(--c-text-secondary);
  cursor: pointer;
  transition: all var(--t-fast);
  border-right: 1px solid var(--c-border-light);
}

.toggle-group__btn:last-child { border-right: none; }

.toggle-group__btn:hover { background: var(--c-bg-light); }

.toggle-group__btn.active {
  background: var(--c-primary);
  color: #fff;
  font-weight: 600;
}

/* --- Modal / Popup --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--sp-lg);
  animation: fadeIn 0.15s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-card {
  background: var(--c-bg-white);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  max-width: 640px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  position: relative;
  padding: var(--sp-lg);
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  font-size: 24px;
  color: var(--c-text-muted);
  cursor: pointer;
  line-height: 1;
  z-index: 1;
}

.modal-close:hover { color: var(--c-text); }

.modal-header {
  margin-bottom: var(--sp-sm);
}

/* --- Search Bar --- */
.search-bar {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: var(--c-bg-white);
  border: 1px solid var(--c-border-light);
  border-radius: var(--radius-md);
  transition: border-color var(--t-fast);
}

.search-bar:focus-within {
  border-color: var(--c-primary);
  box-shadow: 0 0 0 3px var(--c-primary-50);
}

.search-bar__input {
  flex: 1;
  border: none;
  outline: none;
  font-family: var(--font);
  font-size: var(--fs-sm);
  color: var(--c-text);
  background: transparent;
}

.search-bar__input::placeholder { color: var(--c-text-muted); }

.search-results {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--c-bg-white);
  border: 1px solid var(--c-border-light);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  z-index: 500;
  max-height: 320px;
  overflow-y: auto;
}

.search-result-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  font-size: var(--fs-sm);
  cursor: pointer;
  border-bottom: 1px solid var(--c-border-light);
  transition: background var(--t-fast);
}

.search-result-item:last-child { border-bottom: none; }
.search-result-item:hover { background: var(--c-primary-50); }

/* --- Namespace Filter --- */
.namespace-filter {
  padding: 6px 12px;
  border: 1px solid var(--c-border-light);
  border-radius: var(--radius-md);
  font-family: var(--font);
  font-size: var(--fs-xs);
  color: var(--c-text-secondary);
  background: var(--c-bg-white);
  cursor: pointer;
}

.namespace-filter:focus {
  border-color: var(--c-primary);
  outline: none;
}

/* --- Conversation List --- */
.conversation-list {
  max-height: 480px;
  overflow-y: auto;
}

.conversation-item {
  display: flex;
  align-items: center;
  gap: var(--sp-sm);
  padding: 10px var(--sp-md);
  border-bottom: 1px solid var(--c-border-light);
  font-size: var(--fs-sm);
  transition: background var(--t-fast);
}

.conversation-item:last-child { border-bottom: none; }
.conversation-item:hover { background: var(--c-primary-50); }

.conversation-item__title {
  flex: 1;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-item__user {
  color: var(--c-text-secondary);
  font-size: var(--fs-xs);
  white-space: nowrap;
}

.conversation-item__date {
  color: var(--c-text-muted);
  font-size: var(--fs-xs);
  white-space: nowrap;
}

.conversation-item__link {
  color: var(--c-primary);
  font-size: var(--fs-xs);
  white-space: nowrap;
}

/* --- Section Title --- */
.section-title {
  font-size: var(--fs-lg);
  font-weight: 600;
  margin-bottom: var(--sp-sm);
  color: var(--c-text-secondary);
}

.section-title--with-margin {
  margin-top: var(--sp-lg);
}

/* --- PDF icon inline --- */
.icon-pdf {
  display: inline-flex;
  color: var(--c-danger);
  vertical-align: middle;
  margin-left: 4px;
}

.icon-pdf svg { width: 14px; height: 14px; }
