/* ErisPulse Dashboard – dashboard (v1.1 重建：stat 网格 / 双卡行 / 环形监控 / 列表卡) */

/* ════════════════ 核心指标 stat 网格 ════════════════ */
/* auto-fit 自适应：窄屏自动收拢，无空轨道（LangBot/AstrBot 概览首行手法） */
#statGrid.stat-grid,
.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr));
    gap: 14px;
    margin-bottom: 20px;
}

/* stat 瓦片：图标软底方块 + 大数字，radius 14（契约：stat 卡档） */
.stat-card {
    background: var(--bg-t);
    border: 1px solid var(--bd);
    border-radius: 14px;
    padding: 16px 18px;
    display: flex;
    align-items: center;
    gap: 14px;
    text-align: left;
    transition:
        border-color 0.15s var(--ease-smooth),
        transform 0.15s var(--ease-smooth),
        box-shadow 0.15s var(--ease-smooth),
        background-color 0.15s var(--ease-smooth);
}

.stat-card:hover {
    border-color: var(--bd-h);
}

.stat-ico {
    flex: none;
    width: 42px;
    height: 42px;
    border-radius: var(--radius-md);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--accent-soft);
    color: var(--accent);
}

.stat-ico svg {
    width: 21px;
    height: 21px;
}

.stat-meta {
    display: flex;
    flex-direction: column;
    gap: 1px;
    min-width: 0;
}

.stat-val {
    font-size: 26px;
    font-weight: 700;
    color: var(--tx-p);
    line-height: 1.15;
    font-variant-numeric: tabular-nums;
    transition: color 0.15s var(--ease-smooth);
}

.stat-label {
    font-size: 11px;
    color: var(--tx-s);
    font-weight: 500;
    letter-spacing: 0.4px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* 可点击统计卡 - 下钻：hover 上浮 + 边框点亮 + 数字转 accent（QvQChat 手法） */
.stat-card.is-link {
    cursor: pointer;
}
.stat-card.is-link:hover {
    border-color: color-mix(in srgb, var(--accent) 40%, var(--bd));
    background: color-mix(in srgb, var(--accent) 3%, var(--bg-t));
    transform: translateY(-1px);
    box-shadow: var(--shadow-hover);
}
.stat-card.is-link:hover .stat-val {
    color: var(--accent);
}
.stat-card.is-link:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3.5px var(--accent-soft);
}

/* 首载骨架瓦片 */
.stat-card.stat-skel {
    pointer-events: none;
}

.stat-card .skel-ico {
    flex: none;
    width: 42px;
    height: 42px;
    border-radius: var(--radius-md);
    background: var(--bg-s);
    animation: statSkelBreathe 1.6s ease-in-out infinite;
}

.stat-card .skel-lines {
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 0;
    flex: 1;
}

.stat-card .skel-big {
    display: block;
    width: 46%;
    height: 18px;
    border-radius: 999px;
    background: var(--bg-s);
    animation: statSkelBreathe 1.6s ease-in-out infinite;
}

.stat-card .skel-sm {
    display: block;
    width: 70%;
    height: 9px;
    border-radius: 999px;
    background: var(--bg-s);
    animation: statSkelBreathe 1.6s ease-in-out infinite;
}

@keyframes statSkelBreathe {
    0%,
    100% {
        opacity: 1;
    }
    50% {
        opacity: 0.55;
    }
}
@media (prefers-reduced-motion: reduce) {
    .stat-card .skel-ico,
    .stat-card .skel-big,
    .stat-card .skel-sm {
        animation: none;
    }
}

/* ════════════════ 实时事件卡（页面尾区块，限高内部滚动） ════════════════ */
.live-card {
    display: flex;
    flex-direction: column;
}

.dash-events {
    flex: 1;
    min-height: 228px; /* 兜底约 6 行 */
    max-height: 380px; /* 约 10 行（行高 ≈ 38px），内部滚动 */
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 4px 0;
}

/* ── 性能卡（全宽单行）：环区居左 + 指标格居右铺满剩余宽度，自然高度 ── */
.perf-card {
    margin-bottom: 14px;
}

.perf-card .card-body.perf-card-body {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: clamp(20px, 3vw, 36px);
    padding: 6px clamp(14px, 1.8vw, 18px) 16px;
}

/* 环区：固定内容宽度靠左，不被拉伸 */
.perf-card .card-body.perf-grid,
.perf-grid {
    display: grid;
    grid-template-columns: auto auto;
    gap: 20px;
    align-items: center;
    flex: none;
}

.perf-section {
    position: relative;
    cursor: pointer;
    padding: 10px 8px 6px;
    border-radius: var(--radius-md);
    border: 1px solid transparent;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    transition:
        border-color 0.15s var(--ease-smooth),
        background-color 0.15s var(--ease-smooth);
}

.perf-section:hover {
    border-color: var(--bd);
    background: var(--bg-s);
}
.perf-section:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3.5px var(--accent-soft);
}

.perf-ring-label {
    font-size: 12px;
    color: var(--tx-s);
    text-align: center;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 6px;
    justify-content: center;
}

.perf-ring-val {
    font-weight: 600;
    color: var(--tx-p);
    font-variant-numeric: tabular-nums;
}

.circular-progress {
    width: 80px;
    height: 80px;
    flex-shrink: 0;
}

.circular-progress circle {
    fill: none;
    stroke-width: 4;
    transition: stroke-dashoffset 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}

.circular-progress-bg {
    stroke: var(--bg-s);
}

.circular-progress-fill {
    stroke: var(--accent);
    stroke-linecap: round;
    transform: rotate(-90deg);
    transform-origin: 50% 50%;
}

.circular-progress-fill.high {
    stroke: var(--wr-c);
}

.circular-progress-fill.critical {
    stroke: var(--er-c);
}

.circular-progress-text {
    font-size: 15px;
    font-weight: 700;
    fill: var(--tx-p);
    text-anchor: middle;
    dominant-baseline: central;
}

/* ── 轻量指标格：2 列自适应小格（label 上 / value 下），格间 14px gap，无通栏分隔线 ── */
/* ── 轻量指标格：居右铺满剩余宽度（auto-fit 4-5 列自适应小格，无分隔线） ── */
.perf-extra {
    flex: 1;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(96px, 1fr));
    gap: 14px;
    min-width: 0;
}

.perf-extra:empty {
    display: none;
}

.perf-extra-cell {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    background: var(--bg-s);
}

.perf-extra-label {
    font-size: 12px;
    color: var(--tx-s);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.perf-extra-val {
    font-size: 13px;
    font-weight: 500;
    color: var(--tx-p);
    font-variant-numeric: tabular-nums;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ── 实时事件卡：卡底跳转脚注 ── */
.dash-events .skeleton {
    margin: 10px 18px;
}

.dash-events-foot {
    border-top: 1px solid color-mix(in srgb, var(--bd) 50%, transparent);
    padding: 8px 16px 10px;
    display: flex;
    justify-content: flex-end;
    margin-top: auto;
}

.dash-events-more {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 12px;
}

/* ════════════════ 消息统计卡 ════════════════ */
/* 适配器/模块双列列表区（grid-2 栅格由 overrides 提供）：
   stretch 等高使双卡底线对齐；两卡内容量相近，等高差值由列表区 flex:1 吸收 */
.dash-list-grid {
    align-items: stretch;
    margin-bottom: 14px;
}

/* 卡体纵向弹性：card-body 即列表区，等高产生的余量全部留在列表区内部 */
.dash-list-grid > .card {
    display: flex;
    flex-direction: column;
}

.dash-list-grid > .card > .card-body {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

/* 短卡呈现空态时垂直居中，等高不再表现为卡底悬空 */
.dash-list-grid .empty-state {
    margin: auto 0;
}

/* 页面尾卡：消息统计（下接实时事件卡） */
.msg-stats-card {
    min-width: 0;
    margin-bottom: 14px;
}

.dash-sub-label {
    font-size: 12px;
    font-weight: 600;
    color: var(--tx-s);
    margin-bottom: 10px;
    letter-spacing: 0.2px;
}

.msg-trend-block {
    border-top: 1px solid color-mix(in srgb, var(--bd) 55%, transparent);
    padding-top: 14px;
    margin-top: 14px;
}

/* 适配器/模块列表行内文字 */
.dash-list-name {
    flex: 1;
    min-width: 0;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.dash-list-sub {
    flex: none;
    font-size: 12px;
    color: var(--tx-s);
    font-variant-numeric: tabular-nums;
}

.dash-list-chip {
    flex: none;
    min-width: 58px;
    justify-content: center;
    text-transform: capitalize;
}

/* ════════════════ 性能详情浮层（popover） ════════════════ */
.popover {
    position: fixed;
    z-index: 1000;
    min-width: 260px;
    max-width: 320px;
    max-height: 80vh;
    overflow-y: auto;
    background: var(--bg-t);
    border: 1px solid var(--bd);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-hover);
    padding: 16px;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    scrollbar-width: thin;
    transition:
        background-color 0.15s var(--ease-smooth),
        color 0.15s var(--ease-smooth),
        border-color 0.15s var(--ease-smooth);
}

.popover::-webkit-scrollbar {
    width: 4px;
}

.popover::-webkit-scrollbar-thumb {
    background: var(--bd);
    border-radius: 2px;
}

.popover::-webkit-scrollbar-track {
    background: transparent;
}

.popover.show {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    animation: popoverFadeIn 0.2s var(--ease-soft-spring);
}

@keyframes popoverFadeIn {
    from {
        opacity: 0;
        transform: translateY(4px) scale(0.98);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.popover-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid color-mix(in srgb, var(--bd) 55%, transparent);
}

.popover-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--tx-p);
}

.popover-close {
    width: 24px;
    height: 24px;
    border: none;
    background: none;
    color: var(--tx-s);
    cursor: pointer;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition:
        background-color 0.15s var(--ease-smooth),
        color 0.15s var(--ease-smooth);
}

.popover-close:hover {
    background: var(--bg-s);
    color: var(--tx-p);
}

.popover-content {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.popover-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 10px;
    transition: background-color 0.15s var(--ease-smooth);
    border-radius: var(--radius-sm);
}

.popover-item:hover {
    background: var(--bg-s);
}

.popover-item-label {
    font-size: 12px;
    color: var(--tx-s);
}

.popover-item-value {
    font-size: 13px;
    font-weight: 600;
    color: var(--tx-p);
    font-variant-numeric: tabular-nums;
}

/* ════════════════ 消息统计图表（api-routes.js loadMessageStats 注入） ════════════════ */
.stat-bar-chart {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 10px;
}

.stat-bar-chart:last-child {
    margin-bottom: 2px;
}

.stat-bar-label {
    min-width: 60px;
    font-size: 12px;
    font-weight: 500;
    color: var(--tx-s);
    text-align: right;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stat-bar-track {
    flex: 1;
    height: 6px;
    background: var(--bg-s);
    border-radius: 3px;
    overflow: hidden;
    min-width: 0;
}

.stat-bar-fill {
    height: 100%;
    border-radius: 3px;
    transition: width 0.5s var(--ease-smooth);
    min-width: 2px;
    background: var(--accent);
}

.stat-bar-value {
    min-width: 48px;
    font-size: 11px;
    color: var(--tx-s);
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* 24小时趋势柱状图 */
.hourly-chart {
    display: flex;
    align-items: flex-end;
    gap: 3px;
    height: 84px;
}

.hourly-bar-wrap {
    flex: 1;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
}

.hourly-bar {
    width: 100%;
    border-radius: var(--radius-xs) var(--radius-xs) 0 0;
    background: color-mix(in srgb, var(--accent) 62%, transparent);
    transition:
        opacity 0.15s var(--ease-smooth),
        background-color 0.15s var(--ease-smooth);
}

.hourly-bar-wrap:hover .hourly-bar {
    background: var(--accent);
}

.hourly-labels {
    display: flex;
    justify-content: space-between;
    padding: 6px 2px 0;
}

.hourly-label {
    font-size: 10px;
    color: var(--tx-t);
    font-variant-numeric: tabular-nums;
}

/* ════════════════ 快捷导航 pins ════════════════ */
.home-section-hd {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid color-mix(in srgb, var(--bd) 35%, transparent);
}

.home-section-hd > span {
    font-size: 13px;
    font-weight: 600;
    letter-spacing: -0.01em;
    color: var(--tx-p);
}

.home-pins {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 168px), 1fr));
    gap: 10px;
    margin-bottom: 22px;
}

.home-pin {
    position: relative;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    border-radius: var(--radius-md);
    border: 1px solid var(--bd);
    background: var(--bg-t);
    color: var(--tx-p);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition:
        border-color 0.15s var(--ease-smooth),
        color 0.15s var(--ease-smooth),
        background-color 0.15s var(--ease-smooth),
        transform 0.15s var(--ease-smooth);
}

.home-pin:hover {
    border-color: color-mix(in srgb, var(--accent) 40%, var(--bd));
    color: var(--accent);
    background: var(--accent-soft);
    transform: translateY(-1px);
}

.home-pin svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

.home-pin-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.home-pin.editing {
    cursor: grab;
}

.home-pin.editing:active {
    cursor: grabbing;
}

.home-pin-grip {
    color: var(--tx-t);
    font-size: 12px;
    letter-spacing: -2px;
    opacity: 0;
    flex-shrink: 0;
    width: 0;
    overflow: hidden;
    transition:
        opacity 0.15s var(--ease-smooth),
        width 0.15s var(--ease-smooth);
}

.home-pin.editing .home-pin-grip {
    opacity: 1;
    width: 12px;
}

.home-pin.dragging {
    opacity: 0.4;
}

.home-pin.drag-over {
    border-color: var(--accent);
    transform: none;
}

.home-pin-remove {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    border: none;
    background: var(--er-c);
    color: var(--fg-on-accent, #fff);
    font-size: 13px;
    line-height: 1;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
}

.home-pin.editing .home-pin-remove {
    display: flex;
}

.home-pin-section {
    position: relative;
}

.home-pin-add {
    position: absolute;
    left: 50%;
    transform: translateX(-50%); /* 居中（属性曾被剥落导致按钮偏右） */
    bottom: -18px;
    z-index: 10;
}

.home-pin-picker {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    bottom: calc(100% + 8px);
    /* 上限约束在内容宽度内：100vw 含滚动条与侧栏占位，直接引用会造成横向溢出 */
    max-width: min(340px, calc(100vw - 32px));
    padding: 8px;
    border: 1px solid var(--bd);
    border-radius: var(--radius-md);
    background: var(--bg-t);
    box-shadow: var(--shadow-hover);
    display: grid;
    grid-template-columns: 1fr; /* 单列列表：页面/子 tab 层级用缩进表达 */
    gap: 2px;
    max-height: 260px;
    min-width: 230px;
    overflow-y: auto;
    overflow-x: hidden;
}

.home-pin-option {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 13px;
    color: var(--tx-p);
    transition: background-color 0.15s var(--ease-smooth);
}

.home-pin-option:hover {
    background: var(--bg-s);
}

.home-pin-option svg {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
}

/* Pin 选择器子 tab */
.home-pin-subgroup {
    margin-top: 4px;
    padding-left: 12px;
    border-left: 2px solid var(--bd);
}

.home-pin-suboption {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px 6px 16px;
    border-radius: var(--radius-xs);
    cursor: pointer;
    font-size: 12px;
    color: var(--tx-s);
    transition: background-color 0.15s var(--ease-smooth);
}

.home-pin-suboption:hover {
    background: var(--bg-s);
    color: var(--tx-p);
}

.home-pin-suboption svg {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
    opacity: 0.7;
}

.home-pin-group-label {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    font-size: 12px;
    font-weight: 600;
    color: var(--tx-t);
    cursor: default;
    margin-top: 4px;
}

.home-pin-group-label svg {
    width: 14px;
    height: 14px;
}

/* ════════════════ tab 区容器（跨页通用） ════════════════ */
.tab-section {
    display: none;
}

.tab-section.active {
    display: block;
}

/* ════════════════ 响应式 ════════════════ */
/* <960px：性能卡竖排回退（环区上/指标格下）、适配器+模块单列堆叠（DOM 源序即视觉序） */
@media (max-width: 960px) {
    .perf-card .card-body.perf-card-body {
        flex-direction: column;
        align-items: stretch;
        gap: 12px;
    }

    .perf-card .card-body.perf-grid,
    .perf-grid {
        grid-template-columns: 1fr 1fr;
    }

    .dash-list-grid {
        grid-template-columns: 1fr;
    }
}

/* <768px：仪表盘移动端整页适配（规则全部收在本页与媒体查询内，桌面布局不动） */
@media (max-width: 768px) {
    /* 四边统一 16px 边距（id 优先级覆盖 overrides 后置的 .page 12px 档） */
    #p-dashboard {
        padding: 16px;
    }

    /* 统计网格：强制 2 列铺满（auto-fit 200px 起步在窄屏会塌成 1 列），
       网格间距与瓦片内距收紧，22/11 大小字号层级保持一眼可扫 */
    #statGrid.stat-grid,
    #p-dashboard .stat-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 10px;
        margin-bottom: 14px;
    }

    #p-dashboard .stat-card {
        padding: 12px 14px;
        gap: 10px;
    }

    .stat-val {
        font-size: 22px;
    }

    .stat-ico {
        width: 38px;
        height: 38px;
    }

    .stat-ico svg {
        width: 19px;
        height: 19px;
    }

    /* 性能卡竖排（<960px 已环上格下）：收紧环区与指标格纵向节奏 */
    .perf-card .card-body.perf-card-body {
        gap: 14px;
        padding: 4px 12px 14px;
    }

    .perf-card .perf-grid,
    .perf-grid {
        gap: 12px;
    }

    .perf-section {
        padding: 8px 6px 4px;
    }

    #p-dashboard .perf-extra {
        gap: 10px;
    }

    /* 环体尺寸需以 id 优先级盖过 overrides 后置的 .circular-progress 56px */
    #p-dashboard .circular-progress {
        width: 68px;
        height: 68px;
    }

    .circular-progress-text {
        font-size: 13px;
    }

    /* 快捷 pin：改横滑轨道（拇指滑动不折行），pin 定宽、标签省略、近距吸附 */
    #p-dashboard .home-pins {
        display: flex;
        gap: 10px;
        overflow-x: auto;
        overscroll-behavior-x: contain;
        scroll-snap-type: x proximity;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        padding-bottom: 2px;
    }

    #p-dashboard .home-pins::-webkit-scrollbar {
        display: none;
    }

    #p-dashboard .home-pin {
        flex: 0 0 auto;
        min-width: 136px;
        max-width: 60vw;
        scroll-snap-align: start;
    }

    /* 实时事件：上限收小至约 6 行（行高约 40px），缩短移动端首屏 */
    .dash-events {
        min-height: 120px;
        max-height: 240px;
    }

    .dash-events-foot {
        padding: 4px 12px 6px;
    }

    /* 触屏命中区 ≥40px：图标按钮/文字链接放大到可点尺寸，仅本页生效 */
    #p-dashboard .btn-icon,
    #p-dashboard .btn-icon-sm {
        min-width: 40px;
        min-height: 40px;
    }

    #p-dashboard .home-pin-add .btn {
        min-height: 40px;
    }

    #p-dashboard .dash-events-more {
        min-height: 40px;
        padding: 8px 12px;
    }

    /* popover 在触屏上由环区点开，关闭键同步放大 */
    #p-dashboard .popover-close {
        width: 40px;
        height: 40px;
    }

    /* pin 移除点：视觉 24px + 外扩 8px 透明命中区凑足 40px */
    #p-dashboard .home-pin-remove {
        width: 24px;
        height: 24px;
        font-size: 14px;
    }

    #p-dashboard .home-pin-remove::after {
        content: "";
        position: absolute;
        inset: -8px;
    }

    .popover {
        min-width: 220px;
        max-width: 280px;
    }
}

@media (max-width: 480px) {
    /* 窄档环体再收一档：68 → 56（id 优先级与上方 768 档对齐才能覆盖） */
    #p-dashboard .circular-progress {
        width: 56px;
        height: 56px;
    }

    .circular-progress-text {
        font-size: 11px;
    }

    .popover {
        min-width: 200px;
        max-width: 260px;
        padding: 12px;
    }

    .popover-item {
        padding: 6px 8px;
    }

    .popover-item-label,
    .popover-item-value {
        font-size: 11px;
    }
}
