:root {
  --bg: #0b0f19;
  --bg-elev: #121829;
  --bg-card: #1a2236;
  --bg-user: #4f46e5;
  --text: #f3f4f6;
  --text-muted: #9ca3af;
  --border: rgba(255, 255, 255, 0.09);
  --accent: #6366f1;
  --accent-soft: rgba(99, 102, 241, 0.16);
  --danger: #ef4444;
  --ok: #22c55e;
  --code-bg: #070a12;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
}

html[data-theme="light"] {
  --bg: #f4f6fb;
  --bg-elev: #ffffff;
  --bg-card: #eef1f8;
  --bg-user: #4f46e5;
  --text: #111827;
  --text-muted: #4b5563;
  --border: rgba(17, 24, 39, 0.1);
  --accent: #4f46e5;
  --accent-soft: rgba(79, 70, 229, 0.12);
  --danger: #dc2626;
  --ok: #16a34a;
  --code-bg: #111827;
  --shadow: 0 12px 32px rgba(15, 23, 42, 0.12);
}

* {
  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: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Arabic", sans-serif;
  line-height: 1.55;
}

.hidden {
  display: none !important;
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 900px;
  margin: 0 auto;
  background: var(--bg);
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.top-title {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1;
}

.brand-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
}

.brand-dot.ok {
  background: var(--ok);
  box-shadow: 0 0 8px var(--ok);
}

.brand-dot.err {
  background: var(--danger);
}

.brand-name {
  font-weight: 700;
  font-size: 0.95rem;
  white-space: nowrap;
}

.model-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  max-width: 170px;
  min-height: 36px;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--accent-soft);
  color: var(--text);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
}

.model-pill span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.model-pill svg {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  border: none;
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}

.icon-btn.small {
  min-width: 38px;
  min-height: 38px;
}

.icon-btn:active {
  background: var(--bg-card);
}

.icon-btn svg {
  width: 22px;
  height: 22px;
}

.strategy-bar {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  overflow-x: auto;
  scrollbar-width: none;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.strategy-bar::-webkit-scrollbar {
  display: none;
}

.chip {
  flex-shrink: 0;
  min-height: 34px;
  padding: 5px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
}

.chip.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.api-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  background: var(--accent-soft);
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
}

.banner-btn {
  flex-shrink: 0;
  min-height: 36px;
  padding: 6px 12px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.empty-state {
  margin: auto;
  text-align: center;
  max-width: 440px;
  padding: 20px 12px;
}

.empty-logo {
  width: 72px;
  height: 72px;
  border-radius: 20px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

.empty-state h1 {
  margin: 0 0 6px;
  font-size: 1.35rem;
}

.empty-state p {
  margin: 0 0 18px;
  color: var(--text-muted);
  font-size: 0.92rem;
}

.starter-grid {
  display: grid;
  gap: 10px;
}

.starter-card {
  width: 100%;
  min-height: 46px;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  font-size: 0.9rem;
  text-align: start;
  cursor: pointer;
}

.msg-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: 92%;
}

.msg-row.user {
  align-self: flex-start;
}

.msg-row.assistant {
  align-self: flex-end;
  max-width: 96%;
  width: 100%;
}

.msg-bubble {
  padding: 11px 14px;
  border-radius: 16px;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  font-size: 0.96rem;
}

.msg-row.user .msg-bubble {
  background: var(--bg-user);
  color: #fff;
  border-bottom-right-radius: 6px;
  white-space: pre-wrap;
}

.msg-row.assistant .msg-bubble {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}

.msg-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  font-size: 0.76rem;
  color: var(--text-muted);
}

.routed-badge {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.msg-action-btn {
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-muted);
  border-radius: 8px;
  padding: 4px 9px;
  min-height: 30px;
  font-size: 0.75rem;
  cursor: pointer;
}

.md-content p {
  margin: 0 0 8px;
}

.md-content p:last-child {
  margin-bottom: 0;
}

.md-content ul, .md-content ol {
  margin: 6px 0;
  padding-inline-start: 22px;
}

.md-content h1, .md-content h2, .md-content h3 {
  margin: 10px 0 6px;
  font-size: 1.05rem;
}

.md-content code.inline-code {
  padding: 2px 6px;
  border-radius: 6px;
  background: var(--bg-card);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.86em;
}

.code-block-wrap {
  margin: 8px 0;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--code-bg);
  color: #e5e7eb;
  direction: ltr;
  text-align: left;
}

.code-block-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 10px;
  background: rgba(255, 255, 255, 0.05);
  font-size: 0.75rem;
  color: #9ca3af;
}

.code-copy-btn {
  border: none;
  border-radius: 6px;
  padding: 4px 8px;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  font-size: 0.74rem;
  cursor: pointer;
}

.code-block-wrap pre {
  margin: 0;
  padding: 10px 12px;
  overflow-x: auto;
  font-size: 0.84rem;
  line-height: 1.45;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.composer-wrap {
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.composer-box {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 18px;
  border: 1px solid var(--border);
  background: var(--bg-card);
}

#promptInput {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
  max-height: 140px;
  padding: 8px 6px;
  line-height: 1.45;
}

.send-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  flex-shrink: 0;
  cursor: pointer;
}

.send-btn.stop {
  background: var(--danger);
}

.send-btn svg {
  width: 20px;
  height: 20px;
}

.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 40;
}

.drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  inset-inline-start: 0;
  width: min(84vw, 330px);
  background: var(--bg-elev);
  border-inline-end: 1px solid var(--border);
  z-index: 50;
  display: flex;
  flex-direction: column;
  padding: calc(14px + env(safe-area-inset-top)) 14px calc(14px + env(safe-area-inset-bottom));
  transform: translateX(105%);
  transition: transform 0.2s ease;
}

html[dir="ltr"] .drawer {
  transform: translateX(-105%);
}

.drawer.open {
  transform: translateX(0);
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.drawer-header h2 {
  margin: 0;
  font-size: 1.1rem;
}

.drawer-actions {
  display: flex;
  gap: 4px;
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 12px 0;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 12px;
  border: 1px solid transparent;
  background: var(--bg-card);
  cursor: pointer;
}

.chat-item.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.chat-item-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.88rem;
}

.chat-del-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  min-width: 32px;
  min-height: 32px;
  cursor: pointer;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 12px;
}

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

.modal-card {
  width: 100%;
  max-width: 480px;
  max-height: 86dvh;
  overflow-y: auto;
  border-radius: 20px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  padding: 16px;
  box-shadow: var(--shadow);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.05rem;
}

.field-label {
  display: block;
  margin: 12px 0 6px;
  font-size: 0.86rem;
  color: var(--text-muted);
}

.key-row, .model-search-row {
  display: flex;
  gap: 8px;
}

input[type="password"],
input[type="text"],
input[type="search"],
.settings-body textarea {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text);
  font-family: inherit;
  font-size: 0.94rem;
}

.slider-row {
  margin: 10px 0;
}

.slider-row input[type="range"] {
  width: 100%;
}

.primary-full-btn, .install-btn {
  width: 100%;
  min-height: 46px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
}

.install-btn {
  background: var(--ok);
}

.secondary-btn {
  min-height: 44px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
}

.secondary-link-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 10px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text);
  text-decoration: none;
  font-size: 0.88rem;
  font-weight: 600;
  text-align: center;
}

.danger-ghost-btn {
  width: 100%;
  min-height: 42px;
  border-radius: 12px;
  border: 1px solid rgba(239, 68, 68, 0.35);
  background: transparent;
  color: var(--danger);
  font-weight: 600;
  cursor: pointer;
}

.settings-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 14px;
}

.install-hint {
  margin: 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.model-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 12px;
  max-height: 55dvh;
  overflow-y: auto;
}

.model-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 44px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text);
  font-size: 0.88rem;
  text-align: left;
  direction: ltr;
  cursor: pointer;
}

.model-option.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.toast {
  position: fixed;
  bottom: calc(80px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  padding: 8px 16px;
  border-radius: 999px;
  background: #1f2937;
  color: #fff;
  font-size: 0.84rem;
  z-index: 80;
  box-shadow: var(--shadow);
}
