/* OpenClaw Control UI - Mobile Optimization */
/* 专门优化移动端聊天界面 */

/* 基础移动端优化 */
@media (max-width: 768px) {
  /* 全屏显示，隐藏地址栏和底部工具栏 */
  html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    position: fixed;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: none;
  }

  /* 确保页面不会横向滚动 */
  body {
    overflow-x: hidden;
    -webkit-text-size-adjust: 100%;
  }

  openclaw-app {
    overflow-x: hidden;
    height: 100%;
    display: block;
  }

  /* 优化 Shell 布局 */
  .shell {
    --shell-pad: 8px;
    --shell-gap: 8px;
    --shell-topbar-height: 52px;
    grid-template-columns: 1fr;
    grid-template-rows: var(--shell-topbar-height) auto 1fr;
    grid-template-areas:
      "topbar"
      "nav"
      "content";
  }

  /* 顶部栏优化 */
  .topbar {
    padding: 8px 12px;
    height: var(--shell-topbar-height);
    flex-wrap: nowrap;
  }

  .topbar-left {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .brand {
    display: flex !important;
    align-items: center;
    gap: 8px;
  }

  .brand-logo {
    display: none !important;
  }

  .brand-title {
    font-size: 14px;
    display: block !important;
  }

  .brand-sub {
    display: none;
  }

  /* 导航栏优化 - 横向滚动 */
  .nav {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    gap: 6px;
    padding: 10px 12px;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .nav::-webkit-scrollbar {
    display: none;
  }

  .nav-group {
    display: contents;
  }

  .nav-label {
    display: none;
  }

  .nav-item {
    padding: 8px 14px;
    font-size: 13px;
    white-space: nowrap;
    flex-shrink: 0;
  }

  /* 内容区域优化 */
  .content {
    padding: 8px 0 16px 0 !important;
    gap: 12px;
    overflow-x: hidden;
  }

  /* ===== 聊天界面专项优化 ===== */

  /* 聊天容器 */
  .chat {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
  }

  /* 聊天头部 */
  .chat-header {
    padding: 8px 12px 12px;
    margin-bottom: 8px;
    flex-wrap: wrap;
    gap: 8px;
  }

  .chat-header__left {
    flex-direction: column;
    align-items: stretch;
    width: 100%;
    gap: 8px;
  }

  .chat-header__right {
    width: 100%;
    justify-content: space-between;
  }

  .chat-session {
    min-width: 100%;
    width: 100%;
  }

  .chat-session select {
    width: 100%;
    padding: 10px 12px;
    font-size: 14px;
  }

  /* 聊天控制按钮 */
  .chat-controls {
    flex-wrap: wrap;
    gap: 8px;
    width: 100%;
  }

  .chat-controls__session {
    min-width: 100%;
    width: 100%;
  }

  .btn--icon {
    min-width: 44px !important;
    height: 44px !important;
    padding: 12px !important;
  }

  .btn--icon svg {
    width: 20px !important;
    height: 20px !important;
  }

  /* 聊天线程/消息区域 */
  .chat-thread {
    flex: 1;
    overflow-y: auto !important;
    overflow-x: hidden;
    padding: 12px 6px 12px 6px !important;
    margin: 0;
    -webkit-overflow-scrolling: touch;
    display: flex;
    flex-direction: column;
  }

  /* 聊天消息组 - 头像和消息在同一行 */
  .chat-group {
    margin-bottom: 16px;
    margin-left: 0;
    margin-right: 0;
    gap: 8px;
    flex-direction: row !important;
    align-items: flex-start !important;
    width: 100%;
  }

  /* 用户消息：头像在右边 */
  .chat-group.user {
    flex-direction: row-reverse !important;
    justify-content: flex-start !important;
    align-items: flex-start !important;
  }

  /* 助手消息：头像在左边 */
  .chat-group.assistant,
  .chat-group.other {
    flex-direction: row !important;
    justify-content: flex-start !important;
    align-items: flex-start !important;
  }

  /* 头像容器对齐到顶部 */
  .chat-group-header {
    align-self: flex-start !important;
  }

  /* 发送者信息（头像+名称+时间） - 横向排列 */
  .chat-group-header {
    display: flex !important;
    align-items: center;
    gap: 8px;
    flex-direction: row;
    visibility: visible !important;
  }

  .chat-avatar {
    order: 0;
  }

  .chat-sender-name {
    font-weight: 700 !important;
    font-size: 13px;
    color: var(--text-strong);
    order: 1;
    display: block !important;
    visibility: visible !important;
    opacity: 1 !important;
  }

  .chat-group-timestamp,
  .chat-stamp {
    font-size: 11px;
    color: var(--muted);
    opacity: 0.7;
    order: 2;
    margin-left: 4px;
    display: inline !important;
    visibility: visible !important;
  }

  /* 确保昵称和时间显示 */
  .chat-group-footer {
    display: flex !important;
    order: -1;
    margin-bottom: 4px;
  }

  .chat-group-footer * {
    display: inline !important;
    visibility: visible !important;
  }

  .chat-group-messages {
    max-width: 75% !important;
    width: auto;
    display: flex;
    flex-direction: column;
  }

  /* 助手消息靠左 */
  .chat-group.assistant .chat-group-messages,
  .chat-group.other .chat-group-messages {
    align-items: flex-start;
  }

  /* 用户消息靠右 */
  .chat-group.user .chat-group-messages {
    align-items: flex-end;
  }

  .chat-msg {
    max-width: 100% !important;
    width: auto;
  }

  .chat-bubble {
    max-width: 100% !important;
    width: fit-content;
  }

  .chat-msg {
    max-width: 96% !important;
  }

  /* 聊天头像 - 圆形彩色，缩小尺寸 */
  .chat-avatar {
    width: 36px;
    height: 36px;
    font-size: 13px;
    font-weight: 600;
    flex-shrink: 0;
    border-radius: 50% !important;
    border: 2px solid var(--border-strong);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    align-self: flex-start !important;
  }

  .chat-avatar.user {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
    border-color: #667eea;
    color: white !important;
  }

  .chat-avatar.assistant {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%) !important;
    border-color: #f093fb;
    color: white !important;
  }

  .chat-avatar.other {
    background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%) !important;
    border-color: #4facfe;
    color: white !important;
  }

  /* 聊天气泡 */
  .chat-bubble {
    padding: 12px 14px;
    border-radius: 12px;
    font-size: 16px !important;
    line-height: 1.6;
    max-width: 100%;
    word-wrap: break-word;
    overflow-wrap: break-word;
  }

  /* 用户消息 - 绿色 */
  .chat-group.user .chat-bubble {
    background: linear-gradient(135deg, #95ec69 0%, #8ce563 100%) !important;
    border: 1px solid #7dd663 !important;
    color: #333 !important;
  }

  .chat-group.user .chat-text {
    color: #333 !important;
  }

  /* 助手消息 - 白色 */
  .chat-group.assistant .chat-bubble,
  .chat-group.other .chat-bubble {
    background: white !important;
    border: 1px solid #e5e5e5 !important;
    color: #333 !important;
  }

  .chat-group.assistant .chat-text,
  .chat-group.other .chat-text {
    color: #333 !important;
  }

  /* 深色模式下的调整 */
  :root[data-theme=dark] .chat-group.assistant .chat-bubble,
  :root[data-theme=dark] .chat-group.other .chat-bubble {
    background: #2a2a2a !important;
    border-color: #3a3a3a !important;
    color: #e5e5e5 !important;
  }

  :root[data-theme=dark] .chat-group.assistant .chat-text,
  :root[data-theme=dark] .chat-group.other .chat-text {
    color: #e5e5e5 !important;
  }

  /* 聊天文本内容 */
  .chat-text {
    font-size: 16px !important;
    line-height: 1.65;
  }

  .chat-text code {
    font-size: 14px !important;
  }

  .chat-text pre {
    padding: 12px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .chat-text pre code {
    font-size: 13px !important;
  }

  /* ===== 聊天输入框 - 重点优化 ===== */

  .chat-compose {
    position: sticky;
    bottom: 0;
    z-index: 10;
    margin-top: auto;
    padding: 4px 6px 12px 6px !important;
    background: transparent !important;
    gap: 6px;
  }

  /* 输入框容器 */
  .chat-compose__row {
    display: flex;
    flex-direction: row;
    gap: 8px;
    width: 100%;
    align-items: center;
  }

  /* 隐藏新建会话按钮 */
  #new-session-btn {
    display: none !important;
  }

  .chat-compose__field {
    flex: 1;
    min-width: 0;
    order: 0 !important;
  }

  .chat-compose__actions {
    order: 1 !important;
  }

  /* 输入框 - 微信风格 */
  .chat-compose__field textarea {
    width: 100% !important;
    min-height: 100px !important;
    max-height: 200px !important;
    padding: 16px 18px !important;
    font-size: 16px !important;
    line-height: 1.5 !important;
    border-radius: 8px !important;
    border: 1px solid #d9d9d9 !important;
    background: white !important;
    resize: none;
    -webkit-appearance: none;
    box-sizing: border-box;
  }

  /* 深色模式 */
  :root[data-theme=dark] .chat-compose__field textarea {
    background: #2a2a2a !important;
    border-color: #3a3a3a !important;
    color: #e5e5e5 !important;
  }

  /* 显示提示文字 */
  .chat-compose__field textarea::placeholder {
    color: #999 !important;
    opacity: 1 !important;
  }

  .chat-compose__field textarea:focus {
    border-color: #07c160 !important;
    outline: none;
  }

  /* 按钮区域 */
  .chat-compose__actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
    flex-shrink: 0;
  }

  /* 发送按钮 - 合适尺寸，图标更清晰 */
  .chat-compose__actions .btn {
    min-width: 56px !important;
    min-height: 56px !important;
    height: 56px !important;
    width: 56px !important;
    padding: 8px !important;
    font-size: 0 !important;
    font-weight: 600 !important;
    border-radius: 12px !important;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    display: flex !important;
    flex-direction: column;
    align-items: center !important;
    justify-content: center !important;
    gap: 0;
  }

  /* SVG图标基础样式 - 超大尺寸 */
  .chat-compose__actions .btn svg {
    width: 44px !important;
    height: 44px !important;
    stroke-width: 4px !important;
    display: block !important;
    visibility: visible !important;
    opacity: 1 !important;
  }

  /* 主要按钮（发送）更突出 */
  .chat-compose__actions .btn.primary,
  .chat-compose__actions button[type="submit"] {
    background: #07c160 !important;
    border-color: #07c160 !important;
    color: white !important;
  }

  /* 发送按钮图标 - 强制白色，超粗，加阴影 */
  .chat-compose__actions .btn.primary svg,
  .chat-compose__actions button[type="submit"] svg {
    stroke: white !important;
    fill: white !important;
    color: white !important;
    stroke-width: 4.5px !important;
    filter: drop-shadow(0 1px 2px rgba(0,0,0,0.2));
  }

  /* 所有SVG子元素都设置为白色超粗 */
  .chat-compose__actions .btn.primary svg *,
  .chat-compose__actions button[type="submit"] svg * {
    stroke: white !important;
    fill: white !important;
    color: white !important;
    stroke-width: 4.5px !important;
  }

  /* 其他按钮图标 - 深色超粗 */
  .chat-compose__actions .btn:not(.primary) svg {
    stroke: #1a1a1a !important;
    fill: none !important;
    stroke-width: 4px !important;
  }

  .chat-compose__actions .btn:not(.primary) svg * {
    stroke: #1a1a1a !important;
    fill: none !important;
    stroke-width: 4px !important;
  }

  /* 确保所有可能的SVG元素都可见 */
  .chat-compose__actions .btn svg path,
  .chat-compose__actions .btn svg polyline,
  .chat-compose__actions .btn svg line,
  .chat-compose__actions .btn svg circle,
  .chat-compose__actions .btn svg rect,
  .chat-compose__actions .btn svg polygon {
    stroke: inherit !important;
    stroke-width: inherit !important;
    visibility: visible !important;
    opacity: 1 !important;
  }

  /* 附件预览 */
  .chat-attachments {
    padding: 10px;
    gap: 10px;
    max-width: 100%;
  }

  .chat-attachment {
    width: 70px;
    height: 70px;
  }

  /* 工具卡片 */
  .chat-tool-card {
    padding: 12px;
    margin-top: 8px;
    max-height: none;
    overflow: visible;
  }

  .chat-tool-card__title {
    font-size: 13px;
  }

  .chat-tool-card__detail {
    font-size: 12px;
  }

  /* 复制按钮 */
  .chat-copy-btn {
    opacity: 1 !important;
    pointer-events: auto !important;
    top: 8px;
    right: 8px;
    padding: 6px 8px;
  }

  /* 聊天队列 */
  .chat-queue {
    margin-top: 10px;
    padding: 10px;
  }

  .chat-queue__item {
    padding: 10px;
    gap: 10px;
  }

  .chat-queue__text {
    font-size: 14px;
  }

  /* 聊天焦点模式 */
  .shell--chat-focus .content {
    padding: 0 8px 8px;
  }

  .shell--chat-focus .chat-compose {
    padding: 12px 8px 16px;
  }

  /* 聊天分屏 */
  .chat-split-container {
    flex-direction: column;
  }

  .chat-main {
    min-width: 100%;
  }

  .chat-sidebar {
    min-width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
  }

  /* 卡片优化 */
  .card {
    padding: 14px;
    border-radius: 12px;
  }

  /* 表单字段 */
  .field input,
  .field textarea,
  .field select {
    padding: 12px 14px;
    font-size: 16px; /* 防止 iOS 自动缩放 */
    border-radius: 10px;
  }

  /* 按钮通用优化 */
  .btn {
    min-height: 44px;
    padding: 12px 18px;
    font-size: 16px !important;
    border-radius: 10px;
    touch-action: manipulation;
  }

  .btn svg {
    width: 18px;
    height: 18px;
  }
}

/* 小屏手机额外优化 */
@media (max-width: 480px) {
  .shell {
    --shell-topbar-height: 48px;
  }

  .topbar {
    padding: 6px 10px;
  }

  .brand-title {
    font-size: 13px;
  }

  .nav {
    padding: 8px 10px;
  }

  .nav-item {
    padding: 7px 12px;
    font-size: 12px;
  }

  .content {
    padding: 6px 0 12px 0 !important;
  }

  .chat-compose {
    padding: 10px 4px 10px 4px !important;
  }

  .chat-compose__row {
    flex-direction: row;
    align-items: flex-end;
  }

  .chat-compose__field {
    flex: 1;
  }

  .chat-compose__field textarea {
    min-height: 70px !important;
    padding: 12px 14px !important;
    font-size: 16px !important;
  }

  .chat-compose__actions {
    flex-direction: column;
    gap: 6px;
  }

  .chat-compose__actions .btn {
    min-width: 50px !important;
    min-height: 50px !important;
    height: 50px !important;
    width: 50px !important;
    padding: 8px !important;
    font-size: 0 !important;
  }

  .chat-compose__actions .btn svg {
    width: 22px !important;
    height: 22px !important;
  }

  .chat-bubble {
    padding: 10px 12px;
    font-size: 16px !important;
  }

  .chat-text {
    font-size: 16px !important;
  }

  .chat-group-messages {
    max-width: 96% !important;
  }

  .chat-msg {
    max-width: 96% !important;
  }
}

/* 横屏模式优化 */
@media (max-width: 768px) and (orientation: landscape) {
  .chat-compose__field textarea {
    min-height: 60px !important;
    max-height: 120px !important;
  }

  .chat-thread {
    padding: 8px;
  }
}

/* 防止双击缩放 */
@media (max-width: 768px) {
  button,
  a,
  input,
  textarea,
  select {
    touch-action: manipulation;
  }
}

/* iOS 安全区域适配 */
@supports (padding: env(safe-area-inset-bottom)) {
  @media (max-width: 768px) {
    .chat-compose {
      padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    }

    .shell--chat-focus .chat-compose {
      padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    }
  }
}
