:root {
  --bg: #0a0a0a;
  --bg-elev: #121212;
  --bg-hover: #1b1b1b;
  --border: #262626;
  --text: #f5f5f5;
  --text-dim: #a3a3a3;
  --text-faint: #737373;
  --accent-1: #f5f5f5;
  --accent-2: #b3b3b3;
  --accent-3: #6e6e6e;
  --gradient: linear-gradient(135deg, var(--accent-1), var(--accent-2), var(--accent-3));
  --radius: 14px;
  --font: 'Cairo', 'Segoe UI', Tahoma, system-ui, sans-serif;
  --shadow: 0 10px 30px rgba(0, 0, 0, .5);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  display: flex;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

button {
  font-family: var(--font);
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
  touch-action: manipulation;
}

a {
  color: var(--accent-3);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

/* ===== Scrollbars ===== */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-thumb {
  background: #2e2e2e;
  border-radius: 8px;
}

::-webkit-scrollbar-thumb:hover {
  background: #424242;
}

::-webkit-scrollbar-track {
  background: transparent;
}

/* ===== Sidebar ===== */
.sidebar {
  width: 280px;
  min-width: 280px;
  background: var(--bg-elev);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px;
  gap: 14px;
  transition: transform .25s ease;
  z-index: 30;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 6px 2px;
}

.logo-mark {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--gradient);
  color: #fff;
  font-weight: 800;
  font-size: 20px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  box-shadow: 0 0 22px rgba(190, 190, 190, .35);
}

.logo-img {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  object-fit: contain;
  background: #fff;
  padding: 5px;
  flex-shrink: 0;
  box-shadow: 0 0 14px rgba(190, 190, 190, .25);
  border: 1px solid #e2e2e2;
}

.logo-mark.sm {
  width: 30px;
  height: 30px;
  font-size: 16px;
  border-radius: 8px;
  box-shadow: 0 0 14px rgba(190, 190, 190, .3);
}

.logo-text {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: .5px;
}

.logo-text b {
  background: var(--gradient);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.new-chat {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 11px 12px;
  border-radius: 12px;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  transition: all .15s;
}

.new-chat:hover {
  border-color: var(--accent-2);
  transform: translateY(-1px);
  box-shadow: 0 4px 14px rgba(190, 190, 190, .15);
}

.conv-label {
  font-size: 12px;
  color: var(--text-faint);
  font-weight: 600;
  padding: 0 8px;
  margin-top: 4px;
}

.conversations {
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 13px;
  color: var(--text-dim);
  text-align: right;
  transition: background .12s, color .12s;
  border: 1px solid transparent;
}

.conv-item:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.conv-item.active {
  background: var(--bg-hover);
  color: var(--text);
  border-color: var(--border);
}

.conv-item.active .conv-title {
  font-weight: 700;
}

.conv-item.active::before {
  content: '';
  width: 3px;
  height: 18px;
  border-radius: 2px;
  background: var(--gradient);
  margin-left: 2px;
}

.conv-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-del {
  color: var(--text-faint);
  padding: 4px;
  border-radius: 6px;
  line-height: 0;
  opacity: .55;
  transition: opacity .12s;
}

.conv-item:hover .conv-del {
  opacity: 1;
}

.conv-del:hover {
  color: var(--danger, #ef4444);
  background: rgba(239, 68, 68, .12);
  opacity: 1;
}

.sidebar-bottom {
  border-top: 1px solid var(--border);
  padding-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.settings-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 13px;
  color: var(--text-dim);
  text-align: right;
  transition: background .12s, color .12s;
}

.clear-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 13px;
  color: var(--text-dim);
  text-align: right;
  transition: background .12s, color .12s;
}

.clear-btn:hover {
  background: rgba(239, 68, 68, .1);
  color: var(--danger, #ef4444);
}

.settings-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.status {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px 2px;
  font-size: 12px;
  color: var(--text-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--danger, #ef4444);
  flex-shrink: 0;
}

.status.ok .status-dot {
  background: #22c55e;
  box-shadow: 0 0 10px rgba(34, 197, 94, .6);
}

/* ===== Main ===== */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  min-width: 0;
}

.main::before {
  content: '';
  position: absolute;
  inset: 0;
  background:
    radial-gradient(700px circle at 50% -10%, rgba(190, 190, 190, .08), transparent 60%),
    radial-gradient(550px circle at 88% 25%, rgba(190, 190, 190, .06), transparent 60%),
    radial-gradient(550px circle at 10% 70%, rgba(190, 190, 190, .06), transparent 60%);
  pointer-events: none;
  z-index: 0;
}

.header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 22px;
  border-bottom: 1px solid var(--border);
  background: rgba(10, 10, 10, .6);
  backdrop-filter: blur(12px);
  z-index: 1;
}

body.light .header {
  background: rgba(255, 255, 255, .75);
}

.menu-btn {
  display: none;
  padding: 7px;
  border-radius: 10px;
  color: var(--text-dim);
}

.menu-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-col {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

.logo-sub {
  font-size: 11px;
  color: var(--text-faint);
}

.header-actions {
  margin-right: auto;
  display: flex;
  gap: 4px;
}

.icon-btn {
  padding: 8px;
  border-radius: 10px;
  color: var(--text-dim);
  display: inline-grid;
  place-items: center;
  line-height: 0;
  transition: background .12s, color .12s;
}

.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.moon { display: none; }
body.light .sun { display: none; }
body.light .moon { display: block; }

/* ===== Chat ===== */
.chat-scroll {
  flex: 1;
  overflow-y: auto;
  scroll-behavior: smooth;
  z-index: 1;
}

.chat {
  max-width: 800px;
  margin: 0 auto;
  padding: 28px 22px 30px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.msg {
  display: flex;
  gap: 12px;
  animation: fadeUp .3s ease;
}

@keyframes fadeUp {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg.user {
  flex-direction: row-reverse;
}

.avatar {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  font-size: 14px;
  font-weight: 800;
  margin-top: 2px;
}

.ai-avatar {
  background: var(--gradient);
  color: #fff;
  box-shadow: 0 0 14px rgba(190, 190, 190, .3);
}

.user-avatar {
  background: var(--bg-hover);
  border: 1px solid var(--border);
  color: var(--text-dim);
}

.msg-content {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: calc(100% - 42px);
}

.msg.user .msg-content {
  align-items: flex-end;
}

.bubble {
  padding: 12px 16px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.75;
  word-break: break-word;
}

.msg.user .bubble {
  background: var(--gradient);
  color: #fff;
  border-bottom-left-radius: 4px;
  box-shadow: 0 4px 16px rgba(190, 190, 190, .18);
  white-space: pre-wrap;
}

.msg.ai .bubble {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-bottom-right-radius: 4px;
}

/* markdown content inside AI bubble */
.bubble h2, .bubble h3, .bubble h4 {
  margin: 14px 0 6px;
  line-height: 1.4;
  font-weight: 800;
}

.bubble h2 { font-size: 17px; }
.bubble h3 { font-size: 16px; }
.bubble h4 { font-size: 15px; }

.bubble p {
  margin: 6px 0;
}

.bubble p:first-child {
  margin-top: 0;
}

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

.bubble strong {
  color: #fff;
}

body.light .bubble strong {
  color: #111;
}

.bubble ul, .bubble ol {
  padding-right: 22px;
  margin: 8px 0;
}

.bubble li {
  margin: 3px 0;
}

.bubble blockquote {
  border-right: 3px solid var(--accent-2);
  padding: 4px 12px;
  margin: 8px 0;
  color: var(--text-dim);
  background: var(--bg);
  border-radius: 4px;
}

.bubble code {
  font-family: Consolas, 'Courier New', monospace;
  font-size: 13px;
  background: rgba(190, 190, 190, .12);
  color: var(--accent-3);
  padding: 2px 6px;
  border-radius: 6px;
  direction: ltr;
  unicode-bidi: embed;
}

.bubble pre.code-block {
  background: #0d0d0d;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  overflow-x: auto;
  margin: 10px 0;
  direction: ltr;
  text-align: left;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .03);
}

.bubble pre.code-block code {
  background: none;
  color: #e4e4e7;
  padding: 0;
  font-size: 13px;
  line-height: 1.6;
}

.bubble-img {
  display: block;
  max-width: 260px;
  max-height: 260px;
  border-radius: 10px;
  margin-top: 6px;
  cursor: pointer;
  border: 1px solid var(--border);
}

.bubble-text {
  white-space: pre-wrap;
}

.msg-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--text-faint);
  padding: 0 4px;
}

.copy-btn {
  color: var(--text-faint);
  display: inline-grid;
  place-items: center;
  line-height: 0;
  padding: 4px;
  border-radius: 6px;
  opacity: 0;
  transition: opacity .15s;
}

.msg:hover .copy-btn {
  opacity: 1;
}

.copy-btn:hover {
  color: var(--text);
  background: var(--bg-hover);
}

/* typing indicator */
.typing-dots {
  display: inline-flex;
  gap: 5px;
  padding: 6px 2px;
}

.typing-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--gradient);
  animation: bounce 1.2s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: .15s; }
.typing-dots span:nth-child(3) { animation-delay: .3s; }

@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: .4; }
  40% { transform: translateY(-6px); opacity: 1; }
}

.cursor {
  display: inline-block;
  width: 8px;
  height: 16px;
  background: var(--gradient);
  border-radius: 2px;
  vertical-align: -2px;
  margin-inline-start: 3px;
  animation: blink 1s steps(2, start) infinite;
}

@keyframes blink { 50% { opacity: 0; } }

/* welcome screen */
.welcome {
  text-align: center;
  margin: auto;
  padding: 40px 0;
  max-width: 600px;
}

.welcome .glow-wrap {
  position: relative;
  display: inline-block;
  margin-bottom: 20px;
}

.welcome .glow-wrap::before {
  content: '';
  position: absolute;
  inset: -30px;
  background: var(--gradient);
  filter: blur(50px);
  opacity: .35;
  border-radius: 50%;
  z-index: 0;
}

.welcome .big-mark {
  position: relative;
  z-index: 1;
  width: 72px;
  height: 72px;
  border-radius: 20px;
  background: var(--gradient);
  color: #fff;
  font-weight: 800;
  font-size: 38px;
  display: grid;
  place-items: center;
  margin: 0 auto;
}

.welcome .big-logo {
  position: relative;
  z-index: 1;
  width: 76px;
  height: 76px;
  border-radius: 20px;
  object-fit: contain;
  background: #fff;
  padding: 12px;
  margin: 0 auto;
  border: 1px solid #e2e2e2;
  box-shadow: 0 0 30px rgba(190, 190, 190, .3);
}

.welcome h1 {
  font-size: 30px;
  font-weight: 800;
  margin-bottom: 10px;
}

.welcome h1 span {
  background: var(--gradient);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.welcome .sub {
  color: var(--text-dim);
  margin-bottom: 32px;
  font-size: 15px;
  line-height: 1.7;
}

.suggestions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.suggestion {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--bg-elev);
  font-size: 13.5px;
  color: var(--text-dim);
  text-align: right;
  transition: all .18s;
  line-height: 1.6;
  display: flex;
  align-items: center;
  gap: 10px;
}

.suggestion svg {
  flex-shrink: 0;
  color: var(--accent-2);
  transition: transform .2s;
}

.suggestion:hover {
  border-color: rgba(190, 190, 190, .5);
  color: var(--text);
  transform: translateY(-3px);
  box-shadow: 0 8px 24px rgba(190, 190, 190, .12);
}

.suggestion:hover svg {
  transform: scale(1.15);
}

/* ===== Composer ===== */
.composer-wrap {
  padding: 8px 22px 16px;
  max-width: 800px;
  margin: 0 auto;
  width: 100%;
  z-index: 1;
}

.model-row {
  display: flex;
  justify-content: center;
  margin-bottom: 8px;
}

.model-badge {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  font-size: 12px;
  color: var(--text-dim);
  transition: all .15s;
}

.model-badge:hover {
  border-color: var(--accent-2);
  color: var(--text);
}

.model-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #22c55e;
  box-shadow: 0 0 8px rgba(34, 197, 94, .7);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 12px 14px;
  transition: border-color .15s, box-shadow .2s;
  box-shadow: var(--shadow);
}

.attach-btn {
  width: 36px;
  height: 36px;
  min-width: 36px;
  border-radius: 10px;
  color: var(--text-dim);
  display: grid;
  place-items: center;
  transition: background .12s, color .12s;
}

.attach-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.attach-preview {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 8px;
  padding: 8px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  max-width: 280px;
}

.attach-preview[hidden] {
  display: none;
}

.attach-preview img {
  max-width: 190px;
  max-height: 140px;
  border-radius: 8px;
  object-fit: cover;
}

.attach-remove {
  color: var(--text-dim);
  display: grid;
  place-items: center;
  padding: 4px;
  border-radius: 6px;
  line-height: 0;
}

.attach-remove:hover {
  color: var(--danger, #ef4444);
  background: rgba(239, 68, 68, .12);
}

.composer:focus-within {
  border-color: var(--accent-2);
  box-shadow: 0 0 0 4px rgba(190, 190, 190, .15), var(--shadow);
}

#input {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.6;
  resize: none;
  max-height: 180px;
  min-height: 24px;
}

#input::placeholder {
  color: var(--text-faint);
}

.send-btn {
  width: 38px;
  height: 38px;
  min-width: 38px;
  border-radius: 12px;
  background: var(--gradient);
  color: #fff;
  display: grid;
  place-items: center;
  transition: opacity .15s, transform .1s;
  box-shadow: 0 4px 14px rgba(190, 190, 190, .3);
}

.send-btn:hover { opacity: .92; transform: scale(1.05); }
.send-btn:active { transform: scale(.94); }
.send-btn:disabled { opacity: .35; cursor: default; transform: none; box-shadow: none; }

.send-btn.stop-mode {
  background: var(--bg-hover);
  border: 1px solid var(--border);
  box-shadow: none;
}

.send-btn.stop-mode:hover {
  background: #2a2a2a;
}

body.light .send-btn.stop-mode {
  background: #ececec;
  border-color: #d6d6d6;
}

body.light .send-btn.stop-mode:hover {
  background: #e0e0e0;
}

.disclaimer {
  text-align: center;
  font-size: 11px;
  color: var(--text-faint);
  margin-top: 10px;
}

/* ===== Modal ===== */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .6);
  backdrop-filter: blur(3px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
  z-index: 50;
}

.overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -46%) scale(.97);
  width: min(440px, calc(100vw - 32px));
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 22px;
  z-index: 60;
  opacity: 0;
  pointer-events: none;
  transition: all .22s ease;
  box-shadow: var(--shadow);
}

.modal.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
  pointer-events: auto;
}

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

.modal-head h2 {
  font-size: 18px;
  font-weight: 800;
}

.modal-body label {
  display: block;
  font-size: 12.5px;
  color: var(--text-dim);
  margin: 14px 0 6px;
  font-weight: 600;
}

.modal-body input,
.modal-body select {
  width: 100%;
  padding: 11px 13px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  outline: none;
  transition: border-color .15s;
}

.modal-body select {
  appearance: none;
  cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-dim) 50%), linear-gradient(135deg, var(--text-dim) 50%, transparent 50%);
  background-position: calc(100% - 20px) 50%, calc(100% - 15px) 50%;
  background-size: 5px 5px;
  background-repeat: no-repeat;
  padding-left: 34px;
}

.modal-body input:focus,
.modal-body select:focus {
  border-color: var(--accent-2);
}

.modal-body .hint {
  font-size: 12px;
  color: var(--text-faint);
  margin-top: 12px;
  line-height: 1.7;
}

.save-btn {
  width: 100%;
  margin-top: 20px;
  padding: 12px;
  border-radius: 12px;
  background: var(--gradient);
  color: #fff;
  font-weight: 800;
  font-size: 15px;
  transition: opacity .15s, transform .1s;
}

.save-btn:hover { opacity: .92; transform: translateY(-1px); }
.save-btn:active { transform: scale(.98); }

/* ===== Responsive ===== */
@media (max-width: 768px) {
  html, body {
    height: 100vh;
    height: 100dvh;
  }

  .menu-btn { display: block; }

  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    right: 0;
    transform: translateX(100%);
    z-index: 60;
    width: min(300px, 82vw);
    border-left: none;
    box-shadow: -20px 0 60px rgba(0, 0, 0, .5);
    padding-top: max(14px, env(safe-area-inset-top));
    padding-bottom: max(14px, env(safe-area-inset-bottom));
  }

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

  .header {
    padding-top: max(12px, env(safe-area-inset-top));
  }

  .suggestions {
    grid-template-columns: 1fr;
  }

  .bubble {
    font-size: 14.5px;
  }

  .chat {
    padding: 20px 14px 24px;
  }

  .chat-scroll {
    scroll-behavior: auto;
  }

  .composer-wrap {
    padding: 6px 12px max(12px, env(safe-area-inset-bottom));
  }

  #input {
    font-size: 16px;
  }
}

@media (max-width: 380px) {
  .logo-sub {
    display: none;
  }

  .msg-meta {
    font-size: 10px;
  }
}

/* ===== Light theme ===== */
body.light {
  --bg: #fafafa;
  --bg-elev: #ffffff;
  --bg-hover: #f2f2f2;
  --border: #e5e5e5;
  --text: #1a1a1a;
  --text-dim: #565656;
  --text-faint: #9a9a9a;
  --shadow: 0 10px 30px rgba(0, 0, 0, .12);
}

body.light .msg.ai .bubble {
  border-color: #e8e8e8;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .04);
}

body.light .bubble pre.code-block {
  background: #0d0d0d;
}

body.light .bubble code {
  background: rgba(190, 190, 190, .15);
}

body.light .bubble blockquote {
  background: #f6f6f6;
}

body.light .user-avatar {
  background: #efefef;
}
