:root {
  color-scheme: light;
  --bg: #f4f5f7;
  --text: #1c1e21;
  --panel: #fff;
  --line: #ddd;
  --accent: #1f4b99;
  --on-accent: #fff;
  --selected: #d6e4ff;
  --user: #e7eefc;
  --muted: #5c6570;
  --faint: #6b7280;
  --danger: #9b1c1c;
  --danger-bg: #fdecea;
  --chip: #e7e9ee;
  --input: #fff;
  --input-border: #ccc;
  --code-bg: #eceff3;
  --pre-bg: #1e1e1e;
  --pre-text: #f3f3f3;
  --table-line: #d5d8de;
  --hover: #eceff3;
  font-family: "Segoe UI", sans-serif;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #14161a;
  --text: #e8eaed;
  --panel: #1c1f26;
  --line: #2e333d;
  --accent: #8eb4ff;
  --on-accent: #0e1524;
  --selected: #243656;
  --user: #243044;
  --muted: #aeb6c2;
  --faint: #9aa3af;
  --danger: #ffb4b4;
  --danger-bg: #3a2224;
  --chip: #2a303a;
  --input: #12141a;
  --input-border: #3a4150;
  --code-bg: #2a303a;
  --pre-bg: #0e1014;
  --pre-text: #f3f3f3;
  --table-line: #3a4150;
  --hover: #2a303a;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

button {
  background: var(--accent);
  color: var(--on-accent);
  border: 0;
  border-radius: 6px;
  padding: 8px 12px;
  cursor: pointer;
}

button:disabled {
  opacity: 0.6;
  cursor: default;
}

#theme,
#admin,
#logout {
  background: var(--chip);
  color: var(--text);
  margin-left: 8px;
}

#admin,
#logout,
#banner {
  display: none;
}

#login {
  max-width: 360px;
  margin: 12vh auto;
  background: var(--panel);
  padding: 24px;
  border-radius: 10px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .18);
}

#login label,
#login input {
  display: block;
  width: 100%;
}

#login input,
textarea {
  background: var(--input);
  border: 1px solid var(--input-border);
  border-radius: 6px;
  padding: 8px;
}

#login input {
  margin: 6px 0 14px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: inherit;
  text-decoration: none;
  font-weight: 650;
}

.logo {
  width: 32px;
  height: 32px;
  flex: none;
}

#banner {
  padding: 8px 20px;
  background: var(--selected);
}

#banner button {
  margin-left: 12px;
}

#admin-panel {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .45);
  z-index: 5;
}

#admin-panel.open {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 48px 16px;
  overflow: auto;
}

.admin-card {
  width: min(720px, 100%);
  background: var(--panel);
  color: var(--text);
  border-radius: 12px;
  padding: 16px;
}

.admin-head,
#user-form,
.user-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.admin-head {
  justify-content: space-between;
}

#user-form {
  margin: 12px 0;
  flex-wrap: wrap;
}

#user-form input,
#user-form select {
  background: var(--input);
  border: 1px solid var(--input-border);
  border-radius: 6px;
  padding: 8px;
}

.user-row {
  justify-content: space-between;
  padding: 8px 0;
  border-top: 1px solid var(--line);
}

.revenue-head {
  margin: 20px 0 4px;
  font-size: 18px;
}

.hint {
  margin: 0 0 8px;
  color: var(--faint);
  font-size: 13px;
}

.revenue-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  border-top: 1px solid var(--line);
}

#app.viewing #composer,
#app.viewing #pending,
#app.viewing #new-chat,
#app.viewing .icon,
#app.viewing .fork {
  display: none;
}

#app {
  display: none;
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
  align-items: stretch;
  grid-template-columns: 260px minmax(0, 1fr) 260px;
  grid-template-rows: minmax(0, 1fr);
}

#chats,
#files {
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
}

#chats,
#files {
  background: var(--panel);
  overflow: auto;
  padding: 12px;
}

#chats {
  border-right: 1px solid var(--line);
}

#files {
  border-left: 1px solid var(--line);
}

#new-chat {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  margin-bottom: 8px;
  padding: 8px;
  background: transparent;
  color: var(--text);
  text-align: left;
}

#new-chat svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#new-chat:hover {
  background: var(--hover);
}

#chat-search {
  width: 100%;
  margin-bottom: 8px;
  padding: 8px;
  background: transparent;
  color: var(--text);
  border: 0;
  border-radius: 6px;
}

#chat-search:focus {
  outline: 1px solid var(--line);
  background: var(--bg);
}

.chat-group {
  margin: 14px 8px 4px;
  color: var(--faint);
  font-size: 12px;
}

.chat-more,
.chat-archived {
  display: block;
  width: 100%;
  margin: 2px 0 8px;
  padding: 6px 8px;
  background: transparent;
  color: var(--faint);
  text-align: left;
}

.chat-more:hover,
.chat-archived:hover {
  background: var(--hover);
  color: var(--text);
}

#chat-list .chat {
  display: block;
  width: auto;
  margin: 0;
  text-align: left;
  background: transparent;
  color: inherit;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  border-radius: 8px;
  padding: 3px 6px;
  position: relative;
}

#chat-list .chat-row.active {
  background: var(--selected);
  box-shadow: inset 3px 0 0 var(--accent);
}

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

.chat-row.active .chat {
  font-weight: 650;
}

.chat-when {
  flex: none;
  color: var(--faint);
  font-size: 12px;
}

.chat-row .actions {
  display: none;
  position: absolute;
  right: 2px;
  top: 1px;
  bottom: 1px;
  align-items: center;
  background: var(--panel);
}

.chat-row:hover .actions,
.chat-row:focus-within .actions {
  display: flex;
}

.chat-row.active .actions {
  background: var(--selected);
}

.chat-row:hover .actions {
  background: var(--hover);
}

.chat-row.active:hover .actions {
  background: var(--selected);
}

.chat-row .icon {
  flex: none;
  width: 26px;
  height: 26px;
  padding: 4px;
  background: transparent;
  color: var(--muted);
}

.chat-row .icon svg {
  display: block;
  width: 16px;
  height: 16px;
  margin: 0 auto;
  fill: currentColor;
}

.chat-row .icon:hover,
.copy:hover {
  background: var(--hover);
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  flex: none;
}

.dot.idle {
  background: transparent;
  box-shadow: inset 0 0 0 1.5px var(--faint);
}

.msg-tools,
.block-tools {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  margin-bottom: 6px;
}

.copy,
.fork {
  background: var(--chip);
  color: var(--text);
  padding: 2px 8px;
  font-size: 12px;
}

.fork {
  margin-top: 8px;
}

.visuals {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 10px;
}

.visuals figure {
  margin: 0;
}

.visual-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
}

.visuals figcaption {
  color: var(--faint);
  font-size: 12px;
}

.expand,
.visual-close {
  background: var(--chip);
  color: var(--text);
  padding: 2px 8px;
  font-size: 12px;
}

#share-modal {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.45);
}

.share-card {
  width: min(420px, 100%);
  background: var(--panel);
  color: var(--text);
  border-radius: 12px;
  padding: 16px;
}

.share-card h2 {
  margin: 0 0 8px;
  font-size: 18px;
}

.share-card select {
  width: 100%;
  margin: 8px 0 12px;
  padding: 8px;
  background: var(--input);
  color: var(--text);
  border: 1px solid var(--input-border);
  border-radius: 6px;
}

.share-actions {
  display: flex;
  gap: 8px;
}

#visual-modal {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  padding: 16px;
  background: rgba(0, 0, 0, 0.72);
}

.visual-modal-panel {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 12px;
  background: var(--panel);
  border-radius: 12px;
}

.visual-close {
  align-self: flex-end;
  margin-bottom: 8px;
}

.visual-modal-panel img,
.visual-modal-panel svg,
.visual-modal-panel iframe {
  flex: 1;
  width: 100%;
  height: 100%;
  min-height: 0;
  border: 0;
  border-radius: 8px;
  background: #fff;
}

.visuals img,
.visuals svg {
  display: block;
  width: 100%;
  height: auto;
  background: #fff;
  border-radius: 8px;
}

.visuals iframe {
  width: 100%;
  height: 420px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.msg .time {
  display: block;
  color: var(--faint);
  font-size: 12px;
  margin-top: 6px;
}

#composer-status,
#export-notice {
  min-height: 0;
  margin: 0;
  padding: 0 20px;
}

#composer-status:empty,
#export-notice:empty {
  display: none;
}

#export-notice a,
.msg.assistant a,
#files a {
  color: var(--accent);
}

#files a {
  display: block;
  margin: 8px 0;
  word-break: break-all;
}

#thread {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  overflow: hidden;
}

#log {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  padding: 20px;
}

.msg {
  background: var(--panel);
  border-radius: 8px;
  padding: 12px 14px;
  margin: 0 0 12px;
  line-height: 1.45;
}

.msg.user {
  background: var(--user);
  white-space: pre-wrap;
}

.trace {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 13px;
}

.trace summary {
  cursor: pointer;
}

.thought {
  white-space: pre-wrap;
  max-height: 180px;
  overflow: auto;
  margin: 8px 0;
}

.steps {
  list-style: none;
  padding: 0;
  margin: 8px 0 0;
}

.steps li {
  margin: 2px 0;
}

.steps li.running::before {
  content: "• ";
  color: var(--accent);
}

.steps li.done::before {
  content: "✓ ";
}

.steps li.error::before {
  content: "× ";
  color: var(--danger);
}

.msg.status {
  color: var(--muted);
  background: transparent;
  padding: 0 0 8px;
  font-size: 13px;
}

.msg.error {
  background: var(--danger-bg);
  color: var(--danger);
  white-space: pre-wrap;
}

.msg.assistant h1,
.msg.assistant h2,
.msg.assistant h3 {
  margin: 0.4em 0;
  font-size: 1.05rem;
}

.msg.assistant p {
  margin: 0.4em 0;
}

.msg.assistant pre {
  background: var(--pre-bg);
  color: var(--pre-text);
  padding: 12px;
  border-radius: 8px;
  overflow: auto;
}

.msg.assistant code {
  font-family: Consolas, monospace;
  font-size: 13px;
}

.msg.assistant p code,
.msg.assistant li code {
  background: var(--code-bg);
  padding: 1px 4px;
  border-radius: 4px;
}

.msg.assistant table {
  border-collapse: collapse;
  margin: 8px 0;
}

.msg.assistant th,
.msg.assistant td {
  border: 1px solid var(--table-line);
  padding: 4px 8px;
  text-align: left;
}

#question-card {
  display: none;
  margin: 0 20px 8px;
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
}

#question-card.open {
  display: block;
}

#question-card h2 {
  margin: 0 0 8px;
  font-size: 16px;
}

.question-field {
  margin: 0 0 10px;
}

.question-field p {
  margin: 0 0 6px;
}

.question-option {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 4px 0;
}

#question-card textarea,
#question-card input[type="text"] {
  width: 100%;
  min-height: 0;
}

#composer {
  margin-top: auto;
  padding: 8px 20px 12px;
}

#composer-box {
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--input);
  padding: 8px 10px 6px;
}

#composer textarea {
  display: block;
  width: 100%;
  min-height: 48px;
  max-height: 200px;
  margin: 0;
  padding: 6px 6px 2px;
  border: 0;
  resize: none;
  background: transparent;
  box-shadow: none;
}

#composer textarea:focus {
  outline: none;
}

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

.composer-model {
  color: var(--muted);
  font-size: 13px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--chip);
}

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

#pending {
  display: none;
  flex-wrap: wrap;
  gap: 8px;
  padding: 2px 4px 6px;
}

#pending.shown {
  display: flex;
}

#pending .chip {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--chip);
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 13px;
}

#pending img,
.attachments img {
  max-height: 160px;
  max-width: 240px;
  border-radius: 6px;
  display: block;
}

.attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

#attach,
#send {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
}

#attach:hover,
#ticket:hover,
#cancel:hover {
  background: var(--hover);
}

#send {
  border-radius: 999px;
  background: var(--accent);
  color: var(--on-accent);
}

#send:disabled {
  opacity: 0.45;
}

#attach svg,
#send svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

textarea {
  flex: 1;
  min-height: 72px;
  resize: vertical;
}

#ticket,
#cancel {
  background: transparent;
  color: var(--muted);
  padding: 4px 8px;
  font-size: 13px;
}

#cancel {
  display: none;
  color: var(--danger);
}

#ticket-modal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 30;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.45);
}

#ticket-modal.open {
  display: flex;
}

#ticket-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(960px, 94vw);
  max-height: calc(100vh - 48px);
  overflow: auto;
  margin: 0;
  padding: 16px;
  background: var(--panel);
  color: var(--text);
  border-radius: 12px;
}

#ticket-form input,
#ticket-form select,
#ticket-form textarea {
  width: 100%;
}

#ticket-form textarea {
  min-height: 46vh;
}

.error-text {
  color: var(--danger);
  min-height: 0;
  padding: 0;
}

@media (max-width: 800px) {
  #app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(120px, 28vh) minmax(0, 1fr) minmax(80px, 22vh);
    height: auto;
    min-height: 0;
  }

  #files,
  #chats {
    border: 0;
    border-top: 1px solid var(--line);
    overflow: auto;
  }

  .chat-row .actions {
    display: flex;
    position: static;
    background: transparent;
  }

  #thread {
    min-height: 0;
  }
}