<template>
  <div class="progress-page">
    <div class="canvas">
      <header class="topbar"><div class="topbar-center"><div class="model-title">XXX特大桥主体模型.rvt</div></div></header>

      <section class="model-stage">
        <ModelPlaceholder ref="modelRef" @componentBindEncoding="onComponentBindEncoding" @codeBindComponent="codeBindComponent" />
      </section>

      <section class="module-topbar">
        <div class="module-topbar-panel">
          <div class="field field-time">
            <select class="select" v-model="timeStatMode"><option value="cutoff">日期截止统计</option></select>
            <input class="date-input" type="date" v-model="cutoffDateDraft" />
            <button class="btn btn-sm btn-accent" type="button" @click="applyCutoffDate">查询</button>
            <div class="kpi kpi-progress">整体完成百分比：<span class="kpi-number">{{ formatPercentValue(overallPercent, 2) }}</span></div>
          </div>

          <div class="field field-percent">
            <span class="field-label">进度百分比</span>
            <label class="chip chip-status"><input type="checkbox" :checked="percentFilters.all" @change="togglePercentFilter('all', $event.target.checked)" /> 全部</label>
            <label class="chip chip-status chip-gray"><input type="checkbox" :checked="percentFilters.p0" @change="togglePercentFilter('p0', $event.target.checked)" /> 0%</label>
            <label class="chip chip-status chip-red"><input type="checkbox" :checked="percentFilters.p0_50" @change="togglePercentFilter('p0_50', $event.target.checked)" /> 0%-50%</label>
            <label class="chip chip-status chip-blue"><input type="checkbox" :checked="percentFilters.p50_100" @change="togglePercentFilter('p50_100', $event.target.checked)" /> 50%-100%</label>
            <label class="chip chip-status chip-green"><input type="checkbox" :checked="percentFilters.p100" @change="togglePercentFilter('p100', $event.target.checked)" /> 100%</label>
          </div>
        </div>
      </section>

      <aside class="sidepanel" :class="{ 'is-collapsed': sideCollapsed }">
        <header class="sidepanel-header">
          <div class="sidepanel-title">工程部位</div>
          <button class="iconbtn" type="button" @click="sideCollapsed = !sideCollapsed">{{ sideCollapsed ? "▸" : "▾" }}</button>
        </header>
        <div class="sidepanel-body" v-show="!sideCollapsed">
<div class="tree">
              <div class="tree-item" :class="{ 'is-active': row.leaf && row.id === selectedStructureId }" :style="{ paddingLeft: `${10 + row.level * 14}px` }" v-for="row in visibleTreeRows" :key="row.id" @click="onTreeRowClick(row)" @contextmenu.prevent="onTreeRightClick($event, row)">
                <button class="tree-caret" type="button" :class="{ 'is-leaf': row.leaf }" :disabled="row.leaf" @click.stop="toggleTreeExpand(row)">{{ row.leaf ? "•" : row.open ? "▾" : "▸" }}</button>
                <span class="tree-bullet"></span>
                <span class="tree-text">{{ row.name }}</span>
              </div>
            </div>

            <div v-if="showContextMenu" class="context-menu" :style="{ left: contextMenuX + 'px', top: contextMenuY + 'px' }" @click.stop>
              <div class="context-menu-item" @click="onAddComponent">绑定编码</div>
              <div class="context-menu-item" @click="onViewComponent">查看编辑</div>
            </div>

            <div v-if="showAddModal" class="add-modal" :style="modalPosition" @pointerdown.stop="onModalPointerDown">
              <header class="add-modal-header">
                <div class="add-modal-title">绑定编码</div>
                <button class="add-modal-close" type="button" @click="showAddModalClose">✕</button>
              </header>
              <div class="add-modal-body">
                <div class="add-modal-split">
                  <div class="add-modal-left">
                    <div class="add-modal-section-title">部位列表</div>
                    <div class="add-modal-list">
                      <div class="add-modal-tag" v-for="(id, index) in partList" :key="index">
                        <span>{{ getStructureName(id.id) }}</span>
                        <button class="add-modal-tag-delete" type="button" @click="partList.splice(index, 1)">✕</button>
                      </div>
                    </div>
                  </div>
                  <div class="add-modal-right">
                    <div class="add-modal-section-title">构件列表</div>
                    <div class="add-modal-list">
                      <div class="add-modal-tag" v-for="(item, index) in componentList" :key="index">
<!--                        <input class="add-form-input" v-model="item.code" placeholder="请输入构件编码" />-->
                        <span>{{ item.code }}</span>
                        <button class="add-modal-tag-delete" type="button" @click="componentList.splice(index, 1)">✕</button>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="add-modal-footer">
                  <button class="add-modal-btn add-modal-btn-secondary" type="button" @click="showAddModalHandle">取消</button>
                  <button class="add-modal-btn add-modal-btn-primary" type="button" @click="onSaveComponent">保存</button>
                </div>
</div>
            </div>

            <div v-if="showViewModal" class="add-modal" :style="viewModalPosition" @pointerdown.stop="onViewModalPointerDown">
              <header class="add-modal-header">
                <div class="add-modal-title">查看构件</div>
                <button class="add-modal-close" type="button" @click="showViewModal = false">✕</button>
              </header>
              <div class="add-modal-body">
                <div class="add-modal-split">
                  <div class="add-modal-left">
                    <div class="add-modal-section-title">部位列表</div>
                    <div class="add-modal-list">
                      <div class="add-modal-tag" v-for="(id, index) in viewPartList" :key="index">
                        <span>{{ getStructureName(id) }}</span>
                      </div>
                    </div>
                  </div>
                  <div class="add-modal-right">
                    <div class="add-modal-section-title">构件列表</div>
                    <div class="add-modal-list">
                      <div class="add-modal-tag" v-for="(item, index) in viewComponentList" :key="index">
                        <span>{{ item.codeId }}</span>
                        <button class="add-modal-tag-delete" type="button" @click="viewComponentList.splice(index, 1)">✕</button>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="add-modal-footer">
                  <button class="add-modal-btn add-modal-btn-secondary" type="button" @click="showViewModal = false">取消</button>
                  <button class="add-modal-btn add-modal-btn-primary" type="button" @click="onDeleteComponent">保存</button>
                </div>
              </div>
            </div>
          </div>
        </aside>

      <section class="bottompanel" :class="{ 'is-collapsed': bottomCollapsed }">
        <header class="bottompanel-header"><div class="tabs"><button class="tab is-on" type="button">项目日进度明细</button></div></header>
        <button class="iconbtn bottompanel-toggle" type="button" @click="bottomCollapsed = !bottomCollapsed">{{ bottomCollapsed ? "▴" : "▾" }}</button>
        <div class="bottompanel-body" v-show="!bottomCollapsed">
          <table class="table">
            <thead>
              <tr>
<!--                <th>序号</th><th>全名称</th><th>清单编号</th><th>清单名称</th><th>单位</th><th>合同数量</th><th>合同金额</th><th>填报日期</th><th>完成数量</th><th>完成金额</th><th>累计完成数量</th>-->
                <th>序号</th><th>全名称</th><th>清单编号</th><th>清单名称</th><th>单位</th><th>合同数量</th><th>合同金额</th><th>合同金额不含税</th><th>累计完成数量</th><th>进度</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="r in progressRows" :key="r.no">
                <td>{{ r.no }}</td><td>{{ r.fullName }}</td><td>{{ r.workCode }}</td><td>{{ r.name }}</td><td>{{ r.workUnit }}</td><td>{{ formatNumber(r.meteringNum,2) }}</td><td>{{ formatMoney(r.meteringAmt) }}</td><td>{{ formatMoney(r.meteringNotaxAmt) }}</td><td>{{ r.totalNum }}</td><td>{{ proportion(r.meteringNum,r.totalNum) }}</td>
              </tr>
            </tbody>
          </table>
        </div>
      </section>
    </div>
  </div>

  <div v-if="toastText" class="toast" :class="'toast-' + toastType">{{ toastText }}</div>
</template>

<script setup>
import {computed, onMounted, reactive, ref} from "vue";
import ModelPlaceholder from "../../components/model-placeholder/index.vue";
import {progressApi} from "../../service/api/progress.js";

const structures = ref([]);
const modelRef = ref(null);
const elementTree = ref([]);
const progressData = ref([]);
const sideCollapsed = ref(false);
const bottomCollapsed = ref(false);
const selectedStructureId = ref("");
const expanded = ref(new Set());
const loading = ref(false);

const showContextMenu = ref(false);
const contextMenuX = ref(0);
const contextMenuY = ref(0);
const contextMenuRow = ref(null);
const showAddModal = ref(false);
const componentList = ref([]);
const partList = ref([]);
const showViewModal = ref(false);
const viewPartList = ref([]);
const viewComponentList = ref([]);
const toastText = ref("");
const toastType = ref("success");
const normalLight = ref([]);

const modalPosition = ref({ left: "50%", top: "50%", transform: "translate(-50%, -50%)" });
let modalDragging = false;
let modalStartX = 0, modalStartY = 0;
let modalStartLeft = 0, modalStartTop = 0;

function onModalPointerDown(e) {
  if (e.target.closest(".add-modal-close") || e.button !== 0) return;
  modalDragging = true;
  modalStartX = e.clientX;
  modalStartY = e.clientY;
  const pos = modalPosition.value;
  if (pos.transform) {
    modalStartLeft = 50;
    modalStartTop = 50;
  }
  window.addEventListener("pointermove", onModalPointerMove);
  window.addEventListener("pointerup", onModalPointerUp);
}

function onModalPointerMove(e) {
  if (!modalDragging) return;
  const dx = e.clientX - modalStartX;
  const dy = e.clientY - modalStartY;
  modalPosition.value = {
    left: `calc(50% + ${dx}px)`,
    top: `calc(50% + ${dy}px)`,
    transform: "translate(-50%, -50%)"
  };
}

function onModalPointerUp() {
  modalDragging = false;
  window.removeEventListener("pointermove", onModalPointerMove);
  window.removeEventListener("pointerup", onModalPointerUp);
}

const viewModalPosition = ref({ left: "50%", top: "50%", transform: "translate(-50%, -50%)" });
let viewModalDragging = false;
let viewModalStartX = 0, viewModalStartY = 0;

function onViewModalPointerDown(e) {
  if (e.target.closest(".add-modal-close") || e.button !== 0) return;
  viewModalDragging = true;
  viewModalStartX = e.clientX;
  viewModalStartY = e.clientY;
  window.addEventListener("pointermove", onViewModalPointerMove);
  window.addEventListener("pointerup", onViewModalPointerUp);
}

function onViewModalPointerMove(e) {
  if (!viewModalDragging) return;
  const dx = e.clientX - viewModalStartX;
  const dy = e.clientY - viewModalStartY;
  viewModalPosition.value = {
    left: `calc(50% + ${dx}px)`,
    top: `calc(50% + ${dy}px)`,
    transform: "translate(-50%, -50%)"
  };
}

function onViewModalPointerUp() {
  viewModalDragging = false;
  window.removeEventListener("pointermove", onViewModalPointerMove);
  window.removeEventListener("pointerup", onViewModalPointerUp);
}

const timeStatMode = ref("cutoff");
const baselineDate = ref(todayISODate());
const baselineOverallPercent = ref(0);
const cutoffDate = ref(todayISODate());
const cutoffDateDraft = ref(cutoffDate.value);

const percentFilters = reactive({ all: true, p0: false, p0_50: false, p50_100: false, p100: false });

const visibleTreeRows = computed(() => {
  const rows = [];
  const walk = (node, level) => {
    const leaf = node.isLeaf === true;
    const open = expanded.value.has(node.id);
    rows.push({ id: node.id, name: node.name, level, leaf, open, createDate: node.createDate, projectId: node.projectId });
    if (!leaf && open && node.children) {
      node.children.forEach((c) => walk(c, level + 1));
    }
  };
  elementTree.value.forEach((n) => walk(n, 0));
  return rows;
});

const selectedStructureName = computed(() => {
  const findById = (nodes) => {
    for (const n of nodes) {
      if (n.id === selectedStructureId.value) return n.name;
      if (n.children) {
        const found = findById(n.children);
        if (found) return found;
      }
    }
  };
  return findById(elementTree.value) || "";
});

const selectedStructureNode = computed(() => {
  return findNode(elementTree.value, selectedStructureId.value);
});

const overallPercent = computed(() => {
  if (!selectedStructureNode.value) return 0;
  return 0;
});

const progressRows = computed(() => {
  if (!selectedStructureId.value && progressData.value.length === 0) return [];
  return progressData.value.map((item, index) => ({
    no: index + 1,
    fullName: item.fullName || "",
    workCode: item.workCode || "",
    name: item.name || "",
    workUnit: item.workUnit || "-",
    meteringAmt: item.meteringAmt || 0,
    meteringNotaxAmt: item.meteringNotaxAmt || 0,
    meteringNum: item.meteringNum || 0,
    totalNum: item.totalNum || '',
    doneQty: item.doneQty || 0,
    doneAmt: item.doneAmt || 0,
    cumDoneQty: item.cumDoneQty || 0,
  }));
});

async function loadWbsTree(parentId) {
  loading.value = true;
  try {
    const data = await progressApi.getTree(parentId);
    if (!parentId) {
      elementTree.value = data.map((item) => ({
        ...item,
        children: item.isLeaf ? [] : [],
        isLeaf: !!item.isLeaf,
      }));
      if (data.length > 0) {
        const firstLeaf = findFirstLeaf(elementTree.value);
        if (firstLeaf) selectedStructureId.value = firstLeaf.id;
      }
    } else {
      updateChildren(elementTree.value, parentId, data);
    }
  } catch (e) {
    console.error("加载WBS树失败:", e);
  } finally {
    loading.value = false;
  }
}

function findFirstLeaf(nodes) {
  for (const n of nodes) {
    if (n.isLeaf) return n;
    if (n.children && n.children.length > 0) {
      const found = findFirstLeaf(n.children);
      if (found) return found;
    }
  }
}

function updateChildren(nodes, parentId, children) {
  for (const n of nodes) {
    if (n.id === parentId) {
      n.children = children.map((item) => ({
        ...item,
        children: item.isLeaf ? [] : [],
        isLeaf: !!item.isLeaf,
      }));
      return true;
    }
    if (n.children && updateChildren(n.children, parentId, children)) return true;
  }
  return false;
}

function findNode(nodes, id) {
  for (const n of nodes) {
    if (n.id === id) return n;
    if (n.children) {
      const found = findNode(n.children, id);
      if (found) return found;
    }
  }
  return null;
}

onMounted(() => {
  loadWbsTree();
  window.addEventListener("click", closeContextMenu);
  console.log(6666,progressApi.calculateProgress());
});

function todayISODate() {
  return new Date().toISOString().slice(0, 10);
}

function getStructureName(id) {
  const findName = (nodes) => {
    for (const n of nodes) {
      if (n.id === id) return n.name;
      if (n.children) {
        const found = findName(n.children);
        if (found) return found;
      }
    }
  };
  return findName(elementTree.value) || id;
}

function daysBetweenISO(a, b) {
  const da = new Date(`${a}T00:00:00`);
  const db = new Date(`${b}T00:00:00`);
  return Math.round((db.getTime() - da.getTime()) / 86400000);
}

function clamp(value, min, max) {
  return Math.min(max, Math.max(min, value));
}

function formatPercentValue(value, digits = 2) {
  return `${Number(value).toFixed(digits)}%`;
}

function formatMoney(value) {
  if (value == null || Number.isNaN(value)) return "--";
  const abs = Math.abs(value);
  const sign = value < 0 ? "-" : "";
  if (abs >= 1e8) return `${sign}${(abs / 1e8).toFixed(2)}亿`;
  if (abs >= 1e4) return `${sign}${(abs / 1e4).toFixed(2)}万`;
  return `${sign}${abs.toFixed(2)}元`;
}

function formatNumber(value, digits = 0) {
  return Number(value).toLocaleString("zh-CN", { minimumFractionDigits: digits, maximumFractionDigits: digits });
}

function applyCutoffDate() {
  cutoffDate.value = cutoffDateDraft.value || cutoffDate.value || todayISODate();
}
// 计算比例：count / allCount，并显示百分比
function proportion(count, allCount) {
  if (count==="" || allCount==="")
    return ""
  // 防止除以 0 报错
  if (!allCount || allCount === 0) return `0 / 0 (0%)`

  // 计算百分比（保留2位小数）
  let percent = ((count / allCount) * 100).toFixed(2)

  // 返回格式：5 / 10 (50.00%)
  return `${count} / ${allCount} (${percent}%)`
}

function applyAllExclusive(key, checked) {
  if (key === "all") {
    percentFilters.all = checked;
    if (checked) {
      percentFilters.p0 = true;
      percentFilters.p0_50 = true;
      percentFilters.p50_100 = true;
      percentFilters.p100 = true;
    } else {
      percentFilters.p0 = false;
      percentFilters.p0_50 = false;
      percentFilters.p50_100 = false;
      percentFilters.p100 = false;
    }
    return;
  }
  percentFilters[key] = checked;
  if (checked) percentFilters.all = false;
  if (!percentFilters.p0 && !percentFilters.p0_50 && !percentFilters.p50_100 && !percentFilters.p100) percentFilters.all = false;
}

function togglePercentFilter(key, checked) { applyAllExclusive(key, checked); }

function toggleTreeExpand(row) {
  if (row.leaf) return;
  if (expanded.value.has(row.id)) {
    expanded.value.delete(row.id);
  } else {
    expanded.value.add(row.id);
    const node = findNode(elementTree.value, row.id);
    if (node && (!node.children || node.children.length === 0)) {
      loadWbsTree(row.id);
    }
  }
}

async  function onTreeRowClick(row) {
  modelRef.value?.cancelLightModels();
  normalLight.value = [];
  const data = await progressApi.getByPartId(row.id);
  if (data.length > 0) {
    for (const item of data) {
      const codeData = getParams(item.codeData);
      const url = codeData.url;
      const id = Number(codeData.id);
      // 1. 去找数组里有没有相同的 url
      let existItem = normalLight.value.find((i) => i.url === url);
      if (existItem) {
        // 2. 找到了 → 把id加进去
        existItem.ids.push(id);
      } else {
        // 3. 没找到 → 新增
        normalLight.value.push({ url, ids: [id] });
      }
    }
    modelRef.value?.highlightModels(normalLight.value);
  }
  if (row.leaf) {
    selectedStructureId.value = row.id;
    await loadProgressData(row.id, row.createDate);
  } else {
    toggleTreeExpand(row);
    progressData.value = []
    progressData.value = await progressApi.findActAmtByPositionId(row.projectId, row.id, row.createDate);
  }
}


//获取"{url=https://lyz-1259524260.cos.ap-guangzhou.myqcloud.com/iflow/models/417664a3-76c8-4d94-9344-1337246a5d4e, id=353503}" return obj =>{url,id}
function getParams(str) {
  let obj = {};
  str.replace(/{([\s\S]+)}/, '$1').split(', ').forEach(i => {
    let [k, v] = i.split('=');
    obj[k] = v;
  });
  return obj;
}

function onTreeRightClick(e, row) {
  // if (!row.leaf) return;
  contextMenuX.value = e.clientX;
  contextMenuY.value = e.clientY;
  contextMenuRow.value = row;
  showContextMenu.value = true;
}

function onAddComponent() {
  partList.value.push({id:contextMenuRow.value.id,createDate:contextMenuRow.value.createDate});
  componentList.value = []
  showAddModal.value = true;
  showContextMenu.value = false;
}

async function onViewComponent() {
  const partId = contextMenuRow.value?.id;
  if (!partId) return;
  showContextMenu.value = false;
  try {
    const data = await progressApi.getByPartId(partId);
    // const grouped = processCodeData(data);
    // console.log("处理后数据:", grouped);
    viewPartList.value = [partId];
    viewComponentList.value = data;
    showViewModal.value = true;
  } catch (e) {
    console.error("获取构件列表失败:", e);
    showToast("获取失败", "error");
  }
}

async function onDeleteComponent() {
  const partIds = viewPartList.value;
  const allIds = [];
  for (const item of viewComponentList.value) {
    if (item.ids) allIds.push(...item.ids);
  }
  if (!partIds.length || !allIds.length) return;
  try {
    const res = await progressApi.deleteBatch({ partIds, codeIds: allIds });
    if (res?.code === 200) {
      showToast("删除成功！", "success");
      showViewModal.value = false;
    } else {
      showToast(res?.message || "删除失败！", "error");
    }
  } catch (e) {
    console.error("删除失败:", e);
    showToast("删除失败！", "error");
  }
}

function closeContextMenu() {
  showContextMenu.value = false;
}

function onComponentBindEncoding(data) {
  if (showAddModal.value) {
    showAddModal.value = false;
    return;
  }
  componentList.value = [];
  partList.value = [];
  showAddModal.value = true;
  for (let component of data.components) {
    componentList.value.push({code:component.id,data:component})
  }
}
async function codeBindComponent(data) {
  // if (!showAddModal.value) {
  //   return
  // }
  if (!data?.components?.length) return;
  progressData.value = []
  const newData = [];
  for (const comp of data.components) {
    const res = await progressApi.getByCodeId(comp.id);
    for (const item of res) {
      if (item.pjDayData) {
        if (Array.isArray(item.pjDayData)) {
          newData.push(...item.pjDayData);
        } else {
          newData.push(item.pjDayData);
        }
      }
    }
    componentList.value.push({ code: comp.id || "", data: comp });
  }
  progressData.value = [...progressData.value, ...newData];
}

async function onSaveComponent() {
  const partIds = partList.value;
  const codeIds = componentList.value;
  // const codeIds = componentList.value.filter((c) => c.code).map((c) => c.code);
  if (!partIds.length || !codeIds.length) return;
  try {
    const res = await progressApi.saveBatch({ partIds, codeIds });
    if (res.code === 200) {
      showToast("保存成功!", "success");
      showAddModal.value = false;
    } else {
      showToast(res.message || "保存失败!", "error");
    }
  } catch (e) {
    console.error("保存失败:", e);
    showToast("保存失败!", "error");
  }
}

function showToast(text, type = "success") {
  toastText.value = text;
  toastType.value = type;
  setTimeout(() => { toastText.value = ""; }, 2000);
}
async function showAddModalHandle() {
  partList.value = [];
  componentList.value = [];
  showAddModal.value = false;
}
async function showAddModalClose() {
  partList.value = [];
  componentList.value = [];
  showAddModal.value = false;
}

async function loadProgressData(positionId,time) {
  loading.value = true;
  try {
    const data = await progressApi.getPjProgress({
      projectId: "5e4bde33ec084f1a8673eb59b190dce7",
      positionIds: positionId,
      period: time,
    });
    progressData.value = data;
  } catch (e) {
    console.error("加载进度明细失败:", e);
    progressData.value = [];
  } finally {
    loading.value = false;
  }
}
</script>

<style scoped>
.progress-page { min-height: 100vh; font-family: "Microsoft YaHei", "PingFang SC", "Hiragino Sans GB", "Noto Sans CJK SC", sans-serif; }
.canvas { position: relative; min-height: 100vh; overflow: hidden; border-radius: 24px; border: 1px solid rgba(98,191,206,.34); background: radial-gradient(circle at 50% 30%, #bcc9d6 0%, #b2c3d2 60%, #a8b8c9 100%); box-shadow: inset 0 0 0 8px rgba(214,230,241,.55); }
.topbar { position: absolute; inset: 0 0 auto; height: 120px; z-index: 30; }
.topbar-center { position: absolute; left: 50%; transform: translateX(-50%); top: 16px; }
.model-title { font-size: 20px; letter-spacing: 1px; font-weight: 700; color: rgba(255,128,52,.95); text-shadow: 0 2px 10px rgba(0,0,0,.25); }
.model-stage { position: absolute; inset: 0; }

.module-topbar { position: absolute; left: 50%; top: 20px; transform: translateX(-50%); width: min(800px, calc(100% - 180px)); z-index: 99; }
.module-topbar-panel { border-radius: 18px; border: 1px solid rgba(83,214,206,.26); background: radial-gradient(420px 120px at 18% 0%, rgba(83,214,206,.22), transparent 70%), linear-gradient(180deg, rgba(18,32,40,.94), rgba(15,25,32,.88)); box-shadow: 0 20px 56px rgba(0,0,0,.28), 0 0 0 1px rgba(83,214,206,.12) inset; padding: 10px 14px; display: grid; gap: 10px; }
.field { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.field-time { flex-wrap: nowrap; }
.field-percent { flex-wrap: nowrap; }
.select, .date-input { height: 34px; border-radius: 10px; border: 1px solid rgba(255,255,255,.14); background: rgba(0,0,0,.18); color: rgba(255,255,255,.9); padding: 0 10px; font-size: 13px; font-weight: 700; }
.date-input { width: 150px; }
.btn { appearance: none; border: 1px solid rgba(255,255,255,.1); background: rgba(255,255,255,.08); color: rgba(255,255,255,.88); border-radius: 12px; padding: 8px 10px; cursor: pointer; }
.btn-sm { padding: 6px 10px; border-radius: 999px; font-size: 12px; font-weight: 800; }
.btn-accent { border-color: rgba(83,214,206,.22); background: rgba(43,191,178,.22); }

.kpi { margin-left: auto; font-size: 13px; font-weight: 800; color: rgba(236,248,251,.86); white-space: nowrap; }
.kpi-number { font-weight: 900; color: rgba(83,214,206,.95); margin-left: 4px; }

.field-label { font-weight: 900; color: rgba(244,252,255,.92); font-size: 14px; white-space: nowrap; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: 999px; border: 1px solid rgba(255,255,255,.12); background: rgba(0,0,0,.2); color: rgba(255,255,255,.88); font-weight: 800; font-size: 13px; white-space: nowrap; }
.chip input { width: 13px; height: 13px; margin: 0; accent-color: rgba(64,224,208,.92); }
.chip-status::before { content: ""; width: 10px; height: 10px; border-radius: 3px; border: 1px solid rgba(255,255,255,.2); background: transparent; }
.chip-status.chip-gray::before { border-color: rgba(110,125,150,.45); background: rgba(110,125,150,.24); }
.chip-status.chip-red::before { border-color: rgba(217,54,62,.4); background: rgba(217,54,62,.22); }
.chip-status.chip-blue::before { border-color: rgba(21,108,255,.4); background: rgba(21,108,255,.22); }
.chip-status.chip-green::before { border-color: rgba(47,143,47,.4); background: rgba(47,143,47,.22); }

.sidepanel { position: absolute; left: 16px; top: 175px; width: 320px; bottom: 100px; border-radius: 18px; border: 1px solid rgba(83,214,206,.24); background: radial-gradient(260px 140px at 8% 0%, rgba(63,203,191,.16), transparent 62%) padding-box, linear-gradient(180deg, rgba(20,31,37,.92), rgba(15,23,29,.88)) padding-box; box-shadow: 0 22px 60px rgba(0,0,0,.34), 0 0 0 1px rgba(83,214,206,.12) inset; overflow: hidden; z-index: 25; }
.sidepanel.is-collapsed { width: 64px; }
.sidepanel-header { display: flex; align-items: center; justify-content: space-between; padding: 10px 10px 10px 12px; border-bottom: 1px solid rgba(83,214,206,.16); background: radial-gradient(360px 100px at 10% 0%, rgba(83,214,206,.24), transparent 68%), linear-gradient(180deg, rgba(28,134,122,.84), rgba(15,60,74,.62)); }
.sidepanel-title { font-size: 14px; font-weight: 900; color: rgba(207,247,242,.96); }
.iconbtn { width: 30px; height: 30px; border-radius: 10px; border: 1px solid rgba(255,255,255,.12); background: rgba(255,255,255,.42); color: rgba(11,27,58,.86); font-weight: 900; cursor: pointer; }
.sidepanel-body { height: calc(100% - 52px); overflow: auto; padding: 8px; }

.tree { display: grid; gap: 8px; }
.tree-item { display: grid; grid-template-columns: 20px 10px 1fr; align-items: center; gap: 8px; min-height: 40px; border-radius: 14px; border: 1px solid rgba(154,186,198,.22); background: linear-gradient(180deg, rgba(44,58,68,.58), rgba(37,49,58,.54)); cursor: pointer; }
.tree-item:hover { border-color: rgba(83,214,206,.32); background: linear-gradient(180deg, rgba(51,67,78,.66), rgba(43,57,67,.62)); }
.tree-item.is-active { border-color: rgba(132,188,255,.3); background: linear-gradient(180deg, rgba(112,146,198,.34), rgba(95,128,180,.28)); }
.tree-caret { border: 0; background: transparent; color: rgba(203,230,236,.88); cursor: pointer; font-size: 14px; }
.tree-caret.is-leaf { cursor: default; }
.tree-bullet { width: 10px; height: 10px; border-radius: 50%; background: rgba(83,214,206,.85); }
.tree-text {
  font-size: 13px;
  font-weight: 800;
  color: rgba(222,238,244,.9);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 180px;
  display: inline-block;
}

.bottompanel { position: absolute; left: 370px; right: 16px; bottom: 100px; height: 390px; border-radius: 18px; border: 1px solid rgba(83,214,206,.24); background: radial-gradient(420px 180px at 14% 0%, rgba(83,214,206,.2), transparent 66%) padding-box, radial-gradient(520px 220px at 82% 0%, rgba(40,156,228,.14), transparent 70%) padding-box, linear-gradient(180deg, rgba(18,33,40,.94), rgba(14,24,31,.9)) padding-box; box-shadow: 0 24px 70px rgba(0,0,0,.36), 0 0 0 1px rgba(83,214,206,.14) inset; overflow: hidden; z-index: 26; }
.bottompanel-header { padding: 12px 52px 12px 12px; border-bottom: 1px solid rgba(83,214,206,.28); background: radial-gradient(380px 120px at 18% 0%, rgba(83,214,206,.28), transparent 70%), linear-gradient(180deg, rgba(25,137,124,.86), rgba(16,66,82,.64)); }
.bottompanel-toggle { position: absolute; right: 12px; top: 12px; }
.tabs { display: flex; gap: 10px; }
.tab { border: 1px solid rgba(255,255,255,.12); background: rgba(255,255,255,.08); color: rgba(214,235,241,.9); border-radius: 999px; padding: 8px 16px; cursor: pointer; font-weight: 900; font-size: 12px; }
.tab.is-on { border-color: rgba(83,214,206,.32); background: rgba(83,214,206,.24); }
.bottompanel.is-collapsed { height: 70px; }
.bottompanel-body { padding: 0 12px 12px; overflow: auto; height: calc(100% - 66px); }

.table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 14px; }
.table th, .table td { text-align: left; padding: 12px 14px; border-bottom: 1px solid rgba(178,206,216,.16); }
.table th { color: rgba(219,240,246,.86); font-weight: 900; background: linear-gradient(90deg, rgba(12,38,52,.92), rgba(16,47,61,.86)); position: sticky; top: 0; z-index: 2; }
.table td { color: rgba(222,238,244,.86); background: linear-gradient(90deg, rgba(37,51,61,.42), rgba(31,45,54,.36)); }
.table tbody tr:hover td { background: linear-gradient(90deg, rgba(69,102,130,.36), rgba(56,86,113,.32)); }

@media (max-width: 1300px) {
  .module-topbar { width: calc(100% - 32px); }
  .field-time, .field-percent { flex-wrap: wrap; }
  .kpi { margin-left: 0; }
  .sidepanel { width: 280px; }
  .bottompanel { left: 330px; }
}

.context-menu {
  position: fixed;
  z-index: 9999;
  border-radius: 10px;
  border: 1px solid rgba(83, 214, 206, 0.25);
  background: linear-gradient(180deg, rgba(20, 31, 37, 0.95), rgba(15, 23, 29, 0.92));
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.4);
  overflow: hidden;
}

.context-menu-item {
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 700;
  color: rgba(207, 247, 242, 0.9);
  cursor: pointer;
}

.context-menu-item:hover {
  background: rgba(83, 214, 206, 0.18);
}

.add-modal {
  position: fixed;
  width: min(520px, 86vw);
  border-radius: 16px;
  border: 1px solid rgba(83, 214, 206, 0.25);
  background: linear-gradient(180deg, rgba(20, 31, 37, 0.95), rgba(15, 23, 29, 0.92));
  box-shadow: 0 32px 80px rgba(0, 0, 0, 0.45);
  z-index: 100;
}

.add-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid rgba(83, 214, 206, 0.16);
  background: radial-gradient(360px 100px at 10% 0%, rgba(83, 214, 206, 0.18), transparent 68%),
    linear-gradient(180deg, rgba(28, 134, 122, 0.45), rgba(15, 60, 74, 0.32));
}

.add-modal-title {
  font-size: 15px;
  font-weight: 800;
  color: rgba(207, 247, 242, 0.96);
}

.add-modal-close {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.08);
  color: rgba(203, 230, 236, 0.75);
  font-size: 14px;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.add-modal-close:hover {
  background: rgba(255, 255, 255, 0.14);
  color: rgba(236, 246, 250, 0.95);
}

.add-modal-body {
  padding: 18px;
  min-height: 300px;
  max-height: 400px;
  overflow: auto;
}

.add-modal-split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  height: 100%;
}

.add-modal-left,
.add-modal-right {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.add-modal-section-title {
  font-size: 13px;
  font-weight: 800;
  color: rgba(207, 247, 242, 0.9);
}

.add-modal-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  flex: 1;
}

.add-modal-tag {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid rgba(83, 214, 206, 0.25);
  background: rgba(0, 0, 0, 0.2);
  color: rgba(207, 247, 242, 0.9);
  font-size: 13px;
}

.add-modal-tag-delete {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  border: 1px solid rgba(217, 54, 62, 0.4);
  background: rgba(217, 54, 62, 0.1);
  color: rgba(217, 54, 62, 0.8);
  font-size: 12px;
  cursor: pointer;
}

.add-form-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.add-form-item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.add-form-input {
  flex: 1;
  height: 36px;
  border-radius: 8px;
  border: 1px solid rgba(83, 214, 206, 0.25);
  background: rgba(0, 0, 0, 0.2);
  color: rgba(255, 255, 255, 0.9);
  padding: 0 12px;
  font-size: 13px;
}

.add-form-input:focus {
  outline: none;
  border-color: rgba(83, 214, 206, 0.5);
}

.add-form-delete {
  width: 32px;
  height: 36px;
  border-radius: 8px;
  border: 1px solid rgba(217, 54, 62, 0.4);
  background: rgba(217, 54, 62, 0.15);
  color: rgba(217, 54, 62, 0.9);
  font-size: 14px;
  cursor: pointer;
}

.add-form-delete:hover {
  background: rgba(217, 54, 62, 0.25);
}

.add-form-add-btn {
  margin-top: 12px;
  height: 36px;
  border-radius: 8px;
  border: 1px solid rgba(83, 214, 206, 0.3);
  background: rgba(83, 214, 206, 0.15);
  color: rgba(83, 214, 206, 0.95);
  font-size: 13px;
  font-weight: 700;
  padding: 0 16px;
  cursor: pointer;
}

.add-form-add-btn:hover {
  background: rgba(83, 214, 206, 0.25);
}

.add-modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding-top: 16px;
  border-top: 1px solid rgba(83, 214, 206, 0.16);
  margin-top: 16px;
}

.add-modal-btn {
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  border: 1px solid transparent;
}

.add-modal-btn-secondary {
  background: rgba(255, 255, 255, 0.1);
  color: rgba(255, 255, 255, 0.85);
  border-color: rgba(255, 255, 255, 0.15);
}

.add-modal-btn-secondary:hover {
  background: rgba(255, 255, 255, 0.15);
}

.add-modal-btn-primary {
  background: rgba(83, 214, 206, 0.25);
  color: rgba(83, 214, 206, 0.95);
  border-color: rgba(83, 214, 206, 0.3);
}

.add-modal-btn-primary:hover {
  background: rgba(83, 214, 206, 0.35);
}

.toast {
  position: fixed;
  left: 50%;
  top: 16px;
  transform: translateX(-50%);
  z-index: 9999;
  padding: 10px 20px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 700;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

.toast-success {
  background: rgba(83, 214, 206, 0.25);
  border: 1px solid rgba(83, 214, 206, 0.4);
  color: rgba(83, 214, 206, 0.95);
}

.toast-error {
  background: rgba(217, 54, 62, 0.25);
  border: 1px solid rgba(217, 54, 62, 0.4);
  color: rgba(255, 255, 255, 0.95);
}
</style>
