/* cc-bridge 前端 —— ChatGPT / Claude.ai 級介面。
   ⚠️ mobile-first，一部 phone + iPad + desktop 都要順。

   ⚠️ [hidden] 一定要用 !important 壓返：
      瀏覽器默認 [hidden]{display:none} specificity 太低，
      任何 .class{display:flex/grid} 都會蓋過佢 —— 否則 el.hidden=true
      靜靜失效（登入頁永遠蓋住 app 就係咁嚟）。 */

/* ── 設計 token ─────────────────────────────────────────────────────────── */
:root {
  --bg: #262626;
  --bg-deep: #1f1f1f;              /* topbar / sidebar / code blocks */
  --surface: #303030;              /* cards, sidebar, chips */
  --surface-2: #3d3d3d;            /* user bubble, composer, inline code */
  --surface-3: #4a4a4a;            /* hover / active / disabled send */
  --border: rgba(255,255,255,.08);
  --border-strong: rgba(255,255,255,.14);
  --text: #f4f2ec;
  --text-dim: #a3a09a;
  --text-faint: #6f6d68;
  --accent: #d97757;
  --accent-strong: #c05f42;
  --accent-soft: rgba(217,119,87,.16);
  --success: #66c46a;
  --warn: #e2b714;
  --danger: #e5484d;
  --danger-soft: rgba(229,72,77,.10);
  --radius: 12px;
  --radius-pill: 999px;
  --maxw: 46rem;                   /* 736px 可讀寬度 */
  --ease: cubic-bezier(.4,0,.2,1);
  --font-mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
}
[data-theme="light"] {
  --bg: #faf9f5; --bg-deep: #f0eee9;
  --surface: #ffffff; --surface-2: #f0eee9; --surface-3: #e5e2db;
  --border: rgba(0,0,0,.08); --border-strong: rgba(0,0,0,.14);
  --text: #1f1e1b; --text-dim: #6e6b63; --text-faint: #a09c94;
  --accent: #b3553a; --accent-strong: #9c4a33; --accent-soft: rgba(179,85,58,.12);
  --success: #2e8b57; --warn: #b78a00; --danger: #d33b2e; --danger-soft: rgba(211,59,46,.08);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI",
        "PingFang HK", "Noto Sans CJK HK", sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
button, input, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
::selection { background: var(--accent-soft); }

/* ── 登入 ──────────────────────────────────────────────────────────────── */
.login {
  position: fixed; inset: 0; display: grid; place-items: center; padding: 24px;
  background: var(--bg); z-index: 50;
}
.login-card {
  width: 100%; max-width: 320px; text-align: center;
  display: flex; flex-direction: column; gap: 12px;
}
.login-mark { font-size: 34px; color: var(--accent); }
.login-card h1 { margin: 0; font-size: 20px; font-weight: 600; }
.login-hint { margin: 0; font-size: 13px; color: var(--text-dim); }
.login-card input {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px; text-align: center;
}
.login-card input:focus { outline: 2px solid var(--accent); outline-offset: 2px; }
.login-card button {
  background: var(--accent); color: #fff; border: 0;
  border-radius: 10px; padding: 12px; font-weight: 600; cursor: pointer;
  transition: background .15s var(--ease);
}
.login-card button:hover { background: var(--accent-strong); }
.login-card button:disabled { opacity: .6; cursor: not-allowed; }
.login-err { color: var(--danger); font-size: 13px; margin: 0; }

/* ── 版面骨架（grid：topbar / transcript / composer 永遠垂直）─────────── */
.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100vh;                       /* 舊 iOS WebKit fallback */
  height: 100dvh;                      /* iOS 動態視口 */
  height: var(--app-height, 100dvh);   /* visualViewport 驅動（鍵盤彈出時） */
}
.topbar        { grid-row: 1; }
.transcript   { grid-row: 2; min-height: 0; }   /* min-height:0 先可以喺 1fr row 內 scroll */
.composer-wrap { grid-row: 3; }

.topbar {
  display: flex; align-items: center; gap: 8px;
  height: calc(56px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 12px 0;
  border-bottom: 1px solid var(--border); background: var(--bg-deep);
}
.topbar-title { flex: 1; font-weight: 600; font-size: 15px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.icon-btn {
  width: 44px; height: 44px; border: 0; background: transparent;
  border-radius: 10px; cursor: pointer; font-size: 18px; color: var(--text-dim);
  display: grid; place-items: center;
  transition: background .15s var(--ease), color .15s var(--ease);
  -webkit-tap-highlight-color: transparent;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.state-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--text-faint); }
.state-dot[data-state="working"] { background: var(--accent); animation: pulse 1.4s infinite; }
.state-dot[data-state="done"]    { background: var(--success); box-shadow: 0 0 0 3px var(--accent-soft); }
@keyframes pulse { 0%,100% { opacity: 1 } 50% { opacity: .3 } }

/* ── 側欄（窄屏 = fixed 抽屜；寬屏 = 靜態左欄）─────────────────────────── */
.sidebar {
  position: fixed; top: 0; bottom: 0; left: 0; z-index: 40;
  width: min(86vw, 320px);
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  transform: translateX(-102%);
  visibility: hidden;
  transition: transform .24s cubic-bezier(.32,.72,0,1), visibility 0s .24s;
}
.sidebar.open {
  transform: none;
  visibility: visible;
  transition: transform .24s cubic-bezier(.32,.72,0,1);
}
.sidebar-head {
  height: calc(56px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 16px 0;
  display: flex; align-items: center; font-weight: 600; font-size: 15px;
}
.sidebar-scroll { flex: 1; overflow-y: auto; padding: 4px 8px 8px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.group-head {
  font-size: 11px; letter-spacing: .06em; text-transform: uppercase;
  color: var(--text-faint); padding: 14px 8px 6px;
}
.group-hint { text-transform: none; letter-spacing: 0; opacity: .7; }

.session-list, .file-list { display: flex; flex-direction: column; gap: 1px; }
.session-item {
  display: flex; align-items: center; gap: 8px;
  padding: 11px 10px; min-height: 44px; border-radius: 8px; cursor: pointer;
  font-size: 14px; color: var(--text); text-align: left;
  background: transparent; border: 0; width: 100%;
  transition: background .15s var(--ease);
}
.session-item:hover { background: var(--surface-2); }
.session-item.active { background: var(--surface-2); }
.session-item .dot { width: 6px; height: 6px; border-radius: 50%; background: transparent; flex: none; }
.session-item.active .dot { background: var(--accent); }
.session-item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-item .when { font-size: 11px; color: var(--text-faint); flex: none; }

.file-item {
  padding: 7px 10px; font-size: 13px; color: var(--text-dim);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  direction: rtl; text-align: left;   /* 長路徑由尾顯示 */
}
.file-empty { padding: 7px 10px; font-size: 13px; color: var(--text-faint); opacity: .6; }

.new-chat {
  margin: 8px; padding: 12px; text-align: left;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 10px; cursor: pointer; font-size: 14px; font-weight: 500;
  margin-bottom: calc(8px + env(safe-area-inset-bottom));
  transition: background .15s var(--ease);
}
.new-chat:hover { background: var(--surface-3); }

.scrim {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 30;
}
.scrim.open { opacity: 1; pointer-events: auto; }

/* ── 對話區 ────────────────────────────────────────────────────────────── */
.transcript {
  overflow-y: auto; overflow-anchor: none;
  padding: 24px 16px 48px;
  display: flex; flex-direction: column; gap: 8px;
  -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
}
.transcript::-webkit-scrollbar { width: 8px; }
.transcript::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 4px; }
.transcript > *, .answer, .msg.user { min-width: 0; }
.transcript > *:not(.msg.user) { max-width: var(--maxw); width: 100%; margin: 0 auto; }

/* 用戶 bubble：暖色 surface、唔對稱 radius、靠右 shrink-to-fit */
.msg.user {
  align-self: flex-end; width: auto;
  max-width: min(80%, 40rem);
  margin-top: 20px;                  /* 新 user 回合上面留白 */
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 20px 20px 6px 20px; padding: 10px 16px;
  color: var(--text); font-size: 15px; line-height: 1.5;
  white-space: pre-wrap; overflow-wrap: anywhere;
}

/* assistant 答案：plain prose，無 bubble */
.answer { color: var(--text); font-size: 15px; line-height: 1.7; padding: 2px 0; margin-bottom: 2px; }
.answer > div, .answer > p { margin: 0 0 .85em; }
.answer > *:first-child { margin-top: 0; }
.answer > *:last-child { margin-bottom: 0; }
.answer h1 { font-size: 1.5em;  font-weight: 700; letter-spacing: -.01em; margin: 1.4em 0 .5em; }
.answer h2 { font-size: 1.28em; font-weight: 700; letter-spacing: -.01em; margin: 1.2em 0 .45em; }
.answer h3 { font-size: 1.12em; font-weight: 600; letter-spacing: -.01em; margin: 1em 0 .4em; }
.answer a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.answer ul, .answer ol { margin: .6em 0 .9em; padding-left: 1.5em; }
.answer li { margin: .25em 0; }
.answer code {
  background: var(--surface-2); padding: 2px 6px; border-radius: 6px;
  font-size: .85em; font-family: var(--font-mono);
  overflow-wrap: anywhere; word-break: break-all;
}
.answer pre {
  background: var(--bg-deep); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px 16px; margin: .6em 0; overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.answer pre code { background: none; padding: 0; font-size: .85em; }

/* 動作節點（tool） */
.tool {
  border: 1px solid var(--border); border-radius: 12px; background: var(--surface);
  font-size: 13px; margin: 4px 0; overflow: hidden;
  transition: background .15s var(--ease), border-color .15s var(--ease);
}
.tool:hover { background: var(--surface-2); border-color: var(--border-strong); }
.tool > summary {
  display: flex; align-items: center; gap: 10px;
  padding: 12px; min-height: 44px; cursor: pointer; color: var(--text-dim);
  list-style: none;
}
.tool > summary::-webkit-details-marker { display: none; }
.tool > summary::after { content: "⌄"; margin-left: auto; color: var(--text-faint); transition: transform .2s var(--ease); }
.tool[open] > summary::after { transform: rotate(180deg); }
.tool .name { color: var(--text); font-weight: 500; }
.tool .arg { flex: 1; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap; font-family: var(--font-mono); font-size: 12px; }
.tool .st { flex: none; font-weight: 600; }
.tool[data-ok="1"] .st { color: var(--success); }
.tool[data-ok="0"] { border-color: rgba(229,72,77,.45); background: var(--danger-soft); }
.tool[data-ok="0"] .st { color: var(--danger); }
.tool pre {
  margin: 0; padding: 12px 14px; border-top: 1px solid var(--border);
  max-height: 260px; overflow: auto; font-size: 12px; white-space: pre-wrap;
  color: var(--text-dim); background: var(--bg-deep);
  -webkit-overflow-scrolling: touch;
}

/* 思考路徑 —— 默認收埋，撳先開 */
details.think {
  border: 1px dashed var(--border-strong); border-radius: 12px;
  background: var(--accent-soft); font-size: 13px; color: var(--text-dim);
  margin: 4px 0; overflow: hidden;
}
details.think > summary {
  display: flex; align-items: center; gap: 10px;
  padding: 12px; min-height: 44px; cursor: pointer; list-style: none;
}
details.think > summary::-webkit-details-marker { display: none; }
details.think > summary::after { content: "⌄"; margin-left: auto; color: var(--text-faint); transition: transform .2s var(--ease); }
details.think[open] > summary::after { transform: rotate(180deg); }
details.think.live > summary { opacity: .75; }
details.think.live .ic { animation: pulse 1.4s ease-in-out infinite; }
details.think .tx {
  padding: 12px 14px; border-top: 1px solid var(--border);
  max-height: 320px; overflow: auto; -webkit-overflow-scrolling: touch;
  white-space: pre-wrap; font-size: 12px; line-height: 1.6; opacity: .85;
}

.notice { font-size: 13px; color: var(--text-dim); text-align: center; margin: 10px 0 4px; }
.notice[data-level="err"] { color: var(--danger); }
.notice[data-level="warn"] { color: var(--warn); }
.meta { font-size: 12px; color: var(--text-faint); opacity: .75; margin: 4px 0 12px; }

/* typing 指示器（三點） */
.typing { display: flex; gap: 5px; padding: 8px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--text-faint); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%,80%,100% { opacity: .25 } 40% { opacity: 1 } }

/* ── 輸入區 ────────────────────────────────────────────────────────────── */
.composer-wrap {
  padding: 8px 16px calc(14px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 72%, transparent);
}
.composer {
  max-width: var(--maxw); margin: 0 auto;
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 28px; padding: 6px;
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
  transition: border-color .18s var(--ease), box-shadow .18s var(--ease);
}
.composer:focus-within {
  border-color: var(--border-strong);
  box-shadow: 0 0 0 3px var(--accent-soft), 0 8px 24px rgba(0,0,0,.25);
}
.composer textarea {
  flex: 1; background: none; border: 0; outline: none; resize: none;
  padding: 9px 6px; max-height: 200px; line-height: 1.5; font-size: 15px;
}
.composer textarea::placeholder { color: var(--text-faint); }

.icon-btn.round, .send {
  width: 44px; height: 44px; border-radius: 50%; border: 0; flex: none;
  display: grid; place-items: center; cursor: pointer;
  transition: background .15s var(--ease), color .15s var(--ease), transform .12s var(--ease), box-shadow .15s var(--ease);
  -webkit-tap-highlight-color: transparent;
}
.round { background: transparent; color: var(--text-dim); }
.round:hover { background: var(--surface-3); color: var(--text); }
.send { background: var(--surface-3); color: var(--text-faint); }
.send svg { width: 20px; height: 20px; display: block; }
.send[data-ready="1"] { background: var(--accent); color: #fff; box-shadow: 0 2px 8px var(--accent-soft); }
.send[data-ready="1"]:hover { background: var(--accent-strong); }
.send[data-ready="1"]:active { transform: scale(.92); }
.send[data-stop="1"] { background: var(--danger); color: #fff; }
.send:disabled { background: var(--surface-3); color: var(--text-faint); cursor: not-allowed; }

.attach-chips {
  max-width: var(--maxw); margin: 0 auto 6px; display: flex; gap: 6px;
  overflow-x: auto; -webkit-overflow-scrolling: touch;
}

/* ── 空狀態 ────────────────────────────────────────────────────────────── */
.empty { text-align: center; margin: auto; padding: 48px 0; }
.empty-mark { font-size: 34px; color: var(--accent); }
.empty-title { font-size: 18px; font-weight: 600; color: var(--text); margin: 12px 0 24px; }
.chips { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; max-width: 520px; margin: 0 auto; }
.chip {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 12px; min-height: 44px; font-size: 14px; line-height: 1.4;
  color: var(--text); text-align: left; cursor: pointer;
  transition: background .15s var(--ease), border-color .15s var(--ease), transform .15s var(--ease);
}
.chip:hover { background: var(--surface-2); border-color: var(--border-strong); transform: translateY(-1px); }
.chip:active { transform: translateY(0) scale(.98); }

/* ── jump-to-latest 藥丸（sticky 喺 .transcript 內）────────────────────── */
#jump {
  position: sticky; bottom: 12px; align-self: center;
  width: max-content; max-width: none; margin: 0 auto;
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px; border: 1px solid var(--border-strong); border-radius: 999px;
  background: var(--surface-2); color: var(--text); font-size: 13px; font-weight: 500;
  cursor: pointer; box-shadow: 0 6px 20px rgba(0,0,0,.3); z-index: 5;
  transition: transform .12s var(--ease), background .15s var(--ease);
}
#jump:hover { background: var(--surface-3); }
#jump:active { transform: scale(.95); }
.jump-count {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: var(--accent); color: #fff; font-size: 12px; font-weight: 600;
  display: inline-grid; place-items: center;
}
.jump-count:empty { display: none; }

/* ── 動畫 + reduced-motion ─────────────────────────────────────────────── */
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.msg.user, .answer, .tool, details.think, .notice, .typing { animation: rise .22s var(--ease); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

/* ── screen-reader live region ─────────────────────────────────────────── */
.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ── 闊屏：sidebar 由 fixed 抽屜 → 靜態全高左欄 ────────────────────────── */
@media (min-width: 900px) {
  .app {
    grid-template-columns: 300px minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas:
      "sidebar topbar"
      "sidebar transcript"
      "sidebar composer";
  }
  .topbar        { grid-area: topbar; }
  .sidebar       {
    grid-area: sidebar;
    position: static; transform: none; width: auto; height: auto;
    visibility: visible;               /* 寬屏永遠顯示 */
    border-right: 1px solid var(--border);
  }
  .transcript    { grid-area: transcript; }
  .composer-wrap { grid-area: composer; }
  .scrim { display: none; }
  #menu  { display: none; }
}
