:root {
  --bg: #ffffff;
  --surface: #f4f5f7;
  --surface-2: #eceef2;
  --text: #14181d;
  --text-2: #667085;
  --border: rgba(15, 23, 42, 0.12);
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --danger: #d92d20;
  --warn-bg: #fff4e5;
  --warn-fg: #8a4b00;
  --radius: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1017;
    --surface: #141b25;
    --surface-2: #1c2530;
    --text: #e9eef5;
    --text-2: #94a3b4;
    --border: rgba(255, 255, 255, 0.12);
    --accent: #4c8dff;
    --accent-text: #06101f;
    --danger: #ff6b5e;
    --warn-bg: #2a1f10;
    --warn-fg: #ffc57a;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

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

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC",
        "Helvetica Neue", "Microsoft YaHei", sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

.ic {
  width: 1.35em;
  height: 1.35em;
  display: block;
  flex: none;
}

.hidden { display: none !important; }

/* ---------- views ---------- */

.view {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.topbar {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 12px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

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

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

.subtitle {
  font-size: 12px;
  color: var(--text-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--accent);
  padding: 6px;
  border-radius: var(--radius);
  cursor: pointer;
}

.icon-btn.small .ic { width: 1.05em; height: 1.05em; }
.icon-btn:active { background: var(--surface-2); }

.menu {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 44px);
  right: 10px;
  min-width: 168px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.24);
  overflow: hidden;
  z-index: 30;
}

.menu button {
  display: block;
  width: 100%;
  text-align: left;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 15px;
  padding: 12px 14px;
  cursor: pointer;
}

.menu button + button { border-top: 1px solid var(--border); }
.menu button:active { background: var(--surface-2); }
.menu button.danger { color: var(--danger); }
.menu button:disabled { color: var(--text-2); }

/* ---------- messages ---------- */

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.empty {
  margin: auto;
  text-align: center;
  color: var(--text-2);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.empty-ic { width: 34px; height: 34px; opacity: 0.5; }
.empty p { margin: 0; font-size: 14px; }

.msg { display: flex; }
.msg.user { justify-content: flex-end; }
.msg.assistant { justify-content: flex-start; }

.bubble {
  max-width: 84%;
  padding: 9px 12px;
  border-radius: var(--radius);
  background: var(--surface);
  overflow-wrap: anywhere;
}

.msg.user .bubble {
  background: var(--accent);
  color: var(--accent-text);
}

.bubble p { margin: 0 0 6px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble > :first-child { margin-top: 0; }

.bubble a { color: inherit; text-decoration: underline; }

.bubble code.inline {
  font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
  font-size: 0.86em;
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 5px;
}

.msg.user .bubble code.inline { background: rgba(255, 255, 255, 0.22); }

.bubble pre.code {
  margin: 8px 0;
  padding: 10px 12px;
  background: var(--surface-2);
  border-radius: var(--radius);
  overflow-x: auto;
}

.msg.user .bubble pre.code { background: rgba(0, 0, 0, 0.22); }

.bubble pre.code code {
  font-family: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
  font-size: 13px;
  background: none;
  padding: 0;
}

.typing { display: inline-flex; gap: 4px; padding: 3px 0; }
.typing i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-2);
  animation: blink 1.2s infinite ease-in-out;
}
.typing i:nth-child(2) { animation-delay: 0.18s; }
.typing i:nth-child(3) { animation-delay: 0.36s; }

@keyframes blink {
  0%, 80%, 100% { opacity: 0.25; }
  40% { opacity: 0.95; }
}

/* ---------- error ---------- */

.error-banner {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 9px 12px;
  background: var(--warn-bg);
  color: var(--warn-fg);
  font-size: 13px;
}
.error-banner span { flex: 1; overflow-wrap: anywhere; }
.error-banner .icon-btn { color: var(--warn-fg); }

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

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

#input {
  flex: 1;
  min-height: 38px;
  max-height: 140px;
  resize: none;
  padding: 9px 12px;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  outline: none;
}

#input:focus { border-color: var(--accent); }
#input:disabled { opacity: 0.6; }

.send-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--accent);
  padding: 3px;
  cursor: pointer;
}
.send-btn .ic { width: 32px; height: 32px; }
.send-btn:disabled { color: var(--text-2); opacity: 0.5; }

.attach-btn {
  appearance: none;
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-2);
  display: grid;
  place-items: center;
  padding: 0;
  cursor: pointer;
}
.attach-btn .ic { width: 21px; height: 21px; }
.attach-btn:active { background: var(--surface-2); }
.attach-btn:disabled { opacity: 0.5; }

/* ---------- attachments ---------- */

.attachments {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 10px 12px 0;
  border-top: 1px solid var(--border);
}

.attachment {
  position: relative;
  flex: 0 0 auto;
  width: 64px;
  height: 64px;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--surface);
}

.attachment img { width: 100%; height: 100%; object-fit: cover; display: block; }

.attachment-remove {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 20px;
  height: 20px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.62);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
}
.attachment-remove .ic { width: 12px; height: 12px; }

/* ---------- images in bubbles ---------- */

.bubble.has-images { padding: 6px; }
.bubble.has-images .md { padding: 1px 6px 0; }
.bubble.has-images .md:empty { display: none; }

.msg-images {
  display: grid;
  gap: 4px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
.msg-images.single { grid-template-columns: minmax(0, 1fr); }
.msg-images img {
  display: block;
  width: 100%;
  max-height: 200px;
  object-fit: cover;
  border-radius: 6px;
  background: rgba(15, 23, 42, 0.06);
  cursor: zoom-in;
}
.msg-images.single img { max-height: 300px; object-fit: contain; }

.msg-note {
  font-size: 12px;
  color: var(--text-2);
  padding: 0 6px 4px;
}
.msg.user .msg-note { color: rgba(255, 255, 255, 0.82); }

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0, 0, 0, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }

/* ---------- tabbar ---------- */

.tabbar {
  display: flex;
  border-top: 1px solid var(--border);
  background: var(--bg);
  padding-bottom: env(safe-area-inset-bottom);
}

.tab {
  flex: 1;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-2);
  font: inherit;
  font-size: 11px;
  padding: 7px 0 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  cursor: pointer;
}
.tab .ic { width: 24px; height: 24px; }
.tab.active { color: var(--accent); }

/* ---------- settings ---------- */

.form {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 12px 24px;
}

.group {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 4px 12px 12px;
  margin-bottom: 18px;
}

.group-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 12px 0 8px;
}

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  border-top: 1px solid var(--border);
}

.row:first-of-type { border-top: 0; }
.row.col { display: block; padding: 10px 0; }

.row label {
  font-size: 15px;
  flex: none;
  min-width: 84px;
  color: var(--text);
}

.row input[type="text"],
.row input[type="url"],
.row input[type="password"],
.row select,
.row textarea {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 15px;
  color: var(--text);
  background: transparent;
  border: 0;
  outline: none;
  padding: 8px 0;
  text-align: right;
}

.row select { text-align-last: right; }

.row.col textarea {
  width: 100%;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 11px;
  margin-top: 8px;
  resize: vertical;
}

.row.col input[type="range"] {
  width: 100%;
  margin-top: 10px;
  accent-color: var(--accent);
}

.hint {
  font-size: 12px;
  line-height: 1.5;
  color: var(--text-2);
  margin: 10px 0 0;
  overflow-wrap: anywhere;
}

.hint code {
  font-family: "SF Mono", ui-monospace, Menlo, monospace;
  background: var(--surface);
  padding: 1px 5px;
  border-radius: 5px;
}

.row-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--accent);
  font: inherit;
  font-size: 15px;
  padding: 11px 0;
  cursor: pointer;
}
.row-btn:disabled { color: var(--text-2); }

.model-list:empty { display: none; }

.model-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  appearance: none;
  border: 0;
  border-top: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 15px;
  padding: 11px 0;
  cursor: pointer;
  text-align: left;
}

.model-item span { flex: 1; overflow-wrap: anywhere; }
.model-item .ic { color: var(--accent); width: 1.15em; height: 1.15em; }

.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }
