/* ==========================================================================
   Authentic Youth — v9 additions
   --------------------------------------------------------------------------
   The rebuilt live-chat console, the participant ticket thread, the manual
   participant form, the backup module and the back-to-top control.

   Loaded after app.css so it can refine existing components without editing
   them.  Every rule is written to work in both directions: logical properties
   (margin-inline-start, inset-inline-end) are used instead of left/right so
   the Arabic right-to-left layout mirrors correctly with no extra rules.
   ========================================================================== */

/* ---------------------------------------------------------------- stat row */
.stat-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: var(--space-4);
    margin-bottom: var(--space-6);
}

.stat-card {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    padding: var(--space-4) var(--space-5);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
}

.stat-card--alert {
    border-color: var(--warning, #b45309);
    background: var(--warning-soft, #fffbeb);
}

.stat-value {
    font-size: var(--text-2xl);
    font-weight: 700;
    line-height: 1.1;
    color: var(--text-primary);
}

.stat-label {
    font-size: var(--text-xs);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-secondary);
}

/* ------------------------------------------------------------ chat toolbar */
.chat-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
    align-items: flex-end;
}

.chat-toolbar-search {
    flex: 1 1 260px;
    min-width: 0;
}

/* 200px was too narrow once the option text is a real sentence: the audit
   filter showed "Removed, not yet r" with the rest cut off.  A select cannot
   ellipsis its own option label, so the control has to be wide enough for the
   longest one it offers. */
.chat-toolbar-sort {
    flex: 0 1 260px;
    min-width: 0;
}

.chat-toolbar-sort .select {
    text-overflow: ellipsis;
}

/* ------------------------------------------------------------- pages table */
/* Seven columns, one of them a free-text title and another a URL path.  Left
   to itself the browser sizes those to their longest value and the table ends
   up 52px wider than its panel, which put the Edit/Delete column outside the
   visible area.  A fixed layout with a percentage split keeps every column
   inside the panel and lets long values wrap instead. */
.pages-table {
    table-layout: fixed;
    width: 100%;
}

.pages-table th:nth-child(1), .pages-table td:nth-child(1) { width: 26%; }
.pages-table th:nth-child(2), .pages-table td:nth-child(2) { width: 20%; }
.pages-table th:nth-child(3), .pages-table td:nth-child(3) { width: 12%; }
.pages-table th:nth-child(4), .pages-table td:nth-child(4) { width: 8%; }
.pages-table th:nth-child(5), .pages-table td:nth-child(5) { width: 10%; }
.pages-table th:nth-child(6), .pages-table td:nth-child(6) { width: 12%; }
.pages-table th:nth-child(7), .pages-table td:nth-child(7) { width: 12%; }

.pages-table .pages-title strong {
    display: block;
    overflow-wrap: break-word;
}

.pages-table td code {
    /* A URL slug has no spaces, so it has to break somewhere — but never
       mid-word if it can break at a slash instead.  `/pages/accessibility`
       was splitting as "accessibilit / y"; breaking after the slash keeps
       each segment whole. */
    overflow-wrap: break-word;
    word-break: normal;
    white-space: normal;
}

.pages-table .cluster {
    flex-wrap: wrap;
    gap: var(--space-1);
}

.chat-toolbar .input,
.chat-toolbar .select {
    width: 100%;
    /* A <select> and a text <input> do not share an intrinsic height across
       browsers, which leaves the sort control sitting a few pixels above the
       search box.  An explicit height on both lines them up exactly. */
    height: 44px;
    min-height: 44px;
    box-sizing: border-box;
    padding-block: 0;
}

.chat-toolbar .btn {
    height: 44px;
    min-height: 44px;
}

.chat-tabs {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    margin-top: var(--space-4);
    padding-top: var(--space-4);
    border-top: 1px solid var(--border);
    /* Follows the document direction, so the pills start on the right in
       Arabic without a separate rule. */
    justify-content: flex-start;
}

.chat-tab {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-4);
    border: 1px solid var(--border);
    border-radius: 999px;
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--text-secondary);
    text-decoration: none;
    /* Comfortable tap target on a phone. */
    min-height: 40px;
}

.chat-tab:hover {
    border-color: var(--primary-500, #1f6f4a);
    color: var(--text-primary);
}

.chat-tab.is-active {
    background: var(--primary-500, #1f6f4a);
    border-color: var(--primary-500, #1f6f4a);
    color: #fff;
}

.chat-tab-count {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 999px;
    background: var(--danger, #b91c1c);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
}

/* -------------------------------------------------------------- chat table */
.chat-table {
    /* Ten columns, sized to their content rather than to fixed percentages.
       Fixed percentages were the root of two rounds of bugs: at 5% the word
       "Messages" could not fit in 47px, and forcing it produced mid-word
       breaks ("MES SAG ES").  Letting the browser size the columns and giving
       the table a sensible min-width means words stay whole and the wrapper
       scrolls if a phone is narrower than the table. */
    width: 100%;
    min-width: 880px;
    border-collapse: collapse;
    table-layout: auto;
}

.chat-table th,
.chat-table td {
    padding: var(--space-2);
    vertical-align: middle;
    text-align: start;
    /* `break-word` only splits a token that genuinely cannot fit — unlike
       `anywhere`, which happily broke "Open" across two lines. */
    overflow-wrap: break-word;
    box-sizing: border-box;
}

/* Keep the customer column the one that absorbs spare width; every other
   column is content-sized. */
.chat-table th:first-child,
.chat-table td:first-child {
    width: 24%;
}

.chat-table .row-actions {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-1);
}

.chat-table .row-actions .btn,
.chat-table .row-actions form {
    width: 100%;
    white-space: normal;
}

.chat-table th {
    font-size: var(--text-xs);
    text-transform: uppercase;
    letter-spacing: 0.02em;
    color: var(--text-secondary);
    /* Headers are short labels; `nowrap` keeps each on one line so the row is
       one even band.  It is safe here because the table is allowed to be wider
       than the panel and scroll inside .table-wrap. */
    white-space: nowrap;
    line-height: 1.25;
    vertical-align: bottom;
}

.chat-table td {
    font-size: var(--text-sm);
    border-top: 1px solid var(--border);
}

.chat-table tr.is-unread {
    background: var(--warning-soft, #fffbeb);
}

.chat-table tr.is-unread td:first-child {
    box-shadow: inset 3px 0 0 var(--warning, #b45309);
}

[dir="rtl"] .chat-table tr.is-unread td:first-child {
    box-shadow: inset -3px 0 0 var(--warning, #b45309);
}

.chat-customer {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    text-decoration: none;
    color: inherit;
}

.chat-avatar {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--primary-500, #1f6f4a);
    color: #fff;
    font-weight: 700;
    font-size: var(--text-sm);
}

.chat-preview {
    display: block;
    font-size: var(--text-xs);
    color: var(--text-secondary);
    /* The flex child that holds the name and preview must be allowed to
       shrink, otherwise its content pushes the cell 174px past its column and
       the name runs under the Age cell. */
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-customer > span:last-child {
    min-width: 0;
}

.chat-customer strong,
.chat-customer .badge {
    overflow-wrap: anywhere;
}

.chat-table .num {
    text-align: end;
    font-variant-numeric: tabular-nums;
}

/* ------------------------------------------------------- conversation view */
.chat-console {
    display: grid;
    grid-template-columns: minmax(260px, 320px) minmax(0, 1fr);
    gap: var(--space-6);
    align-items: start;
}

.chat-console-side {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    min-width: 0;
}

.chat-console-main {
    min-width: 0;
}

.chat-thread-admin {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    max-height: 60vh;
    overflow-y: auto;
    padding-inline-end: var(--space-2);
}

.chat-msg {
    max-width: 78%;
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    background: var(--surface);
    overflow-wrap: anywhere;
}

.chat-msg--visitor {
    align-self: flex-start;
    background: var(--surface-2, #f6f7f6);
}

.chat-msg--staff {
    align-self: flex-end;
    background: var(--primary-soft, #e8f3ee);
    border-color: var(--primary-500, #1f6f4a);
}

.chat-msg-head {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    align-items: baseline;
    justify-content: space-between;
    margin-bottom: var(--space-1);
    font-size: var(--text-xs);
}

.chat-msg-body {
    font-size: var(--text-sm);
    line-height: 1.55;
}

.chat-msg-meta {
    color: var(--text-secondary);
    font-size: var(--text-xs);
}

/* --------------------------------------------------------- ticket threads */
.ticket-thread {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
}

.ticket-msg {
    padding: var(--space-3) var(--space-4);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--surface);
    overflow-wrap: anywhere;
}

.ticket-msg--staff {
    background: var(--primary-soft, #e8f3ee);
    border-color: var(--primary-500, #1f6f4a);
}

.ticket-msg--participant {
    background: var(--surface-2, #f6f7f6);
}

.ticket-msg-head {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: var(--space-1);
    font-size: var(--text-xs);
    color: var(--text-secondary);
}

.ticket-badge {
    display: inline-block;
    padding: 2px 10px;
    border-radius: 999px;
    font-size: var(--text-xs);
    font-weight: 600;
}

.ticket-badge--open {
    background: var(--success-soft, #e7f5ec);
    color: var(--success, #166534);
}

.ticket-badge--closed {
    background: var(--surface-2, #f1f2f1);
    color: var(--text-secondary);
}

/* ------------------------------------------------------------ back to top */
.back-to-top {
    position: fixed;
    inset-block-end: var(--space-6);
    inset-inline-end: var(--space-6);
    z-index: 60;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 48px;
    height: 48px;
    border: 1px solid var(--border);
    border-radius: 50%;
    background: var(--surface);
    color: var(--text-primary);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.16);
    cursor: pointer;
    opacity: 0;
    visibility: hidden;
    transform: translateY(8px);
    transition: opacity 0.2s ease, transform 0.2s ease, visibility 0.2s ease;
}

.back-to-top.is-visible {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.back-to-top:hover {
    background: var(--primary-500, #1f6f4a);
    border-color: var(--primary-500, #1f6f4a);
    color: #fff;
}

.back-to-top svg {
    width: 20px;
    height: 20px;
}

/* The chat launcher already owns the bottom corner on the public site, so the
   back-to-top control sits above it rather than on top of it. */
body.has-chat .back-to-top {
    inset-block-end: 96px;
}

/* --------------------------------------------------------- backup module */
.backup-list {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

.backup-item {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
    align-items: center;
    justify-content: space-between;
    padding: var(--space-3) var(--space-4);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface);
}

.backup-item-meta {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.backup-item-name {
    font-family: var(--font-mono, monospace);
    font-size: var(--text-sm);
    overflow-wrap: anywhere;
}

.backup-actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
}

/* ------------------------------------------------------- responsive fixes */
@media (max-width: 1024px) {
    .chat-console {
        grid-template-columns: 1fr;
    }

    .chat-thread-admin {
        max-height: none;
    }
}

@media (max-width: 768px) {
    .stat-row {
        grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
        gap: var(--space-3);
    }

    .stat-card {
        padding: var(--space-3) var(--space-4);
    }

    .chat-msg {
        max-width: 92%;
    }

    .chat-toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    .chat-toolbar-search,
    .chat-toolbar-sort {
        flex: 1 1 auto;
    }

    .chat-toolbar .btn {
        width: 100%;
    }

    .back-to-top {
        inset-block-end: var(--space-4);
        inset-inline-end: var(--space-4);
        width: 44px;
        height: 44px;
    }

    body.has-chat .back-to-top {
        inset-block-end: 88px;
    }

    .backup-item {
        flex-direction: column;
        align-items: stretch;
    }

    .backup-actions .btn {
        flex: 1 1 auto;
    }
}

@media (max-width: 480px) {
    .chat-tab {
        flex: 1 1 auto;
        justify-content: center;
    }

    .chat-avatar {
        width: 30px;
        height: 30px;
    }

    .chat-preview {
        max-width: 22ch;
    }
}

/* Respect a visitor who has asked for less motion. */
@media (prefers-reduced-motion: reduce) {
    .back-to-top {
        transition: none;
    }
}

/* ---------------------------------------------------------------------
   Split registrations queue (v13).
   ---------------------------------------------------------------------
   The queue is three tables rather than one, so it needs a small header
   row and a right-aligned action column.  Every rule here is written so
   the layout survives RTL: nothing uses a left/right offset that would
   point the wrong way in Arabic, and the action column aligns to the
   text direction rather than to a physical side. */

.split-head {
    display: flex;
    flex-wrap: wrap;
    gap: var(--s3);
    align-items: center;
    justify-content: space-between;
    margin-block: var(--s5) var(--s4);
}

.split-count {
    margin: 0;
    font-size: var(--text-sm);
    color: var(--text-muted);
}

/* The last cell of each row is a short action.  Narrow it so it never
   eats the column beside it, and keep the button from wrapping. */
.cell-action {
    width: 1%;
    white-space: nowrap;
    text-align: end;
}

/* The pager in the design system centres itself; in an admin table it
   reads better spaced out with Back and Next at the edges. */
.pager {
    justify-content: space-between;
    gap: var(--s3);
    margin-block-start: var(--s6);
}

.pager-pages {
    display: flex;
    align-items: center;
    gap: var(--s1);
    flex-wrap: wrap;
    justify-content: center;
}

.pager-num {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    /* 44px square: the smallest reliable touch target, so the pager works
       on a phone as well as with a mouse. */
    min-width: 44px;
    min-height: 44px;
    padding-inline: var(--s2);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    text-decoration: none;
    font-variant-numeric: tabular-nums;
}

a.pager-num:hover {
    border-color: var(--primary);
    color: var(--primary);
    text-decoration: none;
}

.pager-num.is-current {
    background: var(--primary);
    border-color: var(--primary);
    color: var(--text-invert);
    font-weight: 700;
}

.pager-gap {
    padding-inline: var(--s1);
    color: var(--text-muted);
}

/* Back and Next are real links when available and inert spans when not,
   so a keyboard user never tabs onto a dead control. */
.pager-btn {
    display: inline-flex;
    align-items: center;
    gap: var(--s1);
    min-height: 44px;
    padding-inline: var(--s3);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    text-decoration: none;
    white-space: nowrap;
}

a.pager-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
    text-decoration: none;
}

.pager-btn.is-disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

@media (max-width: 560px) {
    /* On a phone the numbered pages are the least useful control: Back and
       Next are what people actually reach for. */
    .pager {
        justify-content: space-between;
    }

    .pager-pages {
        order: 3;
        width: 100%;
    }
}
