:root {
    --ink: #2b1858;
    --ink-2: #6d28d9;
    --accent: #7c3aed;
    --accent-2: #6d28d9;
    --accent-soft: #f2ebfe;
    --grad: linear-gradient(135deg, #8b5cf6 0%, #d6409f 55%, #f97316 120%);
    --grad-btn: linear-gradient(180deg, #8b5cf6, #7c3aed);
    --bg: #f6f3fc;
    --panel: #ffffff;
    --line: #ece6f6;
    --line-strong: #ddd2ef;
    --muted: #7a7196;
    --text: #271f3d;
    --surface-2: #f7f2fd;
    --topbar-bg: rgba(255,255,255,.9);
    --input-bg: #ffffff;
    --ok: #0e9f6e;
    --ok-bg: #e3f8ef;
    --err: #e11d48;
    --err-bg: #fdeaef;
    --warn: #d97706;
    --warn-bg: #fdf2e1;
    --purple: #9333ea;
    --purple-bg: #f6e9fd;
    --teal: #0891b2;
    --teal-bg: #e1f5fb;
    --pink: #db2777;
    --pink-bg: #fde7f1;
    --radius: 14px;
    --radius-sm: 9px;
    --shadow-sm: 0 1px 2px rgba(76, 29, 149, .07);
    --shadow: 0 2px 8px rgba(76, 29, 149, .09), 0 1px 2px rgba(76, 29, 149, .06);
    --shadow-lg: 0 16px 36px rgba(76, 29, 149, .20);
    --sidebar-w: 248px;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
    margin: 0;
    font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
}

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

::selection { background: rgba(59, 111, 224, .18); }

/* Scrollbars */
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-thumb { background: rgba(120, 140, 170, .4); border-radius: 20px; border: 2px solid transparent; background-clip: content-box; }
*::-webkit-scrollbar-thumb:hover { background: rgba(120, 140, 170, .65); background-clip: content-box; }

/* ======================= Layout ======================= */
.layout { display: flex; min-height: 100vh; }

.sidebar {
    width: var(--sidebar-w);
    background: linear-gradient(185deg, #5b21b6 0%, #4c1d95 55%, #2e1065 100%);
    color: #e4d8f7;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    flex-shrink: 0;
    padding-bottom: 24px;
    box-shadow: inset -1px 0 0 rgba(255,255,255,.04);
}
.sidebar::-webkit-scrollbar-thumb { background: rgba(255,255,255,.18); background-clip: content-box; }

.brand {
    display: flex; align-items: center; gap: 12px;
    padding: 20px 20px 16px;
    position: sticky; top: 0;
    background: #5b21b6;
    z-index: 2;
    border-bottom: 1px solid rgba(255,255,255,.08);
}
.brand-mark {
    width: 38px; height: 38px; border-radius: 11px;
    background: linear-gradient(135deg, #c084fc, #e0409f);
    display: flex; align-items: center; justify-content: center;
    color: #fff; box-shadow: 0 4px 14px rgba(224, 64, 159, .55);
    flex-shrink: 0;
}
.brand-mark svg { width: 20px; height: 20px; }
.brand-name { font-weight: 700; font-size: 18px; color: #fff; letter-spacing: .3px; line-height: 1.1; }
.brand-sub { font-size: 10.5px; color: #c4abed; text-transform: uppercase; letter-spacing: .08em; margin-top: 2px; }

.nav { padding: 10px 12px 0; }
.nav-group { padding: 16px 10px 6px; font-size: 10.5px; text-transform: uppercase; letter-spacing: .1em; color: #a885db; font-weight: 600; }
.nav-link {
    display: flex; align-items: center; gap: 11px;
    padding: 9px 11px; margin: 1px 0;
    color: #e4d8f7; font-size: 13.5px; font-weight: 500;
    border-radius: 9px;
    transition: background .15s, color .15s, transform .05s;
    position: relative;
}
.nav-link svg { width: 18px; height: 18px; flex-shrink: 0; opacity: .85; }
.nav-link:hover { background: rgba(255,255,255,.1); color: #fff; }
.nav-link.active { background: linear-gradient(90deg, rgba(224,64,159,.30), rgba(192,132,252,.14)); color: #fff; font-weight: 600; }
.nav-link.active svg { opacity: 1; color: #f0abfc; }
.nav-link.active::before {
    content: ""; position: absolute; left: -12px; top: 6px; bottom: 6px;
    width: 3px; border-radius: 0 3px 3px 0; background: linear-gradient(180deg, #e879f9, #f97316);
}

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

.topbar {
    display: flex; align-items: center; justify-content: space-between;
    background: var(--topbar-bg);
    backdrop-filter: saturate(180%) blur(8px);
    padding: 13px 26px; border-bottom: 1px solid var(--line);
    position: sticky; top: 0; z-index: 10;
}
.topbar-title { font-size: 18px; font-weight: 700; color: var(--ink); letter-spacing: -.01em; }
.topbar-user { display: flex; align-items: center; gap: 12px; }
.avatar {
    width: 36px; height: 36px; border-radius: 50%;
    background: linear-gradient(135deg, #8b5cf6, #db2777);
    color: #fff; font-weight: 700; font-size: 13px;
    display: flex; align-items: center; justify-content: center;
    box-shadow: 0 3px 10px rgba(219,39,119,.35);
}
.user-meta { display: flex; flex-direction: column; line-height: 1.2; text-align: right; }
.user-name { font-weight: 600; font-size: 13.5px; color: var(--ink); }
.user-role { font-size: 10.5px; color: var(--accent-2); background: var(--accent-soft); padding: 1px 8px; border-radius: 20px; align-self: flex-end; margin-top: 2px; font-weight: 600; }

.content { padding: 26px; max-width: 1280px; width: 100%; animation: fadeUp .35s ease; }
@keyframes fadeUp { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ======================= Headings ======================= */
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 21px; color: var(--ink); font-weight: 700; letter-spacing: -.01em; }
.subtle { color: var(--muted); font-size: 13px; font-weight: 400; }

/* ======================= Panels ======================= */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.panel-pad { padding: 20px 22px; }
.panel-head { padding: 15px 22px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; gap: 12px; background: linear-gradient(180deg, var(--surface-2), var(--panel)); }
.panel-head h2 { margin: 0; font-size: 15px; color: var(--ink); font-weight: 650; }

/* ======================= Stat tiles ======================= */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 16px; margin-bottom: 22px; }
.stat {
    background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
    padding: 18px; box-shadow: var(--shadow);
    display: flex; align-items: flex-start; gap: 14px;
    transition: transform .15s, box-shadow .15s;
    position: relative; overflow: hidden;
}
.stat:hover { transform: translateY(-3px); box-shadow: var(--shadow-lg); }
.stat-ico { width: 42px; height: 42px; border-radius: 11px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; background: var(--accent-soft); color: var(--accent); }
.stat-ico svg { width: 21px; height: 21px; }
.stat-ico.green { background: var(--ok-bg); color: var(--ok); }
.stat-ico.red { background: var(--err-bg); color: var(--err); }
.stat-ico.amber { background: var(--warn-bg); color: var(--warn); }
.stat-ico.purple { background: var(--purple-bg); color: var(--purple); }
.stat-ico.teal { background: var(--teal-bg); color: var(--teal); }
.stat-body { min-width: 0; }
.stat .label { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.stat .value { font-size: 29px; font-weight: 750; color: var(--ink); margin-top: 2px; line-height: 1.1; letter-spacing: -.02em; }
.stat .value.ok { color: var(--ok); }
.stat .value.err { color: var(--err); }
.stat .foot { font-size: 12px; color: var(--muted); margin-top: 3px; }

/* ======================= Grid helpers ======================= */
.grid-2 { display: grid; grid-template-columns: 1.5fr 1fr; gap: 18px; align-items: start; }
.mt { margin-top: 18px; }
.mb { margin-bottom: 18px; }

/* ======================= Tables ======================= */
.table-wrap { overflow-x: auto; }
table.grid { width: 100%; border-collapse: collapse; font-size: 13px; }
table.grid th, table.grid td { padding: 11px 14px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.grid th { background: var(--surface-2); color: var(--ink-2); font-weight: 650; font-size: 11px; text-transform: uppercase; letter-spacing: .035em; position: sticky; top: 0; }
table.grid tbody tr { transition: background .12s; }
table.grid tbody tr:hover { background: var(--accent-soft); }
table.grid tbody tr:last-child td { border-bottom: none; }
table.grid td.wrap, table.grid th.wrap { white-space: normal; }
.muster td, .muster th { padding: 6px 7px; text-align: center; font-size: 11px; }
.muster tbody tr:hover { background: var(--accent-soft); }

/* ======================= Badges ======================= */
.badge { display: inline-block; padding: 2px 10px; border-radius: 20px; font-size: 11.5px; font-weight: 600; line-height: 1.6; }
.badge.green { background: var(--ok-bg); color: var(--ok); }
.badge.red { background: var(--err-bg); color: var(--err); }
.badge.amber { background: var(--warn-bg); color: var(--warn); }
.badge.grey { background: var(--surface-2); color: var(--muted); }
.badge.blue { background: var(--accent-soft); color: var(--accent-2); }
.mark-P { color: var(--ok); font-weight: 700; }
.mark-HD { color: var(--warn); font-weight: 700; }
.mark-A { color: var(--err); font-weight: 700; }
.mark-WO { color: var(--muted); }
.mark-PH { color: var(--accent); font-weight: 600; }

/* ======================= Buttons ======================= */
.btn {
    display: inline-flex; align-items: center; gap: 7px; justify-content: center;
    border: 1px solid transparent; border-radius: var(--radius-sm);
    padding: 8px 15px; font-size: 13.5px; font-weight: 600; cursor: pointer;
    background: linear-gradient(135deg, #8b5cf6, #d6409f); color: #fff;
    box-shadow: 0 2px 8px rgba(139,92,246,.38); transition: transform .08s, box-shadow .15s, filter .15s;
    font-family: inherit; text-decoration: none; line-height: 1.4;
}
.btn:hover { filter: brightness(1.07) saturate(1.05); box-shadow: 0 5px 16px rgba(214,64,159,.45); color: #fff; }
.btn:active { transform: translateY(1px); }
.btn-sm { padding: 5px 11px; font-size: 12.5px; }
.btn-ghost { background: var(--panel); color: var(--ink-2); border-color: var(--line-strong); box-shadow: var(--shadow-sm); }
.btn-ghost:hover { background: var(--surface-2); color: var(--ink); border-color: var(--accent); }
.btn-danger { background: linear-gradient(135deg, #fb7185, #e11d48); box-shadow: 0 2px 8px rgba(225,29,72,.35); }
.btn-danger:hover { filter: brightness(1.06); }
.btn-ok { background: linear-gradient(135deg, #34d399, #0e9f6e); box-shadow: 0 2px 8px rgba(14,159,110,.35); }
.btn-ok:hover { filter: brightness(1.06); }
.inline { display: inline; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ======================= Forms ======================= */
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px 20px; }
.form-grid .full { grid-column: 1 / -1; }
.field label { display: block; font-size: 12.5px; font-weight: 600; color: var(--ink-2); margin-bottom: 5px; }
.field input[type=text], .field input[type=password], .field input[type=number],
.field input[type=date], .field input[type=datetime-local], .field input[type=time],
.field select, .field textarea, input.ctl, select.ctl {
    width: 100%; padding: 9px 11px; border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
    font-size: 13.5px; background: var(--input-bg); font-family: inherit; color: var(--text); transition: border-color .15s, box-shadow .15s;
}
.field input:focus, .field select:focus, .field textarea:focus, .ctl:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(59,111,224,.15); }
.check { display: flex; align-items: center; gap: 8px; font-size: 13.5px; cursor: pointer; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }
.form-actions { margin-top: 20px; display: flex; gap: 10px; align-items: center; }
.filters { display: flex; gap: 12px; flex-wrap: wrap; align-items: end; margin-bottom: 18px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow-sm); }
.filters .field { min-width: 150px; margin: 0; }
.filters .btn, .filters .btn-ghost { margin-bottom: 1px; }

/* ======================= Alerts ======================= */
.alert { padding: 12px 16px; border-radius: var(--radius-sm); margin-bottom: 18px; font-size: 13.5px; border: 1px solid transparent; display: flex; align-items: center; gap: 9px; animation: fadeUp .3s ease; }
.alert::before { font-weight: 700; }
.alert-ok { background: var(--ok-bg); color: var(--ok); border-color: #bfe6d3; }
.alert-ok::before { content: "✓"; }
.alert-err { background: var(--err-bg); color: var(--err); border-color: #f1c9c6; }
.alert-err::before { content: "!"; }
.validation { color: var(--err); font-size: 12.5px; }
.field .validation { margin-top: 3px; }

/* ======================= Login ======================= */
.login-wrap {
    min-height: 100vh; display: flex; align-items: center; justify-content: center;
    background: radial-gradient(1000px 500px at 15% -10%, rgba(249,115,22,.35) 0, transparent 55%), radial-gradient(900px 500px at 90% 10%, rgba(219,39,119,.4) 0, transparent 55%), linear-gradient(135deg, #4c1d95 0%, #7c3aed 50%, #db2777 120%);
    padding: 20px; position: relative; overflow: hidden;
}
.login-wrap::before { content: ""; position: absolute; inset: 0; background-image: radial-gradient(rgba(255,255,255,.06) 1px, transparent 1px); background-size: 22px 22px; opacity: .6; }
.login-card { background: var(--panel); width: 100%; max-width: 400px; border-radius: 18px; box-shadow: 0 24px 60px rgba(0,0,0,.35); padding: 34px 32px; position: relative; z-index: 1; animation: fadeUp .4s ease; }
.login-logo { width: 54px; height: 54px; border-radius: 15px; background: linear-gradient(135deg, #a855f7, #db2777); display: flex; align-items: center; justify-content: center; margin-bottom: 16px; box-shadow: 0 8px 22px rgba(219,39,119,.5); }
.login-logo svg { width: 28px; height: 28px; color: #fff; }
.login-card h1 { margin: 0 0 4px; font-size: 23px; color: var(--ink); font-weight: 750; }
.login-card .sub { color: var(--muted); font-size: 13px; margin-bottom: 24px; }
.login-card .field { margin-bottom: 15px; }
.login-card .btn { width: 100%; padding: 11px; font-size: 14.5px; margin-top: 4px; }
.login-roles { margin-top: 20px; border-top: 1px dashed var(--line-strong); padding-top: 16px; }
.login-roles .lbl { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 9px; font-weight: 600; }
.role-chips { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.role-chip { border: 1px solid var(--line-strong); background: var(--surface-2); border-radius: 9px; padding: 8px 10px; font-size: 12.5px; cursor: pointer; text-align: left; transition: border-color .15s, background .15s, transform .06s; font-family: inherit; }
.role-chip:hover { border-color: var(--accent); background: var(--accent-soft); transform: translateY(-1px); }
.role-chip b { display: block; color: var(--ink); font-size: 12.5px; }
.role-chip span { color: var(--muted); font-size: 11px; }

/* ======================= Misc ======================= */
.empty { text-align: center; color: var(--muted); padding: 34px; }
.pill-legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin-top: 12px; }
.device-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 7px; box-shadow: 0 0 0 3px rgba(0,0,0,.04); }
.dot-online { background: var(--ok); box-shadow: 0 0 0 3px rgba(21,138,95,.18); }
.dot-offline { background: #aab4c2; }
.dot-error { background: var(--err); box-shadow: 0 0 0 3px rgba(192,48,39,.18); }
.row-split { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.row-split:last-child { border-bottom: none; }
.perm-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 9px 18px; }
.perm-group-title { grid-column: 1/-1; font-weight: 700; color: var(--ink-2); margin-top: 10px; font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; }

/* Live auto-refresh indicator */
.live-pill { display: inline-flex; align-items: center; gap: 7px; margin-left: auto; font-size: 12px; color: var(--muted); font-weight: 600; padding: 6px 10px; border-radius: 20px; background: var(--surface-2); transition: background .3s; }
.live-pill.flash { background: var(--ok-bg); color: var(--ok); }
.live-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); box-shadow: 0 0 0 3px rgba(21,138,95,.18); animation: livePulse 1.6s ease-in-out infinite; }
@keyframes livePulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }

/* Mini bar for section counts */
.minibar { height: 7px; background: var(--surface-2); border-radius: 20px; overflow: hidden; margin-top: 5px; }
.minibar > span { display: block; height: 100%; background: linear-gradient(90deg, #8b5cf6, #d6409f); border-radius: 20px; }

/* ======================= Dark theme ======================= */
.theme-toggle { padding: 7px 9px; }
.theme-toggle svg { width: 16px; height: 16px; display: block; }

:root[data-theme="dark"] {
    --ink: #f1e9fb;
    --ink-2: #c9b3ef;
    --accent: #a78bfa;
    --accent-2: #8b5cf6;
    --accent-soft: rgba(167, 139, 250, .18);
    --bg: #150f24;
    --panel: #201834;
    --line: #322748;
    --line-strong: #43345e;
    --muted: #a294bf;
    --text: #ece6f6;
    --surface-2: #281e40;
    --input-bg: #19122b;
    --topbar-bg: rgba(27, 18, 46, .9);
    --ok: #34d399; --ok-bg: rgba(14, 159, 110, .22);
    --err: #fb7185; --err-bg: rgba(225, 29, 72, .22);
    --warn: #fbbf24; --warn-bg: rgba(217, 119, 6, .24);
    --purple: #c084fc; --purple-bg: rgba(147, 51, 234, .24);
    --teal: #22d3ee; --teal-bg: rgba(8, 145, 178, .22);
    --pink: #f472b6; --pink-bg: rgba(219, 39, 119, .22);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .35);
    --shadow: 0 2px 8px rgba(0, 0, 0, .4);
    --shadow-lg: 0 16px 36px rgba(0, 0, 0, .55);
}
:root[data-theme="dark"] body { background: var(--bg); }
:root[data-theme="dark"] .badge.blue { background: rgba(167,139,250,.22); color: #c9b3ef; }
:root[data-theme="dark"] .btn-ghost { color: var(--ink-2); }
:root[data-theme="dark"] .sidebar { background: linear-gradient(185deg, #3b1a6e 0%, #2a1250 55%, #1b0f33 100%); }
:root[data-theme="dark"] .brand { background: #3b1a6e; }
:root[data-theme="dark"] .login-card { box-shadow: 0 24px 60px rgba(0,0,0,.6); }

/* ======================= Responsive ======================= */
@media (max-width: 980px) {
    .grid-2 { grid-template-columns: 1fr; }
    .sidebar { width: 68px; }
    .brand-name, .brand-sub, .nav-group { display: none; }
    .brand { justify-content: center; padding: 18px 0; }
    .nav-link { justify-content: center; padding: 11px 0; }
    .nav-link span { display: none; }
    .nav-link.active::before { left: 0; }
    .user-meta { display: none; }
    .content { padding: 18px 16px; }
}
