/* ============================================= */
/* SUBSCRIPTION MODAL — compact mobile layout    */
/* ============================================= */
@media (max-width: 768px) {
    /* Padding tightening for the generic modal — kept compact, but no longer
       forces a 92vh bottom-sheet (that was making short forms like "إضافة
       استثمار" or "إضافة هدف" appear oddly tall and anchored to the bottom).
       Removed: align-self:flex-end, max-height:92vh, border-radius, full-width,
       and the global `.modal-overlay { align-items: flex-end !important }` —
       that last one was the real culprit overriding the new flex-start anchor. */
    #generic-modal .modal-header {
        padding: 14px 16px !important;
    }
    #generic-modal .modal-body {
        padding: 12px 16px 16px !important;
    }

    /* Compact subscription form */
    #sub-form { gap: 8px !important; }
    #sub-form input, #sub-form select {
        padding: 8px 10px !important;
        font-size: 0.88rem !important;
    }
    /* Shrink preset grid */
    .sub-presets-grid {
        grid-template-columns: repeat(4, 1fr) !important;
        max-height: 164px !important;
        gap: 5px !important;
        padding: 6px !important;
    }
    .sub-preset-btn {
        padding: 7px 3px !important;
        gap: 2px !important;
    }
    .sub-preset-icon { font-size: 1.3rem !important; }
    .sub-preset-name { font-size: 0.62rem !important; }

    /* Put freq + date on one row */
    #sub-form {
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
    }
    #sub-form > div:first-child,
    #sub-form > div:nth-child(2),
    #sub-form #sub-name,
    #sub-form #sub-amount,
    #sub-form #sub-wallet,
    #sub-form button[type="submit"] {
        grid-column: 1 / -1;
    }
    #sub-form #sub-freq { grid-column: 1; }
    #sub-form #sub-due { grid-column: 2; }

    /* Save button: prominent, sticky-ish at visible area */
    #sub-form button[type="submit"] {
        padding: 12px !important;
        border-radius: 12px !important;
        font-size: 0.95rem !important;
        font-weight: 700 !important;
        margin-top: 4px !important;
    }
}

/* ============================================= */
/* LEDGER — main page quick-action buttons       */
/* ============================================= */
.ledger-quick-actions { display: none; }

@media (max-width: 768px) {
    /* Main page: 3 quick-action cards that open the sheet */
    .ledger-quick-actions {
        display: grid !important;
        grid-template-columns: repeat(3, 1fr);
        gap: 8px;
        margin: 0 0 12px;
    }
    .quick-action-btn {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 6px;
        padding: 14px 6px;
        background: var(--c-surface);
        border: 1px solid var(--c-border);
        border-radius: 14px;
        cursor: pointer;
        font-family: var(--font-ui);
        transition: transform var(--mo-fast) var(--ease-out), opacity var(--mo-fast) var(--ease-out), background-color var(--mo-fast) var(--ease-out), color var(--mo-fast) var(--ease-out), border-color var(--mo-fast) var(--ease-out);
        box-shadow: 0 1px 2px rgba(15,23,42,0.03);
    }
    .quick-action-btn:active { transform: scale(0.96); }
    .quick-action-btn i { font-size: 1.3rem; }
    .quick-action-btn span { font-size: 0.78rem; font-weight: 700; color: var(--text-strong); }
    .quick-action-btn:nth-child(1) i { color: var(--danger-text); }
    .quick-action-btn:nth-child(2) i { color: var(--accent-text); }
    .quick-action-btn:nth-child(3) i { color: var(--success-text); }
    @media (hover: hover) and (pointer: fine) {
        .quick-action-btn:nth-child(1):hover { border-color: var(--c-danger-line); }
        .quick-action-btn:nth-child(2):hover { border-color: var(--c-primary-line); }
        .quick-action-btn:nth-child(3):hover { border-color: var(--c-success-line); }
    }

    /* In-sheet tab bar (top of the bottom sheet) */
    .ledger-sheet-tabs {
        display: flex;
        gap: 4px;
        padding: 4px;
        background: var(--c-surface-2);
        border-radius: 12px;
        margin-bottom: 14px;
        align-items: center;
        flex-shrink: 0;
    }
    .ledger-sheet-tabs .ledger-tab-btn {
        flex: 1;
        display: flex !important;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 3px;
        padding: 8px 4px !important;
        background: transparent;
        border: none;
        border-radius: 9px;
        cursor: pointer;
        font-family: var(--font-ui);
        color: var(--text-muted) !important;
        transition: transform var(--mo-base) var(--ease-out), opacity var(--mo-base) var(--ease-out), background-color var(--mo-base) var(--ease-out), color var(--mo-base) var(--ease-out), border-color var(--mo-base) var(--ease-out);
    }
    .ledger-sheet-tabs .ledger-tab-btn i { font-size: 1.05rem !important; display: block !important; margin: 0 !important; }
    .ledger-sheet-tabs .ledger-tab-btn span { font-size: 0.7rem; font-weight: 700; }
    .ledger-sheet-tabs #btn-mode-expense.active {
        background: var(--c-danger-soft) !important;
        color: var(--c-danger) !important;
        box-shadow: 0 1px 3px rgba(220,38,38,0.18), 0 0 0 1px var(--c-danger-line) !important;
    }
    .ledger-sheet-tabs #btn-mode-transfer.active {
        background: var(--money-move-soft) !important;
        color: var(--c-primary) !important;
        box-shadow: 0 1px 3px rgba(59,130,246,0.18), 0 0 0 1px var(--c-primary-line) !important;
    }
    .ledger-sheet-tabs #btn-mode-deposit.active {
        background: var(--c-success-soft) !important;
        color: var(--c-success) !important;
        box-shadow: 0 1px 3px rgba(16,185,129,0.18), 0 0 0 1px var(--c-success-line) !important;
    }
    /* Close button (X) */
    .ledger-sheet-close {
        flex: 0 0 38px;
        width: 38px;
        height: 38px;
        border-radius: 50%;
        background: var(--c-surface);
        border: 1px solid var(--c-border);
        color: var(--text-muted);
        font-size: 0.9rem;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-inline-start: 4px;
    }
    .ledger-sheet-close:active { transform: scale(0.9); }
}

/* ============================================= */
/* LEDGER AS FULL BOTTOM-SHEET OVERLAY           */
/* ============================================= */
.ledger-sheet-top-bar { display: none; }

@media (max-width: 768px) {
    /* When body has ledger-sheet-open: view-ledger becomes a fixed auto-height overlay above the bottom nav */
    body.ledger-sheet-open #view-ledger.active {
        position: fixed !important;
        inset-inline-end: 0 !important;
        inset-inline-start: 0 !important;
        top: auto !important;
        /* Sits right on the bar (62px): no strip of the page shows between them. */
        bottom: calc(62px + env(safe-area-inset-bottom, 0px)) !important;
        max-height: calc(100vh - 62px - 80px - env(safe-area-inset-bottom, 0px)) !important;
        max-height: calc(100dvh - 62px - 80px - env(safe-area-inset-bottom, 0px)) !important;
        height: auto !important;
        z-index: 1100 !important;
        background: var(--c-surface) !important;
        border-radius: var(--radius-lg) var(--radius-lg) 0 0 !important;
        box-shadow: var(--el-3) !important;
        overflow-y: auto !important;
        /* No top padding: the sticky top bar starts at the edge, so nothing scrolls into a gap above it. */
        padding: 0 14px 14px !important;
        display: block !important;
        animation: ledgerSheetSlideUp var(--mo-slow) var(--ease-out);
    }
    /* Grab handle — same as the windows (33-sheets.css) */
    body.ledger-sheet-open #view-ledger.active::before {
        content: '';
        position: absolute;
        top: 7px; left: 50%;  /* physical on purpose: centred with translateX(-50%) */
        width: 40px; height: 4px;
        transform: translateX(-50%);
        border-radius: var(--radius-pill);
        background: var(--c-border-strong);
        pointer-events: none;
        z-index: 6;       }
    /* Keyboard up: the bar is hidden, the sheet goes down to the keyboard. */
    body.kb-open.ledger-sheet-open #view-ledger.active { bottom: 0 !important; max-height: calc(100dvh - 40px) !important; }
    body.kb-open.ledger-sheet-open::before { bottom: 0; }
    @keyframes ledgerSheetSlideUp {
        from { transform: translateY(40vh); opacity: 0.4; }
        to { transform: translateY(0); opacity: 1; }
    }

    /* Inside the ledger sheet: kill the nested form-sheet behavior, make form inline */
    body.ledger-sheet-open #ledger-form {
        position: static !important;
        border-radius: 0 !important;
        box-shadow: none !important;
        max-height: none !important;
        padding: 0 !important;
        background: transparent !important;
        transform: none !important;
        margin: 0 !important;
    }
    body.ledger-sheet-open #ledger-sheet-backdrop { display: none !important; }
    /* Submit button in-flow inside the sheet */
    body.ledger-sheet-open #ledger-submit-btn,
    body.ledger-sheet-open #ledger-fields-row #ledger-submit-btn {
        position: sticky !important;
        width: 100% !important;
        margin-top: 10px !important;
        bottom: 0 !important;
        inset-inline-end: auto !important;
        inset-inline-start: auto !important;
        min-height: var(--tap);
        background: var(--c-primary) !important;
        color: var(--c-on-primary) !important;
        border-color: transparent !important;
    }
    @media (hover: hover) and (pointer: fine) {
        body.ledger-sheet-open #ledger-submit-btn:hover { background: var(--c-primary-hover) !important; }
    }
    body.ledger-sheet-open #ledger-fields-row #ledger-cancel-edit {
        position: static !important;
        width: 100% !important;
        margin-top: 6px !important;
    }
    /* Hide the drag handle pseudo since the sheet already has a top bar */
    body.ledger-sheet-open #ledger-form::before { display: none !important; }
    /* Backdrop when ledger sheet is open — stops at top of mobile nav so nav stays visible */
    body.ledger-sheet-open::before {
        content: '';
        position: fixed;
        inset-inline-end: 0;
        inset-inline-start: 0;
        top: 0;
        bottom: calc(62px + env(safe-area-inset-bottom, 0px));
        background: var(--c-overlay);
        z-index: 1099;
        animation: ledgerBackdropFadeIn var(--mo-base) var(--ease-out);
    }
    /* Ensure nav stays above the sheet in ALL cases */
    @keyframes ledgerBackdropFadeIn {
        from { opacity: 0; }
        to { opacity: 1; }
    }
    /* Top bar with close X + compact tab row */
    body.ledger-sheet-open .ledger-sheet-top-bar {
        display: flex !important;
        align-items: center;
        gap: 8px;
        margin-bottom: 12px;
        position: sticky;
        top: 0;
        background: var(--c-surface);
        padding: 18px 0 10px;   /* room for the grab handle; solid, so scrolled fields hide under it */
        z-index: 5;
    }
    .ledger-top-tabs {
        flex: 1;
        display: flex;
        gap: 4px;
        background: var(--c-surface-2);
        border-radius: var(--radius-md);
        padding: 3px;
    }
    .ledger-top-tabs .ltab {
        flex: 1;
        min-height: 40px;           /* + 3px tray padding on each side = 46px tap row */
        padding: 8px 4px;
        border: none;
        border-radius: var(--radius-sm);
        background: transparent;
        color: var(--c-text-muted);
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: background-color var(--mo-fast) var(--ease-out), transform var(--mo-fast) var(--ease-out);
    }
    .ledger-top-tabs .ltab:active { transform: scale(0.97); }
    .ledger-top-tabs .ltab i { font-size: 1rem; }
    .ledger-top-tabs .ltab.active { background: var(--c-surface); box-shadow: var(--el-1); }
    .ledger-top-tabs #btn-mode-ai.active { color: var(--c-primary); }
    .ledger-top-tabs .ltab-expense.active { color: var(--danger-text); }
    .ledger-top-tabs .ltab-transfer.active { color: var(--accent-text); }
    .ledger-top-tabs .ltab-deposit.active { color: var(--success-text); }

    /* Hide redundant elements inside the sheet (quick-actions not needed when tabs are visible) */
    body.ledger-sheet-open .ledger-quick-actions { display: none !important; }
}

/* In-sheet ledger-sheet-tabs (the previously-added inner ones) — hide since we use top tabs now */
@media (max-width: 768px) {
    .ledger-sheet-tabs { display: none !important; }
}

/* ============================================= */
/* TAB-SWITCH ANIMATION inside ledger sheet       */
/* ============================================= */
@media (max-width: 768px) {
    .tab-switch-in { animation: tabSwitchIn .22s cubic-bezier(.22,.9,.37,1); }
    @keyframes tabSwitchIn {
        from { transform: translateY(12px); opacity: 0.3; }
        to { transform: translateY(0); opacity: 1; }
    }
}

