/*
 * Lịch Dụng Sự / Đại Quái / Chính Ngũ Hành trên điện thoại (≤ 600px).
 * Chỉ thêm quy tắc cho màn hình nhỏ; giao diện máy tính không đổi.
 *  - Lưới tháng dàn vừa bề ngang (7 cột đều nhau), ô ngày rút gọn; bấm vào ngày để xem chi tiết đầy đủ bên dưới.
 *  - Các bảng kết quả (chi tiết ngày, 12 giờ, Phi Tinh, Tam Sát…) co theo bề ngang, chữ tự xuống dòng.
 * Phạm vi: lưới table[id^="calendar"] và các khung kết quả #mapdungsu, #mapdaiquai, #mapcnguhanh.
 */
@media (max-width: 600px) {

    /* ================= bảng kết quả ================= */
    #mapdungsu table, #mapdaiquai table, #mapcnguhanh table,
    table#calday, table#ThoiTableDescript, table#HoursTableDescript {
        width: 100% !important;
        max-width: 100%;
    }
    #mapdungsu td, #mapdungsu th,
    #mapdaiquai td, #mapdaiquai th,
    #mapcnguhanh td, #mapcnguhanh th,
    table#calday td, table#calday th,
    table#ThoiTableDescript td, table#ThoiTableDescript th,
    table#HoursTableDescript td, table#HoursTableDescript th {
        overflow-wrap: anywhere;      /* cho phép ô co lại vừa khung */
        padding: 2px !important;
    }
    #mapdungsu td[width], #mapdaiquai td[width], #mapcnguhanh td[width], table#calday td[width] {
        width: auto !important;
    }
    /* cột nhãn bên trái của chi tiết ngày (Ngũ hành, Lộc Mã QN, Trực/Tú…): chỉ xuống dòng giữa các từ */
    table#calday > tbody > tr > td:first-child:not([colspan]),
    table#calday > tbody > tr > th:first-child:not([colspan]) {
        overflow-wrap: normal !important;
        min-width: 4.5em;
        width: 22% !important;
    }
    /* ô vuông Cửu Tinh (cellpadding=8) */
    table.BadziKvadrat td, table.BadziKvadrat th { padding: 3px !important; }
    /* ô chọn trong form (giờ, ngày, tháng, năm, toạ, tuổi) */
    #mapdungsu select, #mapdaiquai select, #mapcnguhanh select { max-width: 100%; font-size: 13px !important; }

    /* bảng 12 giờ dạng lưới (chế độ "Xem ngắn") rộng cố định: cuộn ngang trong khung */
    table#HoursTable {
        display: block;
        max-width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* ================= lưới tháng ================= */
    table[id^="calendar"] {
        width: 100% !important;
        max-width: 100%;
        table-layout: fixed;
    }
    table[id^="calendar"] td,
    table[id^="calendar"] th {
        padding: 1px !important;
        overflow: hidden;
        overflow-wrap: normal !important;   /* không ngắt giữa chữ/số */
        word-break: normal !important;
    }
    table[id^="calendar"] select { font-size: 13px !important; max-width: 100%; }
    table[id^="calendar"] .btn,
    #toatuoi .btn, #toatuoi1 .btn { padding: 1px 4px !important; font-size: 12px !important; line-height: 1.3; }
    #toatuoi, #toatuoi1 { width: 100% !important; table-layout: fixed; }
    #toatuoi select, #toatuoi1 select { max-width: 100%; }
    /* tên thứ (Chủ nhật, Thứ 2…) */
    table[id^="calendar"] > thead td,
    table[id^="calendar"] > thead th { font-size: 11px !important; line-height: 1.2; }
    .dlich, .solar_la { white-space: nowrap; }

    /* ô ngày Lịch Dụng Sự: giữ ngày dương, ngày âm, dấu hoàng/hắc đạo (cắt ngắn); ẩn can chi, trực, sao, thần sát */
    #dayList td > a > .lunar_nal,
    #dayList td > a > .lunar_nar ~ .lunar_nar { display: none; }
    #dayList td > a > .lunar_nar {
        font-size: 10px; line-height: 1.2;
        white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    }
    #dayList td > a > .dlich { font-size: 18px; line-height: 1.2; }
    #dayList td > a > .lunar_bo { font-size: 11px; }

    /* ô ngày Đại Quái (bảng con): giữ 2 quẻ (thượng/hạ), ngày dương, ngày âm; ẩn tên quẻ, số ngũ hành, Tử Bạch */
    #dayList td > a > table.tableMP { width: 100%; table-layout: fixed; }
    #dayList td > a > table.tableMP tr:nth-child(1),
    #dayList td > a > table.tableMP tr:nth-child(2),
    #dayList td > a > table.tableMP tr:nth-child(6),
    #dayList td > a > table.tableMP td.xgterm { display: none; }
    #dayList td > a > table.tableMP tr:nth-child(5) td:nth-child(2) div { display: none; }
    #dayList td > a > table.tableMP td { width: auto !important; font-size: 11px; }
    #dayList td > a > table.tableMP .solar_la { font-size: 15px; font-weight: bold; }
}

/* ---- bổ sung 2026-11-22: khung chứa lịch và bảng chi tiết ngày gọn hơn trên điện thoại ---- */
@media (max-width: 600px) {
    /* đệm khung thẻ chứa lịch: 24px -> 8px (chỉ thẻ có lưới tháng / bảng kết quả lịch) */
    .card-body:has(table[id^="calendar"]), .card-body:has(#mapdungsu), .card-body:has(#mapdaiquai), .card-body:has(#mapcnguhanh) {
        padding: 8px !important;
    }
    /* chi tiết ngày: dòng 2 ô (nhãn | nội dung) — nội dung căn trái, chữ vừa phải để danh sách dài không thành dải hẹp */
    table#calday > tbody > tr > td:first-child + td:last-child {
        text-align: left !important;
        font-size: 13px;
        line-height: 1.4;
    }
}

/* ---- bổ sung 2026-11-22 (2): mobile — nhãn xếp trên nội dung, bảng 12 giờ thành khối theo giờ ---- */
@media (max-width: 600px) {
    /* chi tiết ngày (#calday): mỗi dòng "nhãn | nội dung" -> thanh nhãn ở trên, nội dung đầy đủ bề rộng ở dưới */
    table#calday, table#calday > thead, table#calday > tbody { display: block; width: 100% !important; }
    table#calday > thead > tr:not(.DNone), table#calday > tbody > tr:not(.DNone) { display: flex; flex-wrap: wrap; }
    table#calday > thead > tr > *, table#calday > tbody > tr > * { display: block; flex: 1 1 0; min-width: 0; box-sizing: border-box; }
    table#calday > tbody > tr:has(> :nth-child(2):last-child) > * { flex-basis: 100%; }
    table#calday > tbody > tr:has(> :nth-child(2):last-child) > :first-child {
        width: auto !important; min-width: 0; text-align: left !important;
        background: #eef6ee; color: #1b5e20; font-weight: 600; font-size: 13px;
        padding: 3px 8px !important; border-bottom: 0;
    }
    table#calday > tbody > tr:has(> :nth-child(2):last-child) > :first-child br { display: none; }
    table#calday > tbody > tr:has(> :nth-child(2):last-child) > :last-child { padding: 4px 6px 8px !important; }
    table#calday > tbody > tr > td > table, table#calday > tbody > tr > th > table { width: 100% !important; margin: 0 auto; }

    /* 12 giờ (lichdungsu.html): table.thoi, mỗi giờ 3 dòng Tinh / Nghi / Kị
       -> dải đầu giờ (Giờ · Đại Quái · Cửu Tinh), dưới là từng nhãn + nội dung */
    table.thoi:has(td[rowspan="3"]), table.thoi:has(td[rowspan="3"]) > tbody { display: block; width: 100% !important; }
    table.thoi:has(td[rowspan="3"]) > caption { display: block; }
    table.thoi:has(td[rowspan="3"]) > tbody > tr:not(.DNone) { display: flex; flex-wrap: wrap; }
    table.thoi:has(td[rowspan="3"]) > tbody > tr > td { display: block; box-sizing: border-box; min-width: 0; flex: 0 0 100%; width: auto !important; }
    table.thoi:has(td[rowspan="3"]) > tbody > tr:first-child { display: none; }   /* tiêu đề cột: thay bằng nhãn nhỏ trong dải đầu giờ */
    table.thoi:has(td[rowspan="3"]) > tbody > tr > td[rowspan] { flex: 1 1 0; text-align: center; }
    table.thoi:has(td[rowspan="3"]) > tbody > tr > td[rowspan]:first-child { flex-grow: 2; }
    table.thoi:has(td[rowspan="3"]) > tbody > tr > td[rowspan]:nth-child(2)::before,
    table.thoi:has(td[rowspan="3"]) > tbody > tr > td[rowspan]:nth-child(3)::before {
        display: block; font-size: 11px; font-weight: 400; font-style: italic; color: #6c757d;
    }
    table.thoi:has(td[rowspan="3"]) > tbody > tr > td[rowspan]:nth-child(2)::before { content: "Đại Quái"; }
    table.thoi:has(td[rowspan="3"]) > tbody > tr > td[rowspan]:nth-child(3)::before { content: "Cửu Tinh"; }
    table.thoi:has(td[rowspan="3"]) > tbody > tr > td[rowspan] {
        background: #f3f8f3; font-weight: 600; margin-top: 10px; border-top: 3px solid #2e7d32; line-height: 1.3;
    }
    table.thoi:has(td[rowspan="3"]) > tbody > tr > td:not([rowspan]):not(:last-child) {
        text-align: left; background: #fafafa; font-size: 12px; font-weight: 600; padding: 2px 8px !important; border-bottom: 0;
    }
    table.thoi:has(td[rowspan="3"]) > tbody > tr:not(:first-child) > td:last-child:not([rowspan]) {
        text-align: left; font-size: 13px; line-height: 1.4; padding: 3px 8px 6px !important;
    }

    /* 12 giờ (ldspro): #ThoiTableDescript 3 cột Đại Quái | Cát Hung Thần | Cửu Tinh
       -> Cát Hung Thần đầy đủ bề rộng ở trên, Đại Quái và Cửu Tinh cạnh nhau ở dưới */
    table#ThoiTableDescript, table#ThoiTableDescript > thead, table#ThoiTableDescript > tbody { display: block; width: 100% !important; }
    table#ThoiTableDescript > thead > tr:not(.DNone), table#ThoiTableDescript > tbody > tr:not(.DNone) { display: flex; flex-wrap: wrap; }
    table#ThoiTableDescript > thead > tr > *, table#ThoiTableDescript > tbody > tr > * {
        display: block; box-sizing: border-box; min-width: 0; flex: 1 1 50%; width: auto !important;
    }
    table#ThoiTableDescript > thead > tr > [colspan], table#ThoiTableDescript > * > tr > :nth-child(2):not(:last-child) { flex-basis: 100%; order: -1; }
    table#ThoiTableDescript > thead > tr:nth-child(2) { display: none; }
    table#ThoiTableDescript > tbody > tr:not(.DNone) { margin-top: 10px; border-top: 3px solid #2e7d32; }
    table#ThoiTableDescript > tbody > tr > :nth-child(2):not(:last-child) { text-align: left; font-size: 13px; line-height: 1.4; }
    table#ThoiTableDescript > tbody > tr > * table { width: 100% !important; margin: 0 auto; }

    /* Chính ngũ hành: các bảng 24 sơn (table.sonvi) -> tên sơn / tên sát làm thanh nhãn, nội dung xuống dưới;
       bỏ dòng tiêu đề cột (td.hungday) và ô rỗng */
    table.sonvi, table.sonvi > tbody { display: block; width: 100% !important; }
    table.sonvi > tbody > tr { display: flex; flex-wrap: wrap; }
    table.sonvi > tbody > tr > td { display: block; box-sizing: border-box; flex: 0 0 100%; width: auto !important; min-width: 0; }
    table.sonvi > tbody > tr > td:empty, table.sonvi > tbody > tr:has(> td.hungday) { display: none; }
    table.sonvi > tbody > tr > td:first-child:not([colspan]) {
        background: #eef6ee; color: #1b5e20; font-weight: 600; font-size: 13px; padding: 3px 8px !important; margin-top: 6px;
    }
    table.sonvi > tbody > tr > td:not(:first-child), table.sonvi > tbody > tr > td[colspan="2"] { padding: 3px 8px !important; line-height: 1.4; }

    /* Đại Quái: bảng "Luận" 5 cột (Thiên, Thiên-Địa, Thiên-Nhân, Địa-Nhân, Thiên-Địa-Nhân) -> 5 khối dọc,
       mỗi khối: dòng kết luận ở trên, phần giải thích ở dưới */
    table.thoi table:has(> tbody > tr > td[width="20%"]:nth-child(5)) > tbody { display: flex; flex-direction: column; }
    table.thoi table:has(> tbody > tr > td[width="20%"]:nth-child(5)) > tbody > tr { display: contents; }
    table.thoi table:has(> tbody > tr > td[width="20%"]:nth-child(5)) > tbody > tr > td { display: block; width: auto !important; padding: 3px 8px !important; }
    table.thoi table:has(> tbody > tr > td[width="20%"]:nth-child(5)) > tbody > tr:first-child > td { background: #eef6ee; margin-top: 6px; }
    table.thoi table:has(> tbody > tr > td[width="20%"]:nth-child(5)) > tbody > tr > td:nth-child(1) { order: 1; }
    table.thoi table:has(> tbody > tr > td[width="20%"]:nth-child(5)) > tbody > tr > td:nth-child(2) { order: 2; }
    table.thoi table:has(> tbody > tr > td[width="20%"]:nth-child(5)) > tbody > tr > td:nth-child(3) { order: 3; }
    table.thoi table:has(> tbody > tr > td[width="20%"]:nth-child(5)) > tbody > tr > td:nth-child(4) { order: 4; }
    table.thoi table:has(> tbody > tr > td[width="20%"]:nth-child(5)) > tbody > tr > td:nth-child(5) { order: 5; }
    /* Đại Quái: bảng 7 cột (năm, tháng, ngày, giờ, toạ, tuổi): không cắt chữ giữa từ */
    table.thoi table:has(> tbody > tr > :nth-child(7):last-child) td { overflow-wrap: normal !important; font-size: 13px; }
}
