:root {
  color-scheme: light;
  --bg: #f3f6f8;
  --surface: #ffffff;
  --surface-muted: #edf2f5;
  --text: #17212b;
  --muted: #687582;
  --line: #dce4e9;
  --blue: #1877d2;
  --blue-soft: #e7f2fc;
  --green: #258c61;
  --green-soft: #e6f4ed;
  --gold: #d79a17;
  --gold-soft: #fff3d5;
  --coral: #d95f52;
  --radius: 8px;
  --shadow: 0 8px 24px rgba(32, 53, 67, 0.07);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  padding: 0 0 calc(76px + env(safe-area-inset-bottom));
}

button, input { font: inherit; letter-spacing: 0; }

button { -webkit-tap-highlight-color: transparent; }

.app-shell { width: min(100%, 1120px); margin: 0 auto; }

.topbar {
  min-height: 92px;
  padding: calc(14px + env(safe-area-inset-top)) 16px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

h1, h2, p { margin-top: 0; }

h1 { margin: 0; font-size: 24px; line-height: 1.15; }

h2 { margin-bottom: 0; font-size: 18px; line-height: 1.25; }

.star-balance {
  min-width: 122px;
  height: 56px;
  padding: 7px 13px;
  border: 1px solid color-mix(in srgb, var(--gold) 42%, transparent);
  background: color-mix(in srgb, var(--gold-soft) 82%, var(--surface));
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.star-balance small, .star-balance strong { display: block; text-align: right; }
.star-balance small { color: color-mix(in srgb, var(--gold) 70%, var(--text)); font-size: 11px; }
.star-balance strong { margin-top: 1px; font-size: 19px; }
.star-symbol {
  flex: 0 0 auto;
  color: #f5b82e;
  display: block;
  font-size: 31px;
  line-height: 1;
  text-shadow: 0 1px 1px rgba(123, 78, 0, 0.12);
}
.star-symbol span { display: block; line-height: 1; }

main { padding: 14px; }

.overview {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 14px;
}

.metric {
  min-width: 0;
  min-height: 94px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.metric > span, .metric small { display: block; color: var(--muted); }
.metric > span { font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.metric strong { display: block; margin: 7px 0 3px; font-size: 22px; line-height: 1; }
.metric small { font-size: 10px; line-height: 1.2; }

.view { display: none; }
.view.active { display: block; }

.panel {
  margin-bottom: 14px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.panel-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}

.eyebrow { margin-bottom: 3px; color: var(--green); font-size: 11px; font-weight: 800; text-transform: uppercase; }
.revenue-panel .eyebrow { color: var(--gold); }
.referral-panel .eyebrow { color: var(--blue); }
.monetization-panel .eyebrow, .product-panel .eyebrow { color: var(--gold); }

.panel-number { text-align: right; flex: 0 0 auto; }
.panel-number strong { display: block; font-size: 19px; line-height: 1.1; }
.panel-number span { color: var(--muted); font-size: 10px; }

.chart-mode-select { position: relative; flex: 0 0 auto; }
.chart-mode-button {
  height: 38px;
  min-width: 136px;
  padding: 0 10px 0 13px;
  border: 1px solid var(--line);
  border-radius: 7px;
  color: var(--text);
  background: var(--surface-muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.chart-mode-button svg { width: 15px; height: 15px; fill: none; stroke: var(--muted); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; transition: transform .16s ease; }
.chart-mode-button[aria-expanded="true"] svg { transform: rotate(180deg); }
.chart-mode-menu {
  position: absolute;
  z-index: 12;
  top: 44px;
  right: 0;
  width: 170px;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 12px 32px rgba(20,34,45,.2);
}
.chart-mode-menu[hidden] { display: none; }
.chart-mode-menu button {
  width: 100%;
  height: 38px;
  padding: 0 10px;
  border: 0;
  border-radius: 5px;
  color: var(--text);
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.chart-mode-menu button.active { color: var(--blue); background: var(--blue-soft); font-weight: 700; }

.period-control {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  margin-bottom: 16px;
  padding: 4px;
  background: var(--surface-muted);
  border-radius: var(--radius);
}

.period-control button {
  min-width: 0;
  height: 34px;
  padding: 0 5px;
  border: 0;
  border-radius: 5px;
  color: var(--muted);
  background: transparent;
  font-size: 12px;
  cursor: pointer;
}

.period-control button.active { color: var(--blue); background: var(--surface); font-weight: 700; box-shadow: 0 1px 5px rgba(28, 50, 67, 0.1); }

.chart-wrap { position: relative; width: 100%; height: 230px; }
.chart-wrap.tall { height: 280px; }

.panel-footer {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  gap: 16px;
  color: var(--muted);
}
.panel-footer strong { color: var(--text); }

.campaign-select .chart-mode-button { max-width: 176px; }
.campaign-select .chart-mode-button span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.campaign-menu { width: min(240px, calc(100vw - 44px)); max-height: 280px; overflow-y: auto; }
.campaign-menu button { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.utm-legend { margin-top: -2px; margin-bottom: 10px; }
.utm-panel .table-scroll { margin-top: 16px; }

.referral-totals div { padding: 11px; background: var(--surface-muted); border-radius: 6px; }
.referral-totals span { display: block; color: var(--muted); font-size: 11px; }
.referral-totals strong { display: block; margin-top: 5px; font-size: 17px; }

.table-scroll { overflow-x: auto; }
table { width: 100%; min-width: 560px; border-collapse: collapse; font-size: 13px; }
th, td { padding: 10px 8px; border-bottom: 1px solid var(--line); text-align: right; }
th { color: var(--muted); font-size: 11px; font-weight: 700; }
th:first-child, td:first-child { text-align: left; }
td:first-child { max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 650; }

.empty-state { margin: 14px 0 0; color: var(--muted); text-align: center; }

.legend { display: flex; flex-wrap: wrap; gap: 14px; margin: -2px 0 12px; color: var(--muted); font-size: 11px; }
.dot { display: inline-block; width: 8px; height: 8px; margin-right: 5px; border-radius: 50%; }
.dot.green { background: var(--green); }
.dot.gold { background: var(--gold); }
.dot.blue { background: var(--blue); }
.dot.coral { background: var(--coral); }

.referral-totals { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 14px; }

.money-metrics {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 14px;
}
.money-metrics div, .retention-grid div {
  min-width: 0;
  padding: 11px;
  border-radius: 6px;
  background: var(--surface-muted);
}
.money-metrics span, .retention-grid span,
.money-metrics small, .retention-grid small {
  display: block;
  color: var(--muted);
  font-size: 10px;
  line-height: 1.25;
}
.money-metrics strong, .retention-grid strong {
  display: block;
  margin: 5px 0 3px;
  font-size: 17px;
  line-height: 1.15;
}

.signal-list { display: grid; gap: 8px; }
.signal {
  position: relative;
  min-width: 0;
  padding: 11px 12px 11px 15px;
  border-radius: 6px;
  background: var(--surface-muted);
  overflow: hidden;
}
.signal::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 4px;
  background: var(--muted);
}
.signal.positive::before { background: var(--green); }
.signal.negative::before { background: var(--coral); }
.signal.warning::before { background: var(--gold); }
.signal > span { display: block; color: var(--muted); font-size: 10px; }
.signal strong { display: block; margin: 4px 0 2px; font-size: 15px; }
.signal small { display: block; color: var(--muted); font-size: 10px; line-height: 1.35; }

.product-list { display: grid; gap: 15px; }
.product-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 12px;
}
.product-heading span { min-width: 0; display: flex; align-items: center; gap: 7px; }
.product-heading span b {
  flex: 0 0 auto;
  width: 20px;
  height: 20px;
  border-radius: 5px;
  display: grid;
  place-items: center;
  color: var(--gold);
  background: var(--gold-soft);
  font-size: 10px;
}
.product-heading strong { flex: 0 0 auto; font-size: 12px; }
.product-bar { height: 6px; margin: 6px 0 4px 27px; border-radius: 3px; background: var(--surface-muted); overflow: hidden; }
.product-bar i { display: block; height: 100%; border-radius: inherit; background: var(--gold); }
.product-row small { display: block; margin-left: 27px; color: var(--muted); font-size: 9px; line-height: 1.35; }

.retention-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.retention-grid div { padding: 10px; }
.retention-grid strong { color: var(--green); }

.segment-list { display: grid; gap: 8px; }
.segment-row {
  min-width: 0;
  padding: 11px 12px;
  border-radius: 6px;
  background: var(--surface-muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.segment-row > div { min-width: 0; }
.segment-row strong, .segment-row small { display: block; }
.segment-row strong { font-size: 13px; }
.segment-row > div small { margin-top: 3px; color: var(--muted); font-size: 9px; line-height: 1.35; }
.segment-row > span { flex: 0 0 auto; min-width: 54px; text-align: right; }
.segment-row > span b, .segment-row > span small { display: block; }
.segment-row > span b { color: var(--blue); font-size: 15px; }
.segment-row > span small { margin-top: 2px; color: var(--muted); font-size: 9px; }


.bottom-nav {
  position: fixed;
  z-index: 20;
  left: 0;
  right: 0;
  bottom: 0;
  height: calc(64px + env(safe-area-inset-bottom));
  padding: 5px max(8px, env(safe-area-inset-left)) env(safe-area-inset-bottom) max(8px, env(safe-area-inset-right));
  background: rgba(255, 255, 255, 0.96);
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
}

.compact-heading { margin-bottom: 10px; }
.funnel-stages { display: grid; gap: 14px; }
.funnel-stage-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 13px;
}
.funnel-stage-heading span { min-width: 0; display: flex; align-items: center; gap: 8px; }
.funnel-stage-heading b {
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  color: var(--blue);
  background: var(--blue-soft);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
}
.funnel-stage-heading strong { flex: 0 0 auto; font-size: 14px; }
.funnel-bar { height: 7px; margin: 7px 0 4px 30px; border-radius: 4px; background: var(--surface-muted); overflow: hidden; }
.funnel-bar i { display: block; height: 100%; border-radius: inherit; background: var(--blue); }
.funnel-stage small { display: block; margin-left: 30px; color: var(--muted); font-size: 10px; }
.data-note { margin: 16px 0 0; color: var(--muted); font-size: 11px; line-height: 1.45; }

.nav-button {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
}
.nav-icon { display: block; width: 24px; height: 24px; }
.nav-icon svg { display: block; width: 100%; height: 100%; overflow: visible; }
.nav-icon .icon-outline {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.nav-icon .icon-filled { display: none; fill: currentColor; }
.nav-button.active .icon-outline { display: none; }
.nav-button.active .icon-filled { display: block; }
.nav-label { display: block; line-height: 15px; }
.nav-button.active { color: var(--blue); font-weight: 700; }

.status-screen {
  position: fixed;
  z-index: 50;
  inset: 0;
  padding: 30px;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}
.status-screen[hidden] { display: none; }
.status-screen p { max-width: 330px; color: var(--muted); line-height: 1.45; }
.status-screen button { height: 42px; padding: 0 18px; border: 0; border-radius: 6px; color: white; background: var(--blue); font-weight: 700; }
.spinner { width: 34px; height: 34px; margin-bottom: 16px; border: 3px solid var(--line); border-top-color: var(--blue); border-radius: 50%; animation: spin 0.8s linear infinite; }
.status-symbol { width: 42px; height: 42px; margin-bottom: 14px; border-radius: 50%; color: white; background: var(--coral); display: grid; place-items: center; font-size: 22px; font-weight: 800; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (min-width: 760px) {
  body { padding-bottom: 20px; }
  .topbar { padding-left: 24px; padding-right: 24px; }
  main { padding: 20px 24px; }
  .view[data-view="summary"].active { display: block; }
  .view[data-view="summary"] .panel { margin-bottom: 0; }
  .summary-panel { max-width: 850px; margin-left: auto; margin-right: auto; }
  .segment-panel { max-width: 850px; margin-left: auto; margin-right: auto; }
  .bottom-nav { position: static; width: 520px; height: 58px; margin: 0 auto 20px; border: 1px solid var(--line); border-radius: var(--radius); padding: 3px; }
  .overview { grid-template-columns: repeat(3, 1fr); }
  .metric { min-height: 84px; }
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #111a22;
    --surface: #1b2732;
    --surface-muted: #24333f;
    --text: #f0f4f7;
    --muted: #9eabb5;
    --line: #324451;
    --blue: #5ca9ef;
    --blue-soft: #203d56;
    --green: #63c89b;
    --green-soft: #203e34;
    --gold: #efbd51;
    --gold-soft: #40371f;
    --shadow: none;
  }
  .star-balance { border-color: #67572d; }
  .bottom-nav { background: rgba(27, 39, 50, 0.97); }
}

@media (max-width: 370px) {
  main { padding-left: 10px; padding-right: 10px; }
  .metric { padding: 10px 8px; }
  .metric strong { font-size: 19px; }
  .panel { padding: 13px; }
  .period-control button { font-size: 11px; }
  .nav-button { font-size: 10px; }
  .nav-icon { width: 22px; height: 22px; }
  .retention-grid { grid-template-columns: 1fr; }
}
