:root {
  --bg: #0a0a0f;
  --bg-elevated: #14141d;
  --bg-bubble-user: #3b5bdb;
  --bg-bubble-ai: #1c1c28;
  --text: #e7e7ee;
  --text-muted: #8a8a9a;
  --text-dim: #5a5a6a;
  --accent: #6c8cff;
  --accent-press: #5470e8;
  --border: rgba(255,255,255,0.08);
  --danger: #ff5c5c;
  --ok: #38d39f;
  --warn: #ffb454;
  --claude: #d97757;
  --mistral: #ff7000;
  --deepseek: #4f8cff;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --header-h: 52px;
  --composer-h: 64px;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", "Roboto", "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

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

button { font-family: inherit; }

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-h);
  padding: 0 8px;
  padding-top: var(--safe-top);
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 20;
  flex: 0 0 auto;
}
.header-left, .header-right { flex: 1; display: flex; }
.header-right { justify-content: flex-end; }
.header-center { flex: 0 0 auto; }

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text);
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background .15s;
}
.icon-btn:active { background: rgba(255,255,255,0.08); }
.icon-btn.small { width: 36px; height: 36px; }

.provider-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
}
.provider-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  display: inline-block;
}

/* Drawer */
.drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 80%;
  max-width: 320px;
  background: var(--bg-elevated);
  z-index: 50;
  transform: translateX(-100%);
  transition: transform .25s ease;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}
.drawer.open { transform: translateX(0); }
.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}
.drawer-header h2 { margin: 0; font-size: 16px; }
.conversation-list {
  list-style: none;
  margin: 0;
  padding: 8px;
  overflow-y: auto;
  flex: 1;
}
.conversation-list li {
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.conversation-list li.active { background: var(--bg); color: var(--text); }
.conversation-list li .del {
  opacity: 0;
  background: transparent; border: none; color: var(--danger);
  font-size: 18px; padding: 0 4px;
}
.conversation-list li:hover .del, .conversation-list li:active .del { opacity: 1; }
.drawer-footer { padding: 12px 16px; border-top: 1px solid var(--border); }
.hint { font-size: 12px; color: var(--text-dim); margin: 0; }

.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
}
.scrim.show { opacity: 1; pointer-events: auto; }

/* Settings sheet */
.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg-elevated);
  z-index: 60;
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
  padding: 8px 16px calc(16px + var(--safe-bottom));
  max-height: 88vh;
  overflow-y: auto;
  transform: translateY(100%);
  transition: transform .25s ease;
}
.sheet.open { transform: translateY(0); }
.sheet-handle {
  width: 40px; height: 4px; background: var(--text-dim);
  border-radius: 2px; margin: 6px auto 12px;
}
.sheet h2 { margin: 0 0 12px; font-size: 18px; }
.field {
  display: block;
  margin-bottom: 14px;
}
.field-label {
  display: block;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 6px;
}
.field-hint {
  display: block;
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 6px;
  line-height: 1.4;
}
.field input[type="text"],
.field input[type="password"],
.field input[type="number"],
.field select,
.field textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 15px;
  font-family: inherit;
}
.field textarea { resize: vertical; }
.field.row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--text-muted);
}
.key-input-row {
  display: flex;
  gap: 6px;
  align-items: center;
}
.key-input-row input { flex: 1; }
.sheet-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 16px;
}
.security-note {
  font-size: 12px;
  color: var(--text-dim);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  margin-top: 12px;
  line-height: 1.5;
}

/* Buttons */
.primary-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.primary-btn:active { background: var(--accent-press); }
.primary-btn.small { padding: 6px 10px; font-size: 13px; }
.ghost-btn {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 14px;
  cursor: pointer;
}
.ghost-btn:active { background: rgba(255,255,255,0.05); }

/* Chat area */
.chat-area {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 0;
  position: relative;
}
.messages { display: flex; flex-direction: column; gap: 10px; padding: 0 12px; }

.empty-state {
  text-align: center;
  padding: 60px 24px;
  color: var(--text-muted);
}
.empty-icon { color: var(--text-dim); margin-bottom: 12px; }
.empty-state h3 { margin: 0 0 8px; color: var(--text); font-size: 17px; }
.empty-state p { font-size: 14px; margin: 0 auto; max-width: 320px; line-height: 1.5; }

.msg {
  max-width: 88%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 15px;
  word-wrap: break-word;
  overflow-wrap: break-word;
  white-space: pre-wrap;
  position: relative;
}
.msg.user {
  align-self: flex-end;
  background: var(--bg-bubble-user);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.msg.ai {
  align-self: flex-start;
  background: var(--bg-bubble-ai);
  color: var(--text);
  border-bottom-left-radius: 4px;
}
.msg.system {
  align-self: center;
  background: transparent;
  color: var(--text-dim);
  font-size: 12px;
  font-style: italic;
}
.msg.error {
  align-self: center;
  background: rgba(255,92,92,0.1);
  color: var(--danger);
  border: 1px solid rgba(255,92,92,0.3);
  font-size: 13px;
}
.msg .meta {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 4px;
  display: flex;
  justify-content: space-between;
}
.msg.user .meta { color: rgba(255,255,255,0.7); }

/* Typing indicator */
.typing-indicator {
  display: inline-flex;
  gap: 4px;
  padding: 10px 14px;
  background: var(--bg-bubble-ai);
  border-radius: 16px;
  border-bottom-left-radius: 4px;
  margin: 0 12px;
  align-self: flex-start;
  width: fit-content;
}
.typing-indicator span {
  width: 7px; height: 7px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: blink 1.4s infinite both;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink {
  0%, 80%, 100% { opacity: 0.3; }
  40% { opacity: 1; }
}

/* Composer */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 10px calc(8px + var(--safe-bottom));
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
  flex: 0 0 auto;
  position: relative;
  z-index: 10;
}
.composer textarea {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 18px;
  padding: 10px 14px;
  font-size: 16px;
  font-family: inherit;
  resize: none;
  max-height: 120px;
  line-height: 1.4;
}
.send-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex: 0 0 auto;
  transition: background .15s, opacity .15s;
}
.send-btn:active { background: var(--accent-press); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* Code blocks */
.msg pre {
  background: #000;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  overflow-x: auto;
  font-size: 13px;
  margin: 6px 0;
}
.msg code {
  font-family: "SF Mono", "Menlo", "Consolas", monospace;
}

/* Scrollbar */
.chat-area::-webkit-scrollbar { width: 4px; }
.chat-area::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* Disable text selection on UI chrome */
.app-header, .composer, .drawer-header, .sheet-handle { user-select: none; -webkit-user-select: none; }
