/* ======================================================
   medgraph Health Navigator - Stylesheet
   WCAG 2.2 AA compliant, bilingual, dark/high-contrast
   ====================================================== */

/* --- Reset & base --- */

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

/* --- Theme tokens (light — default) --- */

:root {
  --bg: #f5f7fa;
  --surface: #ffffff;
  --text: #1a1a2e;
  --text-secondary: #46465e;
  --accent: #4f6ef7;
  --accent-hover: #3b58d6;
  --accent-light: #e8ecff;
  --user-bubble: #4f6ef7;
  --user-text: #ffffff;
  --assistant-bubble: #ffffff;
  --assistant-text: #1a1a2e;
  --safety-bg: #fff3e0;
  --safety-border: #e65100;
  --safety-text: #b71c1c;
  --border: #d0d4dc;
  --focus-ring: #4f6ef7;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.08);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
    Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
  --mono: "SF Mono", SFMono-Regular, Consolas, "Liberation Mono", Menlo,
    monospace;
  --mic-active: #d32f2f;
  --mic-active-ring: rgba(211, 47, 47, 0.35);
  --toolbar-bg: transparent;
  --toolbar-border: var(--border);
  --toolbar-text: var(--text-secondary);
}

/* --- Dark theme --- */

[data-theme="dark"] {
  --bg: #121220;
  --surface: #1e1e32;
  --text: #e8e8f0;
  --text-secondary: #a8a8c0;
  --accent: #7b96ff;
  --accent-hover: #96adff;
  --accent-light: #1c2248;
  --user-bubble: #4f6ef7;
  --user-text: #ffffff;
  --assistant-bubble: #1e1e32;
  --assistant-text: #e8e8f0;
  --safety-bg: #3e2200;
  --safety-border: #ff9800;
  --safety-text: #ffcc80;
  --border: #2e2e48;
  --focus-ring: #7b96ff;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.4);
  --mic-active: #ef5350;
  --mic-active-ring: rgba(239, 83, 80, 0.4);
  --toolbar-text: #a8a8c0;
}

/* --- High contrast (light) --- */

[data-contrast="high"] {
  --bg: #ffffff;
  --surface: #ffffff;
  --text: #000000;
  --text-secondary: #222222;
  --accent: #0033cc;
  --accent-hover: #002299;
  --accent-light: #cce0ff;
  --user-bubble: #0033cc;
  --user-text: #ffffff;
  --assistant-bubble: #ffffff;
  --assistant-text: #000000;
  --safety-bg: #fff0f0;
  --safety-border: #cc0000;
  --safety-text: #990000;
  --border: #000000;
  --focus-ring: #0033cc;
  --mic-active: #cc0000;
}

/* --- High contrast + dark --- */

[data-theme="dark"][data-contrast="high"] {
  --bg: #000000;
  --surface: #0a0a0a;
  --text: #ffffff;
  --text-secondary: #dddddd;
  --accent: #80aaff;
  --accent-hover: #99bbff;
  --accent-light: #0a1530;
  --user-bubble: #3366ff;
  --user-text: #ffffff;
  --assistant-bubble: #0a0a0a;
  --assistant-text: #ffffff;
  --safety-bg: #330000;
  --safety-border: #ff4444;
  --safety-text: #ffaaaa;
  --border: #ffffff;
  --focus-ring: #80aaff;
  --mic-active: #ff4444;
}

/* --- Base typography (scalable) --- */

html {
  font-size: 100%; /* Respects user browser settings */
}

body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 0.9375rem; /* 15px at default, scales with user pref */
  line-height: 1.55;
}

/* --- Skip link --- */

.skip-link {
  position: absolute;
  top: -100%;
  left: 1rem;
  z-index: 10000;
  padding: 0.75rem 1.5rem;
  background: var(--accent);
  color: #ffffff;
  font-weight: 700;
  font-size: 1rem;
  text-decoration: none;
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
  transition: top 0.15s;
}

.skip-link:focus {
  top: 0;
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

/* --- Screen reader only (visually hidden) --- */

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* --- Global focus indicator --- */

:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

/* Remove browser default outline only when :focus-visible is supported */
:focus:not(:focus-visible) {
  outline: none;
}

/* --- App shell --- */

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

/* --- Header --- */

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.125rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.header-content {
  min-width: 0;
}

.header h1 {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.header-accent {
  color: var(--accent);
}

.header-subtitle {
  font-size: 0.82rem;
  color: var(--text-secondary);
  margin-top: 0.125rem;
}

/* --- Header controls (toolbar) --- */

.header-controls {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  flex-shrink: 0;
}

.toolbar-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2.75rem; /* 44px */
  min-height: 2.75rem; /* 44px */
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--toolbar-text);
  font-family: var(--font);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.toolbar-btn:hover {
  border-color: var(--accent);
  background: var(--accent-light);
  color: var(--accent);
}

.toolbar-btn:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.toolbar-btn[aria-pressed="true"] {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
}

.toolbar-icon {
  display: block;
}

/* Theme toggle icon switching */
.toolbar-icon--moon {
  display: none;
}

[data-theme="dark"] .toolbar-icon--sun {
  display: none;
}

[data-theme="dark"] .toolbar-icon--moon {
  display: block;
}

/* --- Patient bar --- */

.patient-bar {
  padding: 0.75rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}

.patient-bar-label {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
  margin-bottom: 0.5rem;
}

/* --- Patient info button & tooltip --- */

.patient-bar-label {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  position: relative;
}

.info-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.25rem;
  height: 1.25rem;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: none;
  color: var(--text-secondary);
  cursor: pointer;
  opacity: 0.6;
  transition: opacity 0.15s, color 0.15s;
}

.info-btn:hover,
.info-btn:focus-visible {
  opacity: 1;
  color: var(--accent);
}

.info-tooltip {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 0.375rem;
  padding: 0.5rem 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  font-size: 0.78rem;
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text-secondary);
  line-height: 1.45;
  max-width: 280px;
  z-index: 10;
  white-space: normal;
}

.info-tooltip.visible {
  display: block;
  animation: fadeSlideIn 0.15s ease-out;
}

.patient-gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
}

.patient-btn {
  min-height: 2.75rem; /* 44px touch target */
  padding: 0.5rem 0.75rem;
  border: 2px solid var(--border);
  border-radius: 1.25rem;
  background: var(--surface);
  font-family: var(--font);
  font-size: 0.8rem;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  color: var(--text);
}

.patient-btn:hover {
  border-color: var(--accent);
  background: var(--accent-light);
}

.patient-btn:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.patient-btn[aria-pressed="true"] {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
}

.patient-btn-meta {
  font-size: 0.75rem;
  opacity: 0.75;
}

.gallery-error {
  color: var(--safety-text);
  font-size: 0.8rem;
  role: alert;
}

/* --- Patient card --- */

.patient-card {
  padding: 0.75rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--accent-light);
  flex-shrink: 0;
  animation: fadeSlideIn 0.2s ease-out;
}

.patient-card-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.25rem;
}

.patient-card-name {
  font-weight: 700;
  font-size: 0.95rem;
}

.patient-card-demo {
  font-size: 0.8rem;
  color: var(--text-secondary);
  background: var(--surface);
  padding: 0.0625rem 0.5rem;
  border-radius: 0.625rem;
  border: 1px solid var(--border);
}

.patient-card-clear {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2.75rem; /* 44px touch target */
  min-height: 2.75rem;
  background: none;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-size: 1.4rem;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 0;
  line-height: 1;
  transition: color 0.15s, border-color 0.15s;
}

.patient-card-clear:hover {
  color: var(--text);
  border-color: var(--border);
}

.patient-card-clear:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.patient-card-headline {
  font-size: 0.82rem;
  color: var(--text-secondary);
  margin-bottom: 0.375rem;
}

.patient-card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.condition-tag {
  font-size: 0.75rem;
  padding: 0.125rem 0.5rem;
  border-radius: 0.625rem;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
}

.condition-tag-more {
  font-style: italic;
}

/* --- Chat container --- */

.chat-container {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* --- Welcome message --- */

.welcome-message {
  text-align: center;
  padding: 2rem 1.25rem;
  color: var(--text-secondary);
  font-size: 0.92rem;
  line-height: 1.6;
}

.welcome-message strong {
  color: var(--text);
}

.welcome-disclaimer {
  margin-top: 0.75rem;
  font-size: 0.82rem;
  color: var(--safety-text);
  background: var(--safety-bg);
  border: 1px solid var(--safety-border);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.875rem;
  display: inline-block;
}

/* --- Message bubbles --- */

.message {
  max-width: 85%;
  animation: fadeSlideIn 0.2s ease-out;
}

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

.message-user {
  align-self: flex-end;
}

.message-assistant {
  align-self: flex-start;
}

.message-bubble {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  line-height: 1.55;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message-user .message-bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  border-bottom-right-radius: 4px;
}

.message-assistant .message-bubble {
  background: var(--assistant-bubble);
  color: var(--assistant-text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow);
}

.message-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
  margin-bottom: 0.25rem;
}

.message-user .message-label {
  text-align: right;
}

/* --- Markdown content inside bubbles --- */

.message-bubble p {
  margin-bottom: 0.5em;
}

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

.message-bubble ul,
.message-bubble ol {
  margin: 0.4em 0;
  padding-left: 1.4em;
}

.message-bubble li {
  margin-bottom: 0.25em;
}

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

.message-bubble code {
  font-family: var(--mono);
  font-size: 0.9em;
  background: var(--bg);
  padding: 0.125em 0.375em;
  border-radius: 4px;
}

.message-bubble pre {
  background: var(--bg);
  padding: 0.75rem;
  border-radius: var(--radius-sm);
  overflow-x: auto;
}

.message-bubble pre code {
  background: none;
  padding: 0;
}

/* --- Pipeline status --- */

.pipeline-status {
  align-self: flex-start;
  max-width: 85%;
  animation: fadeSlideIn 0.2s ease-out;
}

.pipeline-card {
  background: var(--accent-light);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.75rem 1rem;
  font-size: 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.pipeline-title {
  font-weight: 700;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--accent);
}

.pipeline-row {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  gap: 0.375rem;
}

.agent-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3125rem;
  padding: 0.25rem 0.625rem;
  min-height: 1.75rem;
  border-radius: 1.25rem;
  font-size: 0.78rem;
  font-weight: 600;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
}

.agent-badge.processing {
  border-color: var(--accent);
  color: var(--accent);
}

.agent-badge.done {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
}

.agent-badge-icon {
  font-size: 0.7rem;
}

.pipeline-reasoning {
  font-size: 0.82rem;
  color: var(--text-secondary);
  font-style: italic;
  background: var(--surface);
  border-left: 3px solid var(--accent);
  padding: 0.5rem 0.75rem;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  display: none;
}

.pipeline-reasoning.visible {
  display: block;
}

/* --- Badge timer --- */

.badge-timer {
  font-family: var(--mono);
  font-size: 0.7rem;
  opacity: 0.7;
  margin-left: 0.25rem;
}

/* --- Local / cloud color modifiers --- */

.agent-badge.local.processing {
  border-color: #2e7d32;
  color: #2e7d32;
}

.agent-badge.local.processing .spinner {
  border-color: rgba(46, 125, 50, 0.25);
  border-top-color: #2e7d32;
}

.agent-badge.local.done {
  background: #2e7d32;
  color: #ffffff;
  border-color: #2e7d32;
}

[data-theme="dark"] .agent-badge.local.processing {
  border-color: #66bb6a;
  color: #66bb6a;
}

[data-theme="dark"] .agent-badge.local.processing .spinner {
  border-color: rgba(102, 187, 106, 0.25);
  border-top-color: #66bb6a;
}

[data-theme="dark"] .agent-badge.local.done {
  background: #66bb6a;
  color: #000000;
  border-color: #66bb6a;
}

/* --- Spinner --- */

.spinner {
  display: inline-block;
  width: 0.75rem;
  height: 0.75rem;
  border: 2px solid var(--accent-light);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* --- Safety disclaimer --- */

.safety-disclaimer {
  background: var(--safety-bg);
  border: 1px solid var(--safety-border);
  border-radius: var(--radius-sm);
  padding: 0.625rem 0.875rem;
  margin-top: 0.625rem;
  font-size: 0.84rem;
  color: var(--safety-text);
  line-height: 1.5;
}

/* --- Input area --- */

.input-area {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.input-form {
  display: flex;
  gap: 0.625rem;
  align-items: center;
}

.message-input {
  flex: 1;
  min-height: 2.75rem; /* 44px touch target */
  padding: 0.75rem 1rem;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  transition: border-color 0.2s, box-shadow 0.2s;
}

.message-input::placeholder {
  color: var(--text-secondary);
}

.message-input:focus-visible {
  border-color: var(--focus-ring);
  box-shadow: 0 0 0 3px rgba(79, 110, 247, 0.18);
  outline: none;
}

.message-input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* --- Mic button --- */

.mic-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2.75rem; /* 44px touch target */
  min-height: 2.75rem;
  padding: 0.5rem;
  border: 2px solid var(--border);
  border-radius: 50%;
  background: var(--surface);
  color: var(--text-secondary);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, color 0.15s, box-shadow 0.15s;
  flex-shrink: 0;
  position: relative;
}

.mic-btn:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
}

.mic-btn:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.mic-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.mic-btn.listening {
  border-color: var(--mic-active);
  background: var(--mic-active);
  color: #ffffff;
  box-shadow: 0 0 0 4px var(--mic-active-ring);
  animation: micPulse 1.5s ease-in-out infinite;
}

@keyframes micPulse {
  0%, 100% {
    box-shadow: 0 0 0 4px var(--mic-active-ring);
  }
  50% {
    box-shadow: 0 0 0 8px var(--mic-active-ring);
  }
}

.mic-listening-label {
  /* Managed by sr-only, visible only to screen readers when listening */
}

/* --- Send button --- */

.send-btn {
  min-height: 2.75rem; /* 44px touch target */
  padding: 0.75rem 1.5rem;
  background: var(--accent);
  color: #ffffff;
  border: 2px solid transparent;
  border-radius: var(--radius);
  font-size: 0.92rem;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}

.send-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

.send-btn:active:not(:disabled) {
  transform: scale(0.97);
}

.send-btn:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Dark mode: darken button fill for 4.5:1 contrast with white text */
[data-theme="dark"] .send-btn {
  background: #3a5cd0;
}

[data-theme="dark"] .send-btn:hover:not(:disabled) {
  background: #4a6ce0;
}

/* --- Model indicator --- */

.model-indicator {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.25rem 0.625rem;
  min-height: 1.75rem;
  border-radius: 1.25rem;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  transition: border-color 0.2s, color 0.2s;
}

.model-indicator-dot {
  display: inline-block;
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 50%;
  background: var(--text-secondary);
  transition: background 0.2s;
}

.model-indicator.local {
  border-color: #2e7d32;
  color: #2e7d32;
}

.model-indicator.local .model-indicator-dot {
  background: #2e7d32;
}

.model-indicator.cloud {
  border-color: var(--accent);
  color: var(--accent);
}

.model-indicator.cloud .model-indicator-dot {
  background: var(--accent);
}

[data-theme="dark"] .model-indicator.local {
  border-color: #66bb6a;
  color: #66bb6a;
}

[data-theme="dark"] .model-indicator.local .model-indicator-dot {
  background: #66bb6a;
}

/* ======================================================
   Reduced motion
   ====================================================== */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .spinner {
    animation: none;
    border-color: var(--accent-light);
    border-top-color: var(--accent);
    opacity: 0.7;
  }

  .mic-btn.listening {
    animation: none;
    box-shadow: 0 0 0 4px var(--mic-active-ring);
  }

  .skip-link {
    transition: none;
  }
}

/* ======================================================
   Responsive
   ====================================================== */

@media (max-width: 600px) {
  .app {
    max-width: 100%;
    box-shadow: none;
  }

  .header {
    padding: 0.875rem 1rem;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .header-controls {
    gap: 0.25rem;
  }

  .toolbar-btn {
    min-width: 2.5rem;
    min-height: 2.5rem;
  }

  .patient-bar {
    padding: 0.75rem 1rem;
  }

  .patient-card {
    padding: 0.75rem 1rem;
  }

  .chat-container {
    padding: 1rem;
  }

  .input-area {
    padding: 0.75rem 1rem;
  }

  .message {
    max-width: 92%;
  }

  .send-btn {
    padding: 0.75rem 1rem;
  }

  .input-form {
    gap: 0.375rem;
  }
}

/* ======================================================
   Scrollbar styling
   ====================================================== */

.chat-container::-webkit-scrollbar {
  width: 6px;
}

.chat-container::-webkit-scrollbar-track {
  background: transparent;
}

.chat-container::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.chat-container::-webkit-scrollbar-thumb:hover {
  background: var(--text-secondary);
}
