/* 表格制作器 /tools/tables —— 三端排版与配色
 *
 * 排版策略：
 *  · 桌面 ≥1000px：容器占满屏幕（工具箱规范）；顶部操作条吸顶（偏移量由 JS 量出
 *    站点吸顶 .page-header 的高度写入 --tb-stick，避免被遮挡）；左编辑区自适应，
 *    右侧控制面板定宽 330~380px 并吸顶。
 *  · 平板 680-999px：单列，控制面板两列并排。
 *  · 手机 ≤680px：单列、按钮整行铺满、触控区 ≥44px、输入 16px 防 iOS 缩放，
 *    表格区域横向滚动而不压缩列宽。
 *  · ≤420px：主题按钮两列、间距收紧。
 *
 * 主题：6 套（默认 / 浅色 / 中等 / 深色 / 样式 1 / 样式 2）全部通过
 *      .tb-table 上的 CSS 变量实现，用户自定义颜色由 JS 写 inline 变量覆盖，
 *      导出图片时读取同一套变量，屏幕与导出观感一致。
 */

.tb-page {
    width: 100%;
    max-width: none;
    margin: 0;
    padding: 24px clamp(16px, 2.4vw, 40px) 64px;
    box-sizing: border-box;
}

/* ---------- Hero ---------- */
.tb-hero { text-align: center; padding: 4px 0 clamp(16px, 2.2vw, 24px); }
.tb-hero-ico {
    width: clamp(44px, 4.6vw, 54px);
    height: clamp(44px, 4.6vw, 54px);
    margin: 0 auto 10px;
    border-radius: 15px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(22px, 2.4vw, 27px);
    background: color-mix(in srgb, var(--accent) 14%, transparent);
    border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
}
.tb-hero h1 { margin: 0 0 6px; font-size: clamp(20px, 2.2vw, 26px); font-weight: 700; letter-spacing: .4px; }
.tb-hero-sub { margin: 0 auto; max-width: 720px; font-size: 13.5px; color: var(--text-secondary); line-height: 1.7; }

/* ---------- 顶部操作条 ---------- */
.tb-bar {
    position: sticky;
    top: var(--tb-stick, 76px);
    z-index: 30;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-wrap: wrap;
    margin-top: 8px;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: color-mix(in srgb, var(--bg-secondary) 94%, transparent);
    backdrop-filter: blur(8px);
    box-shadow: 0 1px 2px rgba(0, 0, 0, .04);
}
.tb-bar-l { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1 1 200px; }
.tb-bar-r { display: flex; gap: 8px; flex-wrap: wrap; }
.tb-name { flex: 0 1 220px; font-weight: 600; }
.tb-size { font-size: 12.5px; color: var(--text-secondary); white-space: nowrap; }

/* ---------- 工作区 ---------- */
.tb-row { display: grid; grid-template-columns: 1fr; gap: 18px; margin-top: 16px; }
.tb-editor, .tb-panel { min-width: 0; }

.tb-editor {
    border: 1px solid var(--border);
    border-radius: 16px;
    background: var(--bg-secondary);
    padding: 14px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .04);
}
.tb-grid-bar {
    display: flex;
    gap: 14px 20px;
    flex-wrap: wrap;
    align-items: center;
    margin-bottom: 10px;
}
.tb-grp { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.tb-grp-t { font-size: 12.5px; color: var(--text-secondary); margin-right: 2px; }
.tb-cap-row {
    margin-bottom: 12px;
    padding-bottom: 12px;
    border-bottom: 1px dashed var(--border);   /* 控制区与表格区之间的层次分隔 */
}
.tb-cap-row .tb-input { width: 100%; }

.tb-grid-wrap {
    position: relative;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--bg-primary);
    padding: 10px;
    overflow-x: auto;
    min-height: 260px;
}
.tb-hint { margin: 10px 0 0; font-size: 12.5px; color: var(--text-secondary); line-height: 1.7; }
.tb-empty { margin: 20px 0; text-align: center; font-size: 13.5px; color: var(--text-secondary); }
.tb-empty[hidden] { display: none !important; }

/* ---------- 表格本体与 6 套主题 ---------- */
.tb-table {
    --tb-head-bg: #e9ecef;
    --tb-head-color: #212529;
    --tb-body-color: #212529;
    --tb-border: #dee2e6;
    --tb-border-w: 1px;
    --tb-zebra: #f8f9fa;
    --tb-radius: 6px;
    --tb-pad: 8px;
    --tb-font: 14px;
    --tb-align-head: left;
    --tb-align-body: left;

    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: var(--tb-font);
    border-radius: var(--tb-radius);
    overflow: hidden;
    color: var(--tb-body-color);
    background: var(--bg-primary);
}
.tb-table.theme-default { }
.tb-table.theme-light { --tb-head-bg: #ffffff; --tb-head-color: #000000; --tb-border: #e9ecef; --tb-zebra: #fafbfc; }
.tb-table.theme-medium { --tb-head-bg: #495057; --tb-head-color: #ffffff; --tb-border: #ced4da; --tb-zebra: #f1f3f5; }
.tb-table.theme-dark { --tb-head-bg: #212529; --tb-head-color: #ffffff; --tb-border: #3a3f45; --tb-zebra: #2b3035; }
.tb-table.theme-style1 {
    --tb-head-bg: transparent;
    --tb-head-color: #0d6efd;
    --tb-border: transparent;
    --tb-border-w: 0 0 1px 0;
    --tb-zebra: transparent;
}
.tb-table.theme-style2 { --tb-head-bg: #0d6efd; --tb-head-color: #ffffff; --tb-border: #0a58ca; --tb-zebra: #eef4ff; }

.tb-table caption {
    caption-side: top;
    text-align: left;
    font-size: var(--tb-font);
    color: var(--text-secondary);
    padding: 0 2px 8px;
    font-weight: 600;
}
.tb-table th,
.tb-table td {
    position: relative;
    padding: var(--tb-pad);
    border: var(--tb-border-w) solid var(--tb-border);
    text-align: var(--tb-align-body);
    vertical-align: middle;
    word-break: break-word;
    white-space: pre-wrap;   /* 单元格内 \n 需按硬换行显示 */
    min-width: 44px;
}
.tb-table th {
    background: var(--tb-head-bg);
    color: var(--tb-head-color);
    text-align: var(--tb-align-head);
    font-weight: 600;
}
.tb-table tbody tr:nth-child(even) { background: var(--tb-zebra); }
.tb-table.zebra-off tbody tr:nth-child(even) { background: transparent; }
.tb-table.hover-row tbody tr:hover td { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.tb-table td.col-hover,
.tb-table th.col-hover { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.tb-table td.is-focus, .tb-table th.is-focus { box-shadow: inset 0 0 0 2px var(--accent); }
.tb-table th[contenteditable], .tb-table td[contenteditable] { cursor: text; }
.tb-table th:focus, .tb-table td:focus { outline: none; }

/* 列宽拖拽手柄 */
.tb-resizer {
    position: absolute;
    right: -4px;
    top: 0;
    width: 9px;
    height: 100%;
    cursor: col-resize;
    z-index: 2;
}
.tb-resizer::after {
    content: '';
    position: absolute;
    left: 4px;
    top: 22%;
    width: 1px;
    height: 56%;
    background: color-mix(in srgb, var(--text-primary) 22%, transparent);
    transition: background .15s ease;
}
.tb-resizer:hover::after { background: var(--accent); }

/* ---------- 右侧面板 ---------- */
.tb-panel { display: flex; flex-direction: column; gap: 12px; }
.tb-tabs {
    display: flex;
    padding: 3px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--bg-secondary);
}
.tb-tab {
    flex: 1 1 0;
    border: 0;
    background: transparent;
    color: var(--text-secondary);
    border-radius: 9px;
    padding: 8px 6px;
    font-size: 13.5px;
    font-family: inherit;
    cursor: pointer;
    min-height: 36px;
    transition: background .15s ease, color .15s ease;
}
.tb-tab.is-on { background: var(--accent); color: #fff; font-weight: 600; }
.tb-tab:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.tb-pane { display: none; flex-direction: column; gap: 12px; }
.tb-pane.is-on { display: flex; }

.tb-card {
    border: 1px solid var(--border);
    border-radius: 16px;
    background: var(--bg-secondary);
    padding: 13px 15px 15px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .04);
}
.tb-card-h { font-size: 13.5px; font-weight: 700; margin-bottom: 10px; }
.tb-note { margin: 10px 0 0; font-size: 12px; color: var(--text-secondary); line-height: 1.7; }

/* ---------- 主题选择 ---------- */
.tb-themes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tb-theme {
    border: 1px solid var(--border);
    background: var(--bg-primary);
    border-radius: 10px;
    padding: 7px 6px 6px;
    cursor: pointer;
    font-family: inherit;
    display: flex;
    flex-direction: column;
    gap: 5px;
    align-items: center;
    transition: border-color .15s ease, background .15s ease;
}
.tb-theme:hover { border-color: var(--accent); }
.tb-theme.is-on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--bg-primary)); }
.tb-theme:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tb-theme span { font-size: 11.5px; color: var(--text-secondary); }
.tb-theme.is-on span { color: var(--accent); }
/* 迷你预览 */
.tb-prev {
    width: 100%;
    height: 30px;
    border-radius: 4px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    border: 1px solid var(--border);
}
.tb-prev i { flex: 1 1 0; display: block; }

/* ---------- 表单 ---------- */
.tb-grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 12px; }
.tb-f { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--text-secondary); }
.tb-f-btns { justify-content: flex-end; flex-direction: row; gap: 6px; }
.tb-mt { margin-top: 10px; }
.tb-v { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: var(--text-primary); font-size: 12px; }
.tb-switches { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.tb-sw { display: flex; align-items: center; gap: 9px; font-size: 13.5px; color: var(--text-primary); cursor: pointer; min-height: 34px; }
.tb-sw input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--accent); }

.tb-input {
    background: var(--bg-primary);
    border: 1px solid var(--border);
    color: var(--text-primary);
    border-radius: 9px;
    padding: 8px 10px;
    font-size: 14px;
    font-family: inherit;
    min-width: 0;
    width: 100%;
}
.tb-input:focus { outline: none; border-color: var(--accent); }
.tb-input-sm { width: auto; min-width: 110px; }
.tb-area {
    width: 100%;
    background: var(--bg-primary);
    border: 1px solid var(--border);
    color: var(--text-primary);
    border-radius: 10px;
    padding: 9px 10px;
    font-size: 13px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    line-height: 1.6;
    resize: vertical;
}
.tb-area:focus { outline: none; border-color: var(--accent); }
.tb-row-btns { display: flex; gap: 8px; margin-top: 9px; flex-wrap: wrap; }
.tb-exp2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tb-f input[type="color"] {
    width: 100%;
    height: 34px;
    padding: 2px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-primary);
    cursor: pointer;
}
.tb-f input[type="range"] { accent-color: var(--accent); height: 26px; }

/* ---------- 表格库 ---------- */
.tb-lib { display: flex; flex-direction: column; gap: 7px; max-height: 320px; overflow-y: auto; }
.tb-lib-item {
    display: flex;
    align-items: center;
    gap: 9px;
    width: 100%;
    border: 1px solid var(--border);
    background: var(--bg-primary);
    border-radius: 10px;
    padding: 8px 10px;
    cursor: pointer;
    font-family: inherit;
    font-size: 13px;
    color: var(--text-primary);
    text-align: left;
    transition: border-color .15s ease;
}
.tb-lib-item:hover { border-color: var(--accent); }
.tb-lib-item.is-on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--bg-primary)); font-weight: 600; }
.tb-lib-item:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tb-lib-n { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tb-lib-m { flex: 0 0 auto; font-size: 11.5px; color: var(--text-secondary); }

/* ---------- 按钮 ---------- */
.tb-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 38px;
    border: 1px solid var(--border);
    background: var(--bg-primary);
    color: var(--text-primary);
    border-radius: 10px;
    padding: 9px 15px;
    font-size: 14px;
    font-family: inherit;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    white-space: nowrap;
    transition: border-color .15s ease, color .15s ease, filter .15s ease;
}
.tb-btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.tb-btn:active:not(:disabled) { transform: translateY(1px); }
.tb-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tb-btn:disabled { opacity: .5; cursor: not-allowed; }
.tb-btn-sm { min-height: 34px; padding: 6px 12px; font-size: 13px; }
.tb-btn-pri { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.tb-btn-pri:hover:not(:disabled) { filter: brightness(1.08); color: #fff; }
.tb-btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.tb-btn-danger:hover:not(:disabled) { border-color: var(--danger); color: var(--danger); }

/* ---------- 说明 ---------- */
.tb-help {
    max-width: 940px;
    margin: 18px auto 0;
    border: 1px solid var(--border);
    border-radius: 16px;
    background: var(--bg-secondary);
    padding: 14px 18px;
}
.tb-help > summary { cursor: pointer; font-size: 15px; font-weight: 700; list-style: none; }
.tb-help > summary::-webkit-details-marker { display: none; }
.tb-help > summary::before { content: '▾ '; color: var(--accent); }
.tb-help[open] > summary::before { content: '▴ '; }
.tb-help-ol { margin: 12px 0 0; padding-left: 22px; font-size: 14px; line-height: 1.9; }
.tb-help-ol code { background: var(--bg-primary); border: 1px solid var(--border); border-radius: 5px; padding: 1px 5px; font-size: 12.5px; }
.tb-tip {
    margin-top: 12px;
    padding: 12px 14px;
    border-radius: 12px;
    background: color-mix(in srgb, var(--accent) 9%, var(--bg-primary));
    border: 1px solid color-mix(in srgb, var(--accent) 24%, transparent);
    font-size: 13.5px;
    line-height: 1.85;
}
.tb-tip ul { margin: 6px 0 0; padding-left: 22px; }

/* ---------- Toast ---------- */
.tb-toast {
    position: fixed;
    left: 50%;
    bottom: 34px;
    transform: translateX(-50%);
    background: var(--text-primary);
    color: var(--bg-primary);
    padding: 10px 18px;
    border-radius: 10px;
    font-size: 14px;
    z-index: 100000;
    box-shadow: 0 8px 26px rgba(0, 0, 0, .22);
    max-width: 88vw;
    text-align: center;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
}
.tb-toast[hidden] { display: none !important; }

/* ================= 响应式 ================= */
@media (min-width: 1000px) {
    .tb-row {
        grid-template-columns: minmax(0, 1fr) minmax(330px, 380px);
        align-items: start;
    }
    .tb-panel {
        position: sticky;
        /* 偏移 = 站点吸顶头实测高度 + 操作条实测高度（操作条换行成两行时也能正确避让） */
        top: calc(var(--tb-stick, 76px) + var(--tb-bar-h, 58px) + 10px);
        max-height: calc(100vh - var(--tb-stick, 76px) - var(--tb-bar-h, 58px) - 28px);
        overflow-y: auto;
        padding-right: 2px;
    }
}

@media (min-width: 680px) and (max-width: 999px) {
    /* 平板：保持页签切换（否则四张卡会一次性全铺出来，页面过长且失去焦点），
       但把当前页签内的卡片排成两列，减少纵向滚动 */
    .tb-pane.is-on {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 12px;
        align-items: start;
    }
    /* 平板宽度富余，四个数据导出按钮排成一行，减少竖向占用 */
    .tb-exp2 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

@media (max-width: 900px) {
    .tb-hero h1 { font-size: 21px; }
}

@media (max-width: 680px) {
    .tb-page { padding: 16px 14px 48px; }
    .tb-bar { position: static; padding: 10px 12px; }
    .tb-bar-l { flex: 1 1 100%; }
    .tb-name { flex: 1 1 auto; }
    .tb-bar-r {
        width: 100%;
        /* ★ 原来用 flex:1 1 0 平分，375px 屏每格只有约 60px，而「导出图片」是
           nowrap（实际需约 86px），flex 子项 min-width:auto 不会缩到内容以下，
           整行会溢出屏幕。改 3 列网格：撤销/重做/示例 一行，新建 + 导出图片 一行。 */
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 8px;
    }
    .tb-bar-r .tb-btn { width: 100%; min-width: 0; min-height: 44px; padding: 8px 6px; }
    .tb-bar-r #tbNew { grid-column: 1; }
    .tb-bar-r .tb-btn-pri { grid-column: 2 / -1; }
    .tb-input { font-size: 16px; }     /* 防 iOS 聚焦缩放 */
    .tb-grid-bar { gap: 10px; }
    .tb-grp { width: 100%; }
    .tb-grp .tb-btn { flex: 1 1 0; min-height: 40px; }
    .tb-tab { min-height: 42px; }
    .tb-exp2 { grid-template-columns: 1fr; }
    .tb-editor { padding: 12px; }
    .tb-help { padding-left: 16px; padding-right: 16px; border-radius: 14px; }
    .tb-panel .tb-btn { min-height: 44px; }
}

@media (max-width: 420px) {
    .tb-themes { grid-template-columns: repeat(2, 1fr); }
    .tb-grid2 { grid-template-columns: 1fr; }
    .tb-hero-ico { width: 44px; height: 44px; font-size: 22px; }
    .tb-card { padding: 12px 13px 14px; }
}

@media (prefers-reduced-motion: reduce) {
    .tb-btn, .tb-tab, .tb-theme, .tb-resizer::after { transition: none !important; }
    .tb-btn:active { transform: none !important; }
}
