/* 0. Remove custom cursor on mobile */
* {
    cursor: auto !important;
}

button,
a,
[role="button"],
.grid-btn,
.tab-btn {
    cursor: pointer !important;
}

#editor {
    cursor: text !important;
}

/* 1. Hide Desktop Action Bar and Status Bar */
.action-bar-wrapper {
    display: none !important;
}

.status-bar,
#btn-layout-toggle {
    display: none !important;
}

/* 2. Hide Fake Caret & Custom I-Beam Cursor logic since mobile keyboards don't need it. 
   Keep #fake-caret-overlay visible so .fake-selection highlights still work on mobile. */
.fake-caret,
.fake-caret-inline,
#custom-cursor {
    display: none !important;
}

/* 3. Layout Overrides for Mobile */
#editor-wrapper {
    padding-bottom: 0 !important;
    height: calc(100% - 40px) !important;
    /* Leave space for mobile footer */
}

.editor-container {
    padding: 0 !important;
    max-width: 100% !important;
}

#editor {
    padding: 15px 15px 0px 15px !important;
    font-size: 1.15rem;
}


#bottom-bg-block,
#btn-exit-focus {
    display: none !important;
}

/* 4. Mobile FAB Styles */
#mobile-fab {
    position: fixed !important;
    bottom: max(24px, env(safe-area-inset-bottom));
    right: 8px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--ui-bg);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
    border: 1px solid var(--ui-border);
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: transform 0.2s cubic-bezier(0.2, 0.8, 0.2, 1), background 0.2s, opacity 0.2s;
    pointer-events: auto;
    padding: 0;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    outline: none;
    transform: translateY(calc(-1 * var(--kb-offset, 0px))) rotate(var(--fab-rotate, 0deg));
}

[data-theme="dark"] #mobile-fab {
    background: rgba(48, 49, 52, 1);
}

[data-theme="warm"] #mobile-fab {
    background: rgba(245, 230, 149, 1);
}

#mobile-fab img {
    width: 32px;
    height: 32px;
    object-fit: contain;
    transition: transform 0.3s;
}

.fab-open #mobile-fab {
    --fab-rotate: 45deg;
}

/* 5. Mobile FAB Menus */
#mobile-fab-menu {
    position: fixed;
    bottom: 0;
    right: 0;
    left: 0;
    top: 0;
    max-width: none;
    pointer-events: none;
    z-index: 900;
    background: transparent;
}

#mobile-qt-trigger {
    position: fixed !important;
    bottom: calc(max(24px, env(safe-area-inset-bottom)) + 4px);
    /* slightly smaller 48px, so +4px vertically aligns with 56px FAB */
    right: 80px;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--ui-bg);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
    border: 1px solid var(--ui-border);
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.3s;
    pointer-events: none;
    opacity: 0;
    transform: translateY(calc(-1 * var(--kb-offset, 0px))) scale(0);
    cursor: pointer;
    font-size: 20px;
}

#mobile-qt-trigger.visible {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(calc(-1 * var(--kb-offset, 0px))) scale(1);
}

.fab-open #mobile-qt-trigger {
    opacity: 0 !important;
    pointer-events: none !important;
    transform: translateY(calc(-1 * var(--kb-offset, 0px))) scale(0) !important;
}

#quick-tip-popup.qt-mobile-anchored {
    top: auto !important;
    left: auto !important;
    right: 16px !important;
    bottom: calc(max(24px, env(safe-area-inset-bottom)) + 80px) !important;
    transform: translateY(calc(-1 * var(--kb-offset, 0px))) !important;
    transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.2s !important;
    max-height: calc(100vh - var(--kb-offset, 0px) - max(24px, env(safe-area-inset-bottom)) - 100px) !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    -webkit-overflow-scrolling: touch;
}

/* Make submenus accordion-style so they aren't clipped by overflow-y */
#quick-tip-popup.qt-mobile-anchored .qt-submenu {
    position: static !important;
    width: auto !important;
    box-shadow: none !important;
    border: none !important;
    border-radius: 0 !important;
    padding: 0 0 0 8px !important;
    margin: 4px 16px 4px 8px;
    background: transparent !important;
    border-left: 2px solid var(--ui-border) !important;
}

#quick-tip-popup.qt-mobile-anchored .has-submenu:hover>button.submenu-trigger span,
#quick-tip-popup.qt-mobile-anchored .has-submenu.kb-open>button.submenu-trigger span {
    transform: rotate(90deg);
    display: inline-block;
}

.action-group-horizontal {
    position: absolute;
    bottom: calc(max(24px, env(safe-area-inset-bottom)) + 9px);
    right: 8px;
    display: flex;
    flex-direction: row-reverse;
    align-items: center;
    gap: 10px;
    background: var(--ui-bg);
    border: 1px solid var(--ui-border);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    border-radius: 12px 28px 28px 12px;
    padding: 2px 56px 2px 2px;
    pointer-events: none;
    opacity: 0;
    transform: translateY(calc(-1 * var(--kb-offset, 0px))) translateX(20px) scale(0.9);
    transform-origin: calc(100% - 28px) center;
    transition: opacity 0.3s ease, transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.action-group-vertical {
    position: absolute;
    bottom: max(24px, env(safe-area-inset-bottom));
    right: 17px;
    display: flex;
    flex-direction: column-reverse;
    align-items: center;
    gap: 10px;
    background: var(--ui-bg);
    border: 1px solid var(--ui-border);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    border-radius: 12px 12px 28px 28px;
    padding: 2px 2px 56px 2px;
    pointer-events: none;
    opacity: 0;
    transform: translateY(calc(20px - var(--kb-offset, 0px))) scale(0.9);
    transform-origin: center calc(100% - 28px);
    transition: opacity 0.3s ease, transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.fab-open .action-group-horizontal {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(calc(-1 * var(--kb-offset, 0px))) translateX(0) scale(1);
}

.fab-open .action-group-vertical {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(calc(-1 * var(--kb-offset, 0px))) scale(1);
}

.action-group-horizontal>*,
.action-group-vertical>* {
    background: transparent;
    border: none;
    position: relative !important;
    width: 34px !important;
    height: 34px !important;
    min-width: 34px !important;
    min-height: 34px !important;
    max-width: 34px !important;
    max-height: 34px !important;
    box-sizing: border-box !important;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 !important;
    margin: 0 !important;
    cursor: pointer;
    border-radius: 50%;
    transition: background 0.2s;
    -webkit-tap-highlight-color: transparent;
    outline: none;
}

/* Dividers between buttons */
.action-group-horizontal>*:not(:last-child)::after {
    content: '';
    position: absolute;
    left: -5.5px;
    top: 50%;
    transform: translateY(-50%);
    width: 1px;
    height: 16px;
    background: var(--ui-border);
    opacity: 0.8;
}

.action-group-vertical>*:not(:last-child)::after {
    content: '';
    position: absolute;
    top: -5.5px;
    left: 50%;
    transform: translateX(-50%);
    width: 16px;
    height: 1px;
    background: var(--ui-border);
    opacity: 0.8;
}

#btn-link-action::after {
    display: none !important;
}

.action-group-horizontal>button,
.action-group-vertical>button {
    font-size: 1.1rem;
    color: var(--text-color);
}

.action-group-horizontal .dropdown>button,
.action-group-vertical .dropdown>button {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background: transparent;
    border: none;
    padding: 0;
    margin: 0;
    font-size: 1.1rem;
    border-radius: 50%;
    color: var(--text-color);
}

.action-group-horizontal>*:active,
.action-group-vertical>*:active,
.action-group-horizontal .dropdown>button:active,
.action-group-vertical .dropdown>button:active {
    background: rgba(128, 128, 128, 0.15);
}

.fab-open .action-group-horizontal,
.fab-open .action-group-vertical {
    opacity: 1;
    transform: translate(0, 0) scale(1);
    pointer-events: auto;
}

/* 1. Hide the explicit hide-mobile spans on the main buttons (File, Insert, Tools, Info) */
#mobile-fab-menu .hide-mobile,
body>.dropdown-content .hide-mobile {
    display: none !important;
}

/* 3. Portaled Dropdown — moved to document.body by mobile.js.
          JS sets position:fixed + top/left/maxHeight/maxWidth dynamically
          to avoid overlapping the FAB zone. CSS only sets base appearance. */
body>.dropdown-content {
    overflow-y: auto !important;
    z-index: 99999 !important;
    box-sizing: border-box !important;
}

/* 6. Mobile Footer */
#mobile-footer {
    position: fixed !important;
    bottom: 0;
    left: 0;
    right: 0;
    height: 24px;
    background: var(--ui-bg);
    border-top: 1px solid var(--ui-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    font-family: inherit;
    font-size: 0.75rem;
    color: var(--text-color);
    z-index: 1000;
}

/* 7. Focus Mode on Mobile */
body.mobile-focus-mode #mobile-footer {
    display: none !important;
}

body.mobile-focus-mode #editor-wrapper {
    height: 100% !important;
}

body.mobile-focus-mode #editor {
    padding-bottom: calc(56px + var(--kb-offset, 0px)) !important;
}

body.mobile-focus-mode #mobile-fab-menu:not(.fab-open) #mobile-fab {
    right: 0;
    left: auto;
    bottom: 0;
    border-radius: 100% 0 0 0;
    width: 48px;
    height: 48px;
    border-right: none;
    border-bottom: none;
    transition: all 0.3s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.2s;
}

#mobile-fab-menu:not(.fab-open) #mobile-fab {
    opacity: 0.2;
}

body.mobile-focus-mode #mobile-fab-menu.fab-open #mobile-fab {
    right: 8px;
    left: auto;
    bottom: max(24px, env(safe-area-inset-bottom));
    border-radius: 50%;
    width: 56px;
    height: 56px;
    border: 1px solid var(--ui-border);
    transition: all 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
}

/* 8. Landscape Mode Swap */
@media (orientation: landscape) {
    .action-group-horizontal {
        bottom: max(24px, env(safe-area-inset-bottom));
        right: 17px;
        flex-direction: column-reverse;
        align-items: center;
        padding: 2px 2px 56px 2px;
        border-radius: 12px 12px 28px 28px;
        transform: translateY(calc(20px - var(--kb-offset, 0px))) scale(0.9);
        transform-origin: center calc(100% - 28px);
    }

    .action-group-vertical {
        bottom: calc(max(24px, env(safe-area-inset-bottom)) + 9px);
        right: 8px;
        flex-direction: row-reverse;
        align-items: center;
        padding: 2px 56px 2px 2px;
        border-radius: 12px 28px 28px 12px;
        transform: translateY(calc(-1 * var(--kb-offset, 0px))) translateX(20px) scale(0.9);
        transform-origin: calc(100% - 28px) center;
    }

    .fab-open .action-group-horizontal {
        transform: translateY(calc(-1 * var(--kb-offset, 0px))) scale(1);
    }

    .fab-open .action-group-vertical {
        transform: translateY(calc(-1 * var(--kb-offset, 0px))) translateX(0) scale(1);
    }

    /* Swap Dividers for Landscape */
    .action-group-horizontal>*:not(:last-child)::after {
        left: 50%;
        top: -5.5px;
        transform: translateX(-50%);
        width: 16px;
        height: 1px;
    }

    .action-group-vertical>*:not(:last-child)::after {
        top: 50%;
        left: -5.5px;
        transform: translateY(-50%);
        width: 1px;
        height: 16px;
    }

    /* Dropdowns are universally centered now, no need to swap orientations! */
}

/* 9. Fix Sticky Hover on Touch Devices */
@media (hover: none) {
    #mobile-fab-menu button:not(#mobile-fab):hover {
        background: transparent !important;
    }

    #mobile-fab-menu button:not(#mobile-fab):active {
        background: rgba(128, 128, 128, 0.15) !important;
    }
}

/* 10. Remove native tap highlight and non-keyboard focus rings from all FAB buttons */
#mobile-fab,
#mobile-fab-menu button {
    -webkit-tap-highlight-color: transparent !important;
}

#mobile-fab:focus:not(:focus-visible),
#mobile-fab-menu button:focus:not(:focus-visible) {
    outline: none !important;
    box-shadow: none !important;
}

body {
    display: flex !important;
    flex-direction: column !important;
    height: 100vh !important;
    height: 100dvh !important;
}

#app-container {
    flex: 1 !important;
    order: 2 !important;
    height: auto !important;
}

/* 11. Minimal UI Find & Replace */
#floating-find-replace {
    position: static !important;
    order: 1 !important;
    width: 100% !important;
    max-width: 100% !important;
    transform: none !important; /* No translateY */
    border-radius: 0 0 16px 16px !important;
    max-height: 250px !important; /* Large enough to fit content */
    padding: 8px 10px !important;
    opacity: 1;
    visibility: visible;
    transition: max-height 0.3s ease, padding 0.3s ease, opacity 0.2s ease, visibility 0.3s ease !important;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1) !important;
    box-sizing: border-box !important;
    overflow: hidden !important;
    user-select: none !important;
    -webkit-user-select: none !important;
}

/* Force tap-highlight transparent on ALL children */
#floating-find-replace * {
    -webkit-tap-highlight-color: transparent !important;
}

/* Ensure inputs can still be selected */
#floating-find-replace input[type="text"] {
    user-select: auto !important;
    -webkit-user-select: auto !important;
}

/* Provide a native-feeling click effect for all interactive elements */
#floating-find-replace button:active,
#floating-find-replace label:active {
    opacity: 0.7 !important;
    transform: scale(0.95) !important;
    transition: transform 0.1s ease, opacity 0.1s ease !important;
}

/* Remove blue focus borders/rings on mouse/touch click */
#floating-find-replace button:focus:not(:focus-visible),
#floating-find-replace label:focus:not(:focus-visible),
#floating-find-replace input[type="checkbox"]:focus:not(:focus-visible) {
    outline: none !important;
    box-shadow: none !important;
}

/* Disable sticky hover effects on mobile devices */
#floating-find-replace .action-btn:hover {
    background: var(--bg-color) !important;
    color: var(--text-color) !important;
}

#floating-find-replace .icon-btn:hover {
    background: transparent !important;
}

#floating-find-replace.hidden {
    display: flex !important; /* Override standard .hidden (display: none) */
    max-height: 0 !important;
    padding-top: 0 !important;
    padding-bottom: 0 !important;
    border-width: 0 !important;
    opacity: 0 !important;
    visibility: hidden !important;
    margin: 0 !important;
}

.float-header {
    display: contents !important;
}

#fr-float-title {
    display: none !important;
}

#btn-fr-close-float {
    position: absolute !important;
    top: 6px !important;
    right: 6px !important;
    display: flex !important;
    padding: 4px !important;
}

.float-body {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: wrap !important;
    justify-content: flex-start !important;
    gap: 6px !important;
    margin-top: 0 !important;
    padding: 0 !important;
    padding-right: 26px !important; /* Space for absolute close button on first row */
}

/* Ensure rows wrap nicely */
.fr-row, .fr-col {
    display: flex !important;
    flex-direction: row !important;
    gap: 6px !important;
    align-items: center !important;
    justify-content: flex-start !important;
    margin-bottom: 0 !important;
    flex: 1 1 auto !important; /* Grow to fill row, but allow side-by-side */
}

/* Flatten the nested replace groups */
#fr-replace-group .fr-row {
    display: contents !important;
}

#fr-replace-group {
    flex-direction: row !important;
    flex-wrap: wrap !important;
    gap: 6px !important;
    align-items: center !important;
    justify-content: flex-start !important;
    position: relative !important;
    flex: 1 1 auto !important; /* Grow to fill row, but allow side-by-side */
}

/* Force hiding when JS sets inline style to display: none */
#fr-replace-group[style*="display: none"] {
    display: none !important;
}

/* Ensure Find and Replace inputs are properly sized and stretch to fill space */
#fr-find-input {
    min-width: 160px !important;
    flex: 1 1 160px !important; /* Grow if space allows */
}

/* Reorder Replace Group */
#fr-replace-input {
    order: 1 !important;
    min-width: 120px !important;
    flex: 1 1 120px !important; /* Grow to fill row */
    padding-right: 12px !important; /* Normal padding */
    border-top-right-radius: 0 !important;
    border-bottom-right-radius: 0 !important;
    z-index: 5 !important;
}

#fr-replace-input:focus {
    z-index: 12 !important; /* Bring input and its focus ring above the syn button */
}

#btn-fr-synonym {
    order: 2 !important;
    margin: 0 !important;
    margin-left: -6px !important; /* Close the 6px gap in flex container */
    transform: none !important; /* No visual pulling, actually attached */
    width: auto !important;
    flex: 1 1 46px !important; /* Dynamic sizing, same as rep/all */
    height: 33px !important; /* Standard input height */
    padding: 0 !important;
    display: flex !important;
    justify-content: center !important;
    align-items: center !important;
    z-index: 10 !important;
    background: var(--bg-color) !important;
    border: 1px solid var(--ui-border) !important;
    border-left: none !important; /* Join seamlessly with input's right border */
    border-top-left-radius: 0 !important;
    border-bottom-left-radius: 0 !important;
    border-top-right-radius: 12px !important; /* Match mobile input radius */
    border-bottom-right-radius: 12px !important;
    color: var(--text-color) !important;
    font-size: 0 !important; /* Hide original text */
}

#btn-fr-synonym::after {
    content: 'syn ▾' !important;
    font-size: 0.75rem !important;
    font-weight: 500 !important;
}

#btn-fr-replace {
    order: 3 !important;
    font-size: 0 !important;
    min-width: unset !important;
    width: auto !important;
    flex: 1 1 46px !important; /* Dynamic sizing */
    padding: 6px !important;
    margin-left: 0 !important; /* Remove negative margin hack */
    display: flex !important;
    justify-content: center !important;
}

#btn-fr-replace::after {
    content: 'rep' !important;
    font-size: 0.85rem !important;
}

#btn-fr-replace-all {
    order: 4 !important;
    font-size: 0 !important;
    min-width: unset !important;
    width: auto !important;
    flex: 1 1 46px !important; /* Dynamic sizing */
    padding: 6px !important;
    display: flex !important;
    justify-content: center !important;
}

#btn-fr-replace-all::after {
    content: 'all' !important;
    font-size: 0.85rem !important;
}

/* Make sure synonym list stays relative to the modal correctly */
#fr-synonym-list {
    order: 5 !important;
    position: absolute !important;
    top: 100% !important;
    left: 50% !important;
    transform: translateX(-50%) !important;
    min-width: 150px !important;
}

.fr-options {
    display: flex !important;
    flex-direction: row !important;
    gap: 6px !important;
    align-items: center !important;
    justify-content: flex-start !important;
    margin-top: 0 !important;
    flex-wrap: nowrap !important; /* Prevent checkboxes from wrapping */
    flex: 0 1 auto !important; /* Take only necessary space */
}

#floating-find-replace .fr-options label {
    font-size: 0.75rem !important;
    white-space: nowrap !important;
}

#fr-status {
    margin: 0 !important;
    margin-left: 6px !important;
    align-self: center !important;
    /* Move it down slightly so the text baseline matches the checkbox text baseline */
    transform: translateY(1px) !important;
    font-size: 0.75rem !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    flex: 1 1 0% !important; /* Take remaining space, but can shrink */
}

/* Sleeker inputs for minimal mode */
.modal-input {
    width: auto !important;
    min-width: 140px !important;
    padding: 6px 12px !important;
    border-radius: 12px !important;
}