/* =====================================================================
   MOBILE DOCUMENT WRITER — PRODUCTION REDESIGN
   =====================================================================
   Notion/Bear/iA Writer–inspired mobile editor experience.
   Load this AFTER document-writer.css.
   Selector strategy:
     - Core layout & sidebar: `html body #documents-view.documents-view` /
       `html body #documents-view .x` → specificity (1,1,2) to beat
       desktop ID selectors like `#documents-view.documents-view` (1,1,0).
     - Child elements: `html body .x` → specificity (0,1,2) to beat
       desktop class selectors like `.x` (0,1,0).
   Breakpoint: max-width: 1024px (matches visibility-system, floating-nav,
   immersive-mode). Desktop override at min-width: 1025px.

   Architecture:
     §1  Core Layout (fixed viewport, no scroll escape)
     §2  Compact Header (single row: hamburger | title | overflow)
     §3  Desktop Toolbar — Hidden (replaced by mobile bottom toolbar)
     §4  Editor Area — Full-width immersive writing surface
     §5  Desktop Footer — Hidden (stats inside bottom toolbar)
     §6  Sidebar — GPU-accelerated slide-in drawer
     §7  Mobile Bottom Toolbar — Fixed, keyboard-aware (created by JS)
     §8  Action Sheet — iOS-style overflow menu (created by JS)
     §9  AI Panel & Coming-Soon — Hidden on mobile
     §10 Mode Toggle — Compact pill
     §11 Touch, Typography, Safe Areas
     §12 Scrollbars — Ultra-thin
     §13 Landscape & Immersive
     §14 Desktop Overrides (hide mobile-only elements)
     §15 Animations
     §16 Reduced Motion
   ===================================================================== */

/* =====================================================================
   §1  CORE LAYOUT
   ===================================================================== */
@media screen and (max-width: 1024px) {

    :root {
        --mobile-toolbar-h: 48px;
        --mobile-stats-h: 32px;
        --doc-status-saved: #28a745;
        --doc-status-error: #ef4444;
    }

    html body #documents-view.documents-view {
        position: fixed;
        top: var(--floating-nav-height-mobile, 58px);
        left: 0; right: 0; bottom: 0;
        width: 100%;
        height: calc(100dvh - var(--floating-nav-height-mobile, 58px));
        min-height: 0;
        overflow: hidden;
        /* display controlled by visibility-system.css */
        flex-direction: column;
        background: var(--doc-bg-primary, #0a0e27);
        z-index: var(--z-feature-view, 400);
        /* Disable expensive background animation on mobile */
        animation: none;
    }

    /* Kill all futuristic pseudo-elements for performance */
    html body #documents-view::before,
    html body .document-header::after,
    html body .editor-footer::before,
    html body #documents-view .document-sidebar::after,
    html body .document-ai-panel::before,
    html body .preview-pane::before,
    html body .document-writer-container::before,
    html body .document-writer-container::after,
    html body .editor-container::before,
    html body .editor-content::before,
    html body .editor-content::after,
    html body .preview-pane::after,
    html body .document-item::after,
    html body .doc-btn-primary::before,
    html body .storage-bar-fill::after,
    html body .sidebar-brand-icon::before {
        display: none !important;
    }

    /* Kill animated gradient title on mobile */
    html body .sidebar-brand-text .sidebar-title {
        animation: none;
        background-size: 100% 100%;
    }

    /* Disable staggered entrance */
    html body .document-item { animation: none; opacity: 1; }

    /* Hide AI Coming Soon */
    html body .document-ai-panel-coming-soon { display: none; }

    html body .document-writer-container {
        display: flex;
        flex-direction: column;
        height: 100%;
        overflow: hidden;
    }

/* =====================================================================
   §2  COMPACT HEADER — Single Row
   Layout: [hamburger] [title ···] [save-dot] [overflow ⋮]
   ===================================================================== */

    html body .document-header {
        display: flex;
        flex-direction: row;
        align-items: center;
        height: 52px;
        min-height: 52px;
        max-height: 52px;
        padding: 0 10px;
        padding-top: env(safe-area-inset-top, 0px);
        background: var(--doc-bg-secondary, #151a35);
        border-bottom: 1px solid rgba(0, 242, 254, 0.1);
        box-shadow: 0 1px 8px rgba(0, 0, 0, 0.18);
        gap: 6px;
        z-index: var(--z-sticky, 150);
        flex-shrink: 0;
        position: relative;
    }

    /* Flatten the two-row desktop layout into one row */
    html body .document-header-top { display: contents; }
    html body .document-header-bottom { display: contents; }

    /* Hide grouped button containers — actions move to overflow/bottom toolbar */
    html body .document-header-left { display: none; }
    html body .document-header-right { display: none; }

    /* Hamburger menu button — order 1 (left).
       Raised from 40px to 44px to meet WCAG 2.5.5 minimum touch target. */
    html body .doc-mobile-menu-toggle {
        display: flex;
        width: 44px; height: 44px;
        min-width: 44px; min-height: 44px;
        /* Keep badge visible even on flex layouts that default to clipping */
        overflow: visible;
        align-items: center;
        justify-content: center;
        background: transparent;
        border: none;
        color: rgba(255, 255, 255, 0.7);
        font-size: 18px;
        cursor: pointer;
        border-radius: 10px;
        transition: background-color 0.15s ease;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
        flex-shrink: 0;
        order: 1;
    }

    html body .doc-mobile-menu-toggle:active {
        background: rgba(255, 255, 255, 0.08);
    }

    /* Keyboard focus indicators — required for users on iPads / Bluetooth
     * keyboards / Switch Control. Mobile-only selectors needed because the
     * desktop focus-visible block above scopes by #documents-view. */
    html body .doc-mobile-menu-toggle:focus-visible,
    html body .sidebar-close-btn:focus-visible,
    html body .mobile-overflow-btn:focus-visible,
    html body .mobile-toolbar-btn:focus-visible {
        outline: 2px solid rgba(0, 242, 254, 0.6);
        outline-offset: 2px;
    }

    /* Title input — left-aligned + truncated — order 2.
     * Previously `text-align: center` default with `:focus { text-align: left }`
     * caused the text to reflow on tap, which moved the caret to an
     * unexpected position and was disorienting. Now consistently left-aligned. */
    html body .document-title-input {
        flex: 1;
        min-width: 0;
        font-size: 16px;
        font-weight: 700;
        padding: 6px 8px;
        border: none;
        border-bottom: 2px solid transparent;
        background: transparent;
        color: #fff;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
        text-align: left;
        letter-spacing: -0.3px;
        order: 2;
        border-radius: 0;
        transition: border-color 0.2s ease;
        height: 40px;
    }

    html body .document-title-input:focus {
        border-bottom-color: var(--doc-accent-cyan, #00f2fe);
        outline: none;
        text-shadow: none;
    }

    /* Document status — hide original (mobile uses inline save indicator) */
    html body .document-status { display: none; }

    /* Mobile save indicator — sits in header, right of title — order 3 */
    html body .mobile-save-indicator {
        display: flex;
        align-items: center;
        gap: 4px;
        font-size: 10px;
        color: rgba(255, 255, 255, 0.45);
        white-space: nowrap;
        flex-shrink: 0;
        order: 3;
        transition: color 0.3s ease;
    }

    html body .mobile-save-indicator .save-dot-mini {
        width: 6px; height: 6px;
        border-radius: 50%;
        background: var(--doc-status-saved);
        flex-shrink: 0;
        transition: background-color 0.3s ease;
    }

    html body .mobile-save-indicator.saving .save-dot-mini {
        background: var(--doc-accent-cyan, #00f2fe);
        animation: mobilePulseDot 1s ease-in-out infinite;
    }

    html body .mobile-save-indicator.saving {
        color: var(--doc-accent-cyan, #00f2fe);
    }

    html body .mobile-save-indicator.saved .save-dot-mini { background: var(--doc-status-saved); }
    html body .mobile-save-indicator.error .save-dot-mini { background: var(--doc-status-error); }

    /* Overflow button (⋮) — order 4 (right-most).
       Raised to 44px to match the hamburger and satisfy WCAG 2.5.5. */
    html body .mobile-overflow-btn {
        display: flex;
        width: 44px; height: 44px;
        min-width: 44px; min-height: 44px;
        align-items: center;
        justify-content: center;
        background: transparent;
        border: none;
        color: rgba(255, 255, 255, 0.7);
        font-size: 18px;
        cursor: pointer;
        border-radius: 10px;
        transition: background-color 0.15s ease;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
        flex-shrink: 0;
        order: 4;
    }

    html body .mobile-overflow-btn:active {
        background: rgba(255, 255, 255, 0.08);
    }

/* =====================================================================
   §3  DESKTOP TOOLBAR — Hidden on mobile
   ===================================================================== */

    html body .editor-toolbar { display: none; }

/* =====================================================================
   §4  EDITOR AREA — Full-width writing surface
   ===================================================================== */

    html body .document-main {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: hidden;
        position: relative;
        background: var(--doc-bg-primary, #0a0e27);
    }

    html body .document-editor-area {
        flex: 1;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        width: 100%;
        max-width: 100%;
        padding: 0;
        contain: layout style;
    }

    html body .editor-container {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: hidden;
        border-radius: 0;
        background: var(--doc-bg-primary, #0a0e27);
    }

    html body .editor-content {
        flex: 1;
        display: flex;
        flex-direction: column;
        overflow: hidden;
        background: var(--doc-bg-primary, #0a0e27);
    }

    html body .markdown-editor {
        flex: 1;
        width: 100%;
        max-width: 100%;
        padding: 20px 18px;
        /* Bottom padding: room for mobile bottom toolbar */
        padding-bottom: calc(var(--mobile-toolbar-h, 48px) + var(--mobile-stats-h, 32px) + env(safe-area-inset-bottom, 0px) + 20px);
        font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
        font-size: 16px;
        line-height: 1.7;
        color: rgba(255, 255, 255, 0.92);
        background: var(--doc-bg-primary, #0a0e27);
        border: none;
        resize: none;
        overflow-y: auto;
        overscroll-behavior: contain;
        caret-color: var(--doc-accent-cyan, #00f2fe);
        -webkit-text-size-adjust: 100%;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        margin: 0;
        box-sizing: border-box;
        min-height: 0;
    }

    html body .markdown-editor::placeholder { color: rgba(255, 255, 255, 0.35); }
    html body .markdown-editor:focus {
        outline: 2px solid rgba(0, 242, 254, 0.3);
        outline-offset: -2px;
        border: none;
    }

    html body .preview-pane {
        flex: 1;
        padding: 20px 18px;
        padding-bottom: calc(var(--mobile-toolbar-h, 48px) + var(--mobile-stats-h, 32px) + env(safe-area-inset-bottom, 0px) + 20px);
        overflow-y: auto;
        overscroll-behavior: contain;
        background: var(--doc-bg-primary, #0a0e27);
        border-left: none;
    }

    html body .preview-content {
        font-size: 15px;
        line-height: 1.7;
        color: rgba(255, 255, 255, 0.92);
        max-width: 100%;
    }

    html body .preview-content h1 { font-size: clamp(22px, 5.5vw, 28px); line-height: 1.3; margin-top: 1.5rem; }
    html body .preview-content h2 { font-size: clamp(18px, 4.5vw, 22px); line-height: 1.3; }
    html body .preview-content h3 { font-size: clamp(16px, 4vw, 20px); line-height: 1.4; }
    html body .preview-content p { font-size: 15px; line-height: 1.7; }
    html body .preview-content code { font-size: 13px; }
    html body .preview-content pre { overflow-x: auto; font-size: 13px; padding: 16px; border-radius: 10px; }
    html body .preview-content table { font-size: 13px; display: block; overflow-x: auto; }
    html body .preview-content blockquote { margin-left: 0; padding: 12px 16px; border-radius: 0 10px 10px 0; }
    html body .preview-content img { max-width: 100%; height: auto; }

/* =====================================================================
   §5  DESKTOP FOOTER — Hidden (replaced by mobile stats bar)
   ===================================================================== */

    html body .editor-footer { display: none; }

    /* Keep save-indicator structure for JS but hide desktop version */
    html body .save-indicator { display: none; }

/* =====================================================================
   §6  SIDEBAR — GPU-accelerated slide-in drawer
   ===================================================================== */

    html body #documents-view .document-sidebar {
        display: flex;
        position: fixed;
        top: 0; left: 0; bottom: 0;
        width: 85vw;
        max-width: 340px;
        max-height: none;
        z-index: var(--z-sidebar-mobile, 850);
        background: var(--doc-bg-secondary, #151a35);
        border-right: 1px solid rgba(0, 242, 254, 0.12);
        border-bottom: none;
        transform: translateX(-100%);
        transition: transform 0.32s cubic-bezier(0.32, 0.72, 0, 1);
        flex-direction: column;
        flex-wrap: nowrap;
        overflow-y: auto;
        overscroll-behavior: contain;
        padding: 0;
        box-shadow: none;
        will-change: transform;
    }

    html body #documents-view .document-sidebar.open {
        transform: translateX(0);
        box-shadow: 8px 0 40px rgba(0, 0, 0, 0.35);
    }

    /* Remove old CSS pseudo-element decorators */
    html body #documents-view .document-sidebar::before { display: none; }
    html body #documents-view .document-sidebar::after { display: none; }

    /* Close button */
    html body .sidebar-close-btn {
        display: flex;
        position: absolute;
        top: 12px; right: 12px;
        width: 44px; height: 44px;
        min-width: 44px; min-height: 44px;
        align-items: center;
        justify-content: center;
        background: rgba(255, 255, 255, 0.06);
        border: 1px solid rgba(255, 255, 255, 0.1);
        border-radius: 10px;
        color: rgba(255, 255, 255, 0.7);
        font-size: 16px;
        cursor: pointer;
        z-index: 2;
        transition: background-color 0.15s ease;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }

    html body .sidebar-close-btn:active {
        background: rgba(255, 255, 255, 0.12);
    }

    /* Sidebar brand */
    html body .sidebar-brand { padding: 1rem 1rem 0.5rem; }
    html body .sidebar-brand-icon { width: 32px; height: 32px; font-size: 0.85rem; }
    html body .sidebar-brand h2 { font-size: 1rem; }
    html body .sidebar-title { display: block; font-size: 18px; font-weight: 700; color: var(--doc-accent-cyan, #00f2fe); padding: 16px 16px 8px; letter-spacing: -0.3px; }

    /* Hide storage on mobile */
    html body .sidebar-footer-info { display: none; }

    html body .sidebar-section { padding: 12px; }

    html body .document-search input {
        height: 44px;
        font-size: 15px;
        border-radius: 12px;
    }

    html body .document-item {
        padding: 14px 12px;
        border-radius: 12px;
        margin-bottom: 6px;
        min-height: 44px;
    }

    html body .document-item-title { font-size: 15px; margin-bottom: 4px; }
    html body .document-item-meta { font-size: 12px; }

    /* Generating-card countdown: keep it bottom-right and legible at the
       larger mobile card padding. margin-left:auto comes from the desktop
       rule; just nudge the size. */
    html body .task-countdown { font-size: 12px; }

    /* AI dialog "explore other services" links + countdown on mobile:
       full-width, comfortable tap targets, stacked when they don't fit. */
    html body .ai-dialog-explore-links {
        flex-direction: column;
        gap: 8px;
        width: 100%;
    }
    html body .ai-dialog-explore-link {
        width: 100%;
        justify-content: center;
        min-height: 48px;
        font-size: 15px;
    }
    html body .ai-dialog-countdown { font-size: 1.8rem; }
    html body .ai-dialog-ready-actions { width: 100%; }
    html body .ai-dialog-ready-actions .btn { flex: 1 1 auto; min-height: 48px; }

    /* Sidebar overlay/backdrop */
    html body .doc-sidebar-overlay {
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: var(--z-overlay, 800);
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.3s cubic-bezier(0.32, 0.72, 0, 1);
        -webkit-tap-highlight-color: transparent;
    }

    html body .doc-sidebar-overlay.open {
        opacity: 1;
        pointer-events: auto;
    }

/* =====================================================================
   §7  MOBILE BOTTOM TOOLBAR — Fixed, keyboard-aware
   Notion/Bear–inspired bottom formatting bar. Created by JS.
   ===================================================================== */

    html body .mobile-bottom-toolbar {
        position: fixed;
        bottom: 0; left: 0; right: 0;
        z-index: var(--z-fixed, 200);
        background: var(--doc-bg-secondary, #151a35);
        border-top: 1px solid rgba(0, 242, 254, 0.1);
        padding-bottom: env(safe-area-inset-bottom, 0px);
        box-shadow: 0 -2px 16px rgba(0, 0, 0, 0.2);
        transition: transform 0.15s ease;
        will-change: transform;
    }

    /* Stats bar — single line, compact */
    html body .mobile-stats-bar {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 16px;
        height: var(--mobile-stats-h, 32px);
        padding: 0 16px;
        font-size: 11px;
        color: rgba(255, 255, 255, 0.45);
        border-bottom: 1px solid rgba(255, 255, 255, 0.04);
        background: rgba(10, 14, 39, 0.6);
        overflow: hidden;
        max-height: var(--mobile-stats-h, 32px);
        transition: max-height 0.2s ease, opacity 0.2s ease;
    }

    html body .mobile-stats-bar span {
        display: flex;
        align-items: center;
        gap: 4px;
        white-space: nowrap;
    }

    html body .mobile-stats-bar i {
        font-size: 10px;
        color: var(--doc-accent-cyan, #00f2fe);
        opacity: 0.6;
    }

    /* Primary toolbar row — horizontal scroll */
    html body .mobile-toolbar-row {
        display: flex;
        align-items: center;
        height: var(--mobile-toolbar-h, 48px);
        padding: 0 6px;
        overflow-x: auto;
        overflow-y: hidden;
        overscroll-behavior: contain;
        scrollbar-width: none;
        gap: 2px;
    }

    html body .mobile-toolbar-row::-webkit-scrollbar { display: none; }

    /* Expand chevron for secondary tools.
       Raised from 36×36 to 44×44 (WCAG 2.5.5 minimum). Visual size kept
       compact via inner padding so the toolbar density doesn't balloon. */
    html body .mobile-toolbar-expand {
        width: 44px; height: 44px;
        border: none;
        background: rgba(255, 255, 255, 0.05);
        color: rgba(255, 255, 255, 0.6);
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 12px;
        cursor: pointer;
        flex-shrink: 0;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
        transition: transform 0.2s ease, background-color 0.15s ease;
        margin-left: auto;
    }

    html body .mobile-toolbar-expand.expanded {
        transform: rotate(180deg);
        background: rgba(0, 242, 254, 0.1);
        color: var(--doc-accent-cyan, #00f2fe);
    }

    /* Secondary row — collapsed by default */
    html body .mobile-toolbar-secondary {
        display: flex;
        align-items: center;
        height: 0;
        max-height: 0;
        padding: 0 6px;
        overflow: hidden;
        gap: 2px;
        transition: max-height 0.25s cubic-bezier(0.32, 0.72, 0, 1),
                    height 0.25s cubic-bezier(0.32, 0.72, 0, 1);
        border-top: 0px solid transparent;
    }

    html body .mobile-toolbar-secondary.expanded {
        height: var(--mobile-toolbar-h, 48px);
        max-height: var(--mobile-toolbar-h, 48px);
        border-top: 1px solid rgba(255, 255, 255, 0.04);
    }

    /* Toolbar buttons — 44×44 touch targets */
    html body .mobile-toolbar-btn {
        width: 40px; height: 40px;
        min-width: 40px;
        border: none;
        background: transparent;
        color: rgba(255, 255, 255, 0.65);
        font-size: 15px;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        flex-shrink: 0;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
        transition: background-color 0.12s ease, color 0.12s ease;
    }

    html body .mobile-toolbar-btn:active {
        background: rgba(0, 242, 254, 0.15);
        color: var(--doc-accent-cyan, #00f2fe);
    }

    html body .mobile-toolbar-btn.active {
        background: rgba(0, 242, 254, 0.12);
        color: var(--doc-accent-cyan, #00f2fe);
    }

    html body .mobile-toolbar-btn .heading-label {
        font-size: 13px;
        font-weight: 700;
        letter-spacing: -0.5px;
    }

    html body .mobile-toolbar-divider {
        width: 1px; height: 24px;
        background: rgba(255, 255, 255, 0.08);
        margin: 0 4px;
        flex-shrink: 0;
    }

/* =====================================================================
   §8  ACTION SHEET — iOS-style bottom sheet (created by JS)
   ===================================================================== */

    html body .mobile-action-sheet-backdrop {
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.55);
        z-index: var(--z-modal-backdrop, 1000);
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.3s cubic-bezier(0.32, 0.72, 0, 1);
        -webkit-tap-highlight-color: transparent;
    }

    html body .mobile-action-sheet-backdrop.visible {
        opacity: 1;
        pointer-events: auto;
    }

    html body .mobile-action-sheet {
        position: fixed;
        bottom: 0; left: 0; right: 0;
        background: var(--doc-bg-secondary, #151a35);
        border-top-left-radius: 20px;
        border-top-right-radius: 20px;
        z-index: var(--z-modal, 1050);
        transform: translateY(100%);
        transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
        padding-bottom: env(safe-area-inset-bottom, 0px);
        max-height: 70vh;
        overflow-y: auto;
        overscroll-behavior: contain;
        box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.35);
    }

    html body .mobile-action-sheet.open {
        transform: translateY(0);
    }

    html body .mobile-action-sheet-handle {
        width: 36px; height: 4px;
        background: rgba(255, 255, 255, 0.2);
        border-radius: 2px;
        margin: 10px auto 6px;
    }

    html body .mobile-action-sheet-title {
        padding: 8px 20px 12px;
        font-size: 12px;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.8px;
        color: rgba(255, 255, 255, 0.4);
        border-bottom: 1px solid rgba(0, 242, 254, 0.08);
    }

    html body .mobile-action-item {
        display: flex;
        align-items: center;
        gap: 16px;
        padding: 16px 20px;
        color: #fff;
        font-size: 16px;
        font-weight: 500;
        cursor: pointer;
        border: none;
        background: transparent;
        width: 100%;
        text-align: left;
        transition: background-color 0.12s ease;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
        min-height: 52px;
    }

    html body .mobile-action-item:active {
        background: rgba(255, 255, 255, 0.06);
    }

    html body .mobile-action-item i {
        width: 24px;
        text-align: center;
        font-size: 18px;
        color: var(--doc-accent-cyan, #00f2fe);
        flex-shrink: 0;
    }

    html body .mobile-action-item.destructive { color: var(--doc-status-error); }
    html body .mobile-action-item.destructive i { color: var(--doc-status-error); }

    html body .mobile-action-divider {
        height: 1px;
        background: rgba(0, 242, 254, 0.06);
        margin: 4px 20px;
    }

    html body .mobile-action-cancel {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 16px 20px;
        color: var(--doc-accent-cyan, #00f2fe);
        font-size: 16px;
        font-weight: 600;
        cursor: pointer;
        border: none;
        background: transparent;
        width: 100%;
        border-top: 1px solid rgba(0, 242, 254, 0.08);
        min-height: 52px;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }

    html body .mobile-action-cancel:active {
        background: rgba(255, 255, 255, 0.06);
    }

/* =====================================================================
   §9  AI PANEL & COMING-SOON — Hidden
   ===================================================================== */

    html body .document-ai-panel {
        position: fixed;
        bottom: 0; left: 0; right: 0;
        max-height: 60vh;
        width: 100%;
        z-index: var(--z-sidebar, 300);
        background: var(--doc-bg-secondary, #151a35);
        border-top-left-radius: 20px;
        border-top-right-radius: 20px;
        border-top: 1px solid rgba(0, 242, 254, 0.12);
        box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.3);
        transform: translateY(100%);
        transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
        will-change: transform;
    }

    html body .document-ai-panel.open { transform: translateY(0); }

    html body .coming-soon-badge,
    html body .doc-btn-coming-soon { display: none; }

/* =====================================================================
   §10  MODE TOGGLE — Compact pill
   ===================================================================== */

    html body .top-mode-toggle-container {
        padding: 8px 16px;
        position: sticky;
        top: 0;
        z-index: var(--z-overlay, 800);
        background: var(--doc-bg-primary, #0a0e27);
        border-bottom: 1px solid rgba(0, 242, 254, 0.08);
        pointer-events: auto;
    }

    html body .mode-toggle-wrapper {
        width: 100%;
        max-width: 260px;
        margin: 0 auto;
        padding: 3px;
        gap: 3px;
        border-radius: 12px;
        background: var(--doc-bg-secondary, #151a35);
        border: 1px solid rgba(0, 242, 254, 0.1);
        box-shadow: none;
    }

    html body .mode-toggle-btn {
        flex: 1;
        padding: 10px 12px;
        font-size: 13px;
        font-weight: 600;
        /* Raised from 36px to 44px so Edit/Preview pill row matches the
           WCAG 2.5.5 touch-target floor. The container resizes around it. */
        min-height: 44px;
        height: 44px;
        gap: 6px;
        border-radius: 11px;
        pointer-events: auto;
        cursor: pointer;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
        border: none;
        transition: background-color 0.2s ease, color 0.2s ease;
        white-space: nowrap;
    }

    html body .mode-toggle-btn:hover { transform: none; }
    html body .mode-toggle-btn:active { transform: scale(0.97); }
    html body .mode-toggle-btn i { font-size: 13px; }

/* =====================================================================
   §11  TOUCH, TYPOGRAPHY, SAFE AREAS
   ===================================================================== */

    html body .doc-mobile-menu-toggle,
    html body .mobile-overflow-btn,
    html body .sidebar-close-btn,
    html body .document-item,
    html body .doc-tab-btn,
    html body .mobile-toolbar-btn,
    html body .mobile-toolbar-expand,
    html body .mobile-action-item,
    html body .mobile-action-cancel {
        touch-action: manipulation;
        user-select: none;
        -webkit-user-select: none;
    }

    /* doc-btn touch targets */
    html body .doc-btn {
        min-height: 44px;
        min-width: 44px;
        border-radius: 10px;
    }

    html body .desktop-only { display: none; }
    html body .toolbar-spacer { display: none; }

    /* Safe area insets */
    @supports (padding: max(0px)) {
        html body .document-header { padding-top: max(0px, env(safe-area-inset-top)); }
        html body #documents-view .document-sidebar {
            padding-top: max(8px, env(safe-area-inset-top));
        }
    }

/* =====================================================================
   §12  SCROLLBARS — Ultra-thin
   ===================================================================== */

    html body #documents-view .document-sidebar::-webkit-scrollbar,
    html body .markdown-editor::-webkit-scrollbar,
    html body .preview-pane::-webkit-scrollbar { width: 3px; }

    html body #documents-view .document-sidebar::-webkit-scrollbar-track,
    html body .markdown-editor::-webkit-scrollbar-track,
    html body .preview-pane::-webkit-scrollbar-track { background: transparent; }

    html body #documents-view .document-sidebar::-webkit-scrollbar-thumb,
    html body .markdown-editor::-webkit-scrollbar-thumb,
    html body .preview-pane::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.15); border-radius: 2px; }

} /* end main @media (max-width: 1024px) */

/* =====================================================================
   §13  LANDSCAPE & IMMERSIVE
   ===================================================================== */
@media screen and (max-width: 1024px) and (orientation: landscape) {
    html body .mobile-stats-bar { display: none; }
    html body .mobile-action-sheet { max-height: 80vh; }
    html body .markdown-editor,
    html body .preview-pane {
        padding-bottom: calc(var(--mobile-toolbar-h, 48px) + env(safe-area-inset-bottom, 0px) + 8px);
    }
}

@media screen and (max-width: 1024px) {
    /* Immersive mode — override immersive-mode.css height: 100dvh at (1,2,1) */
    html body.immersive-mode #documents-view.documents-view {
        height: calc(100dvh - var(--floating-nav-height-mobile, 58px));
        min-height: 0;
    }

    /* Keyboard open — hide toolbar stats, adjust */
    html body.keyboard-open #documents-view .mobile-stats-bar { display: none; }
    html body.keyboard-open #documents-view .mobile-toolbar-secondary { display: none; }
}

/* =====================================================================
   §14  DESKTOP — Hide mobile-only elements
   ===================================================================== */
@media screen and (min-width: 1025px) {
    html body .doc-mobile-menu-toggle { display: none; }
    html body #documents-view .mobile-bottom-toolbar { display: none; }
    html body .mobile-action-sheet[data-writer="doc"] { display: none; }
    html body .mobile-action-sheet-backdrop[data-writer="doc"] { display: none; }
    html body #documents-view .mobile-overflow-btn { display: none; }
    html body #documents-view .mobile-save-indicator { display: none; }
    html body .doc-sidebar-overlay { display: none; }
    html body #documents-view .sidebar-close-btn { display: none; }

    html body .save-indicator {
        display: flex;
        align-items: center;
        gap: 6px;
        font-size: 12px;
        color: var(--doc-text-muted, rgba(255,255,255,0.6));
    }

    html body .save-indicator .save-dot {
        width: 6px; height: 6px;
        border-radius: 50%;
        background: var(--doc-status-saved);
        transition: background-color 0.3s ease;
    }

    html body .save-indicator.saving .save-dot {
        background: #ffd60a;
        animation: pulse-dot 1s ease-in-out infinite;
    }

    html body .save-indicator.unsaved .save-dot { background: #f59e0b; }
}

/* =====================================================================
   §15  ANIMATIONS
   (pulse-dot defined in document-writer.css — not duplicated here)
   ===================================================================== */
@keyframes mobilePulseDot {
    0%, 100% { opacity: 0.5; transform: scale(1); }
    50% { opacity: 1; transform: scale(1.2); }
}

/* =====================================================================
   §16  REDUCED MOTION
   ===================================================================== */
@media (prefers-reduced-motion: reduce) {
    html body #documents-view .document-sidebar,
    html body .doc-sidebar-overlay,
    html body .mobile-bottom-toolbar,
    html body .mobile-action-sheet,
    html body .mobile-action-sheet-backdrop,
    html body .mobile-toolbar-secondary,
    html body .mobile-toolbar-expand,
    html body .mobile-toolbar-btn,
    html body .mobile-save-indicator,
    html body .doc-btn,
    html body .mode-toggle-btn,
    html body .preview-pane,
    html body .document-ai-panel {
        transition: none;
    }

    html body .save-indicator .save-dot,
    html body .mobile-save-indicator .save-dot-mini { animation: none; }
}
