:root {
  --bg: #f7fafc;
  --bg-grad-top: #ffffff;
  --bg-grad-bot: #f1f5f9;
  --card: #ffffff;
  --ink: #0f172a;
  --muted: #475569;
  --border: rgba(0,0,0,0.08);
  --grid-strong: rgba(15,23,42,0.18);
  --grid-weak: rgba(15,23,42,0.10);
  --pill-bg: rgba(0,0,0,0.04);
  --pill-brd: rgba(0,0,0,0.12);
  --warn: #b45309;
  --hover-bg: rgba(0,0,0,0.02);
}

[data-theme="dark"] {
  --bg: #0f172a;
  --bg-grad-top: #0b1022;
  --bg-grad-bot: #0f172a;
  --card: #111827;
  --ink: #e5e7eb;
  --muted: #9ca3af;
  --border: rgba(255,255,255,0.10);
  --grid-strong: rgba(229,231,235,0.18);
  --grid-weak: rgba(229,231,235,0.10);
  --pill-bg: rgba(255,255,255,0.06);
  --pill-brd: rgba(255,255,255,0.12);
  --hover-bg: rgba(255,255,255,0.05);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f172a;
    --bg-grad-top: #0b1022;
    --bg-grad-bot: #0f172a;
    --card: #111827;
    --ink: #e5e7eb;
    --muted: #9ca3af;
    --border: rgba(255,255,255,0.10);
    --grid-strong: rgba(229,231,235,0.18);
    --grid-weak: rgba(229,231,235,0.10);
    --pill-bg: rgba(255,255,255,0.06);
    --pill-brd: rgba(255,255,255,0.12);
    --hover-bg: rgba(255,255,255,0.05);
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  background: linear-gradient(180deg, var(--bg-grad-top), var(--bg-grad-bot));
  color: var(--ink);
  min-height: 100vh;
  transition: background-color 0.3s ease, color 0.3s ease;
}

header {
  padding: 12px;
  text-align: center;
  background: transparent;
}

h1 {
  margin: 12px 0px 24px;
  font-size: clamp(22px, 3.2vw, 36px);
  letter-spacing: 0.2px;
}

.theme-toggle {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 20px;
  color: var(--ink);
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  z-index: 1000;
}

.theme-toggle:hover {
  background: var(--hover-bg);
  transform: scale(1.05);
}

.wrap {
  max-width: 1200px;
  margin: 0 auto;
  padding: 12px;
}

@media (max-width: 768px) {
  .wrap {
    padding: 8px;
  }

  .theme-toggle {
    bottom: 10px;
    right: 10px;
    width: 40px;
    height: 40px;
    font-size: 16px;
  }

  h1 {
    font-size: clamp(18px, 5vw, 28px);
    margin: 8px 0 16px;
  }

  #invitePanel {
    margin: 16px 0 !important;
  }

  #invitePanel h2 {
    font-size: clamp(18px, 4vw, 24px) !important;
  }

  #invitePanel p {
    font-size: 14px !important;
  }

  #invitePanel a {
    display: block !important;
    text-align: center;
    padding: 12px 16px !important;
    font-size: 14px !important;
  }
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px;
  box-shadow: 0 6px 20px rgba(0,0,0,.08);
  transition: background-color 0.3s ease, border-color 0.3s ease;
}

.summary {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px;
  border-radius: 14px;
}

.kpi-row {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  justify-content: center;
}

.kpi-block {
  flex: 1;
  min-width: 150px;
  text-align: center;
}

@media (max-width: 768px) {
  .kpi-row {
    gap: 12px;
  }
  
  .kpi-block {
    min-width: 120px;
  }
}

@media (max-width: 480px) {
  .kpi-row {
    flex-direction: column;
    gap: 8px;
  }
  
  .kpi-block {
    min-width: auto;
  }
}

.kpi-block.large .label {
  font-size: 14px;
  color: var(--muted);
}

.kpi-block.large .value {
  font-size: clamp(28px, 5vw, 48px);
  font-weight: 700;
}

.kpi-block.small .label {
  font-size: 12px;
  color: var(--muted);
}

.kpi-block.small .num {
  font-size: clamp(18px, 3.5vw, 28px);
  font-weight: 600;
}

.kpi-block.small .pct {
  font-size: 12px;
  color: var(--muted);
}

.updated {
  font-size: 12px;
  color: var(--muted);
  text-align: right;
}

.controls {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

@media (max-width: 768px) {
  .controls {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  #chartToggles {
    flex-direction: column !important;
    gap: 8px !important;
  }

  .seg button {
    padding: 8px 12px;
    font-size: 13px;
    min-height: 44px;
  }

  #chartPanel {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  #barChart {
    min-width: 300px;
  }
}

.seg {
  display: inline-flex;
  border-radius: 8px;
  border: 1px solid var(--pill-brd);
  overflow: hidden;
}

.seg button {
  background: transparent;
  color: var(--ink);
  border: 0;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 12px;
  transition: background-color 0.2s ease;
}

.seg button.active {
  background: var(--pill-bg);
}

.seg button:disabled {
  opacity: 0.45;
  pointer-events: none;
}

.seg button:hover:not(:disabled):not(.active) {
  background: var(--hover-bg);
}

.warn {
  color: var(--warn);
  font-size: 12px;
  margin: 6px 0;
}

#chartPanel, #tablePanel {
  display: block;
  width: 100%;
}

.hidden {
  display: none !important;
}

#barChart {
  display: block;
  width: 100% !important;
  max-width: 100%;
}

canvas#barChart {
  height: auto;
  max-height: none;
}

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

@media (max-width: 768px) {
  #tablePanel {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  table {
    min-width: 600px;
    font-size: 11px;
  }
  
  th, td {
    padding: 6px 8px;
    white-space: nowrap;
  }
}

thead th {
  position: static;
  background: var(--card);
  cursor: pointer;
  transition: background-color 0.2s ease;
}

thead th:hover {
  background: var(--hover-bg);
}

th, td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  color: var(--ink);
  white-space: nowrap;
}

tbody tr:hover {
  background: var(--hover-bg);
}

th.sort-asc::after {
  content: " ▲";
  font-size: 11px;
  color: var(--muted);
}

th.sort-desc::after {
  content: " ▼";
  font-size: 11px;
  color: var(--muted);
}

@media (max-width: 480px) {
  .card {
    padding: 8px;
    border-radius: 8px;
  }

  .summary {
    padding: 8px;
    gap: 12px;
  }

  .kpi-block.large .value {
    font-size: clamp(24px, 8vw, 36px);
  }

  .kpi-block.small .num {
    font-size: clamp(16px, 6vw, 24px);
  }

  .seg {
    width: 100%;
  }

  .seg button {
    flex: 1;
    padding: 10px 8px;
    font-size: 12px;
  }

  table {
    font-size: 10px;
    min-width: 500px;
  }

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

#invitePanel {
  margin: 36px 0;
}

#invitePanel .summary {
  gap: 16px;
}

#invitePanel h2 {
  margin: 0;
  font-size: clamp(22px, 2.6vw, 28px);
  font-weight: 700;
}

#invitePanel p {
  margin: 0;
  color: var(--muted);
  font-size: 16px;
  line-height: 1.5;
}

#invitePanel a {
  display: inline-block;
  margin-top: 10px;
  padding: 10px 16px;
  border: 1px solid var(--pill-brd);
  background: var(--pill-bg);
  border-radius: 8px;
  text-decoration: none;
  color: var(--ink);
  font-weight: 600;
  font-size: 15px;
}

#chartToggles {
  display: flex;
  gap: 10px;
  align-items: center;
}

#popWarning {
  display: none;
}

#tablePanel {
  border-radius: 10px;
}

* {
  transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
}
