/* Phone layout that works like an app: the page scrolls as a whole, a thin title bar stays on top, a tab bar sits at
   the bottom, every control is big enough for a thumb, and lists are compact cards (docs/tasks/mobile-app-ui.md).
   Loaded after admin.css and sales-calendar.css. Nothing here applies above 900px except hiding the tab bar. */

.bottom-nav,
.card-guests { display: none; }

@media (max-width: 900px) {
    :root {
        --tab-bar-h: 62px;
        --safe-top: env(safe-area-inset-top, 0px);
        --safe-bottom: env(safe-area-inset-bottom, 0px);
        --app-bg: #f3f5f8;
        --app-line: rgba(15, 17, 23, 0.08);
        --app-green: #00a845;
    }

    /* ---- shell ---------------------------------------------------------------------------------------------- */
    /* One scrolling page instead of a 100vh box: LINE's and Safari's bars collapse and never cover the end of it.
       overflow-x: clip (not hidden) keeps the title bar sticky. */
    html,
    body,
    #dashboard-section {
        height: auto !important;
        overflow: visible !important;
        overflow-x: clip !important;
    }

    body {
        display: block !important;
        min-height: 100dvh;
        background: var(--app-bg) !important;
        -webkit-tap-highlight-color: transparent;
    }

    html.authed #dashboard-section { display: block !important; }

    #dashboard-section > div[class*="flex-1"] {
        height: auto !important;
        min-height: 100dvh;
        overflow: visible !important;
        padding: 0 12px calc(var(--tab-bar-h) + var(--safe-bottom) + 20px) !important;
    }

    #dashboard-section .mobile-header {
        position: sticky;
        top: 0;
        z-index: 900;
        width: auto !important;
        max-width: none !important;
        margin: 0 -12px 12px !important;
        padding: calc(var(--safe-top) + 6px) 16px 6px !important;
        gap: 6px !important;
        border: 0 !important;
        border-bottom: 1px solid var(--app-line) !important;
        border-radius: 0 !important;
        background: #fff !important;
        box-shadow: none !important;
    }

    #dashboard-section .mobile-header .header-top-row { min-height: 44px; }

    #dashboard-section .mobile-header h2 {
        font-size: 1.15rem !important;
        font-weight: 700 !important;
        color: #111827;
    }

    #dashboard-section .mobile-header h2 i { color: var(--app-green); }

    /* The tab bar has the menu button. */
    #dashboard-section .mobile-header .btn-menu-toggle { display: none !important; }

    .sidebar {
        height: 100dvh !important;
        width: min(84vw, 300px) !important;
        padding-top: var(--safe-top);
        padding-bottom: var(--safe-bottom);
        box-sizing: border-box;
    }

    .sidebar.open { box-shadow: 0 0 0 100vmax rgba(15, 17, 23, 0.45) !important; }

    .bottom-nav {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 950;
        display: grid;
        grid-template-columns: repeat(5, minmax(0, 1fr));
        height: calc(var(--tab-bar-h) + var(--safe-bottom));
        padding-bottom: var(--safe-bottom);
        box-sizing: border-box;
        background: rgba(255, 255, 255, 0.96);
        -webkit-backdrop-filter: saturate(180%) blur(14px);
        backdrop-filter: saturate(180%) blur(14px);
        border-top: 1px solid var(--app-line);
    }

    html:not(.authed) .bottom-nav { display: none; }

    .bottom-nav .bottom-nav-item {
        display: flex !important;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 4px;
        width: auto !important;
        height: auto !important;
        min-width: 0;
        padding: 0;
        border: 0;
        border-radius: 0 !important;
        background: none !important;
        color: #8a9099;
        font: inherit;
        text-decoration: none;
        cursor: pointer;
    }

    .bottom-nav .bottom-nav-item.active { color: var(--app-green); }

    .bottom-nav-icon {
        position: relative;
        display: grid;
        place-items: center;
        width: 52px;
        height: 30px;
        border-radius: 15px;
        font-size: 1.15rem;
        line-height: 1;
        transition: background 0.2s;
    }

    .bottom-nav-item.active .bottom-nav-icon { background: rgba(0, 200, 83, 0.12); }

    .bottom-nav-label {
        max-width: 100%;
        overflow: hidden;
        font-size: 11px;
        font-weight: 600;
        line-height: 1.1;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    .bottom-nav-badge {
        position: absolute;
        top: -4px;
        left: 32px;
        min-width: 18px;
        height: 18px;
        padding: 0 5px;
        box-sizing: border-box;
        border-radius: 9px;
        border: 2px solid #fff;
        background: #ef4444;
        color: #fff;
        font-size: 10px;
        font-weight: 700;
        line-height: 14px;
        text-align: center;
    }

    .bottom-nav-badge.dot {
        min-width: 0;
        width: 12px;
        height: 12px;
        padding: 0;
        top: -1px;
    }

    .bottom-nav-badge.hidden { display: none; }

    /* ---- controls ------------------------------------------------------------------------------------------- */
    /* 16px stops iPhone zooming into a field when it is tapped. */
    #dashboard-section input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
    #dashboard-section select,
    #dashboard-section textarea,
    .swal2-container input:not([type="checkbox"]):not([type="radio"]),
    .swal2-container select,
    .swal2-container textarea {
        font-size: 16px !important;
    }

    #dashboard-section select,
    #dashboard-section input[type="text"],
    #dashboard-section input[type="search"],
    #dashboard-section input[type="tel"],
    #dashboard-section input[type="date"],
    #dashboard-section input[type="month"] {
        min-height: 44px;
        box-sizing: border-box;
    }

    .btn-action-text,
    .btn-icon,
    .lt-btn,
    .filter-btn,
    .btn-sort {
        min-height: 42px !important;
        box-sizing: border-box;
    }

    .btn-copy-phone {
        min-width: 32px;
        min-height: 32px;
    }

    .page-section.active { animation: none; }

    /* ---- รายการจอง ---------------------------------------------------------------------------------------- */
    #page-bookings > .flex.gap-5 {
        gap: 4px !important;
        margin-bottom: 10px !important;
        padding: 4px;
        border: 0 !important;
        border-radius: 12px;
        background: #e8ebf0;
    }

    #page-bookings .tab-btn {
        flex: 1 0 auto;
        min-height: 40px;
        margin: 0 !important;
        padding: 0 12px !important;
        border: 0 !important;
        border-radius: 9px;
        color: #5b616b;
        font-size: 0.82rem !important;
    }

    #page-bookings .tab-btn.active {
        background: #fff;
        color: #111827;
        box-shadow: 0 1px 3px rgba(15, 17, 23, 0.1);
    }

    #page-bookings > .flex.flex-wrap {
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto !important;
        margin-bottom: 12px !important;
    }

    #page-bookings .search-box input,
    #page-bookings select,
    #page-bookings .btn-sort {
        min-height: 40px !important;
        border-radius: 12px !important;
        background-color: #fff;
    }

    #page-bookings .btn-sort { padding: 0 12px !important; white-space: nowrap; }

    /* The desktop table box (admin.css) would sit white behind the white cards; the cards lie on the page instead. */
    #page-bookings .table-responsive {
        overflow: visible !important;
        border: 0 !important;
        border-radius: 0 !important;
        background: transparent !important;
        box-shadow: none !important;
    }

    /* A booking card: name · status, then when · amount, both pinned to the same left and right edges.
       Only a booking that still needs a step gets a third line of two equal buttons.
       Tap the card for everything else (phone, slip, bill, edit, move, cancel).
       The row is a wrapping flex line; tr::before and tr::after are the two line breaks. */
    #page-bookings .data-table tr {
        display: flex !important;
        flex-wrap: wrap;
        align-items: center;
        column-gap: 8px;
        margin-bottom: 14px !important;
        padding: 14px 16px !important;
        border: 1px solid var(--app-line) !important;
        border-radius: 16px !important;
        background: #fff;
        box-shadow: 0 1px 3px rgba(15, 17, 23, 0.06) !important;
        cursor: pointer;
    }

    #page-bookings .data-table tr::before,
    #page-bookings .data-table tr::after {
        content: "";
        flex-basis: 100%;
        height: 0;
    }

    #page-bookings .data-table tr::before { order: 3; }
    #page-bookings .data-table tr::after { order: 6; }

    #page-bookings .data-table tr:active { background: #f6f8fa; }

    #page-bookings .data-table td {
        display: flex !important;
        align-items: center;
        width: auto !important;
        min-width: 0;
        padding: 0 !important;
        border: 0 !important;
        text-align: left;
    }

    #page-bookings .data-table td::before { display: none; }

    #page-bookings td[data-label="ลูกค้า"] { order: 1; flex: 1 1 0; }
    #page-bookings td[data-label="สถานะ"] { order: 2; flex: 0 0 auto; }
    #page-bookings td[data-label="วันเข้าพัก"] { order: 4; flex: 1 1 0; margin-top: 6px; }
    #page-bookings td[data-label="ยอดเงิน"] {
        order: 5;
        flex: 0 0 auto;
        margin-top: 6px;
        font-size: 1rem;
        font-variant-numeric: tabular-nums;
    }
    #page-bookings td[data-label="หลักฐาน"] { order: 7; flex: 1 1 0; margin-top: 12px; }
    #page-bookings td[data-label="จัดการ"] { order: 8; flex: 1 1 0; margin-top: 12px; }

    #page-bookings td[data-label="ผู้เข้าพัก"],
    #page-bookings .user-avatar,
    #page-bookings .phone-row,
    #page-bookings .booking-id-inline,
    #page-bookings .btn-edit-booking,
    #page-bookings .btn-reject,
    #page-bookings tr:not(:has(.btn-approve)) td[data-label="หลักฐาน"],
    #page-bookings td[data-label="หลักฐาน"]:not(:has(button)),
    #page-bookings td[data-label="จัดการ"]:not(:has(.btn-approve)) { display: none !important; }

    #page-bookings .user-cell,
    #page-bookings .user-info { min-width: 0; }

    #page-bookings .user-info h4 {
        margin: 0;
        overflow: hidden;
        color: #111827;
        font-size: 1.02rem;
        font-weight: 700;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    #page-bookings .status-badge {
        padding: 4px 10px !important;
        font-size: 0.74rem !important;
        white-space: nowrap;
    }

    #page-bookings .date-range-line {
        overflow: hidden;
        color: #374151;
        font-size: 0.86rem;
        font-weight: 500;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    #page-bookings .date-dash { margin: 0 1px; color: #9ca3af; }

    #page-bookings .nights-badge,
    #page-bookings .card-guests {
        display: inline;
        margin: 0;
        padding: 0;
        background: none;
        color: #6b7280;
        font-size: inherit;
        font-weight: 400;
    }

    #page-bookings .nights-badge::before,
    #page-bookings .card-guests::before {
        content: "·";
        margin: 0 6px;
        color: #c0c4cc;
    }

    /* formatDateRangeLine already puts a space before the nights. */
    #page-bookings .nights-badge::before { margin-left: 2px; }

    #page-bookings .action-buttons { display: flex !important; flex: 1; }

    #page-bookings td[data-label="หลักฐาน"] .btn-icon,
    #page-bookings td[data-label="จัดการ"] .btn-approve {
        flex: 1;
        justify-content: center;
        width: 100% !important;
        min-height: 42px !important;
        margin: 0 !important;
        padding: 0 10px !important;
        border-radius: 12px !important;
        font-size: 0.86rem !important;
        font-weight: 600 !important;
        white-space: nowrap;
    }

    #page-bookings td[data-label="หลักฐาน"] .btn-icon {
        border: 1px solid #e5e7eb !important;
        background: #fff !important;
        color: #374151 !important;
    }

    #page-bookings td[data-label="จัดการ"] .btn-approve {
        border: 1px solid var(--app-green) !important;
        background: var(--app-green) !important;
        color: #fff !important;
    }

    /* ---- จองคิวให้ลูกค้า ---------------------------------------------------------------------------------- */
    #page-admin-booking button[onclick^="updateAdminCounter"] {
        width: 40px !important;
        height: 40px !important;
    }

    #page-admin-booking input[type="checkbox"] {
        width: 22px !important;
        height: 22px !important;
    }

    #page-admin-booking .admin-reset-btn { min-height: 36px; }

    /* ---- ปฏิทินห้องพัก ------------------------------------------------------------------------------------ */
    #page-calendar { padding: 0 !important; }

    #page-calendar .admin-calendar { gap: 4px !important; }

    #page-calendar .cal-day {
        min-height: 74px !important;
        padding: 4px 3px !important;
        gap: 3px;
        border-radius: 10px !important;
        overflow: hidden;
    }

    #page-calendar .cal-date {
        width: 24px !important;
        height: 24px !important;
        font-size: 0.85rem !important;
    }

    /* Names were squeezed to one letter per line: one line per guest, cut with "…". Tap the day for the details. */
    #page-calendar .cal-info {
        display: block !important;
        width: 100% !important;
        max-width: 100%;
        box-sizing: border-box;
        padding: 2px 4px !important;
        overflow: hidden;
        border-radius: 5px !important;
        font-size: 9.5px !important;
        line-height: 1.35 !important;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    #page-calendar .cal-info span { white-space: nowrap; }
    #page-calendar .cal-info i,
    #page-calendar .cal-info-kind { display: none; }
    #page-calendar .cal-info.out-guest,
    #page-calendar .cal-info.in-guest { display: none !important; }

    /* Without the word, the colour says who is leaving and who is arriving on a changeover day (legend colours). */
    #page-calendar .cal-day.swap .cal-info.checkout { background: #e5e7eb; color: #4b5563; }
    #page-calendar .cal-day.swap .cal-info.booked { background: #ff4d4f; color: #fff; }
    #page-calendar .cal-day.swap .cal-info.pending { background: #ffa940; color: #fff; }
    #page-calendar .cal-day.swap .cal-info.blocked { background: #555; color: #fff; }

    /* ---- ภาพรวม ------------------------------------------------------------------------------------------- */
    #page-overview > .flex.flex-wrap {
        flex-wrap: nowrap !important;
        gap: 8px !important;
        margin: 0 -12px 12px !important;
        padding: 0 12px 2px;
        overflow-x: auto;
        scrollbar-width: none;
    }

    #page-overview > .flex.flex-wrap::-webkit-scrollbar { display: none; }
    #page-overview .filter-btn { flex: 0 0 auto; border-radius: 999px !important; white-space: nowrap; }
    #page-overview #month-picker { margin-left: 0 !important; }

    #page-overview .grid[class*="minmax(260px"] {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        gap: 10px !important;
        margin-bottom: 16px !important;
    }

    #page-overview .hero-card {
        padding: 14px !important;
        border-radius: 16px !important;
    }

    #page-overview .hero-card.revenue-card,
    #page-overview .hero-card:last-child { grid-column: 1 / -1; }
    #page-overview .hero-card .card-icon { top: 12px !important; right: 12px !important; font-size: 1.4rem !important; }
    #page-overview .hero-card-skeleton { inset-inline: 14px !important; top: 14px !important; }
    #page-overview .hero-card-content > div:first-child { margin-bottom: 4px !important; font-size: 0.8rem !important; }
    #page-overview .hero-card-content > div:nth-child(2) { margin-bottom: 4px !important; font-size: 1.6rem !important; }
    #page-overview .hero-card-content > div:nth-child(3) { font-size: 0.72rem !important; }

    /* ---- ร่างจากเพจ · ทีม LINE Bot ---------------------------------------------------------------------- */
    .lt-wrap { gap: 12px !important; }

    .lt-summary {
        flex-wrap: nowrap !important;
        gap: 8px !important;
    }

    .lt-stat {
        flex: 1 1 0;
        min-width: 0 !important;
        padding: 10px 12px !important;
        border-radius: 14px !important;
    }

    .lt-stat strong { font-size: 1.3rem !important; }
    .lt-stat strong.fbd-sync-time { font-size: 0.82rem !important; line-height: 1.35 !important; }
    .lt-stat span { font-size: 0.72rem !important; line-height: 1.3; }

    .lt-summary > .lt-btn {
        flex: 0 0 44px;
        padding: 0 !important;
        border-radius: 14px !important;
        font-size: 0 !important;
    }

    .lt-summary > .lt-btn i { font-size: 1rem; }

    .lt-card {
        padding: 14px !important;
        border-radius: 18px !important;
    }

    .lt-actions { width: 100%; }

    .lt-actions .lt-btn {
        flex: 1 1 auto;
        border-radius: 12px !important;
        font-size: 0.9rem !important;
    }

    /* ---- popups: a sheet from the bottom, like an app --------------------------------------------------- */
    .swal2-container.swal2-center:has(> .swal2-popup:not(.booking-detail-popup):not(.swal2-toast)) {
        display: flex !important;
        flex-direction: column;
        justify-content: flex-end;
        padding: 0 !important;
    }

    .swal2-container.swal2-center > .swal2-popup:not(.booking-detail-popup):not(.swal2-toast) {
        width: 100% !important;
        max-width: 100% !important;
        max-height: 92dvh;
        margin: 0 !important;
        padding: 20px 16px calc(16px + var(--safe-bottom)) !important;
        overflow-y: auto;
        border-radius: 22px 22px 0 0 !important;
        box-sizing: border-box;
    }

    .swal2-container.swal2-center > .swal2-popup:not(.booking-detail-popup):not(.swal2-toast)::before {
        content: "";
        width: 40px;
        height: 4px;
        margin: -8px auto 6px;
        border-radius: 2px;
        background: #d1d5db;
    }

    .swal2-container.swal2-center > .swal2-popup:not(.booking-detail-popup):not(.swal2-toast) .swal2-title {
        padding-top: 0 !important;
        font-size: 1.25rem !important;
    }

    .swal2-container.swal2-center > .swal2-popup.swal2-show:not(.booking-detail-popup):not(.swal2-toast) {
        animation: app-sheet-up 0.25s ease-out;
    }

    .swal2-container.swal2-center > .swal2-popup:not(.booking-detail-popup):not(.swal2-toast) .swal2-actions {
        flex-wrap: nowrap;
        gap: 8px;
        width: 100%;
        margin-top: 16px;
    }

    .swal2-container.swal2-center > .swal2-popup:not(.booking-detail-popup):not(.swal2-toast) .swal2-actions button {
        flex: 1 1 0;
        min-height: 46px;
        margin: 0 !important;
        border-radius: 12px !important;
    }
}

@keyframes app-sheet-up {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}
