/* ╔══════════════════════════════════════════════╗
   ║  Excel 在线预览 — Excel 风格 UI 样式表      ║
   ║  参考微软 Excel Office 365 视觉设计          ║
   ╚══════════════════════════════════════════════╝ */

/* ──────────── CSS 变量 (Excel 配色) ──────────── */
:root {
    --excel-green: #217346;
    --excel-green-dark: #185a37;
    --excel-green-light: #e6f2ea;
    --header-bg: #f0f0f0;
    --header-border: #c6c6c6;
    --grid-line: #d4d4d4;
    --grid-line-light: #e8e8e8;
    --selected-border: #217346;
    --selected-bg: #e6f2ea;
    --cell-hover-bg: #e8f0fe;
    --formula-bg: #ffffff;
    --sheet-tab-active: #ffffff;
    --sheet-tab-inactive: #e0e0e0;
    --sheet-tab-hover: #d0d0d0;
    --status-bar-bg: #f0f0f0;

    --text-primary: #333333;
    --text-secondary: #666666;
    --text-muted: #999999;
    --white: #ffffff;
}

/* ──────────── 全局重置 ──────────── */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    overflow: hidden;
    font-family: "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
    font-size: 13px;
    color: var(--text-primary);
    background: var(--white);
    -webkit-font-smoothing: antialiased;
}

/* ──────────── App 容器 ──────────── */
.excel-app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
}

/* ═══════════════ 1. 公式栏 ═══════════════ */
.formula-bar {
    display: flex;
    align-items: center;
    height: 28px;
    min-height: 28px;
    background: var(--formula-bg);
    border-bottom: 1px solid var(--grid-line);
    padding: 0 4px;
    gap: 0;
    z-index: 98;
}
.formula-cell-ref {
    width: 72px;
    min-width: 72px;
    height: 22px;
    line-height: 22px;
    text-align: center;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-primary);
    border: 1px solid var(--grid-line);
    border-radius: 3px;
    background: var(--white);
}
.formula-sep {
    width: 28px;
    text-align: center;
    font-size: 12px;
    font-style: italic;
    color: var(--text-muted);
    cursor: default;
}
.formula-content {
    flex: 1;
    height: 22px;
    line-height: 22px;
    padding: 0 6px;
    font-size: 12px;
    color: var(--text-primary);
    border: 1px solid var(--grid-line);
    border-radius: 3px;
    background: var(--white);
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

/* ═══════════════ 2. 上传覆盖层 ═══════════════ */
.upload-overlay {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f5f6f7;
}

.upload-dropzone {
    width: 520px;
    max-width: 90vw;
    padding: 56px 40px;
    background: var(--white);
    border: 2px dashed #c8ccd0;
    border-radius: 8px;
    text-align: center;
    cursor: pointer;
    transition: all 0.25s ease;
}
.upload-dropzone:hover,
.upload-dropzone.drag-over {
    border-color: var(--excel-green);
    background: #f5faf7;
    box-shadow: 0 4px 20px rgba(33,115,70, 0.08);
}
.upload-icon-large {
    font-size: 56px;
    margin-bottom: 16px;
    line-height: 1;
}
.upload-main-text {
    font-size: 16px;
    color: var(--text-primary);
    margin-bottom: 8px;
    font-weight: 500;
}
.upload-sub-text {
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 12px;
}
.upload-btn-green {
    display: inline-block;
    padding: 10px 32px;
    background: var(--excel-green);
    color: var(--white);
    font-size: 14px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.2s;
    font-weight: 500;
}
.upload-btn-green:hover {
    background: var(--excel-green-dark);
}
.upload-limit-text {
    margin-top: 16px;
    font-size: 11px;
    color: var(--text-muted);
}

/* ── 上传进度 ── */
.upload-progress-bar {
    text-align: center;
    padding: 40px;
}
.spinner-green {
    width: 36px;
    height: 36px;
    border: 3px solid #e0e0e0;
    border-top-color: var(--excel-green);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
    margin: 0 auto 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.upload-progress-bar p {
    font-size: 13px;
    color: var(--text-secondary);
}

/* ═══════════════ 3. 表格网格容器 ═══════════════ */
.grid-container {
    flex: 1;
    overflow: hidden;
    position: relative;
    background: var(--white);
}
.grid-scroll {
    width: 100%;
    height: 100%;
    overflow: auto;
    position: relative;  /* 必须！让 img-overlay 绝对定位参考此容器，随滚动移动 */
    will-change: scroll-position;
    overflow-anchor: none;  /* 禁止浏览器 Scroll Anchoring: spacer 高度变化不触发 scrollTop 补偿 → 无反馈循环 */
}

/* ── Excel 网格表格 ── */
.excel-grid {
    border-collapse: collapse;
    table-layout: fixed;
}

/* 列头 */
.excel-grid thead th {
    position: sticky;
    top: 0;
    z-index: 3;
    background: var(--header-bg);
    color: var(--text-secondary);
    font-weight: 400;
    font-size: 10pt;  /* Excel 列头字号 */
    text-align: center;
    padding: 3px 4px;
    border-right: 1px solid var(--header-border);
    border-bottom: 1px solid var(--header-border);
    min-width: 72px;
    height: 24px;
    user-select: none;
    cursor: default;
}
.excel-grid thead th:hover {
    background: #e2e2e2;
}
/* 列头右边界 resize 热区 (ECMA-376 §18.3.1.13, Excel auto-fit) */
.excel-grid thead th::after {
    content: '';
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 5px;
    cursor: col-resize;
    z-index: 1;
}

/* 列头 - 左上角交叉格，模拟 Excel 三角形斜线分割 */
.excel-grid thead th.corner-header {
    z-index: 5;
    background: var(--header-bg);
    border-right: 1px solid var(--header-border);
    border-bottom: 1px solid var(--header-border);
    width: 46px;
    min-width: 46px;
    max-width: 46px;
    overflow: hidden;
    position: relative;
    padding: 0;
}
/* Excel 风格的右下角填充三角形 (border 绘制) */
.excel-grid thead th.corner-header .corner-triangle {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 0 0 14px 14px;
    border-color: transparent transparent var(--header-border) transparent;
    pointer-events: none;
}

/* 行头 (固定 46px，不受文件行数/字号影响) */
.excel-grid tbody th {
    position: sticky;
    left: 0;
    z-index: 2;
    background: var(--header-bg);
    color: var(--text-secondary);
    font-weight: 400;
    font-size: 10pt;
    text-align: center;
    padding: 0 4px;
    border-right: 1px solid var(--header-border);
    border-bottom: 1px solid var(--grid-line-light);
    width: 46px;
    min-width: 46px;
    max-width: 46px;
    min-height: 22px;
    overflow: hidden;
    user-select: none;
    cursor: default;
}
.excel-grid tbody th:hover {
    background: #e2e2e2;
}

/* 数据单元格 */
.excel-grid tbody td {
    padding: 2px 5px;
    font-size: 11pt;  /* Excel 默认 11 磅 */
    color: var(--text-primary);
    border-right: 1px solid var(--grid-line-light);
    border-bottom: 1px solid var(--grid-line-light);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 72px;
    max-width: 280px;
    min-height: 22px;
    cursor: cell;
    transition: background 0.05s;
    vertical-align: bottom;  /* Excel 默认垂直靠下 */
}
/* 自动换行单元格 — 与 Excel 的 Wrap Text 对齐 */
.excel-grid tbody td.cell-wrap {
    white-space: pre-wrap;
    word-break: break-word;
    min-height: 22px;
}
.excel-grid tbody td:hover {
    background: var(--cell-hover-bg);
    outline: 1px solid var(--excel-green);
    outline-offset: -1px;
    z-index: 1;
    position: relative;
}

/* 单元格文字不可选中 (拖拽多选时防止浏览器蓝色选中) */
.excel-grid tbody td {
    user-select: none;
    -webkit-user-select: none;
}

/* 锚点单元格 (Excel: 白底 + 深绿边框) */
.excel-grid tbody td.cell-selected {
    outline: 2px solid var(--excel-green);
    outline-offset: -2px;
    background: #ffffff;
    z-index: 2;
    position: relative;
}
/* 选区范围单元格 (Excel: 浅蓝背景，无内部边框) */
.excel-grid tbody td.cell-range {
    background: #cce5ff;
}

/* 列头选中态 */
.excel-grid thead th.highlight-col {
    background: #d4e8da !important;
    color: var(--excel-green);
}
.excel-grid thead th.col-selected {
    background: #b8d9b8 !important;
    color: var(--excel-green-dark);
    font-weight: 600;
}
/* 行头选中态 */
.excel-grid tbody th.highlight-row {
    background: #d4e8da !important;
    color: var(--excel-green);
}
.excel-grid tbody th.row-selected {
    background: #b8d9b8 !important;
    color: var(--excel-green-dark);
    font-weight: 600;
}

/* 单元格样式变体 */
.excel-grid td.cell-bold { font-weight: 700; }
.excel-grid td.cell-italic { font-style: italic; }
.excel-grid td.cell-underline { text-decoration: underline; }
.excel-grid td.cell-strike { text-decoration: line-through; }
/* 水平对齐 — ECMA-376 §18.8.1 Alignment.horizontal */
.excel-grid td.cell-left   { text-align: left; }
.excel-grid td.cell-right  { text-align: right; }
.excel-grid td.cell-center { text-align: center; }
/* ECMA-376 §18.8.1: justify=两端对齐, Excel中数字的justify表现为左对齐 */
.excel-grid td.cell-justify { text-align: justify; }
/* ECMA-376 §18.18.11: number 类型默认右对齐; 显式 ha=* 应覆盖默认 */
.excel-grid td.cell-number {
    text-align: right;
    font-variant-numeric: tabular-nums;
}
.excel-grid td.cell-number.cell-left   { text-align: left; }
.excel-grid td.cell-number.cell-center { text-align: center; }
.excel-grid td.cell-number.cell-right  { text-align: right; }
/* 垂直对齐 (Excel 默认靠下) */
.excel-grid td.cell-va-top { vertical-align: top; }
.excel-grid td.cell-va-center { vertical-align: middle; }
.excel-grid td.cell-va-bottom { vertical-align: bottom; }

/* 被合并单元格覆盖的隐藏单元格 */
.excel-grid td.merged-hidden {
    display: none;
}

/* ── 批注指示器（红色三角 + 原生 title 提示）ECMA-376 §18.7.3 ── */
.excel-grid td.cell-comment {
    position: relative;
}
/* 右上角红色三角 — 与 Excel 批注指示器一致 */
.excel-grid td.cell-comment::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    width: 0;
    height: 0;
    border-left: 7px solid transparent;
    border-top: 7px solid #E03E2D;  /* Excel 标准批注红色 */
    pointer-events: none;
}
/* 批注具体内容通过 HTML title 属性显示原生浏览器 tooltip，
   避免 overflow:hidden 裁剪绝对定位浮层的问题 */

/* ── 虚拟滚动占位行 (维持滚动条总高度, 无边框无内容) ── */
.excel-grid tbody .v-spacer-top,
.excel-grid tbody .v-spacer-bottom {
    height: 0;
    line-height: 0;
}
.excel-grid tbody .v-spacer-top td,
.excel-grid tbody .v-spacer-bottom td {
    padding: 0 !important;
    border: none !important;
    line-height: 0;
    font-size: 0;
}
/* 截断警告行 */
.excel-grid tbody .v-trunc-warn td {
    text-align: center;
    padding: 8px;
    color: #d13438;
    background: #fff3f3;
    font-size: 13px;
    font-weight: 600;
}

/* ═══════════════ 4. 底部 Sheet 标签栏 ═══════════════ */
.sheet-bar {
    display: flex;
    align-items: center;
    height: 30px;
    min-height: 30px;
    background: var(--header-bg);
    border-top: 1px solid var(--header-border);
    padding: 0 4px;
    gap: 0;
    z-index: 97;
    user-select: none;
    position: relative;  /* 为弹出菜单提供定位上下文 */
}
.sheet-tabs-scroll {
    display: flex;
    flex: 1;
    overflow-x: auto;
    overflow-y: hidden;
    gap: 2px;
    height: 100%;
    align-items: flex-end;
    padding-bottom: 2px;
    scrollbar-width: none;
    position: relative;  /* 为 fade 指示器提供定位上下文 */
}
.sheet-tabs-scroll::-webkit-scrollbar {
    display: none;
}

/* ── Sheet Tab 滚动溢出淡出指示器 (absolute 不占 flex 空间) ── */
.sheet-tabs-fade-left,
.sheet-tabs-fade-right {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 18px;
    pointer-events: none;
    z-index: 2;
    opacity: 0;
    transition: opacity 0.2s;
}
.sheet-tabs-fade-left {
    left: 0;
    background: linear-gradient(to right, var(--header-bg) 0%, transparent 100%);
}
.sheet-tabs-fade-right {
    right: 0;
    background: linear-gradient(to left, var(--header-bg) 0%, transparent 100%);
}
.sheet-tabs-fade-left.visible,
.sheet-tabs-fade-right.visible {
    opacity: 1;
}

/* 单个 Sheet Tab */
.sheet-tab-item {
    padding: 4px 16px;
    font-size: 12px;
    color: var(--text-secondary);
    background: var(--sheet-tab-inactive);
    border: 1px solid var(--header-border);
    border-bottom: none;
    border-radius: 6px 6px 0 0;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.15s;
    max-width: 140px;
    overflow: hidden;
    text-overflow: ellipsis;
    height: 26px;
    line-height: 18px;
    flex-shrink: 0;  /* 不压缩，保持可滚动 */
}
/* 多 Sheet 场景: 自动缩小 Tab 宽度，最少 52px */
.sheet-tab-item.compact {
    padding: 4px 10px;
    max-width: 110px;
    font-size: 11px;
}
.sheet-tab-item.ultra-compact {
    padding: 4px 6px;
    max-width: 80px;
    font-size: 10px;
}
.sheet-tab-item:hover {
    background: var(--sheet-tab-hover);
}
.sheet-tab-item.active {
    background: var(--sheet-tab-active);
    color: var(--excel-green);
    font-weight: 600;
    border-bottom: 3px solid var(--excel-green);
    height: 28px;
    padding-bottom: 6px;
}

/* Sheet 导航按钮 */
.sheet-nav {
    display: flex;
    gap: 1px;
    margin-right: 2px;
}
.sheet-nav-btn {
    width: 22px;
    height: 22px;
    border: 1px solid var(--header-border);
    background: var(--white);
    font-size: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 3px;
    color: var(--text-secondary);
    transition: all 0.15s;
}
.sheet-nav-btn:hover {
    background: var(--header-bg);
    color: var(--text-primary);
}

/* ── Sheet 列表按钮 (Excel 风格的 ☰) ── */
.sheet-list-btn {
    width: 24px;
    height: 24px;
    border: 1px solid transparent;
    background: transparent;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 3px;
    color: var(--text-secondary);
    transition: all 0.15s;
    margin-right: 2px;
    flex-shrink: 0;
}
.sheet-list-btn:hover {
    background: var(--white);
    border-color: var(--header-border);
    color: var(--text-primary);
}

/* ── Sheet 列表弹出菜单 ── */
.sheet-list-popup {
    position: fixed;
    bottom: 54px;  /* 30px(sheetBar) + 24px(statusBar) */
    left: 12px;
    width: 240px;
    max-height: 320px;
    background: var(--white);
    border: 1px solid var(--header-border);
    border-radius: 6px;
    box-shadow: 0 4px 24px rgba(0,0,0, 0.15);
    z-index: 200;
    display: flex;
    flex-direction: column;
    animation: popupIn 0.12s ease;
}
.sheet-list-popup.hidden {
    display: none;
}
@keyframes popupIn {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}
.sheet-list-inner {
    overflow-y: auto;
    max-height: 280px;
    padding: 6px 0;
}
.sheet-list-inner::-webkit-scrollbar {
    width: 6px;
}
.sheet-list-inner::-webkit-scrollbar-thumb {
    background: #c8c8c8;
    border-radius: 3px;
}
.sheet-list-item {
    padding: 7px 16px;
    font-size: 13px;
    color: var(--text-primary);
    cursor: pointer;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: background 0.1s;
    display: flex;
    align-items: center;
    gap: 8px;
}
.sheet-list-item:hover {
    background: var(--excel-green-light);
}
.sheet-list-item.active {
    background: #d4e8da;
    color: var(--excel-green);
    font-weight: 600;
}
.sheet-list-badge {
    font-size: 10px;
    color: var(--text-muted);
    min-width: 24px;
    text-align: right;
}

/* ═══════════════ 5. 状态栏 ═══════════════ */
.status-bar {
    display: flex;
    align-items: center;
    height: 24px;
    min-height: 24px;
    background: var(--status-bar-bg);
    border-top: 1px solid var(--header-border);
    padding: 0 10px;
    gap: 8px;
    font-size: 11px;
    color: var(--text-secondary);
    z-index: 96;
    user-select: none;
}
.status-text {
    white-space: nowrap;
}
.status-sep {
    color: var(--grid-line);
}
.status-right {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 8px;
}
.status-sheets {
    font-size: 11px;
    color: var(--text-muted);
}

/* ── 缩放控件 (ECMA-376 §18.2.11 zoomScale) ── */
.zoom-control {
    display: flex;
    align-items: center;
    gap: 4px;
}
.zoom-btn {
    width: 20px;
    height: 18px;
    border: 1px solid transparent;
    background: transparent;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 3px;
    color: var(--text-secondary);
    line-height: 1;
    padding: 0;
    transition: all 0.12s;
}
.zoom-btn:hover {
    background: #e0e0e0;
    border-color: var(--header-border);
    color: var(--text-primary);
}
.zoom-btn:active {
    background: #d0d0d0;
}
/* ── 缩放滑块 (Range Input) ── */
.zoom-slider {
    -webkit-appearance: none;
    appearance: none;
    width: 80px;
    height: 4px;
    background: #d0d0d0;
    border-radius: 2px;
    outline: none;
    cursor: pointer;
}
.zoom-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--excel-green);
    border: 2px solid var(--white);
    box-shadow: 0 1px 3px rgba(0,0,0, 0.2);
    cursor: pointer;
}
.zoom-slider::-moz-range-thumb {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--excel-green);
    border: 2px solid var(--white);
    box-shadow: 0 1px 3px rgba(0,0,0, 0.2);
    cursor: pointer;
}
.zoom-label {
    font-size: 11px;
    color: var(--text-secondary);
    min-width: 32px;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

/* ═══════════════ 6. 通用工具类 ═══════════════ */
.hidden { display: none !important; }

/* ═══════════════ 7. Toast 通知提示 ═══════════════ */
.excel-toast {
    position: fixed;
    bottom: 48px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0;
    background: #ffffff;
    color: var(--text-primary);
    border-radius: 8px;
    font-size: 13px;
    box-shadow: 0 6px 24px rgba(0,0,0, 0.15), 0 0 0 1px rgba(0,0,0, 0.06);
    z-index: 9999;
    animation: toastIn 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    max-width: 480px;
    overflow: hidden;
}
.excel-toast.hidden {
    display: none;
}

/* 左侧色条 */
.excel-toast::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 4px;
    background: #d13438;
    border-radius: 4px 0 0 4px;
}
/* 成功/警告变体 */
.excel-toast.toast-success::before { background: #107c10; }
.excel-toast.toast-warning::before { background: #ff8c00; }

/* 图标 */
.toast-icon {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
    margin-left: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
}

/* 消息文本 */
.toast-message {
    flex: 1;
    padding: 12px 0;
    line-height: 1.4;
}

/* 关闭按钮 */
.toast-close {
    flex-shrink: 0;
    width: 28px;
    height: 28px;
    margin-right: 6px;
    border: none;
    background: transparent;
    font-size: 16px;
    color: var(--text-muted);
    cursor: pointer;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.12s;
    line-height: 1;
}
.toast-close:hover {
    background: #f0f0f0;
    color: var(--text-primary);
}

@keyframes toastIn {
    from { opacity: 0; transform: translateX(-50%) translateY(16px) scale(0.95); }
    to { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
}
@keyframes toastOut {
    from { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
    to { opacity: 0; transform: translateX(-50%) translateY(-8px) scale(0.95); }
}
.excel-toast.toast-hiding {
    animation: toastOut 0.2s ease forwards;
}

/* ═══════════════ 8. 滚动条 Excel 风格 ═══════════════ */
.grid-scroll::-webkit-scrollbar {
    width: 12px;
    height: 12px;
}
.grid-scroll::-webkit-scrollbar-track {
    background: #f5f5f5;
    border-left: 1px solid var(--grid-line-light);
}
.grid-scroll::-webkit-scrollbar-thumb {
    background: #c8c8c8;
    border-radius: 6px;
    border: 2px solid transparent;
    background-clip: content-box;
}
.grid-scroll::-webkit-scrollbar-thumb:hover {
    background: #a0a0a0;
    border: 2px solid transparent;
    background-clip: content-box;
}
.grid-scroll::-webkit-scrollbar-corner {
    background: var(--header-bg);
}

/* ═══════════════ 9. 文件拖入整个窗口时的视觉反馈 ═══════════════ */
.excel-app.drag-active .upload-overlay {
    background: #e6f2ea;
}
.excel-app.drag-active .upload-dropzone {
    border-color: var(--excel-green);
    background: #f5faf7;
    box-shadow: 0 0 0 4px rgba(33,115,70, 0.12);
}
