/* ---------- Design tokens (light default, dark via prefers-color-scheme) ---------- */
:root {
  color-scheme: light;
  --page:            #f5f5f3;
  --surface-1:       #ffffff;
  --surface-2:       #fcfcfb;
  --text-primary:    #0b0b0b;
  --text-secondary:  #52514e;
  --text-muted:      #898781;
  --border:          rgba(11, 11, 11, 0.08);
  --gridline:         #ececE7;
  --accent:          #2a78d6;
  --accent-ink:      #ffffff;
  --good:            #0ca30c;
  --critical:        #d03b3b;
  --radius:          14px;
  --radius-sm:       9px;

  --series-1:        #2a78d6;
  --series-2:        #1baf7a;

  --shadow-sm: 0 1px 2px rgba(11,11,11,0.04), 0 1px 4px rgba(11,11,11,0.05);
  --shadow-md: 0 2px 6px rgba(11,11,11,0.06), 0 8px 24px rgba(11,11,11,0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page:            #0c0c0c;
    --surface-1:       #171716;
    --surface-2:       #1c1c1b;
    --text-primary:    #ffffff;
    --text-secondary:  #c3c2b7;
    --text-muted:      #898781;
    --border:          rgba(255, 255, 255, 0.08);
    --gridline:         #292927;
    --accent:          #3987e5;
    --accent-ink:      #ffffff;
    --good:            #0ca30c;
    --critical:        #e66767;

    --series-1:        #3987e5;
    --series-2:        #199e70;

    --shadow-sm: 0 1px 2px rgba(0,0,0,0.3), 0 1px 4px rgba(0,0,0,0.35);
    --shadow-md: 0 2px 8px rgba(0,0,0,0.35), 0 12px 28px rgba(0,0,0,0.4);
  }
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }

a { color: var(--accent); text-decoration: none; }

.muted { color: var(--text-secondary); }

.text-good { color: var(--good); }
.text-critical { color: var(--critical); }

/* ---------- App shell (sidebar + content) ---------- */
.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 232px;
  flex-shrink: 0;
  background: var(--surface-1);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px 22px;
}

.brand-icon { font-size: 22px; }
.brand-name { font-weight: 700; font-size: 15px; letter-spacing: -0.01em; }

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s ease, color 0.15s ease;
}

.nav-icon { font-size: 15px; }

.nav-link:hover { background: var(--page); color: var(--text-primary); }

.nav-link.active {
  background: var(--accent);
  color: var(--accent-ink);
}

.nav-logout { margin-top: auto; }

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 20px 32px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}

.page-header h1 { font-size: 20px; }

.page-header-actions { display: flex; align-items: center; gap: 10px; }

/* ---------- Layout ---------- */
.container {
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px 32px 48px;
  width: 100%;
}

.container.narrow { max-width: 640px; }

/* ---------- Stat tiles ---------- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 28px;
}

@media (max-width: 900px) {
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 520px) {
  .stats-grid { grid-template-columns: 1fr; }
}

.stat-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-top: 3px solid var(--gridline);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: box-shadow 0.15s ease, transform 0.15s ease;
}

.stat-card:hover { box-shadow: var(--shadow-md); transform: translateY(-1px); }

.stat-card--accent-1 { border-top-color: var(--series-1); }
.stat-card--accent-2 { border-top-color: var(--series-2); }
.stat-card--accent-good { border-top-color: var(--good); }
.stat-card--accent-critical { border-top-color: var(--critical); }

.stat-label {
  font-size: 12.5px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
}

.stat-value {
  font-size: 27px;
  font-weight: 650;
  color: var(--text-primary);
  letter-spacing: -0.01em;
}

/* ---------- Panels & tables ---------- */
.panel {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 24px;
  margin-bottom: 20px;
}

.panel h2 { font-size: 16px; margin-bottom: 16px; }

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}
.panel-header h2 { margin-bottom: 0; }

.table-wrap { overflow-x: auto; margin: -4px -4px 0; }

table { width: 100%; border-collapse: collapse; min-width: 820px; }

th, td {
  padding: 12px 14px;
  text-align: left;
  border-bottom: 1px solid var(--gridline);
  white-space: nowrap;
}

thead th {
  background: var(--surface-2);
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  font-weight: 650;
  border-top: 1px solid var(--gridline);
}

th:first-child { border-top-left-radius: var(--radius-sm); }
th:last-child { border-top-right-radius: var(--radius-sm); }

tbody tr { transition: background 0.1s ease; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: none; }

td.num { font-variant-numeric: tabular-nums; text-align: right; }
th:not(:first-child) { text-align: right; }

td.empty { text-align: center; color: var(--text-muted); padding: 40px 0; white-space: normal; }

.actions { display: flex; gap: 4px; justify-content: flex-end; }
.actions form { display: inline; }

.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 15px;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  transition: background 0.15s ease;
}
.icon-btn:hover { background: var(--gridline); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.12s ease, filter 0.12s ease;
}

.btn-primary { background: var(--accent); color: var(--accent-ink); box-shadow: var(--shadow-sm); }
.btn-primary:hover { filter: brightness(1.06); transform: translateY(-1px); box-shadow: var(--shadow-md); }

.btn-ghost { background: var(--surface-1); color: var(--text-primary); border-color: var(--border); }
.btn-ghost:hover { background: var(--page); transform: translateY(-1px); }

.btn-block { width: 100%; justify-content: center; }

/* ---------- Forms ---------- */
form label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 14px;
}

form input,
form select {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 10px 12px;
  font-size: 15px;
  font-family: inherit;
  color: var(--text-primary);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

form input:focus,
form select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 16px;
}

@media (max-width: 560px) {
  .grid-2 { grid-template-columns: 1fr; }
}

.preview-box {
  display: flex;
  gap: 24px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 18px;
  margin: 6px 0 20px;
}

.preview-box div { display: flex; flex-direction: column; gap: 4px; }
.preview-box span { font-size: 12px; color: var(--text-muted); }
.preview-box strong { font-size: 18px; font-variant-numeric: tabular-nums; }

.form-actions { display: flex; gap: 10px; }

.alert {
  background: rgba(208, 59, 59, 0.10);
  color: var(--critical);
  border: 1px solid rgba(208, 59, 59, 0.22);
  padding: 11px 15px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  margin-bottom: 16px;
}

/* ---------- Partner donut chart & legend ---------- */
.donut-wrap {
  display: flex;
  align-items: center;
  gap: 32px;
  flex-wrap: wrap;
  padding-top: 4px;
}

.donut {
  position: relative;
  width: 160px;
  height: 160px;
  border-radius: 50%;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
}

.donut-hole {
  position: absolute;
  inset: 22px;
  background: var(--surface-1);
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 2px;
  padding: 0 8px;
}

.donut-total-label { font-size: 11px; color: var(--text-muted); }
.donut-total-value { font-size: 15px; font-weight: 650; font-variant-numeric: tabular-nums; }

.legend {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.legend-item {
  display: grid;
  grid-template-columns: 12px 1fr auto auto;
  align-items: center;
  gap: 10px;
  font-size: 14px;
}

.legend-swatch { width: 12px; height: 12px; border-radius: 3px; }
.swatch-1 { background: var(--series-1); }
.swatch-2 { background: var(--series-2); }

.legend-name { color: var(--text-primary); font-weight: 500; }
.legend-pct { font-weight: 650; font-variant-numeric: tabular-nums; }
.legend-amount { color: var(--text-secondary); font-variant-numeric: tabular-nums; }

/* ---------- Login page ---------- */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background:
    radial-gradient(560px circle at 50% 0%, color-mix(in srgb, var(--accent) 10%, transparent), transparent 70%),
    var(--page);
}

.login-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow-md);
  padding: 40px 36px;
  width: 100%;
  max-width: 340px;
  text-align: center;
}

.login-icon {
  width: 56px;
  height: 56px;
  margin: 0 auto 14px;
  border-radius: 16px;
  background: color-mix(in srgb, var(--accent) 14%, var(--surface-1));
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
}

.login-card h1 { font-size: 19px; margin-bottom: 6px; }

.login-card p { margin: 0 0 22px; font-size: 14px; }

.login-card form { text-align: left; }

.login-card label { margin-bottom: 18px; }

/* ---------- Responsive shell ---------- */
@media (max-width: 760px) {
  .app-shell { flex-direction: column; }

  .sidebar {
    width: 100%;
    height: auto;
    position: static;
    flex-direction: row;
    align-items: center;
    padding: 10px 16px;
    gap: 12px;
  }

  .sidebar-brand { padding: 0; }
  .sidebar-nav { flex-direction: row; flex: 1; }
  .nav-logout { margin-top: 0; }

  .page-header { padding: 16px 20px; }
  .container { padding: 20px 20px 36px; }
}
