:root {
  --bg: #eaf2f0;
  --surface: #ffffff;
  --text: #16323a;
  --muted: #5f7169;
  --primary: #0d9488;
  --primary-2: #0b7d72;
  --primary-soft: #e6f4f1;
  --user-bubble: #0d9488;
  --user-text: #ffffff;
  --assistant-bubble: #ffffff;
  --border: #dde9e6;
  --danger: #dc2626;
  --shadow: 0 8px 30px rgba(15, 55, 50, 0.10);
  --radius: 16px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }

body {
  font-family: "Segoe UI", Tahoma, system-ui, -apple-system, "Noto Naskh Arabic", "Noto Sans Arabic", sans-serif;
  background: linear-gradient(180deg, #e6f0ee, #f3f9f7);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: 880px;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  position: relative;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  box-shadow: var(--shadow);
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.brand { display: flex; align-items: center; gap: 11px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.25; }
.brand h1 { font-size: 17px; margin: 0; font-weight: 700; }
.brand-sub { font-size: 11.5px; color: var(--muted); font-weight: 500; }
.logo {
  width: 40px; height: 40px; border-radius: 12px;
  display: grid; place-items: center;
  color: #fff;
  background: linear-gradient(135deg, var(--primary), #10b981);
}
.logo svg { width: 22px; height: 22px; }
.actions { display: flex; gap: 8px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 11px;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font-size: 18px; cursor: pointer;
  display: grid; place-items: center; transition: background .15s, transform .1s;
}
.icon-btn:hover { background: var(--primary-soft); }
.icon-btn:active { transform: scale(0.95); }

/* إخفاء زر الإعدادات (⚙) بناءً على الطلب — لإعادة إظهاره احذف هذا السطر */
#settings-btn { display: none; }

/* Persistent medical disclaimer */
.disclaimer {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 10px 16px;
  font-size: 12.5px;
  line-height: 1.7;
  color: #8a3a12;
  background: #fff6ec;
  border-bottom: 1px solid #ffe3c9;
}
.disclaimer-ico { flex: none; font-size: 15px; line-height: 1.5; }
.disclaimer strong { color: #b4460f; font-weight: 700; }

/* Banner (e.g. "API key not set") */
.banner {
  margin: 12px 14px 0;
  padding: 11px 14px;
  border-radius: 12px;
  background: #fff7ed;
  color: #9a3412;
  border: 1px solid #fed7aa;
  font-size: 14px;
  line-height: 1.6;
}

/* Chat area */
.chat {
  flex: 1;
  overflow-y: auto;
  padding: 22px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.welcome { margin: auto; text-align: center; color: var(--muted); max-width: 470px; padding: 16px; }
.welcome-logo {
  width: 62px; height: 62px; margin: 0 auto 16px; border-radius: 18px;
  display: grid; place-items: center; color: #fff;
  background: linear-gradient(135deg, var(--primary), #10b981);
  box-shadow: 0 10px 22px rgba(13, 148, 136, 0.28);
}
.welcome-logo svg { width: 32px; height: 32px; }
.welcome h2 { color: var(--text); margin: 0 0 8px; font-size: 21px; }
.welcome p { margin: 0 0 20px; line-height: 1.8; font-size: 14.5px; }
.suggestions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.suggestion {
  text-align: start; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-size: 13.5px; line-height: 1.5; cursor: pointer;
  transition: border-color .15s, background .15s, transform .1s;
}
.suggestion:hover { border-color: var(--primary); background: var(--primary-soft); }
.suggestion:active { transform: scale(0.98); }
@media (max-width: 520px) { .suggestions { grid-template-columns: 1fr; } }

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

.bubble {
  max-width: 78%;
  padding: 11px 15px;
  border-radius: var(--radius);
  line-height: 1.8;
  font-size: 15px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  overflow-wrap: anywhere;
}
.msg.user .bubble { background: var(--user-bubble); color: var(--user-text); }
.msg.assistant .bubble { background: var(--assistant-bubble); border: 1px solid var(--border); }
.msg.assistant .bubble.error { background: #fef2f2; border-color: #fecaca; color: var(--danger); }
.bubble .text { white-space: pre-wrap; }

/* Typing indicator */
.typing { display: inline-flex; gap: 5px; padding: 5px 2px; }
.typing i {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--muted); opacity: .5;
  animation: blink 1.2s infinite both;
}
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink {
  0%, 80%, 100% { opacity: .25; transform: translateY(0); }
  40% { opacity: .9; transform: translateY(-3px); }
}

/* Blinking caret appended at the end of the assistant text while it streams in */
.stream-caret {
  display: inline-block;
  width: 2px;
  height: 1.05em;
  margin-inline-start: 3px;
  vertical-align: -0.18em;
  background: var(--primary);
  border-radius: 1px;
  animation: caret-blink 1.5s step-end infinite;
}
@keyframes caret-blink { 50% { opacity: 0; } }

/* Rendered Markdown inside assistant bubbles */
.bubble .text > :first-child { margin-top: 0; }
.bubble .text > :last-child { margin-bottom: 0; }
.bubble .text h1, .bubble .text h2, .bubble .text h3, .bubble .text h4 {
  margin: .7em 0 .35em; line-height: 1.45; font-weight: 700;
}
.bubble .text h1 { font-size: 1.3em; }
.bubble .text h2 { font-size: 1.18em; }
.bubble .text h3 { font-size: 1.08em; }
.bubble .text h4 { font-size: 1em; }
.bubble .text p { margin: .5em 0; }
.bubble .text ul, .bubble .text ol { margin: .45em 0; padding-inline-start: 1.5em; }
.bubble .text li { margin: .2em 0; }
.bubble .text strong { font-weight: 700; }
.bubble .text em { font-style: italic; }
.bubble .text a { color: var(--primary); text-decoration: underline; }
.bubble .text code {
  background: rgba(0, 0, 0, .06); padding: .1em .35em; border-radius: 5px;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: .92em;
  direction: ltr; unicode-bidi: isolate;
}
.bubble .text pre {
  background: #0f172a; color: #e2e8f0; padding: 12px 14px; border-radius: 10px;
  overflow-x: auto; margin: .6em 0; direction: ltr; text-align: left;
}
.bubble .text pre code { background: none; padding: 0; color: inherit; font-size: .9em; }

/* Composer */
.composer {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 160px;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: #f7f9fc;
  font: inherit;
  color: var(--text);
  line-height: 1.6;
  outline: none;
  transition: border-color .15s, background .15s;
}
.composer textarea:focus { border-color: var(--primary); background: #fff; }
.send-btn {
  padding: 0 22px;
  height: 47px;
  border-radius: 14px;
  border: none;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  font-size: 15px;
  cursor: pointer;
  transition: background .15s, transform .1s;
}
.send-btn:hover { background: var(--primary-2); }
.send-btn:active { transform: scale(0.97); }
.send-btn:disabled { opacity: .5; cursor: not-allowed; }
.send-btn.stop { background: var(--danger); }
.send-btn.stop:hover { background: #b91c1c; }

/* Floating "jump to latest" button */
.scroll-latest {
  position: absolute;
  bottom: 84px;
  left: 50%;
  transform: translateX(-50%) translateY(6px);
  width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface); color: var(--primary);
  font-size: 20px; line-height: 1; cursor: pointer;
  display: grid; place-items: center;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.14);
  opacity: 0; visibility: hidden;
  transition: opacity .2s, transform .2s, visibility .2s;
  z-index: 20;
}
.scroll-latest.show { opacity: 1; visibility: visible; transform: translateX(-50%) translateY(0); }
.scroll-latest:hover { background: var(--primary-soft); }

/* Settings modal */
.modal {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}
.modal.open { display: flex; }
.modal-card {
  width: 100%;
  max-width: 480px;
  background: var(--surface);
  border-radius: 18px;
  box-shadow: var(--shadow);
  overflow: hidden;
  animation: pop .16s ease-out;
}
@keyframes pop { from { transform: translateY(8px) scale(.98); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px; border-bottom: 1px solid var(--border);
}
.modal-head h2 { margin: 0; font-size: 17px; }
.confirm-body { padding: 18px; }
.confirm-body p { margin: 0 0 18px; line-height: 1.8; color: var(--text); }
.settings-form { padding: 18px; display: flex; flex-direction: column; gap: 15px; }
.settings-form label {
  display: flex; flex-direction: column; gap: 6px;
  font-size: 14px; font-weight: 600; color: var(--text);
}
.settings-form small { font-weight: 400; color: var(--muted); font-size: 12.5px; }
.settings-form input, .settings-form textarea {
  font: inherit; font-weight: 400;
  padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: #f7f9fc; color: var(--text);
  outline: none; transition: border-color .15s, background .15s;
}
.settings-form input:focus, .settings-form textarea:focus { border-color: var(--primary); background: #fff; }
.settings-form textarea { resize: vertical; line-height: 1.6; }
.modal-foot { display: flex; gap: 10px; justify-content: flex-start; margin-top: 4px; }
.btn-primary {
  padding: 10px 22px; border: none; border-radius: 10px;
  background: var(--primary); color: #fff; font-weight: 700; cursor: pointer;
}
.btn-primary:hover { background: var(--primary-2); }
.btn-secondary {
  padding: 10px 22px; border: 1px solid var(--border); border-radius: 10px;
  background: #fff; color: var(--text); font-weight: 600; cursor: pointer;
}
.btn-secondary:hover { background: #f2f5fb; }

/* Scrollbar */
.chat::-webkit-scrollbar { width: 10px; }
.chat::-webkit-scrollbar-thumb { background: #d7dce6; border-radius: 10px; border: 3px solid var(--surface); }

@media (max-width: 640px) {
  .app { box-shadow: none; }
  .bubble { max-width: 88%; }
  .brand h1 { font-size: 16px; }
}
