:root {
  --bg: #0e0e14;
  --surface: #181822;
  --surface-2: #20202e;
  --surface-3: #2a2a3a;
  --border: #2c2c3c;
  --text: #ecedf3;
  --muted: #9a9bb0;
  --faint: #6c6d82;
  --accent: #7c6cff;
  --accent-2: #9b8dff;
  --accent-press: #6957f0;
  --danger: #ff6b6b;
  --radius: 16px;
  --radius-sm: 10px;
  --header-h: 56px;
  --drawer-w: 300px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overscroll-behavior: none;
}
body { overflow: hidden; }
button { font-family: inherit; cursor: pointer; }
.hidden { display: none !important; }

/* ---------- Login ---------- */
.login {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: calc(var(--safe-top) + 24px) 24px calc(var(--safe-bottom) + 24px);
  background:
    radial-gradient(1200px 600px at 50% -10%, rgba(124,108,255,.22), transparent 60%),
    var(--bg);
}
.login-card { width: 100%; max-width: 360px; text-align: center; }
.login-mark {
  width: 76px; height: 76px; margin: 0 auto 22px;
  display: grid; place-items: center;
  font-size: 38px; line-height: 1;
  border-radius: 22px;
  background: linear-gradient(160deg, var(--accent), var(--accent-press));
  box-shadow: 0 12px 40px rgba(124,108,255,.4);
  color: #fff;
}
.login-title { margin: 0; font-size: 30px; font-weight: 700; letter-spacing: -.02em; }
.login-sub { margin: 8px 0 32px; color: var(--muted); font-size: 15px; }
.btn-login {
  width: 100%; min-height: 52px; border: none; border-radius: 14px;
  background: var(--accent); color: #fff;
  font-size: 16px; font-weight: 600;
  transition: background .15s, transform .05s;
}
.btn-login:hover { background: var(--accent-2); }
.btn-login:active { transform: scale(.985); background: var(--accent-press); }

/* ---------- App shell ---------- */
.app { display: flex; height: 100%; width: 100%; position: relative; }

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

/* ---------- Drawer ---------- */
.drawer {
  width: var(--drawer-w); flex: 0 0 var(--drawer-w);
  display: flex; flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding-top: var(--safe-top);
  padding-left: var(--safe-left);
}
.drawer-head {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 14px 12px;
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 17px; letter-spacing: -.01em; flex: 1; }
.btn-new {
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-2); color: var(--text);
  font-size: 14px; font-weight: 600; padding: 9px 12px;
  min-height: 40px; white-space: nowrap;
  transition: background .15s, border-color .15s;
}
.btn-new:hover { background: var(--surface-3); border-color: var(--accent); }
.btn-new:active { transform: scale(.98); }

.chat-list { flex: 1; overflow-y: auto; padding: 8px; }
.chat-item {
  display: block; width: 100%; text-align: left;
  border: none; background: transparent; color: var(--text);
  padding: 11px 12px; border-radius: 12px; margin-bottom: 2px;
  min-height: 48px;
  transition: background .12s;
}
.chat-item:hover { background: var(--surface-2); }
.chat-item.active { background: var(--surface-3); }
.chat-item .ci-title {
  font-size: 14.5px; font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  display: block;
}
.chat-item .ci-time { font-size: 12px; color: var(--faint); margin-top: 2px; display: block; }
.list-empty { color: var(--faint); font-size: 13px; padding: 16px 12px; text-align: center; }

/* ---------- Main pane ---------- */
.pane {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column;
  height: 100%;
  background: var(--bg);
}
.topbar {
  display: flex; align-items: center; gap: 8px;
  height: calc(var(--header-h) + var(--safe-top));
  padding: var(--safe-top) calc(12px + var(--safe-right)) 0 12px;
  border-bottom: 1px solid var(--border);
  background: rgba(14,14,20,.82);
  backdrop-filter: saturate(160%) blur(12px);
  -webkit-backdrop-filter: saturate(160%) blur(12px);
  position: relative; z-index: 10;
  flex: 0 0 auto;
}
.icon-btn {
  width: 40px; height: 40px; flex: 0 0 40px;
  display: grid; place-items: center;
  border: none; background: transparent; color: var(--text);
  font-size: 20px; border-radius: 10px;
  transition: background .12s;
}
.icon-btn:hover { background: var(--surface-2); }
.chat-title {
  margin: 0; font-size: 16px; font-weight: 600; letter-spacing: -.01em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topbar-spacer { flex: 1; }

.avatar-wrap { position: relative; }
.avatar-btn {
  width: 38px; height: 38px; padding: 0;
  border: none; background: transparent; border-radius: 50%;
}
.avatar {
  width: 36px; height: 36px; border-radius: 50%;
  display: grid; place-items: center;
  background: linear-gradient(160deg, var(--accent), var(--accent-press));
  color: #fff; font-weight: 600; font-size: 15px;
  background-size: cover; background-position: center;
  border: 1px solid rgba(255,255,255,.12);
}
.menu {
  position: absolute; top: 46px; right: 0; z-index: 40;
  min-width: 180px;
  background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 14px;
  padding: 6px; box-shadow: 0 16px 48px rgba(0,0,0,.5);
}
.menu-name {
  font-size: 13px; color: var(--muted);
  padding: 8px 10px 6px; border-bottom: 1px solid var(--border); margin-bottom: 4px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.menu-item {
  display: block; width: 100%; text-align: left;
  border: none; background: transparent; color: var(--text);
  padding: 10px; border-radius: 9px; font-size: 14px; min-height: 40px;
}
.menu-item:hover { background: var(--surface-3); }

/* ---------- Messages ---------- */
.messages {
  flex: 1; min-height: 0;
  overflow-y: auto; overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 16px calc(14px + var(--safe-right)) 8px calc(14px + var(--safe-left));
  display: flex; flex-direction: column; gap: 10px;
}
.row { display: flex; width: 100%; flex-wrap: wrap; }
.row.user { justify-content: flex-end; }
.row.assistant { justify-content: flex-start; }
.msg-time {
  flex-basis: 100%;
  font-size: 11px;
  color: var(--muted, #767b8e);
  margin-top: 3px;
  padding: 0 4px;
  opacity: .7;
}
.row.user .msg-time { text-align: right; }
.row.assistant .msg-time, .row.tool .msg-time { text-align: left; }

.bubble {
  max-width: min(82%, 640px);
  padding: 10px 14px; border-radius: 18px;
  font-size: 15.5px; line-height: 1.45;
  word-wrap: break-word; overflow-wrap: anywhere;
}
.row.user .bubble {
  background: var(--accent); color: #fff;
  border-bottom-right-radius: 6px;
}
.row.assistant .bubble {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}

.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble a { color: var(--accent-2); text-decoration: underline; }
.row.user .bubble a { color: #fff; }
.bubble code.inline {
  background: rgba(255,255,255,.1); padding: 1px 5px; border-radius: 6px;
  font-family: var(--mono); font-size: .88em;
}
.bubble pre {
  background: #0b0b12; border: 1px solid var(--border);
  border-radius: 12px; padding: 12px; margin: 8px 0;
  overflow-x: auto;
}
.bubble pre code { font-family: var(--mono); font-size: 13px; line-height: 1.5; color: #d6d8e6; }
.bubble strong { font-weight: 700; }

/* tool chip */
.row.tool { justify-content: flex-start; }
.tool-chip {
  max-width: min(88%, 640px);
  font-family: var(--mono); font-size: 12.5px;
  color: var(--muted);
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 10px; padding: 7px 11px;
  white-space: pre-wrap; word-break: break-word;
  display: flex; align-items: center; gap: 6px;
}

/* system / status centered */
.row.system { justify-content: center; }
.system-note {
  font-size: 12.5px; color: var(--faint);
  background: var(--surface); border: 1px solid var(--border);
  padding: 5px 12px; border-radius: 999px; text-align: center;
}

/* ---------- Empty state ---------- */
.empty {
  margin: auto; text-align: center; padding: 40px 20px; color: var(--muted);
}
.empty-mark { font-size: 34px; color: var(--accent-2); }
.empty-title { font-size: 18px; font-weight: 600; color: var(--text); margin: 12px 0 4px; }
.empty-sub { font-size: 14px; margin: 0 0 18px; }

/* ---------- Typing indicator ---------- */
.typing {
  display: flex; gap: 6px; align-items: center; width: fit-content;
  margin: 2px 0 6px calc(20px + var(--safe-left));
  padding: 7px 14px;
  background: var(--surface-2);
  border: 1px solid var(--accent);
  border-radius: 14px;
  box-shadow: 0 0 0 3px rgba(124, 108, 255, .12);
}
.typing::before {
  content: "Working"; color: var(--accent-2); font-weight: 600; font-size: 13px;
}
.typing-dot {
  width: 7px; height: 7px; border-radius: 50%; background: var(--accent-2);
  animation: bounce 1.3s infinite ease-in-out;
}
.typing-dot:nth-child(2) { animation-delay: .18s; }
.typing-dot:nth-child(3) { animation-delay: .36s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .5; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* ---------- Composer ---------- */
.composer {
  flex: 0 0 auto;
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px calc(12px + var(--safe-right)) calc(10px + var(--safe-bottom)) calc(12px + var(--safe-left));
  border-top: 1px solid var(--border);
  background: rgba(14,14,20,.92);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.input {
  flex: 1; resize: none;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 20px;
  padding: 11px 15px; font-size: 16px; line-height: 1.4;
  font-family: inherit; max-height: 140px; min-height: 44px;
  outline: none;
  transition: border-color .15s;
}
.input::placeholder { color: var(--faint); }
.input:focus { border-color: var(--accent); }
.send-btn {
  flex: 0 0 44px; width: 44px; height: 44px;
  display: grid; place-items: center;
  border: none; border-radius: 50%;
  background: var(--accent); color: #fff;
  transition: background .15s, transform .05s, opacity .15s;
}
.send-btn:hover:not(:disabled) { background: var(--accent-2); }
.send-btn:active:not(:disabled) { transform: scale(.92); }
.send-btn:disabled { opacity: .4; cursor: default; }
.send-btn svg { transform: translateX(1px); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(96px + var(--safe-bottom));
  transform: translateX(-50%) translateY(8px);
  background: var(--danger); color: #fff;
  padding: 11px 16px; border-radius: 12px;
  font-size: 14px; font-weight: 500; z-index: 60;
  max-width: 90%; text-align: center;
  box-shadow: 0 10px 30px rgba(0,0,0,.4);
  opacity: 0; transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Mobile ---------- */
.only-mobile { display: none; }
@media (max-width: 560px) {
  .only-mobile { display: grid; }
  .drawer {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 35;
    width: 86vw; max-width: var(--drawer-w);
    transform: translateX(-100%);
    transition: transform .26s cubic-bezier(.4,0,.2,1);
    padding-bottom: var(--safe-bottom);
    box-shadow: 0 0 40px rgba(0,0,0,.5);
  }
  .app.drawer-open .drawer { transform: translateX(0); }
  .pane { width: 100%; }
  .bubble { max-width: 86%; font-size: 16px; }
}

@media (min-width: 561px) {
  .scrim { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}

/* trust-tier badge (shown for restricted, non-owner users) */
.tier-badge {
  display: inline-block;
  margin-left: 8px;
  padding: 2px 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .02em;
  text-transform: uppercase;
  color: #f0b429;
  background: rgba(240, 180, 41, .12);
  border: 1px solid rgba(240, 180, 41, .35);
  border-radius: 999px;
  vertical-align: middle;
  white-space: nowrap;
}
.tier-badge[hidden] { display: none; }

/* ---------- Drawer actions / folders ---------- */
.drawer-actions { display: flex; align-items: center; gap: 6px; }

.folder-head {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px 6px; margin-top: 4px;
  color: var(--muted); font-size: 12px; font-weight: 600;
  letter-spacing: .03em; text-transform: uppercase;
  border-radius: 8px; cursor: pointer; user-select: none;
}
.folder-head:hover { background: var(--surface-2); }
.folder-head.plain { cursor: default; }
.folder-head.plain:hover { background: transparent; }
.folder-caret { width: 12px; font-size: 10px; color: var(--faint); }
.folder-name {
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.folder-count {
  font-size: 11px; color: var(--faint);
  background: var(--surface-2); border-radius: 999px;
  padding: 1px 7px; min-width: 18px; text-align: center;
}
.folder-menu-btn {
  border: none; background: transparent; color: var(--faint);
  font-size: 16px; line-height: 1; padding: 2px 6px; border-radius: 6px;
}
.folder-menu-btn:hover { background: var(--surface-3); color: var(--text); }
.folder-empty { font-size: 12px; color: var(--faint); padding: 6px 12px 8px 28px; }

/* chat row = chat item + actions menu */
.chat-row {
  display: flex; align-items: stretch; gap: 2px;
  border-radius: 12px; margin-bottom: 2px;
}
.chat-row.active { background: var(--surface-3); }
.chat-row .chat-item { flex: 1; min-width: 0; margin-bottom: 0; }
.chat-row.active .chat-item { background: transparent; }
.chat-menu-btn {
  flex: 0 0 32px; border: none; background: transparent;
  color: var(--faint); font-size: 18px; line-height: 1;
  border-radius: 10px; opacity: 0; transition: opacity .12s, background .12s;
}
.chat-row:hover .chat-menu-btn, .chat-row.active .chat-menu-btn { opacity: 1; }
.chat-menu-btn:hover { background: var(--surface-2); color: var(--text); }
@media (max-width: 560px) { .chat-menu-btn { opacity: 1; } }

/* ---------- Popup menu (chat / folder actions) ---------- */
.popmenu {
  position: fixed; z-index: 70; min-width: 184px; max-width: 240px;
  background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 14px;
  padding: 6px; box-shadow: 0 16px 48px rgba(0,0,0,.55);
}
.popmenu[hidden] { display: none; }
.popmenu-header {
  font-size: 11px; color: var(--faint); text-transform: uppercase;
  letter-spacing: .04em; padding: 6px 10px 4px;
}
.popmenu-item {
  display: block; width: 100%; text-align: left;
  border: none; background: transparent; color: var(--text);
  padding: 10px; border-radius: 9px; font-size: 14px; min-height: 40px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.popmenu-item:hover { background: var(--surface-3); }
.popmenu-item.danger { color: var(--danger); }

/* ---------- Context meter ---------- */
.ctx-meter {
  display: flex; align-items: center; gap: 7px;
  border: 1px solid var(--border); background: var(--surface);
  border-radius: 999px; padding: 4px 10px 4px 8px;
  color: var(--muted); font-size: 12px; font-weight: 600;
  transition: border-color .15s, background .15s;
}
.ctx-meter:hover { border-color: var(--accent); }
.ctx-bar {
  width: 38px; height: 6px; border-radius: 999px;
  background: var(--surface-3); overflow: hidden;
}
.ctx-fill {
  display: block; height: 100%; width: 0;
  background: var(--accent); border-radius: 999px;
  transition: width .3s ease, background .3s;
}
.ctx-fill.warm { background: #f0b429; }
.ctx-fill.hot { background: var(--danger); }
.ctx-text { font-variant-numeric: tabular-nums; }

.ctx-detail {
  position: absolute; top: calc(var(--header-h) + var(--safe-top) - 4px);
  right: calc(56px + var(--safe-right)); z-index: 40;
  min-width: 210px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 14px; padding: 12px 14px;
  box-shadow: 0 16px 48px rgba(0,0,0,.5);
  font-size: 13px;
}
.ctx-detail[hidden] { display: none; }
.ctx-detail .cd-row {
  display: flex; justify-content: space-between; gap: 16px;
  padding: 4px 0; color: var(--muted);
}
.ctx-detail .cd-row b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---- markdown readability inside assistant bubbles ---- */
.bubble .md-h { margin: 12px 0 6px; line-height: 1.25; font-weight: 700; }
.bubble .md-h:first-child { margin-top: 0; }
.bubble h3.md-h { font-size: 17px; }
.bubble h4.md-h { font-size: 15px; color: var(--accent-2); }
.bubble h5.md-h { font-size: 13.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.bubble ul, .bubble ol { margin: 6px 0; padding-left: 22px; }
.bubble li { margin: 3px 0; }
.bubble li::marker { color: var(--accent-2); }

/* ---- code blocks with copy button ---- */
.bubble .codeblock { position: relative; margin: 8px 0; }
.bubble .codeblock pre { margin: 0; }
.copy-btn {
  position: absolute; top: 6px; right: 6px;
  font: 600 11px var(--font, inherit);
  padding: 3px 9px; border-radius: 7px;
  background: var(--surface-3); color: var(--muted);
  border: 1px solid rgba(255,255,255,.08);
  cursor: pointer; opacity: .65; transition: opacity .15s, background .15s, color .15s;
}
.bubble .codeblock:hover .copy-btn { opacity: 1; }
.copy-btn:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.copy-btn.copied { background: #2e7d4f; color: #fff; border-color: #2e7d4f; opacity: 1; }
@media (hover: none) { .copy-btn { opacity: 1; } }  /* always visible on touch */

/* ---- owner web terminal overlay ---- */
.term-overlay {
  position: fixed; inset: 0; z-index: 60;
  display: flex; flex-direction: column;
  background: #0b0b11;
  padding-top: var(--safe-top); padding-bottom: var(--safe-bottom);
  padding-left: var(--safe-left); padding-right: var(--safe-right);
}
.term-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; border-bottom: 1px solid var(--border); background: var(--surface);
  flex: 0 0 auto;
}
.term-title { font: 600 13px var(--mono); color: var(--accent-2); }
.term-close {
  width: 34px; height: 34px; border: none; border-radius: 9px;
  background: var(--surface-2); color: var(--text); font-size: 15px;
}
.term-close:hover { background: var(--surface-3); }
.term-body { flex: 1; min-height: 0; padding: 6px 8px; overflow: hidden; }
.term-body .xterm, .term-body .xterm-viewport, .term-body .xterm-screen { height: 100% !important; }
