/*
 * Remote Codex mobile adapter v8 (baseline derived from v7)
 * Compact Material-style shell for phones/tablets and Chinese nine-key IMEs.
 * Class-gated (body.relay-mobile): landscape phones get the mobile layout
 * too; the desktop layout is only restyled in narrow windows.
 */

@media all {
  body.relay-mobile {
    --relay-space-1: 4px;
    --relay-space-2: 8px;
    --relay-space-3: 12px;
    --relay-radius-sm: 8px;
    --relay-radius-md: 12px;
    --relay-touch: 40px;
    --relay-touch-primary: 44px;
    --relay-ui-font: 15px;
    --relay-message-font: 17px;
    --relay-input-font: 18px;
    --relay-small-font: 13px;
    --relay-terminal-font: 13px;
    --relay-left-drawer: min(82vw, 320px);
    --relay-right-drawer: clamp(168px, 48vw, 220px);

    position: fixed;
    inset: 0;
    margin: 0;
    overflow: hidden;
    overscroll-behavior: none;
    -webkit-text-size-adjust: 100%;
    text-rendering: optimizeLegibility;
  }

  body.relay-mobile *,
  body.relay-mobile *::before,
  body.relay-mobile *::after {
    box-sizing: border-box;
  }

  body.relay-mobile :where(button, a, input, textarea, [role="button"]) {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  body.relay-mobile :where(button, a, input, textarea, [role="button"]):focus-visible {
    outline: 2px solid var(--theme-accent, #4f7cff)!important;
    outline-offset: -2px;
  }

  body.relay-mobile :where(input:not([type="checkbox"]):not([type="radio"]), textarea, [contenteditable="true"]) {
    font-size: max(16px, var(--relay-input-font))!important;
  }

  body.relay-mobile #root,
  body.relay-mobile .thread-ui-shell {
    position: fixed!important;
    inset: var(--relay-mobile-vv-top, 0px) 0 auto 0!important;
    width: 100%;
    height: var(--relay-mobile-vvh, 100dvh)!important;
    min-height: var(--relay-mobile-vvh, 100dvh)!important;
    max-height: var(--relay-mobile-vvh, 100dvh)!important;
    overflow: hidden!important;
  }

  body.relay-mobile .thread-ui-shell .matter-workbench,
  body.relay-mobile .thread-ui-shell .matter-workbench.is-sidebar-hidden {
    display: grid!important;
    width: 100%;
    height: 100%;
    min-width: 0;
    grid-template-columns: minmax(0, 1fr)!important;
    grid-template-rows: calc(40px + env(safe-area-inset-top, 0px)) minmax(0, 1fr)!important;
    overflow: hidden;
    font-size: var(--relay-ui-font)!important;
  }

  body.relay-mobile .thread-ui-shell .matter-rail {
    display: none!important;
  }

  body.relay-mobile .thread-ui-shell :is(.matter-main, .matter-content, .matter-chat, .thread-graph-chat-panel) {
    min-width: 0;
    max-width: 100vw;
    overflow-x: hidden;
  }

  body.relay-mobile .thread-ui-shell .matter-main {
    grid-column: 1!important;
  }

  /* Compact app bar with usable touch targets. */
  body.relay-mobile .thread-ui-shell .matter-topbar {
    grid-column: 1!important;
    height: calc(40px + env(safe-area-inset-top, 0px))!important;
    min-height: calc(40px + env(safe-area-inset-top, 0px))!important;
    padding: env(safe-area-inset-top, 0px) max(2px, env(safe-area-inset-right, 0px)) 0 max(2px, env(safe-area-inset-left, 0px))!important;
    gap: 0!important;
    border-bottom: 1px solid var(--theme-border);
    background: color-mix(in srgb, var(--matter-chrome) 96%, transparent)!important;
    -webkit-backdrop-filter: blur(12px) saturate(120%);
    backdrop-filter: blur(12px) saturate(120%);
    z-index: 70;
  }

  body.relay-mobile .thread-ui-shell .matter-topbar :is(button, a),
  body.relay-mobile .thread-ui-shell .matter-topbar .matter-search-trigger,
  body.relay-mobile .thread-ui-shell .matter-connection .device-connection-button {
    width: var(--relay-touch)!important;
    min-width: var(--relay-touch)!important;
    height: var(--relay-touch)!important;
    min-height: var(--relay-touch)!important;
    margin: 0!important;
    padding: 0!important;
    border-radius: var(--relay-radius-sm)!important;
  }

  body.relay-mobile .thread-ui-shell .matter-topbar svg {
    width: 17px!important;
    height: 17px!important;
  }

  body.relay-mobile .thread-ui-shell :is(.matter-topbar-brand, .matter-topbar-separator, .matter-search-trigger span, .matter-topbar [aria-label="Go forward"]) {
    display: none!important;
  }

  body.relay-mobile .thread-ui-shell .matter-topbar-end {
    min-width: 0;
    margin-left: auto;
    gap: 0!important;
  }

  /* Left navigation is a dismissible drawer. */
  body.relay-mobile .thread-ui-shell .matter-sidebar {
    position: fixed!important;
    inset: calc(40px + env(safe-area-inset-top, 0px)) auto 0 0!important;
    width: var(--relay-left-drawer)!important;
    min-width: 0!important;
    padding: 4px 7px max(6px, env(safe-area-inset-bottom, 0px))!important;
    visibility: hidden;
    transform: translate3d(-104%, 0, 0);
    transition: transform 180ms cubic-bezier(.2, 0, 0, 1), visibility 180ms;
    border-right: 1px solid var(--theme-border-strong);
    border-radius: 0 16px 16px 0;
    box-shadow: 14px 0 36px rgb(0 0 0 / 24%);
    overscroll-behavior: contain;
    z-index: 65;
  }

  body.relay-mobile .thread-ui-shell .matter-sidebar.is-open,
  body.relay-mobile .thread-ui-shell .matter-sidebar[aria-hidden="false"] {
    visibility: visible;
    transform: translate3d(0, 0, 0);
  }

  body.relay-mobile .thread-ui-shell .matter-sidebar-scrim {
    display: none!important;
    position: fixed!important;
    inset: calc(40px + env(safe-area-inset-top, 0px)) 0 0!important;
    border: 0;
    background: rgb(0 0 0 / 38%)!important;
    z-index: 64!important;
  }

  body.relay-mobile.relay-sidebar-open .thread-ui-shell .matter-sidebar-scrim {
    display: block!important;
  }

  body.relay-mobile .thread-ui-shell .matter-sidebar-heading,
  body.relay-mobile .thread-ui-shell .matter-section-heading {
    min-height: 36px!important;
    padding-inline: 7px!important;
    border-radius: var(--relay-radius-sm)!important;
    font-size: var(--relay-message-font)!important;
  }

  body.relay-mobile .thread-ui-shell .matter-sidebar-heading button {
    width: 36px!important;
    min-width: 36px!important;
    height: 36px!important;
    min-height: 36px!important;
  }

  body.relay-mobile .thread-ui-shell .matter-thread-section {
    margin-bottom: 4px!important;
  }

  body.relay-mobile .thread-ui-shell .matter-thread-row {
    min-height: 42px!important;
    margin-block: 1px!important;
    padding: 3px 8px!important;
    gap: 7px!important;
    border-radius: 10px!important;
  }

  body.relay-mobile .thread-ui-shell .matter-thread-copy {
    gap: 1px!important;
    font-size: var(--relay-ui-font)!important;
    line-height: 19px!important;
  }

  body.relay-mobile .thread-ui-shell .matter-thread-copy small {
    font-size: var(--relay-small-font)!important;
    line-height: 15px!important;
  }

  /* Thread tabs remain one compact horizontal strip. */
  body.relay-mobile .thread-ui-shell :is(.matter-tabs-row, .matter-thread-tabs) {
    height: 34px!important;
    min-height: 34px!important;
  }

  body.relay-mobile .thread-ui-shell .matter-thread-tabs {
    overflow-x: auto!important;
    overflow-y: hidden!important;
    scroll-snap-type: x proximity;
    scrollbar-width: none;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
  }

  body.relay-mobile .thread-ui-shell .matter-thread-tabs::-webkit-scrollbar,
  body.relay-mobile .thread-ui-shell .thread-graph-composer-toolbar::-webkit-scrollbar {
    display: none;
  }

  body.relay-mobile .thread-ui-shell .matter-thread-tabs > :is(a, button) {
    min-width: 76px!important;
    max-width: 148px!important;
    min-height: 34px!important;
    padding-inline: 8px!important;
    gap: 6px!important;
    scroll-snap-align: start;
    font-size: 14px!important;
  }

  body.relay-mobile .thread-ui-shell :is(.matter-new-thread, .matter-toolbar-toggle) {
    width: 34px!important;
    min-width: 34px!important;
    height: 34px!important;
    min-height: 34px!important;
    margin: 0!important;
  }

  body.relay-mobile .thread-ui-shell .matter-breadcrumb {
    height: 36px!important;
    min-height: 36px!important;
    padding-inline: 2px!important;
    gap: 1px!important;
    background: color-mix(in srgb, var(--theme-surface) 96%, transparent)!important;
  }

  body.relay-mobile .thread-ui-shell .matter-breadcrumb :is(button, a) {
    width: 36px!important;
    min-width: 36px!important;
    height: 36px!important;
    min-height: 36px!important;
    border-radius: var(--relay-radius-sm)!important;
  }

  body.relay-mobile .thread-ui-shell :is(.matter-current-title, .matter-breadcrumb > svg) {
    display: none!important;
  }

  body.relay-mobile .thread-ui-shell .matter-workspace-path {
    flex: 1;
    max-width: none!important;
    padding-inline: 4px;
    font-size: 14px!important;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Conversation content uses the whole available width. */
  body.relay-mobile .thread-ui-shell .matter-chat {
    --matter-reading-width: 100%;
  }

  body.relay-mobile .thread-ui-shell .thread-graph-message-list {
    width: 100%!important;
    max-width: 100%!important;
    margin: 0!important;
    padding-inline: 9px!important;
  }

  body.relay-mobile .thread-ui-shell .thread-graph-turn {
    padding-inline: 0!important;
  }

  body.relay-mobile .thread-ui-shell :is(.thread-message-prose, .thread-graph-message-bubble) {
    max-width: 100%;
    font-size: var(--relay-message-font)!important;
    line-height: 1.52!important;
    overflow-wrap: anywhere;
  }

  body.relay-mobile .thread-ui-shell :is(.matter-command-step, .thread-graph-history-detail-row, .thread-history-direct-row, .thread-graph-worked-summary, .thread-graph-worked-label, .thread-graph-history-group-summary > span) {
    font-size: var(--relay-ui-font)!important;
  }

  body.relay-mobile .thread-ui-shell :is(.matter-step-number, .matter-step-time) {
    font-size: var(--relay-small-font)!important;
  }

  body.relay-mobile .thread-ui-shell :is(pre, .thread-diff, .thread-graph-code-block) {
    max-width: calc(100vw - 18px)!important;
    overflow-x: auto!important;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    white-space: pre;
    font-size: var(--relay-terminal-font)!important;
  }

  body.relay-mobile .thread-ui-shell table {
    display: block;
    max-width: calc(100vw - 18px)!important;
    overflow-x: auto!important;
    -webkit-overflow-scrolling: touch;
  }

  body.relay-mobile .thread-ui-shell .prose {
    min-width: 0;
    overflow-wrap: anywhere;
  }

  /* The visual viewport owns keyboard movement; no second bottom offset is added. */
  body.relay-mobile .thread-ui-shell .thread-graph-scroll-container {
    scroll-padding-bottom: calc(86px + env(safe-area-inset-bottom, 0px));
    overscroll-behavior-y: contain;
  }

  body.relay-mobile .thread-ui-shell .thread-graph-composer-host {
    bottom: max(3px, env(safe-area-inset-bottom, 0px))!important;
  }

  body.relay-mobile .thread-ui-shell .thread-graph-composer-layer {
    width: 100%!important;
    max-width: 100%!important;
    padding-inline: max(4px, env(safe-area-inset-left, 0px), env(safe-area-inset-right, 0px))!important;
  }

  body.relay-mobile .thread-ui-shell .thread-graph-composer-form {
    padding: 0!important;
    border: 0!important;
    background: transparent!important;
  }

  body.relay-mobile .thread-ui-shell .thread-graph-composer-shell {
    padding: 4px 5px 3px!important;
    border: 1px solid var(--theme-border-strong)!important;
    border-radius: 14px!important;
    background: color-mix(in srgb, var(--theme-panel) 96%, transparent)!important;
    -webkit-backdrop-filter: blur(12px) saturate(115%);
    backdrop-filter: blur(12px) saturate(115%);
    box-shadow: 0 6px 20px rgb(0 0 0 / 20%)!important;
  }

  body.relay-mobile .thread-ui-shell .thread-graph-composer-input {
    min-height: 38px!important;
    max-height: min(17dvh, 96px)!important;
    padding: 1px 42px 1px 3px!important;
    overflow-y: auto!important;
    font-size: var(--relay-input-font)!important;
    line-height: 1.4!important;
  }

  body.relay-mobile .thread-ui-shell .thread-graph-composer-toolbar {
    gap: 2px!important;
    padding-top: 1px!important;
    flex-wrap: nowrap!important;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
  }

  body.relay-mobile .thread-ui-shell .thread-graph-composer-toolbar > div {
    flex-shrink: 0;
  }

  body.relay-mobile .thread-ui-shell :is(.thread-graph-composer-icon-button, .thread-graph-composer-stop-button) {
    width: 36px!important;
    min-width: 36px!important;
    height: 36px!important;
    min-height: 36px!important;
  }

  body.relay-mobile .thread-ui-shell .thread-graph-composer-send-button {
    width: var(--relay-touch-primary)!important;
    min-width: var(--relay-touch-primary)!important;
    height: var(--relay-touch-primary)!important;
    min-height: var(--relay-touch-primary)!important;
  }

  body.relay-mobile .thread-ui-shell .thread-graph-composer-chip-button {
    min-height: 32px!important;
    padding-inline: 7px!important;
    flex-shrink: 0;
    border-radius: var(--relay-radius-sm)!important;
    font-size: 14px!important;
  }

  body.relay-mobile.relay-ime-open .thread-ui-shell .thread-graph-composer-input {
    max-height: min(13dvh, 72px)!important;
  }

  body.relay-mobile.relay-ime-open .thread-ui-shell .thread-graph-scroll-container {
    scroll-padding-bottom: 78px;
  }

  body.relay-mobile.relay-ime-composing .thread-ui-shell .thread-graph-composer-send-button {
    opacity: .72;
  }

  body.relay-mobile .thread-ui-shell :is(.thread-graph-composer-menu, .thread-composer-menu) {
    width: min(380px, calc(100vw - 10px))!important;
    max-width: calc(100vw - 10px)!important;
    max-height: min(48dvh, 460px)!important;
    overflow-y: auto!important;
    overscroll-behavior: contain;
    border-radius: 14px!important;
    font-size: var(--relay-ui-font)!important;
  }

  /* Right workspace remains a compact right drawer, wide enough for useful content. */
  body.relay-mobile .thread-ui-shell .matter-content {
    position: relative;
  }

  body.relay-mobile .thread-ui-shell .matter-explorer {
    position: absolute!important;
    inset: 0 0 0 auto!important;
    width: var(--relay-right-drawer)!important;
    max-width: 56vw!important;
    min-width: 0!important;
    border-left: 1px solid var(--theme-border-strong)!important;
    border-radius: 14px 0 0 14px;
    background: var(--theme-surface)!important;
    box-shadow: -14px 0 36px rgb(0 0 0 / 24%);
    overflow: hidden;
    z-index: 40!important;
  }

  body.relay-mobile .thread-ui-shell .matter-explorer-heading {
    height: 40px!important;
    min-height: 40px!important;
    padding: 0 2px 0 9px!important;
    font-size: var(--relay-ui-font)!important;
  }

  body.relay-mobile .thread-ui-shell .matter-explorer-heading button {
    width: var(--relay-touch)!important;
    min-width: var(--relay-touch)!important;
    height: var(--relay-touch)!important;
    min-height: var(--relay-touch)!important;
  }

  body.relay-mobile .thread-ui-shell .relay-mobile-right-scrim {
    position: absolute;
    inset: 0;
    display: block;
    border: 0;
    background: rgb(0 0 0 / 28%);
    z-index: 39;
  }

  /* Secondary surfaces use bottom sheets. */
  body.relay-mobile .thread-ui-shell .matter-notifications {
    position: fixed!important;
    inset: auto max(5px, env(safe-area-inset-right, 0px)) max(5px, env(safe-area-inset-bottom, 0px)) max(5px, env(safe-area-inset-left, 0px))!important;
    width: auto!important;
    max-height: min(64dvh, 520px)!important;
    border-radius: 16px!important;
    box-shadow: 0 16px 48px rgb(0 0 0 / 30%)!important;
  }

  body.relay-mobile .thread-ui-shell .matter-notifications-heading {
    min-height: 40px!important;
    padding-inline: 10px 2px!important;
    font-size: var(--relay-ui-font)!important;
  }

  body.relay-mobile .thread-ui-shell .matter-notifications-heading button {
    width: var(--relay-touch)!important;
    min-width: var(--relay-touch)!important;
    height: var(--relay-touch)!important;
    min-height: var(--relay-touch)!important;
  }

  body.relay-mobile .thread-ui-shell .matter-notifications > a {
    min-height: 44px!important;
    padding: 6px 10px!important;
    font-size: var(--relay-ui-font)!important;
  }

  body.relay-mobile .thread-graph-dialog.matter-settings-dialog {
    top: auto!important;
    bottom: max(5px, env(safe-area-inset-bottom, 0px))!important;
    width: calc(100vw - 10px)!important;
    max-width: calc(100vw - 10px)!important;
    max-height: calc(var(--relay-mobile-vvh, 100dvh) - 10px)!important;
    padding: 12px!important;
    transform: translateX(-50%)!important;
    border-radius: 16px!important;
  }
}

@media (max-width: 359px) {
  body.relay-mobile .thread-ui-shell .matter-topbar [aria-label="Go back"] {
    display: none!important;
  }

  body.relay-mobile {
    --relay-right-drawer: 168px;
  }
}

@media (max-height: 620px) {
  body.relay-mobile .thread-ui-shell .thread-graph-composer-input {
    max-height: min(12dvh, 66px)!important;
  }

  body.relay-mobile .thread-ui-shell :is(.thread-graph-composer-menu, .thread-composer-menu) {
    max-height: 42dvh!important;
  }
}

@media (prefers-reduced-motion: reduce) {
  body.relay-mobile *,
  body.relay-mobile *::before,
  body.relay-mobile *::after {
    scroll-behavior: auto!important;
    animation-duration: .001ms!important;
    transition-duration: .001ms!important;
  }
}

@media (prefers-reduced-transparency: reduce) {
  body.relay-mobile .thread-ui-shell :is(.matter-topbar, .thread-graph-composer-shell) {
    background: var(--theme-panel)!important;
    -webkit-backdrop-filter: none!important;
    backdrop-filter: none!important;
  }
}

/* ==========================================================================
 * v8 additions — device-adaptive layer
 * --------------------------------------------------------------------------
 * 前置：v7 的主布局块已由 `@media (max-width: 700px)` 改为 `@media all`
 *       （其内每条规则都以 body.relay-mobile 开头，故改为纯 class 驱动，
 *        横屏手机 / 平板也能拿到移动布局；桌面不加类则完全不受影响）。
 *
 * 本段只做「设备与方向」相关的补充，不重复 v7 已处理的窄屏布局。
 * 可用类：body.relay-device-{phone|tablet|narrow|desktop}
 *         body.relay-orient-{portrait|landscape}
 * ========================================================================== */

/* --- 1. 认证/登录界面 ------------------------------------------------------
 * v7 的规则全部限定在 .thread-ui-shell 下，登录页不属于该树，因此手机上
 * 登录页仍按桌面排版、且输入框字号偏小（iOS 会在聚焦时自动放大整页）。
 * 这里对移动布局下的通用表单元素做兜底。 */
body.relay-mobile input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]):not([type="submit"]),
body.relay-mobile textarea,
body.relay-mobile select {
  font-size: max(16px, 1em)!important;
  min-height: var(--relay-touch-primary, 44px);
}

body.relay-mobile :is(button, [role="button"], a, label, summary, input, select, textarea) {
  touch-action: manipulation;   /* 去掉双击缩放造成的 300ms 点击延迟 */
}

/* --- 2. 横屏手机：宽度可能 > 700px，但垂直空间很紧张 ---------------------- */
body.relay-mobile.relay-orient-landscape {
  --relay-left-drawer: min(52vw, 340px);
  --relay-right-drawer: clamp(200px, 42vw, 300px);
}

body.relay-mobile.relay-orient-landscape .thread-ui-shell .matter-topbar {
  min-height: calc(36px + env(safe-area-inset-top, 0px))!important;
}

body.relay-mobile.relay-orient-landscape .thread-ui-shell .thread-graph-composer-input {
  max-height: min(30dvh, 120px)!important;
}

body.relay-mobile.relay-orient-landscape .thread-ui-shell :is(.thread-graph-composer-menu, .thread-composer-menu) {
  max-height: 60dvh!important;
}

/* --- 3. 平板：抽屉放宽，观感更接近桌面 ------------------------------------ */
body.relay-mobile.relay-device-tablet {
  --relay-left-drawer: min(46vw, 360px);
  --relay-right-drawer: clamp(240px, 34vw, 380px);
}

body.relay-mobile.relay-device-tablet .thread-ui-shell .matter-sidebar,
body.relay-mobile.relay-device-tablet .thread-ui-shell .matter-explorer {
  box-shadow: 0 0 0 1px var(--theme-border, rgba(255,255,255,.12)), 0 18px 48px rgba(0,0,0,.42)!important;
}

/* --- 4. 触控指针下的最小点击目标 ----------------------------------------- */
@media (pointer: coarse) {
  body.relay-mobile .thread-ui-shell :is(.matter-thread-tabs > *, .matter-tabs-row button) {
    min-height: var(--relay-touch, 40px);
  }
}
