/* ==========================================================================
   H5 端（手機版）版面 — 移動優先，向上自適應平板／橫屏
   容器 id 與 core.js 共用；電腦版請改 web.css，兩邊互不影響。
   ========================================================================== */

html, body { height: 100%; overflow: hidden; }   /* 整頁不滾動，只有訊息區／列表滾動 */

#app-view {
  height: 100vh; height: 100dvh;
  display: flex; flex-direction: column; overflow: hidden;
}

/* 頂欄（列表頁／後台頁用；進入聊天頁時用聊天自己的頭部，頂欄與底部導航隱藏） */
.topbar {
  flex-shrink: 0;
  min-height: calc(50px + env(safe-area-inset-top, 0px));
  padding-top: env(safe-area-inset-top, 0px);
  background: var(--bar-bg); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  padding-left: max(12px, env(safe-area-inset-left, 0px));
  padding-right: max(12px, env(safe-area-inset-right, 0px));
}
.topbar-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo { font-weight: 700; font-size: 15px; }
.topbar-right { display: flex; align-items: center; gap: 2px; }
.btn.icon {
  padding: 9px 10px; font-size: 15px; line-height: 1; min-width: 42px;
  display: inline-flex; align-items: center; justify-content: center; text-decoration: none;
}
body.chat-open .topbar { display: none; }

/* 列表 ⇄ 聊天：同一個容器切換（微信式） */
.chat-layout { flex: 1; min-height: 0; display: flex; position: relative; overflow: hidden; }
.sidebar { width: 100%; background: var(--card); display: flex; flex-direction: column; min-height: 0; }
.sidebar-head { padding: 8px 10px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.conv-list { overflow-y: auto; flex: 1; -webkit-overflow-scrolling: touch; }
.conv-item {
  display: flex; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--border);
  cursor: pointer; align-items: center; min-height: 66px;
}
.conv-item:active { background: #f2f3f5; }
.conv-item.active { background: #eafaf1; }
.avatar { width: 46px; height: 46px; }
.conv-info { flex: 1; min-width: 0; }
.conv-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.conv-name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-bottom { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 3px; }
.conv-preview { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-tags { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 4px; }
.conv-time { font-size: 11px; color: var(--muted); flex-shrink: 0; }

.chat-main { display: none; position: absolute; inset: 0; flex-direction: column; min-height: 0; background: var(--chat-bg); }
.chat-layout.mobile-open .sidebar { display: none; }
.chat-layout.mobile-open .chat-main { display: flex; }
.chat-empty { flex: 1; display: flex; align-items: center; justify-content: center; }
#chat-panel { flex: 1; display: flex; flex-direction: column; min-height: 0; overflow: hidden; }

/* 聊天頭部：返回 + 客戶資料 + 會話操作（固定頂部） */
.chat-header {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px calc(8px) 4px;
  padding-top: calc(8px + env(safe-area-inset-top, 0px));
  background: var(--bar-bg); border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.back-btn {
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 26px; line-height: 1; padding: 0; width: 40px; height: 40px;
  border: none; background: none; flex-shrink: 0;
}
.chat-header .avatar { width: 36px; height: 36px; }
.chat-header-info { flex: 1; min-width: 0; }
.chat-name-line, .chat-sub-line { display: flex; align-items: center; gap: 5px; flex-wrap: nowrap; overflow: hidden; }
.chat-name { font-weight: 600; font-size: 15px; max-width: 42vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-sub-line { margin-top: 1px; }
.chat-sub-line .small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-name-line .chip { max-width: 92px; }

/* 訊息區：唯一滾動區 */
.msg-list {
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 12px; display: flex; flex-direction: column; gap: 12px;
}
.msg { max-width: 80%; }
.bubble { font-size: 15px; line-height: 1.45; padding: 10px 13px; border-radius: 8px; }
.bubble img.media { max-width: 200px; max-height: 240px; }
.bubble video.media { max-width: 240px; }
.bubble audio { width: 200px; }
.bubble img.sticker { width: 110px; height: 110px; }
.msg-meta { font-size: 10.5px; }

/* 輸入區：微信風格（貼底 + 底部安全區，輸入框行數自動加高） */
.composer {
  border-top: 1px solid var(--border); background: var(--bar-bg);
  padding: 7px 8px calc(7px + env(safe-area-inset-bottom, 0px));
  flex-shrink: 0;
}
.wx-bar { display: flex; align-items: flex-end; gap: 7px; }
.wx-ic {
  width: 34px; height: 34px; flex-shrink: 0; display: grid; place-items: center;
  border: none; background: none; color: #2c2c2c; font-size: 21px; line-height: 1;
  padding: 0; cursor: pointer; border-radius: 50%;
}
.wx-ic:active { background: rgba(0, 0, 0, .06); }
#msg-input {
  flex: 1; min-width: 0; margin-top: 0; resize: none;
  height: 34px; max-height: 108px;              /* 約 4 行後內部滾動 */
  padding: 6px 10px; font-size: 16px; line-height: 21px;  /* 16px 防止 iOS 聚焦縮放 */
  border: none; border-radius: 6px; background: #fff; overflow-y: hidden;
}
#msg-input:focus { outline: none; }
.wx-send {
  flex-shrink: 0; height: 34px; padding: 0 14px; border: none; border-radius: 5px;
  background: #07c160; color: #fff; font-size: 14px; font-family: inherit; cursor: pointer;
}
.wx-send:active { background: #06ad56; }

/* 表情 / 更多功能面板（從輸入列下方展開，與鍵盤互斥） */
.wx-panel {
  height: 214px; margin: 7px -8px calc(-7px - env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  display: flex; flex-direction: column; border-top: 1px solid var(--border);
}
.wx-panel.hidden { display: none; }
.wx-panel-body { flex: 1; min-height: 0; overflow-y: auto; padding: 10px 12px; }
.wx-tabs { display: flex; gap: 4px; padding: 5px 10px; border-top: 1px solid var(--border); background: #f4f4f4; }
.wx-tab {
  width: 40px; height: 30px; border: none; background: none; font-size: 18px;
  border-radius: 6px; cursor: pointer; padding: 0; font-family: inherit;
}
.wx-tab.active { background: #e7e7e7; }
.wx-emoji-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; }
.wx-emoji-grid button {
  border: none; background: none; font-size: 23px; line-height: 1; padding: 4px 0;
  cursor: pointer; border-radius: 4px; font-family: inherit;
}
.wx-emoji-grid button:active { background: #ececec; }
.wx-sticker-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.wx-sticker-grid img { width: 100%; cursor: pointer; border-radius: 6px; display: block; }
.wx-sticker-tip { grid-column: 1 / -1; font-size: 12px; color: var(--muted); text-align: center; padding: 6px 0; }
.wx-plus-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px 8px; padding: 4px; }
.wx-plus-item { display: flex; flex-direction: column; align-items: center; gap: 6px; border: none; background: none; cursor: pointer; font-family: inherit; padding: 0; }
.wx-plus-icon {
  width: 56px; height: 56px; border-radius: 14px; background: #fff;
  display: grid; place-items: center; font-size: 26px;
}
.wx-plus-label { font-size: 11.5px; color: #4b5563; }

#voice-mode { display: flex; gap: 7px; align-items: center; }
#voice-mode .hold-btn { flex: 1; }
.hold-btn { padding: 8px; height: 38px; font-size: 16px; border-radius: 6px; }

/* 底部導航 */
.tabbar {
  display: flex; flex-shrink: 0; background: #fff; border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
body.chat-open .tabbar { display: none; }
.tabbar-item {
  flex: 1; position: relative; display: flex; flex-direction: column; align-items: center; gap: 1px;
  padding: 8px 0 6px; min-height: 54px; border: none; background: none; cursor: pointer;
  font-family: inherit; font-size: 12px; color: var(--muted);
}
.tabbar-item.active { color: var(--primary); font-weight: 600; }
.tabbar-icon { font-size: 18px; line-height: 1.1; }
.tabbar-item .badge { position: absolute; top: 4px; left: calc(50% + 8px); }

/* 後台（單欄卡片） */
.admin-layout { flex: 1; min-height: 0; padding: 14px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.admin-col { display: flex; flex-direction: column; gap: 16px; }
.admin-col h2 { font-size: 15px; margin-bottom: 10px; }
.admin-col h3 { font-size: 13px; margin: 14px 0 8px; color: var(--muted); }
.card { padding: 16px; }
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -4px; padding: 0 4px; }
.data-table { min-width: 560px; }
.admin-col .btn.block { padding: 11px; font-size: 14px; }

/* 會話設定（底部彈出面板） */
.sheet {
  position: fixed; inset: 0; z-index: 70; display: flex; align-items: flex-end;
  background: rgba(0, 0, 0, .35);
}
.sheet-body {
  width: 100%; background: #fff; border-radius: 16px 16px 0 0;
  padding: 10px 16px calc(16px + env(safe-area-inset-bottom, 0px));
  max-height: 82vh; overflow-y: auto;
}
.sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: #dcdfe4; margin: 0 auto 12px; }
.sheet-head { font-size: 14px; font-weight: 600; margin-bottom: 10px; }
.sheet-tags { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.sheet-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.sheet-actions .btn { padding: 12px; font-size: 15px; }
.sheet label { margin-bottom: 8px; }

/* 貼圖彈窗在手機上往下靠，單手好按 */
.modal { align-items: flex-end; }
.modal-card { width: 100%; border-radius: 16px 16px 0 0; padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px)); }

/* ---------- 會話列表左滑快捷操作 ---------- */
.swipe-wrap { position: relative; overflow: hidden; border-bottom: 1px solid var(--border); }
.swipe-content { position: relative; transition: transform .22s ease; touch-action: pan-y; background: var(--card); }
.swipe-content.no-trans { transition: none; }
.swipe-content .conv-item { border-bottom: none; }
.swipe-actions {
  position: absolute; top: 0; right: 0; bottom: 0; width: 168px;
  display: flex; z-index: 0;
}
.swAct {
  flex: 1; border: none; color: #fff; font-family: inherit; font-size: 12px; line-height: 1.4;
  cursor: pointer; padding: 0;
}
.swAct.pin { background: #c8a400; }
.swAct.pin.on { background: #7a7f88; }
.swAct.hide { background: #b2813d; }
.swAct.del { background: var(--danger); }
.pin-mark { font-size: 11px; margin-right: 3px; }

/* ---------- 通用分頁容器（聯絡人 / 我的 / 後台） ---------- */
.page-section { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: 12px; }
body.chat-open .page-section { display: none; }

/* ---------- 聯絡人 ---------- */
.contacts-list { background: var(--card); }
.contact-item {
  display: flex; align-items: center; gap: 12px; padding: 12px 16px;
  border-bottom: 1px solid var(--border); cursor: pointer; background: var(--card);
}
.contact-item:active { background: #f2f3f5; }
.contact-info { flex: 1; min-width: 0; }
.contact-name { font-weight: 600; font-size: 15px; }
.contact-arrow { color: var(--muted); font-size: 18px; }
.contacts-empty { padding: 48px 24px; text-align: center; line-height: 1.9; }

/* ---------- 我的 ---------- */
.me-group {
  background: var(--card); margin: 12px 12px 0; border-radius: 10px;
  border: 1px solid var(--border); overflow: hidden;
}
.me-item {
  width: 100%; display: flex; align-items: center; gap: 12px; padding: 14px 16px;
  border: none; border-bottom: 1px solid var(--border); background: none;
  font-family: inherit; font-size: 15px; color: var(--text); cursor: pointer;
  text-align: left; text-decoration: none;
}
.me-item:last-child { border-bottom: none; }
.me-item:active { background: #f2f3f5; }
.me-icon { font-size: 17px; flex-shrink: 0; }
.me-val { margin-left: auto; color: var(--muted); font-size: 13px; }
.me-arrow { margin-left: auto; color: var(--muted); font-size: 18px; }
.me-profile { padding: 16px; }
.me-profile-info { display: flex; flex-direction: column; gap: 2px; flex: 1; min-width: 0; }
.me-profile-info b { font-size: 16px; }
.me-profile .avatar { width: 52px; height: 52px; }
.me-tip { padding: 14px 20px; line-height: 1.7; text-align: center; }

/* ---------- 自適應：大手機 / 折疊機 / 平板 ---------- */
@media (min-width: 430px) {
  .msg { max-width: 74%; }
  .bubble img.media { max-width: 240px; }
}
@media (min-width: 600px) {
  .conv-item { padding: 14px 18px; }
  .msg-list { padding: 16px; }
  .msg { max-width: 68%; }
  .chat-header { padding-left: 8px; }
  .chat-name { max-width: 50vw; }
  .admin-layout { padding: 18px; max-width: 720px; margin: 0 auto; width: 100%; }
  .composer { padding-left: 16px; padding-right: 16px; }
}
@media (min-width: 900px) {
  /* 平板橫放：列表與聊天並排，接近電腦版但保留手機操作 */
  .admin-layout { max-width: 100%; }
  .msg { max-width: 62%; }
}

/* ---------- 自適應：橫屏手機（高度很矮） ---------- */
@media (orientation: landscape) and (max-height: 520px) {
  .topbar { min-height: calc(42px + env(safe-area-inset-top, 0px)); }
  .msg-list { padding: 8px 12px; gap: 8px; }
  .chat-header { padding-top: calc(4px + env(safe-area-inset-top, 0px)); padding-bottom: 4px; }
  .chat-header .avatar { width: 30px; height: 30px; }
  .composer-btns { margin-bottom: 6px; }
  .hold-btn { padding: 12px; }
  .tabbar-item { min-height: 46px; padding: 6px 0 4px; }
  .rec-panel { top: 50%; padding: 14px 28px; }
  .rec-panel .rec-time { font-size: 22px; }
}
