:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e3e6eb;
  --text: #16181d;
  --muted: #667085;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-soft: #eef2ff;
  --primary-text: #ffffff;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --danger-soft: #fef2f2;
  --ok: #15803d;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .08);
  --shadow-lg: 0 20px 40px -12px rgba(16, 24, 40, .25);
  --radius: 10px;

  --role-admin-bg: #ede9fe;   --role-admin-fg: #6d28d9;
  --role-manager-bg: #dbeafe; --role-manager-fg: #1d4ed8;
  --role-editor-bg: #dcfce7;  --role-editor-fg: #15803d;
  --role-viewer-bg: #f1f5f9;  --role-viewer-fg: #475569;
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f232c;
  --border: #2a2f3a;
  --text: #e7e9ee;
  --muted: #98a2b3;
  --primary: #818cf8;
  --primary-hover: #a5b4fc;
  --primary-soft: #1e1b4b;
  --primary-text: #0f1115;
  --danger: #f87171;
  --danger-hover: #fca5a5;
  --danger-soft: #2a1414;
  --ok: #4ade80;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  --shadow-lg: 0 20px 40px -12px rgba(0, 0, 0, .7);

  --role-admin-bg: #2e1065;   --role-admin-fg: #c4b5fd;
  --role-manager-bg: #172554; --role-manager-fg: #93c5fd;
  --role-editor-bg: #052e16;  --role-editor-fg: #86efac;
  --role-viewer-bg: #1e293b;  --role-viewer-fg: #cbd5e1;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
code { font: 12px ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.right { text-align: right; }
.spacer { flex: 1; }
[hidden] { display: none !important; }

/* ---------- Form controls ---------- */
label { display: grid; gap: 6px; font-weight: 500; font-size: 13px; }
input, select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  min-height: 36px;
  width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
input:disabled { background: var(--surface-2); color: var(--muted); }
.form-grid { display: grid; gap: 14px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-error { margin: 0; padding: 8px 10px; border-radius: 8px; background: var(--danger-soft); color: var(--danger); font-size: 13px; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

.btn {
  font: inherit; font-weight: 500;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 36px; padding: 0 14px;
  border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
  cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .6; cursor: default; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-hover); border-color: var(--danger-hover); }
.btn-sm { min-height: 30px; padding: 0 10px; font-size: 13px; }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost.danger { color: var(--danger); }
.btn-ghost.danger:hover { background: var(--danger-soft); }
.btn-block { width: 100%; }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 34px; height: 34px; border-radius: 8px;
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
svg.i { width: 18px; height: 18px; flex: none; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 20px; }
.card h3 { font-size: 15px; margin-bottom: 14px; }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 380px; padding: 28px; display: grid; gap: 16px; }
.login-card h1 { font-size: 22px; }
.login-card p { margin: 0; }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; }
.logo {
  display: grid; place-items: center; width: 30px; height: 30px; border-radius: 8px;
  background: var(--primary); color: var(--primary-text); font-size: 14px;
}

/* ---------- App layout ---------- */
.layout { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh;
  background: var(--surface); border-right: 1px solid var(--border);
  padding: 18px 12px; display: flex; flex-direction: column; gap: 22px;
}
.sidebar .brand { padding: 0 8px; }
.nav { display: grid; gap: 2px; }
.nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 8px;
  color: var(--muted); font-weight: 500;
}
.nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav a.active { background: var(--primary-soft); color: var(--primary); }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  height: 60px; padding: 0 24px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 18px; flex: 1; }
.menu-toggle { display: none; }
.view { padding: 24px; display: grid; gap: 20px; align-content: start; max-width: 1200px; width: 100%; }

.user-menu { position: relative; }
.user-btn {
  display: flex; align-items: center; gap: 10px;
  padding: 4px 8px 4px 4px; border-radius: 999px;
  background: transparent; border: 1px solid transparent; color: var(--text); font: inherit; cursor: pointer;
}
.user-btn:hover, .user-btn[aria-expanded="true"] { background: var(--surface); border-color: var(--border); }
.who { display: grid; text-align: left; line-height: 1.2; }
.who small { color: var(--muted); font-size: 12px; }
.avatar {
  display: grid; place-items: center; flex: none;
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--primary-soft); color: var(--primary);
  font-weight: 600; font-size: 12px; letter-spacing: .02em;
}
.dropdown {
  position: absolute; right: 0; top: calc(100% + 6px); width: 220px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-lg); padding: 6px; display: grid;
}
.dropdown .dd-head { padding: 8px 10px; border-bottom: 1px solid var(--border); margin-bottom: 4px; overflow: hidden; text-overflow: ellipsis; }
.dropdown a, .dropdown button {
  display: flex; align-items: center; gap: 10px; text-align: left;
  padding: 8px 10px; border-radius: 6px; border: 0; background: none;
  color: var(--text); font: inherit; cursor: pointer;
}
.dropdown a:hover, .dropdown button:hover { background: var(--surface-2); text-decoration: none; }
.dropdown hr { border: 0; border-top: 1px solid var(--border); margin: 4px 0; }
.dropdown .danger { color: var(--danger); }

/* ---------- Dashboard ---------- */
.welcome h2 { font-size: 22px; margin-bottom: 4px; }
.welcome p { margin: 0; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; }
.stat { padding: 18px 20px; }
.stat .label { color: var(--muted); font-size: 13px; font-weight: 500; }
.stat .value { font-size: 28px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

.bars { display: grid; gap: 14px; }
.bar-row { display: grid; grid-template-columns: 80px 1fr 32px; align-items: center; gap: 12px; }
.bar-track { height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 999px; background: var(--primary); }
.bar-row .n { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }

.list { list-style: none; margin: 0; padding: 0; display: grid; }
.list li { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-top: 1px solid var(--border); }
.list li:first-child { border-top: 0; padding-top: 0; }
.list .meta { flex: 1; min-width: 0; display: grid; line-height: 1.3; }
.list .meta span:last-child { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-foot { margin-top: 14px; display: inline-block; font-weight: 500; }

/* ---------- Users table ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.toolbar input[type="search"] { max-width: 280px; }
.toolbar select { width: auto; }
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 11px 16px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table th { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; background: var(--surface-2); }
.table th[data-sort] { cursor: pointer; user-select: none; }
.table th[data-sort]:hover { color: var(--text); }
.table th .arrow { opacity: 0; margin-left: 4px; }
.table th.sorted .arrow { opacity: 1; }
.table tbody tr:hover { background: var(--surface-2); }
.table tbody tr:last-child td { border-bottom: 0; }
.table td.right, .table th.right { text-align: right; }
.name-cell { display: flex; align-items: center; gap: 10px; font-weight: 500; }
.you { font-size: 11px; color: var(--muted); font-weight: 400; }
.empty { text-align: center; padding: 40px 16px !important; color: var(--muted); }

.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge-admin { background: var(--role-admin-bg); color: var(--role-admin-fg); }
.badge-manager { background: var(--role-manager-bg); color: var(--role-manager-fg); }
.badge-editor { background: var(--role-editor-bg); color: var(--role-editor-fg); }
.badge-viewer { background: var(--role-viewer-bg); color: var(--role-viewer-fg); }
.bar-fill.admin { background: var(--role-admin-fg); }
.bar-fill.manager { background: var(--role-manager-fg); }
.bar-fill.editor { background: var(--role-editor-fg); }
.bar-fill.viewer { background: var(--role-viewer-fg); }

.pager { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; border-top: 1px solid var(--border); color: var(--muted); font-size: 13px; }
.pager .btns { display: flex; gap: 6px; }

/* ---------- Settings ---------- */
.settings { display: grid; grid-template-columns: 200px 1fr; gap: 20px; align-items: start; }
.tabs { display: grid; gap: 2px; }
.tabs a { padding: 8px 12px; border-radius: 8px; color: var(--muted); font-weight: 500; }
.tabs a:hover { background: var(--surface); color: var(--text); text-decoration: none; }
.tabs a.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.panel { padding: 24px; max-width: 560px; }
.panel h3 { font-size: 16px; margin-bottom: 4px; }
.panel > p { margin: 0 0 20px; }
.theme-options { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.theme-opt { position: relative; cursor: pointer; border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; display: grid; gap: 8px; font-weight: 500; }
.theme-opt input { position: absolute; opacity: 0; pointer-events: none; }
.theme-opt:has(input:checked) { border-color: var(--primary); outline: 1px solid var(--primary); }
.theme-swatch { height: 44px; border-radius: 6px; border: 1px solid var(--border); }
.sw-light { background: linear-gradient(135deg, #fff 50%, #eef0f3 50%); }
.sw-dark { background: linear-gradient(135deg, #171a21 50%, #0f1115 50%); }
.sw-system { background: linear-gradient(135deg, #fff 50%, #171a21 50%); }

/* ---------- Modal & toasts ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center; padding: 16px;
  background: rgba(10, 12, 16, .5);
  animation: fade .12s ease-out;
}
.modal { width: 100%; max-width: 460px; box-shadow: var(--shadow-lg); animation: pop .14s ease-out; }
.modal header { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal header h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 20px; display: grid; gap: 14px; }
.modal-body p { margin: 0; }
.modal footer { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); }

#toasts { position: fixed; right: 16px; bottom: 16px; z-index: 60; display: grid; gap: 8px; }
.toast {
  padding: 10px 14px; border-radius: 8px; min-width: 220px; max-width: 360px;
  background: var(--text); color: var(--bg); box-shadow: var(--shadow-lg);
  font-weight: 500; transition: opacity .3s, transform .3s;
}
.toast-error { background: var(--danger); color: #fff; }
.toast.out { opacity: 0; transform: translateY(6px); }
.loading { color: var(--muted); padding: 20px 0; }

@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; z-index: 40; left: 0; top: 0; width: 240px;
    transform: translateX(-100%); transition: transform .2s ease;
    box-shadow: var(--shadow-lg);
  }
  .layout.nav-open .sidebar { transform: none; }
  .menu-toggle { display: inline-grid; }
  .grid-2, .settings { grid-template-columns: 1fr; }
  .tabs { grid-auto-flow: column; justify-content: start; overflow-x: auto; }
}
@media (max-width: 600px) {
  .topbar { padding: 0 16px; }
  .view { padding: 16px; }
  .who { display: none; }
  .form-row { grid-template-columns: 1fr; }
  .toolbar input[type="search"] { max-width: none; flex: 1 1 100%; }
  .theme-options { grid-template-columns: 1fr; }
}

/* ---------- API keys ---------- */
.panel.panel-wide { max-width: none; }
.settings > * { min-width: 0; }
.key-create { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
.key-create input { flex: 1 1 240px; max-width: 360px; }
.key-create .form-error { flex-basis: 100%; }
.key-reveal {
  display: grid; gap: 8px; margin-bottom: 16px; padding: 12px 14px;
  border: 1px solid var(--role-editor-fg); border-radius: 8px; background: var(--role-editor-bg);
}
.key-row { display: flex; align-items: center; gap: 8px; }
.key-row code { flex: 1; min-width: 0; overflow-x: auto; white-space: nowrap; padding: 6px 8px; background: var(--surface); user-select: all; }
.key-table { border: 1px solid var(--border); border-radius: 8px; }

/* ---------- Audit logs ---------- */
.cell-2 { display: grid; line-height: 1.35; min-width: 0; }
.actor { display: flex; align-items: center; gap: 10px; }
.avatar-api { background: var(--surface-2); color: var(--muted); }
.avatar-api svg.i { width: 15px; height: 15px; }
.audit-table td { vertical-align: top; }
.audit-table td.changes { white-space: normal; min-width: 220px; line-height: 1.6; }
.audit-table s { text-decoration-thickness: 1px; }
.badge.act-create { background: var(--role-editor-bg); color: var(--role-editor-fg); }
.badge.act-update { background: var(--role-manager-bg); color: var(--role-manager-fg); }
.badge.act-delete { background: var(--danger-soft); color: var(--danger); }
.badge-api { background: var(--surface-2); color: var(--muted); font-size: 10px; padding: 1px 6px; margin-left: 4px; vertical-align: 1px; }

/* ---------- Workspace (tenant) ---------- */
.workspace {
  display: grid; gap: 1px; margin: -10px 0 -6px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2);
}
.workspace strong { font-size: 13px; }
