:root {
  --brand: #0f7a4d;
  --brand-dark: #0a5b39;
  --brand-soft: #e6f4ed;
  --bg: #f6f7f8;
  --bubble-user: #0f7a4d;
  --bubble-user-text: #ffffff;
  --bubble-assistant: #ffffff;
  --bubble-assistant-text: #1a1a1a;
  --border: #e4e6ea;
  --muted: #6b7280;
  --danger: #d94040;
  --radius: 18px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  font-size: 16px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: #1a1a1a;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 760px;
  margin: 0 auto;
  background: var(--bg);
  box-shadow: 0 0 40px rgba(0,0,0,0.04);
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(var(--safe-top) + 12px) 16px 12px;
  background: rgba(255,255,255,0.92);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.avatar {
  width: 38px;
  height: 38px;
  border-radius: 9px;
  background: var(--brand);
  color: white;
  display: grid;
  place-items: center;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.02em;
  flex-shrink: 0;
}
.header-text { flex: 1; line-height: 1.15; }
.title { font-weight: 600; font-size: 15px; }
.subtitle { color: var(--muted); font-size: 12px; margin-top: 2px; }
.delegate-picker {
  background: white;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 10px;
  font: inherit;
  font-size: 13px;
  color: #1a1a1a;
  cursor: pointer;
  max-width: 150px;
}
.delegate-picker:focus { outline: none; border-color: var(--brand); }

.icon-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 22px;
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 8px;
}
.icon-btn:active { background: var(--border); }

/* Messages */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 16px 8px;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

.welcome {
  text-align: center;
  padding: 32px 20px 16px;
  color: var(--muted);
}
.welcome-logo {
  display: inline-grid;
  place-items: center;
  width: 56px;
  height: 56px;
  border-radius: 14px;
  background: var(--brand);
  color: white;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.02em;
  margin-bottom: 12px;
}
.welcome h1 {
  color: #1a1a1a;
  font-size: 22px;
  margin: 0 0 6px;
  font-weight: 600;
}
.welcome p {
  font-size: 14px;
  margin: 0;
  max-width: 300px;
  margin-inline: auto;
}

/* Bubbles */
.msg {
  display: flex;
  margin: 8px 0;
  animation: slideIn 0.22s ease;
}
@keyframes slideIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
.msg.user { justify-content: flex-end; }
.bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.45;
  font-size: 15px;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg.user .bubble {
  background: var(--bubble-user);
  color: var(--bubble-user-text);
  border-bottom-right-radius: 6px;
}
.msg-widgets {
  margin-bottom: 8px;
}

.msg.assistant .bubble {
  background: var(--bubble-assistant);
  color: var(--bubble-assistant-text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}
.msg.error .bubble {
  background: #fff2f2;
  border: 1px solid #f3c9c9;
  color: var(--danger);
}

/* Markdown content inside bubbles */
.bubble > *:first-child { margin-top: 0; }
.bubble > *:last-child { margin-bottom: 0; }
.bubble h1, .bubble h2, .bubble h3 {
  font-size: 16px;
  margin: 10px 0 6px;
  font-weight: 600;
}
.bubble h1 { font-size: 17px; }
.bubble p { margin: 8px 0; }
.bubble ul, .bubble ol { margin: 6px 0; padding-left: 22px; }
.bubble li { margin: 2px 0; }
.bubble strong { font-weight: 600; }
.bubble em { font-style: italic; }
.bubble code {
  background: rgba(0,0,0,0.06);
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 0.85em;
}
.bubble hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 10px 0;
}
.bubble blockquote {
  border-left: 3px solid var(--brand);
  padding: 2px 10px;
  margin: 8px 0;
  color: var(--muted);
  font-style: italic;
}

.msg.user .bubble a { color: #e6f4ed; text-decoration: underline; }
.msg.assistant .bubble a { color: var(--brand); text-decoration: none; }

/* Typing indicator */
.typing {
  display: inline-flex;
  gap: 4px;
  padding: 12px 16px;
}
.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  opacity: 0.4;
  animation: typing 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.3; }
  30% { transform: translateY(-4px); opacity: 1; }
}

.status-line {
  font-size: 12px;
  color: var(--muted);
  padding: 4px 14px 0;
  display: flex;
  align-items: center;
  gap: 6px;
}
.status-line::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--brand);
  animation: pulse 1.4s infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 0.4; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.3); }
}

/* Quick actions */
.quick-actions {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 6px 12px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  border-top: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}
.quick-actions::-webkit-scrollbar { display: none; }
.quick-actions.hidden { display: none; }
.chip {
  flex-shrink: 0;
  background: white;
  border: 1px solid var(--border);
  color: #1a1a1a;
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, transform 0.1s;
}
.chip:hover { background: var(--brand-soft); border-color: var(--brand); color: var(--brand-dark); }
.chip:active { transform: scale(0.97); }

/* Composer */
.composer {
  display: flex;
  gap: 8px;
  padding: 8px 12px calc(var(--safe-bottom) + 10px);
  background: rgba(255,255,255,0.96);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-top: 1px solid var(--border);
  align-items: flex-end;
}
#input {
  flex: 1;
  border: 1px solid var(--border);
  background: white;
  border-radius: 22px;
  padding: 10px 16px;
  font: inherit;
  font-size: 16px;  /* iOS: prevents zoom on focus */
  outline: none;
  min-height: 22px;
  line-height: 1.3;
}
#input:focus { border-color: var(--brand); }
#micBtn {
  background: white;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: color 0.15s, transform 0.1s;
}
#micBtn:hover { color: var(--brand); }
#micBtn:active { transform: scale(0.93); }
#micBtn.recording { color: var(--danger); border-color: var(--danger); animation: micPulse 1.4s infinite; }
@keyframes micPulse { 0%,100%{box-shadow:0 0 0 0 rgba(217,64,64,.5)} 50%{box-shadow:0 0 0 8px rgba(217,64,64,0)} }

#sendBtn {
  background: var(--brand);
  color: white;
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s, transform 0.1s;
}
#sendBtn:hover { background: var(--brand-dark); }
#sendBtn:active { transform: scale(0.93); }
#sendBtn:disabled {
  background: #cbd0d6;
  cursor: not-allowed;
}

/* Voice recorder overlay */
[hidden] { display: none !important; }
#recorder-overlay {
  position: fixed; inset: 0; background: rgba(17,24,39,0.55);
  backdrop-filter: blur(4px); z-index: 1100;
  display: grid; place-items: center;
  animation: fadeIn 0.18s ease;
}
.recorder-card {
  background: white; border-radius: 16px; padding: 22px;
  width: min(360px, 90vw); text-align: center;
  box-shadow: 0 30px 90px rgba(0,0,0,0.4);
}
.recorder-mode {
  display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px;
  text-align: left;
}
.recorder-mode-opt {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}
.recorder-mode-opt:hover { background: #f6f7f8; }
.recorder-mode-opt input[type=radio] { margin-top: 2px; accent-color: var(--brand); }
.recorder-mode-opt:has(input:checked) { border-color: var(--brand); background: var(--brand-soft); }
.recorder-mode-opt small { color: var(--muted); font-size: 12px; }
.recorder-status { font-weight: 600; font-size: 15px; }
.recorder-pulse {
  width: 80px; height: 80px; border-radius: 50%;
  background: var(--brand-soft); margin: 16px auto;
  display: grid; place-items: center;
  color: var(--brand);
}
.recorder-pulse::before {
  content: '';
  width: 32px;
  height: 32px;
  background: currentColor;
  -webkit-mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M12 14a3 3 0 0 0 3-3V5a3 3 0 0 0-6 0v6a3 3 0 0 0 3 3zm5-3a5 5 0 0 1-10 0H5a7 7 0 0 0 6 6.92V21h2v-3.08A7 7 0 0 0 19 11h-2z'/></svg>") center / contain no-repeat;
          mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M12 14a3 3 0 0 0 3-3V5a3 3 0 0 0-6 0v6a3 3 0 0 0 3 3zm5-3a5 5 0 0 1-10 0H5a7 7 0 0 0 6 6.92V21h2v-3.08A7 7 0 0 0 19 11h-2z'/></svg>") center / contain no-repeat;
}
.recorder-pulse.recording { background: #ffeded; color: var(--danger); animation: micPulse 1.4s infinite; }
.recorder-time { font-variant-numeric: tabular-nums; color: var(--muted); margin-bottom: 14px; }
.recorder-actions { display: flex; gap: 8px; justify-content: center; margin-bottom: 12px; }
.recorder-actions button {
  background: white; color: #1a1a1a;
  border: 1px solid var(--border);
  padding: 9px 16px; border-radius: 999px;
  cursor: pointer; font-size: 14px;
}
.recorder-actions button.primary { background: var(--brand); color: white; border-color: var(--brand); }
.recorder-actions button:active { transform: scale(0.97); }
.recorder-pharmacy {
  display: flex; gap: 8px; align-items: center; justify-content: center;
  font-size: 13px; color: var(--muted); flex-wrap: wrap;
}
.recorder-pharmacy label { color: #1a1a1a; font-weight: 500; }
.recorder-pharmacy select {
  min-width: 200px; max-width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  font-size: 14px;
  background: white;
  cursor: pointer;
}
.recorder-pharmacy select:focus { outline: none; border-color: var(--brand); }

/* Visit draft modal */
#draft-overlay {
  position: fixed; inset: 0; background: rgba(17,24,39,0.55);
  backdrop-filter: blur(4px); z-index: 1100;
  display: grid; place-items: center;
}
.draft-card {
  background: white; border-radius: 16px;
  width: min(560px, 92vw); max-height: 86vh;
  display: flex; flex-direction: column; overflow: hidden;
  box-shadow: 0 30px 90px rgba(0,0,0,0.4);
}
.draft-card header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
  font-weight: 600;
}
.draft-card header button { background: none; border: none; font-size: 24px; cursor: pointer; color: var(--muted); }
#draftBody { padding: 18px; overflow-y: auto; font-size: 14px; line-height: 1.5; }
#draftBody h3 { font-size: 14px; margin: 14px 0 6px; color: var(--brand-dark); }
#draftBody h3:first-child { margin-top: 0; }
#draftBody ul { margin: 4px 0; padding-left: 22px; }
#draftBody .draft-tag { display: inline-block; padding: 2px 8px; border-radius: 999px; background: var(--brand-soft); color: var(--brand-dark); font-size: 12px; margin-right: 4px; }
#draftBody .draft-actions { padding: 14px 0 4px; display: flex; gap: 8px; justify-content: flex-end; }
#draftBody .draft-actions button {
  padding: 8px 14px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border); background: white; font-size: 14px;
}
#draftBody .draft-actions button.primary {
  background: var(--brand); color: white; border-color: var(--brand);
}

/* Desktop refinements */
@media (min-width: 768px) {
  body { background: #ecedef; }
  .app {
    margin-top: 20px;
    margin-bottom: 20px;
    height: calc(100vh - 40px);
    height: calc(100dvh - 40px);
    border-radius: 14px;
    overflow: hidden;
  }
}
