:root {
  --bg: #fafaf9;
  --card: #ffffff;
  --border: #ececec;
  --border-strong: #d4d4d4;
  --text: #0a0a0a;
  --text-muted: #737373;
  --text-soft: #a3a3a3;
  --accent: #dc2626;
  --accent-soft: #fef2f2;
  --green: #16a34a;
  --green-soft: #dcfce7;
  --red: #dc2626;
  --red-soft: #fee2e2;
  --orange: #f97316;
  --blue: #3b82f6;
  --blue-soft: #dbeafe;
  --pink: #f87171;
  --gold: #ca8a04;
  --shadow: 0 1px 2px rgba(0,0,0,0.04);
  --radius: 16px;
  --radius-sm: 10px;
  --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, monospace;
  --sans: 'IBM Plex Sans Arabic', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

/* ─── Top header (replaces sidebar on all screens) ─────── */
.topbar {
  position: sticky; top: 0; z-index: 30;
  background: rgba(255,255,255,0.92);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
  padding: 14px 16px 12px;
}
.topbar-row {
  display: flex; align-items: center; gap: 12px;
}
.brand {
  display: flex; align-items: center; gap: 10px;
  font-weight: 700; font-size: 16px;
}
.brand .logo {
  width: 32px; height: 32px; border-radius: 9px;
  background: var(--accent-soft); display: grid; place-items: center;
  font-size: 18px; flex-shrink: 0;
}
.brand-sub { font-size: 11px; color: var(--text-muted); font-weight: 400; line-height: 1.1; }
.brand-block { display: flex; flex-direction: column; line-height: 1.2; }

.spacer { flex: 1; }

/* ─── Tab navigation ─────────────────────────────────── */
.tabs {
  display: flex; gap: 4px; margin-top: 14px;
  overflow-x: auto; -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  border-bottom: 1px solid var(--border);
  margin-inline: -16px; padding-inline: 16px;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  font-family: inherit; font-size: 14px; font-weight: 500;
  padding: 10px 14px 12px; border: 0; background: transparent;
  color: var(--text-muted); cursor: pointer; white-space: nowrap;
  position: relative; transition: color 0.15s;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.tab:hover { color: var(--text); }
.tab.active {
  color: var(--text); font-weight: 600;
  border-bottom-color: var(--accent);
}

/* ─── Page sections ──────────────────────────────────── */
.page[hidden] { display: none !important; }

/* ─── Filter pill (single button replaces all chips) ──── */
.filter-pill {
  display: inline-flex; align-items: center; gap: 8px;
  font-family: inherit; font-size: 13px; font-weight: 600;
  padding: 9px 14px; border-radius: 999px;
  border: 1px solid var(--border); background: #fff;
  color: var(--text); cursor: pointer; white-space: nowrap;
  transition: all 0.15s;
}
.filter-pill:hover { border-color: var(--border-strong); }
.filter-pill:active { transform: scale(0.97); }
.filter-pill svg { color: var(--text-muted); }

/* ─── Main container ───────────────────────────────────── */
.main {
  padding: 16px;
  max-width: 720px;
  margin: 0 auto;
}

.section-title {
  font-size: 13px; color: var(--text-muted); font-weight: 600;
  margin: 4px 4px 10px; letter-spacing: 0.01em;
}

/* ─── KPI grid (2 cols on mobile, 3 on tablet, 6 on desktop) ──── */
.kpis {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px;
  margin-bottom: 24px;
}
.kpi {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px;
}
.kpi .lbl { font-size: 12px; color: var(--text-muted); font-weight: 500; line-height: 1.2; min-height: 28px; }
.kpi .val {
  font-family: var(--mono); font-size: 19px; font-weight: 600;
  margin-top: 4px; letter-spacing: -0.02em; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.kpi .delta-row { margin-top: 6px; font-size: 11px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.kpi .delta { display: inline-flex; align-items: center; gap: 3px; padding: 2px 6px; border-radius: 5px; font-weight: 600; }
.kpi .delta.up { background: var(--green-soft); color: var(--green); }
.kpi .delta.down { background: var(--red-soft); color: var(--red); }
.kpi .delta.flat { background: #f5f5f4; color: var(--text-muted); }
.kpi .vs-prev { font-family: var(--mono); color: var(--text-soft); }

/* ─── Cards ────────────────────────────────────────────── */
.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; margin-bottom: 16px;
}
.card-head {
  display: flex; justify-content: space-between; align-items: flex-start;
  margin-bottom: 14px; gap: 10px;
}
.card-head h3 { font-size: 15px; font-weight: 700; margin: 0; line-height: 1.3; }
.card-head .sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.card-head .total {
  font-family: var(--mono); font-size: 15px; font-weight: 600; letter-spacing: -0.02em;
}

.legend { display: flex; gap: 12px; font-size: 11.5px; color: var(--text-muted); flex-wrap: wrap; margin: 8px 0; }
.legend i { display: inline-block; width: 9px; height: 9px; border-radius: 3px; margin-inline-end: 5px; vertical-align: middle; }
.legend i.dashed { background: transparent !important; border: 1.5px dashed #94a3b8; border-radius: 0; height: 0; width: 14px; }

.chart-wrap { height: 240px; position: relative; }

/* ─── Chart switch (sales-vs-spend / sales-vs-carts) ──── */
.chart-switch {
  display: grid; grid-template-columns: 1fr 1fr; gap: 4px;
  background: #f5f5f4; border-radius: 10px; padding: 3px;
  margin-bottom: 12px;
}
.chart-switch button {
  font-family: inherit; font-size: 13px; font-weight: 500;
  padding: 9px 12px; border: 0; background: transparent;
  border-radius: 7px; cursor: pointer; color: var(--text-muted);
  transition: all 0.15s;
}
.chart-switch button.active { background: #fff; color: var(--text); font-weight: 600; box-shadow: var(--shadow); }

.chart-controls { display: flex; justify-content: flex-end; margin-bottom: 8px; }
.chart-controls[hidden] { display: none !important; }

/* ─── Mode toggle (للتبديل بين القيمة والعدد) ──────────── */
.mode-toggle {
  display: inline-flex; background: #f5f5f4; border-radius: 8px; padding: 2px;
}
.mode-toggle button {
  font-family: inherit; font-size: 11.5px; font-weight: 500;
  padding: 5px 10px; border: 0; background: transparent;
  border-radius: 6px; cursor: pointer; color: var(--text-muted);
}
.mode-toggle button.active { background: #fff; color: var(--text); font-weight: 600; box-shadow: var(--shadow); }

/* ─── Total block with delta ──────────────────────────── */
.total-block { text-align: end; }
.total-delta { font-family: var(--mono); font-size: 11px; margin-top: 3px; padding: 1px 6px; border-radius: 5px; display: inline-block; }
.total-delta.up { background: var(--green-soft); color: var(--green); }
.total-delta.down { background: var(--red-soft); color: var(--red); }
.total-delta.flat { background: #f5f5f4; color: var(--text-muted); }

/* ─── Spend breakdown ──────────────────────────────────── */
.donut-wrap { height: 170px; position: relative; margin-bottom: 12px; }
.channels { display: flex; flex-direction: column; gap: 2px; }
.channel-row {
  display: grid; grid-template-columns: 12px 1fr auto auto; gap: 10px;
  align-items: center; padding: 9px 6px; border-radius: 8px;
}
.channel-row .swatch { width: 10px; height: 10px; border-radius: 3px; }
.channel-row .name { font-size: 13.5px; font-weight: 500; line-height: 1.3; }
.channel-row .name .badge {
  font-size: 10px; padding: 1px 6px; border-radius: 4px;
  background: #f5f5f4; color: var(--text-soft); margin-inline-start: 6px; font-weight: 400;
}
.channel-row .amt-block { display: flex; flex-direction: column; align-items: flex-end; min-width: 90px; }
.channel-row .amt { font-family: var(--mono); font-size: 13px; font-weight: 600; line-height: 1.2; }
.channel-row .ch-delta { font-family: var(--mono); font-size: 10.5px; margin-top: 1px; padding: 0 5px; border-radius: 4px; line-height: 1.4; }
.channel-row .ch-delta.up { background: var(--green-soft); color: var(--green); }
.channel-row .ch-delta.down { background: var(--red-soft); color: var(--red); }
.channel-row .ch-delta.flat { color: var(--text-soft); }
.channel-row .pct { font-family: var(--mono); font-size: 11px; color: var(--text-muted); min-width: 36px; text-align: end; }
.channel-row.disconnected { opacity: 0.45; }

/* ─── Attention panel ──────────────────────────────────── */
.attn-card {
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 14px; display: flex; gap: 12px; align-items: flex-start;
  margin-bottom: 10px;
}
.attn-card:last-child { margin-bottom: 0; }
.attn-icon {
  width: 36px; height: 36px; border-radius: 10px; flex-shrink: 0;
  display: grid; place-items: center; font-size: 18px;
}
.attn-icon.warm { background: #fff7ed; color: var(--orange); }
.attn-icon.source { background: var(--blue-soft); color: var(--blue); }
.attn-icon.product { background: var(--green-soft); color: var(--green); }
.attn-body { flex: 1; min-width: 0; }
.attn-title { font-size: 13.5px; font-weight: 600; line-height: 1.3; }
.attn-headline { font-family: var(--mono); font-size: 16px; font-weight: 600; margin-top: 3px; letter-spacing: -0.02em; }
.attn-sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; line-height: 1.4; }
.attn-row {
  display: flex; justify-content: space-between; gap: 8px;
  font-size: 12.5px; padding: 6px 0;
  border-top: 1px dashed var(--border);
}
.attn-row:first-of-type { border-top: 0; padding-top: 8px; }
.attn-row .label-cell {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  min-width: 0; flex: 1;
}
.attn-row .value-cell { font-family: var(--mono); color: var(--text-muted); white-space: nowrap; }

.foot {
  margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--border);
  display: flex; justify-content: space-between; font-size: 11px; color: var(--text-soft); flex-wrap: wrap; gap: 8px;
}

/* ─── Drawer (full-screen on mobile, side on desktop) ──── */
.drawer {
  position: fixed; inset: 0; background: #fff;
  transform: translateY(100%); transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
  z-index: 50; display: flex; flex-direction: column;
}
.drawer.open { transform: translateY(0); }
.drawer-head {
  padding: 16px 18px; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
}
.drawer-head h3 { margin: 0; font-size: 16px; font-weight: 700; }
.drawer-head button {
  background: #f5f5f4; border: 0; border-radius: 50%;
  width: 32px; height: 32px;
  font-size: 22px; line-height: 1; cursor: pointer;
  color: var(--text-muted);
}
.drawer-body { padding: 18px; overflow-y: auto; flex: 1; -webkit-overflow-scrolling: touch; }
.drawer-stat {
  background: linear-gradient(135deg, #fafaf9 0%, #f5f5f4 100%);
  border-radius: var(--radius-sm); padding: 18px; margin-bottom: 16px;
}
.drawer-stat .lbl { font-size: 12px; color: var(--text-muted); font-weight: 500; }
.drawer-stat .val { font-family: var(--mono); font-size: 26px; font-weight: 700; margin-top: 4px; letter-spacing: -0.02em; }
.drawer-stat .meta { font-size: 12px; color: var(--text-muted); margin-top: 6px; line-height: 1.5; }
.drawer h4 { font-size: 13px; margin: 18px 0 8px; font-weight: 600; }
.drawer-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.drawer-table td { padding: 9px 0; border-bottom: 1px solid #f5f5f4; }
.drawer-table td:last-child { text-align: end; font-family: var(--mono); color: var(--text-muted); }

/* ─── Loading skeleton ─────────────────────────────────── */
.skeleton {
  background: linear-gradient(90deg, #f5f5f4 0%, #ececec 50%, #f5f5f4 100%);
  background-size: 200% 100%; animation: shimmer 1.5s infinite;
  border-radius: 4px; height: 1em; min-width: 40px; display: inline-block;
}
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

.error-banner {
  background: var(--red-soft); color: var(--red);
  padding: 14px 16px; border-radius: var(--radius-sm);
  font-size: 13px; margin-bottom: 16px;
}

/* ─── Bottom sheet (filter modal) ──────────────────────── */
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  opacity: 0; pointer-events: none; transition: opacity 0.25s;
  z-index: 90; backdrop-filter: blur(2px);
}
.sheet-backdrop.open { opacity: 1; pointer-events: auto; }

.sheet {
  position: fixed; left: 0; right: 0; bottom: 0;
  background: #fff; border-radius: 22px 22px 0 0;
  transform: translateY(100%); transition: transform 0.32s cubic-bezier(0.32, 0.72, 0, 1);
  z-index: 100; max-height: 88vh; overflow-y: auto;
  padding-bottom: env(safe-area-inset-bottom, 16px);
  box-shadow: 0 -10px 40px rgba(0,0,0,0.1);
}
.sheet.open { transform: translateY(0); }
.sheet-handle {
  width: 36px; height: 4px; background: #d4d4d4; border-radius: 2px;
  margin: 10px auto 4px;
}
.sheet-body { padding: 14px 18px 22px; }
.sheet-section { margin-bottom: 22px; }
.sheet-section h4 {
  font-size: 12px; color: var(--text-muted); margin: 0 0 10px;
  font-weight: 600; letter-spacing: 0.02em;
}

.opt-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px;
}
.opt-grid.two-col { grid-template-columns: 1fr 1fr; }
.opt {
  font-family: inherit; font-size: 14px; font-weight: 500;
  padding: 13px; border-radius: 12px;
  border: 1px solid var(--border); background: #fff;
  color: var(--text); cursor: pointer; text-align: center;
  transition: all 0.12s;
}
.opt:active { transform: scale(0.97); }
.opt.active {
  background: var(--text); color: #fff; border-color: var(--text); font-weight: 600;
}
.opt-custom { grid-column: span 2; }
@media (min-width: 600px) { .opt-custom { grid-column: span 3; } }

.custom-dates {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px;
  margin-top: 12px;
}
.custom-dates[hidden] { display: none; }
.custom-dates label {
  display: flex; flex-direction: column; gap: 6px;
  font-size: 12px; color: var(--text-muted); font-weight: 500;
}
.custom-dates input[type="date"] {
  font-family: inherit; font-size: 14px;
  padding: 11px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: #fff;
  color: var(--text);
  -webkit-appearance: none; appearance: none;
}
.custom-dates input[type="date"]:focus {
  outline: none; border-color: var(--text); box-shadow: 0 0 0 3px rgba(0,0,0,0.05);
}

.sheet-done {
  display: block; width: 100%; margin-top: 8px;
  padding: 14px; border: 0; border-radius: 12px;
  background: var(--accent); color: #fff;
  font-family: inherit; font-size: 15px; font-weight: 600;
  cursor: pointer;
}
.sheet-done:active { transform: scale(0.98); }

/* ─── Stale badge for channels ────────────────────────── */
.stale-badge {
  font-size: 9.5px; padding: 1px 6px; border-radius: 4px;
  background: #fff7ed; color: var(--orange); font-family: var(--mono);
  margin-inline-start: 6px; font-weight: 500;
}

/* ─── Funnel ────────────────────────────────────────── */
.funnel { display: flex; flex-direction: column; gap: 10px; margin: 8px 0 18px; }
.funnel-stage {
  position: relative; padding: 14px 16px;
  border-radius: 10px; background: #f5f5f4;
  display: flex; justify-content: space-between; align-items: center;
  overflow: hidden;
}
.funnel-stage .bar {
  position: absolute; inset: 0; right: auto;
  background: linear-gradient(90deg, rgba(59, 130, 246, 0.15), rgba(59, 130, 246, 0.08));
  z-index: 0;
}
.funnel-stage > * { position: relative; z-index: 1; }
.funnel-stage .lbl-block { display: flex; flex-direction: column; gap: 2px; }
.funnel-stage .lbl { font-size: 13.5px; font-weight: 600; }
.funnel-stage .meta { font-size: 11.5px; color: var(--text-muted); font-family: var(--mono); }
.funnel-stage .num { font-family: var(--mono); font-size: 18px; font-weight: 700; letter-spacing: -0.02em; }
.funnel-stage .delta {
  display: inline-block; font-family: var(--mono); font-size: 10.5px;
  padding: 1px 5px; border-radius: 4px; margin-inline-start: 6px;
}
.funnel-stage .delta.up { background: var(--green-soft); color: var(--green); }
.funnel-stage .delta.down { background: var(--red-soft); color: var(--red); }
.funnel-stage .delta.flat { background: #eee; color: var(--text-muted); }

.rates-title { font-size: 12px; color: var(--text-muted); margin: 6px 0 10px; font-weight: 600; }
.rates { display: flex; flex-direction: column; gap: 6px; }
.rate-row {
  display: grid; grid-template-columns: 1fr auto auto; gap: 10px;
  font-size: 13px; padding: 8px 10px; border-radius: 8px; background: #fafafa;
  align-items: center;
}
.rate-row .arrow { color: var(--text-soft); }
.rate-row .pct { font-family: var(--mono); font-weight: 600; }
.rate-row .pct-prev { font-family: var(--mono); font-size: 11px; color: var(--text-soft); }

/* ─── Retention ─────────────────────────────────────── */
.retention-row {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px;
}
.ret-stat {
  background: #fafafa; border-radius: 10px; padding: 14px 16px; text-align: center;
}
.ret-num { font-family: var(--mono); font-size: 22px; font-weight: 700; letter-spacing: -0.02em; }
.ret-lbl { font-size: 11.5px; color: var(--text-muted); margin-top: 4px; line-height: 1.3; }

/* ─── Info banner ───────────────────────────────────── */
.info-banner {
  display: flex; gap: 10px; align-items: flex-start;
  background: #eff6ff; color: #1e40af;
  padding: 12px 14px; border-radius: 10px;
  font-size: 12.5px; line-height: 1.5;
  margin-bottom: 14px;
}
.info-banner strong { display: block; margin-bottom: 2px; }

/* ─── Coupons KPIs grid (4 cols on big, 2 on mobile) ── */
.kpis-4 { grid-template-columns: repeat(2, 1fr); }
@media (min-width: 600px) { .kpis-4 { grid-template-columns: repeat(4, 1fr); } }

/* ─── Growth KPIs grid (5 cols on big, 2 on mobile) ─── */
.kpis-5 { grid-template-columns: repeat(2, 1fr); }
@media (min-width: 600px) { .kpis-5 { grid-template-columns: repeat(3, 1fr); } }
@media (min-width: 960px) { .kpis-5 { grid-template-columns: repeat(5, 1fr); } }

/* ─── Data table ────────────────────────────────────── */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -4px; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; min-width: 540px; }
.data-table th, .data-table td { padding: 10px 8px; text-align: start; }
.data-table th {
  font-size: 11.5px; color: var(--text-muted); font-weight: 600;
  text-transform: none; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: #fff;
}
.data-table td { border-bottom: 1px solid #f5f5f4; }
.data-table tr:hover td { background: #fafafa; }
.data-table .num { text-align: end; font-family: var(--mono); }
.data-table th.num { text-align: end; }
.data-table .code-cell { font-family: var(--mono); font-weight: 600; color: var(--text); }
.data-table .pill {
  display: inline-block; font-size: 10.5px; padding: 2px 8px; border-radius: 999px;
  background: #f5f5f4; color: var(--text-muted); font-weight: 500;
}
.data-table .pill.active { background: var(--green-soft); color: var(--green); }
.data-table .pill.expired { background: var(--red-soft); color: var(--red); }

/* ─── Tablet & Desktop ─────────────────────────────────── */
@media (min-width: 600px) {
  .kpis { grid-template-columns: repeat(3, 1fr); }
  .main { padding: 20px; }
  .chart-wrap { height: 300px; }
  .donut-wrap { height: 200px; }
  .opt-grid { grid-template-columns: repeat(3, 1fr); }
}
@media (min-width: 960px) {
  .topbar { padding: 16px 32px; }
  .main { max-width: 1400px; padding: 24px 32px; }
  .kpis { grid-template-columns: repeat(5, 1fr); gap: 12px; }
  .kpi .val { font-size: 22px; }
  .grid-2 {
    display: grid; grid-template-columns: 1.6fr 1fr; gap: 16px;
  }
  .grid-2 > .card { margin-bottom: 0; }
  .drawer {
    inset: 0 auto 0 0; width: 460px;
    transform: translateX(-100%);
    border-inline-end: 1px solid var(--border);
    box-shadow: 4px 0 30px rgba(0,0,0,0.06);
  }
  .drawer.open { transform: translateX(0); }

  /* Filter sheet centered on desktop */
  .sheet {
    inset: auto auto 5vh 50%;
    transform: translate(-50%, 100vh);
    width: 480px; left: auto; right: auto;
    border-radius: 18px;
    max-height: 80vh;
  }
  .sheet.open { transform: translate(-50%, 0); }
}
