/* ===================================================================
   webterm — chat UI
   =================================================================== */

/* ---------- Theme tokens ---------- */
:root {
  /* Light theme defaults */
  --bg:            #ffffff;
  --bg-subtle:     #f7f7f8;
  --bg-sidebar:    #f9f9f9;
  --bg-elevated:   #ffffff;
  --bg-hover:      rgba(0,0,0,0.04);
  --bg-active:     rgba(0,0,0,0.07);
  --bg-bubble:     #f4f4f4;
  --border:        #e5e5e5;
  --border-strong: #d1d1d1;
  --text:          #0d0d0d;
  --text-muted:    #6b6b6b;
  --text-faint:    #9b9b9b;
  --accent:        #0d0d0d;
  --accent-fg:     #ffffff;
  --danger:        #e24c4c;
  --warn-bg:       #fff6e6;
  --warn-border:   #f0d8a8;
  --warn-text:     #9a5b00;
  --tool-bg:       #eef2ff;
  --tool-border:   #d9e1ff;
  --tool-text:     #3b4a8f;
  --code-bg:       #f5f5f5;
  --code-border:   #e5e5e5;
  --link:          #1e6fd9;
  --focus-ring:    rgba(13,13,13,0.2);
  --shadow-sm:     0 1px 2px rgba(0,0,0,0.04);
  --shadow-md:     0 8px 24px rgba(0,0,0,0.08);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-pill: 999px;

  color-scheme: light;
}

/* Auto dark when OS requests it AND user hasn't forced light */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:            #212121;
    --bg-subtle:     #2f2f2f;
    --bg-sidebar:    #171717;
    --bg-elevated:   #2a2a2a;
    --bg-hover:      rgba(255,255,255,0.06);
    --bg-active:     rgba(255,255,255,0.09);
    --bg-bubble:     #303030;
    --border:        #3d3d3d;
    --border-strong: #4a4a4a;
    --text:          #ececec;
    --text-muted:    #b4b4b4;
    --text-faint:    #8a8a8a;
    --accent:        #ececec;
    --accent-fg:     #0d0d0d;
    --danger:        #ef6b6b;
    --warn-bg:       #3a2a10;
    --warn-border:   #5a4318;
    --warn-text:     #f7c98a;
    --tool-bg:       #1e253d;
    --tool-border:   #2e3855;
    --tool-text:     #b4c2ff;
    --code-bg:       #2a2a2a;
    --code-border:   #3d3d3d;
    --link:          #7cb1ff;
    --focus-ring:    rgba(236,236,236,0.28);
    --shadow-sm:     0 1px 2px rgba(0,0,0,0.3);
    --shadow-md:     0 10px 40px rgba(0,0,0,0.5);

    color-scheme: dark;
  }
}

/* Manual override */
:root[data-theme="dark"] {
  --bg:            #212121;
  --bg-subtle:     #2f2f2f;
  --bg-sidebar:    #171717;
  --bg-elevated:   #2a2a2a;
  --bg-hover:      rgba(255,255,255,0.06);
  --bg-active:     rgba(255,255,255,0.09);
  --bg-bubble:     #303030;
  --border:        #3d3d3d;
  --border-strong: #4a4a4a;
  --text:          #ececec;
  --text-muted:    #b4b4b4;
  --text-faint:    #8a8a8a;
  --accent:        #ececec;
  --accent-fg:     #0d0d0d;
  --danger:        #ef6b6b;
  --warn-bg:       #3a2a10;
  --warn-border:   #5a4318;
  --warn-text:     #f7c98a;
  --tool-bg:       #1e253d;
  --tool-border:   #2e3855;
  --tool-text:     #b4c2ff;
  --code-bg:       #2a2a2a;
  --code-border:   #3d3d3d;
  --link:          #7cb1ff;
  --focus-ring:    rgba(236,236,236,0.28);
  --shadow-sm:     0 1px 2px rgba(0,0,0,0.3);
  --shadow-md:     0 10px 40px rgba(0,0,0,0.5);

  color-scheme: dark;
}

/* ---------- Reset & base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
               "Segoe UI", "Helvetica Neue", Arial, "Noto Sans", "PingFang SC",
               "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.55;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
::selection { background: var(--text); color: var(--bg); }

button { font-family: inherit; }

/* ---------- Login ---------- */
.login-body {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh;
  background: var(--bg);
}
.login-card {
  background: var(--bg-elevated);
  padding: 32px;
  border-radius: var(--radius-lg);
  width: 360px;
  box-shadow: var(--shadow-md);
  border: 1px solid var(--border);
}
.login-card h1 {
  margin: 0 0 4px;
  font-size: 22px; font-weight: 600;
  color: var(--text);
  letter-spacing: -0.01em;
}
.login-card .sub { margin: 0 0 22px; color: var(--text-muted); font-size: 14px; }
.login-card label {
  display: block; margin-bottom: 14px;
  font-size: 12px; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: .05em;
}
.login-card input {
  display: block; width: 100%; margin-top: 6px;
  padding: 11px 14px;
  border: 1px solid var(--border-strong);
  background: var(--bg);
  color: var(--text);
  border-radius: var(--radius-md);
  font-size: 15px; font-family: inherit;
  transition: border-color .15s, box-shadow .15s;
}
.login-card input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--focus-ring); }
.login-card button {
  width: 100%; margin-top: 10px;
  padding: 11px;
  background: var(--accent); color: var(--accent-fg);
  border: 0; border-radius: var(--radius-pill);
  font-size: 15px; font-weight: 600;
  cursor: pointer;
  transition: opacity .15s, transform .06s;
}
.login-card button:hover:not(:disabled) { opacity: 0.9; }
.login-card button:active:not(:disabled) { transform: scale(0.99); }
.login-card button:disabled { opacity: .55; cursor: wait; }
.msg { color: var(--danger); font-size: 13px; margin-top: 12px; min-height: 18px; }

/* ---------- Chat shell ---------- */
.chat-body {
  display: flex; flex-direction: row;
  width: 100vw; height: 100vh; height: 100dvh;
  margin: 0;
  overflow: hidden;
}
.main { flex: 1 1 auto; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 260px;
  flex: 0 0 260px;
  display: flex; flex-direction: column;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  overflow: hidden;
  transition: width .2s ease, flex-basis .2s ease;
}
.sidebar.collapsed { width: 0; flex-basis: 0; border-right-width: 0; }

.chat-new-group { display: flex; flex-direction: column; gap: 6px; padding: 4px 6px 8px; }
.sidebar-user {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 12px;
  min-width: 0;
}
.sidebar-user .who {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  color: var(--text);
  font-weight: 500;
}
.sidebar-user svg { flex: 0 0 auto; opacity: 0.7; }

.sidebar-bottom {
  padding: 10px;
  display: flex; gap: 8px; align-items: center;
}
.sidebar-bottom .ghost { flex: 1; }

.new-chat {
  width: 100%;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  cursor: pointer;
  font-size: 14px; font-weight: 500;
  transition: background .15s, border-color .15s;
}
.new-chat:hover { background: var(--bg-hover); }
.new-chat svg { color: var(--text-muted); }
.new-chat-caret { margin-left: auto; transition: transform .15s; }
.new-chat[aria-expanded="true"] .new-chat-caret { transform: rotate(180deg); }

.new-chat-menu {
  display: flex; flex-direction: column; gap: 2px;
  padding: 4px;
  margin-top: 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg-elevated);
}
.new-chat-menu[hidden] { display: none; }
.new-chat-opt {
  width: 100%;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  background: transparent; border: 0;
  color: var(--text);
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  border-radius: var(--radius-sm);
}
.new-chat-opt:hover { background: var(--bg-hover); }

.chat-cli-badge {
  display: inline-block;
  font-size: 10px; font-weight: 600;
  line-height: 1;
  padding: 2px 4px;
  margin-right: 6px;
  border-radius: 3px;
  background: var(--bg-subtle);
  color: var(--text-muted);
  border: 1px solid var(--border);
  vertical-align: 1px;
}
.chat-cli-badge.cli-gemini { background: #4e7bff22; color: #4e7bff; border-color: #4e7bff55; }
.chat-cli-badge.cli-claude { background: #d97a3b22; color: #d97a3b; border-color: #d97a3b55; }
.chat-cli-badge.cli-codex  { background: #10a37f22; color: #10a37f; border-color: #10a37f55; }

.sidebar-section { padding: 6px 6px 2px; }
.sidebar-heading {
  font-size: 11px; letter-spacing: .04em;
  color: var(--text-faint);
  padding: 10px 12px 4px;
  text-transform: uppercase;
  font-weight: 600;
}
.sidebar-link {
  width: 100%;
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px;
  background: transparent; border: 0;
  border-radius: var(--radius-md);
  color: var(--text);
  cursor: pointer;
  font: inherit; font-size: 14px;
  text-align: left;
  transition: background .12s;
}
.sidebar-link:hover { background: var(--bg-hover); }
.sidebar-link.active { background: var(--bg-active); }
.sidebar-link svg { opacity: .75; flex-shrink: 0; }

.chat-list { flex: 1 1 auto; overflow-y: auto; padding: 2px 0 12px; }
.chat-empty {
  color: var(--text-faint);
  font-size: 13px; text-align: center;
  padding: 20px 10px;
}

.chat-item {
  position: relative;
  display: flex; align-items: center; gap: 6px;
  padding: 8px 12px;
  margin: 1px 0;
  border-radius: var(--radius-md);
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
  user-select: none;
  transition: background .12s;
}
.chat-item:hover { background: var(--bg-hover); }
.chat-item.active { background: var(--bg-active); font-weight: 500; }
.chat-item.busy::before {
  content: '';
  display: inline-block;
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--danger);
  margin-right: 4px;
  flex-shrink: 0;
  animation: pulse 1.2s infinite ease-in-out;
}
@keyframes pulse {
  0%, 100% { opacity: .4; transform: scale(0.9); }
  50%      { opacity: 1;  transform: scale(1); }
}

.chat-item-title {
  flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat-rename {
  flex: 1; min-width: 0;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--accent); border-radius: var(--radius-sm);
  padding: 3px 6px; font: inherit; font-size: 14px;
}

.chat-item-actions {
  display: flex; align-items: center; gap: 2px;
  opacity: 0; transition: opacity .15s;
}
.chat-item:hover .chat-item-actions,
.chat-item.active .chat-item-actions,
.chat-item.busy .chat-item-actions { opacity: 1; }
/* Touch devices have no hover; always show the actions so rename/delete are reachable. */
@media (hover: none) {
  .chat-item-actions { opacity: 1; }
}
.chat-item-actions button {
  display: inline-flex; align-items: center; justify-content: center;
  width: 24px; height: 24px;
  background: transparent;
  border: 0;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  cursor: pointer;
  padding: 0;
  transition: background .12s, color .12s;
}
.chat-item-actions button:hover { background: var(--bg-active); color: var(--text); }
.chat-stop { color: var(--danger) !important; }
.chat-stop:hover { background: rgba(226,76,76,0.15) !important; }

/* ---------- Header ---------- */
.chat-header {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
  min-height: 52px;
}
.chat-header .brand {
  font-size: 14px; font-weight: 600;
  color: var(--text);
  letter-spacing: -0.01em;
}
.chat-header .chat-title {
  font-size: 14px; color: var(--text); font-weight: 500;
  margin-left: 4px; max-width: 45%;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat-header .spacer { flex: 1; }

.ghost {
  background: transparent; color: var(--text);
  border: 1px solid var(--border-strong);
  padding: 6px 14px;
  border-radius: var(--radius-pill);
  font-size: 13px; cursor: pointer;
  font-family: inherit;
  transition: background .12s, border-color .12s;
}
.ghost:hover { background: var(--bg-hover); }
.ghost.icon-only { padding: 6px 8px; display: inline-flex; align-items: center; justify-content: center; border: 0; }

.status {
  font-size: 11px;
  padding: 3px 10px;
  border-radius: var(--radius-pill);
  background: var(--bg-subtle);
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.status.ok      { background: rgba(30,160,70,0.12);  color: #1a7a3d; border-color: rgba(30,160,70,0.25); }
.status.pending { background: rgba(230,170,10,0.14); color: #956200; border-color: rgba(230,170,10,0.3); }
.status.bad     { background: rgba(226,76,76,0.14); color: var(--danger); border-color: rgba(226,76,76,0.3); }
:root[data-theme="dark"] .status.ok,
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .status.ok  { color: #6fd48a; } }
:root[data-theme="dark"] .status.pending,
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .status.pending { color: #f3c367; } }

/* Theme toggle icon button */
.theme-toggle {
  width: 34px; height: 34px;
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--border-strong);
  background: transparent;
  color: var(--text);
  border-radius: var(--radius-pill);
  cursor: pointer;
  flex-shrink: 0;
  transition: background .12s;
  padding: 0;
}
.theme-toggle:hover { background: var(--bg-hover); }
.theme-toggle .sun  { display: none; }
.theme-toggle .moon { display: inline-block; }
:root[data-theme="dark"] .theme-toggle .sun,
:root:not([data-theme="light"]) .theme-toggle .sun { display: none; }
:root[data-theme="dark"] .theme-toggle .moon,
:root:not([data-theme="light"]) .theme-toggle .moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-toggle .sun { display: inline-block; }
}
:root[data-theme="dark"] .theme-toggle .sun { display: inline-block; }
:root[data-theme="light"] .theme-toggle .moon { display: inline-block; }

/* ---------- Transcript ---------- */
.transcript {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 24px 20px 180px;
  scroll-behavior: smooth;
}
.transcript > * {
  max-width: 760px;
  margin-left: auto; margin-right: auto;
}

.empty-hint {
  text-align: center;
  color: var(--text-muted);
  padding: 120px 24px 40px;
}
.empty-hint h1 {
  font-size: 32px;
  font-weight: 600;
  margin: 0 0 12px;
  color: var(--text);
  letter-spacing: -0.02em;
}
.empty-hint p { margin: 4px 0; font-size: 15px; color: var(--text-muted); }
.empty-hint .hint-sub {
  margin-top: 18px;
  color: var(--text-faint);
  font-size: 12.5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

/* ---------- Messages (GPT-like layout) ---------- */
/* User: right-aligned bubble with timestamp below. Assistant: left avatar + flowing text (no bubble). */
.msg { margin: 22px 0; display: flex; }
.msg:first-child { margin-top: 8px; }
.msg-user { flex-direction: column; align-items: flex-end; }
.msg-assistant { gap: 14px; }

.msg-time {
  font-size: 11px;
  color: var(--text-faint);
  margin-top: 4px;
  padding: 0 4px;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.01em;
}

.msg-user .bubble {
  max-width: 80%;
  background: var(--bg-bubble);
  color: var(--text);
  padding: 11px 16px;
  border-radius: 18px;
  font-size: 15px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-wrap: break-word;
  word-break: break-word;
}

.msg-assistant .avatar {
  flex-shrink: 0;
  width: 28px; height: 28px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-fg);
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700;
  letter-spacing: -0.01em;
  margin-top: 2px;
}
.msg-assistant .msg-col {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column;
  gap: 6px;
}
.msg-assistant .activity { display: flex; flex-wrap: wrap; gap: 6px; }
.msg-assistant .activity:empty { display: none; }
.msg-assistant .text {
  color: var(--text);
  font-size: 15px;
  line-height: 1.65;
  word-wrap: break-word;
  min-height: 22px;
}
.msg-assistant .text.no-text { color: var(--text-faint); font-style: italic; }

/* Streaming cursor — inserted by app.js inside the last block element while running */
.stream-cursor {
  display: inline-block;
  color: var(--text-muted);
  margin-left: 2px;
  animation: cursor-blink 1.05s steps(2, jump-none) infinite;
  user-select: none;
}
@keyframes cursor-blink {
  0%   { opacity: 1; }
  100% { opacity: 0; }
}

/* Message fade-in */
.msg { animation: msg-in .22s ease-out; }
@keyframes msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ---------- Markdown styling (assistant) ---------- */
.markdown > :first-child { margin-top: 0; }
.markdown > :last-child  { margin-bottom: 0; }
.markdown p  { margin: 0 0 12px; }
.markdown h1, .markdown h2, .markdown h3,
.markdown h4, .markdown h5, .markdown h6 {
  margin: 18px 0 10px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.01em;
  line-height: 1.3;
}
.markdown h1 { font-size: 1.55em; }
.markdown h2 { font-size: 1.3em; }
.markdown h3 { font-size: 1.12em; }
.markdown h4 { font-size: 1em; }
.markdown ul, .markdown ol { margin: 6px 0 12px; padding-left: 26px; }
.markdown li { margin: 4px 0; }
.markdown li > p { margin: 4px 0; }
.markdown a { color: var(--link); text-decoration: underline; text-underline-offset: 2px; }
.markdown a:hover { text-decoration-thickness: 2px; }
.markdown strong { font-weight: 600; }
.markdown em { font-style: italic; }
.markdown hr { border: 0; border-top: 1px solid var(--border); margin: 20px 0; }
.markdown blockquote {
  margin: 12px 0;
  padding: 4px 14px;
  color: var(--text-muted);
  border-left: 3px solid var(--border-strong);
  background: var(--bg-subtle);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}
.markdown code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.88em;
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  padding: 1.5px 6px;
  border-radius: 5px;
  word-break: break-word;
}
.markdown pre {
  margin: 12px 0;
  padding: 14px 16px;
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  border-radius: var(--radius-md);
  overflow-x: auto;
  font-size: 13.5px;
  line-height: 1.55;
}
.markdown pre code {
  background: transparent;
  border: 0;
  padding: 0;
  font-size: inherit;
  color: var(--text);
  white-space: pre;
}
.markdown table {
  border-collapse: collapse;
  margin: 12px 0;
  width: 100%;
  font-size: 14px;
}
.markdown th, .markdown td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.markdown th { background: var(--bg-subtle); font-weight: 600; }

/* ---------- Tool chips ---------- */
.tool-chip {
  display: inline-flex; align-items: center; gap: 6px;
  max-width: 100%;
  padding: 3px 10px;
  border-radius: var(--radius-pill);
  background: var(--tool-bg);
  color: var(--tool-text);
  border: 1px solid var(--tool-border);
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
}
.tool-chip .tool-name { font-weight: 600; flex-shrink: 0; }
.tool-chip .tool-arg {
  color: inherit; opacity: 0.75;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  max-width: 440px;
}
.tool-chip.warn {
  background: var(--warn-bg);
  color: var(--warn-text);
  border-color: var(--warn-border);
  white-space: pre-wrap;
  display: block;
  max-width: 100%;
}

.meta {
  margin-top: 6px;
  font-size: 11.5px;
  color: var(--text-faint);
}
.meta:empty { display: none; }
.meta.bad { color: var(--danger); }

/* Typing dots */
.typing { display: inline-flex; gap: 4px; align-items: center; height: 18px; }
.typing i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-muted);
  animation: blink 1.2s infinite ease-in-out;
}
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink {
  0%, 80%, 100% { opacity: .25; transform: translateY(0); }
  40%           { opacity: 1;   transform: translateY(-2px); }
}

/* ---------- Composer ---------- */
.composer {
  position: sticky;
  bottom: 0;
  padding: 12px 16px 20px;
  flex: 0 0 auto;
  background: linear-gradient(to top, var(--bg) 60%, rgba(0,0,0,0));
}
.composer-inner {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: 26px;
  padding: 8px 8px 8px 8px;
  box-shadow: var(--shadow-sm);
  transition: border-color .15s, box-shadow .15s;
}

/* "+" attach button, Gemini-style, sits at the left edge of the composer. */
.attach-btn {
  width: 36px; height: 36px;
  border: 0; border-radius: 50%;
  background: transparent;
  color: var(--text-faint);
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 22px; line-height: 1;
  flex-shrink: 0;
  transition: background .15s, color .15s, transform .08s;
}
.attach-btn:hover { background: var(--border-strong); color: var(--text); }
.attach-btn:active { transform: scale(0.94); }

/* Row of pending attachment chips shown above the textarea before send. */
.composer-attachments {
  max-width: 760px;
  margin: 0 auto 6px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 10px;
}
.attach-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 4px 4px 10px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  font-size: 12px;
  color: var(--text);
  max-width: 280px;
}
.attach-chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attach-chip-rm {
  background: transparent; border: 0; cursor: pointer;
  color: var(--text-faint);
  width: 20px; height: 20px; border-radius: 50%;
  font-size: 16px; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
}
.attach-chip-rm:hover { background: var(--border-strong); color: var(--text); }

/* Static chips inside sent user bubbles — non-interactive. */
.bubble-attachments { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 6px; }
.attach-chip-static { padding: 3px 10px; font-size: 11.5px; opacity: 0.9; }
.composer-inner:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

.composer textarea {
  flex: 1;
  background: transparent;
  border: 0;
  outline: none;
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  line-height: 1.5;
  padding: 8px 0;
  resize: none;
  max-height: 220px;
  overflow-y: auto;
}
.composer textarea::placeholder { color: var(--text-faint); }

.send {
  width: 36px; height: 36px;
  border: 0; border-radius: 50%;
  background: var(--accent);
  color: var(--accent-fg);
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  transition: opacity .15s, transform .08s;
  flex-shrink: 0;
}
.send:hover:not(:disabled) { opacity: 0.88; }
.send:active:not(:disabled) { transform: scale(0.94); }
.send:disabled { background: var(--border-strong); color: var(--text-faint); cursor: not-allowed; }

.send .stop-icon { display: none; }
.composer.running .send { background: var(--danger); color: #fff; }
.composer.running .send-icon { display: none; }
.composer.running .send .stop-icon { display: inline-block; }

/* Scrollbars */
.transcript::-webkit-scrollbar,
.composer textarea::-webkit-scrollbar,
.chat-list::-webkit-scrollbar,
.markdown pre::-webkit-scrollbar { width: 10px; height: 10px; }
.transcript::-webkit-scrollbar-thumb,
.composer textarea::-webkit-scrollbar-thumb,
.chat-list::-webkit-scrollbar-thumb,
.markdown pre::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 10px; }
.transcript::-webkit-scrollbar-thumb:hover { background: var(--text-faint); }
.transcript::-webkit-scrollbar-track { background: transparent; }

/* ---------- Views ---------- */
.view { flex: 1 1 auto; display: flex; flex-direction: column; min-height: 0; }
.view[hidden] { display: none; }

/* Terminal */
#terminalView { padding: 0; }

/* Mobile key-bar — Esc/Tab/Ctrl-C/arrows for devices without those keys. */
/* Softkeys and terminal tabs share one bar. Both inner wrappers are
   display:contents so their buttons become direct flex items here and wrap
   individually — a second row appears only once the first is full. Wrapping
   rather than scrolling means nothing is ever hidden off the right edge. */
.term-bar {
  display: flex; flex-wrap: wrap; align-items: center;
  gap: 4px;
  padding: 6px 8px;
  min-height: 38px;
  background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.term-keybar { display: none; }
.term-keybar .tk {
  flex: 0 0 auto;
  min-width: 44px; min-height: 32px;
  padding: 4px 10px;
  font: inherit; font-size: 13px; font-weight: 500;
  background: var(--bg-subtle); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  cursor: pointer; user-select: none;
}
.term-keybar .tk:active { background: var(--bg-active); }
/* Touch devices of any width get the key bar — iPad landscape is wider than
   the 768px phone breakpoint, and Paste/Copy have no touch alternative. */
@media (hover: none) and (pointer: coarse) {
  .term-keybar { display: contents; }
}

/* Copy overlay — the terminal canvas has no touch selection, so the buffer is
   re-rendered as real text here; native long-press selection + system Copy
   work on it. */
.term-copy {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.term-copy[hidden] { display: none; }
.term-copy-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: min(900px, 94vw); height: min(80vh, 94%);
  display: flex; flex-direction: column;
  box-shadow: var(--shadow-md);
}
.term-copy-head {
  padding: 8px 12px;
  display: flex; align-items: center; gap: 10px;
  border-bottom: 1px solid var(--border);
}
.term-copy-title { color: var(--text-muted); font-size: 13px; margin-right: auto; }
.term-copy-text {
  margin: 0; padding: 12px 14px;
  flex: 1; overflow-y: auto;
  background: #1e1e1e; color: #21ff06;
  font-family: ui-monospace, "SF Mono", Menlo, "DejaVu Sans Mono", Consolas, monospace;
  font-size: 13px; line-height: 1.4;
  white-space: pre-wrap; word-break: break-all;
  user-select: text; -webkit-user-select: text;
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}

/* Tap-to-copy toast for OSC 52 yanks (iOS gates clipboard writes on a gesture) */
.yank-toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(18px + env(safe-area-inset-bottom));
  z-index: 101;
  background: var(--bg-elevated); color: var(--text);
  border: 1px solid var(--border); border-radius: 999px;
  padding: 10px 18px; font-size: 14px;
  box-shadow: var(--shadow-md);
  cursor: pointer;
}
.yank-toast[hidden] { display: none; }

/* Image preview lightbox (Files view) */
.img-preview {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,0.75);
  display: flex; align-items: center; justify-content: center;
}
.img-preview[hidden] { display: none; }
.img-preview-box {
  margin: 0; max-width: 94vw; max-height: 92vh;
  display: flex; flex-direction: column; gap: 8px;
}
.img-preview-box img {
  max-width: 94vw; max-height: calc(92vh - 44px);
  object-fit: contain;
  border-radius: var(--radius-sm);
}
.img-preview-cap {
  display: flex; align-items: center; justify-content: center; gap: 14px;
  color: #eee; font-size: 13px;
  min-height: 32px;
}
.img-preview-cap a { color: #9ecbff; }
.img-preview-cap span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 50vw; }
/* contents — the tabs themselves are the flex items of .term-bar, so they wrap
   alongside the softkeys instead of forming a second block that can't fit. */
.term-tabs { display: contents; }
.term-tab {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 8px 5px 12px;
  background: var(--bg-subtle);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  max-width: 220px;
  user-select: none;
  flex-shrink: 0;      /* wrap to the next row rather than squeeze */
  transition: background .12s;
}
.term-tab:hover { background: var(--bg-hover); }
.term-tab.active {
  background: var(--bg-active);
  border-color: var(--accent);
  box-shadow: inset 0 -2px 0 var(--accent);
  font-weight: 600;
}
/* Drag-to-reorder states: armed = touch hold matured, dragging = in flight. */
.term-tab.drag-armed { transform: scale(1.05); box-shadow: var(--shadow-sm); }
.term-tab.dragging {
  transform: scale(1.05);
  box-shadow: var(--shadow-md);
  opacity: 0.9;
  position: relative; z-index: 5;
  cursor: grabbing;
}
.term-tab .title {
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 160px;
  background: transparent; border: 0; color: inherit; font: inherit; padding: 0;
  cursor: inherit;
}
.term-tab .title[contenteditable="true"] {
  background: var(--bg); cursor: text; padding: 0 2px; border-radius: 3px;
  outline: 1px solid var(--accent);
}
.term-tab .close {
  background: transparent; border: 0; color: inherit; opacity: 0.55;
  font-size: 16px; line-height: 1; cursor: pointer; padding: 0 4px;
}
.term-tab .close:hover { opacity: 1; color: var(--danger); }
.term-new {
  background: transparent; border: 0; color: var(--text-muted);
  padding: 4px 10px; border-radius: var(--radius-sm);
  font: inherit; font-size: 13px;
  cursor: pointer; flex-shrink: 0; margin-left: 4px;
  transition: background .12s;
}
.term-new:hover { background: var(--bg-hover); color: var(--text); }
.term-panes {
  position: relative; flex: 1 1 auto; min-height: 0;
  background: #1e1e1e;
  overflow: hidden;
}
/* touch-action: none — without it, mobile browsers claim vertical swipes as a
   native scroll gesture after the first unprevented touchmove; iOS Safari then
   fires touchcancel and stops delivering the gesture, so the touch-scroll
   handler in app.js never gets to translate it into terminal scrolling. */
.term-pane { position: absolute; inset: 0; padding: 6px 8px; display: none; touch-action: none; }
.term-pane.active { display: block; }
.term-pane .xterm { width: 100%; height: 100%; }
.term-empty { color: var(--text-muted); font-size: 13px; padding: 24px; text-align: center; }

/* Users view */
.users-wrap { max-width: 960px; width: 100%; margin: 0 auto; padding: 28px 24px 48px; }
.users-header { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }
.users-header h2 { margin: 0; font-size: 24px; font-weight: 600; color: var(--text); letter-spacing: -0.01em; }
.users-header .spacer { flex: 1; }

.primary-btn {
  background: var(--accent); color: var(--accent-fg);
  border: 0; border-radius: var(--radius-pill);
  padding: 8px 18px;
  font: inherit; font-weight: 600; font-size: 13.5px;
  cursor: pointer;
  margin-left: auto;
  transition: opacity .15s, transform .06s;
}
.primary-btn:hover { opacity: 0.9; }
.primary-btn:active { transform: scale(0.98); }

.users-table {
  width: 100%;
  border-collapse: separate; border-spacing: 0;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  font-size: 14px;
}
.users-table th, .users-table td {
  padding: 11px 14px; text-align: left;
  border-bottom: 1px solid var(--border);
  color: var(--text);
}
.users-table th {
  background: var(--bg-subtle);
  color: var(--text-muted);
  font-weight: 600; font-size: 12px;
  text-transform: uppercase; letter-spacing: .04em;
}
.users-table tr:last-child td { border-bottom: 0; }
.users-table input[type="checkbox"] { cursor: pointer; }
.you-tag {
  display: inline-block;
  font-size: 11px;
  padding: 1.5px 7px;
  border-radius: var(--radius-pill);
  background: var(--tool-bg);
  color: var(--tool-text);
  margin-left: 6px;
}

.row-actions { display: flex; gap: 6px; }
.row-actions .ghost.small { padding: 5px 12px; font-size: 12.5px; border-radius: var(--radius-sm); }
.row-actions .ghost.small.danger { color: var(--danger); border-color: rgba(226,76,76,0.35); }
.row-actions .ghost.small.danger:hover { background: rgba(226,76,76,0.12); }

.add-user-form {
  margin-top: 14px;
  padding: 14px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
}
.add-user-form input:not([type="checkbox"]) {
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  padding: 8px 12px; font: inherit; font-size: 14px;
  min-width: 200px;
}
.add-user-form input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--focus-ring); }
.add-user-form .chk { color: var(--text); font-size: 14px; display: inline-flex; align-items: center; gap: 6px; }

.user-msg {
  margin-top: 14px;
  color: var(--text-muted);
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  min-height: 18px;
  word-break: break-all;
}

/* ===================== Mobile ===================== */
@media (max-width: 768px) {
  .chat-body {
    width: 100vw; height: 100dvh;
    margin: 0;
    border: 0; border-radius: 0;
    box-shadow: none;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: 84vw;
    max-width: 320px;
    z-index: 100;
    transform: translateX(0);
    transition: transform 0.22s ease;
    box-shadow: var(--shadow-md);
  }
  .sidebar.collapsed {
    transform: translateX(-100%);
    width: 84vw;
    flex-basis: auto;
    border-right-width: 1px;
  }
  body.has-mobile-drawer::before {
    content: '';
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 99;
    animation: dm-fade 0.22s ease;
  }
  @keyframes dm-fade { from { opacity: 0 } to { opacity: 1 } }

  .main { width: 100%; }

  .sidebar-link, .new-chat, .ghost, .chat-item, .term-tab { min-height: 42px; }

  .term-keybar { display: contents; }

  /* iOS: 16px font stops auto-zoom on focus */
  #cmd, .composer textarea,
  .login-card input, .add-user-form input { font-size: 16px; }

  .chat-header { padding: 8px 12px; gap: 8px; min-height: 48px; }
  .chat-header .brand { display: none; }
  .chat-header .chat-title { max-width: none; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .chat-header #status { display: none; }

  .transcript { padding: 16px 14px 160px; }

  .users-wrap { padding: 16px; }
  .users-table { display: block; overflow-x: auto; white-space: nowrap; }

  .chat-list, .users-table, #transcript { -webkit-overflow-scrolling: touch; }

  /* Keyboard-aware composer: visualViewport gives us --kb-inset.
     On mobile we pin the composer with fixed positioning to the keyboard's top
     so iOS Safari doesn't slide it behind the keyboard. */
  .composer {
    position: fixed;
    left: 0; right: 0;
    bottom: var(--kb-inset, 0px);
    z-index: 10;
    padding: 10px 14px max(10px, env(safe-area-inset-bottom));
    background: var(--bg);
    transition: bottom .18s ease;
  }
  html.kb-open .composer { padding-bottom: 10px; }

  /* Transcript needs bottom breathing room for the fixed composer + keyboard */
  .transcript {
    padding-bottom: calc(140px + var(--kb-inset, 0px));
  }

  /* Terminal view: shrink the panes above the keyboard so the xterm prompt
     isn't covered. ResizeObserver in app.js refits xterm on size change. */
  #terminalView {
    padding-bottom: var(--kb-inset, 0px);
    transition: padding-bottom .18s ease;
  }
}


/* ===================== Folded chrome =====================
   Collapsing the sidebar (desktop/tablet widths) also hides the top header
   line; a floating hamburger in the corner brings both back. On phones the
   sidebar is an overlay drawer and the header keeps its toggle, so folding
   is desktop/tablet-only (app.js gates on the same 769px breakpoint). */
.unfold-btn {
  position: fixed; top: 8px; left: 8px; z-index: 60;
  width: 36px; height: 36px;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--bg-elevated);
  color: var(--text-muted);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  cursor: pointer;
  opacity: 0.75;
  transition: opacity .15s, background .15s;
}
.unfold-btn:hover { opacity: 1; background: var(--bg-hover); color: var(--text); }
.unfold-btn[hidden] { display: none; }

/* Fullscreen toggles (header button + floating twin shown while folded) —
   swap expand/compress icon with the fullscreen state. */
.fs-toggle .fs-compress { display: none; }
html.is-fullscreen .fs-toggle .fs-expand { display: none; }
html.is-fullscreen .fs-toggle .fs-compress { display: inline; }
.fs-toggle[hidden] { display: none; }
.fs-float { left: 52px; }

@media (min-width: 769px) {
  body.chrome-folded .chat-header { display: none; }
  /* Keep the first terminal tab clear of the floating buttons. */
  body.chrome-folded .term-bar { padding-left: 96px; }
}
/* Touch devices (iPad) show the key bar, so while folded it is the top row.
   Keep the softkeys hugging the left edge: park the floating buttons in the
   top-right corner instead of indenting the bar. */
@media (min-width: 769px) and (hover: none) and (pointer: coarse) {
  body.chrome-folded .term-bar { padding-left: 0; }
  .unfold-btn { left: auto; right: 8px; }
  .fs-float { left: auto; right: 52px; }
}

/* ===================== Tablet keyboard (iPad) =====================
   app.js sets --kb-inset at any width, but the keyboard-aware rules above
   live inside the <=768px media query. An iPad viewport is 810-1024px wide,
   so it gets the desktop layout and the on-screen keyboard covers the
   composers. Lift them here too; hardware-keyboard desktops never get
   .kb-open so this is inert for them. Uses transform (not fixed) so the
   composer stays horizontally aligned with .main next to the sidebar. */
@media (min-width: 769px) {
  html.kb-open .composer {
    transform: translateY(calc(-1 * var(--kb-inset, 0px)));
    transition: transform .18s ease;
  }
  html.kb-open .transcript {
    padding-bottom: calc(40px + var(--kb-inset, 0px));
  }
  html.kb-open #terminalView {
    padding-bottom: var(--kb-inset, 0px);
  }
}

button, .sidebar-link, .chat-item, .term-tab {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Files view — per-user uploaded files with drag-drop + list/download/delete. */
.files-wrap { max-width: 960px; width: 100%; margin: 0 auto; padding: 28px 24px 48px; }
.files-header { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }
.files-header h2 { margin: 0; font-size: 24px; font-weight: 600; color: var(--text); letter-spacing: -0.01em; flex: 1; }
.files-header label.primary-btn { cursor: pointer; }
.file-drop {
  border: 2px dashed var(--border, #444);
  border-radius: 8px;
  padding: 22px;
  text-align: center;
  color: var(--muted, #888);
  margin-bottom: 16px;
  transition: background 0.12s, border-color 0.12s;
}
.file-drop.dragover {
  background: rgba(14, 99, 156, 0.12);
  border-color: #0e639c;
  color: var(--text);
}
.files-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.files-table th, .files-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border, #333);
  text-align: left;
  vertical-align: middle;
}
.files-table th {
  color: var(--muted, #888);
  font-weight: 500;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.files-table td a { color: #4aa3df; text-decoration: none; }
.files-table td a:hover { text-decoration: underline; }
.files-table .files-empty { text-align: center; color: var(--muted, #888); padding: 24px 0; }
.files-table .row-actions { text-align: right; white-space: nowrap; }

