:root {
  --bg: #0d1117;
  --bg-2: #151b24;
  --card: #1a212c;
  --card-2: #212a37;
  --line: #2a3441;
  --text: #e6edf3;
  --muted: #8d99a8;
  --accent: #5b8cff;
  --accent-2: #7aa2ff;
  --ok: #3fb950;
  --warn: #d29922;
  --err: #f85149;
  --radius: 12px;
  --shadow: 0 6px 24px rgba(0, 0, 0, .35);
  --sidebar: 240px;
  font-size: 15px;
}
:root[data-theme="light"] {
  --bg: #f4f6fa;
  --bg-2: #eaeef4;
  --card: #ffffff;
  --card-2: #f3f5f9;
  --line: #dde3ea;
  --text: #17202b;
  --muted: #5f6b7a;
  --accent: #3b6df0;
  --accent-2: #2a58d4;
  --shadow: 0 4px 16px rgba(20, 30, 50, .08);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent-2); text-decoration: none; }
h1, h2, h3 { margin: 0 0 12px; line-height: 1.2; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.1rem; }
code { font-family: ui-monospace, 'SF Mono', Consolas, monospace; font-size: .85em; background: var(--card-2); padding: 2px 6px; border-radius: 6px; word-break: break-all; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }

/* ---------- Layout ---------- */
.layout { min-height: 100%; }
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  height: 56px; padding: 0 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; }
.brand-logo { width: 30px; height: 30px; border-radius: 9px; background: linear-gradient(135deg, var(--accent), #9b6bff); display: grid; place-items: center; color: #fff; font-size: 16px; }
.topbar .spacer { flex: 1; }
.burger { display: inline-grid; place-items: center; width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); color: var(--text); cursor: pointer; font-size: 18px; }

.sidebar {
  position: fixed; inset: 56px auto 0 0; z-index: 30;
  width: min(80vw, var(--sidebar));
  padding: 12px;
  background: var(--bg-2);
  border-right: 1px solid var(--line);
  transform: translateX(-100%);
  transition: transform .22s ease;
  overflow-y: auto;
}
.sidebar.open { transform: none; box-shadow: var(--shadow); }
.backdrop { position: fixed; inset: 56px 0 0; background: rgba(0, 0, 0, .45); z-index: 25; display: none; }
.backdrop.show { display: block; }
.nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; margin-bottom: 2px;
  border-radius: 10px; color: var(--muted); font-weight: 500;
}
.nav a:hover { background: var(--card); color: var(--text); }
.nav a.active { background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--text); }
.nav .ico { width: 22px; text-align: center; }
.nav-footer { margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--line); }

.main { padding: 16px; max-width: 1280px; margin: 0 auto; }
.page-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 16px; }
.page-head h1 { margin: 0; flex: 1 1 auto; }

@media (min-width: 960px) {
  .burger { display: none; }
  .sidebar { transform: none; top: 56px; }
  .backdrop { display: none !important; }
  .main { margin-left: var(--sidebar); padding: 24px 28px; }
}

/* ---------- Cards / grid ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.card + .card { margin-top: 16px; }
.grid { display: grid; gap: 12px; }
.stats { grid-template-columns: repeat(2, 1fr); }
@media (min-width: 720px) { .stats { grid-template-columns: repeat(4, 1fr); } }
.cols-2 { grid-template-columns: 1fr; }
@media (min-width: 960px) { .cols-2 { grid-template-columns: 1fr 1fr; } }

.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.stat .label { color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-size: 1.45rem; font-weight: 700; margin-top: 4px; }
.stat .sub { color: var(--muted); font-size: .82rem; margin-top: 2px; }
.stat.accent { background: linear-gradient(135deg, color-mix(in srgb, var(--accent) 30%, var(--card)), var(--card)); }
.stat.danger { border-color: var(--err); }

.tabs { display: inline-flex; background: var(--card-2); border-radius: 10px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.tabs button { border: 0; background: transparent; color: var(--muted); padding: 6px 12px; border-radius: 8px; cursor: pointer; font: inherit; font-size: .9rem; }
.tabs button.active { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.2); }

/* ---------- Chart ---------- */
.chart { display: flex; align-items: flex-end; gap: 4px; height: 160px; padding-top: 8px; }
.chart .bar { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; min-width: 0; height: 100%; }
.chart .bar .col { width: 100%; max-width: 28px; border-radius: 5px 5px 0 0; background: var(--accent); position: relative; min-height: 2px; }
.chart .bar .col .m { position: absolute; left: 0; right: 0; bottom: 0; background: var(--ok); border-radius: 5px 5px 0 0; }
.chart .bar span { font-size: .68rem; color: var(--muted); margin-top: 4px; }
.legend { display: flex; gap: 14px; font-size: .8rem; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

/* ---------- Tables → cards on mobile ---------- */
.table-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; }
.tbl th, .tbl td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
.tbl th { color: var(--muted); font-weight: 500; font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; }
.tbl tbody tr:hover { background: var(--card-2); }
.tbl tr.clickable { cursor: pointer; }
.tbl tr.dim td { opacity: .5; }

@media (max-width: 719px) {
  .tbl thead { display: none; }
  .tbl, .tbl tbody, .tbl tr, .tbl td { display: block; width: 100%; }
  .tbl tr { background: var(--card-2); border: 1px solid var(--line); border-radius: 10px; margin-bottom: 10px; padding: 6px 4px; }
  .tbl td { border: 0; padding: 6px 10px; display: flex; justify-content: space-between; align-items: center; gap: 12px; text-align: right; }
  .tbl td::before { content: attr(data-label); color: var(--muted); font-size: .8rem; text-align: left; flex-shrink: 0; }
  .tbl td[data-label=""]::before { content: none; }
  .tbl td.actions { justify-content: flex-end; flex-wrap: wrap; }
}

/* ---------- Forms ---------- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: .85rem; color: var(--muted); }
.field .hint { font-size: .78rem; color: var(--muted); }
input, select, textarea {
  font: inherit; color: var(--text);
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent); border-color: var(--accent); }
textarea { min-height: 120px; resize: vertical; }
input.sm { padding: 6px 8px; width: 90px; }
.form-row { display: grid; gap: 12px; grid-template-columns: 1fr; }
@media (min-width: 720px) { .form-row { grid-template-columns: repeat(2, 1fr); } .form-row.three { grid-template-columns: repeat(3, 1fr); } }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.toolbar input[type="search"] { flex: 1 1 220px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; font-size: .9rem;
  padding: 9px 14px; border-radius: 10px; border: 1px solid transparent;
  background: var(--accent); color: #fff; cursor: pointer; white-space: nowrap;
}
.btn:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.ghost { background: var(--card-2); color: var(--text); border-color: var(--line); }
.btn.danger { background: var(--err); }
.btn.ok { background: var(--ok); }
.btn.sm { padding: 5px 10px; font-size: .8rem; border-radius: 8px; }
.btn.block { width: 100%; }

.switch { position: relative; display: inline-block; width: 40px; height: 22px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--line); border-radius: 22px; transition: .2s; cursor: pointer; }
.switch span::before { content: ''; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .2s; }
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::before { transform: translateX(18px); }

/* ---------- Badges ---------- */
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .75rem; font-weight: 600; background: var(--card-2); color: var(--muted); white-space: nowrap; }
.badge.ok { background: color-mix(in srgb, var(--ok) 20%, transparent); color: var(--ok); }
.badge.warn { background: color-mix(in srgb, var(--warn) 20%, transparent); color: var(--warn); }
.badge.err { background: color-mix(in srgb, var(--err) 20%, transparent); color: var(--err); }
.badge.info { background: color-mix(in srgb, var(--accent) 20%, transparent); color: var(--accent-2); }
.pos { color: var(--ok); }
.neg { color: var(--err); }

/* ---------- Misc ---------- */
.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 12px; }
.empty { text-align: center; color: var(--muted); padding: 32px 12px; }
.loader { display: grid; place-items: center; padding: 48px; color: var(--muted); }
.loader::before { content: ''; width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.alert { padding: 12px 14px; border-radius: 10px; margin-bottom: 16px; border: 1px solid; }
.alert.err { border-color: var(--err); background: color-mix(in srgb, var(--err) 12%, transparent); }
.alert.warn { border-color: var(--warn); background: color-mix(in srgb, var(--warn) 12%, transparent); }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }

#toasts { position: fixed; right: 16px; bottom: 16px; left: 16px; z-index: 100; display: flex; flex-direction: column; align-items: flex-end; gap: 8px; pointer-events: none; }
.toast { background: var(--card-2); border: 1px solid var(--line); border-left: 4px solid var(--accent); padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); max-width: 420px; pointer-events: auto; animation: slide .2s ease; }
.toast.err { border-left-color: var(--err); }
.toast.ok { border-left-color: var(--ok); }
@keyframes slide { from { transform: translateY(10px); opacity: 0; } }

dialog.modal { border: 1px solid var(--line); border-radius: 14px; background: var(--card); color: var(--text); padding: 0; width: min(520px, calc(100vw - 24px)); box-shadow: var(--shadow); }
dialog.modal::backdrop { background: rgba(0,0,0,.55); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; }
.modal-body { padding: 16px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); }
.icon-btn { background: none; border: 0; color: var(--muted); font-size: 20px; cursor: pointer; }

/* ---------- Login ---------- */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 16px; background: radial-gradient(circle at 30% 20%, color-mix(in srgb, var(--accent) 18%, transparent), transparent 55%), var(--bg); }
.login-card { width: min(380px, 100%); }
.login-card .brand { justify-content: center; margin-bottom: 20px; font-size: 1.2rem; }
.error-text { color: var(--err); font-size: .88rem; min-height: 1.2em; margin: 0 0 10px; }
