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

:root {
  --green: #22C55E;
  --yellow: #EAB308;
  --red: #EF4444;
  --blue: #3B82F6;
  --gray-50: #F9FAFB;
  --gray-100: #F3F4F6;
  --gray-200: #E5E7EB;
  --gray-400: #9CA3AF;
  --gray-500: #6B7280;
  --gray-600: #4B5563;
  --gray-700: #374151;
  --gray-800: #1F2937;
  --gray-900: #111827;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.07), 0 2px 4px rgba(0,0,0,0.06);
}

html { font-size: 14px; -webkit-text-size-adjust: 100%; }
html, body {
  margin: 0; padding: 0; height: 100%;
  overflow: hidden; /* Prevent ghost outer scroll in GAS iframe */
}
body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #fff;
  color: var(--gray-900);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
#scroll-container {
  position: absolute;
  top: 56px;    /* below header */
  left: 0; right: 0;
  bottom: 60px; /* above tab bar */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: manipulation;
  overscroll-behavior: contain;
}

/* ─── Header ─────────────────────────────────────────────────────────── */
.app-header {
  position: fixed; top: 0; left: 0; right: 0;
  height: 56px;
  background: #fff;
  border-bottom: 1px solid var(--gray-200);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px;
  z-index: 100;
}
.logo {
  font-size: 22px; font-weight: 800; letter-spacing: -0.5px;
  color: var(--gray-900);
}
.logo-img {
  height: 32px; width: auto;
}
.refresh-btn {
  background: none; border: none; cursor: pointer; padding: 8px;
  color: var(--gray-500); border-radius: 50%;
  transition: background 0.2s;
}
.refresh-btn:hover { background: var(--gray-100); }
.refresh-btn.spinning svg { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ─── Tab Bar ────────────────────────────────────────────────────────── */
.tab-bar {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: 64px;
  background: #fff;
  border-top: 1px solid var(--gray-200);
  display: flex; align-items: center; justify-content: space-around;
  z-index: 100;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.tab-btn {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: none; border: none; cursor: pointer;
  padding: 8px 4px; min-width: 56px;
  color: var(--gray-400);
  font-size: 10px; font-weight: 500;
  transition: color 0.2s;
}
.tab-btn.active { color: var(--blue); }
.tab-btn span { margin-top: 2px; }

/* ─── Content Tabs ────────────────────────────────────────────────────── */
.tab-content { display: none; padding: 16px; }
.tab-content.active { display: block; }
.tab-inner { animation: fadeIn 0.25s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ─── Cards ──────────────────────────────────────────────────────────── */
.card {
  background: #fff;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.card-title {
  font-size: 16px; font-weight: 600; color: var(--gray-800);
  margin-bottom: 8px;
}
.card-label {
  font-size: 12px; font-weight: 500; color: var(--gray-500);
  text-transform: uppercase; letter-spacing: 0.5px;
  margin-bottom: 4px;
}
.big-number {
  font-size: 32px; font-weight: 700; color: var(--gray-900);
  line-height: 1.2;
}
.big-number.green { color: var(--green); }
.big-number.red { color: var(--red); }
.big-number.yellow { color: var(--yellow); }

/* ─── Summary Row ────────────────────────────────────────────────────── */
.summary-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}
.summary-card {
  background: var(--gray-50);
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  padding: 12px;
  text-align: center;
}
.summary-card .value {
  font-size: 20px; font-weight: 700; color: var(--gray-900);
}
.summary-card .label {
  font-size: 11px; color: var(--gray-500); margin-top: 2px;
}

@media (min-width: 768px) {
  .summary-row { grid-template-columns: repeat(4, 1fr); }
  .cards-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
}

/* ─── Status Badges ──────────────────────────────────────────────────── */
.badge {
  display: inline-block; padding: 2px 8px;
  border-radius: 12px; font-size: 11px; font-weight: 600;
  line-height: 1.6; white-space: nowrap;
}
.badge-new { background: #DBEAFE; color: #1E40AF; }
.badge-scheduled { background: #DBEAFE; color: #1E3A8A; }
.badge-installed { background: #DCFCE7; color: #166534; }
.badge-active { background: #052E16; color: #22C55E; }
.badge-cancelled { background: #F3F4F6; color: #374151; }
.badge-churned { background: #FEE2E2; color: #991B1B; }
.badge-disputed { background: #FED7AA; color: #9A3412; }
.badge-on_hold, .badge-on-hold { background: #FEF9C3; color: #854D0E; }
.badge-review { background: #F3E8FF; color: #6B21A8; }
.badge-due { background: #DCFCE7; color: #166534; }
.badge-eligible { background: #DBEAFE; color: #1E40AF; }
.badge-pending { background: #FEF9C3; color: #854D0E; }

/* ─── Tier Badges ────────────────────────────────────────────────────── */
.tier-badge {
  display: inline-block; padding: 2px 10px;
  border-radius: 12px; font-size: 11px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.3px;
}
.tier-tier1 { background: var(--gray-200); color: var(--gray-700); }
.tier-tier2 { background: #DBEAFE; color: #1E40AF; }
.tier-r385 { background: #FEF3C7; color: #92400E; border: 1px solid #F59E0B; }

/* ─── Health Gauge ───────────────────────────────────────────────────── */
.gauge-container {
  display: flex; flex-direction: column; align-items: center;
  padding: 16px 0;
}
.gauge-svg { width: 160px; height: 160px; transform: rotate(-90deg); }
.gauge-bg { fill: none; stroke: var(--gray-200); stroke-width: 10; }
.gauge-fill { fill: none; stroke-width: 10; stroke-linecap: round; transition: stroke-dashoffset 1s ease; }
.gauge-fill.green { stroke: var(--green); }
.gauge-fill.yellow { stroke: var(--yellow); }
.gauge-fill.red { stroke: var(--red); }
.gauge-text {
  position: absolute;
  font-size: 36px; font-weight: 800; color: var(--gray-900);
}
.gauge-wrapper { position: relative; display: flex; align-items: center; justify-content: center; }
.gauge-label {
  font-size: 12px; color: var(--gray-500); margin-top: 8px;
}

/* ─── Advance Cap Bar ────────────────────────────────────────────────── */
.cap-bar-container { margin: 12px 0; }
.cap-bar-bg {
  height: 8px; background: var(--gray-200);
  border-radius: 4px; overflow: hidden;
}
.cap-bar-fill {
  height: 100%; border-radius: 4px;
  transition: width 0.6s ease;
}
.cap-bar-fill.green { background: var(--green); }
.cap-bar-fill.yellow { background: var(--yellow); }
.cap-bar-fill.red { background: var(--red); }
.cap-bar-labels {
  display: flex; justify-content: space-between;
  font-size: 11px; color: var(--gray-500); margin-top: 4px;
}

/* ─── Bar Chart ──────────────────────────────────────────────────────── */
.bar-chart {
  display: flex; align-items: flex-end; gap: 6px;
  height: 120px; padding-top: 8px;
}
.bar-col {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: flex-end; height: 100%;
}
.bar {
  width: 100%; min-height: 2px;
  border-radius: 4px 4px 0 0;
  transition: height 0.5s ease;
}
.bar.green { background: var(--green); }
.bar.yellow { background: var(--yellow); }
.bar.red { background: var(--red); }
.bar.blue { background: var(--blue); }
.bar-label {
  font-size: 10px; color: var(--gray-500);
  margin-top: 4px; text-align: center;
}
.bar-value {
  font-size: 9px; color: var(--gray-600);
  margin-bottom: 2px; text-align: center;
}

/* ─── Mini Rows ──────────────────────────────────────────────────────── */
.mini-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--gray-100);
  font-size: 13px;
}
.mini-row:last-child { border-bottom: none; }
.mini-row .left { flex: 1; color: var(--gray-700); }
.mini-row .right { font-weight: 600; color: var(--gray-900); }
.mini-row .right.negative { color: var(--red); }
.mini-row .right.positive { color: var(--green); }

/* ─── Collapsible ────────────────────────────────────────────────────── */
.collapsible-header {
  display: flex; justify-content: space-between; align-items: center;
  cursor: pointer; padding: 8px 0;
  font-size: 14px; font-weight: 600; color: var(--gray-700);
}
.collapsible-header .arrow {
  transition: transform 0.3s; font-size: 12px; color: var(--gray-400);
}
.collapsible-header.open .arrow { transform: rotate(180deg); }
.collapsible-body { display: none; padding-top: 4px; }
.collapsible-body.open { display: block; animation: fadeIn 0.2s ease; }

/* ─── Customer Cards ─────────────────────────────────────────────────── */
.customer-card {
  background: #fff; border: 1px solid var(--gray-200);
  border-radius: var(--radius); padding: 12px 16px;
  margin-bottom: 8px; box-shadow: var(--shadow);
  cursor: pointer; transition: box-shadow 0.2s;
}
.customer-card:active { box-shadow: var(--shadow-md); }
.customer-header {
  display: flex; justify-content: space-between; align-items: flex-start;
}
.customer-name { font-weight: 600; color: var(--gray-800); font-size: 14px; }
.customer-meta { font-size: 12px; color: var(--gray-500); margin-top: 2px; }
.customer-details {
  display: none; margin-top: 12px; padding-top: 12px;
  border-top: 1px solid var(--gray-100);
}
.customer-details.open { display: block; animation: fadeIn 0.2s ease; }
.detail-row {
  display: flex; justify-content: space-between;
  padding: 4px 0; font-size: 13px;
}
.detail-row .label { color: var(--gray-500); }
.detail-row .value { color: var(--gray-800); font-weight: 500; text-align: right; }

/* ─── Search & Filters ───────────────────────────────────────────────── */
.search-bar {
  position: sticky; top: -16px; z-index: 10;
  background: #fff; 
  margin: -16px -16px 16px -16px;
  padding: 16px 16px 12px 16px;
}
.search-input {
  width: 100%; padding: 10px 12px 10px 36px;
  border: 1px solid var(--gray-200); border-radius: var(--radius);
  font-size: 14px; color: var(--gray-800);
  background: var(--gray-50);
  outline: none; transition: border-color 0.2s;
}
.search-input:focus { border-color: var(--blue); }
.search-icon {
  position: absolute; left: 10px; top: 50%; transform: translateY(-50%);
  color: var(--gray-400);
}
.search-wrapper { position: relative; }
.filter-chips {
  display: flex; gap: 6px; overflow-x: auto;
  padding: 4px 0 8px; -webkit-overflow-scrolling: touch;
}
.filter-chips::-webkit-scrollbar { display: none; }
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 6px 12px; border-radius: 20px;
  font-size: 12px; font-weight: 500;
  background: var(--gray-100); color: var(--gray-600);
  border: 1px solid transparent;
  cursor: pointer; white-space: nowrap;
  transition: all 0.2s;
}
.chip.active { background: #EFF6FF; color: var(--blue); border-color: var(--blue); }
.chip:active { transform: scale(0.96); }

/* ─── Count Bar ──────────────────────────────────────────────────────── */
.count-bar {
  position: sticky; bottom: -16px;
  margin: 16px -16px -16px -16px;
  background: var(--gray-50); border-top: 1px solid var(--gray-200);
  padding: 8px 16px; font-size: 12px; color: var(--gray-500);
  text-align: center;
}

/* ─── Team View ──────────────────────────────────────────────────────── */
.team-card {
  background: #fff; border: 1px solid var(--gray-200);
  border-radius: var(--radius); padding: 12px 16px;
  margin-bottom: 8px; box-shadow: var(--shadow);
  cursor: pointer; transition: box-shadow 0.2s;
}
.team-card:active { box-shadow: var(--shadow-md); }
.team-header-row {
  display: flex; justify-content: space-between; align-items: center;
}
.team-rep-name { font-weight: 600; font-size: 14px; color: var(--gray-800); }
.team-stats {
  display: flex; gap: 12px; margin-top: 6px;
  font-size: 12px; color: var(--gray-500);
}
.team-stat-val { font-weight: 600; color: var(--gray-800); }
.health-dot {
  width: 10px; height: 10px; border-radius: 50%; display: inline-block;
}
.health-dot.green { background: var(--green); }
.health-dot.yellow { background: var(--yellow); }
.health-dot.red { background: var(--red); }

.team-totals {
  background: var(--gray-50); border: 1px solid var(--gray-200);
  border-radius: var(--radius); padding: 16px; margin-bottom: 16px;
}
.team-totals-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center;
}
.btn-back {
  display: inline-flex; align-items: center; gap: 4px;
  background: none; border: none; cursor: pointer;
  font-size: 14px; font-weight: 500; color: var(--blue);
  padding: 8px 0; margin-bottom: 8px;
}
.btn-back:hover { text-decoration: underline; }

/* ─── Skeleton ───────────────────────────────────────────────────────── */
.skeleton-container { padding: 0; }
.skeleton-card {
  background: #fff; border: 1px solid var(--gray-200);
  border-radius: var(--radius); padding: 16px; margin-bottom: 12px;
}
.skeleton-line {
  height: 14px; background: var(--gray-200);
  border-radius: 4px; margin-bottom: 10px;
  animation: shimmer 1.5s ease-in-out infinite;
}
.skeleton-block {
  height: 48px; background: var(--gray-200);
  border-radius: 4px;
  animation: shimmer 1.5s ease-in-out infinite;
}
.skeleton-circle {
  width: 120px; height: 120px; border-radius: 50%;
  background: var(--gray-200); margin: 16px auto;
  animation: shimmer 1.5s ease-in-out infinite;
}
.w40 { width: 40%; } .w50 { width: 50%; } .w60 { width: 60%; }
.w70 { width: 70%; } .w80 { width: 80%; }
@keyframes shimmer {
  0% { opacity: 1; } 50% { opacity: 0.5; } 100% { opacity: 1; }
}

/* ─── Error Overlay ──────────────────────────────────────────────────── */
.error-overlay {
  position: fixed; top: 56px; left: 0; right: 0; bottom: 64px;
  background: #fff; display: flex; flex-direction: column;
  align-items: center; justify-content: center; z-index: 50;
  padding: 32px;
}
.error-icon { font-size: 48px; margin-bottom: 16px; }
.error-message {
  font-size: 16px; color: var(--gray-600);
  text-align: center; margin-bottom: 24px;
}
.btn-retry {
  padding: 10px 24px; background: var(--blue); color: #fff;
  border: none; border-radius: var(--radius); font-size: 14px;
  font-weight: 600; cursor: pointer; transition: background 0.2s;
}
.btn-retry:hover { background: #2563EB; }

/* ─── Bonus List ─────────────────────────────────────────────────────── */
.bonus-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--gray-100);
}
.bonus-item:last-child { border-bottom: none; }
.bonus-left { flex: 1; }
.bonus-customer { font-weight: 600; font-size: 13px; color: var(--gray-800); }
.bonus-meta { font-size: 11px; color: var(--gray-500); margin-top: 2px; }
.bonus-right { text-align: right; }
.bonus-amount { font-weight: 700; font-size: 14px; color: var(--gray-900); }
.bonus-countdown { font-size: 10px; color: var(--gray-500); }

/* ─── New Rep Message ────────────────────────────────────────────────── */
.new-rep-msg {
  text-align: center; padding: 32px 16px; color: var(--gray-500);
}
.new-rep-msg .emoji { font-size: 48px; margin-bottom: 12px; }
.new-rep-msg .msg { font-size: 16px; line-height: 1.6; }

/* ─── Rep Detail in Team ─────────────────────────────────────────────── */
.rep-detail-view { display: none; }
.rep-detail-view.active { display: block; animation: fadeIn 0.25s ease; }

/* ─── Responsive ─────────────────────────────────────────────────────── */
@media (min-width: 768px) {
  body { max-width: 768px; margin: 0 auto; }
  .tab-bar { max-width: 768px; left: 50%; transform: translateX(-50%); }
  .app-header { max-width: 768px; left: 50%; transform: translateX(-50%); }
}