:root {
  color-scheme: light dark;
  --bg: #f4f6fb;
  --bg-elevated: #ffffff;
  --bg-subtle: #f6f8fc;
  --sidebar-bg: #ffffff;
  --fg: #171b23;
  --fg-muted: #6c7484;
  --border: #edf0f5;
  --border-strong: #dde1ea;
  --accent: #4f6ef7;
  --accent-hover: #3d5ce0;
  --accent-fg: #ffffff;
  --accent-soft: #eef1ff;
  --success: #12946a;
  --success-bg: #e3f7ef;
  --warning: #a15c00;
  --warning-bg: #fdf1dd;
  --danger: #c02b3c;
  --danger-bg: #fde9ec;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(23, 27, 35, 0.05);
  --shadow-md: 0 12px 28px rgba(23, 27, 35, 0.08);
  --sidebar-w: 248px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d0f14;
    --bg-elevated: #151821;
    --bg-subtle: #1a1e29;
    --sidebar-bg: #12141c;
    --fg: #e9ebf1;
    --fg-muted: #8991a3;
    --border: #262b38;
    --border-strong: #333a4a;
    --accent: #7c93ff;
    --accent-hover: #94a7ff;
    --accent-fg: #0d0f14;
    --accent-soft: #1c2340;
    --success: #4fd8a3;
    --success-bg: #0f2e24;
    --warning: #f2b84f;
    --warning-bg: #34260a;
    --danger: #ff8a94;
    --danger-bg: #3a151b;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
    --shadow-md: 0 16px 36px rgba(0, 0, 0, 0.5);
  }
}
:root[data-theme="dark"] {
  --bg: #0d0f14;
  --bg-elevated: #151821;
  --bg-subtle: #1a1e29;
  --sidebar-bg: #12141c;
  --fg: #e9ebf1;
  --fg-muted: #8991a3;
  --border: #262b38;
  --border-strong: #333a4a;
  --accent: #7c93ff;
  --accent-hover: #94a7ff;
  --accent-fg: #0d0f14;
  --accent-soft: #1c2340;
  --success: #4fd8a3;
  --success-bg: #0f2e24;
  --warning: #f2b84f;
  --warning-bg: #34260a;
  --danger: #ff8a94;
  --danger-bg: #3a151b;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 16px 36px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }
html { -webkit-font-smoothing: antialiased; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: var(--fg);
  background: var(--bg);
}

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

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

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  padding: 20px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.sidebar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 10px 20px;
  font-weight: 800;
  font-size: 16px;
  color: var(--fg);
}
.sidebar .brand:hover { text-decoration: none; }
.brand-mark-icon {
  width: 30px; height: 30px; border-radius: 9px;
  background: linear-gradient(135deg, var(--accent), var(--accent-hover));
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 800; font-size: 14px; flex-shrink: 0;
}

.nav-section-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--fg-muted);
  padding: 8px 10px 6px;
}

.nav-list { display: flex; flex-direction: column; gap: 2px; margin-bottom: 18px; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  color: var(--fg-muted);
  font-weight: 600;
  font-size: 13.5px;
}
.nav-item:hover { background: var(--bg-subtle); color: var(--fg); text-decoration: none; }
.nav-item.active { background: var(--accent-soft); color: var(--accent); }
.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }

/* ---- Top header ---- */
.topbar {
  height: 64px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 24px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar .spacer { flex: 1; }
.icon-btn {
  width: 36px; height: 36px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  color: var(--fg-muted);
  cursor: pointer;
}
.icon-btn:hover { background: var(--bg-subtle); color: var(--fg); }
.icon-btn svg { width: 17px; height: 17px; flex-shrink: 0; }

.avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--accent-soft); color: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 13px; flex-shrink: 0;
}
.user-chip { display: flex; align-items: center; gap: 10px; }
.user-chip .who { display: flex; flex-direction: column; line-height: 1.25; }
.user-chip .who .email { font-weight: 700; font-size: 12.5px; color: var(--fg); }
.user-chip .who .role { font-size: 11.5px; color: var(--fg-muted); text-transform: capitalize; }

.content { flex: 1; min-width: 0; }
main { max-width: 1120px; margin: 0 auto; padding: 28px 28px 80px; }

/* ================= Page header ================= */
.page-header { display: flex; align-items: baseline; justify-content: space-between; gap: 16px; margin-bottom: 22px; }
h1 { font-size: 21px; font-weight: 800; margin: 0 0 4px; letter-spacing: -0.01em; }
h2 { font-size: 13px; font-weight: 700; margin: 0 0 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--fg-muted); }
.page-subtitle, .hint { color: var(--fg-muted); font-size: 13.5px; margin: 0 0 20px; max-width: 70ch; }

/* ================= Stat tiles ================= */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-bottom: 28px; }
.stat-tile {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 18px 20px;
}
.stat-tile .stat-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.stat-tile .stat-icon {
  width: 38px; height: 38px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  background: var(--accent-soft); color: var(--accent);
}
.stat-tile .stat-icon svg { width: 19px; height: 19px; }
.stat-tile .stat-label { color: var(--fg-muted); font-size: 12.5px; font-weight: 600; margin-bottom: 6px; }
.stat-tile .stat-value { font-size: 26px; font-weight: 800; letter-spacing: -0.02em; }

/* ================= Card ================= */
.card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 22px 24px;
  margin-bottom: 24px;
}
.card + h2 { margin-top: 8px; }

/* ================= Table ================= */
.table-wrap {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  margin-bottom: 24px;
}
table { width: 100%; border-collapse: collapse; }
thead th {
  text-align: left;
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--fg-muted);
  background: var(--bg-subtle);
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
tbody td { padding: 13px 18px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--bg-subtle); }
td.empty-state { color: var(--fg-muted); text-align: center; padding: 36px 16px; }

/* ================= Forms ================= */
form { display: flex; flex-direction: column; gap: 14px; max-width: 460px; }
form.form-wide { max-width: 100%; }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 640px) { .field-grid { grid-template-columns: 1fr; } }

label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; font-weight: 600; color: var(--fg); }
label .field-note { font-weight: 400; color: var(--fg-muted); font-size: 12px; }

input, select, textarea {
  font: inherit;
  padding: 9px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  color: var(--fg);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}
input::placeholder { color: var(--fg-muted); }

fieldset {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin: 0;
}
legend { font-size: 12.5px; font-weight: 700; color: var(--fg-muted); padding: 0 4px; }

.checkbox-row { flex-direction: row; align-items: center; gap: 8px; font-weight: 500; }
.checkbox-row input { width: 16px; height: 16px; }

/* ================= Buttons ================= */
button, .btn {
  font: inherit;
  font-weight: 700;
  font-size: 13.5px;
  padding: 9px 18px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  width: fit-content;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background 0.15s ease, border-color 0.15s ease;
}
button[type="submit"], .btn-primary { background: var(--accent); color: var(--accent-fg); }
button[type="submit"]:hover, .btn-primary:hover { background: var(--accent-hover); }

.btn-secondary { background: var(--bg-elevated); color: var(--fg); border-color: var(--border-strong); }
.btn-secondary:hover { background: var(--bg-subtle); text-decoration: none; }

.btn-sm { padding: 5px 11px; font-size: 12.5px; }

button.btn-danger, .btn-danger { background: var(--danger-bg); color: var(--danger); }
button.btn-danger:hover, .btn-danger:hover { filter: brightness(0.95); }

.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.row-actions a {
  font-weight: 700; font-size: 12.5px; padding: 5px 11px;
  border-radius: var(--radius-sm); border: 1px solid var(--border-strong);
  color: var(--fg); background: var(--bg-elevated);
}
.row-actions a:hover { background: var(--bg-subtle); text-decoration: none; }

/* ================= Badges ================= */
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 12px; font-weight: 700;
  padding: 3px 10px; border-radius: 999px; white-space: nowrap;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge-success { background: var(--success-bg); color: var(--success); }
.badge-warning { background: var(--warning-bg); color: var(--warning); }
.badge-danger  { background: var(--danger-bg); color: var(--danger); }
.badge-neutral { background: var(--bg-subtle); color: var(--fg-muted); }

.ok, .warn, .error { font-weight: 700; }
.ok { color: var(--success); }
.warn { color: var(--warning); }
.error { color: var(--danger); }
p.error { background: var(--danger-bg); color: var(--danger); padding: 10px 14px; border-radius: var(--radius-sm); font-weight: 500; }

/* ================= Dashboard quick links ================= */
.quick-links { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.quick-link {
  display: block;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 18px;
  color: var(--fg);
}
.quick-link:hover { border-color: var(--accent); text-decoration: none; box-shadow: var(--shadow-md); }
.quick-link .ql-title { font-weight: 700; font-size: 14.5px; margin-bottom: 4px; }
.quick-link .ql-desc { color: var(--fg-muted); font-size: 12.5px; }

/* ================= Login ================= */
.auth-shell { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; background: var(--bg); }
.auth-card {
  width: 100%; max-width: 380px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 34px 30px;
}
.auth-card .brand-mark { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 15px; margin-bottom: 22px; }
.auth-card h1 { margin-bottom: 22px; }
.auth-card form { max-width: none; }

/* ================= Misc ================= */
.section-desc { color: var(--fg-muted); font-size: 13px; margin: 4px 0 16px; }
.stack { display: flex; flex-direction: column; gap: 24px; }

@media (max-width: 860px) {
  .sidebar { display: none; }
  main { padding: 20px 16px 60px; }
}
