/* ========================================
   Marketing Dashboard — Styles
   Cleaned up, responsive, modern
   ======================================== */

/* --- Reset & Base --- */

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #f3f4f6;
  color: #111827;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* --- App Layout --- */

.app {
  display: flex;
  min-height: 100vh;
  padding: 12px;
  gap: 12px;
  background: #f3f4f6;
}

/* --- Burger Button (mobile only) --- */

.burger-btn {
  display: none;
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 1100;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 10px;
  background: #111827;
  color: #f9fafb;
  font-size: 22px;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
  transition: background 0.2s;
}

.burger-btn:hover {
  background: #1f2937;
}

/* --- Sidebar --- */

/* Sidebar column: sidebar + site-stats stacked */
.sidebar-col {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-self: flex-start;
  position: sticky;
  top: 12px;
  z-index: 1000;
}

.sidebar {
  width: 210px;
  min-width: 210px;
  background: #111827;
  color: #e5e7eb;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  transition: transform 0.3s ease;
  border-radius: 18px;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 999;
}

.logo {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 20px;
  letter-spacing: -0.3px;
}

.nav-item {
  display: block;
  padding: 10px 0;
  opacity: 0.8;
  text-decoration: none;
  color: inherit;
}

.nav-item.active {
  font-weight: 600;
  opacity: 1;
}

/* Source Switch (sidebar buttons) */

.source-switch {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.source-btn {
  width: 100%;
  border: none;
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 500;
  text-align: left;
  background: rgba(255,255,255,0.06);
  color: #9ca3af;
  cursor: pointer;
  transition: all 0.2s ease;
}

.source-btn:hover {
  background: rgba(255,255,255,0.1);
  color: #e5e7eb;
}

.source-btn.active {
  background: #f9fafb;
  color: #111827;
  font-weight: 600;
}

.source-btn.disabled {
  opacity: 0.35;
  cursor: default;
}

/* Sidebar link (e.g. Budget page) */
.sidebar-link {
  display: block;
  margin-top: 12px;
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 12px;
  font-weight: 500;
  color: #9ca3af;
  text-decoration: none;
  background: rgba(255,255,255,0.06);
  transition: all 0.2s ease;
}

.sidebar-link:hover {
  background: rgba(255,255,255,0.1);
  color: #e5e7eb;
}

/* --- Main Content Area --- */

.main {
  flex: 1;
  padding: 24px 32px;
  min-width: 0;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 12px;
}

.header h1 {
  font-size: 24px;
  font-weight: 700;
  margin: 0;
}

.header-title-row {
  display: flex;
  align-items: center;
  gap: 14px;
}

.budget-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 18px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  color: #f9fafb;
  background: linear-gradient(135deg, #2563eb, #7c3aed);
  text-decoration: none;
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.3);
  transition: all 0.2s ease;
  white-space: nowrap;
}

.budget-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 14px rgba(37, 99, 235, 0.4);
  background: linear-gradient(135deg, #1d4ed8, #6d28d9);
}

/* --- Controls (Period + Date Range) --- */

.controls {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
}

.period-toggle {
  display: flex;
  gap: 6px;
}

.period-btn {
  border: none;
  padding: 8px 16px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 500;
  background: #e5e7eb;
  color: #374151;
  cursor: pointer;
  transition: all 0.2s ease;
}

.period-btn:hover {
  background: #d1d5db;
}

.period-btn.active {
  background: #111827;
  color: #f9fafb;
}

/* Date Range */

.date-range {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  background: #e5e7eb;
  border-radius: 12px;
}

.date-range .date-input,
.date-range .flatpickr-input {
  width: 140px;
  height: 36px;
  padding: 0 12px;
  border-radius: 10px;
  background: #ffffff;
  border: 1.5px solid #d7dbe1;
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  text-align: center;
  font-variant-numeric: tabular-nums;
  color: #111827;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.date-range .date-input::placeholder,
.date-range .flatpickr-input::placeholder {
  color: #a0a4ab;
  font-weight: 400;
}

.date-range .date-input:hover,
.date-range .flatpickr-input:hover {
  border-color: #b6bcc5;
}

.date-range .date-input:focus,
.date-range .flatpickr-input:focus {
  outline: none;
  border-color: #111827;
  box-shadow: 0 0 0 2px rgba(17, 24, 39, 0.12);
}

.date-range .flatpickr-input[readonly] {
  cursor: pointer;
}

/* Apply buttons — unified */

#apply-filter,
#tg-apply-filter,
#mt-apply-filter {
  border: none;
  border-radius: 10px;
  padding: 8px 18px;
  font-size: 13px;
  font-weight: 600;
  background: #111827;
  color: #f9fafb;
  cursor: pointer;
  transition: all 0.2s ease;
}

#apply-filter:hover,
#tg-apply-filter:hover,
#mt-apply-filter:hover {
  background: #1f2937;
  transform: translateY(-1px);
}

/* Campaign Select */

.campaign-select {
  min-width: 200px;
  height: 36px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1.5px solid #d7dbe1;
  background: #ffffff;
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  color: #111827;
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.campaign-select:hover {
  border-color: #b6bcc5;
}

.campaign-select:focus {
  border-color: #111827;
  box-shadow: 0 0 0 2px rgba(17, 24, 39, 0.12);
}

/* --- KPI Cards --- */

.kpi-row {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.kpi-card {
  background: #ffffff;
  border-radius: 14px;
  padding: 18px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04), 0 4px 12px rgba(0,0,0,0.04);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.kpi-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0,0,0,0.08), 0 1px 3px rgba(0,0,0,0.04);
}

.kpi-title {
  font-size: 12px;
  font-weight: 500;
  color: #6b7280;
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.kpi-value {
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.kpi-main-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.kpi-delta {
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

.kpi-delta--up { color: #16a34a; }
.kpi-delta--down { color: #dc2626; }
.kpi-delta--neutral { color: #6b7280; }

/* --- Charts --- */

.charts-row {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 16px;
  margin-bottom: 24px;
}

.chart-card {
  background: #ffffff;
  border-radius: 14px;
  padding: 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04), 0 4px 12px rgba(0,0,0,0.04);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.chart-card-side {
  min-width: 0;
}

.chart-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 8px;
}

.chart-header h2 {
  font-size: 16px;
  font-weight: 600;
  margin: 0;
}

.chart-subtitle {
  font-size: 12px;
  color: #6b7280;
}

.chart-card canvas {
  width: 100%;
  max-height: 320px;
}

/* Metric Toggle (checkboxes on chart) */

.metric-toggle {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.metric-checkbox {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border-radius: 8px;
  background: #f3f4f6;
  color: #374151;
  font-size: 12px;
  cursor: pointer;
  user-select: none;
  transition: background 0.15s;
}

.metric-checkbox:hover {
  background: #e5e7eb;
}

.metric-checkbox input[type="checkbox"] {
  width: 14px;
  height: 14px;
  accent-color: #111827;
}

/* Side Chart Arrows */

.side-chart-arrows {
  display: flex;
  align-items: center;
  gap: 6px;
}

.side-arrow {
  border: none;
  border-radius: 8px;
  width: 28px;
  height: 28px;
  background: #f3f4f6;
  color: #111827;
  font-size: 16px;
  cursor: pointer;
  line-height: 1;
  transition: background 0.15s;
}

.side-arrow:hover {
  background: #e5e7eb;
}

.side-label {
  font-size: 13px;
  color: #374151;
  font-weight: 500;
}

.side-metric-toggle {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}

.side-metric-btn {
  border: none;
  padding: 5px 12px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 500;
  background: #f3f4f6;
  color: #374151;
  cursor: pointer;
  transition: all 0.15s ease;
}

.side-metric-btn:hover {
  background: #e5e7eb;
}

.side-metric-btn.active {
  background: #111827;
  color: #f9fafb;
}

/* --- Tables --- */

.tables {
  background: #ffffff;
  border-radius: 14px;
  padding: 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04), 0 4px 12px rgba(0,0,0,0.04);
}

.tables h2 {
  margin: 0 0 4px 0;
  font-size: 18px;
  font-weight: 600;
}

.tables-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  flex-wrap: wrap;
  gap: 12px;
}

.tables-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.table-tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.tab-button {
  border: none;
  padding: 7px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  background: #f3f4f6;
  color: #374151;
  cursor: pointer;
  transition: all 0.15s ease;
}

.tab-button:hover {
  background: #e5e7eb;
}

.tab-button.active {
  background: #111827;
  color: #f9fafb;
}

.export-btn {
  border: none;
  border-radius: 10px;
  padding: 7px 16px;
  font-size: 13px;
  font-weight: 600;
  background: #111827;
  color: #f9fafb;
  cursor: pointer;
  transition: all 0.2s ease;
}

.export-btn:hover {
  background: #1f2937;
  transform: translateY(-1px);
}

.table-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

thead {
  background: #f9fafb;
}

th,
td {
  padding: 10px 8px;
  text-align: left;
  border-bottom: 1px solid #e5e7eb;
}

th {
  font-weight: 600;
  font-size: 12px;
  color: #6b7280;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

/* Zebra striping */
tbody tr:nth-child(even) {
  background: #f9fafb;
}

tbody tr:hover {
  background: #eef2ff;
}

th.sortable {
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}

th.sortable:hover {
  color: #111827;
}

/* --- Avito Side Panel --- */

#avito-side-card {
  min-height: 260px;
}

.avito-metric-row {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  padding: 6px 0;
  border-bottom: 1px solid rgba(148, 163, 184, 0.15);
}

.avito-metric-row:last-child {
  border-bottom: none;
}

.avito-view-toggle {
  margin-top: 8px;
}

/* --- Telegram & Metrica Views (unified) --- */

#telegram-view,
#metrica-view {
  width: 100%;
  min-height: 100%;
  padding: 32px 20px;
  box-sizing: border-box;
  display: none;
  justify-content: center;
  align-items: flex-start;
  background-color: #f3f4f6;
}

#telegram-view .tg-container,
#metrica-view .tg-container {
  background: #ffffff;
  padding: 36px;
  border-radius: 18px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04), 0 8px 24px rgba(0,0,0,0.06);
  text-align: center;
  max-width: 900px;
  width: 100%;
}

.tg-controls {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.tg-controls h1 {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: 700;
  color: #111827;
}

.tg-controls .controls-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.tg-subtitle {
  color: #6b7280;
  margin: 0 0 24px 0;
  font-size: 14px;
}

/* KPI grid for TG & Metrica */

.tg-stats-grid,
.tg-kpi-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}

.tg-card {
  background: #f0f4ff;
  padding: 20px 16px;
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.tg-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.06);
}

.tg-label {
  font-size: 13px;
  color: #6b7280;
  margin-bottom: 6px;
  font-weight: 500;
}

.tg-value {
  font-size: 22px;
  color: #2563eb;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.tg-chart-wrapper {
  height: 340px;
  width: 100%;
  background: #ffffff;
  padding: 16px;
  border-radius: 14px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04);
  margin-bottom: 16px;
}

/* Extra row for Metrica (2 columns) */
.mt-extra-row {
  grid-template-columns: repeat(2, 1fr);
}

/* --- Site Stats Block (under sidebar) --- */

.site-stats {
  width: 210px;
  background: #111827;
  color: #e5e7eb;
  border-radius: 18px;
  padding: 18px 16px;
}

.site-stats__title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: #9ca3af;
  margin-bottom: 14px;
}

.site-stats__row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 4px 0;
}

.site-stats__label {
  font-size: 12px;
  color: #9ca3af;
}

.site-stats__value {
  font-size: 14px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: #f9fafb;
}

.site-stats__divider {
  height: 1px;
  background: rgba(255,255,255,0.08);
  margin: 8px 0;
}

.site-stats__bar-wrap {
  margin-top: 12px;
}

.site-stats__bar-label {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: #9ca3af;
  margin-bottom: 5px;
}

.site-stats__bar {
  height: 6px;
  background: rgba(255,255,255,0.1);
  border-radius: 3px;
  overflow: hidden;
}

.site-stats__bar-fill {
  height: 100%;
  background: #3b82f6;
  border-radius: 3px;
  transition: width 0.8s ease;
}

/* --- Utility Classes --- */

.is-disabled {
  opacity: 0.4;
  pointer-events: none;
}

.is-hidden {
  display: none !important;
}

/* ==========================================
   RESPONSIVE — Tablet (≤1024px)
   ========================================== */

@media (max-width: 1024px) {
  .sidebar-col {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    transform: translateX(-100%);
    box-shadow: 4px 0 20px rgba(0,0,0,0.2);
    border-radius: 0;
    padding: 12px;
    background: #0b0f19;
    overflow-y: auto;
    gap: 12px;
  }

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

  .sidebar {
    border-radius: 14px;
  }

  .site-stats {
    border-radius: 14px;
  }

  .sidebar-overlay.open {
    display: block;
  }

  .burger-btn {
    display: flex;
  }

  .main {
    padding: 20px 20px;
    padding-top: 60px; /* space for burger button */
  }

  .kpi-row {
    grid-template-columns: repeat(3, 1fr);
  }

  .charts-row {
    grid-template-columns: 1fr;
  }

  .tg-stats-grid,
  .tg-kpi-row {
    grid-template-columns: repeat(2, 1fr);
  }

  .mt-extra-row {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ==========================================
   RESPONSIVE — Mobile (≤768px)
   ========================================== */

@media (max-width: 768px) {
  .main {
    padding: 16px;
    padding-top: 56px;
  }

  .header {
    flex-direction: column;
    align-items: flex-start;
  }

  .header h1 {
    font-size: 20px;
  }

  .controls {
    flex-direction: column;
    align-items: stretch;
    width: 100%;
  }

  .period-toggle {
    justify-content: center;
    flex-wrap: wrap;
  }

  .date-range {
    flex-wrap: wrap;
    justify-content: center;
    border-radius: 14px;
    padding: 8px;
  }

  .date-range .date-input,
  .date-range .flatpickr-input {
    width: 120px;
    height: 34px;
    font-size: 12px;
  }

  .campaign-select {
    width: 100%;
    min-width: unset;
  }

  .kpi-row {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }

  .kpi-card {
    padding: 14px;
  }

  .kpi-value {
    font-size: 18px;
  }

  .chart-card {
    padding: 14px;
  }

  .chart-card canvas {
    max-height: 250px;
  }

  .chart-header {
    flex-direction: column;
    gap: 8px;
  }

  .metric-toggle {
    gap: 4px;
  }

  .tables {
    padding: 14px;
  }

  .tables-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .table-tabs {
    overflow-x: auto;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
  }

  .tab-button {
    white-space: nowrap;
    flex-shrink: 0;
  }

  /* Telegram & Metrica */
  #telegram-view .tg-container,
  #metrica-view .tg-container {
    padding: 20px 16px;
    border-radius: 14px;
  }

  .tg-controls h1 {
    font-size: 20px;
  }

  .tg-controls .controls-wrapper {
    flex-direction: column;
    align-items: stretch;
  }

  .tg-chart-wrapper {
    height: 260px;
    padding: 12px;
  }
}

/* ==========================================
   RESPONSIVE — Small Mobile (≤480px)
   ========================================== */

@media (max-width: 480px) {
  .main {
    padding: 12px;
    padding-top: 52px;
  }

  .header h1 {
    font-size: 18px;
  }

  .kpi-row {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .kpi-card {
    padding: 12px;
  }

  .kpi-value {
    font-size: 18px;
  }

  .kpi-title {
    font-size: 11px;
  }

  .period-btn {
    padding: 6px 12px;
    font-size: 12px;
  }

  .date-range {
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
  }

  .date-range .date-input,
  .date-range .flatpickr-input {
    width: 100%;
  }

  .tg-stats-grid,
  .tg-kpi-row {
    grid-template-columns: 1fr;
  }

  .mt-extra-row {
    grid-template-columns: 1fr;
  }

  .chart-card canvas {
    max-height: 200px;
  }

  .tg-chart-wrapper {
    height: 220px;
  }

  table {
    font-size: 12px;
  }

  th, td {
    padding: 8px 6px;
  }
}

/* ==========================================
   PWA standalone mode adjustments
   ========================================== */

@media (display-mode: standalone) {
  body {
    /* safe area for notched devices */
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
}
