/* =========================================================
   CRKD SHARED COMPONENTS
   Confirmation dialogs, skeleton loaders, table states, badges,
   pagination, toolbars, toasts, loading buttons, file pickers,
   the live-update status and the small-screen blocker.
   ui.js and live.js build the markup.

   Every page loads it after its base stylesheet (dashboard.css,
   PointofSale.css or index.css), whose colour tokens it uses.
   Tokens a base stylesheet does not define have fallbacks.
========================================================= */

/* Classes such as .btn set display, which would otherwise override the hidden attribute. */
[hidden] {
    display: none !important;
}

/* =========================
   CONFIRMATION DIALOG (showDialog)
   The page behind every dialog and modal is blurred, and the
   dialog's text and buttons are centred.
========================= */
.modal,
.dialog-overlay {
    -webkit-backdrop-filter: blur(6px);
    backdrop-filter: blur(6px);
}

.dialog-overlay {
    position: fixed;
    inset: 0;
    z-index: var(--z-dialog, 9999);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: var(--color-overlay);
    animation: fade-in 0.15s ease-out;
}

.dialog-card {
    width: min(100%, 440px);
    padding: 28px 24px 24px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    background: var(--color-surface);
    color: var(--color-text);
    box-shadow: var(--shadow-lg);
    text-align: center;
    animation: rise-in 0.18s ease-out;
}

.dialog-title {
    margin: 0 0 8px;
    font-size: 18px;
    font-weight: 700;
}

.dialog-message {
    margin: 0 0 24px;
    color: var(--color-text-muted);
    font-size: var(--font-size-md);
    line-height: 1.6;
    overflow-wrap: anywhere;
}

.dialog-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 12px;
}

/* =========================
   SMALL-SCREEN BLOCKER
   Below 768px wide the app is hidden and ui.js's notice is shown instead.
========================= */
.mobile-blocker {
    display: none;
}

@media (max-width: 767.98px) {
    body > :not(.mobile-blocker) {
        display: none !important;
    }

    .mobile-blocker {
        position: fixed;
        inset: 0;
        z-index: 2147483647;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 24px;
        /* The chrome colour matches the logo's own background, as in the sidebar. */
        background: var(--chrome-bg, var(--color-bg, #0d0d0d));
        color: var(--chrome-text-strong, var(--color-text, #f5f5f5));
        text-align: center;
    }
}

.mobile-blocker-card {
    display: grid;
    justify-items: center;
    gap: 12px;
    max-width: 360px;
}

.mobile-blocker-logo {
    width: 96px;
    margin-bottom: 8px;
}

html[data-theme="light"] .mobile-blocker-logo {
    filter: invert(1);
}

.mobile-blocker h2 {
    margin: 0;
    font-size: 20px;
}

.mobile-blocker p {
    margin: 0;
    color: var(--chrome-text, var(--color-text-muted, #c2c2c2));
    font-size: var(--font-size-md, 14px);
    line-height: 1.6;
}

/* =========================
   FILE PICKER
   ui.js turns input[type="file"][data-file-picker] into a button and
   the chosen file's name, so only the button opens the file chooser.
========================= */
.file-picker {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.file-picker-input {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.file-picker-button {
    flex: 0 0 auto;
}

.file-picker-button[aria-invalid="true"] {
    border-color: var(--color-danger);
}

.file-picker-name {
    min-width: 0;
    overflow: hidden;
    color: var(--color-text-subtle);
    font-size: var(--font-size-sm);
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-picker-name.has-file {
    color: var(--color-text);
    font-weight: 700;
}

/* =========================
   DROPDOWNS (select) ON TOUCH SCREENS
   Tablets draw an open dropdown with tall, finger-sized rows. Where
   the browser lets the page draw that list (appearance: base-select),
   it gets compact rows in the theme colours and scrolls past 280px.
   Mouse-only screens and other browsers keep their own list.
========================= */
@media (any-pointer: coarse) {
    @supports (appearance: base-select) {
        select,
        ::picker(select) {
            appearance: base-select;
        }

        select {
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        select::picker-icon {
            content: "";
            width: 7px;
            height: 7px;
            border: solid currentColor;
            border-width: 0 2px 2px 0;
            color: var(--color-text-subtle);
            rotate: 45deg;
            translate: 0 -2px;
        }

        select:open::picker-icon {
            rotate: 225deg;
            translate: 0 2px;
        }

        /* Opens below the field when it fits, above it otherwise. */
        ::picker(select) {
            position-try-order: normal;
            max-height: min(280px, 50vh);
            margin-block: 4px;
            padding: 4px;
            border: 1px solid var(--color-border);
            border-radius: var(--radius-md);
            background: var(--color-surface);
            color: var(--color-text);
            box-shadow: var(--shadow-lg);
            scrollbar-width: thin;
        }

        option {
            min-height: var(--control-height-sm, 32px);
            padding: 0 10px;
            border-radius: var(--radius-sm);
            font-size: var(--font-size-md);
        }

        option:hover,
        option:focus-visible {
            background: var(--color-surface-2);
        }

        /* Kept inside the list so it isn't clipped. */
        option:focus-visible {
            outline-offset: -3px;
        }

        option:checked {
            font-weight: 700;
        }

        option::checkmark {
            order: 1;
            margin-left: auto;
        }
    }
}

/* =========================
   LIVE-UPDATE STATUS (live.js adds it to the top bar)
========================= */
.live-status {
    --live-color: #9ca3af;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    height: var(--control-height, 40px);
    padding: 0 12px;
    border: 1px solid var(--chrome-control-border, #7a7a7a);
    border-radius: var(--radius-md);
    background: var(--chrome-surface, #1f1f1f);
    color: var(--chrome-text-strong, #ffffff);
    font-size: var(--font-size-sm);
    font-weight: 700;
    white-space: nowrap;
}

.live-status::before {
    content: "";
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--live-color);
}

.live-status.is-live { --live-color: #22c55e; }
.live-status.is-connecting { --live-color: #f59e0b; }

.live-status.is-live::before {
    animation: live-pulse 2s ease-out infinite;
}

@keyframes live-pulse {
    0% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.55); }
    70%, 100% { box-shadow: 0 0 0 6px rgba(34, 197, 94, 0); }
}

/* =========================
   SKELETON LOADING
========================= */
.skeleton {
    display: block;
    border-radius: var(--radius-sm);
    background: linear-gradient(90deg,
        var(--color-surface-2) 25%,
        var(--color-surface-3) 50%,
        var(--color-surface-2) 75%);
    background-size: 300% 100%;
    animation: skeleton-shimmer 1.4s ease-in-out infinite;
}

.skeleton-text {
    width: 100%;
    height: 12px;
}

.skeleton-text + .skeleton-text {
    margin-top: 10px;
}

/* A placeholder inside a line of text, such as a stat value. */
.skeleton-inline {
    display: inline-block;
    height: 0.8em;
    vertical-align: middle;
}

.skeleton-block {
    width: 100%;
    height: 100%;
    border-radius: var(--radius-md);
}

.skeleton-row td {
    pointer-events: none;
}

.skeleton-card {
    pointer-events: none;
}

.skeleton-card:hover {
    box-shadow: none;
    transform: none;
}

.skeleton-media {
    height: var(--skeleton-media-height, 180px);
    margin-bottom: 14px;
    border-radius: var(--radius-md);
}

/* Covers a chart canvas while its data loads. */
.chart-skeleton {
    position: absolute;
    inset: 0;
}

@keyframes skeleton-shimmer {
    from { background-position: 100% 0; }
    to { background-position: 0 0; }
}

/* =========================
   TABLE STATES (empty, no results, error)
========================= */
.table-state td {
    padding: 40px 16px;
    text-align: center;
    white-space: normal;
}

.table-state-content {
    display: grid;
    justify-items: center;
    gap: 6px;
    color: var(--color-text-muted);
}

.table-state-icon {
    margin-bottom: 4px;
    color: var(--color-text-subtle);
    font-size: 28px;
}

.table-state.is-error .table-state-icon {
    color: var(--color-danger);
}

.table-state-title {
    margin: 0;
    color: var(--color-text);
    font-size: var(--font-size-md);
    font-weight: 700;
}

.table-state-message {
    max-width: 440px;
    margin: 0;
    font-size: var(--font-size-sm);
}

.table-state .btn {
    margin-top: 10px;
}

/* =========================
   BADGES
   The label is always spelled out, so colour is not the only cue.
========================= */
.badge {
    --badge-color: var(--color-text-muted);
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px 10px;
    border: 1px solid color-mix(in srgb, var(--badge-color) 45%, transparent);
    border-radius: 999px;
    background: color-mix(in srgb, var(--badge-color) 12%, transparent);
    color: var(--badge-color);
    font-size: var(--font-size-xs);
    font-weight: 700;
    letter-spacing: 0.4px;
    line-height: 1.4;
    text-transform: uppercase;
    white-space: nowrap;
}

.badge::before {
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
}

.badge-success { --badge-color: var(--color-success); }
.badge-info    { --badge-color: var(--color-info, #7cb4ff); }
.badge-warning { --badge-color: var(--color-warning, #fbbf24); }
.badge-danger  { --badge-color: var(--color-danger); }

/* =========================
   TABLE FILTERS & TOOLBAR
   A row of filters, then a row with the result count and the actions.
========================= */
.table-filters {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 12px;
    margin-bottom: 16px;
}

.table-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 12px 16px;
    margin-bottom: 12px;
}

.table-toolbar .table-summary {
    margin: 0;
}

.table-filters .form-field {
    min-width: 140px;
}

.table-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    /* Stays on the right when it wraps below the filters. */
    margin-left: auto;
}

.search-box {
    position: relative;
}

.search-box i {
    position: absolute;
    top: 50%;
    left: 12px;
    color: var(--color-text-subtle);
    font-size: var(--font-size-md);
    pointer-events: none;
    transform: translateY(-50%);
}

.search-box input {
    width: 100%;
    padding-left: 36px;
}

.table-summary {
    min-height: 20px;
    margin: 0 0 12px;
    color: var(--color-text-muted);
    font-size: var(--font-size-sm);
}

input[type="date"] {
    min-height: var(--control-height);
    padding: 0 12px;
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-md);
    background: var(--color-input-bg);
    color: var(--color-text);
    font-size: var(--font-size-md);
}

input[type="date"]:focus-visible {
    border-color: var(--color-focus);
    outline-offset: 1px;
}

/* =========================
   PAGINATION
========================= */
.pagination {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-top: 16px;
}

.pagination:empty {
    display: none;
}

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

.pagination-pages {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.pagination-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-width: var(--control-height-sm, 32px);
    height: var(--control-height-sm, 32px);
    padding: 0 10px;
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-md);
    background: var(--color-surface-2);
    color: var(--color-text);
    font-size: var(--font-size-sm);
    font-weight: 700;
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease;
}

.pagination-button:hover:not(:disabled) {
    background: var(--color-surface-3);
}

.pagination-button[aria-current="page"] {
    background: var(--color-primary);
    border-color: var(--color-primary);
    color: var(--color-on-primary);
}

.pagination-button:disabled {
    cursor: not-allowed;
    opacity: 0.45;
}

.pagination-ellipsis {
    padding: 0 4px;
    color: var(--color-text-subtle);
}

/* A button that reads like an inline link, such as "Download the template".
   :where() keeps these defaults below a page's own .link-button styles (the login page has one). */
:where(.link-button) {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 0;
    border: 0;
    background: none;
    color: var(--color-info, #7cb4ff);
    font: inherit;
    font-weight: 700;
    text-decoration: underline;
    text-underline-offset: 2px;
    cursor: pointer;
}

:where(.link-button:hover) {
    color: var(--color-text);
}

:where(.link-button:disabled) {
    cursor: progress;
    opacity: 0.7;
}

/* =========================
   DETAIL LIST (label / value pairs)
========================= */
.detail-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 14px 24px;
    margin: 0;
}

.detail-grid dt {
    color: var(--color-text-subtle);
    font-size: var(--font-size-xs);
    font-weight: 700;
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

.detail-grid dd {
    margin: 4px 0 0;
    color: var(--color-text);
    font-size: var(--font-size-md);
    font-weight: 700;
    overflow-wrap: anywhere;
}

/* =========================
   TOASTS (non-blocking notices)
========================= */
.toast-stack {
    position: fixed;
    right: 24px;
    bottom: 24px;
    z-index: 10000;
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: min(380px, calc(100vw - 32px));
    pointer-events: none;
}

.toast {
    --toast-accent: var(--color-info, #7cb4ff);
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 14px 14px 14px 16px;
    border: 1px solid var(--color-border);
    border-left: 4px solid var(--toast-accent);
    border-radius: var(--radius-md);
    background: var(--color-surface);
    color: var(--color-text);
    box-shadow: var(--shadow-lg);
    font-size: var(--font-size-md);
    pointer-events: auto;
    animation: toast-in 0.2s ease-out;
}

.toast-success { --toast-accent: var(--color-success); }
.toast-error   { --toast-accent: var(--color-danger); }

.toast-icon {
    margin-top: 2px;
    color: var(--toast-accent);
}

.toast-message {
    flex: 1;
    margin: 0;
    line-height: 1.45;
    overflow-wrap: anywhere;
}

.toast-close {
    display: inline-grid;
    place-items: center;
    width: 28px;
    height: 28px;
    margin: -4px -4px 0 0;
    padding: 0;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--color-text-subtle);
    cursor: pointer;
}

.toast-close:hover {
    background: var(--color-surface-2);
    color: var(--color-text);
}

.toast.is-leaving {
    animation: toast-out 0.18s ease-in forwards;
}

@keyframes toast-in {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes toast-out {
    to { opacity: 0; transform: translateY(8px); }
}

/* =========================
   LOADING BUTTONS & CONTENT FADE
========================= */
.btn-spinner {
    flex: 0 0 auto;
    width: 14px;
    height: 14px;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

.btn[aria-busy="true"] {
    cursor: progress;
}

.fade-in {
    animation: fade-in 0.25s ease-out;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* =========================
   REDUCED MOTION
   Shimmer and slides stop; skeletons stay as plain blocks.
========================= */
@media (prefers-reduced-motion: reduce) {
    .dialog-card {
        animation-name: fade-in;
    }

    .live-status.is-live::before {
        animation: none;
    }

    .skeleton {
        animation: none;
        background: var(--color-surface-3);
    }

    .toast {
        animation-name: fade-in;
    }

    .toast.is-leaving {
        animation: none;
        opacity: 0;
    }

    .btn-spinner {
        animation-duration: 1.6s;
    }
}

@media (max-width: 800px) {
    .table-toolbar,
    .table-filters,
    .table-actions {
        align-items: stretch;
        width: 100%;
    }

    .table-filters .form-field {
        flex: 1 1 140px;
    }

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

    .toast-stack {
        right: 16px;
        bottom: 16px;
    }
}
