/* ============================================================================
   SPURA HUB v3 - Mobile Command Palette
   Shared search/command-palette surface for mobile bottom navigation.
   Consolidates patterns from mobile-five-wins, mobile-next-level,
   mobile-search-results-fix, and mobile-shell-visual-polish.
   ============================================================================ */

@media (max-width: 768px) {
    body.cmd-palette-active::before {
        content: "";
        position: fixed;
        inset: 0;
        z-index: 1090;
        pointer-events: auto;
        background: var(--glass-misc-4920-68);
        -webkit-backdrop-filter: none;
        backdrop-filter: none;
    }

    :root[data-theme='light'] body.cmd-palette-active::before,
    :root:not([data-theme='dark']) body.cmd-palette-active::before {
        background: var(--gray-100);
        opacity: 0.98;
    }

    body.cmd-palette-active .cmd-palette-backdrop {
        display: none;
    }

    body.cmd-palette-active .topbar {
        display: contents !important;
        padding: 0 !important;
        margin: 0 !important;
        background: transparent !important;
        border: 0 !important;
        box-shadow: var(--shadow-none, none) !important;
        overflow: visible !important;
        -webkit-backdrop-filter: none !important;
        backdrop-filter: none !important;
    }

    body.cmd-palette-active .topbar-toggle,
    body.cmd-palette-active .search-kbd {
        display: none !important;
    }

    body.cmd-palette-active .topbar-search {
        position: fixed;
        top: 0;
        right: 0;
        bottom: max(var(--native-keyboard-height, 0px), var(--mobile-visual-keyboard-inset, 0px));
        left: 0;
        height: auto;
        width: 100%;
        min-height: 0;
        max-width: 100%;
        transform: none;
        z-index: 1100;
        background: var(--bg-primary);
        display: flex;
        flex-direction: column;
        gap: 0;
        overflow: hidden;
        isolation: isolate;
        padding:
            max(calc(env(safe-area-inset-top, 0px) + 0.32rem), calc(var(--safe-area-top) + 0.32rem))
            max(1rem, var(--safe-area-right))
            0.04rem
            max(1rem, var(--safe-area-left));
    }

    body.cmd-palette-active .topbar-search[data-state="idle"],
    body.cmd-palette-active .topbar-search[data-state="results"],
    body.cmd-palette-active .topbar-search[data-state="empty"] {
        gap: 0;
    }

    body.cmd-palette-active .topbar-search::before {
        content: none;
    }

    body.native-keyboard-visible.cmd-palette-active .topbar-search {
        padding-bottom: 0.42rem !important;
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search {
        background: var(--gray-100);
    }

    body.cmd-palette-active .mobile-search-heading {
        display: grid;
        grid-template-columns: auto minmax(0, 1fr);
        grid-template-rows: auto;
        align-items: center;
        gap: 0.45rem;
        position: relative;
        z-index: 1;
        flex: 0 0 auto;
        min-height: 0;
        padding: 0.12rem 0 0.5rem;
    }

    body.cmd-palette-active .mobile-search-title {
        display: none;
        min-width: 0;
        padding: 0;
        margin: 0;
        overflow-wrap: anywhere;
        color: var(--text-primary);
        font-size: clamp(2rem, 10vw, 2.55rem);
        font-weight: 800;
        line-height: 1.04;
        border: 0;
    }

    body.cmd-palette-active .mobile-search-close {
        grid-column: 1;
        grid-row: 1;
        align-self: center;
    }

    body.cmd-palette-active .mobile-topbar-heading,
    body.cmd-palette-active .mobile-topbar-user {
        display: none;
    }

    body.cmd-palette-active .topbar-search-wrapper {
        position: relative;
        grid-column: 2;
        grid-row: 1;
        width: 100%;
        min-width: 0;
        display: flex;
        align-items: center;
        z-index: 1;
        min-height: 2.85rem;
        padding-right: 0.1rem;
        border-radius: var(--radius-lg, 14px);
        background: var(--bg-surface);
        border: 1px solid var(--border-color);
        box-shadow: var(--shadow-sm);
        overflow: hidden;
        transition: border-color 0.18s ease, box-shadow 0.18s ease, background 0.18s ease;
    }

    body.cmd-palette-active .topbar-search-wrapper .form-input {
        position: relative;
        z-index: 1;
        width: 100%;
        min-height: 2.85rem;
        flex: 0 0 auto;
        padding-left: 2.52rem;
        padding-right: 3.82rem;
        font-size: 0.93rem;
        font-weight: 600;
        border-radius: var(--radius-lg, 14px);
        background: transparent;
        border: none;
        color: var(--text-primary);
        caret-color: auto;
        box-shadow: var(--shadow-none, none);
    }

    body.cmd-palette-active .topbar-search-wrapper .form-input::placeholder {
        color: var(--text-muted);
    }

    body.cmd-palette-active .topbar-search-icon {
        left: 1rem;
        top: 50%;
        transform: translateY(-50%);
        z-index: 2;
        color: var(--text-muted);
    }

    body.cmd-palette-active .topbar-search-clear {
        position: absolute;
        right: 0.45rem;
        top: 50%;
        transform: translateY(-50%);
        width: var(--touch-target-min, 44px);
        height: var(--touch-target-min, 44px);
        min-width: var(--touch-target-min, 44px);
        min-height: var(--touch-target-min, 44px);
        border-radius: var(--radius-full);
        background: var(--glass-slate-18);
        color: var(--text-muted);
        z-index: 2;
    }

    body.cmd-palette-active .topbar-search-clear:active {
        transform: translateY(-50%) scale(0.96);
    }

    body.cmd-palette-active .topbar-search.has-query .topbar-search-wrapper,
    body.cmd-palette-active .topbar-search[data-state="results"] .topbar-search-wrapper,
    body.cmd-palette-active .topbar-search[data-state="empty"] .topbar-search-wrapper {
        border-color: var(--brand-500);
        box-shadow:
            var(--shadow-md),
            0 0 0 3px var(--glass-brand-12);
    }

    body.cmd-palette-active .mobile-search-close {
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        width: var(--touch-target-min, 44px);
        height: var(--touch-target-min, 44px);
        min-width: var(--touch-target-min, 44px);
        min-height: var(--touch-target-min, 44px);
        padding: 0;
        border: none;
        background: transparent;
        border-radius: var(--radius-full);
        cursor: pointer;
        color: var(--text-primary);
        z-index: 1;
    }

    body.cmd-palette-active .mobile-search-close:active {
        transform: scale(0.96);
    }

    body.cmd-palette-active .topbar-search-results {
        position: static !important;
        top: auto !important;
        right: auto !important;
        left: auto !important;
        flex: 1 1 auto;
        min-height: 0;
        max-height: none;
        overflow-y: auto;
        margin-top: 0;
        border-radius: var(--radius-none, 0);
        z-index: 1;
        overscroll-behavior: contain;
        background: transparent;
        border: none;
        box-shadow: var(--shadow-none, none);
        padding: 0 0 calc(0.18rem + max(0px, var(--safe-area-bottom)));
        scroll-padding-top: 0;
    }

    body.cmd-palette-active .topbar-search-group + .topbar-search-group {
        margin-top: 0.75rem;
    }

    body.cmd-palette-active .topbar-search-group {
        animation: spuraSearchGroupIn var(--mobile-motion-medium) var(--mobile-motion-curve) both;
    }

    body.cmd-palette-active .topbar-search-group-heading {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 0.5rem;
        position: sticky;
        top: 0;
        z-index: 1;
        padding: 0 0.12rem 0.42rem;
        color: var(--text-muted);
        font-size: 0.68rem;
        font-weight: 700;
        letter-spacing: 0.07em;
        text-transform: uppercase;
        background: var(--bg-primary);
    }

    body.cmd-palette-active .topbar-search-group-heading__title {
        min-width: 0;
    }

    body.cmd-palette-active .topbar-search-group-heading__count {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: 1.35rem;
        min-height: 1.08rem;
        padding: 0.04rem 0.28rem;
        border-radius: var(--radius-full);
        background: var(--bg-secondary);
        color: var(--text-secondary);
        font-size: 0.62rem;
        font-weight: 760;
        font-variant-numeric: tabular-nums;
        letter-spacing: -0.01em;
        line-height: 1;
        flex: 0 0 auto;
    }

    body.cmd-palette-active .topbar-search-group-list {
        display: grid;
        gap: 0;
        border-radius: var(--radius-lg, 14px);
        overflow: hidden;
        background: var(--bg-surface);
        border: 1px solid var(--border-color);
        box-shadow: var(--shadow-xs);
    }

    body.cmd-palette-active .topbar-search[data-state="idle"] .topbar-search-group--recent .topbar-search-group-list,
    body.cmd-palette-active .topbar-search[data-state="idle"] .topbar-search-group--quick .topbar-search-group-list {
        background: var(--bg-surface);
        border: 1px solid var(--border-color);
        box-shadow: var(--shadow-xs);
    }

    body.cmd-palette-active .topbar-search-result,
    body.cmd-palette-active .topbar-search-empty {
        border-radius: var(--radius-none, 0);
        border: none;
        background: transparent;
    }

    body.cmd-palette-active .topbar-search-result {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        align-items: center;
        gap: 0.55rem;
        min-height: 2.88rem;
        box-sizing: border-box;
        padding: 0.72rem 0.85rem;
        border-bottom: 1px solid var(--border-color);
        transition: background 0.16s ease;
        box-shadow: var(--shadow-none, none);
    }

    body.cmd-palette-active .topbar-search[data-state="idle"] .topbar-search-group--recent .topbar-search-result {
        min-height: 2.72rem;
        padding: 0.68rem 0.85rem;
        background: transparent;
        border-bottom-color: var(--border-color);
    }

    body.cmd-palette-active .topbar-search[data-state="idle"] .topbar-search-group--quick .topbar-search-result {
        min-height: 2.72rem;
        padding: 0.68rem 0.85rem;
    }

    body.cmd-palette-active .topbar-search-group-list .topbar-search-result:last-child {
        border-bottom: none;
    }

    body.cmd-palette-active .topbar-search-result:hover,
    body.cmd-palette-active .topbar-search-result.active,
    body.cmd-palette-active .topbar-search-result:focus-visible {
        background: var(--bg-surface-hover);
        outline: none;
    }

    body.cmd-palette-active .topbar-search-result:active {
        background: var(--bg-surface-hover);
    }

    body.cmd-palette-active .topbar-search-result-content {
        display: grid;
        gap: 0.1rem;
        min-width: 0;
    }

    body.cmd-palette-active .topbar-search-result-label {
        display: block;
        width: 100%;
        font-size: 0.875rem;
        font-weight: 650;
        line-height: 1.22;
        color: var(--text-primary);
        overflow-wrap: anywhere;
    }

    body.cmd-palette-active .topbar-search-result-meta {
        display: block;
        width: 100%;
        font-size: 0.72rem;
        line-height: 1.2;
        color: var(--text-secondary);
        overflow-wrap: anywhere;
    }

    body.cmd-palette-active .topbar-search-result-chevron {
        margin-top: 0;
        color: var(--text-muted);
        flex-shrink: 0;
        opacity: 0.72;
    }

    body.cmd-palette-active .topbar-search-mark {
        padding: 0 0.18em;
        border-radius: var(--radius-none, 0).32rem;
        background: var(--glass-brand-12);
        color: inherit;
    }

    body.cmd-palette-active .topbar-search-empty {
        display: grid;
        gap: 0.26rem;
        align-content: start;
        padding: 0.4rem 0 0.24rem;
        margin-bottom: 0.08rem;
    }

    body.cmd-palette-active .topbar-search-empty--idle {
        gap: 0.12rem;
        padding: 0 0.18rem 0.04rem;
        margin-bottom: 0.02rem;
        border: 0;
        background: transparent;
        box-shadow: var(--shadow-none, none);
    }

    body.cmd-palette-active .topbar-search-empty--idle .topbar-search-empty-title {
        font-size: 0.74rem;
    }

    body.cmd-palette-active .topbar-search-empty--idle .topbar-search-empty-copy {
        font-size: 0.68rem;
        line-height: 1.18;
    }

    body.cmd-palette-active .topbar-search-empty-title {
        font-size: 0.86rem;
        font-weight: 700;
        color: var(--text-primary);
    }

    body.cmd-palette-active .topbar-search-empty-copy {
        font-size: 0.72rem;
        line-height: 1.24;
        color: var(--text-secondary);
    }

    body.cmd-palette-active .topbar-search-suggestion {
        display: grid;
        gap: 0.12rem;
        justify-items: start;
        margin-top: 0.18rem;
        padding: 0.62rem 0.72rem;
        border: 1px solid var(--glass-brand-18);
        border-radius: var(--radius-md, 0.82rem);
        background: var(--glass-brand-08);
        color: var(--text-primary);
        text-align: left;
    }

    body.cmd-palette-active .topbar-search-suggestion__eyebrow {
        font-size: 0.64rem;
        font-weight: 700;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        color: var(--glass-brandLight-86);
    }

    body.cmd-palette-active .topbar-search-suggestion__label {
        font-size: 0.92rem;
        font-weight: 720;
        line-height: 1.16;
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-wrapper,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-wrapper {
        background: var(--bg-surface);
        border-color: var(--border-color);
        box-shadow: var(--shadow-sm);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-wrapper .form-input,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-wrapper .form-input {
        color: var(--text-primary);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-wrapper .form-input::placeholder,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-wrapper .form-input::placeholder {
        color: var(--gray-500);
    }

    :root[data-theme='light'] body.cmd-palette-active .mobile-search-close,
    :root:not([data-theme='dark']) body.cmd-palette-active .mobile-search-close,
    :root[data-theme='light'] body.cmd-palette-active .topbar-search-clear,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-clear,
    :root[data-theme='light'] body.cmd-palette-active .topbar-search-icon,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-icon {
        color: var(--gray-500);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-clear,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-clear {
        background: var(--glass-slate-10);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-result,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-result,
    :root[data-theme='light'] body.cmd-palette-active .topbar-search-empty,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-empty {
        background: transparent;
        border-color: var(--border-color);
        box-shadow: var(--shadow-none, none);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-empty--idle,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-empty--idle {
        background: transparent;
        border-color: transparent;
        box-shadow: var(--shadow-none, none);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-results,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-results {
        background: transparent;
        border-color: transparent;
        box-shadow: var(--shadow-none, none);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-group-heading,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-group-heading {
        color: var(--text-muted);
        background: var(--gray-100);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-group-heading__count,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-group-heading__count {
        background: var(--bg-secondary);
        color: var(--text-secondary);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-group-list,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-group-list {
        background: var(--bg-surface);
        border-color: var(--border-color);
        box-shadow: var(--shadow-xs);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search[data-state="idle"] .topbar-search-group--recent .topbar-search-group-list,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search[data-state="idle"] .topbar-search-group--recent .topbar-search-group-list,
    :root[data-theme='light'] body.cmd-palette-active .topbar-search[data-state="idle"] .topbar-search-group--quick .topbar-search-group-list,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search[data-state="idle"] .topbar-search-group--quick .topbar-search-group-list {
        background: var(--bg-surface);
        border-color: var(--border-color);
        box-shadow: var(--shadow-xs);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-result:hover,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-result:hover,
    :root[data-theme='light'] body.cmd-palette-active .topbar-search-result.active,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-result.active,
    :root[data-theme='light'] body.cmd-palette-active .topbar-search-result:focus-visible,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-result:focus-visible {
        background: var(--bg-surface-hover);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-result-meta,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-result-meta,
    :root[data-theme='light'] body.cmd-palette-active .topbar-search-empty-copy,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-empty-copy {
        color: var(--glass-ink-90);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-result-chevron,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-result-chevron {
        color: var(--glass-ink-88);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-suggestion,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-suggestion {
        background: var(--glass-brand-08);
        border-color: var(--glass-brand-12);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-suggestion__eyebrow,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-suggestion__eyebrow {
        color: var(--brand-600);
    }

    :root[data-theme='light'] body.cmd-palette-active .topbar-search-mark,
    :root:not([data-theme='dark']) body.cmd-palette-active .topbar-search-mark {
        background: var(--glass-brand-12);
    }

    :root[data-theme='dark'] body.cmd-palette-active .topbar-search-wrapper {
        background: var(--bg-secondary);
        border-color: var(--border-color);
        box-shadow: var(--shadow-sm);
    }

    :root[data-theme='dark'] body.cmd-palette-active .topbar-search-group-heading {
        background: var(--bg-primary);
    }

    :root[data-theme='dark'] body.cmd-palette-active .topbar-search-group-list {
        background: var(--bg-secondary);
        border-color: var(--border-color);
        box-shadow: var(--shadow-none, none);
    }

    :root[data-theme='dark'] body.cmd-palette-active .topbar-search-result,
    :root[data-theme='dark'] body.cmd-palette-active .topbar-search-empty {
        border-color: var(--glass-slate-10);
    }
}

/* ── Fuzzy/typo search indicators ─────────────────────────────────────── */

body.cmd-palette-active .topbar-search-group--fuzzy .topbar-search-group-heading__title::after {
    content: ' · typo';
}

body.cmd-palette-active .topbar-search-result--fuzzy {
    background: var(--glass-brand-045);
}

body.cmd-palette-active .topbar-search-result--fuzzy .topbar-search-result-label::after {
    content: ' sugerido';
}

/* ── Reduced motion ───────────────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
    body.cmd-palette-active .topbar-search,
    body.cmd-palette-active .topbar-search-group {
        animation: none;
    }
}

/* ── Bottom nav search item state ─────────────────────────────────────── */

body.cmd-palette-active .mobile-bottom-nav__item[data-nav-action='search']::before {
    opacity: 0;
}

body.cmd-palette-active .mobile-bottom-nav__item[data-nav-action='search'] {
    color: var(--brand-500);
    background: transparent;
}

/* ── Palette activation overlay (utilitarian; no glass blur) ─────────── */

@media (max-width: 768px) {
    :root[data-theme='dark'] body.cmd-palette-active::before {
        background: var(--bg-primary);
        opacity: 0.94;
    }
}
