.overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.35);
    -webkit-backdrop-filter: blur(4px);
    backdrop-filter: blur(4px);
    z-index: 150;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

.overlay.active {
    opacity: 1;
    pointer-events: auto;
}

/* --- Responsive Design --- */
@media screen and (max-width: 1024px) {
    .emi-layout {
        grid-template-columns: 1fr;
    }
}

@media screen and (max-width: 768px) {
    .mobile-header {
        display: flex;
    }

    .sidebar {
        transform: translateX(-100%);
        box-shadow: none;
        width: 280px;
        height: 100vh;
        height: 100dvh;
        max-height: 100dvh;
    }

    /* The drawer's tool list carries the same pill as everywhere else, drawn
       at rest, which is what a touch device needs: there is no hover to
       reveal it with. This block only adds the touch-scrolling properties. */
    .main-nav {
        -webkit-overflow-scrolling: touch;
        touch-action: pan-y pinch-zoom;
    }

    .sidebar-collapse-btn {
        display: none;
    }

    .sidebar.open {
        transform: translateX(0);
        box-shadow: 8px 0 30px rgba(0, 0, 0, 0.12);
    }

    /* Show mobile header inside sidebar */
    .sidebar-mobile-header {
        display: flex;
    }

    /* Hide the desktop logo-header on mobile */
    .sidebar .logo-header {
        display: none;
    }

    .sidebar-close-btn {
        display: flex;
    }

    /* Staggered nav link animation */
    .sidebar.open .main-nav .nav-link {
        animation: navLinkSlideIn 0.3s ease forwards;
        opacity: 0;
        transform: translateX(-12px);
    }

    .sidebar.open .main-nav li:nth-child(1) .nav-link { animation-delay: 0.06s; }
    .sidebar.open .main-nav li:nth-child(2) .nav-link { animation-delay: 0.09s; }
    .sidebar.open .main-nav li:nth-child(3) .nav-link { animation-delay: 0.12s; }
    .sidebar.open .main-nav li:nth-child(4) .nav-link { animation-delay: 0.15s; }
    .sidebar.open .main-nav li:nth-child(5) .nav-link { animation-delay: 0.18s; }
    .sidebar.open .main-nav li:nth-child(6) .nav-link { animation-delay: 0.21s; }
    .sidebar.open .main-nav li:nth-child(7) .nav-link { animation-delay: 0.24s; }
    .sidebar.open .main-nav li:nth-child(8) .nav-link { animation-delay: 0.27s; }
    .sidebar.open .main-nav li:nth-child(9) .nav-link { animation-delay: 0.30s; }
    .sidebar.open .main-nav li:nth-child(10) .nav-link { animation-delay: 0.33s; }
    .sidebar.open .main-nav li:nth-child(11) .nav-link { animation-delay: 0.36s; }
    .sidebar.open .main-nav li:nth-child(12) .nav-link { animation-delay: 0.39s; }
    .sidebar.open .main-nav li:nth-child(13) .nav-link { animation-delay: 0.42s; }
    .sidebar.open .main-nav li:nth-child(14) .nav-link { animation-delay: 0.45s; }

    @keyframes navLinkSlideIn {
        to {
            opacity: 1;
            transform: translateX(0);
        }
    }

    /* Results must appear instantly — the staggered fade would blank the list
       on every keystroke. */
    .sidebar.open .main-nav.is-filtering .nav-link {
        animation: none;
        opacity: 1;
        transform: none;
    }

    .nav-search {
        padding: var(--spacing-sm) var(--spacing-md);
        border-bottom: 1px solid var(--border-light);
    }

    .nav-search-input {
        padding-top: 11px;
        padding-bottom: 11px;
        /* 16px keeps iOS from zooming the page when the field takes focus. */
        font-size: 16px;
    }

    /* Improve nav links on mobile for touch */
    .main-nav .nav-link {
        /* 40px rows: under the 44px tap target the rest of the mobile chrome
           keeps, traded deliberately for a tool list that scrolls less. */
        padding: 9px 20px;
        font-size: 0.95rem;
        border-left-width: 3px;
        border-radius: 0 8px 8px 0;
        margin-right: 8px;
    }

    .main-nav .nav-link.active {
        border-left-width: 4px;
    }

    .main-content {
        margin-left: 0;
        padding: var(--spacing-md);
        padding-top: calc(60px + var(--spacing-lg));
    }

    .tool-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: var(--spacing-md);
    }

    .section-header h1 {
        font-size: 1.5rem;
    }

    .form-row {
        grid-template-columns: 1fr;
    }

    .action-buttons {
        flex-direction: column;
    }

    .button-group {
        flex-direction: column;
    }

    .btn {
        width: 100%;
        text-align: center;
    }

    /* Password modal responsive */
    .password-modal .modal-content {
        width: 90%;
        max-width: 400px;
        margin: 0 auto;
    }

    /* --- Touch targets ---
       A finger needs 44px. Where the control has room, it simply takes 44px
       on its own box, which also means the layout keeps them apart. */
    .mobile-nav-toggle,
    .mobile-search-toggle {
        width: 44px;
        height: 44px;
    }

    .sidebar-close-btn {
        width: 44px;
        height: 44px;
    }

    /* These two sit 2px apart in a flex row. Hanging a 44px tap area under
       each 32px disc made the two areas overlap by 10px, and the overlap
       resolved to the later element — so aiming at the right edge of the
       theme toggle signed you out. Real 44px boxes cannot overlap: the flex
       row lays them out side by side. The row has 279px for 94px of button. */
    .nav-session-theme,
    .nav-session-out {
        width: 44px;
        height: 44px;
    }

    /* Keep the clear control pinned inside the field. Its tap area takes the
       field's full 44px height and exactly its 32px of right padding. */
    .nav-search-clear {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
    }

    .nav-search-clear::after {
        content: "";
        position: absolute;
        top: 50%;
        left: 50%;
        width: 32px;
        height: 44px;
        transform: translate(-50%, -50%);
    }
}

@media screen and (max-width: 480px) {
    .main-content {
        padding: var(--spacing-sm);
        padding-top: calc(60px + var(--spacing-md));
        max-width: 100vw;
    }

    .section-header {
        margin-bottom: var(--spacing-lg);
    }

    .section-header h1 {
        font-size: 1.25rem;
    }

    /* A phone reads the dashboard as a list of tools rather than a grid of
       cards. Two columns left the text column 146px wide, which pushed the
       descriptions to five lines and broke titles mid-word; one column gives
       the name a full line and the icon a place of its own. */
    .tool-grid {
        grid-template-columns: 1fr;
        gap: var(--spacing-xs);
    }

    .tool-card {
        position: relative;
        padding: var(--spacing-sm) var(--spacing-sm) var(--spacing-sm) 60px;
    }

    .tool-card:active {
        background: var(--bg-hover);
        transform: none;
    }

    .tool-card h3 {
        font-size: 0.95rem;
        gap: 0;
        margin-bottom: 2px;
    }

    /* Out of the title's flow and into a chip down the left edge, matching the
       avatar on the sidebar's signed-in badge. */
    .tool-card h3::before {
        position: absolute;
        left: var(--spacing-sm);
        top: 50%;
        transform: translateY(-50%);
        width: 36px;
        height: 36px;
        flex-basis: 36px;
        border-radius: 50%;
        background: var(--bg-active);
        color: var(--primary-text);
        font-size: 20px;
    }

    /* Held to two lines — enough for every description we ship, and it keeps
       each row the same height whether the text runs one line or three. */
    .tool-card p {
        font-size: 0.78rem;
        line-height: 1.4;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        min-height: 2.8em;
    }

    .calculator-inputs,
    .calculator-results {
        padding: var(--spacing-md);
    }

    .form-section {
        padding: var(--spacing-md);
    }
}

/* --- Print Styles --- */
@media print {

    .sidebar,
    .mobile-header,
    .overlay {
        display: none !important;
    }

    .main-content {
        margin-left: 0;
        padding: 0;
    }
}
