/* ============================================================================
   SmartGrid toolbar — modern, theme-driven look
   ----------------------------------------------------------------------------
   Restyles the header rendered by SpreadsheetHeader.razor (gear menu, action
   buttons, search pill, filter triggers, pager) WITHOUT touching any of the
   if/else render logic in SmartGrid.razor / SpreadsheetHeader.razor.

   Loaded AFTER app.css / appV.css / CustomUI.css so it wins on equal
   specificity; a few rules re-state the old selectors to beat their
   specificity (documented inline).

   Everything is driven off --theme-default (the Cuba accent colour set by the
   Appearance dropdown), so the toolbar re-skins itself with the theme.
   ========================================================================= */

.sheet-header {
    /* ---- palette (light) ------------------------------------------------ */
    --sgh-accent: var(--theme-default, #7366ff);
    --sgh-surface: #fff;
    --sgh-surface-2: #f5f6fa;
    --sgh-border: #e4e7ef;
    --sgh-border-strong: #d3d8e6;
    --sgh-text: #2f3a54;
    --sgh-text-muted: #7b869e;
    --sgh-tint: color-mix(in srgb, var(--sgh-accent) 10%, transparent);
    --sgh-tint-strong: color-mix(in srgb, var(--sgh-accent) 18%, transparent);
    --sgh-ring: color-mix(in srgb, var(--sgh-accent) 22%, transparent);
    /* ---- metrics -------------------------------------------------------- */
    --sgh-btn-h: 32px;
    --sgh-radius: 10px;
    --sgh-gap: .4rem;
    --sgh-font: .765rem;
}

body.dark-only .sheet-header {
    --sgh-surface: #14213f;
    --sgh-surface-2: #1b2b4f;
    --sgh-border: #26365c;
    --sgh-border-strong: #33456f;
    --sgh-text: #d9e2f3;
    --sgh-text-muted: #8ea0c4;
    --sgh-tint: color-mix(in srgb, var(--sgh-accent) 22%, transparent);
    --sgh-tint-strong: color-mix(in srgb, var(--sgh-accent) 32%, transparent);
    --sgh-ring: color-mix(in srgb, var(--sgh-accent) 34%, transparent);
}

/* ===========================================================================
   1. The bar itself
   ======================================================================== */
.sheet-header {
    background: linear-gradient(180deg, var(--sgh-surface) 0%, var(--sgh-surface-2) 100%) !important;
    border: 1px solid var(--sgh-border) !important;
    border-radius: 14px !important;
    box-shadow: 0 1px 2px rgba(15,23,42,.05), 0 6px 18px rgba(15,23,42,.05) !important;
    padding: .45rem .55rem !important;
    position: relative;
}

/* thin accent hairline along the top edge — ties the bar to the theme colour */
.sheet-header::before {
    content: "";
    position: absolute;
    left: 14px;
    right: 14px;
    top: 0;
    height: 2px;
    border-radius: 0 0 3px 3px;
    background: linear-gradient(90deg, var(--sgh-accent), transparent 85%);
    opacity: .75;
    pointer-events: none;
}

/* Re-stated with the same selector as appV.css so the later file wins:
   one row on desktop, never the old 0.75rem padding / wrap. */
.smartgrid-header {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: var(--sgh-gap);
    padding: 0 !important;
    background: transparent;
    min-width: 0;
}

/* appV.css: `.smartgrid-header > div:nth-child(4) { flex-grow: 1 }` — that
   index lands on a different child depending on which optional blocks render,
   so neutralise it and let the explicit rules below own the sizing. */
.smartgrid-header > div:nth-child(4) {
    flex-grow: 0;
}

/* The TopToolbar strip that sits above the grid gets the same rhythm. */
.smartgrid-toolbar:not(:empty) {
    display: flex;
    align-items: center;
    gap: var(--sgh-gap);
    flex-wrap: wrap;
    margin-bottom: .35rem;
}

/* ===========================================================================
   2. Buttons — one shape, one height, everywhere in the bar
   ======================================================================== */
.smartgrid-header .btn {
    --bs-btn-focus-box-shadow: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .3rem;
    min-height: var(--sgh-btn-h);
    height: var(--sgh-btn-h);
    padding: 0 .7rem;
    border-radius: var(--sgh-radius);
    font-size: var(--sgh-font);
    font-weight: 500;
    line-height: 1;
    white-space: nowrap;
    transition: background-color .14s ease, color .14s ease, border-color .14s ease, box-shadow .14s ease, transform .1s ease;
}

.smartgrid-header .btn:active:not(:disabled) {
    transform: translateY(1px);
}

.smartgrid-header .btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px var(--sgh-ring);
}

.smartgrid-header .btn i {
    font-size: .82rem;
    line-height: 1;
}

/* icons carry me-1 from the markup; the flex gap already spaces them */
.smartgrid-header .btn > i.me-1 {
    margin-right: 0 !important;
}

.smartgrid-header .btn .small {
    font-size: inherit;
}

.smartgrid-header .btn:disabled,
.smartgrid-header .btn[aria-disabled="true"] {
    opacity: .45;
}

/* --- primary (Add New / Save / Search) --- */
.smartgrid-header .btn-primary {
    background: var(--sgh-accent);
    border-color: var(--sgh-accent);
    color: #fff;
    box-shadow: 0 1px 2px color-mix(in srgb, var(--sgh-accent) 35%, transparent);
}

.smartgrid-header .btn-primary:hover:not(:disabled) {
    background: color-mix(in srgb, var(--sgh-accent) 88%, #000);
    border-color: color-mix(in srgb, var(--sgh-accent) 88%, #000);
    color: #fff;
}

/* --- outline family: soft tinted fill instead of hard 1px outlines --- */
.smartgrid-header .btn-outline-secondary,
.smartgrid-header .btn-outline-success,
.smartgrid-header .btn-outline-danger,
.smartgrid-header .btn-outline-warning,
.smartgrid-header .btn-outline-primary,
.smartgrid-header .btn-light {
    background: var(--sgh-surface);
    border: 1px solid var(--sgh-border-strong);
    color: var(--sgh-text);
}

.smartgrid-header .btn-outline-secondary:hover:not(:disabled),
.smartgrid-header .btn-outline-primary:hover:not(:disabled),
.smartgrid-header .btn-light:hover:not(:disabled) {
    background: var(--sgh-tint);
    border-color: color-mix(in srgb, var(--sgh-accent) 45%, var(--sgh-border-strong));
    color: var(--sgh-accent);
}

/* keep the semantic colours readable but calm */
.smartgrid-header .btn-outline-success { color: #1a8f5f; }
.smartgrid-header .btn-outline-success:hover:not(:disabled) { background: rgba(26,143,95,.12); border-color: rgba(26,143,95,.45); color: #14724c; }
.smartgrid-header .btn-outline-danger { color: #d2465a; }
.smartgrid-header .btn-outline-danger:hover:not(:disabled) { background: rgba(210,70,90,.12); border-color: rgba(210,70,90,.45); color: #b7364a; }
.smartgrid-header .btn-outline-warning { color: #c07b12; }
.smartgrid-header .btn-outline-warning:hover:not(:disabled) { background: rgba(192,123,18,.14); border-color: rgba(192,123,18,.45); color: #a4670c; }

body.dark-only .smartgrid-header .btn-outline-success { color: #55d6a0; }
body.dark-only .smartgrid-header .btn-outline-danger { color: #ff8298; }
body.dark-only .smartgrid-header .btn-outline-warning { color: #ffc45c; }

/* dropdown carets: subtler, and never a second visual weight */
.smartgrid-header .dropdown-toggle::after {
    margin-left: .3rem;
    opacity: .55;
    border-top-width: .28em;
}

/* CustomUI.css pinned the refresh button to 31.5px — match the new height */
.smartgrid-header .refresh {
    max-height: none;
    width: var(--sgh-btn-h);
    padding: 0 !important;
}

.smartgrid-header .spinner-border {
    width: 13px;
    height: 13px;
    border-width: 2px;
}

/* the rendered-row-count chip */
.smartgrid-header .badge.badge-primary {
    display: inline-flex;
    align-items: center;
    height: var(--sgh-btn-h);
    padding: 0 .6rem;
    border-radius: var(--sgh-radius);
    background: var(--sgh-tint) !important;
    color: var(--sgh-accent) !important;
    font-size: var(--sgh-font);
    font-weight: 600;
}

/* grouped buttons keep their own rounded shape (no fused pill) */
.smartgrid-header .btn-group > .btn,
.smartgrid-header .btn-group > .btn:first-child,
.smartgrid-header .btn-group > .btn:last-child {
    border-radius: var(--sgh-radius) !important;
}

.smartgrid-header .btn-group {
    gap: var(--sgh-gap);
}

.smartgrid-header .btn-group > .btn:not(:first-child) {
    margin-left: 0;
}

/* the pending-draft chip is a <div class="btn btn-light"> wrapper */
.smartgrid-header .btn-group .dropdown.btn-light {
    padding: 0 .3rem;
    background: var(--sgh-tint) !important;
    border-color: color-mix(in srgb, var(--sgh-accent) 35%, transparent);
}

/* gear / settings button — round icon button */
.smartgrid-header > .flex-shrink-0 .btn-primary.dropdown-toggle {
    width: var(--sgh-btn-h);
    padding: 0;
    border-radius: 50%;
}

.smartgrid-header > .flex-shrink-0 .btn-primary.dropdown-toggle::after {
    display: none;
}

/* dropdown menus opened from the bar */
.smartgrid-header .dropdown-menu {
    border: 1px solid var(--sgh-border);
    border-radius: 12px;
    box-shadow: 0 12px 32px rgba(15,23,42,.14);
    padding: .3rem;
    font-size: .8rem;
}

.smartgrid-header .dropdown-menu .dropdown-item {
    border-radius: 8px;
    padding: .4rem .6rem;
}

.smartgrid-header .dropdown-menu .dropdown-item:hover {
    background: var(--sgh-tint);
    color: var(--sgh-accent);
}

/* ===========================================================================
   3. Action-button rail (SingleLineButtonScroller)
   ======================================================================== */
.smartgrid-btn-scroller {
    flex: 0 1 auto;
    min-width: 0;
}

.smartgrid-btn-scroller .slbs__track {
    align-items: center;
    gap: var(--sgh-gap);
    padding: 1px 0;
}

/* the ActionToolbox / ActionButtons wrappers must not wrap inside the rail */
.smartgrid-btn-scroller .slbs__track .d-flex.flex-wrap {
    flex-wrap: nowrap !important;
    gap: var(--sgh-gap) !important;
    align-items: center;
}

/* softer edge fades than the global grey ones */
.smartgrid-btn-scroller.slbs::before {
    background: linear-gradient(to right, var(--sgh-surface), transparent);
    width: 28px;
}

.smartgrid-btn-scroller.slbs::after {
    background: linear-gradient(to left, var(--sgh-surface-2), transparent);
    width: 28px;
}

.smartgrid-btn-scroller .slbs__nav {
    height: 26px;
    width: 26px;
    background: var(--sgh-surface);
    color: var(--sgh-text);
    border: 1px solid var(--sgh-border-strong);
    box-shadow: 0 4px 12px rgba(15,23,42,.14);
    font-size: .7rem;
}

/* ===========================================================================
   4. Search pill + filter triggers
   ======================================================================== */
.grid-search-cluster {
    display: flex;
    align-items: center;
    flex-wrap: nowrap;
    gap: var(--sgh-gap);
    flex-shrink: 0;
    min-width: 0;
}

.grid-search-pill {
    flex: 0 0 auto;
    width: 300px;
    min-width: 0;
    height: var(--sgh-btn-h);
    border-radius: var(--sgh-radius);
    background: var(--sgh-surface);
    box-shadow: none;
    border: 1px solid var(--sgh-border-strong);
    overflow: hidden;
    transition: border-color .14s ease, box-shadow .14s ease;
}

.grid-search-pill:focus-within {
    border-color: var(--sgh-accent);
    box-shadow: 0 0 0 3px var(--sgh-ring);
}

.grid-search-pill > .form-select,
.grid-search-pill > .form-control,
.grid-search-pill > .btn {
    height: 100%;
    min-height: 0;
    border: none !important;
    border-radius: 0 !important;
    background: transparent;
    box-shadow: none !important;
    font-size: var(--sgh-font);
    color: var(--sgh-text);
}

.grid-search-pill > .form-select {
    background: var(--sgh-surface-2);
    border-right: 1px solid var(--sgh-border) !important;
    color: var(--sgh-text-muted);
    font-weight: 500;
}

.grid-search-pill > .form-control {
    padding: 0 .55rem;
}

.grid-search-pill > .form-control::placeholder {
    color: var(--sgh-text-muted);
}

/* the (X) clear button — restated to beat CustomUI.css's !important borders */
.smartgrid-header .grid-search-clear,
.grid-search-pill > .btn.grid-search-clear {
    border: none !important;
    background: transparent !important;
    color: var(--sgh-text-muted);
    width: 26px;
    padding: 0 !important;
}

.grid-search-pill > .btn.grid-search-clear:hover:not(:disabled) {
    color: var(--sgh-accent);
    background: transparent !important;
}

/* the magnifier button caps the pill */
.grid-search-pill > .btn-primary {
    background: var(--sgh-accent) !important;
    border: none !important;
    border-radius: 0 !important;
    width: 38px;
    padding: 0 !important;
    box-shadow: none;
}

/* Advanced search / date range / clear-filter triggers */
.grid-filter-actions {
    display: flex;
    align-items: center;
    gap: var(--sgh-gap);
    flex-wrap: nowrap;
    flex-shrink: 0;
}

.grid-filter-actions .btn {
    min-height: var(--sgh-btn-h);
    height: var(--sgh-btn-h);
    border-radius: var(--sgh-radius);
}

.grid-filter-actions .badge {
    font-size: .6rem;
    padding: .15em .4em;
}

.smartgrid-advsearch {
    border: 1px solid var(--sgh-border-strong) !important;
}

/* ===========================================================================
   5. Pager chip
   ======================================================================== */
.spq-pager-bar {
    background: var(--sgh-surface-2);
    border: 1px solid var(--sgh-border);
    border-radius: var(--sgh-radius);
    padding: .15rem .5rem;
    min-height: var(--sgh-btn-h);
}

.spq-pager-bar-size,
.spq-pager-bar-page {
    color: var(--sgh-text);
}

.spq-pager-bar-count {
    color: var(--sgh-text-muted);
}

.spq-pager-bar-btn {
    color: var(--sgh-text-muted);
}

.spq-pager-bar-btn:hover:not(:disabled) {
    background: var(--sgh-tint);
    color: var(--sgh-accent);
}

.spq-pager-bar-nav {
    border-left-color: var(--sgh-border);
}

.smartgrid-mini-pager .spq-pager-bar {
    margin-left: auto;
}

/* ===========================================================================
   6. Tablet — ≤1200px: shrink the search pill before anything else gives way
   ======================================================================== */
@media (max-width: 1200px) {
    .grid-search-pill {
        width: 210px;
    }
}

@media (max-width: 992px) {
    .grid-search-pill {
        width: 170px;
    }

    .grid-search-pill > .form-select {
        max-width: 34px !important;
    }
}

/* ===========================================================================
   7. Phone — ≤768px
   Three tidy rows, no matter how many buttons are rendered:
     row 1  gear + search pill (grows) + filter triggers
     row 2  action buttons on a swipeable rail
     row 3  pager, full width
   ======================================================================== */
@media (max-width: 768px) {
    .sheet-header {
        --sgh-btn-h: 40px; /* native-app tap targets */
        --sgh-font: .8rem;
        --sgh-gap: .45rem;
        border-radius: 12px !important;
        padding: .5rem !important;
    }

    /* appV.css sets `justify-content: space-between` here at this breakpoint,
       which would spread row 1 apart — the explicit orders/flex below own it. */
    .smartgrid-header {
        flex-wrap: wrap;
        row-gap: .45rem;
        justify-content: flex-start;
    }

    /* gear */
    .smartgrid-header > .flex-shrink-0:has(.dropdown-toggle) {
        order: 1;
    }

    /* search + filters share row 1 with the gear */
    .grid-search-cluster {
        order: 2;
        flex: 1 1 0;
        min-width: 0;
    }

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

    .grid-search-pill > .form-select {
        max-width: 30px !important;
        padding: 0 .2rem !important;
    }

    /* row 2 — the button rail always claims a full line of its own */
    .smartgrid-btn-scroller {
        order: 3;
        flex: 1 1 100%;
        width: 100%;
    }

    /* finger-swiped: hide the arrow affordances, snap the chips */
    .smartgrid-btn-scroller .slbs__nav {
        display: none !important;
    }

    .smartgrid-btn-scroller .slbs__track {
        scroll-snap-type: x proximity;
        scroll-padding-left: .1rem;
        overscroll-behavior-x: contain;
        padding-bottom: 2px;
    }

    .smartgrid-btn-scroller .slbs__track > * {
        scroll-snap-align: start;
    }

    /* row 3 — pager spans the width and spreads out */
    .smartgrid-header .spq-pager-bar {
        order: 4;
        flex: 1 1 100%;
        width: 100%;
        margin-left: 0;
        justify-content: space-between;
    }

    /* appV.css forces `.smartgrid-header > .ms-auto { width:100% }` and centres
       it at this breakpoint; the explicit orders above already handle layout. */
    .smartgrid-header > .ms-auto {
        justify-content: flex-start;
    }

    /* stop the OS scrollbar gutter appearing under the rail on Android */
    .smartgrid-btn-scroller .slbs__track::-webkit-scrollbar {
        display: none;
    }
}

/* ===========================================================================
   8. Small phone — ≤480px
   Text labels collapse to icons so any number of buttons stays reachable in
   one swipe; the gear moves onto the button rail's line to buy search width.
   ======================================================================== */
@media (max-width: 480px) {
    .sheet-header {
        --sgh-gap: .4rem;
        padding: .45rem .4rem !important;
    }

    .smartgrid-header .btn {
        padding: 0 .55rem;
    }

    /* icon-only chips for the long-labelled actions */
    .smartgrid-btn-scroller .btn > span.small {
        display: none;
    }

    .smartgrid-btn-scroller .btn > i.me-1 {
        margin-right: 0 !important;
    }

    .smartgrid-btn-scroller .btn {
        min-width: var(--sgh-btn-h);
        padding: 0 .6rem;
    }

    /* keep the row-count chip compact */
    .smartgrid-header .badge.badge-primary {
        padding: 0 .45rem;
        font-size: .72rem;
    }

    /* pager: drop the per-page select label size a notch so the nav fits */
    .spq-pager-bar-count {
        font-size: .58rem;
    }
}

/* ===========================================================================
   9. Reduced motion
   ======================================================================== */
@media (prefers-reduced-motion: reduce) {
    .smartgrid-header .btn,
    .grid-search-pill,
    .smartgrid-btn-scroller .slbs__track {
        transition: none !important;
        scroll-behavior: auto !important;
    }

    .smartgrid-header .btn:active:not(:disabled) {
        transform: none;
    }
}
