:root {
    --bg: #f5f6fa;
    --card-bg: #ffffff;
    --primary: #6366f1;
    --primary-hover: #4f46e5;
    --primary-light: #eef2ff;
    --success: #10b981;
    --success-light: #ecfdf5;
    --warning: #f59e0b;
    --warning-light: #fffbeb;
    --danger: #ef4444;
    --danger-light: #fef2f2;
    --text: #1e293b;
    --text-secondary: #64748b;
    --text-muted: #94a3b8;
    --border: #e2e8f0;
    --shadow-sm: 0 1px 2px rgba(0,0,0,.04);
    --shadow: 0 1px 3px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.04);
    --shadow-md: 0 4px 12px rgba(0,0,0,.08);
    --shadow-lg: 0 10px 30px rgba(0,0,0,.1);
    --radius: 12px;
    --radius-sm: 8px;
    --radius-xs: 6px;
    --sidebar-w: 240px;
    --header-h: 60px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    min-height: 100vh;
    display: flex;
}

/* ===== 侧边栏 ===== */
.sidebar {
    width: var(--sidebar-w);
    background: #fff;
    border-right: 1px solid var(--border);
    height: 100vh;
    position: fixed;
    left: 0;
    top: 0;
    display: flex;
    flex-direction: column;
    z-index: 50;
    box-shadow: 1px 0 8px rgba(0,0,0,.03);
}
.sidebar-logo {
    height: var(--header-h);
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0 20px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
.sidebar-logo .logo-icon {
    width: 36px;
    height: 36px;
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 18px;
}
.sidebar-logo .logo-text {
    font-weight: 700;
    font-size: 13px;
    line-height: 1.3;
    color: var(--text);
}

.sidebar-nav {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
}
.sidebar-section {
    margin-bottom: 8px;
}
.sidebar-section-title {
    font-size: 11px;
    text-transform: uppercase;
    color: var(--text-muted);
    padding: 8px 12px 4px;
    font-weight: 600;
    letter-spacing: .5px;
}
.sidebar-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: var(--radius-xs);
    cursor: pointer;
    font-size: 14px;
    color: var(--text-secondary);
    transition: all .15s;
    position: relative;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
    font-family: inherit;
}
.sidebar-item:hover {
    background: var(--primary-light);
    color: var(--primary);
}
.sidebar-item.active {
    background: var(--primary-light);
    color: var(--primary);
    font-weight: 600;
}
.sidebar-item .count {
    margin-left: auto;
    font-size: 12px;
    background: #f1f5f9;
    padding: 2px 8px;
    border-radius: 10px;
    min-width: 24px;
    text-align: center;
}
.sidebar-item.active .count {
    background: var(--primary);
    color: #fff;
}
.sidebar-item .item-icon { font-size: 16px; width: 20px; text-align: center; }
.sidebar-item-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
}
.sidebar-del {
    font-size: 13px;
    opacity: 0;
    transition: opacity .15s;
    cursor: pointer;
    flex-shrink: 0;
    padding: 2px 4px;
    border-radius: 4px;
    line-height: 1;
}
.sidebar-item:hover .sidebar-del { opacity: 1; }
.sidebar-del:hover { background: var(--danger-light); }

/* ===== 用户分组（管理员视图） ===== */
.user-group {
    margin-bottom: 24px;
    background: #fff;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
}
.user-group-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 20px;
    background: linear-gradient(135deg, #f8fafc, #f1f5f9);
    border-bottom: 2px solid var(--border);
}
.user-group-header .user-avatar-sm {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 14px;
    flex-shrink: 0;
}
.user-group-header .user-group-name {
    font-size: 15px;
    font-weight: 700;
    color: var(--text);
}
.user-group-header .user-group-count {
    font-size: 12px;
    color: var(--text-muted);
    margin-left: auto;
}
.user-group-body {
    padding: 16px 20px;
}

/* ===== 设备归属徽章 ===== */
.owner-badge {
    display: inline-block;
    font-size: 10px;
    color: var(--primary);
    background: var(--primary-light);
    padding: 2px 7px;
    border-radius: 8px;
    font-weight: 600;
    margin-bottom: 8px;
}


.sidebar-footer {
    border-top: 1px solid var(--border);
    padding: 12px;
    flex-shrink: 0;
}
.sidebar-footer .user-info {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 8px;
    padding: 0 4px;
}
.sidebar-footer .user-avatar {
    width: 30px;
    height: 30px;
    border-radius: 8px;
    background: var(--primary-light);
    color: var(--primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 13px;
}

/* ===== 主内容区域 ===== */
.main-content {
    margin-left: var(--sidebar-w);
    flex: 1;
    min-height: 100vh;
}
.topbar {
    height: var(--header-h);
    background: #fff;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 28px;
    position: sticky;
    top: 0;
    z-index: 40;
}
.topbar-title {
    font-size: 16px;
    font-weight: 600;
}
.topbar-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

.content-body {
    padding: 24px 28px;
}

/* ===== 统计卡片 ===== */
.stats-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 14px;
    margin-bottom: 24px;
}
.stat-card {
    background: #fff;
    border-radius: var(--radius);
    padding: 18px 20px;
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 14px;
    transition: all .2s;
}
.stat-card:hover {
    box-shadow: var(--shadow-md);
    transform: translateY(-1px);
}
.stat-icon {
    width: 44px;
    height: 44px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    flex-shrink: 0;
}
.stat-icon.blue { background: #eef2ff; color: #6366f1; }
.stat-icon.orange { background: #fffbeb; color: #f59e0b; }
.stat-icon.red { background: #fef2f2; color: #ef4444; }
.stat-icon.green { background: #ecfdf5; color: #10b981; }
.stat-icon.yellow { background: #fefce8; color: #ca8a04; }
.stat-label { font-size: 12px; color: var(--text-secondary); margin-bottom: 2px; }
.stat-value { font-size: 24px; font-weight: 700; line-height: 1.2; }
.stat-sub { font-size: 11px; color: var(--text-secondary); }

/* ===== 按钮 ===== */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: none;
    border-radius: var(--radius-xs);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all .2s;
    white-space: nowrap;
    font-family: inherit;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); box-shadow: 0 2px 8px rgba(99,102,241,.3); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { background: #059669; }
.btn-warning { background: var(--warning); color: #fff; }
.btn-warning:hover { background: #d97706; }
.btn-info { background: #0ea5e9; color: #fff; }
.btn-info:hover { background: #0284c7; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #dc2626; }
.btn-outline {
    background: #fff;
    color: var(--text);
    border: 1px solid var(--border);
}
.btn-outline:hover { background: #f8fafc; border-color: #cbd5e1; }
.btn-ghost { background: transparent; color: var(--text-secondary); }
.btn-ghost:hover { background: #f1f5f9; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-xs { padding: 3px 8px; font-size: 11px; }
.btn-icon { width: 32px; height: 32px; padding: 0; justify-content: center; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ===== 项目分组卡片 ===== */
.project-group {
    margin-bottom: 20px;
}
.project-group-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 0 8px;
    border-bottom: 2px solid var(--border);
    margin-bottom: 12px;
}
.project-group-header .project-name {
    font-size: 15px;
    font-weight: 700;
    color: var(--text);
}
.project-group-header .project-actions {
    margin-left: auto;
    display: flex;
    gap: 6px;
}

/* ===== 设备卡片网格 ===== */
.device-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
    gap: 12px;
}
.device-card {
    background: #fff;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    padding: 16px 20px;
    box-shadow: var(--shadow-sm);
    transition: all .2s;
    position: relative;
}
.device-card:hover {
    box-shadow: var(--shadow-md);
    border-color: #cbd5e1;
}
.device-card.expired {
    border-left: 3px solid var(--danger);
}
.device-card.expiring {
    border-left: 3px solid var(--warning);
}
.device-card .card-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    margin-bottom: 10px;
}
.device-card .card-name {
    font-weight: 600;
    font-size: 15px;
    color: var(--text);
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.device-card .card-badge {
    font-size: 11px;
    padding: 3px 8px;
    border-radius: 10px;
    font-weight: 500;
    flex-shrink: 0;
}
.badge-ok { background: var(--success-light); color: #059669; }
.badge-warn { background: var(--warning-light); color: #b45309; }
.badge-err { background: var(--danger-light); color: #dc2626; }
.badge-none { background: #f1f5f9; color: #64748b; }

.device-card .card-info {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px 16px;
    font-size: 13px;
    margin-bottom: 10px;
}
.card-info .info-item {
    display: flex;
    flex-direction: column;
}
.info-item .info-label {
    font-size: 11px;
    color: var(--text-muted);
}
.info-item .info-value {
    font-weight: 500;
    word-break: break-all;
}
.info-item .info-value.code {
    font-family: 'SF Mono', 'Fira Code', monospace;
    font-size: 11px;
    background: #f8fafc;
    padding: 2px 6px;
    border-radius: 4px;
}

.device-card .card-flow {
    display: flex;
    gap: 16px;
    margin-bottom: 10px;
    padding: 10px 12px;
    background: #f8fafc;
    border-radius: var(--radius-xs);
}
.card-flow .flow-item {
    flex: 1;
}
.flow-item .flow-label {
    font-size: 11px;
    color: var(--text-muted);
}
.flow-item .flow-value {
    font-size: 16px;
    font-weight: 700;
}
.flow-value.used { color: #059669; }
.flow-value.remain { color: #6366f1; }

.device-card .card-progress {
    margin-bottom: 10px;
}
.progress-bar {
    height: 5px;
    background: #e2e8f0;
    border-radius: 3px;
    overflow: hidden;
    margin-bottom: 4px;
}
.progress-fill {
    height: 100%;
    border-radius: 3px;
    transition: width .4s ease;
}
.progress-low { background: var(--success); }
.progress-mid { background: var(--warning); }
.progress-high { background: var(--danger); }
.progress-text {
    font-size: 11px;
    color: var(--text-secondary);
    text-align: right;
}

.device-card .card-actions {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
    padding-top: 8px;
    border-top: 1px solid var(--border);
}

/* ===== 查询进度 ===== */
.query-progress {
    display: none;
    background: #fff;
    border-radius: var(--radius);
    padding: 14px 20px;
    margin-bottom: 16px;
    border: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    align-items: center;
    gap: 12px;
}
.query-progress.active { display: flex; }
.spinner {
    width: 20px;
    height: 20px;
    border: 3px solid #e2e8f0;
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== 弹窗 ===== */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: rgba(15,23,42,.4);
    backdrop-filter: blur(3px);
    z-index: 200;
    align-items: center; justify-content: center;
}
.modal-overlay.active { display: flex; }
.modal {
    background: #fff;
    border-radius: var(--radius);
    width: 90%;
    max-width: 520px;
    box-shadow: var(--shadow-lg);
    animation: modalIn .25s ease;
}
@keyframes modalIn {
    from { opacity: 0; transform: scale(.95) translateY(8px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}
.modal-header {
    padding: 18px 24px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.modal-header h3 { font-size: 17px; font-weight: 600; }
.modal-close {
    width: 30px; height: 30px;
    border-radius: 6px;
    border: none;
    background: #f1f5f9;
    cursor: pointer;
    font-size: 16px;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all .2s;
}
.modal-close:hover { background: #e2e8f0; color: var(--text); }
.modal-body { padding: 20px 24px; max-height: 60vh; overflow-y: auto; }
.modal-footer {
    padding: 14px 24px;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

/* 表单 */
.form-group { margin-bottom: 14px; }
.form-group label {
    display: block;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 5px;
}
.form-group input, .form-group select {
    width: 100%;
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-xs);
    font-size: 14px;
    transition: all .2s;
    outline: none;
    font-family: inherit;
    background: #fff;
}
.form-group input:focus, .form-group select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(99,102,241,.1);
}
.form-hint { font-size: 11px; color: var(--text-muted); margin-top: 3px; }

/* Toast */
.toast-container {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.toast {
    padding: 12px 18px;
    border-radius: var(--radius-xs);
    color: #fff;
    font-size: 13px;
    font-weight: 500;
    box-shadow: var(--shadow-md);
    animation: toastIn .3s ease;
    max-width: 380px;
}
.toast-success { background: #059669; }
.toast-error { background: #dc2626; }
.toast-info { background: #6366f1; }
@keyframes toastIn {
    from { opacity: 0; transform: translateX(30px); }
    to { opacity: 1; transform: translateX(0); }
}

.empty-state {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-muted);
}
.empty-state .icon { font-size: 40px; margin-bottom: 8px; opacity: .5; }

/* ===== 视图切换 ===== */
.view-toggle {
    display: inline-flex;
    background: #f1f5f9;
    border-radius: 6px;
    padding: 2px;
    margin-right: 4px;
}
.view-toggle .vt-btn {
    width: 30px; height: 28px;
    border: none;
    background: transparent;
    border-radius: 5px;
    cursor: pointer;
    font-size: 15px;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all .2s;
}
.view-toggle .vt-btn.active {
    background: #fff;
    color: var(--primary);
    box-shadow: 0 1px 2px rgba(0,0,0,.06);
}

/* ===== 列表视图 ===== */
.device-table {
    width: 100%;
    table-layout: fixed;
    border-collapse: collapse;
    background: #fff;
    font-size: 13px;
}
.device-table th {
    background: #f8fafc;
    padding: 10px 14px;
    text-align: left;
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 12px;
    white-space: nowrap;
    border-bottom: 1px solid var(--border);
}
.device-table td {
    padding: 10px 14px;
    border-bottom: 1px solid #f1f5f9;
    vertical-align: middle;
}
.device-table tr:hover td { background: #fafbff; }
.device-table tr.row-expired td { border-left: 3px solid var(--danger); }
.device-table tr.row-expiring td { border-left: 3px solid var(--warning); }
.device-table .table-badge {
    display: inline-block;
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 10px;
    font-weight: 500;
}
.device-table .table-actions {
    display: flex;
    gap: 4px;
}
.device-table .code {
    font-family: 'SF Mono', 'Cascadia Code', monospace;
    font-size: 12px;
    color: var(--text-secondary);
}
.device-table .flow-text {
    font-weight: 600;
    font-size: 13px;
}
.device-table .flow-text.used { color: var(--danger); }
.device-table .flow-text.remain { color: var(--primary); }

/* 响应式 */
@media (max-width: 768px) {
    .sidebar { display: none; }
    .main-content { margin-left: 0; }
    .device-grid { grid-template-columns: 1fr; }
    .stats-row { grid-template-columns: repeat(2, 1fr); }
}
