:root {
  color-scheme: light;
  --bg: #f5f7fb;
  --panel: #ffffff;
  --panel-2: #f1f4f9;
  --text: #1f2937;
  --muted: #6b7280;
  --accent: #4f7cff;
  --accent-soft: #e8eeff;
  --accent-text: #ffffff;
  --danger: #e5484d;
  --border: #e5e8ef;
  --user: #4f7cff;
  --user-text: #ffffff;
  --assistant: #ffffff;
  --code-bg: #f6f8fa;
  --shadow: 0 1px 2px rgba(16,24,40,.04), 0 4px 16px rgba(16,24,40,.06);
  --codex: #10a37f; --codex-bg: #e6f6f1;
  --claude: #d97757; --claude-bg: #fbeee8;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", "Microsoft YaHei", sans-serif;
  -webkit-tap-highlight-color: transparent;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #1b1d24; --panel: #24272f; --panel-2: #2c3039; --text: #eceef3; --muted: #9aa1ae;
    --accent: #7c9dff; --accent-soft: #2f3a5c; --border: #343843;
    --user: #5b84ff; --assistant: #2a2d36; --code-bg: #1f2229;
    --shadow: 0 1px 2px rgba(0,0,0,.2), 0 4px 16px rgba(0,0,0,.2);
    --codex-bg: #183a31; --claude-bg: #3d2a22;
  }
}
* { box-sizing: border-box; }
html, body, #app { height: 100%; margin: 0; background: var(--bg); color: var(--text); }
body { font-size: 16px; line-height: 1.5; }
.view { height: 100%; }
.hidden { display: none !important; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 20px; padding: 24px; box-shadow: var(--shadow); }
h1, h2 { margin: 0; font-weight: 700; letter-spacing: -.01em; }
h1 { font-size: 24px; } h2 { font-size: 20px; }

/* controls */
input, textarea, select {
  font: inherit; font-size: 16px; color: var(--text);
  background-color: var(--panel-2); border: 1px solid transparent; border-radius: 14px;
  padding: 12px 14px; min-height: 46px; outline: none; transition: border-color .15s, box-shadow .15s, background .15s;
}
input:focus, textarea:focus, select:focus { background-color: var(--panel); border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
select { appearance: none; -webkit-appearance: none; padding-right: 36px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%236b7280' stroke-width='2' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 14px center; }
button {
  font: inherit; font-weight: 600; cursor: pointer; border: none; border-radius: 999px;
  padding: 0 20px; min-height: 44px; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--panel-2); color: var(--text); transition: transform .1s, background .15s, opacity .15s;
}
button:active { transform: scale(.97); }
button.primary { background: var(--accent); color: var(--accent-text); box-shadow: 0 4px 12px rgba(79,124,255,.28); }
button.ghost { background: transparent; color: var(--muted); }
button.ghost:hover { background: var(--panel-2); color: var(--text); }
button.small { min-height: 38px; padding: 0 16px; font-size: 14px; }
button.icon { width: 44px; min-height: 44px; padding: 0; font-size: 20px; color: var(--text); }
button:disabled { opacity: .45; cursor: not-allowed; box-shadow: none; transform: none; }
.muted { color: var(--muted); font-size: 13px; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 14px; }

/* login */
#auth-view { display: flex; align-items: center; justify-content: center; background:
  radial-gradient(600px 300px at 15% 0%, #e3ebff 0%, transparent 70%),
  radial-gradient(500px 300px at 100% 100%, #e6f6f1 0%, transparent 70%), var(--bg); }
@media (prefers-color-scheme: dark) { #auth-view { background: var(--bg); } }
#auth-view.hidden { display: none !important; }
.auth-card { width: min(400px, 90vw); display: grid; gap: 14px; text-align: center; padding: 32px 24px; }
.auth-card .logo { width: 64px; height: 64px; margin: 0 auto 4px; border-radius: 20px; display: grid; place-items: center;
  font-weight: 800; font-size: 22px; color: #fff; background: linear-gradient(135deg, #6a8dff, #4fc3a1); box-shadow: 0 8px 20px rgba(79,124,255,.3); }
.auth-card p { margin: 0; }
.auth-card button { min-height: 50px; font-size: 17px; }

/* layout */
#main-view { display: grid; grid-template-columns: 300px 1fr; height: 100%; }
#sidebar { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.sidebar-top, .sidebar-bottom, #chat-header, #composer { display: flex; gap: 8px; align-items: center; padding: 12px 14px; }
.sidebar-top { justify-content: space-between; }
.sidebar-top strong { font-size: 20px; }
.sidebar-bottom { margin-top: auto; border-top: 1px solid var(--border); justify-content: space-between; }
#session-list { overflow: auto; padding: 4px 10px; flex: 1; }
.session-item {
  display: grid; gap: 4px; width: 100%; text-align: left; padding: 12px 14px; margin-bottom: 6px; min-height: 60px;
  border-radius: 16px; background: transparent; font-weight: 400; justify-content: stretch; justify-items: start; align-content: center;
}
.session-item:hover { background: var(--panel-2); }
.session-item .meta { max-width: 100%; }
.session-item.active { background: var(--accent-soft); }
.session-item .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-item .meta, #chat-meta { display: flex; align-items: center; gap: 6px; min-width: 0; }
.badge { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px; letter-spacing: .02em; flex: none; }
.badge-codex { color: var(--codex); background: var(--codex-bg); }
.badge-claude { color: var(--claude); background: var(--claude-bg); }
.badge-mock { color: var(--muted); background: var(--panel-2); }
.dot-run { width: 8px; height: 8px; border-radius: 50%; background: var(--codex); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

#chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
#chat-header { background: color-mix(in srgb, var(--panel) 85%, transparent); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border); position: relative; z-index: 5; }
.head-text { min-width: 0; flex: 1; display: grid; gap: 2px; }
#chat-title { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* messages */
#messages { flex: 1; overflow: auto; padding: 18px 14px; display: flex; flex-direction: column; gap: 12px; }
.bubble { max-width: min(720px, 86%); padding: 11px 15px; border-radius: 20px; white-space: pre-wrap; word-break: break-word; line-height: 1.55; font-size: 15.5px; }
.bubble.user { align-self: flex-end; background: var(--user); color: var(--user-text); border-bottom-right-radius: 6px; box-shadow: 0 2px 8px rgba(79,124,255,.22); }
.bubble.assistant { align-self: flex-start; background: var(--assistant); border: 1px solid var(--border); border-bottom-left-radius: 6px; box-shadow: var(--shadow); }
.bubble.system { align-self: center; color: var(--muted); font-size: 12.5px; background: var(--panel-2); padding: 4px 12px; border-radius: 999px; }
.bubble.error { align-self: center; color: var(--danger); background: #fdecec; font-size: 13px; }
.bubble.tool, .bubble.thinking {
  align-self: flex-start; max-width: min(720px, 92%); background: var(--code-bg); border: 1px solid var(--border);
  border-left: 3px solid var(--accent); border-radius: 12px; color: var(--muted);
  font: 12.5px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; padding: 8px 12px;
}
.bubble.thinking { border-left-color: #b8a4ff; font-style: italic; font-family: inherit; }

/* composer */
#composer { background: var(--panel); border-top: 1px solid var(--border); align-items: flex-end; }
#composer textarea { flex: 1; resize: none; min-height: 46px; max-height: 40vh; border-radius: 23px; padding: 11px 16px; }
#send-btn { min-height: 46px; padding: 0 20px; }

/* dialog */
#new-dialog { border: none; padding: 0; background: transparent; max-width: 100vw; }
#new-dialog::backdrop { background: rgba(17,24,39,.35); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); }
#new-form { width: min(440px, 92vw); display: grid; gap: 14px; }
#new-form label { display: grid; gap: 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.row { display: flex; justify-content: flex-end; gap: 10px; margin-top: 4px; }
.row button { flex: 1; }

#toggle-sidebar, #close-sidebar { display: none; }
.row-inline { display: inline-flex; gap: 6px; align-items: center; }
#drawer-backdrop { position: fixed; inset: 0; background: rgba(17,24,39,.3); z-index: 15; }
@media (max-width: 800px) {
  #main-view { grid-template-columns: 1fr; }
  #sidebar {
    position: fixed; inset: 0 auto 0 0; width: min(84vw, 320px); border-right: none;
    border-radius: 0 24px 24px 0; box-shadow: 8px 0 32px rgba(16,24,40,.15);
    transform: translateX(-105%); transition: transform .22s ease; z-index: 20;
    padding-left: env(safe-area-inset-left);
  }
  #sidebar.open { transform: translateX(0); }
  #toggle-sidebar, #close-sidebar { display: inline-flex; }
}

/* iOS safe areas */
#chat-header, .sidebar-top { padding-top: calc(12px + env(safe-area-inset-top)); }
#chat-header { padding-left: calc(10px + env(safe-area-inset-left)); padding-right: calc(14px + env(safe-area-inset-right)); }
#composer { padding-bottom: calc(8px + env(safe-area-inset-bottom)); padding-left: calc(12px + env(safe-area-inset-left)); padding-right: calc(12px + env(safe-area-inset-right)); }
.sidebar-bottom { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
#auth-view { padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); }
#net-banner { position: fixed; left: 0; right: 0; top: env(safe-area-inset-top); z-index: 50; background: var(--danger); color: #fff; padding: 8px 12px; font-size: 13px; text-align: center; }
@media (min-width: 801px) { #drawer-backdrop { display: none !important; } }

/* ---- v5 ---- */
.seg { display: flex; gap: 4px; margin: 0 14px 6px; padding: 4px; background: var(--panel-2); border-radius: 999px; }
.seg-btn { flex: 1; border: 0; background: transparent; padding: 7px 0; border-radius: 999px; font-size: 13.5px; color: var(--muted); box-shadow: none; }
.seg-btn.on { background: var(--panel); color: var(--text); font-weight: 600; box-shadow: 0 1px 4px rgba(0,0,0,.08); }
.session-item { display: flex !important; flex-direction: row !important; align-items: center; gap: 4px; padding: 0 !important; }
.session-main { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start; gap: 4px; background: transparent; border: 0; box-shadow: none; padding: 10px 12px; text-align: left; color: inherit; border-radius: inherit; }
.session-main .title { max-width: 100%; }
.more-btn { flex: none; width: 36px; height: 36px; border: 0; background: transparent; box-shadow: none; font-size: 20px; color: var(--muted); border-radius: 50%; padding: 0; margin-right: 6px; }
.more-btn:hover { background: var(--panel-2); }
.empty-list { padding: 18px; text-align: center; font-size: 13px; }
dialog.sheet { margin: auto auto 0; width: 100%; max-width: 480px; padding: 0; border: 0; background: transparent; }
.sheet-card { display: flex; flex-direction: column; gap: 6px; padding: 14px 14px calc(14px + env(safe-area-inset-bottom)); border-radius: 22px 22px 0 0; }
.sheet-title { text-align: center; color: var(--muted); font-size: 13px; padding: 4px 8px 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sheet-btn { width: 100%; padding: 13px; border-radius: 14px; font-size: 16px; background: var(--panel-2); border: 0; }
.sheet-btn.danger { color: var(--danger); }
.sheet-btn:disabled { opacity: .5; }
.sheet-btn.cancel { background: var(--panel); border: 1px solid var(--border); margin-top: 4px; }
.chip { display: inline-flex; align-items: center; gap: 4px; max-width: 100%; margin-top: 3px; padding: 2px 10px; border-radius: 999px; border: 1px solid var(--border); background: var(--panel-2); font-size: 12px; color: var(--muted); box-shadow: none; }
.chip #workdir-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: ui-monospace, Menlo, Consolas, monospace; }
.head-text { min-width: 0; }
.input-row { display: flex; gap: 6px; }
.input-row input { flex: 1; min-width: 0; }
button.small { padding: 8px 14px; border-radius: 12px; white-space: nowrap; }
.dir-card { width: min(92vw, 460px); max-height: 86vh; display: flex; flex-direction: column; gap: 8px; padding: 18px; }
.dir-card h2 { margin: 0 0 4px; font-size: 18px; }
.dir-section-title { font-size: 12.5px; color: var(--muted); margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dir-list { display: flex; flex-direction: column; gap: 2px; overflow-y: auto; border: 1px solid var(--border); border-radius: 12px; padding: 4px; }
#dir-recent { max-height: 22vh; }
#dir-browser { max-height: 30vh; min-height: 80px; }
.dir-row { display: flex; gap: 8px; align-items: center; text-align: left; padding: 8px 10px; background: transparent; border: 0; box-shadow: none; border-radius: 8px; font-size: 14px; color: inherit; }
.dir-row:hover { background: var(--panel-2); }
.dir-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; }
.dir-empty { padding: 8px 10px; font-size: 13px; }
#dir-note { font-size: 12.5px; margin: 0; line-height: 1.5; }
#dir-note:empty, #dir-error:empty, #new-error:empty { display: none; }
button.icon { width: 46px; height: 46px; flex: none; border-radius: 50%; font-size: 24px; padding: 0; line-height: 1; }
#attach-preview { display: flex; gap: 8px; padding: 10px 14px 0; background: var(--panel); border-top: 1px solid var(--border); overflow-x: auto; }
#attach-preview.hidden { display: none; }
#attach-preview + #composer { border-top: 0; }
.thumb { position: relative; flex: none; width: 64px; height: 64px; border-radius: 12px; overflow: hidden; border: 1px solid var(--border); }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb.uploading img { opacity: .45; }
.thumb-x { position: absolute; top: 2px; right: 2px; width: 22px; height: 22px; padding: 0; border-radius: 50%; border: 0; background: rgba(0,0,0,.6); color: #fff; font-size: 15px; line-height: 22px; box-shadow: none; }
.bubble-imgs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.bubble-imgs img { max-width: 180px; max-height: 180px; border-radius: 12px; object-fit: cover; cursor: zoom-in; background: #fff; }
.bubble-text:empty { display: none; }
#dir-dialog, #action-sheet { border: none; padding: 0; background: transparent; max-width: 100vw; }
#dir-dialog::backdrop, #action-sheet::backdrop { background: rgba(17,24,39,.35); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); }
#action-sheet { margin: auto auto 0; width: 100%; max-width: 480px; }
.chip { min-height: 24px; height: 24px; padding: 0 10px; font-weight: 500; justify-content: flex-start; width: auto; }
#chat-header .head-text { display: flex; flex-direction: column; align-items: flex-start; flex: 1; }
#chat-header .head-text > div { max-width: 100%; }
.dir-row { justify-content: flex-start; min-height: 38px; font-weight: 500; padding: 0 10px; }
.session-main { justify-content: center; min-height: 60px; font-weight: 400; }
.session-main .title { font-weight: 600; }
.more-btn { min-height: 36px; }
.thumb-x { min-height: 0; display: flex; }

/* ---- v6 ---- */
/* layout pinned to the visual viewport (iOS keyboard) */
html, body { position: fixed; inset: 0; overflow: hidden; overscroll-behavior: none; }
#app { position: fixed; left: 0; right: 0; top: 0; height: var(--app-h, 100dvh); will-change: transform; }
body.kb-open #composer { padding-bottom: 6px !important; }
.auth-card .logo-img { width: 72px; height: 72px; border-radius: 18px; display: block; margin: 0 auto 6px; box-shadow: 0 6px 18px rgba(252,82,1,.25); }

/* unified pill composer */
#composer { background: transparent; border-top: 0; padding-top: 6px; display: block; }
.composer-box { background: var(--panel); border: 1px solid var(--border); border-radius: 26px; box-shadow: 0 2px 12px rgba(17,24,39,.07); transition: border-color .15s, box-shadow .15s; }
.composer-box:focus-within { border-color: color-mix(in srgb, var(--accent) 55%, var(--border)); box-shadow: 0 0 0 3px var(--accent-soft), 0 2px 12px rgba(17,24,39,.07); }
.composer-row { display: flex; align-items: flex-end; gap: 2px; padding: 5px; }
#composer textarea { flex: 1; border: 0 !important; background: transparent !important; box-shadow: none !important; outline: none; min-height: 40px; padding: 9px 6px; border-radius: 0; font-size: 16px; }
.cbtn { flex: none; width: 40px; height: 40px; min-height: 40px; padding: 0; border-radius: 50%; display: grid; place-items: center; box-shadow: none; }
.cbtn.attach { background: transparent; color: var(--muted); }
.cbtn.attach:hover:not(:disabled) { background: var(--panel-2); color: var(--text); }
.cbtn.send { background: var(--accent); color: #fff; }
.cbtn.send:disabled { background: var(--panel-2); color: var(--muted); opacity: 1; }
#attach-preview { background: transparent; border: 0; padding: 10px 10px 2px; }
#attach-preview + .composer-row { padding-top: 2px; }

/* connection dot */
.conn-dot { flex: none; width: 36px; height: 36px; min-height: 36px; padding: 0; background: transparent; box-shadow: none; display: grid; place-items: center; align-self: flex-start; margin-top: 2px; }
.conn-dot span { width: 10px; height: 10px; border-radius: 50%; background: #9ca3af; box-shadow: 0 0 0 3px rgba(156,163,175,.2); }
.conn-dot[data-state="connected"] span { background: #22c55e; box-shadow: 0 0 0 3px rgba(34,197,94,.18); }
.conn-dot[data-state="connecting"] span { background: #f59e0b; box-shadow: 0 0 0 3px rgba(245,158,11,.2); animation: pulse 1.2s ease-in-out infinite; }
.conn-dot[data-state="failed"] span { background: #ef4444; box-shadow: 0 0 0 3px rgba(239,68,68,.2); }
@keyframes pulse { 50% { opacity: .35; transform: scale(.8); } }
#net-banner { display: none !important; }

/* toast */
.toast { position: fixed; left: 50%; top: calc(64px + env(safe-area-inset-top)); transform: translateX(-50%); z-index: 60; max-width: 86vw;
  background: rgba(17,24,39,.86); color: #fff; font-size: 13px; padding: 7px 14px; border-radius: 999px; box-shadow: 0 6px 20px rgba(0,0,0,.18);
  transition: opacity .2s, transform .2s; pointer-events: none; text-align: center; }
.toast.hidden { display: block !important; opacity: 0; transform: translate(-50%, -6px); }

/* swipe rows */
.swipe-row { position: relative; overflow: hidden; border-radius: 16px; margin-bottom: 6px; }
.swipe-row .session-item { position: relative; z-index: 1; margin: 0; background: var(--panel); transition: transform .22s cubic-bezier(.2,.8,.2,1); touch-action: pan-y; }
.swipe-row .session-item.active { background: color-mix(in srgb, var(--accent-soft) 100%, var(--panel)); }
.swipe-row.dragging .session-item { transition: none; }
.swipe-actions { position: absolute; top: 0; right: 0; bottom: 0; display: flex; z-index: 0; }
.swipe-actions button { width: 72px; min-height: 0; height: 100%; border-radius: 0; color: #fff; font-size: 13.5px; flex-direction: column; gap: 2px; box-shadow: none; padding: 0; }
.swipe-actions button:active { transform: none; filter: brightness(.92); }
.swipe-actions .sa-rename { background: #8e8e93; }
.swipe-actions .sa-archive { background: #ff9500; }
.swipe-actions .sa-restore { background: #34a853; }
.swipe-actions .sa-delete { background: #ff3b30; }
.swipe-actions .sa-delete:disabled { background: #f3a19c; opacity: 1; }
.swipe-actions button svg { width: 20px; height: 20px; }
@media (prefers-color-scheme: dark) { .swipe-row .session-item { background: var(--panel); } }
.swipe-actions { visibility: hidden; }
.swipe-row.dragging .swipe-actions, .swipe-row.open .swipe-actions { visibility: visible; }
.toast { border-radius: 14px; line-height: 1.45; }
