:root {
  --blue: #2440E8; --blue-hover: #1B31C4; --blue-soft: #E8EDFF;
  --green: #0A7A53; --green-soft: #E3F5EC;
  --red: #B91C1C;   --red-soft: #FDECEC;
  --amber: #A15C00; --amber-soft: #FFF3DC;
  --bg: #F5F7FB; --surface: #FFFFFF; --surface-2: #F1F4FA;
  --border: rgba(11,15,30,.12); --border-soft: rgba(11,15,30,.07);
  --text-1: #0B0F1E; --text-2: #5A6689;
  --shadow: 0 10px 40px rgba(11,15,30,.10);
  --r: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --blue: #6183FF; --blue-hover: #94ADFF; --blue-soft: rgba(97,131,255,.16);
    --green: #4ADE9B; --green-soft: rgba(74,222,155,.13);
    --red: #F87171;   --red-soft: rgba(248,113,113,.14);
    --amber: #FBBF5B; --amber-soft: rgba(251,191,91,.14);
    --bg: #0A0E1C; --surface: #121831; --surface-2: #19203D;
    --border: rgba(255,255,255,.12); --border-soft: rgba(255,255,255,.07);
    --text-1: #EEF1FF; --text-2: #9AA4C4;
    --shadow: 0 10px 40px rgba(0,0,0,.45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text-1);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; letter-spacing: -.01em; }
h2 { font-size: 22px; }
h3 { font-size: 15px; }
.muted { color: var(--text-2); }
.small { font-size: 12px; }
a { color: var(--blue); }

/* Buttons & inputs */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px; border-radius: 9px; border: 1px solid transparent;
  font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s, color .15s;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--blue); color: #fff; }
@media (prefers-color-scheme: dark) { .btn-primary { color: #0A0E1C; } }
.btn-primary:hover:not(:disabled) { background: var(--blue-hover); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text-1); }
.btn-ghost:hover:not(:disabled) { border-color: var(--blue); color: var(--blue); }
.btn-danger { background: var(--red-soft); color: var(--red); border-color: transparent; }
.btn-danger:hover:not(:disabled) { background: var(--red); color: #fff; }
.btn-block { width: 100%; }
.icon-btn { background: none; border: 0; color: var(--text-2); font-size: 16px; cursor: pointer; width: 32px; height: 32px; border-radius: 8px; }
.icon-btn:hover { background: var(--surface-2); color: var(--text-1); }

input, select, textarea {
  font: inherit; color: var(--text-1); background: var(--surface);
  border: 1px solid var(--border); border-radius: 9px; height: 36px; padding: 0 11px; min-width: 0;
}
textarea { height: auto; padding: 8px 11px; }
input:focus, select:focus, textarea:focus, .btn:focus-visible, .nav-item:focus-visible {
  outline: 2px solid var(--blue); outline-offset: 1px;
}
label { display: flex; flex-direction: column; gap: 5px; font-weight: 600; font-size: 13px; }
.form-error { color: var(--red); min-height: 1.2em; margin: 4px 0 0; font-size: 13px; }

/* Brand */
.brand { display: flex; align-items: center; gap: 9px; font-weight: 800; font-size: 17px; }
.brand-logo { width: 26px; height: 26px; border-radius: 7px; object-fit: cover; }
.brand-tag {
  font-size: 11px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  color: var(--blue); background: var(--blue-soft); padding: 2px 7px; border-radius: 999px;
}

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border-soft);
  border-radius: 16px; padding: 28px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px;
}
.login-card h1 { font-size: 22px; margin-top: 8px; }
.login-card .muted { margin: -8px 0 4px; }
.back-link { text-align: center; font-size: 13px; color: var(--text-2); text-decoration: none; }
.back-link:hover { color: var(--blue); }

/* Shell */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 22px;
  padding: 20px 14px; background: var(--surface); border-right: 1px solid var(--border-soft);
}
.sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  text-align: left; background: none; border: 0; font: inherit; font-weight: 600; color: var(--text-2);
  padding: 9px 12px; border-radius: 9px; cursor: pointer;
}
.nav-item:hover { background: var(--surface-2); color: var(--text-1); }
.nav-item.active { background: var(--blue-soft); color: var(--blue); }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 4px; }
.sidebar-foot .btn { margin-top: 10px; }
.me-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; }
.content { padding: 28px 32px 60px; min-width: 0; }
.view { display: flex; flex-direction: column; gap: 18px; max-width: 1240px; }
.view-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }

/* Stats */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.stat {
  background: var(--surface); border: 1px solid var(--border-soft); border-radius: var(--r); padding: 16px;
}
.stat-label { color: var(--text-2); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.stat-value { font-size: 26px; font-weight: 800; margin-top: 4px; font-variant-numeric: tabular-nums; }
.stat-sub { color: var(--text-2); font-size: 12px; margin-top: 2px; }
.panel { background: var(--surface); border: 1px solid var(--border-soft); border-radius: var(--r); padding: 18px; }
.panel h3 { margin-bottom: 14px; }
.bar-chart { display: flex; align-items: flex-end; gap: 3px; height: 140px; }
.bar { flex: 1; background: var(--blue); border-radius: 3px 3px 0 0; min-height: 2px; opacity: .85; }
.bar:hover { opacity: 1; }
.bar.zero { background: var(--border); }

/* Filters & tables */
.filters { display: flex; gap: 8px; flex-wrap: wrap; }
.filters input[type=search] { flex: 1 1 280px; }
.table-wrap { background: var(--surface); border: 1px solid var(--border-soft); border-radius: var(--r); overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th, .table td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border-soft); white-space: nowrap; }
.table th { color: var(--text-2); font-weight: 600; font-size: 12px; background: var(--surface-2); position: sticky; top: 0; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr.clickable { cursor: pointer; }
.table tbody tr.clickable:hover { background: var(--surface-2); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table .wrap { white-space: normal; max-width: 360px; word-break: break-word; }
.table .empty { text-align: center; color: var(--text-2); padding: 36px; }
.cell-main { font-weight: 600; }
.cell-sub { color: var(--text-2); font-size: 12px; }
.pager { display: flex; align-items: center; justify-content: flex-end; gap: 8px; color: var(--text-2); font-size: 13px; }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 700;
  background: var(--surface-2); color: var(--text-2); margin-right: 3px;
}
.badge.green { background: var(--green-soft); color: var(--green); }
.badge.red { background: var(--red-soft); color: var(--red); }
.badge.amber { background: var(--amber-soft); color: var(--amber); }
.badge.blue { background: var(--blue-soft); color: var(--blue); }

/* Dialogs */
dialog { border: 0; padding: 0; color: var(--text-1); background: var(--surface); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(5,8,20,.5); }
.drawer {
  margin: 0 0 0 auto; height: 100vh; max-height: 100vh; width: min(520px, 100vw);
  border-left: 1px solid var(--border-soft);
}
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border-soft); position: sticky; top: 0; background: var(--surface); }
.drawer-body { padding: 20px; display: flex; flex-direction: column; gap: 18px; }
.kv { display: grid; grid-template-columns: 130px 1fr; gap: 6px 12px; font-size: 13px; }
.kv dt { color: var(--text-2); }
.kv dd { margin: 0; word-break: break-word; }
.section-title { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--text-2); margin-bottom: 8px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row input { flex: 1 1 120px; }
.mini-stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 8px; }
.mini-stats div { background: var(--surface-2); border-radius: 9px; padding: 8px 10px; font-size: 12px; color: var(--text-2); }
.mini-stats b { display: block; font-size: 17px; color: var(--text-1); }
.activity { list-style: none; margin: 0; padding: 0; font-size: 12px; }
.activity li { padding: 6px 0; border-bottom: 1px solid var(--border-soft); display: flex; justify-content: space-between; gap: 10px; }
.danger-zone { border: 1px solid var(--red-soft); border-radius: var(--r); padding: 14px; }

.modal { width: min(420px, calc(100vw - 32px)); border-radius: 14px; }
.modal form { padding: 22px; display: flex; flex-direction: column; gap: 12px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
#cd-fields { display: flex; flex-direction: column; gap: 10px; }
.secret {
  font: 600 18px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; letter-spacing: .04em;
  background: var(--surface-2); padding: 12px; border-radius: 9px; text-align: center; user-select: all;
}

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translate(-50%, 20px); opacity: 0;
  background: var(--text-1); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-weight: 600;
  transition: opacity .2s, transform .2s; pointer-events: none; z-index: 10; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--red); color: #fff; }

/* Mobile */
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; gap: 10px; padding: 12px 16px; border-right: 0; border-bottom: 1px solid var(--border-soft); }
  .sidebar nav { flex-direction: row; overflow-x: auto; order: 3; width: 100%; }
  .sidebar-foot { margin: 0 0 0 auto; flex-direction: row; align-items: center; }
  .sidebar-foot .me-name, .sidebar-foot .muted { display: none; }
  .sidebar-foot .btn { margin: 0; width: auto; }
  .content { padding: 18px 16px 48px; }
  .kv { grid-template-columns: 110px 1fr; }
}
