/* ============================================= */

/* ============================================= */
.ai-fab {
    position: fixed;
    bottom: 90px;
    inset-inline-end: 16px;
    z-index: 900;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--c-primary);
    color: var(--c-on-primary);
    border: none;
    box-shadow: 0 6px 20px rgba(99, 102, 241, 0.45);
    font-size: 1.4rem;
    cursor: pointer;
    transition: transform var(--mo-fast) var(--ease-out), box-shadow var(--mo-fast) var(--ease-out);
}
@media (hover: hover) and (pointer: fine) {
    .ai-fab:hover { transform: scale(1.05); box-shadow: 0 8px 24px rgba(99,102,241,0.55); }
}
.ai-fab:active { transform: scale(0.92); }

@media (min-width: 769px) {
    .ai-fab { bottom: 24px; inset-inline-end: 24px; width: 60px; height: 60px; }
}
@media (max-width: 768px) {
    .ai-fab { display: none !important; }
    .scroll-top-btn { display: none !important; }
}

.ai-modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.55);
    z-index: 9998;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--mo-base) var(--ease-out);
}
.ai-modal-backdrop.open { opacity: 1; pointer-events: auto; }

.ai-modal {
    position: fixed;
    inset-inline-end: 0;
    inset-inline-start: 0;
    bottom: calc(66px + env(safe-area-inset-bottom, 0px));
    top: 8%;
    background: var(--c-surface);
    border-radius: 24px 24px 0 0;
    z-index: 9999;
    display: none;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 0 -10px 40px rgba(15,23,42,0.2);
}
.ai-modal.open {
    display: flex;
    animation: aiModalSlideUp .28s cubic-bezier(.22,.9,.37,1);
}
@keyframes aiModalSlideUp {
    from { transform: translateY(40vh); opacity: 0.4; }
    to { transform: translateY(0); opacity: 1; }
}

/* Backdrop should also stop above the mobile nav so nav stays visible */
.ai-modal-backdrop {
    bottom: calc(60px + env(safe-area-inset-bottom, 0px)) !important;
    height: auto !important;
}
.ai-modal-backdrop.open {
    animation: aiBackdropFadeIn .25s ease;
}
@keyframes aiBackdropFadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* Keep mobile nav ABOVE the AI modal so the slide-up animation appears FROM BEHIND it */

@media (min-width: 769px) {
    .ai-modal {
        top: auto;
        bottom: 24px;
        inset-inline-end: 24px;
        inset-inline-start: auto;
        width: 420px;
        height: 600px;
        max-height: 80vh;
        border-radius: 20px;
    }
}

.ai-modal-header {
    padding: 14px 18px;
    border-bottom: 1px solid var(--c-border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--c-bg);
    flex-shrink: 0;
}
.ai-modal-body {
    flex: 1;
    overflow-y: auto;
    padding: 12px 14px 20px;
    display: flex;
    flex-direction: column;
}
.ai-modal-body { padding: 0; overflow: hidden; }
.ai-modal-body #assistant-content { flex: 1; display: flex; flex-direction: column; min-height: 0; padding: 12px 14px 12px; overflow: hidden; }
.ai-modal-body #chat-messages { flex: 1; min-height: 0 !important; max-height: none !important; overflow-y: auto; }
.ai-modal-body #chat-form { flex-shrink: 0; padding: 8px 0 4px; background: var(--c-surface); position: sticky; bottom: 0; }
.ai-modal-body #assistant-content > div[style*="flex-wrap"] { flex-shrink: 0; }

/* DESKTOP vs MOBILE category layouts */
.cat-accordion { display: none; }
.cat-desktop-grid { display: block; }

@media (max-width: 768px) {
    .cat-desktop-grid { display: none !important; }
    .cat-accordion { display: flex; flex-direction: column; gap: 10px; width: 100%; box-sizing: border-box; }

    /* Override legacy grid rules — accordion needs full-width block flow */
    #ledger-category-row .cat-picker__grid,
    #category-cloud-container {
        display: block !important;
        grid-template-columns: none !important;
        max-height: none !important;
        overflow: visible !important;
        padding: 0 !important;
    }
    #ledger-category-row,
    #ledger-category-row .cat-picker {
        overflow: visible !important;
    }

    /* Clean mobile form layout: every field takes its own row */
    #ledger-fields-row {
        display: flex !important;
        flex-direction: column !important;
        gap: 10px !important;
        align-items: stretch !important;
    }
    #ledger-fields-row > div {
        flex: 1 1 100% !important;
        min-width: 0 !important;
        width: 100% !important;
        box-sizing: border-box !important;
    }
    #ledger-fields-row .ledger-input {
        width: 100% !important;
        box-sizing: border-box !important;
    }
    /* Hide the "selected category" big badge on mobile — the accordion already shows selection */
    #ledger-cat-display-wrapper { display: none !important; }

    /* Source wallet row cleanup */
    #source-wallet-row {
        flex-wrap: wrap !important;
        gap: 8px !important;
    }
    #source-wallet-row > span {
        flex: 0 0 auto !important;
    }
    .wallet-pills-desktop { display: none !important; }
    .wallet-select-mobile { display: block !important; }

    /* Accordion bodies — pills wrap horizontally (override legacy width:100%) */
    #ledger-category-row .cat-acc-body .cat-pill,
    #category-cloud-container .cat-acc-body .cat-pill {
        width: auto !important;
        max-width: none !important;
        min-width: 0 !important;
        white-space: nowrap !important;
        font-size: 0.7rem !important;
        padding: 6px 10px 6px 8px !important;
        height: auto !important;
        justify-content: flex-start !important;
        gap: 4px !important;
        border-radius: 8px !important;
    }

    #ledger-category-row .cat-acc-body .cat-pill .pill-text {
        position: static !important;
        width: auto !important;
        padding: 0 !important;
        overflow: visible !important;
        text-overflow: clip !important;
        font-size: 0.7rem !important;
    }

    /* Section */
    .cat-acc-section {
        background: var(--c-surface);
        border: 1px solid var(--c-border);
        border-radius: 14px;
        overflow: hidden;
    }
    .cat-acc-head {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 12px 14px;
        background: var(--c-bg);
        border: none;
        cursor: pointer;
        font-family: var(--font-ui);
        font-size: 0.95rem;
        font-weight: 700;
        color: var(--text-main);
        transition: background-color var(--mo-fast) var(--ease-out);
    }
    .cat-acc-head:active { background: var(--c-surface-2); }
    .cat-acc-title { display: flex; align-items: center; }
    .cat-acc-meta { display: flex; align-items: center; gap: 10px; color: var(--text-muted); font-weight: 600; font-size: 0.8rem; }
    .cat-acc-count {
        background: var(--c-border);
        color: var(--text-strong);
        padding: 2px 9px;
        border-radius: 999px;
        font-size: 0.75rem;
    }
    .cat-acc-chev {
        transition: transform var(--mo-base) var(--ease-out);
        font-size: 0.85rem;
    }
    .cat-acc-section.open .cat-acc-chev { transform: rotate(180deg); }

    .cat-acc-body {
        max-height: 0;
        overflow: hidden;
        /* transition dropped: it animated layout (width/height/top/left) set from JS */
        padding: 0 12px;
        display: flex;
        flex-wrap: wrap;
        gap: 6px;
    }
    .cat-acc-section.open .cat-acc-body {
        max-height: 170px;
        padding: 10px 12px 12px;
        overflow-y: auto;
    }

    /* Larger tap targets for amount input on mobile (high-specificity + !important to beat #ledger-fields-row input rule) */
    .amount-field-wrap { flex: 1 1 100% !important; }

    /* Ledger form padding (bottom small — submit is now in-flow, not fixed) */
    #ledger-form {
        padding: 14px !important;
    }

    /* Submit button: natural flow (no fixed) — sits after form content so page fits its content */
    #ledger-fields-row {
        position: relative;
    }
    #ledger-fields-row #ledger-submit-btn {
        position: static !important;
        width: 100% !important;
        padding: 14px !important;
        margin-top: 8px !important;
        font-size: 1rem !important;
        font-weight: 700 !important;
        border-radius: 14px !important;
        box-shadow: 0 4px 12px rgba(59, 130, 246, 0.28) !important;
        white-space: nowrap !important;
    }
    #ledger-fields-row #ledger-cancel-edit {
        position: static !important;
        width: 100% !important;
        margin-top: 6px !important;
    }
    /* Remove the huge bottom padding that was reserving space for the fixed button */
    #ledger-form {
        padding-bottom: 14px !important;
    }

    /* Mode tabs more compact */
    .ledger-tab-btn {
        font-size: 0.78rem !important;
        padding: 6px 10px !important;
    }
}

