/* ==========================================================================
   AppDropdown  (Components/Common/Dropdown/AppDropdown.razor, js/app-dropdown.js)

   One popover, two layouts:
     >= 768px  menu anchored to its trigger (top/left set by app-dropdown.js)
     <  768px  bottom sheet sitting on the mobile tab bar (pure CSS); the scrim
               stops at the tab bar, so the tab bar stays visible and tappable.

   Rules that are meant to be overridden by Tailwind classes passed in from
   Razor (padding, widths, display of the wrapper) are wrapped in :where() so
   they have zero specificity.

   NOTE: state rules on .app-dd-pop must reach its OWN panel/scrim with `>` (never a plain
   descendant space): dropdowns nest (exchange picker inside the filters sheet), and an open
   parent would otherwise restyle the nested panel - e.g. its entry animation outranks the
   nested [data-closing] exit animation, which then never runs.

   NOTE: never give .app-dd-pop a `display` value - the browser hides closed
   popovers with display:none and an author `display` would show them all.

   Animation states (keep this contract, it prevents a close-flash):
     open      .app-dd-pop:popover-open:not([data-closing])  -> entry animation
     closing   .app-dd-pop[data-closing]                     -> exit animation, `forwards`
     closed    data-closing stays on until the next open (app-dropdown.js removes it in
               beforetoggle). Chrome keeps a hidden popover painted for the length of its
               overlay/display transition; if the entry animation lived on the panel's base
               rule, removing data-closing at that moment would replay it to 100% visible.
   ========================================================================== */

/* ---------- wrapper + trigger ---------- */

:where(.app-dd) {
    position: relative;
    display: inline-flex;
    align-items: center;
    min-width: 0;
}

:where(.app-dd-trigger) {
    position: relative;
    -webkit-tap-highlight-color: transparent;
}

    :where(.app-dd-trigger):focus-visible {
        outline: 2px solid rgba(var(--accent-primary));
        outline-offset: 2px;
    }

/* Built-in trigger looks (Variant). Custom triggers get none of this. */
.app-dd-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.25rem;
    height: 2.25rem;
    padding: 0 0.625rem;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    line-height: 1.25rem;
    font-weight: 500;
    white-space: nowrap;
    user-select: none;
    cursor: pointer;
    transition: background-color 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
}

@media (min-width: 768px) {
    .app-dd-btn {
        gap: 0.375rem;
        padding: 0 0.75rem;
    }
}

.app-dd-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* design system: buttons.toolbarDropdownTrigger */
.app-dd-btn-toolbar {
    background-color: rgba(var(--surface-muted), 0.3);
    color: rgba(var(--dark));
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
}

/* call to action on the accent colour (referral links) */
.app-dd-btn-accent {
    background-color: rgba(var(--accent-primary), 0.75);
    color: rgba(var(--accent-text), 0.75);
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
}

/* small chip inside a card (per-chart interval on phones) */
.app-dd-btn-chip {
    height: 1.75rem;
    padding: 0 0.5rem;
    gap: 0.25rem;
    border-radius: 0.375rem;
    font-size: 0.75rem;
    line-height: 1rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    background-color: rgba(var(--surface-muted));
    color: rgba(var(--dark));
}

@media (hover: hover) {
    .app-dd-btn-toolbar:hover {
        background-color: rgba(var(--surface-muted), 0.5);
    }

    .app-dd-btn-accent:hover {
        background-color: rgba(var(--accent-primary));
        color: rgba(var(--accent-text));
    }

    .app-dd-btn-chip:hover {
        background-color: rgba(var(--surface-muted), 0.75);
    }
}

/* One chevron for every dropdown. Toolbar triggers drop it on phones (they open a sheet). */
.app-dd-chevron {
    flex: none;
    width: 0.75rem;
    height: 0.75rem;
    transition: transform 0.2s ease;
}

.app-dd-btn-toolbar .app-dd-chevron,
.app-dd-btn-accent .app-dd-chevron {
    display: none;
}

@media (min-width: 768px) {
    .app-dd-btn-toolbar .app-dd-chevron,
    .app-dd-btn-accent .app-dd-chevron {
        display: block;
    }
}

/* Open state of the trigger: pure CSS, no round trip. */
.app-dd:has(> .app-dd-pop:popover-open) > .app-dd-trigger .app-dd-chevron {
    transform: rotate(180deg);
}

.app-dd:has(> .app-dd-pop:popover-open) > .app-dd-btn-toolbar {
    background-color: rgba(var(--surface-muted), 0.6);
}

.app-dd:has(> .app-dd-pop:popover-open) > .app-dd-btn-accent {
    background-color: rgba(var(--accent-primary));
    color: rgba(var(--accent-text));
}

.app-dd:has(> .app-dd-pop:popover-open) > .app-dd-btn-chip {
    box-shadow: 0 0 0 1px rgba(var(--accent-primary));
}

/* Popover polyfill (old Safari): same rule with its class. Must stay a separate rule. */
.app-dd:has(> .app-dd-pop.\:popover-open) > .app-dd-trigger .app-dd-chevron {
    transform: rotate(180deg);
}

/* ---------- popover box ---------- */

.app-dd-pop {
    /* reset the UA popover box; the panel inside carries the visuals */
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
    color: rgba(var(--dark));
    overflow: visible;
    z-index: 1000; /* ignored in the top layer; used by the polyfill */
}

    .app-dd-pop:not(:popover-open) {
        pointer-events: none; /* during a closing transition */
    }

.app-dd-panel {
    box-sizing: border-box;
}

.app-dd-scrim,
.app-dd-head,
.app-dd-label,
.app-dd-foot {
    display: none;
}

/* The body is a flex column; its children keep their size so long content scrolls
   instead of being squeezed. */
:where(.app-dd-body) > * {
    flex-shrink: 0;
}

/* ==========================================================================
   >= 768px: anchored menu
   ========================================================================== */
@media (min-width: 768px) {
    .app-dd-pop {
        inset: auto;
        width: max-content;
        height: auto;
        max-width: none;
        max-height: none;
    }

    .app-dd-panel {
        min-width: var(--app-dd-min-w, 10rem);
        width: var(--app-dd-w, auto);
        max-width: min(var(--app-dd-max-w, 28rem), calc(100vw - 16px));
        /* MaxVisibleItems (--app-dd-rows): N rows of .app-dd-item (2.25rem each) + the 1px gaps
           between them + the body's top padding (0.25rem) + panel border (2 x 1px). The bottom
           padding is left out on purpose: it would show a sliver of row N+1. Without
           --app-dd-rows this var is invalid and the fallback (no row cap) applies. */
        --app-dd-rows-h: calc(var(--app-dd-rows) * 2.25rem + (var(--app-dd-rows) - 1) * 1px + 0.25rem + 2px);
        max-height: min(var(--app-dd-avail, 70vh), var(--app-dd-rows-h, 100vh));
        overflow-y: auto;
        overscroll-behavior: contain;
        border: 1px solid rgba(var(--surface-muted));
        border-radius: 0.5rem;
        background-color: rgba(var(--surface-alt));
        box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.3);
        transform-origin: top left;
    }

    .app-dd-pop[data-x="end"] > .app-dd-panel {
        transform-origin: top right;
    }

    .app-dd-pop[data-side="top"] > .app-dd-panel {
        transform-origin: bottom left;
    }

    .app-dd-pop[data-side="top"][data-x="end"] > .app-dd-panel {
        transform-origin: bottom right;
    }

    /* Entry animation: only while open and not closing (see the note at "Animation states"). */
    .app-dd-pop:popover-open:not([data-closing]) > .app-dd-panel {
        animation: app-dd-menu-in 0.16s cubic-bezier(0.16, 1, 0.3, 1);
    }

    .app-dd-pop[data-side="top"]:popover-open:not([data-closing]) > .app-dd-panel {
        animation-name: app-dd-menu-in-up;
    }

    .app-dd-pop.\:popover-open:not([data-closing]) > .app-dd-panel {
        animation: app-dd-menu-in 0.16s cubic-bezier(0.16, 1, 0.3, 1); /* polyfill */
    }

    /* DesktopScroll = false: grows with its content, nested popups may overflow it */
    .app-dd-pop[data-scroll="off"] > .app-dd-panel {
        max-height: none;
        overflow: visible;
    }

    .app-dd-pop[data-closing] > .app-dd-panel {
        animation: app-dd-menu-out 0.1s ease-in forwards;
    }

    :where(.app-dd-body) {
        display: flex;
        flex-direction: column;
        gap: 1px;
        padding: 0.25rem;
    }

    .app-dd-label {
        display: block;
        padding: 0.375rem 0.625rem 0.25rem;
        font-size: 10px;
        font-weight: 500;
        line-height: 1.2;
        letter-spacing: 0.05em;
        text-transform: uppercase;
        color: rgba(var(--inactive-dark));
        user-select: none;
    }

    .app-dd-foot.app-dd-foot-desktop {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        gap: 0.5rem;
        padding: 0.5rem;
        border-top: 1px solid rgba(var(--divider), 0.1);
    }
}

/* ==========================================================================
   < 768px: bottom sheet on top of the tab bar
   ========================================================================== */
@media (max-width: 767.98px) {
    .app-dd-pop {
        /* everything above the tab bar; --app-bottom-offset comes from the APP SHELL tokens */
        inset: 0 0 var(--app-bottom-offset, 0px) 0;
        width: auto;
        height: auto;
        max-width: none;
        max-height: none;
        overflow: hidden;
    }

    .app-dd-scrim {
        display: block;
        position: absolute;
        inset: 0;
        background-color: rgba(0, 0, 0, 0.45);
        opacity: var(--app-dd-scrim, 1);
        touch-action: none;
        transition: opacity 0.2s ease;
    }

    .app-dd-panel {
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        display: flex;
        flex-direction: column;
        max-height: calc(100% - 2.5rem);
        padding-left: env(safe-area-inset-left, 0px);
        padding-right: env(safe-area-inset-right, 0px);
        border-radius: 1.25rem 1.25rem 0 0;
        background-color: rgba(var(--surface));
        box-shadow: 0 -16px 48px rgba(0, 0, 0, 0.3);
        transform: translateY(var(--app-dd-drag, 0px));
        transition: transform 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
    }

    /* Entry animation: only while open and not closing (see the note at "Animation states"). */
    .app-dd-pop:popover-open:not([data-closing]) > .app-dd-panel {
        animation: app-dd-sheet-in 0.28s cubic-bezier(0.2, 0.8, 0.2, 1);
    }

    .app-dd-pop:popover-open:not([data-closing]) > .app-dd-scrim {
        animation: app-dd-fade-in 0.2s ease-out;
    }

    .app-dd-pop.\:popover-open:not([data-closing]) > .app-dd-panel {
        animation: app-dd-sheet-in 0.28s cubic-bezier(0.2, 0.8, 0.2, 1); /* polyfill */
    }

    .app-dd-pop[data-dragging] > .app-dd-panel,
    .app-dd-pop[data-dragging] > .app-dd-scrim {
        transition: none;
    }

    .app-dd-pop[data-closing] > .app-dd-panel {
        animation: app-dd-sheet-out 0.22s cubic-bezier(0.4, 0, 1, 1) forwards;
    }

    .app-dd-pop[data-closing] > .app-dd-scrim {
        animation: app-dd-fade-out 0.22s ease-in forwards;
    }

    .app-dd-head {
        display: block;
        flex: none;
        padding: 0.5rem 1rem 0.75rem 1.25rem;
        border-bottom: 1px solid rgba(var(--surface-muted), 0.5);
        touch-action: none; /* the strip drags the sheet instead of scrolling the page */
        user-select: none;
        cursor: grab;
    }

    .app-dd-grabber {
        display: block;
        width: 2.5rem;
        height: 4px;
        margin: 0 auto 0.625rem;
        border-radius: 9999px;
        background-color: rgba(var(--inactive-dark), 0.45);
    }

    .app-dd-head-row {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 0.75rem;
    }

    .app-dd-title {
        display: flex;
        align-items: baseline;
        gap: 0.375rem;
        min-width: 0;
        font-size: 1rem;
        font-weight: 600;
        line-height: 1.3;
        color: rgba(var(--dark));
    }

    .app-dd-close {
        position: relative;
        display: flex;
        flex: none;
        align-items: center;
        justify-content: center;
        width: 2rem;
        height: 2rem;
        border-radius: 9999px;
        background-color: rgba(var(--surface-muted), 0.8);
        color: rgba(var(--dark));
        -webkit-tap-highlight-color: transparent;
    }

        .app-dd-close::before {
            content: "";
            position: absolute;
            inset: -0.375rem; /* ~44px hit area */
        }

        .app-dd-close .material-symbols-outlined {
            font-size: 18px;
            line-height: 1;
        }

    .app-dd-body {
        flex: 1 1 auto;
        min-height: 0;
        overflow-y: auto;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
    }

    :where(.app-dd-body) {
        display: flex;
        flex-direction: column;
        gap: 0.25rem;
        padding: 0.75rem 0.75rem 1rem;
    }

    .app-dd-foot {
        display: flex;
        flex: none;
        align-items: center;
        justify-content: flex-end;
        gap: 0.5rem;
        padding: 0.75rem 1.25rem;
        border-top: 1px solid rgba(var(--surface-muted), 0.5);
    }

    /* page scroll lock while a sheet is open; :has() keeps it right whatever Blazor re-renders */
    html:has(.app-dd-pop:popover-open) {
        overflow: hidden;
    }

    html:has(.app-dd-pop.\:popover-open) {
        overflow: hidden;
    }
}

/* Closing by outside tap / Escape / another dropdown opening is done by the browser. Where it
   can keep the popover in the top layer while it animates (overlay), animate that close too. */
@supports (overlay: auto) {
    .app-dd-pop {
        transition: overlay 0.22s allow-discrete, display 0.22s allow-discrete;
    }

    @media (min-width: 768px) {
        .app-dd-panel {
            transition: opacity 0.12s ease-in, transform 0.12s ease-in;
        }

        .app-dd-pop:not(:popover-open) > .app-dd-panel {
            opacity: 0;
            transform: scale(0.97);
        }
    }

    @media (max-width: 767.98px) {
        .app-dd-pop:not(:popover-open) > .app-dd-panel {
            transform: translateY(calc(100% + 4.5rem));
        }

        .app-dd-pop:not(:popover-open) > .app-dd-scrim {
            opacity: 0;
        }
    }
}

/* ==========================================================================
   Content building blocks
   ========================================================================== */

/* list row (menus) */
.app-dd-item {
    box-sizing: border-box;
    display: flex;
    align-items: center;
    gap: 0.625rem;
    width: 100%;
    min-width: 0;
    padding: 0.5rem 0.625rem;
    border-radius: 0.375rem;
    font-size: 0.875rem;
    line-height: 1.25rem;
    text-align: left;
    color: rgba(var(--dark));
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: background-color 0.15s ease;
}

    .app-dd-item:focus-visible {
        outline: none;
        box-shadow: inset 0 0 0 1px rgba(var(--accent-primary));
    }

    .app-dd-item:active {
        background-color: rgba(var(--surface-muted), 0.6);
    }

@media (hover: hover) {
    .app-dd-item:hover {
        background-color: rgba(var(--surface-muted), 0.6);
    }
}

.app-dd-item[aria-checked="true"],
.app-dd-item[aria-pressed="true"] {
    background-color: rgba(var(--accent-primary), 0.2);
}

.app-dd-item-label {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.app-dd-check {
    flex: none;
    color: rgba(var(--dark));
    visibility: hidden;
}

.app-dd-item[aria-checked="true"] .app-dd-check,
.app-dd-item[aria-pressed="true"] .app-dd-check {
    visibility: visible;
}

/* trailing hint (e.g. external-link icon): on hover with a mouse, always on phones */
.app-dd-trail {
    flex: none;
    opacity: 0;
    color: rgba(var(--inactive-dark));
    transition: opacity 0.15s ease;
}

.app-dd-item:hover .app-dd-trail,
.app-dd-item:focus-visible .app-dd-trail {
    opacity: 1;
}

/* option grid (intervals) */
.app-dd-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.25rem;
}

.app-dd-chip {
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 2rem;
    padding: 0 0.5rem;
    border-radius: 0.375rem;
    font-size: 0.75rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    color: rgba(var(--inactive-dark));
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: background-color 0.15s ease, color 0.15s ease;
}

    .app-dd-chip:focus-visible {
        outline: none;
        box-shadow: inset 0 0 0 1px rgba(var(--accent-primary));
    }

@media (hover: hover) {
    .app-dd-chip:hover {
        background-color: rgba(var(--surface-muted), 0.75);
        color: rgba(var(--dark));
    }
}

.app-dd-chip[aria-checked="true"],
.app-dd-chip[aria-pressed="true"] {
    background-color: rgba(var(--accent-primary));
    color: rgba(var(--accent-text));
}

.app-dd-sep {
    height: 1px;
    margin: 0.375rem 0.25rem;
    background-color: rgba(var(--divider), 0.1);
}

/* phones: thumb-sized targets */
@media (max-width: 767.98px) {
    .app-dd-item {
        min-height: 2.75rem;
        padding: 0.625rem 0.75rem;
        border-radius: 0.625rem;
    }

    .app-dd-trail {
        opacity: 1;
    }

    .app-dd-grid {
        gap: 0.5rem;
    }

    .app-dd-chip {
        height: 2.75rem;
        border-radius: 0.5rem;
        font-size: 0.875rem;
        background-color: rgba(var(--surface-muted), 0.35);
        color: rgba(var(--dark));
    }

    .app-dd-sep {
        margin: 0.5rem 0.25rem;
    }
}

/* ---------- motion ---------- */

@keyframes app-dd-menu-in {
    from {
        opacity: 0;
        transform: translateY(-4px) scale(0.97);
    }

    to {
        opacity: 1;
        transform: none;
    }
}

@keyframes app-dd-menu-in-up {
    from {
        opacity: 0;
        transform: translateY(4px) scale(0.97);
    }

    to {
        opacity: 1;
        transform: none;
    }
}

@keyframes app-dd-menu-out {
    to {
        opacity: 0;
        transform: scale(0.97);
    }
}

@keyframes app-dd-sheet-in {
    from {
        transform: translateY(calc(100% + 4.5rem));
    }

    to {
        transform: translateY(var(--app-dd-drag, 0px));
    }
}

@keyframes app-dd-sheet-out {
    from {
        transform: translateY(var(--app-dd-drag, 0px));
    }

    to {
        transform: translateY(calc(100% + 4.5rem));
    }
}

@keyframes app-dd-fade-in {
    from {
        opacity: 0;
    }

    to {
        opacity: var(--app-dd-scrim, 1);
    }
}

@keyframes app-dd-fade-out {
    from {
        opacity: var(--app-dd-scrim, 1);
    }

    to {
        opacity: 0;
    }
}

@media (prefers-reduced-motion: reduce) {
    .app-dd-pop,
    .app-dd-panel,
    .app-dd-scrim,
    .app-dd-chevron {
        animation: none !important;
        transition: none !important;
    }
}
