 :root {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1e293b;
  --border: #263349;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #06b6d4;
  --accent-2: #2563eb;
  --danger: #ef4444;
  --success: #22c55e;
  --warn: #f59e0b;
  --user-bubble: #1d4ed8;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

[data-theme="light"] {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --accent: #0284c7;
  --accent-2: #1d4ed8;
  --user-bubble: #1d4ed8;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "IBM Plex Sans Arabic", system-ui, -apple-system, sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

.hidden { display: none !important; }

.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: radial-gradient(circle at top, #172554 0%, var(--bg) 65%);
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 28px 24px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
}

.brand-avatar-wrap {
  width: 84px;
  height: 84px;
  margin: 0 auto 14px;
  border-radius: 50%;
  overflow: hidden;
  border: 2px solid var(--accent);
}

.brand-avatar { width: 100%; height: 100%; object-fit: cover; }

.brand-fallback, .rail-fallback {
  width: 100%;
  height: 100%;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  font-weight: 700;
  font-size: 1.4rem;
}

.auth-card h1 { font-size: 1.4rem; margin-bottom: 4px; }
.auth-subtitle { color: var(--muted); font-size: 0.9rem; margin-bottom: 20px; }

.auth-form { display: flex; flex-direction: column; gap: 14px; text-align: right; }

.field { display: flex; flex-direction: column; gap: 6px; font-size: 0.9rem; }
.field span { color: var(--muted); font-weight: 500; }

input, textarea, select {
  width: 100%;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 13px;
  font: inherit;
  outline: none;
  transition: border-color 0.2s;
}

input:focus, textarea:focus, select:focus { border-color: var(--accent); }

.form-error {
  background: rgba(239, 68, 68, 0.14);
  border: 1px solid rgba(239, 68, 68, 0.4);
  color: #fca5a5;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 0.85rem;
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  border: none;
  border-radius: 11px;
  padding: 11px 18px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.btn-primary:disabled { opacity: 0.55; cursor: not-allowed; }

.app-shell {
  display: grid;
  grid-template-columns: 74px 270px 1fr;
  height: 100dvh;
  width: 100%;
}

.desktop-rail {
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 14px 8px;
  gap: 18px;
}

.rail-brand {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  overflow: hidden;
  border: 1.5px solid var(--accent);
}

.rail-brand img { width: 100%; height: 100%; object-fit: cover; }

.rail-nav { display: flex; flex-direction: column; gap: 10px; width: 100%; }

.rail-nav .nav-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 4px;
  border: none;
  border-radius: 12px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
  font-size: 0.72rem;
}

.rail-nav .nav-btn.active,
.rail-nav .nav-btn:hover {
  background: var(--surface-2);
  color: var(--accent);
}

.nav-icon { font-size: 1.25rem; }

.sidebar {
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px;
  gap: 12px;
  min-height: 0;
}

.sidebar-header { display: flex; justify-content: space-between; align-items: center; }
.sidebar-brand { display: flex; flex-direction: column; }
.sidebar-brand small { color: var(--muted); font-size: 0.78rem; }

.btn-new-chat {
  background: var(--surface-2);
  color: var(--text);
  border: 1px dashed var(--accent);
  border-radius: 11px;
  padding: 10px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.conv-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.conv-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 10px 11px;
  border-radius: 10px;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text);
  cursor: pointer;
  text-align: right;
  font: inherit;
  font-size: 0.88rem;
}

.conv-item:hover, .conv-item.active {
  background: var(--surface-2);
  border-color: var(--border);
}

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

.conv-del {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 2px 5px;
  border-radius: 6px;
}

.conv-del:hover { color: var(--danger); }

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

.health-box {
  background: var(--surface-2);
  border-radius: 10px;
  padding: 9px 10px;
  font-size: 0.78rem;
}

.health-head { display: flex; justify-content: space-between; margin-bottom: 6px; color: var(--muted); }
.text-btn { background: none; border: none; color: var(--accent); cursor: pointer; font: inherit; font-size: 0.78rem; }

.health-pills { display: flex; flex-wrap: wrap; gap: 5px; }
.health-pill {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 3px 8px;
  font-size: 0.72rem;
}
.health-pill.ok { border-color: var(--success); color: var(--success); }
.health-pill.cool { border-color: var(--warn); color: var(--warn); }

.footer-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.btn-ghost {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 8px;
  font: inherit;
  font-size: 0.82rem;
  cursor: pointer;
}
.btn-ghost.danger { color: #f87171; }

.workspace {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  position: relative;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: calc(10px + var(--safe-top)) 18px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar-right { display: flex; align-items: center; gap: 10px; min-width: 0; }
.topbar-titles { display: flex; flex-direction: column; min-width: 0; }
.topbar-titles h2 { font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.template-badge { font-size: 0.76rem; color: var(--accent); }

.status-dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.status-dot.healthy { background: var(--success); box-shadow: 0 0 8px var(--success); }
.status-dot.warn { background: var(--warn); }

.icon-btn {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 38px;
  height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 1.05rem;
}

.view { display: none; flex: 1; min-height: 0; flex-direction: column; position: relative; }
.view.active { display: flex; }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-width: 840px;
  width: 100%;
  margin: 0 auto;
}

.empty-state {
  margin: auto;
  text-align: center;
  max-width: 420px;
  color: var(--muted);
}
.empty-logo { width: 72px; height: 72px; border-radius: 50%; overflow: hidden; margin: 0 auto 12px; border: 2px solid var(--accent); }
.empty-logo img { width: 100%; height: 100%; object-fit: cover; }
.empty-state h3 { color: var(--text); margin-bottom: 8px; }

.msg { display: flex; flex-direction: column; gap: 6px; max-width: 92%; }
.msg.user { align-self: flex-start; }
.msg.assistant { align-self: stretch; max-width: 100%; }

.msg-bubble {
  padding: 13px 16px;
  border-radius: var(--radius);
  line-height: 1.75;
  font-size: 0.96rem;
  word-wrap: break-word;
}

.msg.user .msg-bubble {
  background: var(--user-bubble);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg.assistant .msg-bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

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

.provider-tag {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 2px 9px;
  color: var(--accent);
}

.msg-action-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 7px;
  padding: 2px 8px;
  font: inherit;
  font-size: 0.74rem;
  cursor: pointer;
}
.msg-action-btn:hover { color: var(--text); border-color: var(--accent); }

.msg-bubble h1, .msg-bubble h2, .msg-bubble h3 { margin: 10px 0 6px; font-size: 1.05rem; color: var(--accent); }
.msg-bubble ul, .msg-bubble ol { padding-right: 20px; margin: 6px 0; }
.msg-bubble li { margin: 4px 0; }
.msg-bubble p { margin: 6px 0; }
.msg-bubble code.inline-code {
  direction: ltr;
  unicode-bidi: embed;
  background: var(--surface-2);
  padding: 2px 6px;
  border-radius: 6px;
  font-family: ui-monospace, monospace;
  font-size: 0.88em;
}

.code-block {
  margin: 10px 0;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: #020617;
  color: #e2e8f0;
  text-align: left;
}
.code-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 12px;
  background: #0f172a;
  font-size: 0.76rem;
  color: #94a3b8;
}
.copy-code-btn {
  background: #1e293b;
  color: #e2e8f0;
  border: none;
  border-radius: 6px;
  padding: 3px 9px;
  cursor: pointer;
  font: inherit;
}
.code-block pre { padding: 12px; overflow-x: auto; font-size: 0.85rem; line-height: 1.5; }

.md-table-wrap { overflow-x: auto; margin: 10px 0; }
.md-table { width: 100%; border-collapse: collapse; font-size: 0.86rem; }
.md-table th, .md-table td { border: 1px solid var(--border); padding: 7px 10px; text-align: right; }
.md-table th { background: var(--surface-2); }

.scroll-bottom {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 126px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--accent);
  border-radius: 99px;
  padding: 6px 14px;
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
  z-index: 5;
}

.composer-wrap {
  width: 100%;
  max-width: 840px;
  margin: 0 auto;
  padding: 8px 14px 12px;
}

.quick-templates {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 8px;
  scrollbar-width: none;
}

.quick-pill {
  white-space: nowrap;
  background: var(--surface);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 5px 12px;
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
}
.quick-pill.active, .quick-pill:hover { border-color: var(--accent); color: var(--accent); }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 8px;
}

.composer textarea {
  border: none;
  background: transparent;
  resize: none;
  max-height: 150px;
  padding: 8px 6px;
  line-height: 1.5;
}

.btn-send {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  border: none;
  border-radius: 11px;
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  cursor: pointer;
  font-size: 1rem;
}
.btn-send.stopping { background: var(--danger); }

.composer-hint { text-align: center; color: var(--muted); font-size: 0.74rem; margin-top: 6px; }

.section-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  max-width: 980px;
  width: 100%;
  margin: 0 auto;
}

.section-head { margin-bottom: 18px; }
.section-head p { color: var(--muted); font-size: 0.9rem; margin-top: 4px; }

.templates-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }

.template-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  cursor: pointer;
  text-align: right;
  color: var(--text);
  font: inherit;
  transition: border-color 0.2s, transform 0.15s;
}
.template-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.template-cat { font-size: 0.74rem; color: var(--accent); }
.template-card p { font-size: 0.84rem; color: var(--muted); line-height: 1.6; }

.image-layout { display: grid; grid-template-columns: 360px 1fr; gap: 16px; align-items: start; }
.panel-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.inline-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.checkbox-row { display: flex; align-items: center; gap: 8px; font-size: 0.86rem; cursor: pointer; }
.checkbox-row input { width: auto; }
.muted-note { color: var(--muted); font-size: 0.84rem; }

.img-gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.img-card { background: var(--surface-2); border-radius: 12px; overflow: hidden; border: 1px solid var(--border); }
.img-card img { width: 100%; display: block; aspect-ratio: 1/1; object-fit: cover; }

.audio-tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 14px; }
.audio-tab {
  background: var(--surface);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 10px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.audio-tab.active { border-color: var(--accent); color: var(--accent); background: var(--surface-2); }

.record-center { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 14px 0; }
.record-btn {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: 3px solid var(--accent);
  background: var(--surface-2);
  font-size: 1.8rem;
  cursor: pointer;
}
.record-btn.recording { border-color: var(--danger); background: rgba(239, 68, 68, 0.2); }
.tts-meta { display: flex; justify-content: space-between; font-size: 0.8rem; color: var(--muted); }
.row-actions { display: flex; gap: 10px; }

.toast {
  position: fixed;
  bottom: calc(80px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--accent);
  padding: 9px 16px;
  border-radius: 99px;
  font-size: 0.85rem;
  z-index: 200;
}

.mobile-only { display: none; }
.mobile-bottom-nav { display: none; }
.drawer-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.55); z-index: 40; }

@media (max-width: 960px) {
  .app-shell { grid-template-columns: 1fr; }
  .desktop-rail { display: none; }
  .mobile-only { display: inline-flex; }

  .sidebar {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 82%;
    max-width: 310px;
    z-index: 50;
    padding-top: calc(14px + var(--safe-top));
    padding-bottom: calc(14px + var(--safe-bottom));
    transform: translateX(100%);
    transition: transform 0.25s ease;
  }
  .sidebar.open { transform: translateX(0); }

  .templates-grid { grid-template-columns: 1fr; }
  .image-layout { grid-template-columns: 1fr; }

  .mobile-bottom-nav {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 6px 8px calc(6px + var(--safe-bottom));
  }
  .mobile-bottom-nav .nav-btn {
    background: transparent;
    border: none;
    color: var(--muted);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 4px;
    font: inherit;
    font-size: 0.72rem;
    cursor: pointer;
  }
  .mobile-bottom-nav .nav-btn.active { color: var(--accent); }
  .composer-wrap { padding: 6px 10px 8px; }
}
