/* ============================================
   MOBILE UX OPTIMIZATION — MATHMATIX AI
   Comprehensive mobile-first responsive design
   ============================================ */

/* --- 1. GLOBAL TOUCH & INTERACTION FIXES --- */

button,
.btn,
.tab-btn,
a {
    -webkit-tap-highlight-color: rgba(18, 179, 179, 0.15);
    touch-action: manipulation;
}

.input-toolbar,
.header-nav {
    pointer-events: auto;
}

.input-toolbar button,
.header-nav button {
    pointer-events: auto;
}

/* Fix for iOS Safari button tap delay */
@media (hover: none) {
    button { cursor: default; }
}

/* Note: touch-action: manipulation is applied only to interactive elements
   (buttons, links, tabs) above — NOT to * wildcard, which blocks scrolling
   on nested containers and causes "stuck scroll" on mobile. */

/* Ensure proper safe area insets for notched devices */
@supports (padding: env(safe-area-inset-bottom)) {
    body {
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }
}

/* --- 1b. TOUR ELEMENTS — keep them out of the way on mobile --- */

@media (max-width: 768px) {
    /* The persistent tour help button (position:fixed via inline JS) overlaps
       the chat input area on mobile.  Hide it so it doesn't block interactive
       elements.  The initial tour prompt still appears for new users. */
    .tour-help-btn {
        display: none !important;
    }

    /* The one-time tour prompt popup is positioned fixed at bottom-right via
       inline styles.  Push it above the bottom nav so it doesn't overlap. */
    #tour-prompt > div {
        bottom: 72px !important;
    }
}

/* --- 2. MOBILE HEADER (<=768px) — Hidden ---
   Header is completely hidden on mobile.
   Branding via watermark, navigation via bottom bar. */

@media (max-width: 768px) {
    /* Hide desktop sidebar toggle */
    #sidebar-toggle {
        display: none !important;
    }

    /* Header hidden on mobile — watermark handles branding,
       bottom nav handles navigation. */
    .landing-header {
        display: none !important;
    }

    /* All header children hidden via parent display:none */

    /* General hide helpers */
    .mobile-hide,
    .desktop-only,
    #open-resources-modal-btn,
    #restart-audio-btn,
    #pause-audio-btn,
    #open-settings-modal-btn {
        display: none !important;
    }
}

/* --- 3. MOBILE CHAT LAYOUT --- */

@media (max-width: 768px) {
    /* Main layout adjustments — height & flex set in Section 16 */
    #app-layout-wrapper {
        position: relative;
        z-index: 1;
        margin: 0;
    }

    #app-layout-wrapper.sidebar-layout {
        margin-left: 0 !important;
        max-width: 100vw !important;
    }

    /* Chat container with proper header offset */
    #chat-container {
        position: relative;
        z-index: 1;
        margin: 0 !important;
        padding: 0 !important;
        border-radius: 0;
        box-shadow: none;
        border: none;
    }

    /* Chat messages — clean, airy background */
    #chat-messages-container {
        padding: 12px 14px;
        padding-bottom: 16px;
        gap: 4px;
        position: relative;
        z-index: 1;
        border-radius: 0;
        border: none;
        border-top: none;
        background: var(--clr-bg-subtle, #f8fafb);
        /* Base font that scales with pinch-to-zoom */
        font-size: calc(16px * var(--chat-font-scale, 1));
    }

    /* Remove logo watermark on mobile — it creates a positioned element that
       can interfere with scroll and touch, and wastes rendering resources */
    #chat-messages-container::before {
        display: none;
    }

    /* Message bubbles: touch-friendly, readable, modern */
    .message {
        max-width: 88%;
        padding: 10px 14px;
        margin-bottom: 1px;
        font-size: inherit;
        line-height: 1.55;
        border-radius: 18px;
        word-break: break-word;
    }

    .message-container {
        margin-bottom: 6px;
    }

    .message.user,
    .message.user-message {
        margin-left: auto;
        margin-right: 4px;
        border-bottom-right-radius: 6px;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
    }

    .message.ai,
    .message.ai-message {
        margin-right: auto;
        margin-left: 4px;
        border-bottom-left-radius: 6px;
        background: var(--clr-bg-light, #ffffff);
        border: 1px solid var(--clr-border-light, #f1f5f9);
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
    }

    /* Compact avatars */
    .message-avatar {
        width: 28px;
        height: 28px;
    }

    .message-avatar img {
        width: 28px;
        height: 28px;
        border-radius: 50%;
        object-fit: cover;
    }

    /* Timestamps */
    .message-timestamp {
        font-size: 0.65rem;
        color: var(--clr-text-dim, #999);
        margin-top: 3px;
        opacity: 0.8;
    }

    /* Thinking indicator */
    #thinking-indicator {
        padding: 8px 14px;
        font-size: 0.85rem;
        color: var(--clr-text-dim, #888);
    }
}

/* --- 4. MOBILE INPUT AREA --- */

@media (max-width: 768px) {
    /* Input container — sits above the bottom nav; no safe-area padding
       needed here because the bottom nav bar handles that. */
    #input-container {
        position: relative;
        z-index: 100;
        padding: 8px 12px;
        background: var(--clr-bg-light, #fff);
        border-top: 1px solid rgba(0, 0, 0, 0.06);
        box-shadow: 0 -1px 6px rgba(0, 0, 0, 0.03);
    }

    /* User input: prevent iOS zoom (16px min), comfortable padding */
    #user-input {
        font-size: 16px;
        padding: 10px 16px;
        min-height: 44px;
        border-radius: 22px;
        background: var(--clr-bg-subtle, #f8fafb);
        border: 1.5px solid var(--clr-border-light, #e8ecf0);
        line-height: 1.5;
        resize: none;
        transition: border-color 200ms ease, box-shadow 200ms ease, background 200ms ease;
    }

    #user-input:focus {
        background: var(--clr-bg-light, #fff);
        border-color: var(--clr-primary-teal, #12B3B3);
        box-shadow: 0 0 0 3px rgba(18, 179, 179, 0.08);
    }

    #user-input::placeholder {
        color: var(--clr-text-dim, #999);
        opacity: 0.7;
    }

    /* Send button: iMessage-style circle */
    #send-button {
        width: 36px;
        height: 36px;
        min-width: 36px;
        min-height: 36px;
        padding: 0;
        border-radius: 50%;
        margin-top: 0;
        font-size: 15px;
    }

    #send-button:active {
        transform: scale(0.9);
    }

    /* Scrollable toolbar with fade edges */
    .input-toolbar {
        display: flex;
        gap: 6px;
        padding: 4px 4px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        position: relative;
        scrollbar-width: none;
        border: none;
        background: transparent;
    }

    .input-toolbar::-webkit-scrollbar { display: none; }

    .input-toolbar button {
        min-width: 40px;
        min-height: 40px;
        flex-shrink: 0;
        padding: 8px;
        font-size: 1rem;
        border-radius: 12px;
        background: var(--clr-bg-subtle, #f8fafb);
        border: 1px solid var(--clr-border-light, #f1f5f9);
        color: var(--clr-text-dim, #5B6876);
        transition: color 150ms ease, background 150ms ease, transform 100ms ease;
    }

    .input-toolbar button:active {
        background: rgba(var(--clr-primary-teal-rgb), 0.1);
        border-color: var(--clr-primary-teal);
        color: var(--clr-primary-teal);
        transform: scale(0.92);
    }

    .input-toolbar button i {
        font-size: 1.05rem;
    }
}

/* --- 5. STREAMLINED MOBILE TOOLBAR --- */

.toolbar-more { display: none; }

@media (max-width: 767px) {
    .toolbar-secondary { display: none !important; }
    .toolbar-more { display: flex; }

    /* Bottom Sheet More Tools Menu */
    .more-tools-menu {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: var(--clr-bg-light, #fff);
        border-top-left-radius: 20px;
        border-top-right-radius: 20px;
        box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.12);
        z-index: 2100;
        padding: 12px 16px;
        padding-bottom: max(env(safe-area-inset-bottom, 16px), 16px);
        transform: translateY(100%);
        transition: transform 300ms cubic-bezier(0.22, 1, 0.36, 1);
    }

    .more-tools-menu.active {
        transform: translateY(0);
    }

    /* Drag handle */
    .more-tools-menu::before {
        content: '';
        display: block;
        width: 36px;
        height: 4px;
        background: #d1d5db;
        border-radius: 2px;
        margin: 0 auto 12px;
    }

    .more-tools-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 16px;
        padding-bottom: 10px;
        border-bottom: 1px solid var(--clr-border, #e2e8f0);
    }

    .more-tools-header span {
        font-size: 1.1rem;
        font-weight: 600;
        color: var(--clr-text, #18202B);
    }

    .close-more-tools {
        background: var(--clr-bg-subtle, #f8fafb);
        border: none;
        font-size: 1.4rem;
        color: var(--clr-text-dim, #5B6876);
        width: 36px;
        height: 36px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;
        box-shadow: none;
        padding: 0;
    }

    .close-more-tools:active {
        background: var(--clr-border, #e2e8f0);
    }

    .more-tools-grid {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }

    .more-tool-separator {
        grid-column: 1 / -1;
        height: 1px;
        background: var(--clr-border-light, #e2e8f0);
        margin: 2px 0;
    }

    .more-tool-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: 16px 12px;
        background: var(--clr-bg-subtle, #f8fafb);
        border: 1.5px solid var(--clr-border-light, #f1f5f9);
        border-radius: 14px;
        min-height: 88px;
        gap: 8px;
        transition: all 150ms ease;
    }

    .more-tool-item:active {
        transform: scale(0.95);
        background: rgba(var(--clr-primary-teal-rgb), 0.06);
        border-color: var(--clr-primary-teal);
    }

    .more-tool-item i {
        font-size: 1.6rem;
        color: var(--clr-primary-teal);
    }

    .more-tool-item span {
        font-size: 0.85rem;
        font-weight: 500;
        color: var(--clr-text);
        text-align: center;
    }

    /* Overlay */
    .more-tools-overlay {
        position: fixed;
        top: 0; left: 0; right: 0; bottom: 0;
        background: rgba(0, 0, 0, 0.3);
        backdrop-filter: blur(2px);
        -webkit-backdrop-filter: blur(2px);
        z-index: 2099;
        opacity: 0;
        visibility: hidden;
        transition: opacity 300ms ease, visibility 300ms ease;
    }

    .more-tools-overlay.active {
        opacity: 1;
        visibility: visible;
    }
}

@media (min-width: 768px) {
    /* Hide secondary items — accessible through the dropdown menu */
    .toolbar-secondary,
    .landing-nav > #open-settings-modal-btn,
    .landing-nav > .logout-button { display: none !important; }

    /* Show the dropdown trigger on desktop */
    .toolbar-more { display: flex !important; }

    /* Desktop dropdown (replaces mobile bottom-sheet) */
    .more-tools-menu {
        position: fixed;
        top: var(--header-height, 72px);
        right: var(--page-horizontal-padding, 20px);
        left: auto;
        bottom: auto;
        width: 210px;
        background: var(--clr-bg-light, #fff);
        border-radius: 10px;
        box-shadow: 0 4px 24px rgba(0, 0, 0, 0.12);
        border: 1px solid var(--clr-border-light, #e2e8f0);
        z-index: 2100;
        padding: 6px 0;
        transform: translateY(-8px);
        opacity: 0;
        pointer-events: none;
        transition: opacity 150ms ease, transform 150ms ease;
    }

    .more-tools-menu.active {
        opacity: 1;
        transform: translateY(0);
        pointer-events: auto;
    }

    /* Hide bottom-sheet chrome on desktop */
    .more-tools-menu::before,
    .more-tools-header { display: none; }

    /* List layout instead of grid */
    .more-tools-grid {
        display: flex;
        flex-direction: column;
        gap: 0;
    }

    .more-tool-item {
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 10px;
        padding: 9px 16px;
        min-height: auto;
        border-radius: 0;
        border: none;
        background: transparent;
    }

    .more-tool-item:hover {
        background: var(--clr-bg-subtle, #f8fafb);
    }

    .more-tool-item:active {
        transform: none;
        background: var(--clr-border-light, #e2e8f0);
    }

    .more-tool-item i {
        font-size: 0.95rem;
        width: 20px;
        text-align: center;
    }

    .more-tool-item span {
        font-size: 0.88rem;
        font-weight: 500;
    }

    /* Separator between groups */
    .more-tool-separator {
        height: 1px;
        background: var(--clr-border-light, #e2e8f0);
        margin: 4px 12px;
    }

    /* Desktop overlay — invisible click-catcher */
    .more-tools-overlay {
        position: fixed;
        top: 0; left: 0; right: 0; bottom: 0;
        background: transparent;
        z-index: 2099;
        display: none;
    }

    .more-tools-overlay.active {
        display: block;
    }
}

/* --- 6. PINCH-TO-ZOOM CHAT FONT SIZE --- */

#chat-messages-container {
    font-size: calc(1em * var(--chat-font-scale, 1));
}

#font-scale-indicator { /* Styled via JS */ }

.pinch-zoom-hint {
    position: fixed;
    bottom: 72px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(18, 179, 179, 0.92);
    color: white;
    padding: 10px 18px;
    border-radius: 22px;
    font-size: 0.88rem;
    text-align: center;
    z-index: 1001;
    box-shadow: 0 4px 16px rgba(18, 179, 179, 0.3);
    animation: fadeInOut 4s ease-in-out;
    pointer-events: none;
}

@keyframes fadeInOut {
    0% { opacity: 0; transform: translateX(-50%) translateY(10px); }
    10% { opacity: 1; transform: translateX(-50%) translateY(0); }
    90% { opacity: 1; transform: translateX(-50%) translateY(0); }
    100% { opacity: 0; transform: translateX(-50%) translateY(10px); }
}

/* --- 6b. BOTTOM NAVIGATION BAR ---
   Five-item glass-morphism nav: Sessions · Tools · Chat · Theme · Profile
   Created dynamically by mobile-chat-nav.js; styled entirely here. */

.mobile-chat-nav {
    display: none;
}

@media (max-width: 768px) {
    body.has-chat-nav .mobile-chat-nav,
    body.landing-page-body .mobile-chat-nav {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 56px;
        box-sizing: content-box;                   /* so padding-bottom adds to 56px */
        padding-bottom: env(safe-area-inset-bottom, 0);
        background: rgba(255, 255, 255, 0.88);
        backdrop-filter: blur(24px) saturate(180%);
        -webkit-backdrop-filter: blur(24px) saturate(180%);
        border-top: 1px solid rgba(0, 0, 0, 0.06);
        box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.04);
        z-index: 1800;
        align-items: center;
        justify-content: space-around;
    }

    .chat-nav-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 3px;
        flex: 1;
        padding: 6px 0;
        background: none;
        border: none;
        color: var(--clr-text-dim, #8896a6);
        transition: color 200ms ease, transform 150ms ease;
        -webkit-tap-highlight-color: transparent;
        cursor: pointer;
        position: relative;
    }

    .chat-nav-item i {
        font-size: 1.15rem;
        transition: transform 250ms cubic-bezier(0.34, 1.56, 0.64, 1);
    }

    .chat-nav-item span {
        font-size: 0.58rem;
        font-weight: 700;
        letter-spacing: 0.04em;
        text-transform: uppercase;
    }

    /* Active state — brand teal with gentle scale */
    .chat-nav-item.active {
        color: var(--clr-primary-teal, #12B3B3);
    }

    .chat-nav-item.active i {
        transform: scale(1.12);
    }

    /* Active indicator dot */
    .chat-nav-item.active::after {
        content: '';
        position: absolute;
        bottom: 2px;
        left: 50%;
        transform: translateX(-50%);
        width: 4px;
        height: 4px;
        background: var(--clr-primary-teal, #12B3B3);
        border-radius: 50%;
    }

    /* Tap feedback */
    .chat-nav-item:active {
        transform: scale(0.88);
        color: var(--clr-primary-teal, #12B3B3);
    }

    /* Dark mode glass */
    [data-theme="dark"] .mobile-chat-nav {
        background: rgba(18, 22, 30, 0.88);
        border-top-color: rgba(255, 255, 255, 0.06);
        box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.2);
    }
}

/* --- 6d. MOBILE MENU BOTTOM SHEET ---
   Slide-up sheet with Settings, Tools, Theme toggle, etc.
   Built dynamically by mobile-chat-nav.js.
   -------------------------------------------------------- */

.mobile-menu-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0, 0, 0, 0.35);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    z-index: 2049;
    opacity: 0;
    visibility: hidden;
    transition: opacity 300ms ease, visibility 300ms ease;
}

.mobile-menu-overlay.active {
    opacity: 1;
    visibility: visible;
}

.mobile-menu-sheet {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--clr-bg-light, #fff);
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.14);
    z-index: 2050;
    display: none;
    transform: translateY(100%);
    transition: transform 300ms cubic-bezier(0.22, 1, 0.36, 1);
    padding-bottom: max(env(safe-area-inset-bottom, 16px), 16px);
}

.mobile-menu-sheet.active {
    transform: translateY(0);
}

/* Drag handle */
.menu-sheet-handle {
    width: 36px;
    height: 4px;
    background: #d1d5db;
    border-radius: 2px;
    margin: 10px auto 4px;
}

.menu-sheet-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 20px 14px;
    border-bottom: 1px solid var(--clr-border-light, #f1f5f9);
}

.menu-sheet-header span {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--clr-text, #18202B);
}

.menu-sheet-close {
    background: var(--clr-bg-subtle, #f8fafb);
    border: none;
    font-size: 1.2rem;
    color: var(--clr-text-dim, #5B6876);
    width: 34px;
    height: 34px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    padding: 0;
    cursor: pointer;
}

.menu-sheet-close:active {
    background: var(--clr-border, #e2e8f0);
}

/* Menu items list */
.menu-sheet-list {
    padding: 8px 12px;
}

.menu-sheet-item {
    display: flex;
    align-items: center;
    gap: 14px;
    width: 100%;
    padding: 14px 16px;
    background: none;
    border: none;
    border-radius: 12px;
    font-size: 0.95rem;
    font-weight: 500;
    color: var(--clr-text, #18202B);
    cursor: pointer;
    transition: background 150ms ease;
    text-align: left;
}

.menu-sheet-item:active {
    background: rgba(18, 179, 179, 0.06);
}

.menu-sheet-item i:first-child {
    width: 22px;
    text-align: center;
    font-size: 1.1rem;
    color: var(--clr-primary-teal, #12B3B3);
    flex-shrink: 0;
}

.menu-sheet-arrow {
    margin-left: auto;
    font-size: 0.7rem !important;
    color: var(--clr-text-dim, #aaa) !important;
    opacity: 0.5;
}

/* Settings row — make it visually prominent */
.menu-sheet-item[data-menu-action="open-settings"] {
    font-weight: 600;
}

/* Toggle row for theme */
.menu-sheet-toggle-row {
    justify-content: space-between;
}

.menu-sheet-item-left {
    display: flex;
    align-items: center;
    gap: 14px;
}

.menu-sheet-item-left i {
    width: 22px;
    text-align: center;
    font-size: 1.1rem;
    color: var(--clr-primary-teal, #12B3B3);
}

/* Toggle switch */
.menu-toggle-switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
    flex-shrink: 0;
}

.menu-toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.menu-toggle-slider {
    position: absolute;
    cursor: pointer;
    top: 0; left: 0; right: 0; bottom: 0;
    background: #ccd5de;
    border-radius: 24px;
    transition: background 200ms ease;
}

.menu-toggle-slider::before {
    content: '';
    position: absolute;
    height: 18px;
    width: 18px;
    left: 3px;
    bottom: 3px;
    background: white;
    border-radius: 50%;
    transition: transform 200ms ease;
    box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}

.menu-toggle-switch input:checked + .menu-toggle-slider {
    background: var(--clr-primary-teal, #12B3B3);
}

.menu-toggle-switch input:checked + .menu-toggle-slider::before {
    transform: translateX(20px);
}

/* Dark mode menu sheet */
[data-theme="dark"] .mobile-menu-sheet {
    background: var(--clr-bg-dark, #1a1d2e);
    box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.4);
}

[data-theme="dark"] .menu-sheet-handle {
    background: #444;
}

[data-theme="dark"] .menu-sheet-close {
    background: rgba(255, 255, 255, 0.08);
    color: #aaa;
}

[data-theme="dark"] .menu-sheet-item:active {
    background: rgba(18, 179, 179, 0.1);
}

/* --- 6c. CHAT WATERMARK ---
   Semi-transparent brand mark behind the message area.
   pointer-events: none keeps it completely touch-safe. */

#chat-watermark {
    display: none;
}

@media (max-width: 768px) {
    body.landing-page-body #chat-watermark {
        display: flex;
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 0;
        pointer-events: none;
        opacity: 0.07;
        width: 180px;
        height: 180px;
        align-items: center;
        justify-content: center;
    }

    #chat-watermark img {
        width: 100%;
        height: 100%;
        object-fit: contain;
        filter: grayscale(100%);
        user-select: none;
        -webkit-user-drag: none;
    }

    /* Dark mode: invert the watermark for subtle white-on-dark effect */
    [data-theme="dark"] #chat-watermark {
        opacity: 0.08;
        filter: invert(1);
    }
}

/* --- 7. MOBILE DRAWERS --- */

.mobile-drawer {
    position: fixed;
    top: 0;
    bottom: 0;
    width: 85%;
    max-width: 340px;
    background: #ffffff;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.1);
    z-index: 2000;
    transform: translateX(-100%);
    transition: transform 300ms cubic-bezier(0.22, 1, 0.36, 1);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    display: flex;
    flex-direction: column;
}

.left-drawer {
    left: 0;
    transform: translateX(-100%);
}
.left-drawer.open {
    transform: translateX(0);
}

.right-drawer {
    right: 0;
    left: auto;
    transform: translateX(100%);
}
.right-drawer.open {
    transform: translateX(0);
}

/* Drawer Header */
.drawer-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 18px 20px;
    background: linear-gradient(135deg, var(--clr-primary-teal, #12B3B3) 0%, #0da3a3 100%);
    color: white;
    flex-shrink: 0;
    padding-top: max(env(safe-area-inset-top, 18px), 18px);
}

.drawer-header h3 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 700;
    color: white;
    letter-spacing: -0.01em;
}

.drawer-close-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    font-size: 1.3rem;
    color: white;
    padding: 0;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: background 150ms ease;
    box-shadow: none;
}

.drawer-close-btn:hover,
.drawer-close-btn:active {
    background: rgba(255, 255, 255, 0.35);
    transform: none;
}

/* Drawer Content */
.drawer-content {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 0;
}

.drawer-section {
    padding: 16px 20px;
    border-bottom: 1px solid var(--clr-border-light, #f1f5f9);
}

.drawer-section:last-child {
    border-bottom: none;
}

.drawer-section h4 {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--clr-text, #333);
    margin: 0 0 12px 0;
    display: flex;
    align-items: center;
    gap: 8px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    font-size: 0.8rem;
}

.drawer-section h4 i {
    color: var(--clr-primary-teal);
    font-size: 0.9rem;
}

/* Progress Section in Drawer */
.drawer-progress-section {
    background: linear-gradient(135deg, var(--clr-primary-teal, #12B3B3) 0%, #0E9494 100%);
    color: white;
    padding: 0;
}

.drawer-progress-card {
    padding: 20px;
}

.drawer-progress-level {
    font-size: 1.8rem;
    font-weight: 700;
    margin-bottom: 10px;
    text-align: center;
}

.drawer-progress-bar {
    width: 100%;
    height: 10px;
    background: rgba(255, 255, 255, 0.25);
    border-radius: 5px;
    overflow: hidden;
    margin: 10px 0;
}

.drawer-progress-fill {
    height: 100%;
    background: white;
    border-radius: 5px;
    transition: width 400ms ease;
}

.drawer-progress-xp {
    font-size: 0.95rem;
    opacity: 0.9;
    text-align: center;
    font-weight: 500;
}

/* Stats Grid */
.drawer-stats-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
}

.drawer-stat-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 12px;
    background: var(--clr-bg-subtle, #f8fafb);
    border-radius: 10px;
    border: 1px solid var(--clr-border-light, #f1f5f9);
}

.drawer-stat-label {
    font-size: 0.88rem;
    color: var(--clr-text-dim, #666);
    font-weight: 500;
}

.drawer-stat-value {
    font-size: 1rem;
    font-weight: 700;
    color: var(--clr-primary-teal);
}

/* Quick Stats Row (streak, total XP, solved) */
.drawer-quick-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    text-align: center;
}

.drawer-quick-stat {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 10px 4px;
    background: var(--clr-bg-subtle, #f8fafb);
    border-radius: 12px;
    border: 1px solid var(--clr-border-light, #f1f5f9);
}

.drawer-quick-stat i {
    font-size: 1.2rem;
    margin-bottom: 2px;
}

.drawer-quick-stat-value {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--clr-text, #18202B);
}

.drawer-quick-stat-label {
    font-size: 0.68rem;
    font-weight: 600;
    color: var(--clr-text-dim, #8896a6);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

/* Drawer Action Buttons (Settings, Change Tutor) */
.drawer-action-btn {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 12px 14px;
    background: var(--clr-bg-subtle, #f8fafb);
    border: 1px solid var(--clr-border-light, #f1f5f9);
    border-radius: 10px;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--clr-text, #18202B);
    cursor: pointer;
    transition: background 150ms ease;
    text-align: left;
    margin-bottom: 8px;
}

.drawer-action-btn:last-child {
    margin-bottom: 0;
}

.drawer-action-btn:active {
    background: rgba(18, 179, 179, 0.06);
}

.drawer-action-btn i:first-child {
    width: 20px;
    text-align: center;
    color: var(--clr-primary-teal, #12B3B3);
    font-size: 1rem;
}

.drawer-action-arrow {
    margin-left: auto;
    font-size: 0.7rem !important;
    color: var(--clr-text-dim, #aaa) !important;
    opacity: 0.5;
}

/* Leaderboard Table */
.leaderboard-table-mobile {
    width: 100%;
    font-size: 0.88rem;
    border-collapse: collapse;
}

.leaderboard-table-mobile th {
    font-weight: 600;
    color: var(--clr-text-dim, #666);
    padding: 8px 6px;
    text-align: left;
    border-bottom: 1.5px solid var(--clr-border, #e2e8f0);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.leaderboard-table-mobile td {
    padding: 10px 6px;
    border-bottom: 1px solid var(--clr-border-light, #f1f5f9);
}

.leaderboard-table-mobile tr:last-child td {
    border-bottom: none;
}

/* Link Code */
.drawer-link-code {
    padding: 12px;
    background: rgba(var(--clr-primary-teal-rgb), 0.05);
    border: 2px dashed rgba(var(--clr-primary-teal-rgb), 0.3);
    border-radius: 10px;
    text-align: center;
    font-family: var(--font-family-code-math, monospace);
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--clr-primary-teal);
    cursor: pointer;
    transition: all 150ms ease;
}

.drawer-link-code:active {
    background: rgba(var(--clr-primary-teal-rgb), 0.1);
    transform: scale(0.98);
}

/* Drawer Overlay */
.drawer-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
    z-index: 1999;
    opacity: 0;
    visibility: hidden;
    transition: opacity 300ms ease, visibility 300ms ease;
}

.drawer-overlay.active {
    opacity: 1;
    visibility: visible;
}

/* Hide drawers on desktop */
@media (min-width: 769px) {
    .mobile-drawer,
    .drawer-overlay {
        display: none !important;
    }
}

/* --- 8. MOBILE Z-INDEX STACKING --- */

@media (max-width: 768px) {
    .landing-header            { display: none !important; }
    footer                     { display: none !important; }
    #xp-feed                   { top: 10px; right: 10px; max-width: calc(100vw - 20px); z-index: 1400; }
    #streak-counter            { top: 10px; left: 10px; z-index: 1400; }
    #app-sidebar               { z-index: 1500; }
    #whiteboard-panel,
    #floating-calculator       { z-index: 1600; }
    .mobile-chat-nav           { z-index: 1800; }
    .drawer-overlay            { z-index: 1999; }
    .mobile-drawer             { z-index: 2000; }
    .more-tools-overlay        { z-index: 2099; }
    .more-tools-menu           { z-index: 2100; }
    .modal-overlay             { z-index: 9999; }
    .modal-overlay.is-visible  { touch-action: none; }
    .modal-content             { touch-action: auto; }
    #levelup-celebration-modal,
    #badge-celebration-modal   { z-index: 10000; }

    /* Session stats bar — hidden on mobile (available in drawer) */
    #session-stats-bar,
    .session-stats-bar {
        display: none !important;
    }

    /* File grid */
    #file-grid-container {
        padding: 8px;
        max-width: 100%;
    }
}

/* --- 9. ORIENTATION HANDLING --- */

/* Landscape mobile — squeeze bottom bar for max chat space */
@media (max-height: 500px) and (orientation: landscape) {
    /* Header already hidden via Section 2 */

    /* Compact bottom nav in landscape */
    body.has-chat-nav .mobile-chat-nav,
    body.landing-page-body .mobile-chat-nav {
        height: 40px !important;
    }

    /* Adjust wrapper offset for smaller nav */
    #app-layout-wrapper {
        padding-bottom: calc(40px + env(safe-area-inset-bottom, 0px)) !important;
    }

    .chat-nav-item span {
        display: none;
    }

    .chat-nav-item i {
        font-size: 1.05rem;
    }

    .chat-nav-item.active::after {
        bottom: 1px;
    }

    .input-toolbar button {
        min-width: 36px;
        min-height: 36px;
    }

    #send-button {
        width: 32px;
        height: 32px;
        min-width: 32px;
        min-height: 32px;
        padding: 0;
    }

    #chat-messages-container {
        padding: 6px 10px;
    }

    .message {
        padding: 8px 12px;
        margin-bottom: 1px;
    }

    .message-avatar {
        width: 24px;
        height: 24px;
    }

    .message-avatar img {
        width: 24px;
        height: 24px;
    }
}

/* Tablet landscape with touch — keep sidebar toggle, don't swap to drawers */
@media (min-width: 768px) and (max-width: 1024px) and (orientation: landscape) {
    body.is-touch-device .landing-header {
        min-height: 44px;
        position: sticky;
        top: 0;
        z-index: 1000;
        width: 100%;
    }
}

/* Touch device overrides — header hidden on mobile */
@media (max-width: 768px) {
    body.is-touch-device .landing-header,
    body.is-mobile .landing-header {
        display: none !important;
    }

    /* On mobile, sidebar toggle is replaced by drawer toggle + bottom nav */
    body.is-touch-device #sidebar-toggle,
    body.is-mobile #sidebar-toggle {
        display: none !important;
    }
}

/* --- 10. PRINT STYLESHEET --- */

@media print {
    #chat-messages-container::before { display: none !important; }

    header, .landing-header, .drawer-toggle-btn,
    .input-toolbar, #send-button, .modal-overlay,
    #app-sidebar { display: none !important; }

    #chat-messages-container {
        padding: 0 !important;
        max-width: 100% !important;
        background: white !important;
    }

    .message {
        page-break-inside: avoid;
        box-shadow: none !important;
        border: 1px solid #eee !important;
    }
}

/* --- 11. RESPONSIVE FORM PAGES (Login/Signup/Auth) --- */

@media (max-width: 768px) {
    .login-page-wrapper,
    .signup-page-wrapper {
        padding: 16px;
        justify-content: flex-start;
        padding-top: 24px;
    }

    .login-container,
    .signup-container {
        max-width: 100%;
        padding: 24px 20px;
        border-radius: 16px;
    }

    .login-container h1,
    .signup-container h1 {
        font-size: 1.6rem;
    }

    .login-container input,
    .signup-container input,
    .login-container select,
    .signup-container select {
        font-size: 16px; /* Prevent iOS zoom */
        padding: 12px 14px;
        border-radius: 10px;
    }

    .social-login-buttons {
        flex-direction: column;
        gap: 10px;
    }

    .social-btn {
        width: 100%;
        padding: 12px;
        border-radius: 10px;
        font-size: 0.95rem;
    }
}

/* --- 12. RESPONSIVE DASHBOARD PAGES --- */

@media (max-width: 768px) {
    .dashboard-container {
        padding: 12px;
    }

    .dashboard-grid {
        grid-template-columns: 1fr;
        gap: 12px;
    }

    .dashboard-card {
        padding: 16px;
        border-radius: 14px;
    }

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

    /* Tab scrolling for dashboard tabs */
    .dashboard-tabs {
        display: flex;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        gap: 4px;
        padding-bottom: 4px;
    }

    .dashboard-tabs::-webkit-scrollbar { display: none; }

    .tab-btn {
        white-space: nowrap;
        flex-shrink: 0;
        padding: 10px 16px;
        font-size: 0.88rem;
        border-radius: 10px;
        min-height: 44px;
    }
}

/* --- 13. RESPONSIVE LANDING PAGE FIXES --- */

@media (max-width: 480px) {
    /* Extra-small screens */
    .lp-hero h1 { font-size: 1.75rem; }
    .lp-hero .lp-hero-sub { font-size: 0.95rem; }

    .lp-hero-buttons .btn {
        width: 100%;
        max-width: 260px;
        padding: 12px 20px;
    }

    .lp-section-heading { font-size: 1.4rem; }

    .lp-feature-card,
    .lp-testimonial,
    .lp-compare-card {
        padding: 16px;
    }

    .lp-role-tabs {
        grid-template-columns: 1fr 1fr 1fr;
        gap: 6px;
    }

    .lp-role-tab {
        padding: 10px 4px;
    }

    .lp-role-tab-icon {
        width: 38px;
        height: 38px;
    }

    .lp-role-tab-label {
        font-size: 0.75rem;
    }
}

/* --- 14. RESPONSIVE GAME/INTERACTIVE PAGES --- */

@media (max-width: 768px) {
    /* Mastery Arcade */
    .arcade-container,
    .game-container {
        padding: 8px;
        max-width: 100%;
    }

    /* Number Run / Fact Fluency */
    .game-controls {
        flex-wrap: wrap;
        gap: 8px;
    }

    .game-controls button {
        min-height: 48px;
        min-width: 48px;
        font-size: 1.1rem;
    }
}

/* --- 15. ENHANCED ACCESSIBILITY --- */

@media (max-width: 768px) {
    /* Ensure all interactive elements meet 44px touch target */
    a[href],
    button,
    input[type="submit"],
    input[type="button"],
    .btn,
    .tab-btn,
    select {
        min-height: 44px;
    }

    /* Better focus states for mobile keyboard users */
    :focus-visible {
        outline: 2px solid var(--clr-primary-teal, #12B3B3);
        outline-offset: 2px;
    }
}

/* --- 16. MOBILE LAYOUT OVERRIDES (CRITICAL) ---
   Body becomes a flex column:  chat (flex:1) → bottom nav (56px).
   Header & footer hidden — branding via watermark, nav via bottom bar.
   Uses 100dvh for modern browsers, falls back to 100vh.
   ------------------------------------------------------------------- */

@media (max-width: 768px) {
    /* Hide footer — copyright is not needed in chat */
    body.landing-page-body footer {
        display: none !important;
    }

    /* ---- THE FLEX COLUMN ---- */
    body.landing-page-body {
        display: flex !important;
        flex-direction: column !important;
        height: 100vh !important;
        height: 100dvh !important;
        overflow: hidden !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    /* Header hidden — no flex space needed */
    body.landing-page-body .landing-header {
        flex: 0 0 0px !important;
        display: none !important;
    }

    :root {
        --header-height: 0px;
    }

    /* App wrapper fills the space between header and bottom nav.
       Bottom nav is position:fixed (56px + safe-area), so we need
       padding-bottom to prevent content from being hidden behind it. */
    #app-layout-wrapper {
        flex: 1 1 0% !important;
        min-height: 0 !important;
        max-height: none !important;
        height: auto !important;
        overflow: hidden !important;
        padding: 0 !important;
        padding-bottom: calc(56px + env(safe-area-inset-bottom, 0px)) !important;
        display: flex !important;
        flex-direction: column !important;
        align-items: stretch !important;
    }

    /* Remove desktop sidebar margin */
    body.landing-page-body #chat-container {
        margin-bottom: 0 !important;
    }

    /* Chat container — inner flex column */
    #chat-container {
        flex: 1 1 0% !important;
        min-height: 0 !important;
        display: flex !important;
        flex-direction: column !important;
        border-radius: 0;
        box-shadow: none;
        margin-bottom: 0 !important;
    }

    /* Messages: scrollable, fills remaining height */
    #chat-messages-container {
        flex: 1 1 0% !important;
        min-height: 0 !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-y: contain;
    }

    /* Input: pinned at bottom of chat, never collapses */
    #input-container {
        flex: 0 0 auto !important;
        position: relative !important;
        z-index: 100 !important;
        background: var(--clr-bg-light, #fff);
        border-top: 1px solid rgba(0, 0, 0, 0.06);
        box-shadow: 0 -1px 6px rgba(0, 0, 0, 0.03);
    }

    /* Compact inline toolbar row (overrides inline styles) */
    #input-container > div[style*="display: flex"] {
        padding: 2px 8px !important;
        gap: 6px !important;
    }

    .input-toolbar-compact {
        gap: 2px !important;
    }

    .input-toolbar-compact .btn-sm {
        padding: 4px 8px !important;
        min-height: 32px !important;
        min-width: 32px !important;
        font-size: 0.85rem !important;
    }

    /* Send button — iMessage circle */
    #send-button {
        width: 36px !important;
        height: 36px !important;
        min-width: 36px !important;
        min-height: 36px !important;
        padding: 0 !important;
        border-radius: 50% !important;
    }

    /* Text input */
    #user-input {
        min-height: 36px !important;
        padding: 8px 14px !important;
        margin-bottom: 0 !important;
        border-radius: 20px;
        font-size: 16px;
    }

    /* Hide session stats on mobile */
    .session-stats-bar {
        display: none !important;
    }

    /* Chat disclaimer */
    .chat-disclaimer {
        font-size: 0.68rem;
        margin: 2px 0 4px;
        padding: 0 4px;
        opacity: 0.65;
    }

    .chat-disclaimer i {
        font-size: 0.6rem;
    }
}

/* --- 17. IEP ELEMENTS MOBILE SAFETY ---
   Prevent IEP fixed-position buttons from covering chat input.
   ----------------------------------------------------------- */

@media (max-width: 600px) {
    /* Move IEP break button above the bottom nav */
    .iep-break-btn {
        bottom: 72px;
        left: 10px;
        font-size: 12px;
        padding: 8px 12px;
    }

    /* Move multiplication chart button to the right to not overlap break btn */
    .iep-mult-chart-btn {
        bottom: 72px;
        right: 10px;
        left: auto;
        font-size: 12px;
        padding: 8px 12px;
    }

    /* IEP goal notifications at bottom of mobile screen */
    .iep-goal-notification {
        right: 8px;
        left: 8px;
        max-width: none;
    }

    .iep-goal-notification.visible {
        bottom: 72px;
    }

    /* Stop-audio button: float above chat on mobile since header is hidden */
    #stop-audio-btn {
        position: fixed !important;
        top: 12px;
        right: 12px;
        z-index: 9999;
        border-radius: 50%;
        width: 44px;
        height: 44px;
        padding: 0;
        font-size: 18px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
        background: #e74c3c;
        color: #fff;
        border: none;
    }

    /* Graph tool: responsive width on small screens */
    .graph-tool-container svg,
    .graph-tool-container canvas {
        max-width: 100% !important;
        height: auto !important;
    }
}

/* --- 18. FREE-TIME INDICATOR: ABOVE INPUT + BOTTOM NAV ---
   The billing time indicator is fixed at bottom:12px on desktop.
   On mobile it must sit above both the bottom nav AND the input container
   so it never blocks the chat input box (especially on iPhone). */
@media (max-width: 768px) {
    #free-time-indicator {
        /* Position above input area: 64px nav + safe-area + ~70px input + 8px gap */
        bottom: calc(64px + env(safe-area-inset-bottom, 0px) + 78px) !important;
        /* Compact pill on right side — NOT full-width, so it doesn't block input */
        right: 10px !important;
        left: auto !important;
        max-width: 200px !important;
        border-radius: 10px !important;
        text-align: center !important;
        font-size: 11px !important;
        padding: 6px 10px !important;
        opacity: 0.95 !important;
    }
}

/* Ensure input container is always above the time indicator on mobile */
@media (max-width: 768px) {
    #input-container {
        position: relative;
        z-index: 1800 !important;
    }
}

@media (max-width: 600px) {
    /* --- Equation Palette: Mobile Bottom Sheet --- */
    .inline-equation-palette {
        position: fixed !important;
        top: auto !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        transform: none !important;
        width: 100% !important;
        max-width: 100% !important;
        max-height: 55vh !important;
        border-radius: 16px 16px 0 0 !important;
        box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.2) !important;
        padding: 0 !important;
        margin: 0 !important;
        resize: none !important;
        animation: slideUpSheet 0.25s ease-out !important;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        z-index: 1001 !important;
    }

    @keyframes slideUpSheet {
        from { transform: translateY(100%); }
        to   { transform: translateY(0); }
    }

    /* Drag handle at top of bottom sheet */
    .inline-equation-palette .equation-palette-header {
        position: sticky;
        top: 0;
        background: inherit;
        z-index: 2;
        padding: 10px 16px 6px;
        margin: 0;
        border-bottom: 1px solid rgba(128, 128, 128, 0.2);
        cursor: default;
    }

    .inline-equation-palette .equation-palette-header::before {
        content: '';
        display: block;
        width: 36px;
        height: 4px;
        background: rgba(128, 128, 128, 0.4);
        border-radius: 2px;
        margin: 0 auto 6px;
    }

    /* Close button larger on mobile */
    .inline-equation-palette .palette-close-btn {
        width: 44px;
        height: 44px;
        font-size: 1.8em;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* Move common symbols ABOVE subscripts/superscripts on mobile */
    .inline-equation-palette .equation-symbols-grid {
        order: -1;
        padding: 6px 12px;
        grid-template-columns: repeat(5, 1fr);
        gap: 5px;
    }

    /* Collapse subscripts section — less useful on mobile, show behind a summary */
    .inline-equation-palette .equation-section-header {
        padding: 0 12px;
        margin-top: 2px;
        font-size: 0.8em;
    }
    .inline-equation-palette .subscript-superscript-grid {
        padding: 0 12px;
        grid-template-columns: repeat(4, 1fr);
        gap: 5px;
    }

    /* Symbol buttons: compact touch targets */
    .inline-equation-palette .symbol-btn {
        min-height: 40px;
        font-size: 1.15em;
        padding: 6px;
    }
    .inline-equation-palette .script-btn {
        min-height: 40px;
        padding: 8px;
    }

    /* Hide tip text on mobile to save space */
    .inline-equation-palette .palette-tip {
        display: none;
    }

    /* Math field container pinned to bottom of sheet */
    .inline-equation-palette .inline-math-field-container {
        position: sticky;
        bottom: 0;
        background: inherit;
        z-index: 2;
        padding: 8px 12px;
        border-top: 1px solid rgba(128, 128, 128, 0.2);
        box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.06);
    }

    .inline-equation-palette .inline-math-field-container label {
        font-size: 0.8em;
    }

    /* Insert button: full-width on mobile */
    .inline-equation-palette .math-line-actions {
        flex-direction: row;
    }
    .inline-equation-palette #insert-inline-equation {
        flex: 1;
        padding: 10px;
        font-size: 1em;
    }

    /* Backdrop overlay for bottom sheet */
    .equation-palette-backdrop {
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.4);
        z-index: 1000;
        animation: fadeIn 0.2s ease;
    }

    @keyframes fadeIn {
        from { opacity: 0; }
        to   { opacity: 1; }
    }

    /* Hide MathLive's virtual keyboard on mobile — we use our own math keyboard */
    .ML__keyboard {
        display: none !important;
    }

    /* --- Math Keyboard Panel: mobile tuning --- */
    .math-keyboard-panel {
        padding-bottom: env(safe-area-inset-bottom, 0);
    }

    .mk-key {
        min-height: 44px;
        font-size: 1.05rem;
    }

    .math-keyboard-field {
        min-height: 38px;
        font-size: 1.05rem;
    }
}
