:root {
  --brand: #ff6200;
  --logo: #fc630a;
  --bg: #181818;
  --card: #242424;
  --panel: #2c2c2c;
  --border: #393939;
  --text: #ffffffe6;
  --muted: #ffffff8c;
  --faint: #ffffff59;
  --ok: #07b87d;
  --warn: #fa9500;
  --err: #fc4128;
  --npc-bubble: #2a241f;
  --user-bubble: #2c2c2c;
  --radius: 10px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
button, input, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.screen { min-height: 100%; }
.muted { color: var(--muted); }
.err { color: var(--err); min-height: 1.2em; }

.btn { border: 1px solid var(--border); background: var(--panel); color: var(--text); border-radius: 8px; padding: 8px 12px; cursor: pointer; }
.btn:hover { border-color: #555; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.danger { background: #3a1510; border-color: var(--err); color: #ffd2c8; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { background: transparent; border: 0; color: var(--text); font-size: 18px; cursor: pointer; padding: 6px 8px; }
.mobile-only { display: none; }

.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { margin: 0; font-size: 22px; }
.brand.mini strong { font-size: 16px; }
.logo { background: var(--logo); color: #fff; border-radius: 6px; padding: 2px 8px; font-weight: 800; font-size: 13px; }

#login { display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-card { width: 420px; max-width: 100%; background: var(--card); border: 1px solid var(--border); border-radius: 16px; padding: 28px; }
.login-card .sub { color: var(--muted); }
.login-card label { display: block; margin: 12px 0 6px; color: var(--muted); }
.login-card input, #sess-search, .modal input, .modal select, textarea {
  width: 100%; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; outline: none;
}
.login-card input:focus, textarea:focus { border-color: var(--brand); }
.hint { color: var(--faint); font-size: 12px; }
.option-list { max-height: 180px; overflow: auto; margin: 8px 0 16px; }
.option { padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.option:hover, .option.active { background: var(--panel); }

#app { display: flex; height: 100%; }
#sidebar { width: 280px; flex: 0 0 280px; background: var(--card); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 12px; gap: 10px; }
.side-head { display: flex; justify-content: space-between; align-items: center; }
.side-actions { display: flex; flex-direction: column; gap: 8px; }
.sess-list { flex: 1; overflow: auto; }
.sess { padding: 10px; border-radius: 8px; cursor: pointer; border: 1px solid transparent; display: flex; gap: 8px; align-items: flex-start; }
.sess:hover { background: var(--panel); }
.sess.active { border-color: var(--brand); background: #2a1c14; }
.sess .meta { flex: 1; min-width: 0; }
.sess .t { font-weight: 600; }
.sess .m { color: var(--muted); font-size: 12px; }
.sess .del {
  flex: 0 0 auto;
  opacity: 0.35;
  background: transparent;
  border: 0;
  color: var(--muted);
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 6px;
  line-height: 1;
  font-size: 16px;
}
.sess:hover .del, .sess.active .del { opacity: 1; }
.sess .del:hover { color: var(--err); background: #3a1510; }
.quota { border-top: 1px solid var(--border); padding: 10px 4px 2px; font-size: 12px; flex: 0 0 auto; }
.quota-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.quota-label { color: var(--muted); }
.quota-num { font-variant-numeric: tabular-nums; font-weight: 600; }
.quota-bar { height: 6px; border-radius: 99px; background: var(--bg); border: 1px solid var(--border); overflow: hidden; margin: 6px 0 4px; }
.quota-fill { height: 100%; width: 0; background: var(--ok); border-radius: 99px; transition: width .3s; }
.quota-fill.warn { background: var(--warn); }
.quota-fill.full { background: var(--err); }
.quota-sub { color: var(--faint); font-size: 11px; display: flex; justify-content: space-between; gap: 8px; }
.quota-state { color: var(--muted); font-size: 11px; }
.pill { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 99px; background: var(--panel); color: var(--muted); margin-left: 6px; }
.pill.run { background: #3a2a10; color: var(--warn); }

main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 6px 10px; border-bottom: 1px solid var(--border); min-height: 44px; }
.top-title { flex: 1; min-width: 0; }
#chat-title { font-weight: 700; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.25; }
#chat-sub { font-size: 11px; line-height: 1.2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.usage-inline { font-size: 11px; color: var(--muted); white-space: nowrap; flex-shrink: 0; letter-spacing: 0; }
.btn.compact { padding: 4px 8px; font-size: 12px; }
.org-wrap { position: relative; flex-shrink: 0; }
.menu { position: absolute; right: 0; top: 110%; background: var(--card); border: 1px solid var(--border); border-radius: 8px; min-width: 220px; max-height: 280px; overflow: auto; z-index: 20; }
.menu div { padding: 8px 10px; cursor: pointer; }
.menu div:hover { background: var(--panel); }
.menu .menu-item.active { color: var(--brand); font-weight: 600; }
.menu .menu-item.active:hover { background: var(--panel); }

.empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; color: var(--muted); }
#chat { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.timeline { flex: 1; overflow: auto; padding: 12px; display: flex; flex-direction: column; gap: 12px; }
.msg-row { display: flex; gap: 8px; width: 100%; align-items: flex-start; }
.msg-row.me { flex-direction: row-reverse; }
.avatar { width: 36px; height: 36px; border-radius: 50%; flex: 0 0 36px; object-fit: cover; background: var(--panel); }
.avatar.fallback { display: flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 700; color: #fff; }
.avatar.fallback.npc { background: var(--brand); }
.avatar.fallback.me { background: #4a5560; }
.bubble {
  min-width: calc((100% - 44px) * 0.9);
  width: calc(100% - 44px);
  max-width: calc(100% - 44px);
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--user-bubble);
  border: 1px solid var(--border);
}
.msg-row.npc .bubble { background: var(--npc-bubble); border-color: #4a3828; }
.who { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.bubble .body { overflow-wrap: anywhere; word-break: break-word; }
.bubble .body img {
  display: block;
  width: 100%;
  max-width: 400px;
  height: auto;
  border-radius: 8px;
  cursor: zoom-in;
}

.card { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 12px; width: 100%; }
.steps-row { display: flex; gap: 8px; width: 100%; align-items: flex-start; }
.steps-row > .avatar { margin-top: 2px; }
.task-panel { flex: 1; min-width: 0; background: var(--bg); border-radius: 10px; padding: 6px 4px 8px 2px; }
.task-head { font-size: 12px; color: var(--muted); padding: 2px 8px 6px; }
.task-list { display: flex; flex-direction: column; gap: 2px; }
.task { display: flex; align-items: flex-start; gap: 8px; padding: 3px 8px; color: var(--text); line-height: 1.45; }
.task-ico { width: 16px; height: 16px; flex: 0 0 16px; margin-top: 2px; display: block; }
.task-ico svg { display: block; width: 16px; height: 16px; }
.task.completed { color: var(--text); }
.task.completed .task-ico { color: var(--ok); }
.task.in_progress { color: var(--text); }
.task.in_progress .task-ico { color: var(--warn); }
.task.pending { color: var(--muted); }
.task.pending .task-ico { color: var(--faint); }
.task-name { min-width: 0; overflow-wrap: anywhere; }
.task-stopped { display: flex; align-items: flex-start; gap: 8px; padding: 3px 8px; color: var(--muted); line-height: 1.45; }
.task-ico.stopped { color: var(--err); }
.pr-actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.btn.busy { opacity: 0.85; }

.queue { background: #2a2410; color: var(--warn); padding: 6px 12px; font-size: 12px; }
.queue-head { margin-bottom: 4px; }
.queue-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  color: var(--text);
}
.queue-text {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.queue-cancel { padding: 2px 8px; font-size: 12px; flex: 0 0 auto; }
#btn-stop {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 4.5em;
}
#btn-stop .spin {
  width: 12px;
  height: 12px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex: 0 0 auto;
}
@keyframes spin { to { transform: rotate(360deg); } }
.write-token-row { display: flex; gap: 8px; margin-top: 8px; align-items: center; }
.write-token-row input { flex: 1; min-width: 0; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; }
#write-token-bar .err { margin: 6px 0 0; }
.composer { border-top: 1px solid var(--border); padding: 10px 12px 14px; background: var(--card); }
.composer textarea { display: block; width: 100%; resize: vertical; min-height: 72px; }
.composer-row {
  display: flex;
  align-items: stretch;
  gap: 6px;
  margin-top: 8px;
  flex-wrap: nowrap;
}
.composer-row .file-btn {
  flex: 0 0 auto;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  padding: 8px 10px;
}
.npc-select {
  flex: 1 1 auto;
  min-width: 0;
  max-width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  color: inherit;
  font: inherit;
}
.composer-row .btn { flex: 0 0 auto; white-space: nowrap; padding: 8px 12px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  max-width: 100%;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 2px 4px 2px 8px;
  font-size: 12px;
}
.chip-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  object-fit: cover;
  flex: 0 0 auto;
}
.chip-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 180px;
}
.chip-x {
  flex: 0 0 auto;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 2px 6px;
  border-radius: 99px;
}
.chip-x:hover { color: var(--text); background: var(--border); }

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: #000c;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  cursor: zoom-out;
}
.lightbox.hidden { display: none !important; }
.lightbox img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  cursor: default;
  border-radius: 4px;
}
.lightbox-x {
  position: absolute;
  top: 10px;
  right: 12px;
  z-index: 51;
  border: 0;
  background: #0008;
  color: #fff;
  font-size: 28px;
  line-height: 1;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
}
.lightbox-x:hover { background: #000c; }

.modal { position: fixed; inset: 0; background: #0008; display: flex; align-items: flex-end; justify-content: center; z-index: 30; }
.modal-card { background: var(--card); border: 1px solid var(--border); border-radius: 16px 16px 0 0; width: min(560px, 100%); max-height: 80vh; overflow: auto; padding: 16px; }
.modal-card h3 { margin: 0 0 12px; }
.sheet-item { padding: 10px; border-radius: 8px; cursor: pointer; display: flex; justify-content: space-between; gap: 8px; }
.sheet-item:hover, .sheet-item.active { background: var(--panel); }
.issue-row { padding: 10px; border-bottom: 1px solid var(--border); cursor: pointer; }
.issue-row.disabled { opacity: .5; }

/* markdown preview inside chat bubbles */
.bubble .body > :first-child { margin-top: 0; }
.bubble .body > :last-child { margin-bottom: 0; }
.bubble .body p { margin: 0 0 8px; white-space: break-spaces; }
.bubble .body h1, .bubble .body h2, .bubble .body h3,
.bubble .body h4, .bubble .body h5, .bubble .body h6 {
  margin: 14px 0 8px; line-height: 1.3; font-weight: 700;
}
.bubble .body h1 { font-size: 18px; }
.bubble .body h2 { font-size: 17px; }
.bubble .body h3 { font-size: 15px; }
.bubble .body h4, .bubble .body h5, .bubble .body h6 { font-size: 14px; color: var(--muted); }
.bubble .body ul, .bubble .body ol { margin: 0 0 8px; padding-left: 22px; }
.bubble .body li { margin: 2px 0; }
.bubble .body li > p { margin: 0; }
.bubble .body blockquote {
  margin: 0 0 8px; padding: 2px 10px; color: var(--muted);
  border-left: 3px solid var(--border);
}
.bubble .body code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px; background: #0006; border-radius: 5px; padding: 1px 5px;
}
.bubble .body pre {
  margin: 0 0 8px; padding: 10px 12px; overflow: auto; border-radius: 8px;
  background: #0006; border: 1px solid var(--border);
}
.bubble .body pre code { background: none; padding: 0; font-size: 12.5px; line-height: 1.5; }
.bubble .body a { color: var(--brand); text-decoration: none; }
.bubble .body a:hover { text-decoration: underline; }
.bubble .body hr { border: 0; border-top: 1px solid var(--border); margin: 12px 0; }
.bubble .body table { border-collapse: collapse; margin: 0 0 8px; display: block; overflow: auto; max-width: 100%; }
.bubble .body th, .bubble .body td { border: 1px solid var(--border); padding: 5px 9px; text-align: left; }
.bubble .body th { background: #0004; font-weight: 600; }
.bubble .body del { color: var(--faint); }

@media (max-width: 768px) {
  .mobile-only { display: inline-flex; }
  #sidebar { position: fixed; inset: 0 auto 0 0; transform: translateX(-110%); transition: .2s; z-index: 25; width: 86vw; height: 100%; }
  #sidebar.open { transform: none; }
  .composer-row .btn, .composer-row .file-btn { padding: 8px 8px; }
  .npc-select { padding: 8px; }
}
