/* ==========================================================================
   responsive.css — breakpoints
   >=1200  full desktop: sidebar + fixed header + inner scroll panes
   <=1100  tighter header, narrower sidebar
   <=900   drawer sidebar + bottom nav + page scroll
   <=700   tables stack into cards, charts compress
   <=480   single column everywhere
   ========================================================================== */

@media (max-width: 1200px) {
    :root {
        --sidebar-width: 236px;
    }

    .app-content {
        padding: var(--sp-5);
    }
}

/* --------------------------------------------------------------------------
   <=1100 — reclaim header space
   -------------------------------------------------------------------------- */
@media (max-width: 1100px) {
    .header-tools .search-box input {
        min-width: 150px;
        width: 150px;
    }
}

/* --------------------------------------------------------------------------
   <=900 — sidebar becomes an overlay drawer; the page scrolls as one column
   -------------------------------------------------------------------------- */
@media (max-width: 900px) {
    html,
    body {
        height: auto;
    }

    body {
        overflow: auto; /* let the document scroll again */
    }

    .app-container {
        height: auto;
        min-height: 100dvh;
        display: block;
    }

    .app-main {
        height: auto;
        min-height: 100dvh;
    }

    .app-header {
        position: sticky;
        top: 0;
        z-index: 90;
        padding-inline: var(--sp-4);
    }

    .app-content {
        overflow: visible;
        padding: var(--sp-4);
        /* clear the fixed bottom nav */
        padding-bottom: calc(var(--bottom-nav-height) + var(--sp-6));
    }

    /* --- drawer --- */
    .app-sidebar {
        position: fixed;
        inset-block: 0;
        inset-inline-start: 0;
        width: min(84vw, var(--sidebar-width));
        transform: translateX(100%);
        box-shadow: var(--elevation-4);
        border-inline-end: 1px solid var(--hairline);
    }

    [dir="ltr"] .app-sidebar {
        transform: translateX(-100%);
    }

    .app-sidebar.is-open {
        transform: translateX(0);
    }

    /* The mobile drawer always shows full labels and the user block. */

    .sidebar-backdrop.is-active {
        display: block;
        opacity: 1;
    }

    /* --- header row --- */
    .header-lead {
        flex: none;
    }

    .header-tools {
        flex: 1 1 auto;
        min-width: 0;
    }

    .search-box {
        flex: 1 1 auto;
        min-width: 0;
    }

    /* Matches the <=1100 rule's specificity, which pins the field to 150px. */
    .header-tools .search-box input {
        width: 100%;
        min-width: 0;
    }

    /* Decorative: the whole button is the account affordance. */
    .account-btn svg {
        display: none;
    }

    /* The page carries its own heading in .page-head, so the header title is a
       duplicate here — and it costs the search field ~50px, which is the
       difference between a usable field and a stub. */
    .header-title {
        display: none;
    }

    /* --- bottom nav --- */
    .bottom-nav {
        position: fixed;
        inset-inline: 0;
        bottom: 0;
        height: var(--bottom-nav-height);
        background: var(--surface-container-low);
        border-top: 1px solid var(--hairline);
        display: flex;
        align-items: stretch;
        justify-content: space-around;
        z-index: 95;
        padding-bottom: env(safe-area-inset-bottom, 0);
        box-shadow: 0 -1px 3px rgba(16, 20, 22, 0.06);
    }

    .bnav-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        text-decoration: none;
        background: none;
        border: 0;
        color: var(--text-muted);
        font-size: var(--fs-2xs);
        font-weight: 600;
        font-family: inherit;
        cursor: pointer;
        padding: 0;
        position: relative;
        transition: color var(--motion-fast) var(--ease-standard);
    }

    .bnav-item svg {
        width: 22px;
        height: 22px;
        fill: currentColor;
    }

    .bnav-item.is-active {
        color: var(--primary);
    }

    .bnav-item.is-active::before {
        content: "";
        position: absolute;
        top: 0;
        inset-inline: 28%;
        height: 3px;
        background: var(--primary);
        border-radius: 0 0 var(--r-full) var(--r-full);
    }

    .bnav-badge {
        position: absolute;
        top: 6px;
        inset-inline-start: 50%;
        margin-inline-start: 4px;
        background: var(--error);
        color: var(--on-error);
        font-size: 9px;
        font-weight: 700;
        line-height: 1;
        padding: 2px 4px;
        border-radius: var(--r-full);
        font-variant-numeric: tabular-nums;
    }

    /* Toast must clear the bottom nav. */
    .toast-container {
        inset-inline: var(--sp-4);
        bottom: calc(var(--bottom-nav-height) + var(--sp-4));
        max-width: none;
    }

    /* Grids collapse a step */
    .kpi-grid {
        grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
        gap: var(--sp-3);
    }

    .stat-value {
        font-size: var(--fs-h1);
    }

    .kanban-col {
        flex: 0 0 82vw;
        max-width: none;
    }

    .page-head {
        margin-bottom: var(--sp-4);
    }

    .page-head h1 {
        font-size: var(--fs-h2);
    }
}

/* --------------------------------------------------------------------------
   <=700 — tables become stacked cards; charts breathe
   -------------------------------------------------------------------------- */
@media (max-width: 700px) {
    /* Stacked table: each row is a card, each cell shows its header as a label.
       The label comes from data-label, injected at render time. */
    .table-stack thead {
        display: none;
    }

    .table-stack,
    .table-stack tbody,
    .table-stack tr,
    .table-stack td {
        display: block;
        width: 100%;
    }

    .table-stack tbody tr {
        background: var(--surface-container-lowest);
        border: 1px solid var(--hairline);
        border-radius: var(--r-md);
        padding: var(--sp-3) var(--sp-4);
        margin-bottom: var(--sp-3);
        box-shadow: var(--elevation-1);
    }

    .table-stack tbody tr:hover {
        background: var(--surface-container-lowest);
    }

    .table-stack td {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: var(--sp-4);
        padding: 0.4rem 0;
        border-bottom: 1px solid var(--hairline);
        text-align: start;
    }

    .table-stack td:last-child {
        border-bottom: 0;
    }

    .table-stack td::before {
        content: attr(data-label);
        flex: none;
        font-size: var(--fs-2xs);
        font-weight: 700;
        color: var(--text-muted);
    }

    /* Cells with no label (actions row) span and right-align. */
    .table-stack td[data-label=""]::before {
        display: none;
    }

    .table-stack td[data-label=""] {
        justify-content: flex-end;
        padding-top: var(--sp-2);
    }

    /* Action buttons row in a stacked table. */
    .table-stack td.col-actions {
        width: auto;
        justify-content: flex-end;
    }

    .table-stack .row-actions .icon-button {
        width: 40px;
        height: 40px;
    }

    .table-foot {
        padding: var(--sp-4);
    }

    .cal-cell {
        min-height: 62px;
        padding: 4px;
    }

    .cal-event {
        font-size: 9px;
        padding: 1px 3px;
    }

    .cal-head {
        padding: var(--sp-2) 2px;
        font-size: var(--fs-2xs);
    }

    .grid-2,
    .grid-3,
    .detail-list,
    .field-grid {
        grid-template-columns: 1fr;
    }

    .card {
        padding: var(--sp-4);
        border-radius: var(--r-md);
    }

    .modal-foot {
        flex-direction: column-reverse;
    }

    .modal-foot .btn {
        width: 100%;
    }

    .modal-foot .spacer {
        display: none;
    }

    .filter-bar {
        gap: var(--sp-2);
    }

    .filter-bar .field,
    .filter-bar .search-box {
        flex: 1 1 100%;
    }
}

/* --------------------------------------------------------------------------
   <=480 — single column, compact chrome
   -------------------------------------------------------------------------- */
@media (max-width: 480px) {
    :root {
        --header-height: 58px;
    }

    .app-content {
        padding: var(--sp-3);
        padding-bottom: calc(var(--bottom-nav-height) + var(--sp-5));
    }

    .kpi-grid {
        grid-template-columns: 1fr;
    }

    .header-title {
        font-size: var(--fs-h3);
    }

    .auth-card {
        padding: var(--sp-6) var(--sp-5);
        border-radius: var(--r-lg);
    }

    .stat-value {
        font-size: var(--fs-h2);
    }

    .segmented {
        width: 100%;
    }

    .segmented button {
        flex: 1;
    }
}

/* --------------------------------------------------------------------------
   Short viewports: give the drawer room to scroll
   -------------------------------------------------------------------------- */
@media (max-height: 560px) and (max-width: 900px) {
    .app-sidebar {
        overflow-y: auto;
    }
}

/* TICKET CHAT FINAL ALIGNMENT V2 */

/*
  RTL chat:
  mine  = RIGHT + primary blue
  other = LEFT  + neutral surface
*/

.ticket-chat-list .ticket-chat-row {
    display: flex !important;
    width: 100% !important;
}

/* MY MESSAGE -> RIGHT */
.ticket-chat-list .ticket-chat-row.is-mine {
    justify-content: flex-start !important;
}

/* OTHER MESSAGE -> LEFT */
.ticket-chat-list .ticket-chat-row.is-other {
    justify-content: flex-end !important;
}

/* MY BUBBLE */
.ticket-chat-list .ticket-chat-row.is-mine > .ticket-chat-bubble {
    width: fit-content !important;
    max-width: 78% !important;

    background: var(--primary) !important;
    border: 1px solid var(--primary) !important;
    color: #fff !important;

    border-radius: 18px 18px 6px 18px !important;

    box-shadow:
        0 4px 12px
        color-mix(in srgb, var(--primary) 22%, transparent) !important;
}

.ticket-chat-list .ticket-chat-row.is-mine .ticket-chat-meta,
.ticket-chat-list .ticket-chat-row.is-mine .ticket-chat-meta strong,
.ticket-chat-list .ticket-chat-row.is-mine .ticket-chat-meta time,
.ticket-chat-list .ticket-chat-row.is-mine .ticket-chat-text {
    color: #fff !important;
}

.ticket-chat-list .ticket-chat-row.is-mine .ticket-chat-meta time {
    opacity: .78 !important;
}

/* OTHER BUBBLE */
.ticket-chat-list .ticket-chat-row.is-other > .ticket-chat-bubble {
    width: fit-content !important;
    max-width: 78% !important;

    background: var(--surface-container-lowest, #fff) !important;
    border: 1px solid var(--outline-variant, var(--border)) !important;
    color: var(--on-surface, var(--text)) !important;

    border-radius: 18px 18px 18px 6px !important;

    box-shadow:
        0 3px 10px rgba(0,0,0,.06) !important;
}

.ticket-chat-list .ticket-chat-row.is-other .ticket-chat-meta,
.ticket-chat-list .ticket-chat-row.is-other .ticket-chat-meta strong,
.ticket-chat-list .ticket-chat-row.is-other .ticket-chat-text {
    color: var(--on-surface, var(--text)) !important;
}

.ticket-chat-list .ticket-chat-row.is-other .ticket-chat-meta time {
    color: var(--on-surface-variant, var(--muted)) !important;
}


/* FILE INSIDE MY MESSAGE */
.ticket-chat-list .ticket-chat-row.is-mine .ticket-chat-file {
    background: rgba(255,255,255,.13) !important;
    border-color: rgba(255,255,255,.28) !important;
    color: #fff !important;
}


/* DARK MODE - MY MESSAGE STAYS PRIMARY */
[data-theme="dark"] .ticket-chat-list .ticket-chat-row.is-mine > .ticket-chat-bubble {
    background: var(--primary) !important;
    border-color: var(--primary) !important;
    color: #fff !important;
}


/* DARK MODE - OTHER MESSAGE */
[data-theme="dark"] .ticket-chat-list .ticket-chat-row.is-other > .ticket-chat-bubble {
    background: var(--surface-container, var(--surface)) !important;
    border-color: var(--outline-variant, var(--border)) !important;
    color: var(--on-surface, var(--text)) !important;
    box-shadow: 0 4px 14px rgba(0,0,0,.20) !important;
}


@media (max-width: 700px) {
    .ticket-chat-list .ticket-chat-row.is-mine > .ticket-chat-bubble,
    .ticket-chat-list .ticket-chat-row.is-other > .ticket-chat-bubble {
        max-width: 88% !important;
    }
}
