:root {
  --bg: #FFFFFF;
  --sidebar: #F9F9F9;
  --sidebar-hover: #ECECEC;
  --sidebar-active: #ECECEC;
  --border: #E5E5E5;
  --input-border: #D9D9E3;
  --text: #0D0D0D;
  --muted: #6B6B6B;
  --bubble: #F7F7F8;
  --user-bubble: #E9EDF3;
  --code-bg: #0D0D0D;
  --code-text: #E6E6E6;
  --accent: #10A37F;
  --accent-soft: #E7F4F0;
  --amber: #B45309;
  --amber-bg: #FFF7E6;
  --amber-border: #F5D9A8;
  --red: #D32F2F;
  --shadow: 0 2px 10px rgba(0, 0, 0, .08);
  --radius: 14px;
  --mono: ui-monospace, "Cascadia Code", Consolas, monospace;
}

body.dark {
  --bg: #212121;
  --sidebar: #171717;
  --sidebar-hover: #2A2A2A;
  --sidebar-active: #2A2A2A;
  --border: #3E3E3E;
  --input-border: #4D4D4F;
  --text: #ECECEC;
  --muted: #A6A6A6;
  --bubble: #303030;
  --user-bubble: #3B3B3B;
  --code-bg: #0D0D0D;
  --code-text: #D4D4D4;
  --accent-soft: #1A3A32;
  --amber-bg: #33291A;
  --amber-border: #5C4A26;
  --shadow: 0 2px 10px rgba(0, 0, 0, .45);
}

/* ---------- login ---------- */
.login-page {
  background: var(--bg);
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-wrap { width: 100%; max-width: 400px; }

.login-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 30px 28px;
  box-shadow: var(--shadow);
  text-align: center;
}

.login-logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 54px;
  height: 54px;
  border-radius: 15px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 1.1rem;
  margin-bottom: 14px;
}

.login-card h1 { margin: 0 0 4px; font-size: 1.35rem; color: var(--text); }
.login-sub { margin: 0 0 20px; color: var(--muted); font-size: .9rem; }

.login-form { display: grid; gap: 10px; text-align: left; }

.login-form label {
  font-size: .85rem;
  font-weight: 500;
  color: var(--muted);
}

.login-form input {
  width: 100%;
  border: 1px solid var(--input-border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  padding: 12px 14px;
  outline: none;
}

.login-form input:focus { border-color: var(--text); }

.login-btn {
  margin-top: 6px;
  border: none;
  background: var(--text);
  color: var(--bg);
  border-radius: 999px;
  padding: 12px;
  font-size: .95rem;
  font-weight: 600;
  cursor: pointer;
}

.login-btn:hover { opacity: .85; }

.alert-error {
  background: var(--amber-bg);
  border: 1px solid var(--amber-border);
  color: var(--amber);
  border-radius: 12px;
  padding: 10px 14px;
  margin: 0 0 14px;
  font-size: .85rem;
  text-align: left;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.55;
  height: 100%;
}

button { font-family: inherit; }

.app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ---------- sidebar ---------- */
.sidebar {
  width: 264px;
  flex: 0 0 264px;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 30;
}

.side-top { padding: 12px 12px 8px; }

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  font-size: 1rem;
  padding: 8px 8px 14px;
  color: var(--text);
}

.logo, .empty-logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: .78rem;
  flex: 0 0 auto;
}

.new-chat {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  font-size: .92rem;
  font-weight: 500;
  cursor: pointer;
}

.new-chat:hover { background: var(--sidebar-hover); }
.new-chat.big {
  width: auto;
  margin: 16px auto 0;
  padding: 12px 22px;
  border-radius: 999px;
  font-weight: 600;
}

.search-input {
  margin-top: 8px;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: .88rem;
  font-family: inherit;
  padding: 9px 12px;
  outline: none;
}

.search-input:focus { border-color: var(--text); }

.side-scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 4px 8px;
}

.thread-list { display: grid; grid-template-columns: minmax(0, 1fr); gap: 2px; }

.thread-item {
  display: block;
  width: 100%;
  min-width: 0;
  text-align: left;
  background: transparent;
  border: none;
  border-radius: 8px;
  padding: 9px 10px;
  color: var(--text);
  cursor: pointer;
  font-size: .88rem;
}

.thread-item:hover { background: var(--sidebar-hover); }
.thread-item.active { background: var(--sidebar-active); }
.thread-item .t-name {
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.thread-item .t-name span:first-child { overflow: hidden; text-overflow: ellipsis; }
.thread-item .t-preview {
  color: var(--muted);
  font-size: .78rem;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.thread-item .t-time { display: none; }
.t-menu {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.05rem;
  line-height: 1;
  padding: 0 3px;
  margin-left: auto;
  flex: 0 0 auto;
}

.t-menu:hover { color: var(--text); }
.t-del {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: .9rem;
  line-height: 1;
  padding: 0 3px;
  flex: 0 0 auto;
}

.t-del:hover { color: var(--red); }
.t-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #A8A8A8;
  flex: 0 0 auto;
}
.t-dot.running { background: var(--accent); }
.t-dot.awaiting { background: #E5A50A; }
.t-dot.error { background: var(--red); }
.t-count {
  background: var(--amber-bg);
  color: var(--amber);
  border: 1px solid var(--amber-border);
  font-size: .68rem;
  font-weight: 700;
  border-radius: 999px;
  padding: 0 7px;
  flex: 0 0 auto;
}

.side-bottom {
  border-top: 1px solid var(--border);
  padding: 10px 12px 12px;
  display: grid;
  gap: 8px;
}

.signed-in {
  color: var(--muted);
  font-size: .78rem;
  padding: 0 2px;
}

.bridge-note {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: .8rem;
}

.bridge-note .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #A8A8A8;
  flex: 0 0 auto;
}

.bridge-note.online .dot { background: var(--accent); }
.bridge-note.offline .dot { background: var(--red); }

.side-actions { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

.version {
  color: var(--muted);
  font-size: .72rem;
  margin-left: auto;
  padding: 0 4px;
}

.icon-btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  border-radius: 8px;
  width: 34px;
  height: 34px;
  cursor: pointer;
  font-size: 1rem;
}

.icon-btn:hover { background: var(--sidebar-hover); }

.logout {
  color: var(--muted);
  text-decoration: none;
  font-size: .82rem;
  padding: 8px 10px;
  border-radius: 8px;
}

.logout:hover { background: var(--sidebar-hover); color: var(--text); }

.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .4);
  z-index: 25;
}

.menu-btn {
  display: none;
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 15;
  width: 38px;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 1.1rem;
  cursor: pointer;
  box-shadow: var(--shadow);
}

/* ---------- main ---------- */
.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}

.chat-head {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 14px 16px 4px;
  font-size: .85rem;
  color: var(--muted);
}

.chat-title { font-weight: 600; color: var(--text); max-width: 40%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-meta { max-width: 34%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.stop-btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  border-radius: 999px;
  padding: 5px 14px;
  font-size: .8rem;
  cursor: pointer;
}

.stop-btn:hover { background: var(--sidebar-hover); }

.usage-chip {
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: .72rem;
  font-weight: 600;
  white-space: nowrap;
}

.working-chip {
  background: var(--amber-bg);
  color: var(--amber);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: .72rem;
  font-weight: 600;
  white-space: nowrap;
}

.feed-wrap {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}

.feed {
  max-width: 760px;
  margin: 0 auto;
  padding: 24px 20px 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.blk { max-width: 100%; }

.blk-turn {
  color: var(--muted);
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  text-align: center;
  padding: 4px 0;
}

.blk-msg {
  display: flex;
  gap: 10px;
  max-width: 88%;
  align-self: flex-start;
  min-width: 0;
}

.blk-msg.user { align-self: flex-end; }
.blk-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: .68rem;
  font-weight: 700;
  flex: 0 0 auto;
  margin-top: 2px;
}

.blk-msg .bubble {
  padding: 10px 14px;
  border-radius: 14px;
  font-size: .95rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-wrap: break-word;
  min-width: 0;
}

.blk-msg.assistant .bubble { background: var(--bubble); border-bottom-left-radius: 4px; }
.blk-msg.user .bubble { background: var(--user-bubble); border-bottom-right-radius: 4px; }

.copy-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: .85rem;
  align-self: flex-end;
  padding: 2px 4px;
  flex: 0 0 auto;
  opacity: .5;
}

.copy-btn:hover { opacity: 1; color: var(--text); }

.blk-msg .bubble p { margin: 0 0 8px; }
.blk-msg .bubble p:last-child { margin-bottom: 0; }
.blk-msg .bubble ul { margin: 4px 0 8px; padding-left: 20px; }
.blk-msg .bubble li { margin: 2px 0; }
.blk-msg .bubble .md-h { margin: 10px 0 4px; font-size: 1em; }
.blk-msg .bubble pre.md-code {
  background: var(--code-bg);
  color: var(--code-text);
  padding: 8px 10px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: .78rem;
  margin: 8px 0;
  font-family: var(--mono);
}
.blk-msg .bubble code {
  background: var(--sidebar-hover);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: var(--mono);
  font-size: .82em;
}
.blk-msg .bubble a { color: var(--accent); }

.blk-tool { align-self: flex-start; width: min(92%, 640px); }
.blk-tool-inner {
  background: var(--sidebar-hover);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-family: var(--mono);
  font-size: .76rem;
}
.blk-tool-inner summary {
  cursor: pointer;
  padding: 7px 10px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.blk-tool-inner pre {
  margin: 0;
  padding: 8px 10px;
  border-top: 1px solid var(--border);
  white-space: pre-wrap;
  word-wrap: break-word;
  max-height: 260px;
  overflow-y: auto;
  color: var(--text);
  font-family: var(--mono);
  font-size: .74rem;
}

.blk-diff { align-self: flex-start; width: min(92%, 640px); }
.blk-diff-inner {
  background: var(--code-bg);
  color: var(--code-text);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-family: var(--mono);
  font-size: .74rem;
}
.blk-diff-inner summary {
  cursor: pointer;
  padding: 7px 10px;
  color: var(--muted);
}
.blk-diff-inner pre {
  margin: 0;
  padding: 8px 10px;
  border-top: 1px solid var(--border);
  white-space: pre-wrap;
  word-wrap: break-word;
  max-height: 300px;
  overflow-y: auto;
}
.blk-note { color: var(--muted); font-size: .8rem; }
.older-btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: .8rem;
  cursor: pointer;
  align-self: flex-start;
}

.older-btn:hover { background: var(--sidebar-hover); color: var(--text); }
.blk-img { align-self: flex-start; max-width: min(320px, 82%); }
.blk-img img {
  width: 100%;
  display: block;
  border-radius: 12px;
  border: 1px solid var(--border);
}
.blk-file { align-self: flex-start; }
.blk-file a {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--accent);
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  background: var(--sidebar-hover);
  font-size: .82rem;
}
.blk-alert {
  background: var(--amber-bg);
  border: 1px solid var(--amber-border);
  color: var(--amber);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: .85rem;
  white-space: pre-wrap;
}

/* ---------- approval ---------- */
.approval-area {
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  padding: 8px 20px 0;
}

.approval {
  background: var(--amber-bg);
  border: 1px solid var(--amber-border);
  border-radius: 14px;
  padding: 12px 14px;
  margin-bottom: 8px;
}

.approval h3 { margin: 0 0 4px; font-size: .9rem; color: var(--amber); }
.approval .a-cmd {
  font-family: var(--mono);
  font-size: .8rem;
  background: var(--code-bg);
  color: var(--code-text);
  border-radius: 8px;
  padding: 8px 10px;
  white-space: pre-wrap;
  word-wrap: break-word;
  margin: 8px 0;
}
.approval .a-reason { color: var(--muted); font-size: .82rem; margin: 0 0 10px; }
.approval .a-actions { display: flex; gap: 8px; }
.btn-approve, .btn-deny {
  border-radius: 999px;
  padding: 8px 18px;
  font-size: .85rem;
  font-weight: 600;
  cursor: pointer;
}
.btn-approve { background: var(--accent); color: #fff; border: none; }
.btn-approve:hover { opacity: .9; }
.btn-deny { background: transparent; color: var(--red); border: 1px solid var(--red); }
.btn-deny:hover { background: rgba(211, 47, 47, .08); }

/* ---------- composer ---------- */
.composer {
  max-width: 760px;
  width: calc(100% - 40px);
  margin: 10px auto 16px;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  border: 1px solid var(--input-border);
  border-radius: 24px;
  background: var(--bg);
  padding: 8px 8px 8px 18px;
  box-shadow: var(--shadow);
}

.composer:focus-within { border-color: var(--text); }

.attach-btn {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 2px;
}

.attach-btn:hover { background: var(--sidebar-hover); color: var(--text); }

.mic-btn {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  margin-bottom: 2px;
}

.mic-btn:hover { background: var(--sidebar-hover); color: var(--text); }
.mic-btn.listening { color: var(--red); background: var(--amber-bg); animation: micpulse 1s infinite; }
@keyframes micpulse { 0%, 100% { opacity: 1; } 50% { opacity: .5; } }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: .85rem;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease;
  max-width: 90vw;
  text-align: center;
}

.toast.show { opacity: 1; }

.attach-list {
  max-width: 760px;
  width: calc(100% - 40px);
  margin: 0 auto;
  padding: 0 4px 6px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.attach-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--sidebar-hover);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 8px 5px 12px;
  font-size: .78rem;
  color: var(--text);
  max-width: 220px;
}

.attach-chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attach-chip button {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: .9rem;
  padding: 0 2px;
  line-height: 1;
}

.attach-chip button:hover { color: var(--red); }

.field .attach-list {
  width: 100%;
  max-width: none;
  margin: 0;
  padding: 0;
}

.mode-bar {
  max-width: 760px;
  width: calc(100% - 40px);
  margin: 0 auto;
  padding: 0 4px 8px;
  display: flex;
  gap: 6px;
}

.mode-btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  padding: 6px 18px;
  font-size: .82rem;
  font-weight: 600;
  cursor: pointer;
}

.mode-btn:hover { background: var(--sidebar-hover); color: var(--text); }
.mode-btn.active {
  background: var(--text);
  color: var(--bg);
  border-color: var(--text);
}

.composer textarea {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  resize: none;
  max-height: 160px;
  line-height: 1.45;
  padding: 8px 0;
}

.composer textarea::placeholder { color: var(--muted); }

.send-btn {
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  border: none;
  border-radius: 50%;
  background: var(--text);
  color: var(--bg);
  font-size: 1.05rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.send-btn:hover { opacity: .85; }
.send-btn:disabled { opacity: .35; cursor: default; }

/* ---------- empty state ---------- */
.empty-state {
  margin: auto;
  text-align: center;
  color: var(--muted);
  padding: 30px 24px;
  max-width: 460px;
}

.empty-logo {
  width: 52px;
  height: 52px;
  border-radius: 14px;
  font-size: 1.15rem;
  margin: 0 auto 18px;
}

.empty-state h1 { margin: 0 0 8px; font-size: 1.4rem; color: var(--text); }
.empty-state p { margin: 0 0 6px; }
.empty-hint { font-size: .82rem; margin-top: 14px !important; }

/* ---------- modal ---------- */
.modal {
  border: none;
  background: transparent;
  padding: 0;
}

.modal::backdrop { background: rgba(0, 0, 0, .5); }

.modal-card {
  width: min(92vw, 520px);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 24px;
  box-shadow: var(--shadow);
}

.modal-card h2 { margin: 0 0 18px; font-size: 1.2rem; }

.field { display: grid; gap: 6px; margin-bottom: 14px; font-size: .85rem; color: var(--muted); font-weight: 500; }

.field textarea, .field select {
  width: 100%;
  border: 1px solid var(--input-border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  padding: 12px 14px;
  outline: none;
}

.field textarea:focus, .field select:focus { border-color: var(--text); }

.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }

.btn-ghost {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  border-radius: 999px;
  padding: 10px 20px;
  font-size: .9rem;
  cursor: pointer;
}

.btn-ghost:hover { background: var(--sidebar-hover); }

.btn-solid {
  border: none;
  background: var(--text);
  color: var(--bg);
  border-radius: 999px;
  padding: 10px 22px;
  font-size: .9rem;
  font-weight: 600;
  cursor: pointer;
}

.btn-solid:hover { opacity: .85; }

.menu-row {
  width: 100%;
  margin-bottom: 8px;
  text-align: left;
  justify-content: flex-start;
}

.menu-row.danger {
  color: var(--red);
  border-color: rgba(211, 47, 47, .5);
}

/* ---------- mobile ---------- */
@media (max-width: 820px) {
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    transform: translateX(-105%);
    transition: transform .2s ease;
    box-shadow: 2px 0 20px rgba(0, 0, 0, .2);
  }

  body.sidebar-open .sidebar { transform: translateX(0); }
  .menu-btn { display: inline-flex; align-items: center; justify-content: center; }
  .main { padding-top: 8px; }
  .chat-head { padding-left: 64px; justify-content: flex-start; }
  .feed { padding: 44px 16px 8px; }
  .composer { width: calc(100% - 24px); }
  .approval-area { padding-left: 12px; padding-right: 12px; }
}
