:root {
  --bg: #141413;
  --bg-raised: #1c1b19;
  --bg-soft: #232220;
  --bg-input: #24231f;
  --line: #34312c;
  --line-soft: #2a2825;
  --text: #f0eee6;
  --text-dim: #b6b1a8;
  --muted: #8f8a81;
  --accent: #d97757;
  --accent-soft: #2e211b;
  --accent-ink: #1c120d;
  --green: #8fb573;
  --red: #e07a68;
  --radius: 18px;
  --tap: 44px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "Cascadia Code", Consolas, monospace;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.55;
  overscroll-behavior: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  cursor: pointer;
}

code {
  font-family: var(--mono);
  font-size: 0.86em;
}

.hidden {
  display: none !important;
}

/* --------------------------------------------------------------- top bar */

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 10px 8px;
  background: rgba(20, 20, 19, 0.88);
  backdrop-filter: saturate(140%) blur(14px);
  -webkit-backdrop-filter: saturate(140%) blur(14px);
  border-bottom: 1px solid var(--line-soft);
  z-index: 20;
  flex: 0 0 auto;
}

.icon-btn {
  width: var(--tap);
  height: var(--tap);
  display: grid;
  place-items: center;
  border-radius: 12px;
  color: var(--text-dim);
  transition: background 0.15s ease, color 0.15s ease;
  flex: 0 0 auto;
}

.icon-btn:active {
  background: var(--bg-soft);
  color: var(--text);
}

.icon-btn svg {
  width: 21px;
  height: 21px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.titles {
  flex: 1;
  min-width: 0;
  text-align: center;
}

.title {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.subtitle {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 11.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  flex: 0 0 auto;
  transition: background 0.2s ease, box-shadow 0.2s ease;
}

.dot.online {
  background: var(--green);
  box-shadow: 0 0 0 3px rgba(143, 181, 115, 0.12);
}

.dot.busy {
  background: var(--accent);
  box-shadow: 0 0 0 3px rgba(217, 119, 87, 0.16);
  animation: pulse 1.6s ease-in-out infinite;
}

.dot.offline {
  background: var(--red);
}

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

/* ---------------------------------------------------------------- thread */

.thread {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 18px 14px 8px;
  -webkit-overflow-scrolling: touch;
}

.empty {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 10px;
  padding: 30px 24px 60px;
  animation: rise 0.45s ease both;
}

.empty-mark {
  width: 66px;
  height: 66px;
  display: grid;
  place-items: center;
  border-radius: 22px;
  background: radial-gradient(circle at 30% 25%, #35261f, #1d1a17 70%);
  border: 1px solid #3b2f27;
  margin-bottom: 6px;
}

.empty-mark svg {
  width: 34px;
  height: 34px;
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.2;
  stroke-linecap: round;
}

.empty h1 {
  margin: 0;
  font-size: 21px;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.empty p {
  margin: 0 0 12px;
  max-width: 300px;
  color: var(--muted);
  font-size: 13.5px;
  line-height: 1.6;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

.msg {
  display: flex;
  flex-direction: column;
  margin-bottom: 16px;
  animation: rise 0.22s ease both;
}

.msg.user {
  align-items: flex-end;
}

.msg.user .bubble {
  max-width: 86%;
  background: var(--bg-soft);
  border: 1px solid var(--line-soft);
  border-radius: 18px 18px 6px 18px;
  padding: 10px 14px;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 15px;
}

.msg.assistant .body {
  width: 100%;
  font-size: 15px;
  color: var(--text);
  word-break: break-word;
  line-height: 1.68;
}

.msg.assistant .body.streaming {
  white-space: pre-wrap;
}

.msg.assistant .body > *:first-child {
  margin-top: 0;
}

.msg.assistant .body > *:last-child {
  margin-bottom: 0;
}

.msg.system {
  align-items: center;
}

.msg.system .bubble {
  max-width: 92%;
  font-size: 12.5px;
  color: var(--text-dim);
  background: var(--bg-raised);
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  padding: 8px 12px;
  text-align: left;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.system.error .bubble {
  color: #f0b3a6;
  background: #2a1c19;
  border-color: #4a2d27;
}

.body h1,
.body h2,
.body h3 {
  margin: 16px 0 8px;
  line-height: 1.3;
  font-weight: 600;
}

.body h1 { font-size: 19px; }
.body h2 { font-size: 17px; }
.body h3 { font-size: 15.5px; }

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

.body a {
  color: #e8a184;
  text-decoration: none;
  border-bottom: 1px solid rgba(232, 161, 132, 0.35);
}

.body ul,
.body ol {
  margin: 6px 0 12px;
  padding-left: 20px;
}

.body li {
  margin: 3px 0;
}

.body code:not(pre code) {
  background: var(--bg-soft);
  border: 1px solid var(--line-soft);
  border-radius: 5px;
  padding: 1px 5px;
  color: #e6c3ae;
}

.body strong {
  font-weight: 600;
}

.code {
  margin: 12px 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  background: #111110;
}

.code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px 6px 12px;
  background: var(--bg-raised);
  border-bottom: 1px solid var(--line-soft);
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.code-head .copy {
  font-size: 11.5px;
  color: var(--text-dim);
  padding: 4px 8px;
  border-radius: 6px;
  border: 1px solid var(--line);
}

.code-head .copy:active {
  background: var(--bg-soft);
}

.code pre {
  margin: 0;
  padding: 12px 13px;
  overflow-x: auto;
  font-size: 12.5px;
  line-height: 1.6;
}

.code pre code {
  font-size: inherit;
  color: #ddd8ce;
  white-space: pre;
}

/* ------------------------------------------------------------ tool cards */

.tool {
  margin: 8px 0 12px;
  border: 1px solid var(--line-soft);
  border-left: 2px solid #47413a;
  border-radius: 10px;
  background: var(--bg-raised);
  overflow: hidden;
  animation: rise 0.2s ease both;
}

.tool[open] {
  border-left-color: var(--accent);
}

.tool summary {
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  cursor: pointer;
  font-size: 12.5px;
  color: var(--text-dim);
  min-width: 0;
}

.tool summary::-webkit-details-marker {
  display: none;
}

.tool-name {
  font-family: var(--mono);
  font-size: 11.5px;
  font-weight: 600;
  color: #c9a48f;
  flex: 0 0 auto;
  letter-spacing: 0.02em;
}

.tool-summary {
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.tool-icon {
  flex: 0 0 auto;
  color: #6f675d;
  font-size: 12px;
}

.tool-body {
  padding: 0 12px 10px;
}

.tool-body pre {
  margin: 0;
  padding: 9px 10px;
  max-height: 220px;
  overflow: auto;
  background: #111110;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.55;
  color: #cfc9bf;
  white-space: pre-wrap;
  word-break: break-word;
}

.tool-result {
  margin-top: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  background: #191817;
  border: 1px solid var(--line-soft);
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-dim);
  max-height: 200px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.tool-result.error {
  color: #e9a89a;
  background: #241a18;
  border-color: #4a2d27;
}

.tool-pending {
  color: var(--muted);
  font-size: 11px;
  padding: 0 12px 9px;
}

/* --------------------------------------------------------------- typing */

.typing {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  padding: 6px 2px 14px;
}

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

.typing i:nth-child(2) { animation-delay: 0.16s; }
.typing i:nth-child(3) { animation-delay: 0.32s; }

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

/* ------------------------------------------------------------- approvals */

.permission {
  flex: 0 0 auto;
  margin: 0 12px 6px;
  padding: 10px 12px 12px;
  background: #221d19;
  border: 1px solid #4a362b;
  border-radius: 14px;
  z-index: 16;
}

.permission-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.permission-tag {
  font-size: 10.5px;
  letter-spacing: 0.06em;
  color: #f0b48a;
  background: #3a2a20;
  border-radius: 6px;
  padding: 2px 6px;
}

.permission-tool {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.permission-preview {
  margin: 0 0 10px;
  padding: 8px 10px;
  max-height: 148px;
  overflow: auto;
  font-size: 11.5px;
  line-height: 1.45;
  color: #cfc6bd;
  background: #191614;
  border-radius: 9px;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.permission-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.permission-actions .primary,
.permission-actions .ghost-btn {
  padding: 7px 18px;
  border-radius: 9px;
  font-size: 13px;
}

.permission-actions button:disabled {
  opacity: 0.5;
}

.toggle-field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.toggle {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
}

/* -------------------------------------------------------------- composer */

.composer {
  flex: 0 0 auto;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 72%, rgba(20, 20, 19, 0));
  z-index: 15;
}

.composer-box {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 7px 7px 7px 14px;
  background: var(--bg-input);
  border: 1px solid var(--line);
  border-radius: 24px;
  transition: border-color 0.15s ease;
}

.composer-box:focus-within {
  border-color: #4c4640;
}

#input {
  flex: 1;
  min-height: 30px;
  max-height: 132px;
  padding: 5px 0;
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
}

#input::placeholder {
  color: #7d786f;
}

.round {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  transition: transform 0.12s ease, opacity 0.12s ease, background 0.12s ease;
}

.round:active {
  transform: scale(0.92);
}

.round svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.round.send {
  background: var(--accent);
  color: var(--accent-ink);
}

.round.send:disabled {
  opacity: 0.4;
}

.round.stop {
  background: var(--bg-soft);
  color: var(--red);
  border: 1px solid var(--line);
}

.round.stop svg {
  fill: currentColor;
  stroke: none;
  width: 15px;
  height: 15px;
}

.meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px 0;
  font-size: 10.5px;
  color: #6f6a62;
  letter-spacing: 0.01em;
  gap: 10px;
}

#modelLabel {
  font-family: var(--mono);
  font-size: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#statusLabel {
  flex: 0 0 auto;
}

/* ---------------------------------------------------------------- drawer */

.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 40;
  animation: fade 0.2s ease both;
}

@keyframes fade {
  from { opacity: 0; }
  to { opacity: 1; }
}

.drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: min(84vw, 330px);
  background: #191817;
  border-right: 1px solid var(--line);
  z-index: 50;
  display: flex;
  flex-direction: column;
  transform: translateX(-102%);
  transition: transform 0.24s cubic-bezier(0.32, 0.72, 0, 1);
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

.drawer.open {
  transform: none;
}

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 10px 10px 18px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.03em;
  color: var(--text-dim);
}

.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
}

.session-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 10px;
  border-radius: 11px;
  cursor: pointer;
  border: 1px solid transparent;
  margin-bottom: 2px;
}

.session-row:active {
  background: var(--bg-soft);
}

.session-row.active {
  background: var(--accent-soft);
  border-color: #3f2d25;
}

.list-head {
  padding: 16px 10px 5px;
  font-size: 10.5px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.native-row .row-title {
  color: var(--text);
}

.row-main {
  flex: 1;
  min-width: 0;
}

.row-title {
  display: block;
  font-size: 13.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-preview {
  display: block;
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}

.row-side {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  flex: 0 0 auto;
}

.row-time {
  font-size: 10px;
  color: #6c675f;
}

.run-pip {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.4s infinite;
}

.row-del {
  width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  border-radius: 7px;
  color: #6d675e;
  font-size: 15px;
  line-height: 1;
}

.row-del:active {
  background: #2c2421;
  color: var(--red);
}

.drawer-foot {
  border-top: 1px solid var(--line-soft);
  padding: 10px 14px 14px;
}

.ghost-btn {
  width: 100%;
  text-align: left;
  padding: 9px 6px;
  font-size: 13px;
  color: var(--text-dim);
  border-radius: 8px;
}

.ghost-btn:active {
  background: var(--bg-soft);
}

.workspace {
  font-family: var(--mono);
  font-size: 9.5px;
  color: #615c54;
  padding: 6px 6px 0;
  word-break: break-all;
  line-height: 1.5;
}

/* ---------------------------------------------------------------- modals */

.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  animation: fade 0.18s ease both;
  padding: 0;
}

@media (min-width: 560px) {
  .modal {
    align-items: center;
    padding: 20px;
  }
}

.sheet {
  width: 100%;
  max-width: 520px;
  background: #1d1b19;
  border: 1px solid var(--line);
  border-radius: 22px 22px 0 0;
  padding: 22px 20px calc(24px + env(safe-area-inset-bottom));
  animation: sheet-in 0.26s cubic-bezier(0.32, 0.72, 0, 1) both;
}

@media (min-width: 560px) {
  .sheet {
    border-radius: 20px;
    padding-bottom: 24px;
  }
}

@keyframes sheet-in {
  from { transform: translateY(22px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

.sheet h2 {
  margin: 0 0 10px;
  font-size: 18px;
  font-weight: 600;
}

.sheet p {
  margin: 0 0 16px;
  font-size: 13px;
  line-height: 1.6;
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.sheet-head h2 {
  margin: 0;
}

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

.small {
  font-size: 11.5px !important;
}

.text-input,
.select {
  width: 100%;
  padding: 12px 13px;
  background: var(--bg-input);
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--text);
  font-size: 16px;
  outline: none;
  margin-bottom: 12px;
}

.text-input:focus,
.select:focus {
  border-color: #5a4a3f;
}

.select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, #8f8a81 50%),
    linear-gradient(135deg, #8f8a81 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  font-family: var(--mono);
  font-size: 13px;
}

.primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 12px 20px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  font-size: 14px;
  border-radius: 12px;
}

.primary:active {
  opacity: 0.85;
}

.primary.full {
  width: 100%;
}

.error-text {
  margin: 10px 0 0 !important;
  color: #e9a89a;
  font-size: 12px !important;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 4px;
}

.field > span {
  font-size: 12px;
  color: var(--muted);
}

.kv {
  display: flex;
  gap: 12px;
  align-items: baseline;
  padding: 7px 0;
  border-bottom: 1px solid var(--line-soft);
  font-size: 12px;
  color: var(--muted);
}

.kv > span {
  flex: 0 0 64px;
}

.kv code {
  flex: 1;
  color: var(--text-dim);
  word-break: break-all;
  font-size: 10.5px;
}

/* ----------------------------------------------------------------- toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(96px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(8px);
  background: #2a2723;
  border: 1px solid var(--line);
  color: var(--text);
  font-size: 12.5px;
  padding: 9px 15px;
  border-radius: 999px;
  z-index: 80;
  opacity: 0;
  transition: opacity 0.18s ease, transform 0.18s ease;
  max-width: 86vw;
  text-align: center;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* --------------------------------------------------------------- scrollbar */

.thread::-webkit-scrollbar,
.session-list::-webkit-scrollbar,
.code pre::-webkit-scrollbar,
.tool-body pre::-webkit-scrollbar,
.tool-result::-webkit-scrollbar {
  width: 5px;
  height: 5px;
}

.thread::-webkit-scrollbar-thumb,
.session-list::-webkit-scrollbar-thumb,
.code pre::-webkit-scrollbar-thumb,
.tool-body pre::-webkit-scrollbar-thumb,
.tool-result::-webkit-scrollbar-thumb {
  background: #3a3732;
  border-radius: 99px;
}
