:root {
  color-scheme: light;
  --bg: #f6f7f8;
  --surface: #ffffff;
  --line: #d7dce0;
  --text: #222426;
  --muted: #62666a;
  --accent: #1f6f64;
  --accent-dark: #174f48;
  --danger: #a43f36;
  --warn: #9a6a12;
  --sidebar-bg: #202628;
  --sidebar-text: #eef3f1;
  --sidebar-muted: #aab4b1;
  --sidebar-active: #dfeee8;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

a { color: var(--accent-dark); text-decoration: none; }
a:hover { text-decoration: underline; }

.app-shell {
  display: grid;
  grid-template-columns: 248px minmax(0, 1fr);
  min-height: 100vh;
}

.app-shell-single {
  grid-template-columns: minmax(0, 1fr);
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: auto;
  padding: 18px 14px;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
}

.sidebar-brand {
  margin: 0 0 22px;
  padding: 0 10px;
  font-size: 18px;
  font-weight: 750;
}

.sidebar-nav,
.sidebar-section {
  display: grid;
  gap: 6px;
}

.sidebar-section + .sidebar-section { margin-top: 18px; }

.sidebar-heading {
  padding: 0 10px;
  color: var(--sidebar-muted);
  font-size: 11px;
  font-weight: 750;
  letter-spacing: 0;
  text-transform: uppercase;
}

.sidebar-link {
  display: block;
  padding: 9px 10px;
  border-radius: 6px;
  color: var(--sidebar-text);
}

.sidebar-link:hover {
  background: rgba(255, 255, 255, 0.08);
  text-decoration: none;
}

.sidebar-link.active {
  background: var(--sidebar-active);
  color: var(--accent-dark);
  font-weight: 700;
}

.app-body {
  min-width: 0;
  display: grid;
  grid-template-rows: auto 1fr;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 58px;
  padding: 10px 24px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
}

.topbar-title { font-size: 16px; }
.spacer { flex: 1; }

.sidebar-toggle {
  display: inline-flex;
  width: 36px;
  min-height: 36px;
  padding: 0;
  align-items: center;
  justify-content: center;
}

.content { min-width: 0; padding: 20px 24px 40px; }

.user-menu { display: flex; align-items: center; gap: 12px; margin-left: auto; }
.logout-form { margin: 0; }

.logout-button {
  min-height: 30px;
  padding: 5px 10px;
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
  font-size: 13px;
}

h1 { margin: 0 0 16px; font-size: 24px; font-weight: 650; }
h2 { margin: 24px 0 10px; font-size: 18px; }

.page-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.page-heading h1,
.page-heading p { margin-bottom: 0; }

.data-readiness {
  width: 100%;
  margin-bottom: 12px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-left-width: 5px;
  border-radius: 8px;
  background: var(--surface);
  overflow-wrap: anywhere;
}

.data-readiness.ready { border-color: #88b6a3; background: #f1f8f4; }
.data-readiness.incomplete,
.data-readiness.error { border-color: #d49b95; background: #fbf0ef; }
.data-readiness.stale { border-color: #d7b56d; background: #fbf5e7; }
.data-readiness.empty { border-color: var(--line); background: var(--surface); }
.data-readiness-title { font-weight: 700; margin-bottom: 2px; }

.data-progress { display: grid; gap: 5px; margin: 6px 0 8px; }
.data-progress-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.data-progress-percent {
  color: var(--text);
  font-size: 20px;
  font-weight: 750;
  line-height: 1.1;
  white-space: nowrap;
}
.data-progress-track {
  width: 100%;
  height: 12px;
  overflow: hidden;
  border: 1px solid rgba(34, 36, 38, 0.16);
  border-radius: 6px;
  background: rgba(34, 36, 38, 0.08);
}
.data-progress-fill {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width 180ms ease;
}
.data-readiness.ready .data-progress-fill { background: var(--accent); }
.data-readiness.incomplete .data-progress-fill,
.data-readiness.error .data-progress-fill { background: var(--danger); }
.data-readiness.stale .data-progress-fill { background: var(--warn); }
.data-readiness.empty .data-progress-fill { background: var(--muted); }
.data-progress-meta { color: var(--muted); font-size: 13px; line-height: 1.35; }
.data-readiness-message,
.data-readiness-activity,
.data-readiness-details { color: var(--muted); }
.data-readiness-activity.loading { color: var(--warn); font-weight: 600; }
.data-readiness-details { margin-top: 4px; font-size: 12px; line-height: 1.4; }

.toolbar, .filters, .panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  margin-bottom: 16px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  align-items: end;
}

label { display: grid; gap: 4px; color: var(--muted); font-size: 12px; }

input, select, button {
  min-height: 34px;
  border-radius: 6px;
  border: 1px solid var(--line);
  padding: 6px 8px;
  font: inherit;
}

button {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-weight: 600;
}

button.secondary { background: #fff; color: var(--accent-dark); }

.button-link {
  display: inline-flex;
  min-height: 34px;
  align-items: center;
  justify-content: center;
  padding: 6px 10px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.button-link:hover { text-decoration: none; }
.button-link.secondary { background: #fff; color: var(--accent-dark); }
.danger-link { color: var(--danger); }
.danger-button { border-color: var(--danger); background: var(--danger); }

.row-actions {
  display: flex;
  gap: 10px;
  white-space: nowrap;
}

.form-panel { max-width: 760px; }

.stack-form {
  display: grid;
  gap: 12px;
}

.stack-form label {
  color: var(--text);
  font-size: 14px;
}

.stack-form input,
.stack-form select,
.stack-form textarea { width: 100%; }

.errorlist {
  margin: 4px 0 0;
  padding-left: 18px;
  color: var(--danger);
}

.form-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  margin-top: 14px;
}

table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--line);
}

th, td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

th {
  position: sticky;
  top: 0;
  background: #ecebe6;
  z-index: 1;
  font-size: 12px;
  color: #3a3d40;
}

.report-total td { background: #f3f1ea; font-weight: 700; }
.table-wrap { overflow: auto; max-width: 100%; border-radius: 8px; }
.messages { display: grid; gap: 8px; margin-bottom: 16px; }

.anomaly-summary {
  display: grid;
  gap: 10px;
  margin-bottom: 12px;
}

.anomaly-card {
  border: 1px solid #dfb8a4;
  border-left: 4px solid var(--warn);
  border-radius: 8px;
  padding: 10px 12px;
  background: #fff8ee;
}

.anomaly-card-title {
  margin-bottom: 8px;
  font-weight: 700;
}

.anomaly-card-grid {
  display: grid;
  grid-template-columns: 130px minmax(0, 1fr);
  gap: 6px 12px;
  margin: 0;
}

.anomaly-card-grid dt {
  color: var(--muted);
  font-weight: 600;
}

.anomaly-card-grid dd {
  margin: 0;
}
.message {
  padding: 10px 12px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: #fff;
}
.message.success { border-color: #88b6a3; }
.message.error { border-color: #d49b95; color: var(--danger); }
.muted { color: var(--muted); }

.badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 999px;
  background: #e7efec;
  color: var(--accent-dark);
  white-space: nowrap;
  font-size: 12px;
}

.badge.warn { background: #f6e7bf; color: var(--warn); }

.kv {
  display: grid;
  grid-template-columns: minmax(180px, 260px) 1fr;
  gap: 1px;
  border: 1px solid var(--line);
  background: var(--line);
}

.kv div {
  background: var(--surface);
  padding: 8px 10px;
  min-width: 0;
  overflow-wrap: anywhere;
}

pre {
  overflow: auto;
  padding: 12px;
  background: #202628;
  color: #f6f5ef;
  border-radius: 8px;
  max-height: 520px;
}

.pagination { margin: 14px 0; display: flex; gap: 10px; align-items: center; }
.auth-panel { max-width: 420px; }
.auth-form { display: grid; gap: 12px; }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 10px;
}

.kpi-item {
  min-width: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.kpi-label { color: var(--muted); font-size: 12px; }
.kpi-value { margin-top: 4px; font-size: 22px; font-weight: 750; }

.chart-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 14px;
}

.chart-panel {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 14px;
}

.chart-panel h2 { margin-top: 0; }

.chart-panel-wide {
  grid-column: 1 / -1;
}

.report-chart {
  width: 100%;
  height: 320px;
  min-height: 320px;
}

.chart-fallback {
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
}

.chart-ready .chart-fallback { display: none; }

.counter-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 16px;
}

.counter-tab {
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
}

.counter-tab:hover { text-decoration: none; border-color: var(--accent); }
.counter-tab.active { border-color: var(--accent); background: var(--accent); color: #fff; }

.counter-table {
  width: max-content;
  min-width: 100%;
  font-size: 12px;
}

.counter-table th { white-space: nowrap; }

.counter-table td {
  min-width: 120px;
  max-width: 320px;
  padding: 6px 8px;
}

.counter-value {
  display: block;
  max-width: 304px;
  max-height: 66px;
  overflow: auto;
  color: inherit;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

body.sidebar-collapsed .app-shell { grid-template-columns: 0 minmax(0, 1fr); }
body.sidebar-collapsed .sidebar { display: none; }

@media (max-width: 860px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: static;
    height: auto;
    max-height: none;
    display: none;
  }
  body.sidebar-open .sidebar { display: block; }
  body.sidebar-collapsed .app-shell { grid-template-columns: 1fr; }
  body.sidebar-collapsed .sidebar { display: none; }
  .topbar { padding: 10px 14px; }
  .content { padding: 14px; }
  .data-progress-header { align-items: flex-start; }
  .data-progress-percent { font-size: 18px; }
  .kv { grid-template-columns: 1fr; }
  .chart-grid { grid-template-columns: 1fr; }
}

@media (max-width: 520px) {
  .grid,
  .kpi-grid {
    grid-template-columns: 1fr;
  }

  .topbar {
    align-items: flex-start;
  }

  .user-menu {
    gap: 8px;
    flex-wrap: wrap;
    justify-content: flex-end;
  }
}
