<script setup>
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import * as echarts from 'echarts'
import { Odometer, CircleCheckFilled, VideoCameraFilled, WarnTriangleFilled } from '@element-plus/icons-vue'
import { convertPipeline, convertPoints } from '@/utils/coordTransform'
import {
  getAlarmStats,
  getWaterFacilityFeatures,
  getWaterFacilityOverview,
  getDeviceGisAllPoints,
  getDeviceGisStatistics,
  listRealtimeAlarms,
  listDeviceLedger,
  listFlowMonitoring,
  listPressureMonitoring,
  listLeakageMonitoring
} from '@/api/pipeNetwork/waterSupply'
import { normalizeGeoJson, normalizeMonitoringRecord, normalizePage, unwrapResponse } from '@/utils/waterSupplyModel'

const nowText = ref('')
const weekText = ref('')
const mapError = ref('')
const loading = ref(false)

const dashboardData = ref({})
const pipelineLines = ref([])
const pipePoints = ref([])

const facilityTypes = [
  { label: '水源地', value: 'source', color: '#38a3ff' },
  { label: '水厂', value: 'plant', color: '#38d0a0' },
  { label: '泵站', value: 'pumpStation', color: '#ffc15e' },
  { label: '管网', value: 'pipe', color: '#7b9bb8' },
  { label: '用水户', value: 'user', color: '#ff7894' }
]

const typeCount = type => {
  const row = (dashboardData.value.facilityOverview || []).find(item => item.facilityType === type)
  return Number(row?.totalCount ?? row?.count ?? 0)
}

const baseCards = computed(() => {
  const fs = dashboardData.value.facilityOverview || []
  const es = dashboardData.value.equipmentStats || {}
  const alarm = dashboardData.value.alarmStats || {}
  const totalFacility = fs.reduce((sum, item) => sum + Number(item.totalCount || 0), 0)
  const totalDevice = Number(es.total ?? es.totalDeviceCount ?? 0)
  const onlineDevice = Number(es.online ?? es.onlineCount ?? 0)
  const realtimeAlarm = Number(alarm.realtimeAlarmCount ?? alarm.realtimeCount ?? 0)
  return [
    { label: '设施总数', value: totalFacility, unit: '处', icon: Odometer, color: '#60b8ff' },
    { label: '设备总数', value: totalDevice, unit: '台', icon: CircleCheckFilled, color: '#52e0a8' },
    { label: '在线设备', value: onlineDevice, unit: '台', icon: VideoCameraFilled, color: '#ff8e8e' },
    { label: '实时报警', value: realtimeAlarm, unit: '条', icon: WarnTriangleFilled, color: '#ffb54d' }
  ]
})

const operationSummary = computed(() => {
  return [
    { label: '供水管网', value: typeCount('pipe') + '条' },
    { label: '水厂', value: typeCount('plant') + '座' },
    { label: '泵站', value: typeCount('pumpStation') + '座' }
  ]
})

const repairRecords = computed(() => {
  const list = dashboardData.value.facilityOverview || []
  if (list.length) {
    return list.map(item => ({ month: item.facilityTypeName || item.facilityType || '设施', value: Number(item.totalCount || 0) }))
  }
  return [
    { month: '水源地', value: 0 }, { month: '水厂', value: 0 }, { month: '泵站', value: 0 },
    { month: '管网', value: 0 }, { month: '用水户', value: 0 }
  ]
})

const pointRows = computed(() => {
  const list = dashboardData.value.pointRows || []
  return list.length > 0 ? list : [{ id: 1, name: '暂无数据', time: '--', value: '--', status: '离线' }]
})

const equipmentRows = computed(() => {
  const list = dashboardData.value.equipmentRows || []
  return list.length > 0 ? list : [{ id: 1, name: '暂无数据', code: '--', location: '--', status: '--' }]
})

const warningRows = computed(() => {
  const list = dashboardData.value.warningRows || []
  return list.length > 0 ? list : [{ level: '中', title: '暂无告警', code: '--', time: '--:--:--', desc: '当前无告警信息', tag: '正常' }]
})

const hazardSummary = computed(() => {
  const alarm = dashboardData.value.alarmStats || {}
  return [
    { label: '实时报警', value: alarm.realtimeAlarmCount ?? alarm.realtimeCount ?? 0, color: '#ff6d79' },
    { label: '今日报警', value: alarm.todayAlarmCount ?? alarm.todayCount ?? 0, color: '#ffb54d' },
    { label: '异常率', value: alarm.deviceAbnormalRate ?? alarm.abnormalRate ?? 0, color: '#f3e64d' }
  ]
})

const mapKpis = computed(() => {
  const es = dashboardData.value.equipmentStats || {}
  const alarm = dashboardData.value.alarmStats || {}
  const totalDevice = Number(es.total ?? es.totalDeviceCount ?? 0)
  const onlineDevice = Number(es.online ?? es.onlineCount ?? 0)
  return [
    { label: '设备在线率', value: totalDevice > 0 ? (onlineDevice / totalDevice * 100).toFixed(1) + '%' : '--' },
    { label: '供水设施', value: dashboardData.value.facilityOverview?.reduce((sum, item) => sum + Number(item.totalCount || 0), 0) || 0 },
    { label: '今日报警', value: (alarm.todayAlarmCount ?? alarm.todayCount ?? 0) + '条' }
  ]
})

const mapWrapRef = ref(null)
const repairChartRef = ref(null)
const hazardChartRef = ref(null)
let mapInstance = null
let repairChart = null
let hazardChart = null
let clockTimer = null

function formatWeek(day) {
  return ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'][day]
}

function pad(value) {
  return String(value).padStart(2, '0')
}

function updateClock() {
  const now = new Date()
  nowText.value = `${now.getFullYear()}/${pad(now.getMonth() + 1)}/${pad(now.getDate())} ${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`
  weekText.value = formatWeek(now.getDay())
}

function initRepairChart() {
  if (!repairChartRef.value) return
  repairChart?.dispose()
  repairChart = echarts.init(repairChartRef.value)
  const data = repairRecords.value
  repairChart.setOption({
    grid: { left: 30, right: 12, top: 20, bottom: 24, containLabel: true },
    xAxis: {
      type: 'category',
      data: data.map(item => item.month),
      axisLine: { lineStyle: { color: 'rgba(91, 167, 219, 0.8)' } },
      axisLabel: { color: '#d2efff' }
    },
    yAxis: {
      type: 'value',
      splitLine: { lineStyle: { color: 'rgba(91, 167, 219, 0.18)' } },
      axisLine: { show: false },
      axisLabel: { color: '#d2efff' }
    },
    tooltip: { trigger: 'axis' },
    series: [{
      type: 'bar',
      barWidth: 24,
      data: data.map(item => item.value),
      itemStyle: {
        borderRadius: [6, 6, 0, 0],
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: '#38d8ff' },
          { offset: 1, color: '#166dff' }
        ])
      }
    }]
  })
}

function initHazardChart() {
  if (!hazardChartRef.value) return
  hazardChart?.dispose()
  hazardChart = echarts.init(hazardChartRef.value)
  const data = hazardSummary.value
  hazardChart.setOption({
    grid: { left: 18, right: 18, top: 10, bottom: 10, containLabel: true },
    xAxis: {
      type: 'value',
      splitLine: { show: false },
      axisLine: { show: false },
      axisTick: { show: false },
      axisLabel: { show: false }
    },
    yAxis: {
      type: 'category',
      data: data.map(item => item.label),
      axisLine: { show: false },
      axisTick: { show: false },
      axisLabel: { color: '#d2efff', fontSize: 13 }
    },
    tooltip: { trigger: 'axis', axisPointer: { type: 'shadow' } },
    series: [{
      type: 'bar',
      data: data.map(item => ({ value: item.value, itemStyle: { color: item.color, borderRadius: 10 } })),
      barWidth: 14,
      label: { show: true, position: 'right', color: '#fff', fontWeight: 'bold' },
      showBackground: true,
      backgroundStyle: { color: 'rgba(255,255,255,0.08)', borderRadius: 10 }
    }]
  })
}

function waitForBMapGL() {
  return new Promise((resolve, reject) => {
    if (window.BMapGL) {
      resolve(window.BMapGL)
      return
    }
    let attempts = 0
    const timer = setInterval(() => {
      attempts += 1
      if (window.BMapGL) {
        clearInterval(timer)
        resolve(window.BMapGL)
        return
      }
      if (attempts >= 100) {
        clearInterval(timer)
        reject(new Error('百度地图加载超时'))
      }
    }, 200)
  })
}

function addPipeLine(BMapGL, pipe) {
  const bmapPoints = pipe.points.map(([lng, lat]) => new BMapGL.Point(lng, lat))
  const polyline = new BMapGL.Polyline(bmapPoints, {
    strokeColor: pipe.color,
    strokeWeight: 4,
    strokeOpacity: 0.95
  })
  mapInstance.addOverlay(polyline)

  bmapPoints.forEach((point) => {
    const ring = new BMapGL.Circle(point, 14, {
      strokeColor: pipe.color,
      strokeWeight: 2,
      strokeOpacity: 0.7,
      fillColor: 'rgba(56,182,233,0.08)',
      fillOpacity: 0.18
    })
    mapInstance.addOverlay(ring)
  })
}

function addPipePoint(BMapGL, point) {
  const markerPoint = new BMapGL.Point(point.lng, point.lat)
  const marker = new BMapGL.Marker(markerPoint)
  mapInstance.addOverlay(marker)
  const label = new BMapGL.Label(`${point.name} ${point.value}`, { offset: new BMapGL.Size(-42, -40) })
  label.setPosition(markerPoint)
  label.setStyle({
    color: '#d8faff',
    background: 'rgba(18, 68, 112, 0.75)',
    border: '1px solid rgba(56, 182, 233, 0.55)',
    borderRadius: '8px',
    padding: '5px 8px',
    fontSize: '12px'
  })
  mapInstance.addOverlay(label)
}

async function initMap() {
  if (!mapWrapRef.value) return
  try {
    const BMapGL = await waitForBMapGL()
    mapInstance = new BMapGL.Map(mapWrapRef.value, { enableMapClick: true })
    const center = new BMapGL.Point(110.3965, 28.4638)
    mapInstance.centerAndZoom(center, 15)
    mapInstance.enableScrollWheelZoom(true)
    mapInstance.setMapStyleV2({ styleId: 'a0f4e6e6f9e7a6c5d8e7f7e7f7e7f7e5' })

    const bdPipelineLines = pipelineLines.value.map(pipe => convertPipeline(pipe))
    const bdPipePoints = convertPoints(pipePoints.value)
    bdPipelineLines.forEach(pipe => addPipeLine(BMapGL, pipe))
    bdPipePoints.forEach(point => addPipePoint(BMapGL, point))
  } catch (error) {
    mapError.value = '百度地图加载失败，请检查地图脚本或网络连接。'
  }
}

function handleResize() {
  repairChart?.resize()
  hazardChart?.resize()
}

function parseFeatures(features) {
  const lines = []
  const points = []
  ;(features || []).forEach(feature => {
    const coordinates = feature.geometry?.coordinates || []
    const properties = feature.properties || {}
    if (feature.geometry?.type === 'LineString' && coordinates.length > 1) {
      lines.push({
        id: feature.id || `line-${lines.length}`,
        name: properties.facilityTypeName || properties.name || '供水管网',
        color: '#37b6e9',
        points: coordinates
      })
    }
    if (feature.geometry?.type === 'Point' && coordinates.length >= 2) {
      points.push({
        id: feature.id || properties.id,
        name: properties.facilityName || properties.name || properties.facilityTypeName || '供水设施',
        value: properties.facilityTypeName || properties.facilityType || '',
        lng: coordinates[0],
        lat: coordinates[1],
        type: properties.facilityType,
        properties
      })
    }
  })
  pipelineLines.value = lines
  pipePoints.value = points
}

function buildPointRows(flowPage, pressurePage, leakagePage) {
  const flow = normalizePage(flowPage).records.map(item => normalizeMonitoringRecord(item, 'flow'))
  const pressure = normalizePage(pressurePage).records.map(item => normalizeMonitoringRecord(item, 'pressure'))
  const leakage = normalizePage(leakagePage).records.map(item => normalizeMonitoringRecord(item, 'power'))
  return [...flow, ...pressure, ...leakage].slice(0, 8).map(item => ({
    id: item.id,
    name: item.equipmentName,
    time: item.updateTime,
    value: item.value == null ? '--' : item.value,
    status: item.status === 1 || item.status === '1' ? '预警' : '在线'
  }))
}

async function loadDashboardData() {
  loading.value = true
  try {
    const [overviewResponse, featuresResponse, devicePointsResponse, deviceStatsResponse, alarmResponse, alarmSummaryResponse, devicePageResponse, flowResponse, pressureResponse, leakageResponse] = await Promise.all([
      getWaterFacilityOverview(),
      getWaterFacilityFeatures(),
      getDeviceGisAllPoints(),
      getDeviceGisStatistics(),
      listRealtimeAlarms(),
      getAlarmStats(),
      listDeviceLedger({ pageNum: 1, pageSize: 8 }),
      listFlowMonitoring({ pageNum: 1, pageSize: 5 }),
      listPressureMonitoring({ pageNum: 1, pageSize: 5 }),
      listLeakageMonitoring({ pageNum: 1, pageSize: 5 })
    ])
    const overviewData = unwrapResponse(overviewResponse)
    const facilityOverview = Array.isArray(overviewData) ? overviewData : (overviewData?.facilityOverview || [])
    const features = normalizeGeoJson(featuresResponse).features || []
    const deviceData = unwrapResponse(devicePointsResponse)
    const deviceRows = Array.isArray(deviceData) ? deviceData : (deviceData?.features || deviceData?.records || [])
    const devicePage = normalizePage(devicePageResponse)
    const rawStats = unwrapResponse(deviceStatsResponse) || {}
    const typeStatistics = rawStats.typeStatistics || []
    const flatten = list => (Array.isArray(list) ? list : []).reduce((sum, item) => sum + Number(item.deviceCount || item.count || 0) + flatten(item.children), 0)
    const equipmentStats = {
      ...rawStats,
      total: Number(rawStats.total ?? rawStats.totalDeviceCount ?? 0) || flatten(typeStatistics) || deviceRows.length,
      online: Number(rawStats.online ?? rawStats.onlineCount ?? deviceRows.filter(item => Number(item.onlineStatus) === 1).length),
      abnormal: Number(rawStats.abnormal ?? rawStats.abnormalCount ?? deviceRows.filter(item => Number(item.alarmStatus) === 1).length)
    }
    const alarmList = Array.isArray(unwrapResponse(alarmResponse)) ? unwrapResponse(alarmResponse) : []
    const alarmStats = unwrapResponse(alarmSummaryResponse) || {}
    const equipmentRows = devicePage.records.slice(0, 8).map(item => ({
      id: item.equipmentId || item.id,
      name: item.equipmentName || item.name || '-',
      code: item.equipmentCode || item.code || '-',
      location: item.equipmentLocation || item.location || '-',
      status: Number(item.status || item.onlineStatus) === 1 ? '异常' : '正常'
    }))
    const warningRows = alarmList.slice(0, 6).map(item => ({
      level: Number(item.alarmLevel) <= 2 ? '高' : '中',
      title: item.deviceName || item.deviceCode || '监测设备',
      code: item.warningType || item.alarmCode || '-',
      time: item.alarmTime || '--',
      desc: item.equipmentLocation || '报警详情待查看',
      tag: Number(item.alarmLevel) <= 2 ? '紧急' : '一般'
    }))
    dashboardData.value = {
      facilityOverview,
      equipmentStats,
      alarmStats,
      warningRows,
      equipmentRows,
      pointRows: buildPointRows(flowResponse, pressureResponse, leakageResponse)
    }
    parseFeatures(features)
  } catch (error) {
    console.warn('供水首页数据加载失败', error)
    dashboardData.value = { facilityOverview: [], equipmentStats: {}, alarmStats: {}, warningRows: [], equipmentRows: [], pointRows: [] }
    pipelineLines.value = []
    pipePoints.value = []
  } finally {
    loading.value = false
  }
}

onMounted(async () => {
  updateClock()
  clockTimer = window.setInterval(updateClock, 1000)
  await loadDashboardData()
  await nextTick()
  initRepairChart()
  initHazardChart()
  initMap()
  window.addEventListener('resize', handleResize)
})

onBeforeUnmount(() => {
  if (clockTimer) window.clearInterval(clockTimer)
  window.removeEventListener('resize', handleResize)
  repairChart?.dispose()
  hazardChart?.dispose()
  mapInstance = null
})
</script>


<template>
  <div class="gas-home-screen">
    <div class="screen-header">
      <div class="header-time">{{ nowText }} {{ weekText }}</div>
      <div class="header-title-wrap">
        <div class="header-title">供水管网安全运行监测系统</div>
        <div class="header-subtitle">Water Supply Operation Dashboard</div>
      </div>
      <div class="header-tags">
        <span class="header-tag">实时监测</span>
        <span class="header-tag">供水设施</span>
        <span class="header-tag">报警预警</span>
      </div>
    </div>

    <div class="screen-body">
      <div class="side-panel left-panel">
        <section class="panel-card base-card">
          <div class="panel-title">基础概况</div>
          <div class="base-grid">
            <div v-for="item in baseCards" :key="item.label" class="base-item">
              <div class="base-icon" :style="{ background: item.color }">
                <component :is="item.icon" class="base-icon-svg" />
              </div>
              <div class="base-text">
                <div class="base-label">{{ item.label }}</div>
                <div class="base-value">{{ item.value }}<span>{{ item.unit }}</span></div>
              </div>
            </div>
          </div>
        </section>

        <section class="panel-card">
          <div class="panel-title">设施统计</div>
          <div ref="repairChartRef" class="chart-box repair-chart"></div>
        </section>

        <section class="panel-card table-card">
          <div class="panel-title">监测点数据</div>
          <div class="table-wrap">
            <table class="data-table">
              <thead>
                <tr>
                  <th>序号</th>
                  <th>监测点</th>
                  <th>时间</th>
                  <th>数值</th>
                  <th>状态</th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="row in pointRows" :key="row.id">
                  <td>{{ row.id }}</td>
                  <td>{{ row.name }}</td>
                  <td>{{ row.time }}</td>
                  <td>{{ row.value }}</td>
                  <td :class="['status-cell', row.status === '在线' ? 'ok' : row.status === '预警' ? 'warn' : 'off']">{{ row.status }}</td>
                </tr>
              </tbody>
            </table>
          </div>
        </section>
      </div>

      <div class="center-panel">
        <div class="map-stage">
          <div class="map-stage-top">
            <div class="summary-strip">
              <div v-for="item in operationSummary" :key="item.label" class="summary-item">
                <span class="summary-label">{{ item.label }}</span>
                <strong class="summary-value">{{ item.value }}</strong>
              </div>
            </div>
            <div class="map-kpis">
              <div v-for="item in mapKpis" :key="item.label" class="map-kpi">
                <span>{{ item.label }}</span>
                <strong>{{ item.value }}</strong>
              </div>
            </div>
          </div>

          <div ref="mapWrapRef" class="map-view"></div>
          <div v-if="mapError" class="map-error">{{ mapError }}</div>

          <div class="map-panel info-panel">
            <div class="mini-title">供水设施信息</div>
            <div class="mini-row"><span>供水管网</span><strong>{{ typeCount('pipe') }}条</strong></div>
            <div class="mini-row"><span>水厂</span><strong>{{ typeCount('plant') }}座</strong></div>
            <div class="mini-row"><span>泵站</span><strong>{{ typeCount('pumpStation') }}座</strong></div>
          </div>

          <div class="map-panel point-panel">
            <div class="mini-title">当前监测信息</div>
            <div class="mini-row"><span>监测点总数</span><strong>{{ pointRows.length }}个</strong></div>
            <div class="mini-row"><span>在线设备</span><strong>{{ dashboardData.equipmentStats?.online || 0 }}台</strong></div>
            <div class="mini-row"><span>实时报警</span><strong>{{ dashboardData.alarmStats?.realtimeAlarmCount ?? dashboardData.alarmStats?.realtimeCount ?? 0 }}条</strong></div>
          </div>
        </div>
      </div>

      <div class="side-panel right-panel">
        <section class="panel-card">
          <div class="panel-title">设备运行</div>
          <div class="table-wrap equipment-wrap">
            <table class="data-table compact-table">
              <thead>
                <tr>
                  <th>序号</th>
                  <th>设备名称</th>
                  <th>设备编号</th>
                  <th>位置</th>
                  <th>状态</th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="row in equipmentRows" :key="row.id">
                  <td>{{ row.id }}</td>
                  <td>{{ row.name }}</td>
                  <td>{{ row.code }}</td>
                  <td>{{ row.location }}</td>
                  <td>{{ row.status }}</td>
                </tr>
              </tbody>
            </table>
          </div>
        </section>

        <section class="panel-card warning-card">
          <div class="panel-title">告警动态</div>
          <div class="warning-scroll">
            <div class="warning-list">
              <div v-for="(item, index) in warningRows" :key="index" class="warning-item" :class="item.level === '高' ? 'danger' : 'notice'">
                <div class="warning-side">
                  <div class="warning-badge">{{ item.level }}</div>
                  <div class="warning-tag">{{ item.tag }}</div>
                </div>
                <div class="warning-main">
                  <div class="warning-title-row">
                    <span class="warning-title">{{ item.title }}</span>
                    <span class="warning-time">{{ item.time }}</span>
                  </div>
                  <div class="warning-code">{{ item.code }}</div>
                  <div class="warning-desc">{{ item.desc }}</div>
                </div>
              </div>
            </div>
          </div>
        </section>

        <section class="panel-card hazard-card">
          <div class="panel-title">报警统计</div>
          <div class="hazard-summary three-col">
            <div v-for="item in hazardSummary" :key="item.label" class="hazard-summary-item">
              <span class="hazard-dot" :style="{ background: item.color }"></span>
              <span>{{ item.label }}</span>
              <strong :style="{ color: item.color }">{{ item.value }}项</strong>
            </div>
          </div>
          <div ref="hazardChartRef" class="chart-box hazard-chart"></div>
        </section>
      </div>
    </div>

    <a href="https://deerflow.tech" target="_blank" class="deerflow-mark" title="Created By Deerflow">
      <span class="deerflow-diamond">◆</span>
      <span class="deerflow-text">Deerflow</span>
    </a>
  </div>
</template>

<style scoped lang="scss">
.gas-home-screen {
  min-height: 100vh;
  color: #fff;
  background:
    linear-gradient(180deg, rgba(14, 42, 70, 0.96), rgba(8, 20, 37, 1)),
    radial-gradient(circle at center, rgba(36, 122, 184, 0.18), transparent 58%),
    #081425;
  position: relative;
}

.gas-home-screen::before {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background:
    radial-gradient(circle at 28% 32%, rgba(56, 216, 255, 0.06) 0%, transparent 45%),
    radial-gradient(circle at 72% 58%, rgba(22, 109, 255, 0.05) 0%, transparent 40%),
    radial-gradient(circle at 50% 85%, rgba(56, 216, 255, 0.04) 0%, transparent 35%);
  animation: bgPulse 8s ease-in-out infinite alternate;
}

@keyframes bgPulse {
  0% { opacity: 0.6; }
  100% { opacity: 1; }
}

.screen-header {
  height: 72px;
  padding: 0 24px;
  display: grid;
  grid-template-columns: 320px 1fr 320px;
  align-items: center;
  position: relative;
  z-index: 1;
  background: linear-gradient(90deg, rgba(27, 98, 155, 0.95), rgba(18, 53, 98, 0.95));
  border-bottom: 1px solid rgba(93, 194, 255, 0.28);
}

.screen-header::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 5%;
  width: 90%;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(56, 216, 255, 0.7), rgba(22, 109, 255, 0.7), transparent);
  animation: headerGlow 4s ease-in-out infinite alternate;
}

@keyframes headerGlow {
  0% { opacity: 0.4; }
  100% { opacity: 1; }
}

.header-time {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 1.5px;
  color: #ddf6ff;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 0 18px rgba(56, 216, 255, 0.35);
}

.header-title-wrap {
  text-align: center;
}

.header-title {
  font-size: 30px;
  font-weight: 800;
  letter-spacing: 3px;
  background: linear-gradient(180deg, #e8f6ff 0%, #8fd6ff 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-subtitle {
  margin-top: 4px;
  color: rgba(196, 234, 255, 0.65);
  font-size: 11px;
  letter-spacing: 3px;
  text-transform: uppercase;
}

.header-tags {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.header-tag {
  padding: 6px 14px;
  border: 1px solid rgba(117, 206, 255, 0.35);
  border-radius: 16px;
  color: #d8f5ff;
  background: rgba(13, 77, 128, 0.45);
  font-size: 12px;
  letter-spacing: 1px;
  transition: all 0.3s;
  cursor: default;
}

.header-tag:hover {
  border-color: rgba(117, 206, 255, 0.7);
  background: rgba(13, 77, 128, 0.65);
  box-shadow: 0 0 16px rgba(56, 216, 255, 0.15);
}

.screen-body {
  height: calc(100vh - 72px);
  padding: 12px;
  display: grid;
  grid-template-columns: 360px 1fr 360px;
  gap: 12px;
  position: relative;
  z-index: 1;
}

.side-panel,
.center-panel {
  min-height: 0;
}

.left-panel,
.right-panel {
  display: grid;
  gap: 12px;
}

.left-panel {
  grid-template-rows: 220px 220px 1fr;
}

.right-panel {
  grid-template-rows: 250px 260px 1fr;
}

.panel-card {
  min-height: 0;
  padding: 14px;
  position: relative;
  background: linear-gradient(180deg, rgba(13, 33, 56, 0.88), rgba(10, 25, 42, 0.88));
  border: 1px solid rgba(65, 158, 219, 0.22);
  border-radius: 12px;
  box-shadow: 0 2px 24px rgba(0, 0, 0, 0.25), inset 0 0 0 1px rgba(255, 255, 255, 0.025);
  transition: border-color 0.4s;
}

.panel-card::after {
  content: '';
  position: absolute;
  top: -1px;
  left: 20px;
  right: 20px;
  height: 1px;
  border-radius: 1px;
  background: linear-gradient(90deg, transparent, rgba(56, 216, 255, 0.25), transparent);
  pointer-events: none;
}

.panel-card:hover {
  border-color: rgba(65, 158, 219, 0.38);
}

.panel-title {
  position: relative;
  padding-left: 16px;
  margin-bottom: 14px;
  color: #8fe5ff;
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 1px;
}

.panel-title::before {
  content: '';
  position: absolute;
  left: 0;
  top: 3px;
  width: 4px;
  height: 20px;
  border-radius: 2px;
  background: linear-gradient(180deg, #28d7ff, #1776ff);
  box-shadow: 0 0 10px rgba(40, 215, 255, 0.5);
}

.base-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.base-card {
  background: linear-gradient(180deg, rgba(10, 30, 53, 0.95), rgba(9, 24, 42, 0.95));
}

.base-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.base-item {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 72px;
  padding: 14px;
  border-radius: 14px;
  background: linear-gradient(180deg, rgba(20, 57, 89, 0.45), rgba(18, 44, 72, 0.35));
  border: 1px solid rgba(97, 190, 255, 0.12);
  transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  cursor: default;
  position: relative;
  overflow: hidden;
}

.base-item::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 14px;
  background: radial-gradient(circle at 30% 30%, rgba(255, 255, 255, 0.03), transparent 70%);
  pointer-events: none;
  transition: opacity 0.35s;
  opacity: 0;
}

.base-item:hover {
  transform: translateY(-2px);
  border-color: rgba(97, 190, 255, 0.35);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.35), 0 0 0 1px rgba(97, 190, 255, 0.1) inset;
}

.base-item:hover::before {
  opacity: 1;
}

.base-icon {
  width: 48px;
  height: 48px;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 48px;
  position: relative;
  transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.35s;
}

.base-item:hover .base-icon {
  transform: scale(1.08);
  box-shadow: 0 0 28px rgba(56, 216, 255, 0.3);
}

.base-icon-svg {
  width: 26px;
  height: 26px;
  color: #fff;
  transition: transform 0.35s;
}

.base-item:hover .base-icon-svg {
  transform: scale(1.1);
}

.base-text {
  min-width: 0;
}

.base-label {
  color: #c0dff5;
  font-size: 12px;
  letter-spacing: 1px;
  text-transform: uppercase;
}

.base-value {
  margin-top: 4px;
  color: #fff;
  font-size: 32px;
  font-weight: 800;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.5px;
}

.base-value span {
  margin-left: 4px;
  color: #90d9ff;
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0;
}

.chart-box {
  width: 100%;
  height: calc(100% - 36px);
}

.table-card,
.warning-card,
.hazard-card {
  overflow: hidden;
}

.table-wrap {
  height: calc(100% - 38px);
  overflow: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

.data-table thead {
  background: rgba(24, 122, 198, 0.55);
}

.data-table thead th {
  font-weight: 700;
  letter-spacing: 0.5px;
  color: #b9e4ff;
}

.data-table th,
.data-table td {
  padding: 10px 6px;
  color: #f4fbff;
  text-align: center;
  font-size: 13px;
  border-bottom: 1px solid rgba(130, 194, 231, 0.1);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.data-table tbody tr {
  transition: background 0.25s;
}

.data-table tbody tr:nth-child(odd) {
  background: rgba(133, 196, 242, 0.10);
}

.data-table tbody tr:nth-child(even) {
  background: rgba(133, 196, 242, 0.04);
}

.data-table tbody tr:hover {
  background: rgba(56, 216, 255, 0.12);
}

.compact-table th,
.compact-table td {
  padding: 9px 4px;
}

.status-cell.ok {
  color: #6effc4;
  position: relative;
}

.status-cell.warn {
  color: #ffe16a;
}

.status-cell.off {
  color: #ff8e8e;
}

.status-cell::before {
  content: '';
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: middle;
}

.status-cell.ok::before {
  background: #6effc4;
  box-shadow: 0 0 6px rgba(110, 255, 196, 0.6);
}

.status-cell.warn::before {
  background: #ffe16a;
  box-shadow: 0 0 6px rgba(255, 225, 106, 0.6);
}

.status-cell.off::before {
  background: #ff8e8e;
  box-shadow: 0 0 6px rgba(255, 142, 142, 0.6);
}

.center-panel {
  position: relative;
}

.map-stage {
  position: relative;
  height: 100%;
  overflow: hidden;
  border-radius: 12px;
  border: 1px solid rgba(56, 159, 223, 0.2);
  background: rgba(8, 20, 37, 0.64);
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.3), inset 0 0 60px rgba(8, 30, 60, 0.3);
}

.map-stage-top {
  position: absolute;
  top: 14px;
  left: 14px;
  right: 14px;
  z-index: 3;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  pointer-events: none;
}

.summary-strip,
.map-kpis {
  display: flex;
  gap: 10px;
}

.summary-item,
.map-kpi {
  min-width: 110px;
  padding: 10px 14px;
  border-radius: 10px;
  background: rgba(8, 31, 54, 0.82);
  border: 1px solid rgba(79, 177, 238, 0.18);
  backdrop-filter: blur(8px);
  transition: all 0.3s;
}

.summary-label,
.map-kpi span {
  display: block;
  color: #91d7ff;
  font-size: 12px;
  letter-spacing: 0.5px;
}

.summary-value,
.map-kpi strong {
  display: block;
  margin-top: 4px;
  color: #fff;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 1px;
}

.map-view {
  width: 100%;
  height: 100%;
}

.map-error {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #ffd3d3;
  background: rgba(5, 10, 20, 0.72);
  z-index: 4;
}

.map-panel {
  position: absolute;
  z-index: 3;
  width: 190px;
  padding: 14px;
  border-radius: 12px;
  background: rgba(8, 31, 54, 0.82);
  border: 1px solid rgba(79, 177, 238, 0.18);
  backdrop-filter: blur(10px);
}

.info-panel {
  left: 18px;
  bottom: 20px;
}

.point-panel {
  right: 18px;
  bottom: 20px;
}

.mini-title {
  margin-bottom: 10px;
  color: #8fe5ff;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.mini-row {
  display: flex;
  justify-content: space-between;
  margin-top: 6px;
  padding: 4px 0;
  color: #d6efff;
  font-size: 13px;
  border-bottom: 1px solid rgba(130, 194, 231, 0.06);
}

.mini-row:last-child {
  border-bottom: none;
}

.warning-scroll {
  height: calc(100% - 38px);
  overflow-y: auto;
  padding-right: 4px;
}

.warning-scroll::-webkit-scrollbar {
  width: 5px;
}

.warning-scroll::-webkit-scrollbar-thumb {
  border-radius: 999px;
  background: rgba(96, 180, 235, 0.3);
}

.warning-scroll::-webkit-scrollbar-track {
  background: transparent;
}

.warning-list {
  display: grid;
  gap: 10px;
}

.warning-item {
  display: flex;
  gap: 12px;
  align-items: stretch;
  padding: 14px;
  border-radius: 14px;
  border: 1px solid rgba(255, 108, 108, 0.1);
  background: linear-gradient(180deg, rgba(72, 24, 38, 0.42), rgba(49, 19, 31, 0.30));
  transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
}

.warning-item::before {
  content: '';
  position: absolute;
  left: 0;
  top: 12px;
  bottom: 12px;
  width: 3px;
  border-radius: 0 3px 3px 0;
  transition: all 0.35s;
}

.warning-item.danger::before {
  background: linear-gradient(180deg, #ff5858, #ff3030);
  box-shadow: 0 0 12px rgba(255, 58, 58, 0.5);
  animation: dangerPulse 2s ease-in-out infinite;
}

.warning-item.notice::before {
  background: linear-gradient(180deg, #ffc635, #ff9f1a);
  box-shadow: 0 0 10px rgba(255, 198, 53, 0.35);
}

@keyframes dangerPulse {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

.warning-item:hover {
  transform: translateX(3px);
}

.warning-item.danger:hover {
  border-color: rgba(255, 108, 108, 0.25);
  box-shadow: 0 4px 20px rgba(255, 58, 58, 0.1);
}

.warning-item.notice:hover {
  border-color: rgba(255, 201, 92, 0.2);
  box-shadow: 0 4px 20px rgba(255, 198, 53, 0.08);
}

.warning-item.notice {
  border-color: rgba(255, 201, 92, 0.1);
  background: linear-gradient(180deg, rgba(78, 63, 23, 0.28), rgba(48, 43, 20, 0.18));
}

.warning-side {
  width: 52px;
  flex: 0 0 52px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
}

.warning-badge {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 800;
  color: #fff;
  transition: transform 0.3s;
}

.warning-item:hover .warning-badge {
  transform: scale(1.1);
}

.warning-tag {
  width: 100%;
  padding: 3px 0;
  border-radius: 999px;
  text-align: center;
  font-size: 10px;
  letter-spacing: 0.5px;
  color: #dcefff;
  background: rgba(255, 255, 255, 0.06);
}

.warning-item.danger .warning-badge {
  background: rgba(255, 58, 58, 0.28);
  color: #ff8787;
}

.warning-item.danger .warning-title {
  color: #ffd8d8;
}

.warning-item.notice .warning-badge {
  background: rgba(255, 198, 53, 0.2);
  color: #ffd45d;
}

.warning-item.notice .warning-title {
  color: #fff2c4;
}

.warning-main {
  min-width: 0;
  flex: 1;
}

.warning-title-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.warning-title {
  font-weight: 700;
  font-size: 16px;
  line-height: 1.2;
}

.warning-time,
.warning-code,
.warning-desc {
  color: #dcefff;
  font-size: 12px;
}

.warning-time {
  color: #b6dfff;
  white-space: nowrap;
}

.warning-code {
  margin: 6px 0 4px;
  color: #9ad8ff;
}

.warning-desc {
  line-height: 1.5;
}

.hazard-summary {
  display: flex;
  gap: 12px;
  margin-bottom: 10px;
}

.hazard-summary.three-col {
  justify-content: space-between;
}

.hazard-summary-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  padding: 8px 12px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.04);
  transition: all 0.3s;
}

.hazard-summary-item:hover {
  background: rgba(255, 255, 255, 0.06);
  border-color: rgba(255, 255, 255, 0.08);
}

.hazard-dot {
  width: 8px;
  height: 24px;
  display: inline-block;
  border-radius: 4px;
  box-shadow: 0 0 10px currentColor;
}

.hazard-chart {
  height: 160px;
}

.equipment-wrap {
  height: calc(100% - 38px);
}

:deep(.BMap_cpyCtrl),
:deep(.anchorBL) {
  display: none !important;
}

.deerflow-mark {
  position: fixed;
  bottom: 14px;
  right: 18px;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  opacity: 0.28;
  transition: opacity 0.4s;
}

.deerflow-mark:hover {
  opacity: 0.6;
}

.deerflow-diamond {
  font-size: 10px;
  color: #8fe5ff;
}

.deerflow-text {
  font-size: 11px;
  letter-spacing: 1px;
  color: #8fe5ff;
  font-weight: 500;
}

@media (max-width: 1500px) {
  .screen-body {
    grid-template-columns: 320px 1fr 320px;
  }

  .header-title {
    font-size: 24px;
  }

  .summary-item,
  .map-kpi {
    min-width: 96px;
  }
}
</style>
