/* +----------------------------------------------------------------------
 * 全局样式 - 能源监测管理系统
 * 基于 Bootstrap5 扩展
 * +---------------------------------------------------------------------- */

:root {
    --sidebar-width: 240px;
    --sidebar-bg: #2c3e50;
    --sidebar-hover: #34495e;
    --topbar-height: 56px;
    --primary-color: #0d6efd;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", Roboto, sans-serif;
    background: #f0f2f5;
    margin: 0;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ========== 应用容器布局 ========== */
.app-container { display: flex; min-height: 100vh; position: relative; }

/* ========== 侧边栏遮罩（移动端） ========== */
.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 999;
    opacity: 0;
    transition: opacity 0.3s;
}
.sidebar-backdrop.show {
    display: block;
    opacity: 1;
}

/* ========== 侧边栏 ========== */
.sidebar {
    width: var(--sidebar-width);
    background: var(--sidebar-bg);
    color: #ecf0f1;
    position: fixed;
    top: 0; left: 0; bottom: 0;
    transition: transform 0.3s ease;
    z-index: 1000;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    box-shadow: 2px 0 8px rgba(0,0,0,0.15);
}

.sidebar.collapsed { transform: translateX(-100%); }

.sidebar-header {
    padding: 18px 15px;
    background: rgba(0,0,0,0.2);
    text-align: center;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    position: sticky;
    top: 0;
    z-index: 1;
}

.sidebar-title { font-size: 1rem; font-weight: 600; }

.sidebar-menu { padding: 8px 0; }

.sidebar-menu .nav-item {
    display: flex;
    align-items: center;
    padding: 13px 20px;
    color: #bdc3c7;
    text-decoration: none;
    transition: all 0.2s;
    border-left: 3px solid transparent;
    font-size: 0.92rem;
    min-height: 44px;
    line-height: 1.2;
}

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

.sidebar-menu .nav-item.active {
    background: var(--sidebar-hover);
    color: #fff;
    border-left-color: var(--primary-color);
}

.sidebar-menu .nav-item i {
    width: 22px;
    text-align: center;
    margin-right: 10px;
    font-size: 1.05rem;
    flex-shrink: 0;
}

.sidebar-menu .menu-section {
    padding: 14px 20px 6px;
    font-size: 0.72rem;
    color: #7f8c8d;
    text-transform: uppercase;
    letter-spacing: 1px;
    font-weight: 600;
}

/* ========== 主内容区 ========== */
.main-content {
    flex: 1;
    margin-left: var(--sidebar-width);
    transition: margin-left 0.3s ease;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-width: 0;
}

.main-content.expanded { margin-left: 0; }

/* ========== 顶部导航 ========== */
.top-navbar {
    height: var(--topbar-height);
    position: sticky;
    top: 0;
    z-index: 900;
    min-height: 56px;
}

.top-navbar .dropdown-toggle::after { display: none; }

.top-navbar .navbar {
    padding-left: 0;
    padding-right: 0.75rem;
}

#sidebarToggle {
    width: 48px;
    height: 48px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.5rem;
    margin-left: 4px;
    transition: background 0.2s;
}
#sidebarToggle:hover {
    background: rgba(13, 110, 253, 0.08);
}
#sidebarToggle i { font-size: 1.4rem; }

/* ========== 顶部下拉菜单优化（移动端） ========== */
.top-navbar .dropdown-menu {
    min-width: 180px;
}

/* ========== 内容区域 ========== */
.content-wrapper {
    flex: 1;
    min-width: 0;
}

/* ========== 页头 ========== */
.page-header {
    flex-wrap: wrap;
    gap: 0.75rem;
}
.page-header h4 {
    font-size: 1.25rem;
    margin: 0;
}

/* ========== 卡片 ========== */
.card {
    border: none;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
    border-radius: 0.5rem;
    margin-bottom: 1rem;
}
.card-header {
    background: #fff;
    border-bottom: 1px solid #f0f2f5;
    font-weight: 600;
    padding: 0.85rem 1rem;
}
.card-body {
    padding: 1rem;
}

/* ========== 统计卡片 ========== */
.stat-card {
    border-left: 4px solid var(--primary-color);
    transition: transform 0.2s;
}
.stat-card:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
.stat-card .stat-icon { font-size: 2.2rem; opacity: 0.3; }
.stat-card .stat-value { font-size: 1.5rem; font-weight: 700; }
.stat-card .stat-label { color: #6c758d; font-size: 0.82rem; }

/* ========== 表格 ========== */
.table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.table {
    width: 100%;
    margin-bottom: 0;
    font-size: 0.9rem;
}
.table thead th {
    background: #f8f9fa;
    border-bottom: 2px solid #dee2e6;
    font-weight: 600;
    white-space: nowrap;
    padding: 0.7rem 0.8rem;
    font-size: 0.85rem;
}
.table td, .table th {
    vertical-align: middle;
    padding: 0.7rem 0.8rem;
    border-top: 1px solid #e9ecef;
}
.table-hover tbody tr:hover { background: #f8f9fa; }
.table .btn-group { white-space: nowrap; }

/* ========== 操作按钮组 ========== */
.action-btns .btn {
    margin-right: 4px;
    margin-bottom: 2px;
}

/* ========== 搜索栏 ========== */
.search-bar {
    background: #fff;
    padding: 1rem;
    border-radius: 0.5rem;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
    margin-bottom: 1rem;
}
.search-bar .form-label {
    font-size: 0.82rem;
    margin-bottom: 0.3rem;
    color: #6c757d;
    font-weight: 500;
}

/* ========== 表单 ========== */
.form-control, .form-select {
    min-height: 38px;
    font-size: 0.9rem;
    border-radius: 0.375rem;
}
.form-control-sm, .form-select-sm {
    min-height: 32px;
}
.form-label {
    font-weight: 500;
    margin-bottom: 0.35rem;
    font-size: 0.88rem;
}

/* ========== 按钮 ========== */
.btn {
    border-radius: 0.375rem;
    font-weight: 500;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
}
.btn-sm { font-size: 0.82rem; padding: 0.35rem 0.7rem; }
.btn-lg { min-height: 48px; font-size: 1rem; }

/* ========== 分页 ========== */
.pagination {
    justify-content: center;
    margin-top: 1rem;
    flex-wrap: wrap;
    gap: 2px;
}
.pagination .page-link {
    padding: 0.4rem 0.75rem;
    font-size: 0.85rem;
    border-radius: 0.35rem;
    margin: 0 1px;
}
.page-info {
    text-align: center;
    color: #6c757d;
    font-size: 0.82rem;
    margin-top: 0.5rem;
}

/* ========== 登录页 ========== */
.login-page {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    display: flex;
    align-items: center;
    padding: 1rem 0;
}
.login-card {
    max-width: 420px;
    width: 92%;
    border-radius: 1rem;
    box-shadow: 0 10px 40px rgba(0,0,0,0.2);
    margin: 0 auto;
}
.login-logo { font-size: 2.8rem; color: var(--primary-color); }

/* ========== 徽章 ========== */
.badge { font-weight: 500; padding: 0.4em 0.7em; font-size: 0.78em; border-radius: 0.35rem; }

/* ========== 设备状态指示 ========== */
.status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; }
.status-dot.online { background: #198754; animation: pulse 2s infinite; }
.status-dot.offline { background: #6c757d; }
.status-dot.disabled { background: #dc3545; }
.status-dot.fault { background: #fd7e14; }

@keyframes pulse { 0%,100%{opacity:1;} 50%{opacity:0.5;} }

/* ========== 数据指标 ========== */
.metric-box {
    text-align: center;
    padding: 1rem;
    background: #f8f9fa;
    border-radius: 0.5rem;
}
.metric-box .metric-value { font-size: 1.35rem; font-weight: 700; color: var(--primary-color); }
.metric-box .metric-label { font-size: 0.78rem; color: #6c758d; margin-top: 4px; }

/* ========== Toast ========== */
.toast-container {
    padding: 0.75rem !important;
    max-width: calc(100% - 1rem);
}
.toast { border-radius: 0.5rem; }

/* ========== 模态框 ========== */
.modal-content {
    border-radius: 0.75rem;
    border: none;
    box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}
.modal-header {
    border-bottom: 1px solid #f0f2f5;
    padding: 1rem 1.25rem;
}
.modal-body { padding: 1.25rem; }
.modal-footer {
    border-top: 1px solid #f0f2f5;
    padding: 0.85rem 1.25rem;
}

/* ========== 空状态 ========== */
.empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: #6c757d;
}
.empty-state i { font-size: 3rem; opacity: 0.5; margin-bottom: 1rem; }
.empty-state p { margin: 0.5rem 0; }

/* ========== 代码块 ========== */
code {
    background: #f1f3f5;
    padding: 0.15em 0.4em;
    border-radius: 0.25rem;
    font-size: 0.88em;
    word-break: break-all;
}

/* ========== 行内flex对齐 ========== */
.d-inline-flex-align {
    display: inline-flex;
    align-items: center;
}

/* ==================================================================
 * 平板（≤992px）适配
 * ================================================================== */
@media (max-width: 992px) {
    :root {
        --sidebar-width: 220px;
    }

    .stat-card .stat-value { font-size: 1.3rem; }
    .stat-card .stat-icon { font-size: 1.8rem; }

    .page-header h4 { font-size: 1.15rem; }
}

/* ==================================================================
 * 移动端（≤768px）适配
 * ================================================================== */
@media (max-width: 768px) {
    :root {
        --sidebar-width: 260px;
        --topbar-height: 52px;
    }

    /* ---- 侧边栏 ---- */
    .sidebar {
        transform: translateX(-100%);
        width: min(var(--sidebar-width), 85vw);
        box-shadow: 2px 0 16px rgba(0,0,0,0.25);
    }
    .sidebar.show {
        transform: translateX(0);
    }
    .sidebar.collapsed {
        transform: translateX(-100%);
    }

    /* ---- 主内容区 ---- */
    .main-content {
        margin-left: 0;
    }
    .main-content.expanded {
        margin-left: 0;
    }

    /* ---- 内容内边距缩小 ---- */
    .content-wrapper {
        padding: 0.85rem !important;
    }

    /* ---- 顶部导航 ---- */
    .top-navbar {
        height: var(--topbar-height);
    }
    #sidebarToggle {
        width: 44px;
        height: 44px;
    }
    #sidebarToggle i { font-size: 1.3rem; }

    .top-navbar .navbar {
        padding-right: 0.5rem;
    }

    /* 顶部用户下拉：隐藏文字（移动端只显示头像） */
    .top-navbar .dropdown .me-2,
    .top-navbar .dropdown .ms-2 {
        display: none;
    }
    .top-navbar .dropdown-toggle {
        padding: 0.25rem 0.5rem !important;
    }

    /* ---- 页头 ---- */
    .page-header {
        margin-bottom: 0.8rem !important;
        padding: 0;
    }
    .page-header > div:first-child {
        width: 100%;
    }
    .page-header h4 {
        font-size: 1.08rem;
    }
    .page-header p {
        font-size: 0.78rem;
        margin: 0;
    }
    .page-header .btn {
        font-size: 0.85rem;
        padding: 0.4rem 0.75rem;
    }

    /* ---- 搜索栏：堆叠 ---- */
    .search-bar {
        padding: 0.85rem;
        margin-bottom: 0.85rem;
    }
    .search-bar .row > div {
        width: 100%;
    }
    .search-bar .form-label {
        font-size: 0.8rem;
    }
    .search-bar .btn {
        width: 100%;
        margin-bottom: 0.35rem;
    }
    .search-bar .btn:last-child {
        margin-bottom: 0;
    }

    /* ---- 卡片 ---- */
    .card-header {
        padding: 0.75rem 0.85rem;
        font-size: 0.92rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    .card-header .btn {
        font-size: 0.82rem;
        padding: 0.3rem 0.6rem;
    }
    .card-body {
        padding: 0.75rem;
    }

    /* ---- 统计卡片 ---- */
    .stat-card .card-body {
        padding: 0.85rem 0.85rem;
    }
    .stat-card .stat-value {
        font-size: 1.25rem;
    }
    .stat-card .stat-label {
        font-size: 0.75rem;
    }
    .stat-card .stat-icon {
        font-size: 1.7rem;
    }

    /* ---- 表格：卡片化模式 ---- */
    .table-wrapper.table-card-mode .table,
    .table-wrapper.table-card-mode .table thead,
    .table-wrapper.table-card-mode .table tbody,
    .table-wrapper.table-card-mode .table th,
    .table-wrapper.table-card-mode .table td,
    .table-wrapper.table-card-mode .table tr {
        display: block;
        width: 100%;
    }

    .table-wrapper.table-card-mode .table thead {
        position: absolute;
        top: -9999px;
        left: -9999px;
    }

    .table-wrapper.table-card-mode .table tr {
        border: 1px solid #e9ecef;
        border-radius: 0.5rem;
        margin-bottom: 0.75rem;
        padding: 0.25rem 0;
        background: #fff;
        box-shadow: 0 1px 2px rgba(0,0,0,0.04);
    }

    .table-wrapper.table-card-mode .table td {
        border: none;
        padding: 0.45rem 0.85rem;
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        text-align: right;
        min-height: 36px;
        border-bottom: 1px solid #f5f6f8;
        gap: 0.75rem;
        word-break: break-all;
    }

    .table-wrapper.table-card-mode .table td:last-child {
        border-bottom: none;
    }

    .table-wrapper.table-card-mode .table td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #495057;
        text-align: left;
        min-width: 60px;
        max-width: 40%;
        flex-shrink: 0;
        font-size: 0.82rem;
        padding-top: 1px;
    }

    /* 表头标题行（第一列）加粗 */
    .table-wrapper.table-card-mode .table td:first-child {
        background: #f8f9fa;
        font-weight: 700;
        font-size: 0.95rem;
        border-radius: 0.5rem 0.5rem 0 0;
        justify-content: flex-start;
        padding: 0.6rem 0.85rem;
    }
    .table-wrapper.table-card-mode .table td:first-child::before {
        display: none;
    }

    /* 操作列：按钮排列优化 */
    .table-wrapper.table-card-mode .table td:last-child {
        flex-direction: column;
        align-items: stretch;
        text-align: left;
    }
    .table-wrapper.table-card-mode .table td:last-child::before {
        margin-bottom: 0.25rem;
    }
    .table-wrapper.table-card-mode .table td:last-child .btn-group {
        display: flex;
        flex-wrap: wrap;
        gap: 4px;
        justify-content: flex-start;
    }
    .table-wrapper.table-card-mode .table td:last-child .btn {
        width: auto;
        flex: none;
    }

    /* ---- 表格：横向滚动模式（默认兜底） ---- */
    .table-wrapper:not(.table-card-mode) {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: 0 -0.75rem;
        padding: 0 0.75rem;
        border-radius: 0;
    }
    .table-wrapper:not(.table-card-mode) .table {
        min-width: 640px;
    }

    /* ---- 按钮组：自动换行 ---- */
    .btn-group, .action-btns {
        flex-wrap: wrap;
    }
    .btn {
        min-height: 36px;
    }

    /* ---- 表单 ---- */
    .row.g-2 > div {
        margin-bottom: 0.5rem;
    }
    .row.g-2 > div:last-child {
        margin-bottom: 0;
    }
    .form-label {
        font-size: 0.85rem;
    }
    .form-control, .form-select {
        min-height: 42px;
        font-size: 16px; /* 防止 iOS 自动缩放 */
    }

    /* ---- 分页：精简 ---- */
    .pagination {
        gap: 1px;
    }
    .pagination .page-link {
        padding: 0.35rem 0.6rem;
        font-size: 0.8rem;
    }
    .pagination .page-item:not(.active):not(.disabled):not(:first-child):not(:last-child) {
        display: none;
    }
    .pagination .page-item:nth-last-child(2),
    .pagination .page-item:nth-child(2) {
        display: block;
    }

    /* ---- 模态框 ---- */
    .modal-dialog {
        margin: 0.5rem;
        max-width: none;
    }
    .modal-content {
        border-radius: 0.6rem;
    }
    .modal-header {
        padding: 0.85rem 1rem;
    }
    .modal-title {
        font-size: 1rem;
    }
    .modal-body {
        padding: 0.85rem 1rem;
    }
    .modal-footer {
        padding: 0.7rem 1rem;
    }
    .modal-footer .btn {
        flex: 1;
        min-height: 42px;
    }

    /* ---- 登录页 ---- */
    .login-page {
        padding: 0.5rem 0;
        align-items: flex-start;
    }
    .login-card {
        margin: 1rem auto;
        padding: 1.2rem 1.1rem !important;
        width: 94%;
    }
    .login-logo {
        font-size: 2.3rem;
    }
    .login-card h4 {
        font-size: 1.1rem;
    }
    .login-card .form-control,
    .login-card .input-group-text {
        min-height: 48px;
    }
    .login-card .btn-lg {
        min-height: 48px;
        font-size: 0.95rem;
    }

    /* ---- Toast ---- */
    .toast-container {
        padding: 0.5rem !important;
        left: 0;
        right: 0;
        max-width: none;
    }
    .toast-container .toast {
        width: 100%;
    }

    /* ---- 数据指标 ---- */
    .metric-box {
        padding: 0.75rem 0.5rem;
    }
    .metric-box .metric-value {
        font-size: 1.15rem;
    }
    .metric-box .metric-label {
        font-size: 0.72rem;
    }

    /* ---- 底部 ---- */
    footer {
        padding: 0.85rem 0.5rem !important;
        font-size: 0.75rem;
    }

    /* ---- Dropdown 菜单：更大触控区 ---- */
    .dropdown-menu {
        font-size: 0.9rem;
    }
    .dropdown-item {
        min-height: 42px;
        display: flex;
        align-items: center;
        padding: 0.55rem 1rem;
    }

    /* ---- input-group ---- */
    .input-group .btn {
        min-height: 38px;
    }
}

/* ==================================================================
 * 小屏手机（≤480px）适配
 * ================================================================== */
@media (max-width: 480px) {
    .login-card {
        padding: 1rem 0.9rem !important;
    }
    .login-logo { font-size: 2rem; }

    .stat-card .stat-value {
        font-size: 1.1rem;
    }
    .stat-card .stat-label {
        font-size: 0.7rem;
    }
    .stat-card .stat-icon {
        font-size: 1.5rem;
    }

    /* 更小的页边距 */
    .content-wrapper {
        padding: 0.65rem !important;
    }

    .card-body {
        padding: 0.65rem;
    }

    .modal-dialog {
        margin: 0.3rem;
    }
    .modal-header, .modal-body, .modal-footer {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
    }
}

/* ==================================================================
 * 暗色模式/打印样式（可选简化）
 * ================================================================== */
@media print {
    .sidebar, .top-navbar, .search-bar, .action-btns, .pagination, footer {
        display: none !important;
    }
    .main-content { margin-left: 0; }
    .content-wrapper { padding: 0; }
    body { background: #fff; }
    .card { box-shadow: none; border: 1px solid #dee2e6; }
}

/* Safari 小修复 */
@supports (-webkit-touch-callout: none) {
    .sidebar {
        position: fixed;
    }
}
