/* NEW-215 (WP2) — hạng mục theo phụ lục trên trang phụ lục chia mục (views/appendix_items.js). Tiền tố api-; CHỈ token màu của app.css
   (sáng + tối), không gradient / glow / bóng. Bảng rộng cuộn ngang trong khung riêng (.table-wrap), cột "Hạng mục" dính trái; trang không cuộn ngang. */
.api { margin: 0 0 6px; }
.aps-items:not(:empty) { margin: 0 0 18px; }
.api-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; margin: 0 0 8px; }
.api-head .aps-h3 { margin: 0; }
.api-acts { display: inline-flex; flex-wrap: wrap; gap: 6px; margin-left: auto; }
.api-s { display: block; font-size: 12px; color: var(--muted); font-weight: 400; }
.api-head > .api-s, .api-grp-sum { display: inline; }
.api-note { font-size: 13px; margin: 0 0 8px; padding: 8px 10px; background: var(--surface-2); border-radius: var(--radius-btn); }
.api-msg { margin: 0 0 8px; font-size: 13px; display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; }
.api-empty { text-align: left; }
.api-err { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.api-negt { color: var(--err); }
.api-h4 { margin: 0; font-size: 14px; font-weight: 600; }

/* ---- bảng hạng mục ---- */
.api-tw { margin: 0 0 4px; }
.api-t { font-size: 13px; }
.api-t th[scope="row"] { font-size: 13px; text-transform: none; letter-spacing: 0; color: var(--ink); position: static; font-weight: 500; background: none;
  border-bottom: 1px solid var(--line-2); white-space: normal; }
.api-t td, .api-t th { vertical-align: top; }
.api-t thead th { white-space: nowrap; }
.api-c-stt { width: 36px; min-width: 36px; color: var(--muted); }
.api-c-name { min-width: 180px; max-width: 280px; }
/* cột đầu (Hạng mục / nội dung) dính trái khi bảng cuộn ngang (nền đặc theo token để chữ không chồng) */
.api-t .api-c-name, .api-t .api-c-first, .api-t th.api-c-name[scope], .api-t th.api-c-first[scope] { position: sticky; left: 0; z-index: 1; background: var(--surface); }
.api-t thead .api-c-name, .api-t thead .api-c-first { z-index: 2; }
.api-t tbody tr:hover th.api-c-name, .api-t tbody tr:hover th.api-c-first { background: var(--hover); }
.api-c-first { min-width: 160px; max-width: 260px; }
.api-kind { margin-bottom: 1px; }
.api-stt-m { display: none; }
.api-name strong { font-weight: 600; overflow-wrap: anywhere; }
.api-sub { color: var(--muted); margin-right: 4px; }
.api-st { display: inline; margin-left: 4px; }
.api-desc { overflow-wrap: anywhere; }
.api-badges { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; margin-top: 4px; }
.api-badge { font-weight: 600; }
.api-child .api-c-name { padding-left: 24px; }
.api-child td, .api-child th { background: var(--surface-2); }
.api-t .api-child .api-c-name, .api-t .api-child th.api-c-name[scope] { background: var(--surface-2); }
.api-off .api-name strong { text-decoration: line-through; color: var(--muted); }
.api-c-unit .api-in { width: 64px; }
.api-c-price .api-in, .api-c-amt .api-in { width: 112px; }
.api-c-qty .api-in { width: 56px; }
.api-c-vat .api-in { width: 168px; }
.api-c-note .api-in { width: 150px; }
.api-c-per { white-space: nowrap; }
.api-c-act { white-space: nowrap; text-align: right; }
.api-ro { font-variant-numeric: tabular-nums; }
/* ô sửa ngay trên dòng: viền nhạt (bảng đọc như bảng tính), đậm lên khi rê / đang gõ */
.api-in { width: 100%; min-height: 32px; padding: 4px 8px; font: inherit; font-size: 13px; color: var(--ink); background: var(--surface); border-color: var(--line); }
.api-in.api-num { text-align: right; font-variant-numeric: tabular-nums; }
.api-in:hover:not(:disabled) { border-color: var(--brand); }
.api-in:disabled { background: var(--surface-2); color: var(--muted); cursor: not-allowed; }
select.api-in { padding-right: 24px; }
.api-link { border: 0; padding: 0; background: none; color: var(--brand-dark); font: inherit; text-decoration: underline; cursor: pointer; }
.api-link:hover { color: var(--brand); }
.api-link:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.api-t tfoot th, .api-t tfoot td { border-top: 1px solid var(--line); background: var(--surface); font-weight: 500; }
.api-t tfoot tr:first-child th, .api-t tfoot tr:first-child td { border-top: 2px solid var(--line); }
.api-t tfoot th { text-align: left; font-weight: 600; }
.api-t tfoot td.api-f-x { white-space: normal; }
.api-t tr.api-sum th, .api-t tr.api-sum td { font-weight: 700; border-top: 2px solid var(--line); }

/* menu ⋯ của dòng */
.api-menu { position: relative; display: inline-block; margin-left: 4px; }
.api-menu > summary { list-style: none; }
.api-menu > summary::-webkit-details-marker { display: none; }
.api-pop { position: absolute; right: 0; top: calc(100% + 4px); z-index: 20; min-width: 170px; padding: 4px; display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow-pop); }
.api-mi { text-align: left; border: 0; background: none; padding: 8px 10px; min-height: 36px; border-radius: var(--radius-btn); color: var(--ink); font: inherit; font-size: 13px; cursor: pointer; }
.api-mi:hover { background: var(--hover); }
.api-mi:focus-visible { outline: 2px solid var(--brand); outline-offset: -2px; }
.api-danger { color: var(--err); }

/* NEW-261: lệch giá trị phụ lục ⇒ MỘT dòng cảnh báo (.msg warn) + nút "Lấy theo tổng hạng mục"; khớp ⇒ không hiện gì */
.api-gap { margin: 6px 0 0; }

/* dòng doanh thu theo hạng mục — ô chọn có bề rộng giới hạn (tên hạng mục dài từ Lark không kéo bảng ra 1.300 px) */
.api-ent { margin: 14px 0 0; }
.api-ent select.api-in { min-width: 180px; width: 100%; max-width: 260px; }
.api-ent .api-t thead th { white-space: normal; }

/* giá vốn theo hạng mục */
.api-grp { margin: 0 0 14px; }
.api-grp-h { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; margin: 0 0 6px; padding: 6px 0 0; border-top: 1px solid var(--line-2); }
.api-grp-sum strong { font-variant-numeric: tabular-nums; color: var(--ink); }
.api-un .api-h4 { color: var(--ink); }           /* gắn hạng mục là TUỲ CHỌN (Q7) — không tô màu cảnh báo; chỉ "Lệch phụ lục" mới là lỗi */
.api-cogs select.api-in { min-width: 170px; width: 100%; max-width: 240px; }
.api-cogs .api-doc, .api-cogs .api-doc a { white-space: nowrap; }
.api-mis th, .api-mis td { background: var(--err-soft); }
.api-hid td { color: var(--muted); font-style: italic; }

/* GP theo hạng mục */
.api-s6 { margin: 16px 0 0; }
.api-gpt td.num { white-space: nowrap; }
.api-below strong { color: var(--err); }
.api-below .api-badge { display: block; width: max-content; margin: 2px 0 0 auto; }
.api-nocost .api-s, .api-gpt td .api-s { text-align: right; }
.api-pn th, .api-pn td { color: var(--muted); }

/* hộp xem trước "Lấy theo tổng hạng mục" */
.api-dl { margin: 0 0 10px; display: grid; gap: 6px; }
.api-dl > div { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 12px; }
.api-dl dt { color: var(--muted); }
.api-dl dd { margin: 0; font-variant-numeric: tabular-nums; }
.api-imp { font-size: 13px; }
.api-imp tr.api-chg th, .api-imp tr.api-chg td { font-weight: 600; }
@media (max-width: 479px) { .api-imp { font-size: 12px; } .api-imp th, .api-imp td { padding-left: 6px; padding-right: 6px; } }
.api-check { display: flex; gap: 8px; align-items: flex-start; margin: 10px 0 0; font-size: 13px; }
.api-check input { margin-top: 3px; }
.api-hint { font-size: 12px; }

/* Bảng hạng mục ⇒ THẺ khi khung của mục hẹp (< 900 px: điện thoại, máy tính bảng, MỌI laptop có menu + cột Trao đổi — đo 12/11: khung 436 px ở
   1280, 522 px ở 1366, 596 px ở 1440) — mỗi hạng mục một thẻ, ô có nhãn, sửa thẳng trong thẻ (NEW-222), không phải cuộn ngang. Khung rộng (≥ 900 px)
   ⇒ bảng. Theo bề rộng KHUNG (container query), không theo cửa sổ. */
.api-s4wrap { container: n215s4 / inline-size; }
/* NEW-222: ô tiền = nút bấm để sửa (✓ ✕ như dòng doanh thu NEW-218) — số căn phải trong bảng, cao bằng ô nhập ĐVT / ghi chú / select VAT
   (32 px; cảm ứng 44 px — app.css) ⇒ chữ số thẳng hàng với chữ trong các ô nhập.
   NEW-261b (chủ dự án 17/11): bấm sửa KHÔNG đổi bố cục — nội dung cũ của ô ẩn bằng visibility (JS), ô nhập NỔI (absolute) đè đúng chỗ nút
   (appendix_items.js placeMoney đặt toạ độ), ✓ ✕ + gợi ý + lỗi ở lớp nổi ngay dưới ô nhập ⇒ cột / dòng không đổi cỡ. */
.api-items .api-ie { min-width: 64px; min-height: 32px; margin: 0 -5px; display: inline-flex; align-items: center; justify-content: flex-end;
  text-align: right; font-variant-numeric: tabular-nums; vertical-align: top; }
.api-items td.api-ie-on { position: relative; }
.api-items .api-ie-box { position: absolute; z-index: 4; display: block; min-width: 0; }
.api-items .api-ie-in { display: block; width: 100%; margin: 0; }
.api-ie-pop { position: absolute; top: calc(100% + 4px); right: 0; z-index: 1; display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end;
  gap: 4px; width: max-content; max-width: 260px; padding: 6px 8px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow-pop); }
.rv-ie-l .api-ie-pop { right: auto; left: 0; justify-content: flex-start; }
/* NEW-261c (chủ dự án 17/11): dạng BẢNG (khung ≥ 900 px) phải vừa khung — nút "Sửa" / "⋯" cột cuối không bị cắt, không cuộn ngang. Đo 17/11: bản
   cũ cần ~1.150 px (khung trang phụ lục 1920 = ~1.075 px). Tiêu đề cột xuống dòng, cột tiền / VAT / ghi chú / tên co lại ⇒ vừa khung từ 900 px. */
@container n215s4 (min-width: 900px) {
  .api .api-t.api-items thead th { white-space: normal; vertical-align: bottom; }   /* thắng .fv-sheet table.data th (nowrap, 0,2,2) */
  .api-items th, .api-items td { padding-left: 8px; padding-right: 8px; }
  .api-items .api-c-name { min-width: 120px; max-width: 260px; }
  .api-items .api-c-stt { width: 28px; min-width: 28px; }
  .api-items .api-c-unit .api-in { width: 60px; }        /* đủ "tháng" / "video" */
  .api-items .api-c-vat .api-in { width: 112px; }
  .api-items .api-c-note .api-in { width: 100%; min-width: 80px; }
  .api-items .api-c-qty .api-ie { min-width: 40px; }
  .api-items .api-c-per { white-space: normal; }        /* kỳ dài "01/10/2026 → 15/12/2026" xuống dòng thay vì đẩy cột Sửa ra ngoài */
  .api-items .api-c-act { white-space: nowrap; }
}
@container n215s4 (max-width: 899px) {
  /* thẻ của dòng sửa được: số cột tự theo bề rộng (≥ 150 px / ô) ⇒ 2 cột ở 1280 (khung 436), 3 cột ở 1366–1440, không tràn nhãn "Thành tiền (trước VAT)" */
  .api-items tbody tr.api-rw { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .api-items .api-ie { text-align: left; justify-content: flex-start; margin: 0; } }
@container n215s4 (max-width: 899px) {
  .api-tw-items { overflow: visible; border: 0; box-shadow: none; }
  .api-items, .api-items tbody, .api-items tfoot { display: block; }
  .api-items thead { display: none; }
  .api-items tbody tr { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px 12px; padding: 10px 12px; margin: 0 0 8px;
    border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
  .api-items tbody tr.api-child { margin-left: 16px; background: var(--surface-2); }
  .api-items tbody tr > * { display: block; padding: 0; border: 0; min-width: 0; max-width: none; position: static; background: none; text-align: left; }
  .api-items.api-t tbody th.api-c-name[scope] { position: static; background: none; padding: 0; }
  .api-items.api-t tbody td.num { text-align: left; }
  .api-items.data tbody tr:hover td, .api-items tbody tr:hover th.api-c-name { background: none; }
  .api-items td[data-label]::before { content: attr(data-label); display: block; margin-bottom: 2px; font-size: 12px; color: var(--muted); }
  .api-items .api-c-stt { display: none; }
  .api-items .api-stt-m { display: inline; margin-right: 4px; color: var(--muted); }
  .api-items .api-c-name, .api-items .api-c-act { grid-column: 1 / -1; }
  .api-items .api-c-note { grid-column: span 2; }
  .api-items .api-c-act { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-start; }
  .api-items .api-in, .api-items .api-c-vat .api-in, .api-items .api-c-unit .api-in, .api-items .api-c-qty .api-in,
  .api-items .api-c-price .api-in, .api-items .api-c-amt .api-in, .api-items .api-c-note .api-in { width: 100%; }
  .api-items .api-num { text-align: left; }
  .api-items tfoot tr { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: 2px 12px; padding: 6px 12px; border-top: 1px solid var(--line-2); }
  .api-items tfoot tr > * { display: block; padding: 0; border: 0 !important; position: static; background: none; }
  .api-items tfoot .api-f-pad, .api-items tfoot .api-f-x:empty { display: none; }
  .api-items tfoot .api-f-x { flex-basis: 100%; }
  .api-pop { right: auto; left: 0; }               /* ⋯ nằm bên trái thẻ ⇒ menu mở sang phải (không tràn mép trái 761–1100 px) */
}
/* khung vừa (480–899 px), dòng NGƯỜI XEM (không sửa được gì ngoài VAT — `tr:not(.api-rw)`): MỖI hạng mục MỘT DÒNG gọn — tên + tóm tắt SL × đơn giá ·
   ghi chú · kỳ, Thành tiền, Thuế GTGT, thao tác. Dòng sửa được (`.api-rw`, NEW-222 chủ dự án 12/11) giữ THẺ đủ ô để sửa thẳng trong thẻ trên laptop. */
@container n215s4 (min-width: 480px) and (max-width: 899px) {
  .api-items tbody tr:not(.api-rw) { grid-template-columns: minmax(0, 1fr) 132px 150px auto; align-items: start; gap: 4px 12px; padding: 8px 12px; margin: 0 0 6px; }
  .api-items tr:not(.api-rw) .api-c-name { grid-column: 1; }
  .api-items tr:not(.api-rw) .api-c-amt { grid-column: 2; }
  .api-items tr:not(.api-rw) .api-c-vat { grid-column: 3; }
  .api-items tr:not(.api-rw) .api-c-act { grid-column: 4; justify-content: flex-end; flex-wrap: nowrap; }
  .api-items tr:not(.api-rw) .api-c-unit, .api-items tr:not(.api-rw) .api-c-price, .api-items tr:not(.api-rw) .api-c-qty,
  .api-items tr:not(.api-rw) .api-c-note, .api-items tr:not(.api-rw) .api-c-per { display: none; }
  .api-items tr:not(.api-rw) .api-calc-m { display: block; }
  .api-items tbody tr.api-child { margin-left: 16px; }
}
/* thẻ (< 480 px hoặc chỉ đọc): ô trống chỉ đọc không in "—" cả hàng */
@container n215s4 (max-width: 899px) {
  .api-items td[data-empty] { display: none; }
}
@container n215s4 (max-width: 479px) {
  .api-items tbody tr { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .api-items .api-c-note { grid-column: 1 / -1; }
}
@media (max-width: 760px) {
  .api-acts { margin-left: 0; width: 100%; }
  .api-c-name, .api-c-first { min-width: 130px; max-width: 170px; }
  .api-in { font-size: 16px; }                     /* iOS không phóng to khi chạm ô */
}
@media (pointer: coarse) {
  .api .btn.small, .api-in { min-height: 44px; }
  .api-link { min-height: 44px; padding: 10px 0; }
  .api-mi { min-height: 44px; }
  .api-menu > summary { min-height: 44px; min-width: 44px; display: inline-flex; align-items: center; justify-content: center; }
}
@media print {
  .api-acts, .api-c-act, [data-api-apply] { display: none !important; }
  .api-t .api-c-name { position: static; }
  .api-in { border: 0; padding: 0; background: none; }
}
/* bảng giá vốn / GP / dòng doanh thu: rộng theo nội dung (số không tràn đè cột Hạng mục dính trái), tối thiểu bằng khung */
.api-cogs, .api-gpt, .api-ent .api-t { width: max-content; min-width: 100%; }
.api-gpt th.api-c-first, .api-cogs th.api-c-first { min-width: 150px; max-width: 240px; white-space: normal; overflow-wrap: anywhere; }
.api-ent .api-t td.api-e-code { white-space: nowrap; }
.api-ent .api-t td.api-e-desc { min-width: 160px; }
/* tiêu đề cột số của bảng GP xuống dòng ⇒ cột hẹp bằng con số (khung ~480 px ở laptop 1280 có chatter) */
.api-t.api-gpt thead th.num { white-space: normal; min-width: 72px; max-width: 120px; overflow-wrap: break-word; }
/* ô tên (th scope=row) của mọi bảng NEW-215 xuống dòng — thắng luật chung `.fv-sheet table.data th { white-space: nowrap }` (form_view.css,
   độ ưu tiên 0,2,2) để tên hạng mục dài không đè lên cột số (review 10/11) */
.api .api-t tbody th[scope="row"], .api .api-t tfoot th[scope="row"], .api-t.api-gpt tbody th.api-c-first, .api-t.api-cogs tbody th.api-c-first,
.api-ent .api-t tbody th[scope="row"] { white-space: normal; overflow-wrap: anywhere; }
.api-calc-m { display: none; }

/* ===== 2.61.1 (sau kiểm tra trên crmdev) ===== */
/* F9: trạng thái dòng giá vốn = chip ngắn một dòng (chi tiết ở title); tên hạng mục khi chỉ đọc */
.api-stchip { white-space: nowrap; }
.api-cogs td.api-c-st { white-space: nowrap; }
.api-itemname { display: inline-block; max-width: 240px; overflow-wrap: anywhere; }
/* F8: khung bảng còn cột bên phải ⇒ câu gợi ý + viền phải màu nhấn (không gradient) */
.api-scrollhint { margin: 0 0 4px; font-size: 12px; color: var(--muted); }
.aps-tw.api-more { border-right: 3px solid var(--brand); }
/* F8: điện thoại (≤ 760 px) — bảng giá vốn theo hạng mục + dòng doanh thu theo hạng mục thành THẺ có nhãn (thấy ngay tiền / trạng thái / NCC) */
@media (max-width: 760px) {
  .api-cards, .api-ent .api-t.api-cards, .api-cards tbody { display: block; width: auto; min-width: 0; }
  .api-cards thead { display: none; }
  .api-cards tbody tr { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px 12px; padding: 10px 12px; border-bottom: 1px solid var(--line); }
  .api-cards tbody tr > * { display: block; padding: 0; border: 0; min-width: 0; max-width: none; position: static; background: none; text-align: left; white-space: normal; }
  .api-cards.api-t tbody th[scope="row"], .api-cards.api-t tbody th.api-c-first { position: static; grid-column: 1 / -1; background: none; padding: 0; max-width: none; }
  .api-cards.api-t tbody td.num { text-align: left; }
  .api-cards td[data-label]::before { content: attr(data-label); display: block; margin-bottom: 2px; font-size: 12px; color: var(--muted); }
  .api-cards td[data-empty], .api-cards td.api-doc:empty { display: none; }
  .api-cards td.api-c-link, .api-cards td.api-e-item, .api-cards td.api-e-desc { grid-column: 1 / -1; }
  .api-cards select.api-in { max-width: none; width: 100%; }
  .api-cards tr.api-hid > td { grid-column: 1 / -1; }
  .api-cards tbody tr:hover th.api-c-first { background: none; }
  .aps-tw:has(> .api-cards) { overflow: visible; }
  .api-ent .api-cards td.api-e-desc { min-width: 0; }
}
/* "Tổng theo loại chi phí" (bảng cũ của mục Giá vốn — đứng sau [data-n215-s5]): tên khoản + câu giải thích
   xuống dòng thay vì đẩy hai cột tiền ra ngoài khung ở điện thoại (luật chung .fv-sheet table.data th nowrap) */
[data-n215-s5] ~ .aps-tw table.data.aps-t th[scope="row"] { white-space: normal; overflow-wrap: anywhere; min-width: 0; }
[data-n215-s5] ~ .aps-tw table.data.aps-t thead th:first-child { white-space: normal; }
