/* =============================================================
   明细网格统一单元格样式（Excel 式"格子即输入区"）
   -------------------------------------------------------------
   接入：在 <table> 上加 class="cell-grid"（可选 data-cell-nav="off" 关闭方向键导航）
   角色类：
     td.cell-ro / td.readonly-cell  只读格（灰底，文字不可改）
     td.cell-act                    操作格（按钮/复选框/上传块，保留内边距）
     td.cell-multi                  复合格（格内多个控件，如 输入+选+填充 按钮）
   可选：table.cell-grid--roomy     行高 36px（默认 32px）

   约束：只作用于「单元格直接子级的裸控件」，不改动 name / value / 提交结构。
   ============================================================= */

.cell-grid {
    --cg-h: 32px;
    --cg-line: #d9d9d9;
    --cg-ro-bg: #f5f5f5;
    --cg-ro-text: #666;
    --cg-focus: #2196F3;
    --cg-focus-bg: #fffdf0;
    --cg-hover: #f7fbff;
    --cg-selected: #bae7ff;
    --cg-font: 14px;
    border-collapse: collapse;
}

.cell-grid.cell-grid--roomy { --cg-h: 36px; }

/* ---------- 单元格本体：单层线，内边距归零 ---------- */
.cell-grid td {
    padding: 0;
    height: var(--cg-h);
    border: 1px solid var(--cg-line);
    vertical-align: middle;
}

/* 纯文本 / 标签格（格内没有录入控件）：保留内边距，避免文字贴线 */
.cell-grid td:not(:has(> input, > select, > textarea)):not(.cell-act):not(.cell-multi) {
    padding: 0 8px;
}

/* 只放复选框/单选框的格（如默认行、勾选列）：保留少量内边距，控件不贴线 */
.cell-grid td:has(> input[type="checkbox"]),
.cell-grid td:has(> input[type="radio"]) {
    padding: 2px 6px;
}

/* 不支持 :has() 的旧浏览器：降级为“单层框 + 缩进的录入框”，仍不出现格子套格子 */
@supports not selector(:has(*)) {
    .cell-grid td {
        padding: 4px 6px;
        height: auto;
        min-height: var(--cg-h);
    }
    .cell-grid td > input,
    .cell-grid td > select,
    .cell-grid td > textarea {
        min-height: calc(var(--cg-h) - 10px);
    }
}

/* 操作格 / 复合格：保留内边距，内部控件按原有方式排列 */
.cell-grid td.cell-act,
.cell-grid td.cell-multi { padding: 2px 4px; }

/* 合计行：保留内边距与原有排版 */
.cell-grid tfoot td {
    padding: 5px 6px;
    height: auto;
    background-color: #fafafa;
}

/* ---------- 裸控件：铺满整格、去内框、透明底 ---------- */
.cell-grid td:not(.cell-act):not(.cell-multi) > input:not([type="checkbox"]):not([type="radio"]):not([type="file"]):not([type="hidden"]),
.cell-grid td:not(.cell-act):not(.cell-multi) > select,
.cell-grid td:not(.cell-act):not(.cell-multi) > textarea {
    display: block;
    width: 100%;
    height: 100%;
    min-height: calc(var(--cg-h) - 2px);
    margin: 0;
    padding: 0 8px;
    border: 0;
    border-radius: 0;
    background-color: transparent;
    font-family: inherit;
    font-size: var(--cg-font);
    color: inherit;
    outline: none;
}

/* 多行文本格：高度由行高内容决定，不锁死 */
.cell-grid td:not(.cell-act):not(.cell-multi) > textarea {
    height: auto;
    padding: 6px 8px;
    line-height: 1.45;
    resize: none;
}

/* 复合格 / 操作格内的输入（如“输入+向下填充按钮”）：由格内布局控尺寸，只去内框 */
.cell-grid td.cell-multi input,
.cell-grid td.cell-act input,
.cell-grid td .purchase-address-cell > input {
    border: 0;
    border-radius: 0;
    background-color: transparent;
    box-shadow: none;
    font-family: inherit;
    font-size: inherit;
    color: inherit;
}
/* “输入+按钮”复合格：格内自带 flex 布局，保留少量内边距 */
.cell-grid td:has(.purchase-address-cell) { padding: 2px 4px; }
.cell-grid td .purchase-address-cell > input:focus,
.cell-grid td.cell-multi input:focus {
    background-color: var(--cg-focus-bg);
    box-shadow: inset 0 0 0 2px var(--cg-focus);
}

/* 带 fit-width 的格内输入（PDF 导入确认页的数量/单价）：保持按内容自适应宽度 */
.cell-grid td:not(.cell-act):not(.cell-multi) > input.fit-width {
    width: auto;
    min-width: 8ch;
    box-sizing: content-box;
}

/* ---------- 数字：去掉上下小箭头（Excel 无箭头） ---------- */
.cell-grid td > input[type="number"] {
    -moz-appearance: textfield;
    appearance: textfield;
}
.cell-grid td > input[type="number"]::-webkit-outer-spin-button,
.cell-grid td > input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

/* ---------- 日期/时间：保留原生日历按钮，仅收齐边距 ---------- */
.cell-grid td > input[type="date"]::-webkit-calendar-picker-indicator,
.cell-grid td > input[type="datetime-local"]::-webkit-calendar-picker-indicator,
.cell-grid td > input[type="time"]::-webkit-calendar-picker-indicator,
.cell-grid td > input[type="month"]::-webkit-calendar-picker-indicator {
    margin: 0 2px 0 0;
    padding: 0;
    opacity: .55;
    cursor: pointer;
}
.cell-grid td > input[type="date"]:hover::-webkit-calendar-picker-indicator,
.cell-grid td > input[type="datetime-local"]:hover::-webkit-calendar-picker-indicator,
.cell-grid td > input[type="time"]:hover::-webkit-calendar-picker-indicator {
    opacity: .9;
}

/* ---------- 下拉：去原生边框，自绘箭头（Chrome/Edge/Firefox 一致） ---------- */
.cell-grid td:not(.cell-act):not(.cell-multi) > select {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    padding-right: 22px;
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath fill='%23888' d='M0 0l5 6 5-6z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 7px center;
    background-size: 9px 5px;
}
.cell-grid td.cell-multi > select { padding-right: 18px; }

/* ---------- 聚焦：蓝色内描边 + 浅黄底（与样品登记工序表一致）
   注意：选择器需比上面的基础规则更具体（多排除 readonly/disabled），
   否则基础规则的 transparent 底会压掉聚焦高亮。 ---------- */
.cell-grid td:not(.cell-act):not(.cell-multi) > input:not([type="checkbox"]):not([type="radio"]):not([type="file"]):not([type="hidden"]):not([readonly]):not([disabled]):focus,
.cell-grid td:not(.cell-act):not(.cell-multi) > select:not([disabled]):focus,
.cell-grid td:not(.cell-act):not(.cell-multi) > textarea:not([readonly]):not([disabled]):focus {
    background-color: var(--cg-focus-bg);
    box-shadow: inset 0 0 0 2px var(--cg-focus);
}

/* ---------- 只读格：灰底；只读控件无聚焦高亮（上面的聚焦规则已排除） ---------- */
.cell-grid td.cell-ro,
.cell-grid td.readonly-cell { background-color: var(--cg-ro-bg); }

.cell-grid td > input[readonly],
.cell-grid td > input[disabled],
.cell-grid td > select[disabled],
.cell-grid td > textarea[readonly] {
    color: var(--cg-ro-text);
    cursor: default;
}

/* ---------- 行悬停：不覆盖只读格与操作格底色 ---------- */
.cell-grid tbody tr:hover > td:not(.cell-ro):not(.readonly-cell):not(.cell-act) {
    background-color: var(--cg-hover);
}

/* ---------- 选区高亮（沿用系统现有口径） ---------- */
.cell-grid td.cell-selected { background-color: var(--cg-selected) !important; }
.cell-grid td.cell-selected > input,
.cell-grid td.cell-selected > select,
.cell-grid td.cell-selected > textarea { background-color: transparent; }

/* ---------- 打印：屏幕无线，纸面清晰；灰底保持 ---------- */
@media print {
    .cell-grid td { border: 1px solid #999; }
    .cell-grid td.cell-ro,
    .cell-grid td.readonly-cell {
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
}

/* ---------- 小屏：避免 iOS 聚焦缩放 ---------- */
@media (max-width: 768px) {
    .cell-grid { --cg-font: 16px; }
}