:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f2f1ee;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --border: rgba(11, 11, 11, 0.1);
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --series: #2a78d6;
  --series-track: #cde2fb;
  --brand: #c2457f;
  --brand-soft: #fbe7f0;
  --ok-bg: #e5f4e5;
  --ok-text: #006300;
  --error-bg: #fbe4e4;
  --error-text: #a12727;
  --danger: #d03b3b;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --border: rgba(255, 255, 255, 0.1);
    --grid: #2c2c2a;
    --axis: #383835;
    --series: #3987e5;
    --series-track: #184f95;
    --brand: #ec8bb8;
    --brand-soft: #3a2230;
    --ok-bg: #16301a;
    --ok-text: #0ca30c;
    --error-bg: #3a1c1c;
    --error-text: #f19a9a;
    --danger: #e66767;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232321;
  --text-primary: #f0efec;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --border: rgba(255, 255, 255, 0.1);
  --grid: #2c2c2a;
  --axis: #383835;
  --series: #3987e5;
  --series-track: #184f95;
  --brand: #ec8bb8;
  --brand-soft: #3a2230;
  --ok-bg: #16301a;
  --ok-text: #0ca30c;
  --error-bg: #3a1c1c;
  --error-text: #f19a9a;
  --danger: #e66767;
  --shadow: none;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--series); text-decoration: none; }
a:hover { text-decoration: underline; }

/* --- Top bar --- */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  color: var(--text-primary);
}
.brand img { width: 34px; height: 34px; border-radius: 50%; }
.brand small { display: block; font-weight: 400; color: var(--text-muted); font-size: 12px; line-height: 1.2; }
.nav { display: flex; gap: 4px; flex-wrap: wrap; flex: 1; }
.nav a {
  padding: 6px 12px;
  border-radius: 8px;
  color: var(--text-secondary);
  font-weight: 500;
}
.nav a:hover { background: var(--surface-2); text-decoration: none; }
.nav a.active { background: var(--brand-soft); color: var(--text-primary); }

main { max-width: 1200px; margin: 0 auto; padding: 24px 16px 60px; }

h1 { font-size: 24px; margin: 0 0 4px; }
h2 { font-size: 17px; margin: 0 0 12px; }
.lead { color: var(--text-secondary); margin: 0 0 20px; }
.muted { color: var(--text-muted); }
.small { font-size: 13px; }

.page-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 20px;
}
.page-head .lead { margin: 0; }

/* --- Cards & grid --- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
  box-shadow: var(--shadow);
  min-width: 0;
}
.card + .card { margin-top: 16px; }
.grid { display: grid; gap: 16px; margin-bottom: 16px; }
.grid > .card + .card { margin-top: 0; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 860px) {
  .grid-2, .grid-3 { grid-template-columns: minmax(0, 1fr); }
}
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }

/* --- Stat tiles --- */
.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
}
.tile-label { color: var(--text-secondary); font-size: 13px; }
.tile-value { font-size: 30px; font-weight: 700; line-height: 1.2; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tile-sub { color: var(--text-muted); font-size: 13px; }
.tile-value.tile-text { font-size: 16px; line-height: 1.35; margin-top: 6px; }

/* --- Tables --- */
.table-wrap { overflow-x: auto; margin: 0 -4px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--grid); vertical-align: top; }
th { font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.03em; white-space: nowrap; }
tr:last-child td { border-bottom: none; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.nowrap { white-space: nowrap; }
.empty { color: var(--text-muted); padding: 12px 0; }

/* --- Inline bars (single series, value at the tip) --- */
.bar-cell { min-width: 110px; width: 28%; }
td.name { min-width: 160px; }
.bar {
  display: flex;
  align-items: center;
  gap: 8px;
}
.bar-mark {
  height: 10px;
  background: var(--series);
  /* Square at the baseline, rounded at the data end */
  border-radius: 0 4px 4px 0;
  min-width: 2px;
}
.bar-value { font-variant-numeric: tabular-nums; color: var(--text-primary); font-size: 13px; white-space: nowrap; }

/* --- Column chart (daily active participants) --- */
.colchart { position: relative; padding-left: 28px; margin-top: 8px; }
.colchart-plot {
  position: relative;
  height: 180px;
  display: flex;
  align-items: flex-end;
  gap: 2px;
  border-bottom: 1px solid var(--axis);
}
.colchart-grid {
  position: absolute;
  left: 0;
  right: 0;
  border-top: 1px solid var(--grid);
  pointer-events: none;
}
.colchart-tick {
  position: absolute;
  left: -28px;
  width: 22px;
  text-align: right;
  font-size: 11px;
  color: var(--text-muted);
  transform: translateY(-50%);
}
.colchart-slot {
  flex: 1;
  height: 100%;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  position: relative;
  cursor: default;
}
.colchart-slot:hover { background: var(--surface-2); }
.colchart-col {
  width: 100%;
  max-width: 24px;
  background: var(--series);
  border-radius: 4px 4px 0 0;
  position: relative;
  z-index: 1;
}
.colchart-labels { display: flex; gap: 2px; margin-top: 4px; }
.colchart-labels span {
  flex: 1;
  text-align: center;
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: visible;
}

.tooltip {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  background: var(--text-primary);
  color: var(--surface);
  font-size: 13px;
  padding: 6px 10px;
  border-radius: 6px;
  white-space: pre-line;
  max-width: 260px;
  display: none;
}

/* --- Forms --- */
form.inline { display: inline; }
label { display: block; font-size: 13px; color: var(--text-secondary); margin-bottom: 4px; font-weight: 500; }
input[type="text"], input[type="password"], input[type="date"], select, textarea {
  width: 100%;
  font: inherit;
  color: var(--text-primary);
  background: var(--surface);
  border: 1px solid var(--axis);
  border-radius: 8px;
  padding: 8px 10px;
}
textarea { min-height: 60px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--series); outline-offset: -1px; border-color: transparent; }
.field { margin-bottom: 12px; }
.field .hint { color: var(--text-muted); font-size: 12px; margin-top: 3px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 14px; }
@media (max-width: 640px) { .form-grid { grid-template-columns: minmax(0, 1fr); } }
.filters { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.filters .field { margin-bottom: 0; min-width: 150px; }
.presets { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font: inherit;
  font-weight: 600;
  border-radius: 8px;
  padding: 8px 14px;
  border: 1px solid transparent;
  cursor: pointer;
  background: var(--series);
  color: #fff;
  white-space: nowrap;
}
.btn:hover { filter: brightness(1.05); text-decoration: none; }
.btn-secondary { background: var(--surface); color: var(--text-primary); border-color: var(--axis); }
.btn-ghost { background: transparent; color: var(--text-secondary); border-color: transparent; padding: 6px 10px; }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn-small { padding: 4px 10px; font-size: 13px; }
.chip {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--axis);
  color: var(--text-secondary);
  font-size: 13px;
}
.chip:hover { background: var(--surface-2); text-decoration: none; }
.chip.active { background: var(--brand-soft); border-color: var(--brand); color: var(--text-primary); }

.flash { border-radius: 10px; padding: 10px 14px; margin-bottom: 16px; font-weight: 500; }
.flash.ok { background: var(--ok-bg); color: var(--ok-text); }
.flash.error { background: var(--error-bg); color: var(--error-text); }

.badge {
  display: inline-block;
  font-size: 12px;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-secondary);
  white-space: nowrap;
}

.list { list-style: none; margin: 0; padding: 0; }
.list li { padding: 8px 0; border-bottom: 1px solid var(--grid); display: flex; justify-content: space-between; gap: 12px; }
.list li:last-child { border-bottom: none; }

.day-nav { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.day-nav form { display: flex; gap: 6px; }
.day-nav input[type="date"] { width: auto; }

/* --- Login page --- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; }
.login-card .brand { justify-content: center; margin-bottom: 18px; font-size: 18px; }
.login-card .brand img { width: 56px; height: 56px; }
.login-card .btn { width: 100%; margin-top: 4px; }

details.danger-zone summary { cursor: pointer; color: var(--danger); font-weight: 600; }
