/* ============================================================
   Med Rep Pro — Ask AI chat drawer + consent modal
   Owns: #ai-chat-root (mounted outside #root by ai-chat.js)
   Tokens: inherits from assets/css/main.css (--accent, --card, etc.)
   ============================================================ */

/* The mount node sits as a sibling to #root. Its descendants use
   position:fixed so the drawer/modal float above everything. */
#ai-chat-root {
  position: fixed; inset: 0;
  pointer-events: none;            /* let clicks fall through when closed */
  z-index: 9000;                   /* above app chrome, below leaflet tooltips? unlikely to collide */
}
#ai-chat-root > * { pointer-events: auto; }

/* ── Floating launcher bubble ───────────────────────────────────────────── */
.ai-bubble {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  right: calc(24px + env(safe-area-inset-right, 0px));
  width: 56px; height: 56px;
  border-radius: var(--r-pill, 9999px);
  border: 1px solid var(--border-acc, rgba(44,194,149,0.40));
  background: linear-gradient(135deg, var(--accent, #2CC295) 0%, var(--accent2, #00DBF1) 100%);
  color: var(--green-dark, #032221);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow-pop, 0 12px 40px rgba(0,0,0,0.40)),
              0 0 0 4px rgba(44,194,149,0.10);
  transition: transform 160ms ease, box-shadow 160ms ease;
}
.ai-bubble:hover  { transform: scale(1.05); box-shadow: var(--shadow-pop), 0 0 0 6px rgba(44,194,149,0.16); }
.ai-bubble:active { transform: scale(0.96); }
.ai-bubble:focus-visible { outline: 2px solid var(--accent2, #00DBF1); outline-offset: 3px; }
.ai-bubble svg { width: 26px; height: 26px; }

/* RTL flip — bubble goes to the left edge in Arabic. */
[dir="rtl"] .ai-bubble { right: auto; left: calc(24px + env(safe-area-inset-left, 0px)); }

/* Hide bubble while drawer is open (drawer is the active surface). */
.ai-bubble[hidden] { display: none !important; }

/* ── Backdrop ───────────────────────────────────────────────────────────── */
.ai-backdrop {
  position: fixed; inset: 0;
  background: rgba(2,12,9,0.55);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  opacity: 0; visibility: hidden;
  transition: opacity 200ms ease, visibility 0s linear 200ms;
}
.ai-backdrop.open {
  opacity: 1; visibility: visible;
  transition: opacity 200ms ease, visibility 0s linear 0s;
}
.theme-light .ai-backdrop { background: rgba(10,46,36,0.30); }

/* ── Drawer ─────────────────────────────────────────────────────────────── */
.ai-drawer {
  position: fixed;
  top: 0; bottom: 0; right: 0;
  width: 100%; max-width: 420px;
  background: var(--bg-2, #04241d);
  color: var(--text-1, #F1F7F6);
  border-left: 1px solid var(--card-border, rgba(44,194,149,0.18));
  box-shadow: -12px 0 40px rgba(0,0,0,0.40);
  display: flex; flex-direction: column;
  transform: translateX(100%);
  transition: transform 260ms cubic-bezier(0.4, 0, 0.2, 1);
}
.ai-drawer.open { transform: translateX(0); }

/* RTL mirror */
[dir="rtl"] .ai-drawer {
  right: auto; left: 0;
  border-left: none;
  border-right: 1px solid var(--card-border, rgba(44,194,149,0.18));
  box-shadow: 12px 0 40px rgba(0,0,0,0.40);
  transform: translateX(-100%);
}
[dir="rtl"] .ai-drawer.open { transform: translateX(0); }

.theme-light .ai-drawer { box-shadow: -8px 0 28px rgba(3,98,76,0.12); }
[dir="rtl"].theme-light .ai-drawer,
.theme-light [dir="rtl"] .ai-drawer { box-shadow: 8px 0 28px rgba(3,98,76,0.12); }

/* ── Header ─────────────────────────────────────────────────────────────── */
.ai-header {
  flex: none;
  padding: 14px 16px;
  padding-top: calc(14px + env(safe-area-inset-top, 0px));
  display: flex; align-items: center; gap: 10px;
  border-bottom: 1px solid var(--card-border, rgba(44,194,149,0.18));
  background: var(--surface, rgba(3,34,33,0.85));
}
.ai-header-title {
  font-family: var(--font-display, 'Syne', system-ui, sans-serif);
  font-size: var(--text-lg, 17px);
  font-weight: 600;
  margin-right: auto;
  display: flex; align-items: center; gap: 8px;
}
[dir="rtl"] .ai-header-title { margin-right: 0; margin-left: auto; }

.ai-header-title svg {
  width: 18px; height: 18px;
  color: var(--accent, #2CC295);
}

.ai-quota-badge {
  font-family: var(--font-mono, ui-monospace, monospace);
  font-size: var(--text-xs, 11px);
  padding: 4px 10px;
  border-radius: var(--r-pill, 9999px);
  background: var(--accent-soft, rgba(44,194,149,0.18));
  color: var(--accent, #2CC295);
  white-space: nowrap;
}
.ai-quota-badge.low  { background: rgba(245,158,11,0.18);  color: var(--warn, #f59e0b); }
.ai-quota-badge.zero { background: rgba(239,68,68,0.20);   color: var(--danger, #ef4444); }

.ai-close-btn {
  width: 32px; height: 32px;
  border-radius: var(--r-sm, 8px);
  border: none;
  background: transparent;
  color: var(--text-2, #AACBC4);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.ai-close-btn:hover { background: var(--card-hover, rgba(44,194,149,0.08)); color: var(--text-1); }
.ai-close-btn svg  { width: 18px; height: 18px; }

/* ── Conversation area ──────────────────────────────────────────────────── */
.ai-messages {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
  scroll-behavior: smooth;
}
.ai-messages::-webkit-scrollbar { width: 6px; }
.ai-messages::-webkit-scrollbar-thumb { background: var(--card-border); border-radius: 3px; }

.ai-msg {
  max-width: 88%;
  padding: 10px 14px;
  border-radius: var(--r-md, 12px);
  font-size: var(--text-base, 14px);
  line-height: 1.5;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.ai-msg-user {
  align-self: flex-end;
  background: var(--accent-soft, rgba(44,194,149,0.18));
  color: var(--text-1, #F1F7F6);
  border: 1px solid var(--border-acc, rgba(44,194,149,0.40));
  border-bottom-right-radius: 4px;
}
[dir="rtl"] .ai-msg-user {
  align-self: flex-start;
  border-bottom-right-radius: var(--r-md, 12px);
  border-bottom-left-radius: 4px;
}

.ai-msg-assistant {
  align-self: flex-start;
  background: var(--card, rgba(3,98,76,0.18));
  border: 1px solid var(--card-border, rgba(44,194,149,0.18));
  border-bottom-left-radius: 4px;
}
[dir="rtl"] .ai-msg-assistant {
  align-self: flex-end;
  border-bottom-left-radius: var(--r-md, 12px);
  border-bottom-right-radius: 4px;
}

.ai-msg-meta {
  font-size: var(--text-xs, 11px);
  color: var(--text-3, #5a8a7a);
  margin-top: 4px;
}

.ai-msg-flag {
  display: inline-block;
  font-size: var(--text-xs, 11px);
  margin-top: 6px;
  padding: 2px 8px;
  border-radius: var(--r-pill, 9999px);
  background: rgba(245,158,11,0.18);
  color: var(--warn, #f59e0b);
}

.ai-msg-error {
  align-self: stretch;
  background: rgba(239,68,68,0.10);
  border: 1px solid rgba(239,68,68,0.30);
  color: var(--danger, #ef4444);
  border-radius: var(--r-md, 12px);
  padding: 10px 14px;
  font-size: var(--text-sm, 12.5px);
}

/* "Thinking..." typing indicator */
.ai-typing {
  align-self: flex-start;
  display: inline-flex; gap: 4px;
  padding: 12px 14px;
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: var(--r-md);
}
.ai-typing span {
  width: 6px; height: 6px;
  background: var(--text-2);
  border-radius: 50%;
  animation: ai-typing-dot 1.2s infinite ease-in-out;
}
.ai-typing span:nth-child(2) { animation-delay: 0.15s; }
.ai-typing span:nth-child(3) { animation-delay: 0.30s; }
@keyframes ai-typing-dot {
  0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  30%           { opacity: 1;   transform: translateY(-3px); }
}

/* Empty-state hint */
.ai-empty {
  text-align: center;
  color: var(--text-3, #5a8a7a);
  font-size: var(--text-sm, 12.5px);
  padding: 32px 16px;
  line-height: 1.6;
}
.ai-empty-title {
  font-family: var(--font-display, 'Syne', system-ui, sans-serif);
  font-size: var(--text-md, 15px);
  color: var(--text-2, #AACBC4);
  margin-bottom: 6px;
}
.ai-empty-suggestions {
  margin-top: 12px;
  display: flex; flex-direction: column; gap: 6px;
}
.ai-suggestion {
  background: var(--card, rgba(3,98,76,0.18));
  border: 1px solid var(--card-border, rgba(44,194,149,0.18));
  color: var(--text-1, #F1F7F6);
  padding: 8px 12px;
  border-radius: var(--r-sm, 8px);
  font-size: var(--text-sm, 12.5px);
  cursor: pointer;
  text-align: start;
  width: 100%;
  font-family: inherit;
}
.ai-suggestion:hover { background: var(--card-hover, rgba(44,194,149,0.08)); border-color: var(--border-acc); }

/* ── Composer ───────────────────────────────────────────────────────────── */
.ai-composer {
  flex: none;
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--card-border, rgba(44,194,149,0.18));
  background: var(--surface, rgba(3,34,33,0.85));
}
.ai-phi-warning {
  font-size: var(--text-xs, 11px);
  color: var(--text-3, #5a8a7a);
  margin-bottom: 8px;
  display: flex; align-items: center; gap: 6px;
}
.ai-phi-warning svg { width: 12px; height: 12px; color: var(--warn, #f59e0b); flex: none; }

.ai-composer-row {
  display: flex; gap: 8px; align-items: flex-end;
}
.ai-input {
  flex: 1 1 auto;
  background: var(--field-bg, rgba(0,0,0,0.20));
  border: 1px solid var(--card-border, rgba(44,194,149,0.18));
  border-radius: var(--r-sm, 8px);
  color: var(--text-1, #F1F7F6);
  font-family: inherit;
  font-size: var(--text-base, 14px);
  padding: 10px 12px;
  resize: none;
  min-height: 40px;
  max-height: 120px;
  line-height: 1.4;
}
.ai-input:focus { outline: none; border-color: var(--accent, #2CC295); background: var(--field-bg, rgba(0,0,0,0.30)); }
.ai-input::placeholder { color: var(--text-3, #5a8a7a); }
.theme-light .ai-input { background: rgba(255,255,255,0.6); }

.ai-send-btn {
  flex: none;
  width: 40px; height: 40px;
  border-radius: var(--r-sm, 8px);
  border: none;
  background: var(--accent, #2CC295);
  color: var(--green-dark, #032221);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 160ms ease;
}
.ai-send-btn:hover:not(:disabled)  { background: var(--accent2, #00DBF1); }
.ai-send-btn:disabled              { opacity: 0.4; cursor: not-allowed; }
.ai-send-btn svg                    { width: 18px; height: 18px; }
[dir="rtl"] .ai-send-btn svg        { transform: scaleX(-1); }

/* ── Consent modal ──────────────────────────────────────────────────────── */
.ai-consent {
  position: fixed; inset: 0;
  background: rgba(2,12,9,0.70);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  padding-top:    calc(24px + env(safe-area-inset-top, 0px));
  padding-bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  opacity: 0; visibility: hidden;
  transition: opacity 200ms ease, visibility 0s linear 200ms;
}
.ai-consent.open {
  opacity: 1; visibility: visible;
  transition: opacity 200ms ease, visibility 0s linear 0s;
}
.theme-light .ai-consent { background: rgba(10,46,36,0.50); }

.ai-consent-card {
  width: 100%; max-width: 480px;
  max-height: 85vh; overflow-y: auto;
  background: var(--bg-2, #04241d);
  border: 1px solid var(--card-border, rgba(44,194,149,0.18));
  border-radius: var(--r-lg, 16px);
  padding: 28px;
  box-shadow: var(--shadow-pop, 0 12px 40px rgba(0,0,0,0.40));
  color: var(--text-1, #F1F7F6);
}
.ai-consent-title {
  font-family: var(--font-display, 'Syne', system-ui, sans-serif);
  font-size: var(--text-xl, 20px);
  margin-bottom: 6px;
  color: var(--accent, #2CC295);
  display: flex; align-items: center; gap: 10px;
}
.ai-consent-title svg { width: 22px; height: 22px; }
.ai-consent-subtitle {
  color: var(--text-2, #AACBC4);
  font-size: var(--text-sm, 12.5px);
  margin-bottom: 18px;
  line-height: 1.5;
}
.ai-consent-list {
  list-style: none;
  padding: 0;
  margin: 0 0 22px 0;
  display: flex; flex-direction: column; gap: 10px;
}
.ai-consent-list li {
  font-size: var(--text-sm, 12.5px);
  line-height: 1.55;
  padding-inline-start: 22px;
  position: relative;
  color: var(--text-1, #F1F7F6);
}
.ai-consent-list li::before {
  content: "•";
  position: absolute;
  inset-inline-start: 6px;
  color: var(--accent, #2CC295);
  font-size: 16px;
  line-height: 1;
  top: 4px;
}
.ai-consent-actions {
  display: flex; gap: 10px; justify-content: flex-end;
}
.ai-consent-btn {
  border: 1px solid var(--card-border, rgba(44,194,149,0.18));
  background: transparent;
  color: var(--text-1, #F1F7F6);
  padding: 10px 18px;
  border-radius: var(--r-sm, 8px);
  cursor: pointer;
  font-family: inherit;
  font-size: var(--text-sm, 12.5px);
  font-weight: 500;
}
.ai-consent-btn:hover { background: var(--card-hover, rgba(44,194,149,0.08)); }
.ai-consent-btn-primary {
  background: var(--accent, #2CC295);
  color: var(--green-dark, #032221);
  border-color: var(--accent, #2CC295);
}
.ai-consent-btn-primary:hover { background: var(--accent2, #00DBF1); }

/* ── Light theme overrides ──────────────────────────────────────────────────
   main.css defines --bg, --surface, --card etc. for both themes, but not
   --bg-2 — our drawer used it as a fallback to a dark hex which means
   the drawer stayed dark when the SPA flipped to light. Explicit
   .theme-light .ai-* rules below give the drawer + composer + message
   bubbles + consent card a proper light skin. */
.theme-light .ai-drawer {
  background: #ffffff;
  color: #0a2e24;
  border-left-color: rgba(3,98,76,0.18);
}
.theme-light [dir="rtl"] .ai-drawer,
[dir="rtl"].theme-light .ai-drawer {
  border-right-color: rgba(3,98,76,0.18);
}
.theme-light .ai-header,
.theme-light .ai-composer {
  background: rgba(238,245,242,0.92);
  border-color: rgba(3,98,76,0.18);
}
.theme-light .ai-input {
  background: #ffffff;
  color: #0a2e24;
  border-color: rgba(3,98,76,0.18);
}
.theme-light .ai-input::placeholder { color: #7a9c8e; }
.theme-light .ai-input:focus { border-color: #03624C; }
.theme-light .ai-close-btn { color: #355c4d; }
.theme-light .ai-close-btn:hover { background: rgba(3,98,76,0.06); color: #0a2e24; }
.theme-light .ai-msg-assistant {
  background: rgba(255,255,255,0.92);
  color: #0a2e24;
  border-color: rgba(3,98,76,0.18);
}
.theme-light .ai-msg-user {
  background: rgba(44,194,149,0.15);
  color: #0a2e24;
  border-color: rgba(44,194,149,0.50);
}
.theme-light .ai-msg-error {
  background: rgba(239,68,68,0.08);
  color: #b91c1c;
  border-color: rgba(239,68,68,0.30);
}
.theme-light .ai-typing { background: rgba(255,255,255,0.92); border-color: rgba(3,98,76,0.18); }
.theme-light .ai-typing span { background: #355c4d; }
.theme-light .ai-suggestion {
  background: rgba(255,255,255,0.85);
  color: #0a2e24;
  border-color: rgba(3,98,76,0.18);
}
.theme-light .ai-suggestion:hover { background: rgba(3,98,76,0.06); border-color: #03624C; }
.theme-light .ai-empty-title { color: #355c4d; }
.theme-light .ai-empty { color: #7a9c8e; }
.theme-light .ai-phi-warning { color: #7a9c8e; }
.theme-light .ai-consent-card {
  background: #ffffff;
  color: #0a2e24;
  border-color: rgba(3,98,76,0.18);
}
.theme-light .ai-consent-title    { color: #03624C; }
.theme-light .ai-consent-subtitle { color: #355c4d; }
.theme-light .ai-consent-list li::before { color: #03624C; }
.theme-light .ai-consent-btn  {
  border-color: rgba(3,98,76,0.20); color: #0a2e24;
}
.theme-light .ai-consent-btn:hover { background: rgba(3,98,76,0.06); }
.theme-light .ai-consent-btn-primary {
  background: #03624C; color: #ffffff; border-color: #03624C;
}
.theme-light .ai-consent-btn-primary:hover { background: #2CC295; color: #032221; }

/* ── Mobile tweaks ──────────────────────────────────────────────────────── */
@media (max-width: 480px) {
  .ai-drawer       { max-width: 100%; }
  .ai-bubble {
    bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    right:  calc(16px + env(safe-area-inset-right,  0px));
    width: 52px; height: 52px;
  }
  [dir="rtl"] .ai-bubble {
    left: calc(16px + env(safe-area-inset-left, 0px));
  }
  .ai-consent-card { padding: 22px; }
}
