:root {
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --sidebar-bg: #1e1b4b;
    --sidebar-text: #c7d2fe;
    --bg: #f1f5f9;
    --card: #ffffff;
    --text: #0f172a;
    --muted: #64748b;
    --border: #e2e8f0;
    --danger: #ef4444;
    --success: #22c55e;
    --warning: #f59e0b;
    --shadow: 0 4px 6px -1px rgba(0,0,0,.1), 0 2px 4px -2px rgba(0,0,0,.1);
    --radius: 12px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
}
a { color: var(--primary); text-decoration: none; }
button, .btn {
    display: inline-flex; align-items: center; justify-content: center;
    gap: 6px; padding: 10px 16px; border: none; border-radius: 8px;
    font-size: 14px; cursor: pointer; transition: .2s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-default { background: #fff; color: var(--text); border: 1px solid var(--border); }
.btn-sm { padding: 6px 12px; font-size: 13px; }
input, select, textarea {
    width: 100%; padding: 10px 12px; border: 1px solid var(--border);
    border-radius: 8px; font-size: 14px; outline: none; transition: .2s;
}
input:focus, select:focus, textarea:focus {
    border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79,70,229,.15);
}
.form-group { margin-bottom: 16px; }
.form-group label { display: block; margin-bottom: 6px; font-weight: 500; font-size: 14px; }
.form-hint { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* Login */
.login-page {
    min-height: 100vh; display: flex; align-items: center; justify-content: center;
    background: linear-gradient(135deg, #312e81 0%, #4f46e5 50%, #7c3aed 100%);
    padding: 20px;
}
.login-card {
    width: 100%; max-width: 420px; background: var(--card);
    border-radius: 16px; box-shadow: 0 25px 50px -12px rgba(0,0,0,.25); padding: 40px 32px;
}
.login-card h1 { font-size: 24px; margin-bottom: 8px; text-align: center; }
.login-card p { color: var(--muted); text-align: center; margin-bottom: 28px; font-size: 14px; }
.login-card .btn { width: 100%; margin-top: 8px; padding: 12px; }

/* Layout */
.admin-layout { display: flex; min-height: 100vh; }
.sidebar {
    width: 260px; background: var(--sidebar-bg); color: var(--sidebar-text);
    flex-shrink: 0; transition: transform .3s; z-index: 1000;
}
.sidebar-header {
    padding: 20px; border-bottom: 1px solid rgba(255,255,255,.08);
    font-size: 18px; font-weight: 700; color: #fff;
}
.sidebar-nav { padding: 12px 0; }
.nav-item > a, .nav-group-title {
    display: flex; align-items: center; gap: 10px; padding: 12px 20px;
    color: var(--sidebar-text); font-size: 14px;
}
.nav-item > a:hover, .nav-item.active > a { background: rgba(255,255,255,.08); color: #fff; }
.nav-item.active > a { border-right: 3px solid #a5b4fc; }
.nav-sub { padding-left: 12px; }
.nav-sub a { padding: 10px 20px 10px 36px; font-size: 13px; }
.nav-group-title { font-size: 12px; text-transform: uppercase; opacity: .6; padding-top: 16px; }
.main-wrap { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
    height: 60px; background: var(--card); border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between; padding: 0 20px;
    position: sticky; top: 0; z-index: 100;
}
.topbar-left { display: flex; align-items: center; gap: 12px; }
.menu-toggle {
    display: none; background: none; border: none; font-size: 22px; cursor: pointer; color: var(--text);
}
.topbar-user { display: flex; align-items: center; gap: 12px; font-size: 14px; }
.content { padding: 20px; flex: 1; }
.page-header { margin-bottom: 20px; }
.page-header h2 { font-size: 22px; margin-bottom: 4px; }
.page-header p { color: var(--muted); font-size: 14px; }

.card {
    background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
    border: 1px solid var(--border); overflow: hidden;
}
.card-header {
    padding: 16px 20px; border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px;
}
.card-body { padding: 20px; }
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.search-box { display: flex; gap: 8px; }
.search-box input { width: 220px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; min-width: 600px; }
th, td { padding: 12px 16px; text-align: left; border-bottom: 1px solid var(--border); }
th { background: #f8fafc; font-weight: 600; color: var(--muted); font-size: 13px; }
tr:hover td { background: #f8fafc; }
.badge {
    display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 500;
}
.badge-success { background: #dcfce7; color: #166534; }
.badge-danger { background: #fee2e2; color: #991b1b; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

.stats-grid {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-bottom: 20px;
}
.stat-card {
    background: var(--card); border-radius: var(--radius); padding: 20px;
    border: 1px solid var(--border); box-shadow: var(--shadow);
}
.stat-card h3 { font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.stat-card .num { font-size: 28px; font-weight: 700; color: var(--primary); }

.pagination { display: flex; gap: 6px; flex-wrap: wrap; padding: 16px 20px; justify-content: center; }
.pagination a, .pagination span {
    padding: 6px 12px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px; color: var(--text);
}
.pagination .current { background: var(--primary); color: #fff; border-color: var(--primary); }

.perm-tree-wrap {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: #fff;
    padding: 12px 16px;
    max-height: 520px;
    overflow: auto;
}
.perm-tree {
    list-style: none;
    margin: 0;
    padding: 0;
}
.perm-tree-root > .perm-tree-node { padding-left: 0; }
.perm-tree ul {
    list-style: none;
    margin: 0;
    padding: 0 0 0 22px;
    position: relative;
}
.perm-tree ul::before {
    content: '';
    position: absolute;
    left: 10px;
    top: 0;
    bottom: 10px;
    border-left: 1px dashed #cbd5e1;
}
.perm-tree-node.is-last > ul::before {
    bottom: auto;
    height: 18px;
}
.perm-tree-node {
    position: relative;
    margin: 0;
}
.perm-tree-node::before {
    content: '';
    position: absolute;
    left: -12px;
    top: 18px;
    width: 12px;
    border-top: 1px dashed #cbd5e1;
}
.perm-tree-root > .perm-tree-node::before,
.perm-tree-root > .perm-tree-node > .perm-tree-row::before { display: none; }
.perm-tree-row {
    display: flex;
    align-items: center;
    gap: 4px;
    min-height: 36px;
    padding: 2px 8px;
    border-radius: 6px;
    position: relative;
}
.perm-tree-row:hover { background: #f8fafc; }
.perm-tree-spacer,
.perm-tree-toggle {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.perm-tree-toggle {
    border: none;
    background: #eef2ff;
    border-radius: 4px;
    cursor: pointer;
    padding: 0;
    transition: .2s;
}
.perm-tree-toggle:hover { background: #e0e7ff; }
.perm-tree-toggle::before {
    content: '';
    width: 0;
    height: 0;
    border-top: 4px solid transparent;
    border-bottom: 4px solid transparent;
    border-left: 5px solid #4f46e5;
    transition: transform .2s;
}
.perm-tree-node.is-expanded > .perm-tree-row .perm-tree-toggle::before {
    transform: rotate(90deg);
}
.perm-tree-node:not(.is-expanded) > ul { display: none; }
.perm-tree-label {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
    min-width: 0;
    margin: 0;
    cursor: pointer;
    font-size: 14px;
}
.perm-tree-check {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
    margin: 0;
}
.perm-tree-name {
    font-weight: 500;
    color: var(--text);
    white-space: nowrap;
}
.perm-tree-code {
    color: var(--muted);
    font-size: 12px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.perm-tree-type {
    flex-shrink: 0;
    font-size: 11px;
    padding: 1px 8px;
    border-radius: 999px;
    background: #eef2ff;
    color: #4338ca;
}
.perm-tree-node.is-leaf .perm-tree-type {
    background: #f1f5f9;
    color: #64748b;
}

.data-input-tabs {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
    border-bottom: 1px solid var(--border);
    padding-bottom: 0;
}
.data-tab {
    padding: 10px 16px;
    border: none;
    background: none;
    cursor: pointer;
    font-size: 14px;
    color: var(--muted);
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
}
.data-tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
    font-weight: 600;
}
.data-tab-panel { display: none; }
.data-tab-panel.active { display: block; }
.data-textarea {
    font-family: Consolas, Monaco, monospace;
    line-height: 1.6;
    resize: vertical;
    min-height: 240px;
}
.data-file {
    padding: 8px 0;
    border: none;
    background: none;
}
.data-result {
    margin-top: 20px;
    padding: 16px;
    border-radius: 8px;
    border: 1px solid var(--border);
}
.data-result-success { background: #f0fdf4; border-color: #bbf7d0; }
.data-result-error { background: #fef2f2; border-color: #fecaca; }
.data-result h4 { margin: 0 0 12px; font-size: 15px; }
.data-result-block { margin-top: 12px; font-size: 14px; }
.data-result-block ul {
    margin: 8px 0 0;
    padding-left: 20px;
    max-height: 200px;
    overflow: auto;
}
.data-result-block li { margin: 4px 0; }
.data-result code {
    background: rgba(0,0,0,.06);
    padding: 2px 6px;
    border-radius: 4px;
}

.overlay {
    display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 999;
}
.overlay.show { display: block; }

.toast {
    position: fixed; top: 20px; right: 20px; padding: 12px 20px; border-radius: 8px;
    color: #fff; font-size: 14px; z-index: 9999; opacity: 0; transition: .3s; pointer-events: none;
}
.toast.show { opacity: 1; }
.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }

@media (max-width: 768px) {
    .sidebar {
        position: fixed; top: 0; left: 0; bottom: 0; transform: translateX(-100%);
    }
    .sidebar.open { transform: translateX(0); }
    .menu-toggle { display: block; }
    .content { padding: 16px; }
    .search-box input { width: 100%; }
    .card-header { flex-direction: column; align-items: stretch; }
    .toolbar { flex-direction: column; align-items: stretch; }
    .search-box { width: 100%; }
    .perm-tree-wrap { padding: 10px 12px; }
    .perm-tree-label { flex-wrap: wrap; }
    .perm-tree-code { width: 100%; padding-left: 24px; }
}
