/* ─── Base ─── */
*, *::before, *::after { box-sizing: border-box; }
body { font-family: 'Inter', system-ui, -apple-system, sans-serif; background: #F1F5F9; }

/* ─── Sidebar ─── */
.sidebar { width: 260px; height: 100vh; background: #0F172A; position: fixed; left: 0; top: 0; z-index: 100; display: flex; flex-direction: column; overflow: hidden; }
.sidebar-logo { padding: 24px 20px; border-bottom: 1px solid rgba(255,255,255,0.08); flex-shrink: 0; }
.sidebar-nav { flex: 1; padding: 16px 12px; overflow-y: auto; min-height: 0; }
.sidebar-nav::-webkit-scrollbar { width: 4px; }
.sidebar-nav::-webkit-scrollbar-track { background: transparent; }
.sidebar-nav::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 4px; }
.sidebar-nav::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.28); }
.sidebar-footer { padding: 16px 12px; border-top: 1px solid rgba(255,255,255,0.08); flex-shrink: 0; }
.nav-item { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 8px; color: #94A3B8; text-decoration: none; font-size: 14px; font-weight: 500; transition: all 0.15s; cursor: pointer; margin-bottom: 2px; }
.nav-item:hover { background: rgba(255,255,255,0.07); color: #E2E8F0; }
.nav-item.active { background: #4F46E5; color: #fff; }
.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }
.nav-section { font-size: 10px; font-weight: 600; color: #475569; text-transform: uppercase; letter-spacing: .08em; padding: 8px 12px 4px; margin-top: 8px; }

/* ─── Main layout ─── */
.main-content { margin-left: 260px; min-height: 100vh; }
.topbar { background: #fff; border-bottom: 1px solid #E2E8F0; padding: 0 28px; height: 64px; display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; z-index: 50; }
.page-content { padding: 28px; }

/* ─── Cards ─── */
.card { background: #fff; border-radius: 12px; border: 1px solid #E2E8F0; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }
.card-body { padding: 20px 24px; }
.card-header { padding: 16px 24px 0; border-bottom: 1px solid #F1F5F9; padding-bottom: 14px; }

/* ─── KPI Cards ─── */
.kpi-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.kpi-card { background: #fff; border-radius: 12px; border: 1px solid #E2E8F0; padding: 20px 22px; box-shadow: 0 1px 3px rgba(0,0,0,0.04); }
.kpi-label { font-size: 12px; font-weight: 600; color: #64748B; text-transform: uppercase; letter-spacing: .06em; }
.kpi-value { font-size: 28px; font-weight: 700; color: #0F172A; margin: 6px 0 4px; line-height: 1; }
.kpi-change { font-size: 12px; font-weight: 600; display: inline-flex; align-items: center; gap: 3px; padding: 2px 7px; border-radius: 20px; }
.kpi-change.up { color: #059669; background: #D1FAE5; }
.kpi-change.down { color: #DC2626; background: #FEE2E2; }
.kpi-icon { width: 40px; height: 40px; border-radius: 10px; display: flex; align-items: center; justify-content: center; }

/* ─── Charts ─── */
.chart-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 16px; }
.chart-wrap { position: relative; }

/* ─── Tables ─── */
.data-table { width: 100%; border-collapse: collapse; }
.data-table th { font-size: 11px; font-weight: 600; color: #64748B; text-transform: uppercase; letter-spacing: .06em; padding: 10px 16px; text-align: left; border-bottom: 1px solid #E2E8F0; }
.data-table td { padding: 12px 16px; font-size: 13px; color: #1E293B; border-bottom: 1px solid #F8FAFC; }
.data-table tbody tr:hover { background: #F8FAFC; }
.data-table tbody tr:last-child td { border-bottom: none; }

/* ─── Badges ─── */
.badge { font-size: 11px; font-weight: 600; padding: 3px 9px; border-radius: 20px; display: inline-block; }
.badge-green { background: #D1FAE5; color: #065F46; }
.badge-yellow { background: #FEF3C7; color: #92400E; }
.badge-red { background: #FEE2E2; color: #991B1B; }
.badge-gray { background: #F1F5F9; color: #475569; }
.badge-blue { background: #DBEAFE; color: #1D4ED8; }

/* ─── Platform badges ─── */
.platform-badge { font-size: 11px; font-weight: 600; padding: 3px 9px 3px 6px; border-radius: 20px; display: inline-flex; align-items: center; }
.platform-meta { background: #EEF2FF; color: #3730A3; }
.platform-google { background: #FEF3C7; color: #92400E; }
.platform-tiktok { background: #FCE7F3; color: #831843; }

/* ─── Buttons ─── */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 9px 16px; border-radius: 8px; font-size: 13px; font-weight: 600; cursor: pointer; border: none; transition: all 0.15s; text-decoration: none; }
.btn-primary { background: #4F46E5; color: #fff; }
.btn-primary:hover { background: #4338CA; }
.btn-secondary { background: #F1F5F9; color: #334155; border: 1px solid #E2E8F0; }
.btn-secondary:hover { background: #E2E8F0; }
.btn-danger { background: #FEE2E2; color: #DC2626; }
.btn-danger:hover { background: #FECACA; }
.btn-sm { padding: 6px 12px; font-size: 12px; }
.btn-icon { padding: 8px; width: 34px; height: 34px; }

/* ─── Form elements ─── */
.form-input { width: 100%; padding: 9px 12px; border: 1px solid #CBD5E1; border-radius: 8px; font-size: 14px; color: #0F172A; background: #fff; transition: border-color 0.15s; outline: none; }
.form-input:focus { border-color: #4F46E5; box-shadow: 0 0 0 3px rgba(79,70,229,0.1); }
.form-label { font-size: 13px; font-weight: 600; color: #374151; margin-bottom: 5px; display: block; }
.form-select { appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%2364748B'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 10px center; background-size: 16px; padding-right: 32px; }

/* ─── Stat row ─── */
.stat-row { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid #F1F5F9; }
.stat-row:last-child { border-bottom: none; }

/* ─── Login page ─── */
.login-bg { min-height: 100vh; background: linear-gradient(135deg, #0F172A 0%, #1E1B4B 60%, #312E81 100%); display: flex; align-items: center; justify-content: center; }
.login-card { background: #fff; border-radius: 16px; padding: 40px; width: 100%; max-width: 400px; box-shadow: 0 25px 50px rgba(0,0,0,0.3); }

/* ─── Avatar ─── */
.avatar { width: 34px; height: 34px; border-radius: 50%; background: #4F46E5; color: #fff; font-size: 12px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; }

/* ─── Connection card ─── */
.conn-card { border: 1px solid #E2E8F0; border-radius: 12px; padding: 20px 22px; background: #fff; }
.conn-card.connected { border-color: #A7F3D0; background: #F0FDF4; }
.conn-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.conn-dot.online { background: #10B981; }
.conn-dot.offline { background: #94A3B8; }

/* ─── Misc ─── */
.divider { height: 1px; background: #E2E8F0; margin: 16px 0; }
.text-muted { color: #64748B; }
.page-title { font-size: 22px; font-weight: 700; color: #0F172A; }
.page-subtitle { font-size: 14px; color: #64748B; margin-top: 2px; }
.section-title { font-size: 14px; font-weight: 700; color: #1E293B; }
.section-subtitle { font-size: 12px; color: #64748B; }
.tag { display: inline-flex; align-items: center; gap: 4px; font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 4px; }

/* ─── Upload zone ─── */
.upload-zone { border: 2px dashed #CBD5E1; border-radius: 10px; padding: 28px; text-align: center; cursor: pointer; transition: all 0.15s; }
.upload-zone:hover { border-color: #4F46E5; background: #F5F3FF; }

/* ─── Progress bar ─── */
.progress { height: 6px; border-radius: 3px; background: #E2E8F0; overflow: hidden; }
.progress-bar { height: 100%; border-radius: 3px; transition: width 0.6s ease; }

/* ─── Scrollbar ─── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 3px; }

/* ─── Mobile hamburger toggle ─── */
.menu-toggle {
  display: none;
  position: fixed;
  top: 14px;
  left: 16px;
  z-index: 200;
  background: #0F172A;
  border: none;
  border-radius: 8px;
  width: 36px;
  height: 36px;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #fff;
}
.menu-toggle svg { width: 20px; height: 20px; }

/* ─── Sidebar overlay ─── */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 99;
}
.sidebar-overlay.open { display: block; }

/* ─── Responsive grid helpers ─── */
.analytics-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 12px;
}
.bottom-grid {
  display: grid;
  grid-template-columns: 1fr 1.6fr;
  gap: 16px;
  margin-bottom: 20px;
}

/* ─── Table scroll wrapper ─── */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* ════════════════════════════════════════
   TABLET  ≤ 1024px
════════════════════════════════════════ */
@media (max-width: 1024px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .analytics-grid { grid-template-columns: repeat(3, 1fr); }
  .bottom-grid { grid-template-columns: 1fr; }
  .chart-grid { grid-template-columns: 1fr; }
}

/* ════════════════════════════════════════
   MOBILE  ≤ 768px
════════════════════════════════════════ */
@media (max-width: 768px) {
  /* Sidebar drawer */
  .sidebar {
    transform: translateX(-260px);
    transition: transform 0.25s ease;
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(0,0,0,0.4);
  }
  .menu-toggle { display: flex; }

  /* Main content full width */
  .main-content { margin-left: 0; }

  /* Topbar */
  .topbar {
    padding: 0 16px 0 62px; /* space for hamburger */
    height: 56px;
  }
  .page-title { font-size: 16px; }
  .page-subtitle { display: none; }

  /* Page content */
  .page-content { padding: 16px; }

  /* KPI grid */
  .kpi-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .kpi-value { font-size: 22px; }
  .kpi-card { padding: 14px 16px; }

  /* Analytics grid (!important to beat per-page inline grid-template-columns) */
  .analytics-grid { grid-template-columns: repeat(2, 1fr) !important; }

  /* Charts & bottom grid */
  .chart-grid { grid-template-columns: 1fr; }
  .bottom-grid { grid-template-columns: 1fr; }

  /* Cards */
  .card-body { padding: 14px 16px; }
  .card-header { padding: 12px 16px; padding-bottom: 10px; }

  /* Tables */
  .data-table th, .data-table td { padding: 10px 12px; font-size: 12px; }

  /* Buttons in topbar */
  .topbar .btn-sm { padding: 5px 10px; font-size: 11px; }

  /* Hide less important topbar elements on very small screens */
  #lastSync { display: none; }
}

/* ════════════════════════════════════════
   SMALL MOBILE  ≤ 480px
════════════════════════════════════════ */
@media (max-width: 480px) {
  .kpi-grid { grid-template-columns: 1fr; }
  .analytics-grid { grid-template-columns: repeat(2, 1fr); }
  .topbar { padding: 0 12px 0 56px; }
  .page-content { padding: 12px; }
  .kpi-value { font-size: 24px; }
}

/* ════════════════════════════════════════
   MOBILE OVERRIDES for inline grids
   Several pages set grid-template-columns inline on #kpiRow /
   #summaryRow, which beats the class-based media queries above by
   specificity. Force these stat rows to collapse on small screens.
   (!important is required to win over the inline style.)
════════════════════════════════════════ */
@media (max-width: 768px) {
  #kpiRow, #summaryRow { grid-template-columns: repeat(2, 1fr) !important; }
}
@media (max-width: 480px) {
  #kpiRow, #summaryRow { grid-template-columns: 1fr !important; }
}

/* Safety net: never let the page scroll sideways on phones. */
@media (max-width: 768px) {
  html, body { max-width: 100%; overflow-x: hidden; }
}

/* Collapse two-column inline form grids on small screens.
   Attribute selector + !important beats the inline style regardless of
   specificity, so per-page modals/forms that hardcode
   `grid-template-columns: 1fr 1fr` stack into one column on phones. */
@media (max-width: 560px) {
  [style*="grid-template-columns:1fr 1fr"],
  [style*="grid-template-columns: 1fr 1fr"] { grid-template-columns: 1fr !important; }
}

/* Let inline flex header/toolbar rows wrap instead of overflowing on phones.
   Only affects rows that would otherwise overflow (flex-wrap is a no-op when
   content already fits). Kanban boards use class-based flex, not inline, so
   their horizontal scroll is untouched. */
@media (max-width: 560px) {
  [style*="display:flex"][style*="align-items:center"] { flex-wrap: wrap; }
}

/* Injected/markdown tables (e.g. proposal-view) that are not wrapped
   in .table-scroll: make them horizontally scrollable on mobile. */
@media (max-width: 768px) {
  .proposal-card table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
}
