:root {
  --bg: #0a0c10;
  --bg-2: #0e1218;
  --panel: #0c1016;
  --elev: #121822;
  --line: #1e2632;
  --line-soft: #161d27;
  --text: #e7edf5;
  --muted: #8b98ab;
  --muted-2: #647084;
  --accent: #5b8cff;
  --accent-2: #7aa2ff;
  --user: #1b2740;
  --user-line: #2a3c5e;
  --ok: #35c07d;
  --warn: #e0a23b;
  --danger: #f0616d;
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 14.5px/1.6 var(--font);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

.hidden {
  display: none !important;
}

.muted {
  color: var(--muted);
}

.error {
  color: var(--danger);
  min-height: 18px;
  margin: 2px 0 0;
  font-size: 13px;
}

.logo {
  color: var(--accent);
  font-size: 18px;
}

/* ---------- login ---------- */
.login {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: radial-gradient(1200px 600px at 50% -10%, #16233b 0%, var(--bg) 55%);
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--elev);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 26px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}

.login-brand {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.3px;
}

.login-card p {
  margin: 0;
}

.login-card input {
  width: 100%;
  padding: 11px 13px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg-2);
  color: var(--text);
  font-size: 15px;
}

.login-card input:focus {
  outline: none;
  border-color: var(--accent);
}

/* ---------- layout ---------- */
.app {
  display: grid;
  grid-template-columns: 288px 1fr;
  height: 100dvh;
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 16px 14px 12px;
}

.brand {
  font-weight: 700;
  letter-spacing: 0.3px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
}

.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.session-item {
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  border-radius: 10px;
  padding: 9px 11px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.session-item:hover {
  background: var(--bg-2);
}

.session-item.active {
  background: var(--elev);
  border-color: var(--line);
}

.session-item .s-title {
  font-size: 13.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-item .s-meta {
  font-size: 11px;
  color: var(--muted-2);
}

.session-empty {
  color: var(--muted-2);
  font-size: 13px;
  padding: 10px 12px;
}

.sidebar-foot {
  border-top: 1px solid var(--line);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

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

.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--warn);
  box-shadow: 0 0 8px rgba(224, 162, 59, 0.6);
}

.conn.online .conn-dot {
  background: var(--ok);
  box-shadow: 0 0 8px rgba(53, 192, 125, 0.6);
}

.workdir {
  font-size: 11px;
  color: var(--muted-2);
  font-family: var(--mono);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- main ---------- */
.main {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: linear-gradient(180deg, #0b0e13 0%, var(--bg) 30%);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line-soft);
  backdrop-filter: blur(8px);
}

.title-wrap {
  min-width: 0;
  flex: 1;
}

.chat-title {
  font-weight: 650;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-meta {
  font-size: 11.5px;
  color: var(--muted-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.model-chip {
  font-size: 11.5px;
  color: var(--muted);
  border: 1px solid var(--line);
  background: var(--bg-2);
  padding: 5px 9px;
  border-radius: 999px;
  white-space: nowrap;
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  display: none;
  background: transparent;
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 9px;
  width: 36px;
  height: 36px;
  font-size: 16px;
  cursor: pointer;
}

/* ---------- messages ---------- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 26px 18px 12px;
  scroll-behavior: smooth;
}

.messages-inner,
.messages > .msg {
  max-width: 840px;
  margin-left: auto;
  margin-right: auto;
}

.msg {
  display: flex;
  gap: 12px;
  margin: 0 auto 20px;
  max-width: 840px;
}

.msg.user {
  flex-direction: row-reverse;
}

.avatar {
  flex: 0 0 30px;
  width: 30px;
  height: 30px;
  border-radius: 9px;
  display: grid;
  place-items: center;
  font-size: 15px;
  background: var(--elev);
  border: 1px solid var(--line);
  color: var(--accent);
  margin-top: 2px;
}

.msg.user .avatar {
  background: var(--user);
  border-color: var(--user-line);
  color: var(--accent-2);
}

.bubble {
  min-width: 0;
  max-width: calc(100% - 46px);
  border-radius: var(--radius);
  padding: 11px 15px;
  background: var(--bg-2);
  border: 1px solid var(--line-soft);
}

.msg.assistant .bubble {
  background: transparent;
  border: none;
  padding: 2px 2px;
  max-width: 100%;
}

.msg.user .bubble {
  background: var(--user);
  border: 1px solid var(--user-line);
}

.msg.user .bubble .md {
  white-space: pre-wrap;
  word-break: break-word;
}

.msg .who {
  font-size: 11px;
  color: var(--muted-2);
  margin-bottom: 4px;
}

/* ---------- markdown ---------- */
.md > *:first-child {
  margin-top: 0;
}

.md > *:last-child {
  margin-bottom: 0;
}

.md p {
  margin: 0 0 10px;
}

.md h1,
.md h2,
.md h3,
.md h4 {
  margin: 18px 0 8px;
  line-height: 1.3;
}

.md h1 {
  font-size: 20px;
}
.md h2 {
  font-size: 17px;
}
.md h3 {
  font-size: 15px;
}

.md ul,
.md ol {
  margin: 6px 0 10px;
  padding-left: 22px;
}

.md li {
  margin: 3px 0;
}

.md a {
  color: var(--accent-2);
  text-decoration: none;
}

.md a:hover {
  text-decoration: underline;
}

.md code {
  font-family: var(--mono);
  font-size: 12.5px;
  background: #10161f;
  border: 1px solid var(--line-soft);
  padding: 1px 5px;
  border-radius: 6px;
}

.md pre {
  margin: 10px 0;
  background: #0a0e14;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  overflow-x: auto;
}

.md pre code {
  background: transparent;
  border: none;
  padding: 0;
  font-size: 12.5px;
  line-height: 1.55;
  white-space: pre;
}

.md blockquote {
  margin: 10px 0;
  padding: 2px 0 2px 12px;
  border-left: 3px solid var(--line);
  color: var(--muted);
}

.md table {
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 13px;
  width: 100%;
  display: block;
  overflow-x: auto;
}

.md th,
.md td {
  border: 1px solid var(--line);
  padding: 6px 10px;
  text-align: left;
}

.md th {
  background: var(--bg-2);
}

.md hr {
  border: none;
  border-top: 1px solid var(--line);
  margin: 16px 0;
}

/* ---------- reasoning + tools ---------- */
.reasoning {
  margin: 6px 0 10px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.015);
}

.reasoning summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 12.5px;
  user-select: none;
}

.reasoning .r-body {
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
  white-space: pre-wrap;
}

.tool {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 4px 6px 4px 0;
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-2);
  font-size: 12px;
  color: var(--muted);
  max-width: 100%;
}

.tool .t-name {
  color: var(--text);
  font-weight: 600;
  font-family: var(--mono);
  font-size: 11.5px;
}

.tool .t-status {
  color: var(--muted-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 320px;
}

.tool.running .t-dot {
  background: var(--warn);
}
.tool.completed .t-dot {
  background: var(--ok);
}
.tool.error .t-dot {
  background: var(--danger);
}

.t-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted-2);
  flex: 0 0 auto;
}

.tool.running .t-dot {
  animation: pulse 1s infinite ease-in-out;
}

@keyframes pulse {
  0%,
  100% {
    opacity: 0.35;
  }
  50% {
    opacity: 1;
  }
}

.err-box {
  border: 1px solid rgba(240, 97, 109, 0.4);
  background: rgba(240, 97, 109, 0.08);
  color: #ffb4ba;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
}

/* ---------- typing ---------- */
.typing {
  display: inline-flex;
  gap: 4px;
  padding: 10px 4px;
  align-items: center;
}

.typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: blink 1.2s infinite ease-in-out;
}

.typing span:nth-child(2) {
  animation-delay: 0.15s;
}
.typing span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes blink {
  0%,
  100% {
    opacity: 0.25;
    transform: translateY(0);
  }
  50% {
    opacity: 1;
    transform: translateY(-2px);
  }
}

/* ---------- permission ---------- */
.perm {
  max-width: 840px;
  margin: 0 auto;
  width: calc(100% - 36px);
  border: 1px solid rgba(224, 162, 59, 0.35);
  background: rgba(224, 162, 59, 0.07);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.perm-label {
  font-size: 11.5px;
  color: var(--warn);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.perm-text {
  font-size: 13px;
  margin-top: 2px;
  word-break: break-word;
}

.perm-actions {
  display: flex;
  gap: 8px;
  flex: 0 0 auto;
}

/* ---------- question ---------- */
.question-panel {
  max-width: 840px;
  margin: 0 auto;
  width: calc(100% - 36px);
  border: 1px solid rgba(91, 140, 255, 0.35);
  background: rgba(91, 140, 255, 0.08);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 46vh;
  overflow-y: auto;
}

.q-item + .q-item {
  border-top: 1px solid var(--line-soft);
  padding-top: 10px;
}

.q-head {
  font-size: 11.5px;
  color: var(--accent-2);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.q-text {
  font-size: 13.5px;
  margin: 2px 0 8px;
  word-break: break-word;
}

.q-options {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.q-opt {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--bg-2);
  cursor: pointer;
  line-height: 1.4;
}

.q-opt:hover {
  border-color: #33445e;
}

.q-opt:has(input:checked) {
  border-color: var(--accent);
  background: rgba(91, 140, 255, 0.12);
}

.q-opt input {
  margin: 2px 0 0;
  accent-color: var(--accent);
  flex: 0 0 auto;
}

.q-opt > span {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.q-opt-label {
  font-size: 13px;
}

.q-opt-desc {
  font-size: 11.5px;
  color: var(--muted);
}

.q-custom {
  margin-top: 8px;
  width: 100%;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 9px;
  color: var(--text);
  font: inherit;
  font-size: 13px;
  padding: 8px 10px;
  outline: none;
}

.q-custom:focus {
  border-color: var(--accent);
}

.q-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  flex: 0 0 auto;
}

/* ---------- composer ---------- */
.composer {
  padding: 10px 18px calc(16px + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, rgba(10, 12, 16, 0) 0%, var(--bg) 40%);
}

.composer-box {
  max-width: 840px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--elev);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 10px 10px 10px 16px;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.35);
}

.composer-box:focus-within {
  border-color: #33445e;
}

.composer textarea {
  flex: 1;
  resize: none;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  line-height: 1.5;
  max-height: 220px;
  padding: 4px 0;
}

.composer-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.send-btn {
  width: 36px;
  height: 36px;
  border-radius: 11px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ---------- buttons ---------- */
.btn {
  border: 1px solid var(--line);
  background: var(--bg-2);
  color: var(--text);
  border-radius: 10px;
  padding: 9px 12px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover {
  filter: brightness(1.12);
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

.btn.ghost {
  background: transparent;
  padding: 7px 10px;
  font-size: 12.5px;
}

.btn.tiny {
  padding: 6px 10px;
  font-size: 12px;
  border-radius: 8px;
}

.btn.danger {
  color: var(--danger);
  border-color: rgba(240, 97, 109, 0.4);
  background: transparent;
}

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  background: #2a1216;
  border: 1px solid rgba(240, 97, 109, 0.5);
  color: #ffb4ba;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 50;
  max-width: 90vw;
}

/* ---------- scrim ---------- */
.scrim {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 20;
}

/* ---------- scrollbars ---------- */
* {
  scrollbar-width: thin;
  scrollbar-color: #2a3442 transparent;
}

*::-webkit-scrollbar {
  width: 9px;
  height: 9px;
}

*::-webkit-scrollbar-thumb {
  background: #2a3442;
  border-radius: 999px;
}

*::-webkit-scrollbar-track {
  background: transparent;
}

/* ---------- responsive ---------- */
@media (max-width: 860px) {
  .app {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    z-index: 30;
    inset: 0 auto 0 0;
    width: 280px;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }

  .app.drawer-open .sidebar {
    transform: translateX(0);
  }

  .app.drawer-open .scrim {
    display: block;
  }

  .icon-btn {
    display: grid;
    place-items: center;
  }

  .messages {
    padding: 18px 12px 8px;
  }

  .msg {
    gap: 10px;
  }

  .avatar {
    flex-basis: 26px;
    width: 26px;
    height: 26px;
    border-radius: 8px;
    font-size: 13px;
  }

  .perm {
    width: calc(100% - 24px);
    flex-direction: column;
    align-items: stretch;
  }

  .question-panel {
    width: calc(100% - 24px);
  }

  .perm-actions {
    justify-content: flex-end;
  }

  .composer {
    padding: 8px 12px calc(12px + env(safe-area-inset-bottom));
  }

  .model-chip {
    display: none;
  }
}
