/* ===== CSS Variables & Reset ===== */
:root {
    --sidebar-bg: #0f172a;
    --sidebar-border: #1e293b;
    --sidebar-text: #94a3b8;
    --sidebar-hover: #1e293b;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-light: #dbeafe;
    --success: #16a34a;
    --success-light: #dcfce7;
    --danger: #dc2626;
    --danger-light: #fee2e2;
    --warning: #d97706;
    --warning-light: #fef3c7;
    --purple: #7c3aed;
    --purple-light: #f3e8ff;
    --orange: #ea580c;
    --orange-light: #fff7ed;
    --bg: #f8fafc;
    --card-bg: #ffffff;
    --text: #0f172a;
    --text-secondary: #64748b;
    --border: #e2e8f0;
    --radius: 16px;
    --radius-sm: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,0.05), 0 1px 2px rgba(0,0,0,0.03);
    --shadow-lg: 0 10px 25px rgba(0,0,0,0.08);
    --transition: all 0.2s ease;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
}

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

/* ===== Sidebar ===== */
.sidebar {
    width: 260px;
    min-width: 260px;
    background: var(--sidebar-bg);
    color: #fff;
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    padding: 24px;
    border-bottom: 1px solid var(--sidebar-border);
}

.sidebar-header h1 {
    font-size: 18px;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 10px;
}

.sidebar-header h1 .icon {
    color: #60a5fa;
    font-size: 22px;
}

.sidebar-header p {
    font-size: 11px;
    color: var(--sidebar-text);
    margin-top: 4px;
    text-transform: uppercase;
    letter-spacing: 2px;
}

.sidebar-nav {
    flex: 1;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-radius: 10px;
    color: var(--sidebar-text);
    cursor: pointer;
    transition: var(--transition);
    border: none;
    background: none;
    width: 100%;
    text-align: left;
    font-size: 14px;
    font-weight: 500;
    font-family: inherit;
}

.nav-item:hover {
    background: var(--sidebar-hover);
    color: #fff;
}

.nav-item.active {
    background: var(--primary);
    color: #fff;
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
}

.nav-item .nav-icon {
    font-size: 18px;
    width: 22px;
    text-align: center;
}

.sidebar-footer {
    padding: 16px 24px;
    border-top: 1px solid var(--sidebar-border);
    font-size: 12px;
    color: var(--sidebar-text);
}

.sidebar-footer .status {
    color: #4ade80;
}

/* ===== Main Content ===== */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.main-header {
    height: 64px;
    min-height: 64px;
    background: var(--card-bg);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 32px;
    position: sticky;
    top: 0;
    z-index: 10;
}

.main-header h2 {
    font-size: 18px;
    font-weight: 600;
}

.main-body {
    flex: 1;
    overflow-y: auto;
    padding: 32px;
}

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
    border: none;
    font-family: inherit;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}

.btn-primary:hover {
    background: var(--primary-hover);
}

.btn-outline {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
}

.btn-outline:hover {
    background: var(--bg);
}

.btn-danger {
    background: var(--danger);
    color: #fff;
}

.btn-danger:hover {
    background: #b91c1c;
}

.btn-sm {
    padding: 6px 14px;
    font-size: 13px;
}

.btn-link {
    background: none;
    border: none;
    color: var(--primary);
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    padding: 0;
    font-family: inherit;
}

.btn-link:hover {
    text-decoration: underline;
}

/* ===== Cards ===== */
.card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.stat-card {
    padding: 24px;
}

.stat-card .stat-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

.stat-card .stat-label {
    font-size: 14px;
    color: var(--text-secondary);
    font-weight: 500;
}

.stat-card .stat-value {
    font-size: 32px;
    font-weight: 700;
    margin-top: 8px;
}

.stat-card .stat-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
}

.stat-card .stat-footer {
    margin-top: 16px;
    font-size: 12px;
    display: flex;
    align-items: center;
    gap: 4px;
}

/* ===== Grid ===== */
.grid-3 {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 24px;
}

.grid-2 {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
}

.col-span-3 {
    grid-column: span 3;
}

/* ===== Table ===== */
.table-container {
    overflow-x: auto;
}

.table-toolbar {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    background: #f8fafc;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: var(--radius) var(--radius) 0 0;
}

.search-input {
    position: relative;
    width: 300px;
}

.search-input input {
    width: 100%;
    padding: 10px 16px 10px 40px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    outline: none;
    font-size: 14px;
    transition: var(--transition);
    font-family: inherit;
}

.search-input input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.search-input .search-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-secondary);
    font-size: 16px;
}

table {
    width: 100%;
    border-collapse: collapse;
}

thead tr {
    background: #f8fafc;
    border-bottom: 1px solid var(--border);
}

th {
    padding: 14px 24px;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    text-align: left;
}

td {
    padding: 16px 24px;
    font-size: 14px;
}

tbody tr {
    border-bottom: 1px solid var(--border);
    transition: var(--transition);
}

tbody tr:hover {
    background: #f8fafc;
}

tbody tr:last-child {
    border-bottom: none;
}

/* ===== Avatar ===== */
.avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-weight: 700;
    font-size: 13px;
    background: linear-gradient(135deg, #3b82f6, #6366f1);
}

.user-cell {
    display: flex;
    align-items: center;
    gap: 12px;
}

.user-cell .name {
    font-weight: 500;
}

/* ===== Badge ===== */
.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 500;
}

.badge-success {
    background: var(--success-light);
    color: var(--success);
}

.badge-danger {
    background: var(--danger-light);
    color: var(--danger);
}

.badge-warning {
    background: var(--warning-light);
    color: var(--warning);
}

/* ===== App Cards ===== */
.app-card {
    padding: 24px;
    position: relative;
    transition: var(--transition);
}

.app-card:hover {
    box-shadow: var(--shadow-lg);
}

.app-card .app-delete {
    position: absolute;
    top: 16px;
    right: 16px;
    color: #cbd5e1;
    cursor: pointer;
    opacity: 0;
    transition: var(--transition);
    background: none;
    border: none;
    font-size: 18px;
}

.app-card:hover .app-delete {
    opacity: 1;
}

.app-card .app-delete:hover {
    color: var(--danger);
}

.app-icon {
    width: 48px;
    height: 48px;
    background: #f1f5f9;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    font-weight: 700;
    color: var(--primary);
    margin-bottom: 16px;
}

.app-card h4 {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 4px;
}

.app-card .app-key {
    font-size: 12px;
    color: var(--text-secondary);
    font-family: 'Courier New', monospace;
    margin-bottom: 16px;
}

.app-card .app-details {
    border-top: 1px solid var(--border);
    padding-top: 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.app-card .detail-row {
    display: flex;
    justify-content: space-between;
    font-size: 13px;
}

.app-card .detail-row .label {
    color: var(--text-secondary);
}

.app-card .detail-row .value {
    font-weight: 500;
}

.status-dot {
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 6px;
}

.status-dot.active {
    background: var(--success);
}

.app-card .app-doc-btn {
    width: 100%;
    margin-top: 20px;
    padding: 10px;
    background: #f8fafc;
    color: var(--text-secondary);
    font-size: 13px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    border: none;
    font-weight: 500;
    transition: var(--transition);
    font-family: inherit;
}

.app-card .app-doc-btn:hover {
    background: #f1f5f9;
}

/* ===== Permission Panel ===== */
.perm-layout {
    display: flex;
    gap: 24px;
    height: calc(100vh - 200px);
}

.perm-user-list {
    width: 320px;
    min-width: 320px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.perm-user-header {
    padding: 16px 20px;
    font-weight: 700;
    background: #f8fafc;
    border-bottom: 1px solid var(--border);
    border-radius: var(--radius) var(--radius) 0 0;
}

.perm-user-scroll {
    flex: 1;
    overflow-y: auto;
}

.perm-user-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    transition: var(--transition);
    background: none;
    border: none;
    border-bottom: 1px solid var(--border);
    width: 100%;
    text-align: left;
    font-family: inherit;
}

.perm-user-item:hover {
    background: #f8fafc;
}

.perm-user-item.active {
    background: #eff6ff;
    border-right: 4px solid var(--primary);
}

.perm-user-item .user-name {
    font-weight: 600;
    font-size: 14px;
}

.perm-user-item .user-id {
    font-size: 12px;
    color: var(--text-secondary);
}

.perm-user-item .chevron {
    color: #cbd5e1;
}

.perm-detail {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.perm-detail-header {
    padding: 16px 20px;
    font-weight: 700;
    background: #f8fafc;
    border-bottom: 1px solid var(--border);
    border-radius: var(--radius) var(--radius) 0 0;
}

.perm-detail-body {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
}

.perm-user-info {
    background: #eff6ff;
    padding: 16px 20px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    gap: 16px;
    margin-bottom: 24px;
}

.perm-user-info .avatar-lg {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 16px;
}

.perm-user-info .info-name {
    font-weight: 700;
    font-size: 16px;
}

.perm-user-info .info-desc {
    font-size: 13px;
    color: var(--text-secondary);
}

.perm-app-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border: 1px solid var(--border);
    border-radius: 12px;
    margin-bottom: 12px;
    transition: var(--transition);
}

.perm-app-item:hover {
    background: #f8fafc;
}

.perm-app-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.perm-app-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.perm-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
}

.perm-empty .empty-icon {
    font-size: 48px;
    opacity: 0.2;
    margin-bottom: 16px;
}

/* ===== Toggle Switch ===== */
.toggle {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
}

.toggle input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    cursor: pointer;
    inset: 0;
    background: #cbd5e1;
    border-radius: 24px;
    transition: var(--transition);
}

.toggle-slider:before {
    content: '';
    position: absolute;
    width: 20px;
    height: 20px;
    left: 2px;
    bottom: 2px;
    background: #fff;
    border-radius: 50%;
    transition: var(--transition);
}

.toggle input:checked + .toggle-slider {
    background: var(--primary);
}

.toggle input:checked + .toggle-slider:before {
    transform: translateX(20px);
}

/* ===== Select ===== */
.select {
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 13px;
    outline: none;
    background: #fff;
    font-family: inherit;
    cursor: pointer;
}

.select:focus {
    border-color: var(--primary);
}

/* ===== Modal ===== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

.modal {
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 32px;
    width: 100%;
    max-width: 480px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.15);
    animation: modalIn 0.2s ease;
}

.modal-lg {
    max-width: 560px;
}

@keyframes modalIn {
    from { opacity: 0; transform: translateY(-10px); }
    to { opacity: 1; transform: translateY(0); }
}

.modal h3 {
    font-size: 20px;
    font-weight: 700;
    margin-bottom: 4px;
}

.modal .modal-desc {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: 24px;
}

.modal .form-group {
    margin-bottom: 16px;
}

.modal label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 6px;
}

.modal input, .modal select {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    outline: none;
    transition: var(--transition);
    font-family: inherit;
}

.modal input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.modal input:disabled {
    background: #f1f5f9;
    color: var(--text-secondary);
    cursor: not-allowed;
}

.modal .form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

.modal-footer {
    display: flex;
    gap: 16px;
    margin-top: 24px;
}

.modal-footer .btn {
    flex: 1;
    justify-content: center;
}

/* ===== Settings ===== */
.settings-card {
    max-width: 640px;
    padding: 32px;
}

.settings-card h3 {
    font-size: 20px;
    font-weight: 700;
    margin-bottom: 4px;
}

.settings-card .settings-desc {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: 32px;
}

.settings-card .form-group {
    margin-bottom: 20px;
}

.settings-card label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 6px;
}

.settings-card input, .settings-card select {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    outline: none;
    transition: var(--transition);
    font-family: inherit;
}

.settings-card input:focus, .settings-card select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.notice-box {
    background: var(--warning-light);
    border: 1px solid #fde68a;
    border-radius: 12px;
    padding: 16px 20px;
    margin: 24px 0;
}

.notice-box h4 {
    font-size: 13px;
    font-weight: 700;
    color: #92400e;
    margin-bottom: 6px;
}

.notice-box p {
    font-size: 12px;
    color: #a16207;
    line-height: 1.8;
}

.settings-footer {
    padding-top: 20px;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: 12px;
}

/* ===== Sync Log ===== */
.log-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 0;
    border-bottom: 1px solid var(--border);
}

.log-item:last-child {
    border-bottom: none;
}

.log-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.log-icon {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: #f1f5f9;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    font-size: 14px;
}

.log-title {
    font-weight: 500;
    font-size: 14px;
}

.log-detail {
    font-size: 12px;
    color: var(--text-secondary);
}

.log-time {
    font-size: 13px;
    color: var(--text-secondary);
}

/* ===== Login Modal ===== */
.login-overlay {
    position: fixed;
    inset: 0;
    background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
}

.login-box {
    background: var(--card-bg);
    border-radius: var(--radius);
    padding: 48px 40px;
    width: 100%;
    max-width: 420px;
    box-shadow: 0 25px 80px rgba(0,0,0,0.3);
    text-align: center;
}

.login-box .login-icon {
    font-size: 40px;
    margin-bottom: 16px;
    color: var(--primary);
}

.login-box h2 {
    font-size: 22px;
    font-weight: 700;
    margin-bottom: 4px;
}

.login-box .login-subtitle {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 32px;
}

.login-box .form-group {
    margin-bottom: 20px;
    text-align: left;
}

.login-box label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 6px;
}

.login-box input {
    width: 100%;
    padding: 12px 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    outline: none;
    transition: var(--transition);
    font-family: inherit;
}

.login-box input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.login-box .btn {
    width: 100%;
    justify-content: center;
    padding: 12px;
    font-size: 15px;
}

.login-error {
    color: var(--danger);
    font-size: 13px;
    margin-bottom: 16px;
}

/* ===== Animations ===== */
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.fade-in {
    animation: fadeIn 0.3s ease;
}

/* ===== Toast ===== */
.toast {
    position: fixed;
    top: 24px;
    right: 24px;
    padding: 14px 24px;
    border-radius: var(--radius-sm);
    color: #fff;
    font-size: 14px;
    font-weight: 500;
    z-index: 300;
    animation: toastIn 0.3s ease;
    box-shadow: var(--shadow-lg);
}

.toast-success {
    background: var(--success);
}

.toast-error {
    background: var(--danger);
}

@keyframes toastIn {
    from { opacity: 0; transform: translateY(-10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ===== Department Tree & Users Layout ===== */
.users-layout {
    display: flex;
    gap: 24px;
    height: calc(100vh - 160px);
}

.dept-tree-panel {
    width: 260px;
    min-width: 260px;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.dept-tree-header {
    padding: 16px 20px;
    font-weight: 700;
    font-size: 14px;
    background: #f8fafc;
    border-bottom: 1px solid var(--border);
    border-radius: var(--radius) var(--radius) 0 0;
    color: var(--text);
}

.dept-tree-scroll {
    flex: 1;
    overflow-y: auto;
}

.dept-tree-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    width: 100%;
    border: none;
    background: none;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    cursor: pointer;
    transition: var(--transition);
    text-align: left;
    font-family: inherit;
    border-bottom: 1px solid transparent;
}

.dept-tree-item:hover {
    background: #f8fafc;
    color: var(--text);
}

.dept-tree-item.active {
    background: var(--primary-light);
    color: var(--primary);
    font-weight: 600;
    border-left: 3px solid var(--primary);
}

.dept-tree-item .dept-count {
    margin-left: auto;
    font-size: 11px;
    font-weight: 500;
    background: #e2e8f0;
    color: var(--text-secondary);
    padding: 1px 8px;
    border-radius: 10px;
}

.dept-tree-item.active .dept-count {
    background: var(--primary);
    color: #fff;
}

.users-table-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.users-table-panel .table-container {
    flex: 1;
    overflow-y: auto;
}

/* ===== Responsive ===== */
@media (max-width: 1024px) {
    .grid-3 {
        grid-template-columns: repeat(2, 1fr);
    }
    .col-span-3 {
        grid-column: span 2;
    }
}

@media (max-width: 768px) {
    .grid-3 {
        grid-template-columns: 1fr;
    }
    .col-span-3 {
        grid-column: span 1;
    }
    .sidebar {
        width: 200px;
        min-width: 200px;
    }
}
