/* ===========================================================================
   Urbios — mobile layout for Element Web
   ---------------------------------------------------------------------------
   Element Web 1.12.24 is desktop-first: 820 KB of theme CSS contains only EIGHT
   width-based media queries, and five of them style the login page. The app
   itself has no mobile layout — it renders two side-by-side columns on a 390 px
   screen, leaving ~195 px for the conversation.

   Injected by nginx (chat vhost), served from /branding/. No Element Web fork,
   no rebuild.

   THE DOM, as measured live on 1.12.24 (2026-10-04) — do not guess at this:

     mx_MatrixChat [390]
      └ div [390]
         ├ div [195@0]    > .mx_LeftPanel_panel > .mx_LeftPanel_outerWrapper   (room list)
         └ div [195@196]  > .mx_LeftPanel_panel > .mx_RoomView_wrapper         (timeline)

   ⚠ `.mx_LeftPanel_panel` is used for BOTH columns — it is a generic column
   class, NOT "the left panel". Hiding it by name blanks the timeline too.
   The only reliable way to tell the columns apart is by what they contain,
   hence the `:has()` selectors below.

   RULES FOR EDITING
   1. Everything stays inside `@media (max-width: 768px)`. Desktop must be
      untouched — that is what makes this safe to deploy to production.
   2. Distinguish columns by content (`:has()`), never by class name alone.
   3. Element's class names are not a public API. Recheck after every upgrade,
      the same as the nginx sub_filters.
   =========================================================================== */

@media (max-width: 768px) {
    /* --- 1. One column at a time ------------------------------------------
       `:has()` lets us detect "a room is open" in pure CSS (Safari 15.4+,
       Chrome 105+ — both below our iOS 16.4 floor). The back button that
       returns to the list is added by urbios-mobile.js. */

    /* ROOM OPEN → hide the room-list column, give the timeline the screen. */
    .mx_MatrixChat:has(.mx_RoomView) div:has(> .mx_LeftPanel_panel > .mx_LeftPanel_outerWrapper) {
        display: none !important;
    }
    .mx_MatrixChat:has(.mx_RoomView) div:has(> .mx_LeftPanel_panel > .mx_RoomView_wrapper),
    .mx_MatrixChat:has(.mx_RoomView) .mx_LeftPanel_panel:has(> .mx_RoomView_wrapper),
    .mx_MatrixChat:has(.mx_RoomView) .mx_RoomView_wrapper {
        flex: 1 1 100% !important;
        width: 100% !important;
        max-width: none !important;
        min-width: 0 !important;
    }

    /* NO ROOM OPEN → the room list is the whole screen. */
    .mx_MatrixChat:not(:has(.mx_RoomView)) div:has(> .mx_LeftPanel_panel > .mx_LeftPanel_outerWrapper),
    .mx_MatrixChat:not(:has(.mx_RoomView)) .mx_LeftPanel_panel:has(> .mx_LeftPanel_outerWrapper),
    .mx_MatrixChat:not(:has(.mx_RoomView)) .mx_LeftPanel_outerWrapper,
    .mx_MatrixChat:not(:has(.mx_RoomView)) .mx_LeftPanel_wrapper,
    .mx_MatrixChat:not(:has(.mx_RoomView)) .mx_LeftPanel,
    .mx_MatrixChat:not(:has(.mx_RoomView)) .mx_LeftPanel_wrapper--user,
    .mx_MatrixChat:not(:has(.mx_RoomView)) .mx_LeftPanel_wrapper--user[data-collapsed] {
        flex: 1 1 100% !important;
        width: 100% !important;
        max-width: none !important;
        min-width: 0 !important;
    }

    /* With no room open Element still renders the right-hand "home page". Measured: it overflowed
       to right:524 on a 390px screen (134px off the edge) and caused sideways scrolling. */
    .mx_MatrixChat:not(:has(.mx_RoomView)) div:has(> .mx_LeftPanel_panel > .mx_RoomView_wrapper),
    .mx_MatrixChat:not(:has(.mx_RoomView)) .mx_RoomView_wrapper,
    .mx_MatrixChat:not(:has(.mx_RoomView)) .mx_HomePage,
    .mx_MatrixChat:not(:has(.mx_RoomView)) .mx_HomePage_default_wrapper {
        display: none !important;
    }

    /* Never usable at this width — it would leave the timeline a few centimetres wide. */
    .mx_RightPanel,
    .mx_RightPanel_wrapper,
    .mx_SpacePanel {
        display: none !important;
    }

    /* A drag handle is meaningless on a touch screen and costs width. */
    .mx_ResizeHandle {
        display: none !important;
    }

    /* The divider that used to sit between the two columns. With the left column hidden it became a
       13px dead strip down the left of the screen, pushing all content right (measured:
       `.mx_Separator [13@0]`, room view starting at x=13 instead of 0). */
    .mx_Separator {
        display: none !important;
    }

    /* --- 2. Real viewport height ------------------------------------------
       100vh on iOS Safari excludes browser chrome, so the composer ends up under
       the address bar and keyboard. 100dvh tracks the visible area. */
    /* `--urbios-vh` is set by urbios-mobile.js from window.visualViewport, falling back to 100dvh
       where that is unavailable.

       Why dvh alone is not enough: on iOS, opening the keyboard does NOT shrink the layout
       viewport. 100dvh keeps reporting the full screen height, so the composer ends up UNDERNEATH
       the keyboard and Safari scrolls the whole page to bring the caret into view — which is why
       the composer appeared to "scroll with the screen" instead of staying put. visualViewport
       reports the area genuinely visible above the keyboard, so sizing to it keeps the composer
       pinned where it belongs. */
    .mx_MatrixChat_wrapper,
    .mx_MatrixChat {
        height: var(--urbios-vh, 100dvh) !important;
        max-height: var(--urbios-vh, 100dvh) !important;
    }

    /* --- 3. Notch / home-indicator safe areas ----------------------------- */
    .mx_MatrixChat_wrapper {
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }
    .mx_MessageComposer {
        padding-bottom: calc(0.5em + env(safe-area-inset-bottom)) !important;
    }

    /* --- 4. Toasts --------------------------------------------------------
       Two separate problems, both measured on a 390px screen:

       1. Positioned `left: 62px` to clear the desktop sidebar, which on a phone dropped them on
          top of the room list and off the right edge. Pin to both edges instead.
       2. Positioned `top: 12px` with a measured height of 187px, while .mx_RoomHeader is exactly
          64px tall and starts at y=0 — so the toast completely buried the header, hiding the back
          button and the call buttons. That is navigation, so it must stay reachable.

       Push the toast below the header. It now covers messages (which scroll) instead of controls
       (which do not). 72px = 64px header + 8px breathing room. */
    .mx_ToastContainer {
        left: 8px !important;
        right: 8px !important;
        top: 72px !important;
        width: auto !important;
        max-width: none !important;
        box-sizing: border-box !important;
    }

    /* --- 4b. Room-list "more options" (⋯) menu ----------------------------
       Element gates it behind hover:
         :is(._roomListItem_x:hover, ._roomListItem_x._keyboardActive_x,
             ._roomListItem_x[data-state=open]) ._hoverMenu_x { display: flex }
       There is no hover on a touchscreen, so on a phone the menu was unreachable — and with it
       **Favourite** (Element's name for "pin this conversation"), notification settings, mark as
       read, and leave room. Users reported this as "can't pin conversations".

       ⚠ Targeted by class-name PREFIX, not the full name. CSS modules emit `_<name>_<hash>_<line>`
       and the hash changes on every Element build, so `._hoverMenu_vaz9j_32` would silently stop
       matching after an upgrade. `[class*="_hoverMenu_"]` survives, because the readable prefix
       comes from the source. */
    /* --- 4a. Let the chat list SCROLL on touch ----------------------------
       The new room list supports drag-to-reorder (see its own a11y strings: drag_start, drag_end,
       drag_instructions). Drag libraries capture the pointer on touchstart, and because Element
       ships NO `touch-action` rule at all, the browser hands every vertical swipe to the drag
       handler instead of scrolling — so dragging a chat moved it rather than scrolling the list.

       `touch-action: pan-y` tells the browser "vertical panning belongs to me, don't wait for JS",
       which restores scrolling. The cost is that drag-to-reorder no longer works by touch — an
       acceptable trade, since reordering is a rare action and scrolling is constant. Reordering
       still works on desktop, where this media query does not apply. */
    /* Inspected live: the rows carry NO `draggable` attribute and no drag-library hooks
       (role="option" in a listbox, scroll container is an overflow-y:auto div four levels up).
       So what was "moving the chat" is **iOS's own native drag** — Safari lets you touch-and-hold
       an <img> (the room avatar) and slide it, which lifts the element instead of scrolling.
       -webkit-user-drag: none disables that. touch-action: pan-y is kept as belt-and-braces so the
       browser commits to vertical panning without waiting on JS. */
    [class*="_roomListItem_"],
    [class*="_roomListItem_"] * {
        touch-action: pan-y !important;
        -webkit-user-drag: none !important;
        -webkit-touch-callout: none !important;
        user-select: none !important;
        -webkit-user-select: none !important;
    }
    /* Avatars are the specific culprit - images are natively draggable on iOS. */
    [class*="_roomListItem_"] img {
        pointer-events: none !important;
    }

    /* The ⋯ menu.
       Element lays the row content out as  [name/preview] [⋯ menu] [notification badge]  inside a
       flex box with `justify-content: space-between`. On desktop that is fine because the ⋯ only
       appears on hover; once we force it visible on mobile, space-between parks it in the MIDDLE of
       the row, so it lands at a different x in every row depending on how long the name is.
       `margin-left: auto` overrides that and pins it to the right-hand end. */
    [class*="_hoverMenu_"] {
        display: flex !important;
        margin-left: auto !important;
        flex: 0 0 auto !important;
    }

    /* The opened ⋯ menu is rendered in a portal on <body>, outside both panels, so the one-panel-at-a-
       time rules above cannot reach it — but it can still end up clipped or underneath the panel on a
       narrow screen. Pin it above everything and keep it inside the viewport. */
    [role="menu"][data-state="open"],
    [data-radix-popper-content-wrapper] {
        z-index: 2147483000 !important;
        max-width: calc(100vw - 16px) !important;
        visibility: visible !important;
        opacity: 1 !important;
    }

    /* Radix unmounts closed menus, so this only ever applies to one that is genuinely open — it cannot
       resurrect a stale menu. Guards against an inherited `display:none` from the panel rules above. */
    [role="menu"][data-state="open"] {
        display: block !important;
    }

    /* ...and the badge next to it collapses to zero width when a room has no unread/mute icon, which
       would still shift the ⋯ by one icon between rows. Reserve the space so the column is straight. */
    [class*="_notificationDecoration_"] {
        min-width: 20px !important;
        display: flex !important;
        justify-content: flex-end !important;
        flex: 0 0 auto !important;
    }

    /* Keep ONLY the first control in that menu (the ⋯ itself).
       Element's hover menu also carries a notification bell, and Element separately hides the mute
       indicator while the menu is showing:
         :is(._roomListItem_x:hover, …) ._notificationDecoration_x { display: none }
       Because we force the menu visible *without* hover, that hide never fires — so every row
       rendered TWO bells, one from the menu and one from the indicator (seen on a real device).
       Dropping the extra controls keeps one compact ⋯, preserves the mute indicator, and gives the
       room name back the width it was losing to truncation. */
    [class*="_hoverMenu_"] > :nth-child(n + 2) {
        display: none !important;
    }

    /* 40px was too chunky at this width and truncated room names; 32 still exceeds the ~28px
       Element ships and stays comfortably tappable. */
    [class*="_hoverMenu_"] [class*="_icon-button_"] {
        min-width: 32px !important;
        min-height: 32px !important;
        width: 32px !important;
        height: 32px !important;
    }

    /* --- 5. Tap targets (Apple's minimum is 44x44; Element's are ~28) ------ */
    .mx_AccessibleButton,
    .mx_RoomHeader button,
    .mx_MessageComposer button,
    .mx_RoomTile {
        min-height: 44px;
    }
    .mx_RoomHeader button,
    .mx_MessageComposer button {
        min-width: 44px;
    }
    .mx_RoomTile {
        padding-top: 6px !important;
        padding-bottom: 6px !important;
    }

    /* The room header's button row does not fit. Measured on a 390px screen: the children run to
       472px inside a 378px header — back button, avatar, name, FOUR icon buttons (video, voice,
       threads, info) and a member count. `overflow:hidden` alone just sliced the last icon in half,
       which looks broken.
       Drop the last two items instead. Threads and room-info both open the right panel, which we
       hide on mobile anyway, so they were dead buttons. Using :nth-last-child keeps this correct
       whether or not our back button has been injected yet. */
    .mx_RoomHeader {
        overflow: hidden !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
        gap: 2px !important;
    }
    .mx_RoomHeader > :nth-last-child(-n + 2) {
        display: none !important;
    }

    /* --- 6. Composer ------------------------------------------------------
       16px minimum or iOS zooms the page when the input is focused. */
    .mx_MessageComposer {
        padding-left: 4px !important;
        padding-right: 4px !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
    }
    .mx_MessageComposer_wrapper {
        padding-left: 8px !important;
        padding-right: 8px !important;
    }
    .mx_BasicMessageComposer_input,
    .mx_MessageComposer_input textarea,
    .mx_MessageComposer input[type="text"] {
        font-size: 16px !important;
    }

    /* --- 7. Dialogs full-screen (they clip their own buttons otherwise) ---- */
    .mx_Dialog {
        width: 100vw !important;
        max-width: 100vw !important;
        height: 100dvh !important;
        max-height: 100dvh !important;
        border-radius: 0 !important;
        padding: 16px !important;
        box-sizing: border-box !important;
        overflow-y: auto !important;
    }
    .mx_Dialog_fixedWidth {
        width: 100% !important;
        max-width: 100% !important;
    }

    /* --- 8. Timeline -------------------------------------------------------
       Measured on a 390px screen before this block: .mx_EventTile_line carries
       `padding-left: 64px` for the avatar column, .mx_DisambiguatedProfile a
       matching 64px margin, and .mx_EventTile_msgOption (hover actions + read
       receipt) takes 90px on the right. Net result: the message text was 167px
       wide — 43% of the screen. Reclaim most of it. */
    .mx_EventTile {
        padding-left: 4px !important;
        padding-right: 4px !important;
    }
    /* Full width accounting measured on a 390px screen before this block:
         376  .mx_RoomView_MessageList   padding 18px each side   -36px
         252  .mx_EventTile_line         margin-right 80px        -80px
         208  .mx_EventTile_body                                  = 53% of the screen
       The 80px right margin reserves room for the hover action bar, which we hide on touch (there
       is no hover) — .mx_EventTile_msgOption measured just 8px wide, so nearly all of it was empty.
       Reclaiming both gives the message text ~300px instead of 208px. */
    .mx_RoomView_MessageList {
        padding-left: 4px !important;
        padding-right: 4px !important;
    }
    .mx_EventTile_line {
        padding-left: 44px !important;
        /* keep a little room for the read receipt, which still lives in msgOption */
        margin-right: 16px !important;
    }
    .mx_DisambiguatedProfile {
        margin-left: 44px !important;
    }
    /* Hover actions are meaningless on touch (there is no hover); the read receipt
       inside still renders, just without reserving a 90px column for buttons. */
    .mx_EventTile_msgOption {
        width: auto !important;
        min-width: 0 !important;
        margin-left: 0 !important;
    }
    .mx_EventTile_msgOption .mx_MessageActionBar {
        display: none !important;
    }
    /* Long URLs and code blocks otherwise force the whole page to scroll
       sideways, which makes the app feel broken. */
    .mx_EventTile_content,
    .mx_EventTile_body {
        overflow-wrap: anywhere;
        word-break: break-word;
    }
    .mx_EventTile_body pre {
        max-width: 100%;
        overflow-x: auto;
    }

    /* --- 9. Never scroll the page itself ---------------------------------- */
    html,
    body {
        overflow-x: hidden !important;
        overscroll-behavior-y: none;
    }

    /* --- 10. Our injected back button -------------------------------------- */
    .urbios_backButton {
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
        min-width: 44px;
        min-height: 44px;
        margin-right: 4px;
        border: none;
        background: transparent;
        cursor: pointer;
        font-size: 24px;
        line-height: 1;
        color: inherit;
        flex: 0 0 auto;
    }
}

/* The back button only makes sense in the narrow one-column layout. */
@media (min-width: 769px) {
    .urbios_backButton {
        display: none !important;
    }
}

/* ===========================================================================
   Telegram-style "@" on mention badges — ALL widths, not just mobile.
   ---------------------------------------------------------------------------
   Element already behaves correctly underneath: `.m.rule.is_user_mention` is an
   OVERRIDE push rule (verified enabled on this server; actions = notify +
   set_tweak=highlight + sound), so a mention highlights wherever notifications
   are allowed at all.

   ⚠ Corrected 2026-10-07. This comment used to claim a mention highlights "even
   in a muted room". It does NOT. Element's Mute is ITSELF an override rule
   (actions: []) in the same priority band, and it suppresses the mention rule
   with everything else — no badge, no sound, no colour. Proven on a live
   account. The setting that preserves mentions is "Mentions & keywords", which
   is a ROOM-band rule that the override mention rule outranks.

   What Element does NOT do is label it. A mention appears only as a RED badge
   (`._notificationBadge_` + `._highlight_` → --cpd-color-icon-critical-primary):
   visually just a differently-coloured unread count. Users reporting "I can't
   see where I was tagged" are reading it correctly — there is no "@" anywhere.

   Adding the glyph is free and makes a mention unmistakable: "@3".

   ⚠ WEB ONLY. This file is injected by nginx into chat.ur-bios.com. The Urbios
   desktop app does not load it, so the "@" will not appear there until the same
   rule is added to the desktop fork. */
[class*="_notificationBadge_"][class*="_highlight_"]::before {
    content: "@";
    margin-right: 1px;
    font-weight: 700;
}

/* ===========================================================================
   Timeline and composer readability — ALL widths.
   ---------------------------------------------------------------------------
   Three separate complaints, three distinct causes, each verified against the
   shipped 1.12.24 stylesheet rather than guessed.
   =========================================================================== */

/* 1. Messages run together with no visual break.
      Element HAS a divider rule — `.mx_EventTile + .mx_EventTile { border-top }` —
      but it is scoped to `.mx_FilePanel`, so the main timeline never gets it.
      We apply the same treatment where it is actually wanted: between sender
      groups. `.mx_EventTile_continuation` marks follow-on messages from the same
      person, so :not() targets exactly the first message of each group. */
.mx_RoomView_MessageList .mx_EventTile:not(.mx_EventTile_continuation):not(:first-child) {
    margin-top: var(--cpd-space-3x);
    padding-top: var(--cpd-space-3x);
    border-top: 1px solid var(--cpd-color-gray-300);
}

/* 2. Put the composer in a box, the way Telegram and WhatsApp do.
      This also solves "no separation from the timeline" — a bordered, tinted box
      separates far more clearly than a rule, and the two together would look wrong.

      Element is already most of the way there: it sets `border-radius: 8px` on
      `.mx_MessageComposer`, then fills it with `--cpd-color-bg-canvas-default`,
      the SAME colour as the page behind it, and gives it no border. So the box
      exists and is simply invisible. We give it a contrasting fill, a real border
      and a rounder corner.

      ⚠ `.mx_MessageComposer_wrapper` is a CHILD of `.mx_MessageComposer` (see
      `.mx_MessageComposer_wysiwyg .mx_MessageComposer_wrapper` in the shipped
      theme), so its `border-top` would draw a stray line INSIDE the new box.
      Element ships it as `2px hidden` — invisible but space-reserving — and we
      keep it off deliberately. */
.mx_MessageComposer {
    /* ⚠ `width: auto` is load-bearing. Element sets `.mx_MessageComposer { width: 100% }`, and
       margins sit OUTSIDE the declared width, so 100% plus our side margins made the box
       100% + 24px wide — it ran off the right edge and took the attachment button with it.
       box-sizing only governs padding and border, never margin, so it cannot save us here.
       As a flex item, `auto` stretches to the container minus margins, which is what we want. */
    width: auto !important;

    /* The bottom margin also clears the iPhone home indicator. The web app is installed to the
       home screen with `apple-mobile-web-app-status-bar-style: black-translucent`, so it draws
       edge to edge and the composer would otherwise sit underneath the indicator and be clipped.
       env() resolves to 0px on every device without an inset, so this is safe everywhere. */
    margin: var(--cpd-space-2x) var(--cpd-space-3x)
        calc(var(--cpd-space-3x) + env(safe-area-inset-bottom, 0px)) !important;

    padding: var(--cpd-space-1x) var(--cpd-space-3x) !important;
    background-color: var(--cpd-color-bg-subtle-secondary) !important;
    border: 1px solid var(--cpd-color-border-interactive-secondary) !important;
    border-radius: 20px !important;
    box-sizing: border-box !important;
}

/* Telegram-like focus cue: the box brightens when you are typing in it. */
.mx_MessageComposer:focus-within {
    border-color: var(--cpd-color-border-interactive-primary) !important;
}

.mx_MessageComposer_wrapper {
    border-top: none !important;
    padding-top: 0 !important;
}

/* 3. Typing a long message scrolls away from the caret.
      Element's composer input is
      `{ margin: auto 0; max-height: 140px; overflow-y: auto; }`.
      `margin: auto` on a flex item whose content overflows is the long-standing
      flexbox bug where the scroll container will not track the caret — the view
      sticks at the top while you type past the fold. Removing the auto margin
      restores normal caret-following; the taller cap simply means you hit the
      limit far less often. */
.mx_SendMessageComposer .mx_BasicMessageComposer .mx_BasicMessageComposer_input {
    margin: 0 !important;
    max-height: 40vh !important;
    scroll-padding-block: var(--cpd-space-2x);
}
