/* This file is intentionally mobile-only. Palleon's tablet and desktop UI is untouched. */
@media only screen and (max-width: 767px) {
    :root {
        --tbmui-orange: #f29a00;
        --tbmui-bg: #111;
        --tbmui-panel: #171717;
        --tbmui-border: #343434;
        --tbmui-nav-height: 58px;
        --tbmui-tools-height: 76px;
    }

    body.tbmui-ready #palleon-body {
        padding: 0 0 calc(var(--tbmui-nav-height) + var(--tbmui-tools-height)) !important;
    }

    body.tbmui-ready.tbmui-properties-open #palleon-body,
    body.tbmui-ready.tbmui-layers-open #palleon-body {
        padding-bottom: var(--tbmui-nav-height) !important;
    }

    body.tbmui-ready #palleon-toggle-left,
    body.tbmui-ready #palleon-toggle-right {
        display: none !important;
    }

    /* Keep Palleon's library above our fixed mobile controls. Without this,
       the bottom navigation covers the template page numbers. */
    body.tbmui-ready .palleon-modal {
        z-index: 11000 !important;
    }

    body.tbmui-ready .palleon-modal-wrap {
        padding: 12px 10px calc(28px + env(safe-area-inset-bottom, 0px)) !important;
        overscroll-behavior: contain;
    }

    body.tbmui-ready #modal-add-new .palleon-modal-inner {
        min-height: calc(100dvh - 40px);
    }

    body.tbmui-ready #modal-template-library .palleon-pagination {
        position: sticky;
        z-index: 4;
        bottom: 0;
        padding: 10px 0 calc(8px + env(safe-area-inset-bottom, 0px));
        background: #121212;
    }

    body.tbmui-ready #modal-template-library .palleon-pagination ul {
        margin-top: 8px !important;
        justify-content: center !important;
        flex-wrap: wrap;
    }

    #tbmui-fit-screen {
        position: fixed;
        z-index: 10005;
        right: 12px;
        bottom: calc(var(--tbmui-nav-height) + var(--tbmui-tools-height) + 12px + env(safe-area-inset-bottom, 0px));
        width: 52px;
        height: 52px;
        padding: 5px 3px;
        border: 1px solid #4a4a4a;
        border-radius: 50%;
        background: #111;
        color: #fff;
        box-shadow: 0 5px 16px rgba(0,0,0,.45);
        font-size: 9px;
        font-weight: 700;
        line-height: 1;
        touch-action: manipulation;
    }

    #tbmui-fit-screen .material-icons {
        display: block;
        margin-bottom: 2px;
        color: var(--tbmui-orange);
        font-size: 22px;
    }

    body.tbmui-properties-open #tbmui-fit-screen,
    body.tbmui-layers-open #tbmui-fit-screen,
    body.tbmui-keyboard-open #tbmui-fit-screen {
        display: none;
    }

    /* Former left toolbar becomes a horizontal, swipeable mobile tool rail. */
    body.tbmui-ready #palleon-icon-menu {
        position: fixed !important;
        left: 0 !important;
        right: 0 !important;
        top: auto !important;
        bottom: var(--tbmui-nav-height) !important;
        width: 100% !important;
        height: var(--tbmui-tools-height) !important;
        padding: 0 8px !important;
        display: flex !important;
        flex-direction: row !important;
        align-items: stretch !important;
        gap: 2px;
        overflow-x: auto !important;
        overflow-y: hidden !important;
        z-index: 10003 !important;
        border-top: 1px solid var(--tbmui-border);
        border-right: 0 !important;
        scrollbar-width: none;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-x: contain;
    }

    body.tbmui-ready #palleon-icon-menu::-webkit-scrollbar {
        display: none;
    }

    body.tbmui-ready #palleon-icon-menu.closed {
        display: none !important;
    }

    body.tbmui-ready #palleon-icon-menu .palleon-icon-menu-btn,
    body.tbmui-ready #palleon-icon-menu .palleon-icon-menu-btn.stick-to-bottom {
        flex: 0 0 68px !important;
        min-width: 68px !important;
        height: var(--tbmui-tools-height) !important;
        margin: 0 !important;
        padding: 8px 4px !important;
        border-radius: 0 !important;
        justify-content: center !important;
    }

    body.tbmui-ready #palleon-icon-menu .palleon-icon-menu-title {
        display: block !important;
        max-width: 66px;
        padding-top: 4px !important;
        overflow: hidden;
        font-size: 10px !important;
        line-height: 1.1;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    body.tbmui-ready #palleon-icon-menu .palleon-icon-menu-btn.active {
        color: var(--tbmui-orange) !important;
        box-shadow: inset 0 -3px 0 var(--tbmui-orange);
    }

    /* Former left properties panel becomes a draggable-looking bottom sheet. */
    body.tbmui-ready #palleon-icon-panel {
        position: fixed !important;
        left: 0 !important;
        right: 0 !important;
        top: auto !important;
        bottom: var(--tbmui-nav-height) !important;
        width: 100% !important;
        max-width: none !important;
        height: min(38vh, 360px) !important;
        padding: 48px 0 0 !important;
        z-index: 10002 !important;
        overflow: hidden !important;
        border: 1px solid var(--tbmui-border) !important;
        border-bottom: 0 !important;
        border-radius: 20px 20px 0 0;
        box-shadow: 0 -14px 34px rgba(0,0,0,.46);
        transform: translateY(calc(100% + var(--tbmui-tools-height))) !important;
        visibility: hidden;
        transition: transform .22s ease, visibility 0s linear .22s;
    }

    body.tbmui-ready.tbmui-properties-open #palleon-icon-panel {
        display: block !important;
        transform: translateY(0) !important;
        visibility: visible;
        transition: transform .22s ease, visibility 0s;
    }

    body.tbmui-ready #palleon-icon-panel-inner {
        height: 100% !important;
        padding: 12px 16px 30px !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }

    .tbmui-sheet-header {
        position: absolute;
        z-index: 2;
        top: 0;
        left: 0;
        right: 0;
        height: 48px;
        display: flex;
        align-items: center;
        gap: 10px;
        padding: 13px 16px 8px;
        background: var(--tbmui-panel);
        border-radius: 20px 20px 0 0;
        border-bottom: 1px solid #292929;
    }

    .tbmui-sheet-header::before {
        content: "";
        position: absolute;
        top: 7px;
        left: 50%;
        width: 46px;
        height: 4px;
        margin-left: -23px;
        border-radius: 4px;
        background: #777;
    }

    .tbmui-sheet-title {
        margin-top: 5px;
        overflow: hidden;
        color: #fff;
        font-size: 15px;
        font-weight: 700;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .tbmui-sheet-minimise {
        width: 36px;
        height: 32px;
        margin: 5px 0 0 auto;
        padding: 0;
        border: 1px solid #3b3b3b;
        border-radius: 6px;
        background: #292929;
        color: #fff;
        font-size: 24px;
        line-height: 28px;
    }

    /* Layers use the same bottom sheet space, preserving all native controls. */
    body.tbmui-ready #palleon-right-col {
        display: flex !important;
        position: fixed !important;
        left: 0 !important;
        right: 0 !important;
        top: auto !important;
        bottom: var(--tbmui-nav-height) !important;
        width: 100% !important;
        height: min(42vh, 400px) !important;
        padding: 0 !important;
        overflow: hidden !important;
        z-index: 10004 !important;
        border: 1px solid var(--tbmui-border) !important;
        border-bottom: 0 !important;
        border-radius: 20px 20px 0 0;
        box-shadow: 0 -14px 34px rgba(0,0,0,.46);
        transform: translateY(calc(100% + var(--tbmui-nav-height))) !important;
        visibility: hidden;
        transition: transform .22s ease, visibility 0s linear .22s;
    }

    body.tbmui-ready.tbmui-layers-open #palleon-right-col {
        display: flex !important;
        transform: translateY(0) !important;
        visibility: visible;
        transition: transform .22s ease, visibility 0s;
    }

    body.tbmui-ready #palleon-right-col .palleon-layers-title {
        position: sticky;
        top: 0;
        z-index: 2;
        min-height: 50px;
        padding: 20px 16px 10px;
        background: var(--tbmui-panel);
        border-bottom: 1px solid #292929;
        flex: 0 0 auto;
    }

    body.tbmui-ready #palleon-right-col .palleon-layers-title::before {
        content: "";
        position: absolute;
        top: 7px;
        left: 50%;
        width: 46px;
        height: 4px;
        margin-left: -23px;
        border-radius: 4px;
        background: #777;
    }

    body.tbmui-ready #palleon-layers {
        flex: 1 1 auto !important;
        min-height: 0 !important;
        max-height: none !important;
        overflow-x: hidden !important;
        overflow-y: scroll !important;
        -webkit-overflow-scrolling: touch;
        touch-action: pan-y !important;
        overscroll-behavior: contain;
    }

    body.tbmui-ready #palleon-layers > li {
        position: relative;
    }

    .tbmui-layer-drag-handle {
        display: inline-flex;
        flex: 0 0 42px;
        width: 42px;
        min-width: 42px;
        height: 100%;
        min-height: 44px;
        margin: 0;
        padding: 0;
        align-items: center;
        justify-content: center;
        border: 0;
        border-right: 1px solid var(--tbmui-border);
        background: transparent;
        color: #929292;
        touch-action: none;
        user-select: none;
        -webkit-user-select: none;
    }

    .tbmui-six-dot-grip {
        display: grid !important;
        grid-template-columns: repeat(2, 4px);
        grid-template-rows: repeat(3, 4px);
        gap: 3px;
        width: 11px;
        height: 18px;
        pointer-events: none;
    }

    .tbmui-six-dot-grip > i {
        display: block !important;
        width: 4px;
        height: 4px;
        margin: 0 !important;
        border: 0 !important;
        border-radius: 50%;
        background: currentColor !important;
    }

    #palleon-layers > li.tbmui-layer-dragging {
        z-index: 4;
        opacity: .88;
        outline: 2px solid var(--tbmui-orange);
        background: #262626 !important;
        box-shadow: 0 8px 24px rgba(0,0,0,.42);
    }

    body.tbmui-ready #palleon-layer-delete-wrap {
        position: relative !important;
        bottom: auto !important;
        flex: 0 0 auto;
        margin-top: 0 !important;
        padding: 10px 14px !important;
    }

    body.tbmui-ready #palleon-add-text.tbmui-hide-when-editing {
        display: none !important;
    }

    /* Touch-friendly minus/value/plus steppers. */
    .tbmui-stepper {
        display: grid !important;
        grid-template-columns: 44px minmax(64px, 1fr) 44px;
        align-items: stretch;
        gap: 7px;
        width: 100%;
    }

    .tbmui-stepper > input[type="number"] {
        width: 100% !important;
        min-width: 0 !important;
        height: 44px !important;
        margin: 0 !important;
        text-align: center;
        appearance: textfield;
        -moz-appearance: textfield;
    }

    .tbmui-stepper > input[type="number"]::-webkit-inner-spin-button,
    .tbmui-stepper > input[type="number"]::-webkit-outer-spin-button {
        margin: 0;
        -webkit-appearance: none;
    }

    .tbmui-step-button {
        width: 44px;
        min-width: 44px;
        height: 44px;
        padding: 0;
        border: 1px solid #414141;
        border-radius: 7px;
        background: #2b2b2b;
        color: #fff;
        font-size: 25px;
        font-weight: 600;
        line-height: 42px;
        text-align: center;
        touch-action: manipulation;
        user-select: none;
        -webkit-user-select: none;
    }

    .tbmui-step-button.tbmui-plus {
        border-color: var(--tbmui-orange);
        background: var(--tbmui-orange);
        color: #111;
    }

    .tbmui-step-button:active {
        transform: scale(.94);
    }

    .tbmui-eyedropper-button {
        display: inline-flex;
        width: 42px;
        min-width: 42px;
        height: 42px;
        margin-left: 7px;
        padding: 0;
        align-items: center;
        justify-content: center;
        vertical-align: middle;
        border: 1px solid var(--tbmui-orange);
        border-radius: 7px;
        background: #292929;
        color: var(--tbmui-orange);
        touch-action: manipulation;
    }

    body.tbmui-ready .tbppf-eyedropper {
        display: none !important;
    }

    body.tbmui-ready .tbmui-eyedropper-button {
        display: none !important;
    }

    .tbmui-eyedropper-button .material-icons {
        font-size: 21px;
    }

    #tbmui-eyedropper-hint {
        position: fixed;
        z-index: 10020;
        top: 68px;
        left: 50%;
        max-width: calc(100vw - 30px);
        padding: 9px 14px;
        border-radius: 20px;
        background: var(--tbmui-orange);
        color: #111;
        font-size: 12px;
        font-weight: 700;
        white-space: nowrap;
        transform: translateX(-50%);
        box-shadow: 0 4px 18px rgba(0,0,0,.35);
    }

    body.tbmui-eyedropper-active #palleon-canvas-wrap {
        cursor: crosshair !important;
        touch-action: none !important;
    }

    /* Persistent mobile navigation. */
    #tbmui-mobile-nav {
        position: fixed;
        z-index: 10006;
        left: 0;
        right: 0;
        bottom: 0;
        height: var(--tbmui-nav-height);
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        padding-bottom: env(safe-area-inset-bottom, 0);
        background: #0b0b0b;
        border-top: 1px solid var(--tbmui-border);
    }

    .tbmui-nav-button {
        position: relative;
        min-width: 0;
        border: 0;
        border-right: 1px solid #242424;
        background: transparent;
        color: #c7c7c7;
        font-size: 10px;
        font-weight: 700;
        text-transform: uppercase;
        touch-action: manipulation;
    }

    .tbmui-nav-button:last-child {
        border-right: 0;
    }

    .tbmui-nav-button .material-icons {
        display: block;
        margin: 0 auto 2px;
        font-size: 21px;
    }

    .tbmui-nav-button.active {
        color: var(--tbmui-orange);
        box-shadow: inset 0 3px 0 var(--tbmui-orange);
    }

    #tbmui-layer-count {
        position: absolute;
        top: 4px;
        left: calc(50% + 8px);
        min-width: 17px;
        height: 17px;
        padding: 0 4px;
        border-radius: 9px;
        background: var(--tbmui-orange);
        color: #111;
        font-size: 10px;
        line-height: 17px;
    }

    body.tbmui-ready.tbmui-properties-open #palleon-content,
    body.tbmui-ready.tbmui-layers-open #palleon-content {
        align-items: flex-start !important;
        padding-top: 10px;
    }

    /* The virtual keyboard must not trigger a second fit-to-screen layout. */
    body.tbmui-ready.tbmui-keyboard-open #palleon-icon-panel {
        height: min(30vh, 250px) !important;
        transition: none !important;
    }

    body.tbmui-ready.tbmui-keyboard-open #palleon-content,
    body.tbmui-ready.tbmui-keyboard-open #palleon-canvas-wrap {
        transition: none !important;
    }

    body.tbmui-ready.tbmui-layers-open #palleon-icon-menu {
        display: none !important;
    }

    body.tbmui-ready.tbmui-properties-open #palleon-icon-menu {
        display: none !important;
    }

    body.tbmui-ready .select2-container {
        max-width: 100% !important;
    }
}

/* Final touch-device fallback. This intentionally has no viewport limit:
   iPad browsers can report a desktop-width layout when "Request Desktop Site"
   is active, but the body class is added only on a real touch device. */
body.tbmui-touch-ready #palleon-layers > li {
    position: relative;
}

body.tbmui-touch-ready #palleon-layers > li > .tbmui-layer-drag-handle {
    display: inline-flex !important;
    flex: 0 0 42px !important;
    width: 42px !important;
    min-width: 42px !important;
    max-width: 42px !important;
    height: 100% !important;
    min-height: 44px !important;
    margin: 0 !important;
    padding: 0 !important;
    align-items: center !important;
    justify-content: center !important;
    border: 0 !important;
    border-right: 1px solid rgba(128,128,128,.35) !important;
    background: transparent !important;
    color: #a9adb5 !important;
    opacity: 1 !important;
    visibility: visible !important;
    position: relative !important;
    inset: auto !important;
    z-index: 20 !important;
    order: -10 !important;
    touch-action: none !important;
    -webkit-user-select: none !important;
    user-select: none !important;
}

body.tbmui-touch-ready .tbmui-six-dot-grip {
    display: grid !important;
    grid-template-columns: repeat(2, 4px) !important;
    grid-template-rows: repeat(3, 4px) !important;
    gap: 3px !important;
    width: 11px !important;
    height: 18px !important;
    pointer-events: none !important;
}

body.tbmui-touch-ready .tbmui-six-dot-grip > i {
    display: block !important;
    width: 4px !important;
    height: 4px !important;
    margin: 0 !important;
    padding: 0 !important;
    border: 0 !important;
    border-radius: 50% !important;
    background: currentColor !important;
}

/* Tablet keeps Palleon's desktop layout; only touch-friendly numeric controls are added. */
@media only screen and (min-width: 768px) and (max-width: 1366px) {
    body.tbmui-touch-ready #palleon-layers {
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
        touch-action: pan-y !important;
        overscroll-behavior: contain;
    }

    body.tbmui-touch-ready #palleon-layers > li {
        position: relative;
    }

    body.tbmui-touch-ready .tbmui-layer-drag-handle {
        display: inline-flex !important;
        flex: 0 0 42px;
        width: 42px;
        min-width: 42px;
        max-width: 42px;
        min-height: 44px;
        margin: 0;
        padding: 0;
        align-items: center;
        justify-content: center;
        border: 0;
        border-right: 1px solid #414141;
        background: transparent;
        color: #929292;
        opacity: 1 !important;
        visibility: visible !important;
        position: relative !important;
        inset: auto !important;
        z-index: 20 !important;
        order: -10;
        touch-action: none;
        user-select: none;
        -webkit-user-select: none;
    }

    body.tbmui-touch-ready .tbmui-six-dot-grip {
        display: grid !important;
        grid-template-columns: repeat(2, 4px);
        grid-template-rows: repeat(3, 4px);
        gap: 3px;
        width: 11px;
        height: 18px;
        pointer-events: none;
    }

    body.tbmui-touch-ready .tbmui-six-dot-grip > i {
        display: block !important;
        width: 4px;
        height: 4px;
        margin: 0 !important;
        border: 0 !important;
        border-radius: 50%;
        background: currentColor !important;
    }

    body.tbmui-touch-ready #palleon-layers > li > .layer-name {
        min-width: 0 !important;
    }

    body.tbmui-touch-ready #palleon-layers > li.tbmui-layer-dragging {
        z-index: 4;
        opacity: .88;
        outline: 2px solid #f29a00;
    }

    body .tbppf-eyedropper,
    body .tbmui-eyedropper-button {
        display: none !important;
    }

    body.tbmui-touch-ready .tbmui-stepper {
        display: grid !important;
        grid-template-columns: 38px minmax(58px, 1fr) 38px;
        align-items: stretch;
        gap: 5px;
        width: 100%;
    }

    body.tbmui-touch-ready .tbmui-stepper > input[type="number"] {
        width: 100% !important;
        min-width: 0 !important;
        height: 38px !important;
        margin: 0 !important;
        text-align: center;
        appearance: textfield;
        -moz-appearance: textfield;
    }

    body.tbmui-touch-ready .tbmui-step-button {
        width: 38px;
        min-width: 38px;
        height: 38px;
        padding: 0;
        border: 1px solid #414141;
        border-radius: 5px;
        background: #2b2b2b;
        color: #fff;
        font-size: 21px;
        line-height: 36px;
        text-align: center;
        touch-action: manipulation;
    }

    body.tbmui-touch-ready .tbmui-step-button.tbmui-plus {
        border-color: #f29a00;
        background: #f29a00;
        color: #111;
    }
}
