/* ---------------------------------------------------------------------------
   Phone layout of each page. Loaded by _Layout.cshtml after every page's own
   styles, so these win over them. Everything is inside the one phone query
   (the same string as app-shell.css and AbcShell.PHONE_QUERY in app-shell.js;
   MobileLayoutContractTests keeps them identical). Nothing here applies to a
   desktop.

   Pages:  Order (.order-page)  History (.order-history-summary,
   #orderDetailsSection)  Items (.products-page)  Reports (.reports-page)
   Sign-in and Password (#loginPartial, the forms under .app-main).
   --------------------------------------------------------------------------- */

@media (max-width: 767.98px), (pointer: coarse) and (max-height: 500px) {

    /* ---- Shared: a line of an order as a card -------------------------------- */

    .order-line-cards,
    .history-line-cards {
        display: flex;
        flex-direction: column;
        gap: .6rem;
    }

    .line-card {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        grid-template-areas:
            "main    price"
            "actions actions";
        gap: .6rem .75rem;
        padding: .8rem .85rem;
        background: var(--app-surface);
        border: 1px solid var(--app-line);
        border-radius: 12px;
    }

    .line-card-main {
        grid-area: main;
        min-width: 0;
    }

    .line-card-name {
        color: var(--app-ink);
        font-weight: 700;
        line-height: 1.25;
        overflow-wrap: anywhere;
    }

    .line-card-meta {
        color: var(--app-muted);
        font-size: .8rem;
        line-height: 1.35;
    }

    .line-card-price {
        grid-area: price;
        text-align: right;
        font-variant-numeric: tabular-nums;
    }

    .line-card-total {
        color: var(--app-ink);
        font-weight: 700;
        font-size: 1.05rem;
    }

    .line-card-each {
        color: var(--app-muted);
        font-size: .75rem;
        white-space: nowrap;
    }

    .line-card-actions {
        grid-area: actions;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: .75rem;
    }

    .line-card-delete {
        width: 44px;
        height: 44px;
        border: 0;
        border-radius: 10px;
        background: #fdf0f0;
        color: #c0392b;
    }

    .order-empty,
    .history-empty {
        padding: 2rem 1rem;
        color: var(--app-muted);
        text-align: center;
    }

    .order-empty i {
        font-size: 2.25rem;
        opacity: .35;
        margin-bottom: .5rem;
    }

    .order-empty-title {
        margin: 0;
        color: var(--app-ink);
        font-weight: 700;
    }

    .order-empty-hint {
        margin: .25rem 0 0;
        font-size: .9rem;
    }

    /* Keep the keyboard company: while a box is being typed in, the bars pinned to the bottom
       step aside (on an iPhone they would otherwise float in the middle of the screen). */
    body:has(.app-main input:not([type="checkbox"]):not([type="radio"]):focus, .app-main textarea:focus) .app-nav,
    body:has(.app-main input:not([type="checkbox"]):not([type="radio"]):focus, .app-main textarea:focus) .order-totals {
        display: none !important;
    }

    /* With the tab bar out of the way, an open order reaches the bottom too. */
    body:has(.app-main input:not([type="checkbox"]):not([type="radio"]):focus, .app-main textarea:focus) #orderDetailsSection.is-open {
        bottom: 0;
    }

    /* ---- Order (Views/Home/Index.cshtml) --------------------------------------
       Lines first, then the order's details; the totals and Submit are pinned
       above the tab bar.                                                        */

    .order-page {
        display: flex;
        flex-direction: column;
        padding: 0 0 76px;
    }

    .order-page > .row {
        margin-left: 0;
        margin-right: 0;
    }

    .order-page > .row > .col-12 {
        padding: 0;
    }

    .order-items-row {
        order: 1;
        margin-bottom: 1rem;
    }

    .order-summary-row {
        order: 2;
        margin-bottom: 0 !important;
    }

    .order-page .card-header {
        padding: .75rem .9rem;
    }

    .order-page .card-title {
        font-size: 1.05rem;
        font-weight: 700;
    }

    .order-page .card-body {
        padding: .9rem;
    }

    /* The lines are cards themselves: no card around them, so they get the full width.
       The heading reads as the page title, like History and Items. */
    .order-items-row > .col-12 > .card {
        background: transparent;
        box-shadow: none;
    }

    .order-items-row .card-header {
        padding: .1rem .1rem .6rem;
        border: 0;
    }

    .order-items-row .card-title {
        color: var(--app-ink);
        font-size: 1.35rem;
    }

    .order-items-row > .col-12 > .card > .card-body {
        padding: 0;
    }

    .order-add {
        --bs-gutter-y: .6rem;
        --bs-gutter-x: .6rem;
        margin: 0 0 .75rem !important;
        padding: .3rem .6rem .9rem;
        background: var(--app-surface);
        border-radius: 14px;
        box-shadow: 0 1px 2px rgba(20, 23, 40, .06);
    }

    .order-add .form-label,
    .order-summary-row .form-label {
        margin-bottom: .25rem;
        font-size: .85rem;
    }

    #addButton {
        min-height: 42px;
        font-weight: 700;
    }

    .order-summary-row .card-body > .row {
        --bs-gutter-x: 0;
    }

    /* The totals card becomes the bar: "$123.45" over "3 items · 24 bottles", then Submit and Clear. */
    .order-totals {
        position: fixed;
        left: 0;
        right: 0;
        bottom: calc(var(--app-tabbar-h) + var(--app-safe-bottom));
        z-index: 1020;
        margin: 0;
        border-radius: 0 !important;
        border-top: 1px solid var(--app-line) !important;
        background: rgba(255, 255, 255, .98) !important;
        box-shadow: 0 -4px 16px rgba(20, 23, 40, .07) !important;
    }

    .order-totals > .card-body {
        display: flex;
        align-items: center;
        gap: .75rem;
        padding: .5rem calc(.75rem + var(--app-safe-right)) .5rem calc(.75rem + var(--app-safe-left)) !important;
    }

    .order-totals-lines {
        flex: 1 1 auto;
        width: auto;
        margin: 0 !important;
    }

    .order-totals-lines,
    .order-totals-lines tbody {
        display: block;
    }

    .order-totals-lines tbody {
        display: flex;
        flex-wrap: wrap;
        align-items: baseline;
        column-gap: .35rem;
    }

    .order-totals-lines tr {
        display: block;
    }

    .order-totals-lines > :not(caption) > * > * {
        display: inline;
        padding: 0;
        border: 0;
        background: transparent !important;
        box-shadow: none !important;
        color: var(--app-muted);
        font-size: .78rem;
    }

    .order-totals-lines td:first-child {
        display: none;
    }

    .order-totals-lines .t-amount {
        order: -1;
        flex: 0 0 100%;
    }

    .order-totals-lines .t-amount td {
        color: var(--app-ink);
        font-size: 1.2rem;
        font-variant-numeric: tabular-nums;
    }

    .order-totals-lines .t-items td:last-child::after {
        content: " items \00B7";
    }

    .order-totals-lines .t-bottles td:last-child::after {
        content: " bottles";
    }

    .order-totals-actions {
        flex: 0 0 auto;
    }

    #submitOrder {
        min-height: 46px;
        padding: 0 1.1rem;
        border-radius: 12px;
        font-weight: 700;
    }

    #clearOrder {
        width: 46px;
        min-height: 46px;
        padding: 0;
        border-radius: 12px;
    }

    /* Toasts on the Order page clear the totals bar too. */
    body:has(.order-page) #toastContainer {
        bottom: calc(var(--app-tabbar-h) + var(--app-safe-bottom) + 84px);
    }

    /* ---- History (Views/OrderHistory/Index.cshtml) ----------------------------
       Orders as a list; a tap opens the order as its own page (.is-open).       */

    .app-main > .container-fluid.p-4 {
        padding: 0 !important;
    }

    .order-history-summary {
        margin-bottom: 0 !important;
    }

    .order-history-summary > .d-flex.justify-content-between {
        flex-direction: column;
        align-items: stretch !important;
        gap: .6rem;
    }

    .order-history-summary h2.text-orange {
        color: var(--app-ink) !important;
        font-size: 1.35rem;
        font-weight: 700;
    }

    .search-container {
        width: 100%;
        margin-top: 0;
    }

    .order-cards-container {
        padding: 0;
    }

    .scroll-btn {
        display: none !important;
    }

    .order-cards {
        flex-direction: column;
        gap: .6rem;
        overflow: visible;
        padding: .25rem 0;
    }

    .order-card {
        flex: none;
    }

    .order-card:hover {
        transform: none;
    }

    .order-card .card,
    .order-card.selected .card {
        background: var(--app-surface);
        border: 1px solid var(--app-line);
        border-radius: 12px;
        box-shadow: none;
    }

    .order-card .card-body {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto auto;
        grid-template-areas:
            "title amount button"
            "date  qty    button";
        align-items: center;
        gap: .1rem .9rem;
        padding: .75rem .85rem;
        cursor: pointer;
    }

    .order-card .card-body > .d-flex {
        grid-area: title;
        flex-direction: row !important;
    }

    .order-card .card-title {
        margin: 0;
        color: var(--app-ink);
        font-size: 1rem;
        font-weight: 700;
    }

    .order-card .order-date {
        grid-area: date;
        margin: 0 !important;
        color: var(--app-muted);
        font-size: .8rem;
    }

    .order-card .order-details {
        display: contents;
    }

    .order-card .order-details > div {
        flex-direction: row !important;
        justify-content: flex-end !important;
        margin: 0 !important;
        text-align: right;
        font-variant-numeric: tabular-nums;
    }

    .order-card .order-details > div > span:first-child {
        display: none;
    }

    .order-card .order-details > div:first-child {
        grid-area: qty;
        color: var(--app-muted);
        font-size: .8rem;
    }

    .order-card .order-quantity::after {
        content: " bottles";
    }

    .order-card .order-details > div:nth-child(2) {
        grid-area: amount;
        color: var(--app-ink);
        font-weight: 700;
    }

    .order-card .reorder-btn {
        grid-area: button;
        width: auto !important;
        min-height: 40px;
        padding: .35rem .8rem;
        border-radius: 10px;
        font-weight: 600;
    }

    /* The order's own page. order-history.js sets display:block inline; only .is-open shows here. */
    #orderDetailsSection {
        display: none !important;
    }

    #orderDetailsSection.is-open {
        display: flex !important;
        flex-direction: column;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: calc(var(--app-tabbar-h) + var(--app-safe-bottom));
        z-index: 1035;
        margin: 0 !important;
        padding: 0 0 1rem;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        background: var(--app-bg);
    }

    body.sheet-open {
        overflow: hidden;
    }

    .sheet-bar {
        position: sticky;
        top: 0;
        z-index: 2;
        display: flex !important;
        align-items: center;
        gap: .5rem;
        min-height: calc(var(--app-topbar-h) + var(--app-safe-top));
        padding: var(--app-safe-top) calc(.75rem + var(--app-safe-right)) 0 calc(.5rem + var(--app-safe-left));
        background: var(--app-surface);
        border-bottom: 1px solid var(--app-line);
    }

    .sheet-back {
        min-height: 44px;
        padding: 0 .35rem;
        border: 0;
        background: none;
        color: var(--app-primary);
        font-weight: 600;
        white-space: nowrap;
    }

    .sheet-title {
        flex: 1 1 auto;
        min-width: 0;
        color: var(--app-ink);
        font-weight: 700;
        text-align: center;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    #sheetReorderAll {
        border-radius: 10px;
        font-weight: 600;
        white-space: nowrap;
    }

    .order-details-head {
        margin: .75rem .75rem .6rem !important;
    }

    .history-line-cards {
        padding: 0 .75rem;
    }

    .history-line-add {
        min-height: 42px;
        padding: 0 1rem;
        border-radius: 10px;
        font-weight: 700;
        white-space: nowrap;
    }

    /* ---- Items (Views/Product/index.cshtml) -----------------------------------
       Each product row is a card: name; code, size, per case, group; price and
       on hand; quantity and Add to Order.                                       */

    .products-page .products-title {
        margin-bottom: .1rem;
        color: var(--app-ink);
        font-size: 1.35rem;
        font-weight: 700;
        text-align: left !important;
    }

    .products-page .products-note {
        margin-bottom: .75rem;
        color: var(--app-muted);
        font-size: .8rem;
        font-weight: 400;
        text-align: left !important;
    }

    .products-page #filterForm {
        margin-bottom: 1rem !important;
        padding: .9rem;
        background: var(--app-surface);
        border-radius: 14px;
    }

    .products-page #filterForm .form-label {
        margin-bottom: .2rem;
        color: var(--app-ink);
        font-size: .85rem;
    }

    .products-page .table-container {
        border: 0 !important;
    }

    .products-page .table,
    .products-page .table tbody {
        display: block;
        border: 0;
    }

    .products-page .table thead {
        display: none;
    }

    .products-page .table tbody tr {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        column-gap: .5rem;
        row-gap: .25rem;
        margin-bottom: .6rem;
        padding: .8rem .85rem;
        background: var(--app-surface);
        border: 1px solid var(--app-line);
        border-radius: 12px;
    }

    /* Line breaks between the card's three rows. */
    .products-page .table tbody tr::before,
    .products-page .table tbody tr::after {
        content: "";
        flex: 0 0 100%;
        height: 0;
    }

    .products-page .table tbody tr::before { order: 3; }
    .products-page .table tbody tr::after  { order: 5; }

    .products-page .table > :not(caption) > * > * {
        display: block;
        padding: 0;
        border: 0;
        background: transparent !important;
        box-shadow: none !important;
        text-align: left !important;
    }

    .products-page .c-name {
        order: 1;
        flex: 0 0 100%;
        color: var(--app-ink);
        font-weight: 700;
        line-height: 1.25;
    }

    .products-page .c-code,
    .products-page .c-size,
    .products-page .c-case,
    .products-page .c-group {
        order: 2;
        color: var(--app-muted);
        font-size: .8rem;
    }

    .products-page .c-case::after {
        content: "/case";
    }

    .products-page .c-price {
        order: 4;
        flex: 1 1 auto;
        color: var(--app-ink);
        font-weight: 700;
        font-variant-numeric: tabular-nums;
    }

    .products-page .c-onhand {
        order: 4;
        color: var(--app-muted);
        font-size: .85rem;
        text-align: right !important;
    }

    .products-page .c-onhand::before {
        content: "On hand ";
    }

    .products-page .c-qty {
        order: 6;
    }

    .products-page .c-qty .quantity-input {
        width: 5rem !important;
        height: 44px;
        text-align: center;
    }

    .products-page .c-action {
        order: 6;
        flex: 1 1 auto;
    }

    .products-page .c-action .btn {
        min-height: 44px;
        border-radius: 10px;
        font-weight: 700;
    }

    /* ---- Reports (Views/Reports/Index.cshtml) ---------------------------------
       reports.css already stacks itself up to 768px wide. A phone held sideways
       is wider than that, so the same releases are repeated here for it.       */

    html:has(.reports-page),
    body:has(.reports-page) {
        overflow: visible;
    }

    .reports-page {
        height: auto;
        min-height: 0;
    }

    .reports-toolbar {
        position: static;
    }

    .reports-table-region {
        flex: 0 0 auto;
        overflow: visible;
    }

    .reports-table-scroll {
        max-height: 60vh;
    }

    .reports-title {
        color: var(--app-ink);
    }

    /* ---- Sign in, Password and Settings ---------------------------------------
       Their text was written for the photo background (white); the phone's is light. */

    .app-main [style*="color: white"],
    .app-main [style*="color:white"] {
        color: var(--app-primary) !important;
    }

    #loginPartial,
    .app-main > .container.mt-4 {
        max-width: 480px;
        margin-top: .5rem !important;
        padding: 0;
    }

    #loginPartial h2,
    .app-main > .container.mt-4 h2 {
        font-size: 1.35rem;
        font-weight: 700;
    }

    #loginPartial h4,
    .app-main > .container.mt-4 h4 {
        color: var(--app-muted);
        font-size: 1rem;
    }

    #loginForm,
    .app-main > .container.mt-4 form {
        margin-top: 1rem;
        padding: 1.1rem;
        background: var(--app-surface);
        border-radius: 14px;
        box-shadow: 0 1px 2px rgba(20, 23, 40, .06);
    }

    #loginForm .form-group,
    .app-main > .container.mt-4 form .form-group {
        margin-bottom: .75rem;
    }

    #loginButton {
        width: 100%;
        min-height: 48px;
        border-radius: 12px;
        font-weight: 700;
    }

    .app-main .text-muted {
        color: var(--app-muted) !important;
    }
}
