/* ============ DIALOGUE WRAPPER ============ */
.dialogue-wrapper {
  display: grid;
  grid-template-areas:
    "scenario scenario"
    "messages choices"
    "footer   footer";
  grid-template-rows: auto 1fr auto;
  grid-template-columns: 1fr 450px;
  height: calc(100vh - 125px);
  min-height: 560px;
  background: var(--c-card);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-md);
  border: 1px solid rgba(15,23,42,.06);
  overflow: hidden;
  position: relative;
}
/* Gradient top bar — matches new brand */
.dialogue-wrapper::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 4px;
  background: var(--grad-text);
  z-index: 2;
}

/* ============ SCENARIO BANNER ============ */
.dialogue-scenario {
  grid-area: scenario;
  padding: var(--sp-4) var(--sp-6);
  background: linear-gradient(135deg, var(--c-primary-pale) 0%, var(--c-accent-pale) 100%);
  border-bottom: 1px solid rgba(37,99,235,.12);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-3);
  text-align: center;
}
.dialogue-scenario__icon {
  font-size: 1.5rem;
  flex-shrink: 0;
}
.dialogue-scenario__text {
  font-size: var(--fs-md);
  color: var(--c-primary);
  font-style: italic;
  line-height: 1.5;
  font-weight: 700;
}

/* ============ MESSAGES AREA ============ */
.dialogue-messages {
  grid-area: messages;
  min-height: 0;
  overflow-y: auto;
  padding: var(--sp-5) var(--sp-5);
  display: flex;
  flex-direction: column;
  gap: var(--sp-5);
  scrollbar-width: thin;
  scrollbar-color: var(--c-border) transparent;
  background: var(--c-bg);
}
.dialogue-messages::-webkit-scrollbar { width: 4px; }
.dialogue-messages::-webkit-scrollbar-thumb {
  background: var(--c-border);
  border-radius: var(--r-full);
}

/* ============ MESSAGE BUBBLES ============ */
.msg {
  display: flex;
  gap: var(--sp-4);
  max-width: 82%;
  animation: msgIn var(--t-base) ease;
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(12px); }
  to   { opacity: 1; transform: translateY(0); }
}
.msg-ai   { align-self: flex-start; }
.msg-user { align-self: flex-end; flex-direction: row-reverse; }

.msg__avatar {
  width: 52px; height: 52px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
}
.msg-ai .msg__avatar {
  background: var(--grad-card);
  color: #fff;
}
.msg-user .msg__avatar {
  background: var(--grad-accent);
  color: #fff;
}

/* Speaker label above the avatar */
.msg__avatar-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}
.msg__speaker-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--c-text-3);
  white-space: nowrap;
}
.msg-ai  .msg__speaker-label { color: var(--c-primary); }
.msg-user .msg__speaker-label { color: var(--c-accent); }

.msg__bubble {
  padding: 1rem 1.35rem;
  border-radius: var(--r-xl);
  font-size: var(--fs-base);
  line-height: 1.7;
  max-width: 100%;
  word-break: break-word;
  position: relative;
}
/* AI bubble — subtle gradient for depth */
.msg-ai .msg__bubble {
  background: linear-gradient(135deg, #fff 0%, #F8FAFF 100%);
  border: 1px solid rgba(15,23,42,.07);
  border-top-left-radius: var(--r-sm);
  color: var(--c-text);
  box-shadow: 0 2px 12px rgba(15,23,42,.06);
}
/* User bubble — keep the distinctive blue gradient */
.msg-user .msg__bubble {
  background: var(--grad-card);
  color: #fff;
  border-top-right-radius: var(--r-sm);
  box-shadow: var(--shadow-blue);
}
/* System messages */
.msg-system {
  align-self: center;
  max-width: 90%;
  width: 100%;
}
.msg-system .msg__bubble {
  background: transparent;
  border: 1px dashed var(--c-border);
  color: var(--c-text-3);
  font-style: italic;
  font-size: var(--fs-xs);
  text-align: center;
  border-radius: var(--r-md);
  box-shadow: none;
}

/* ============ TYPING INDICATOR ============ */
.typing-indicator .msg__bubble {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: var(--sp-3) var(--sp-5);
}
.typing-dot {
  width: 8px; height: 8px;
  background: var(--c-primary);
  border-radius: 50%;
  opacity: .6;
  animation: typing-bounce .9s ease infinite;
}
.typing-dot:nth-child(2) { animation-delay: .15s; background: var(--c-accent); }
.typing-dot:nth-child(3) { animation-delay: .30s; background: var(--c-teal); }
@keyframes typing-bounce {
  0%,80%,100% { transform: translateY(0); opacity: .6; }
  40%          { transform: translateY(-6px); opacity: 1; }
}

/* ============ INPUT AREA ============ */
.dialogue-input-area {
  padding: var(--sp-3) var(--sp-4);
  border-top: 1px solid rgba(15,23,42,.07);
  display: flex;
  align-items: flex-end;
  gap: var(--sp-2);
  background: rgba(255,255,255,0.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.dialogue-input {
  flex: 1;
  padding: var(--sp-3) var(--sp-4);
  border: 1.5px solid var(--c-border);
  border-radius: var(--r-xl);
  font-size: var(--fs-sm);
  font-family: var(--f-sans);
  resize: none;
  max-height: 100px;
  min-height: 46px;
  line-height: 1.5;
  transition: border-color var(--t-fast), box-shadow var(--t-fast);
  background: #fff;
  color: var(--c-text);
}
.dialogue-input:focus {
  outline: none;
  border-color: var(--c-primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,.12);
}
.dialogue-send-btn {
  width: 46px; height: 46px;
  border-radius: 50%;
  background: var(--grad-card);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.05rem;
  border: none;
  cursor: pointer;
  transition: all var(--t-spring);
  flex-shrink: 0;
  box-shadow: var(--shadow-blue);
}
.dialogue-send-btn:hover {
  transform: scale(1.1) rotate(-5deg);
  box-shadow: 0 8px 28px rgba(37,99,235,.38);
}
.dialogue-send-btn:disabled {
  opacity: .4;
  pointer-events: none;
  box-shadow: none;
}

/* ============ FOOTER ============ */
.dialogue-footer {
  grid-area: footer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-2) var(--sp-5);
  border-top: 1px solid rgba(15,23,42,.06);
  background: rgba(255,255,255,0.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.dialogue-footer__note {
  font-size: var(--fs-xs);
  color: var(--c-text-3);
  font-style: italic;
}

/* ============ CHARACTER AVATAR (scripted mode) ============ */
.dialogue-character {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-5) 0;
  animation: msgIn var(--t-base) ease;
}
.dialogue-character__img {
  width: 72px; height: 72px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid var(--c-border-light);
  box-shadow: var(--shadow-sm);
}
.dialogue-character__img--ai   { border-color: var(--c-primary); }
.dialogue-character__img--user { border-color: var(--c-accent); }
.dialogue-character__label {
  font-size: var(--fs-xs);
  font-weight: 600;
  color: var(--c-text-3);
  text-transform: uppercase;
  letter-spacing: .06em;
}

/* ============ YOUR TURN BANNER (sidebar header) ============ */
/* Row layout keeps it compact (~60px tall) so more space goes to cards */
.dialogue-your-turn {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-5);
  background: transparent;
  border-bottom: 1px solid rgba(15,23,42,.08);
  animation: msgIn var(--t-base) ease;
  flex-shrink: 0;
}
.dialogue-your-turn__text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
  min-width: 0;
}
.dialogue-your-turn__label {
  font-size: var(--fs-sm);
  font-weight: 800;
  color: var(--c-accent);
  letter-spacing: .01em;
  line-height: 1.3;
}
.dialogue-your-turn__sub {
  font-size: var(--fs-xs);
  font-weight: 500;
  color: var(--c-text-3);
  letter-spacing: .01em;
  line-height: 1.4;
}

/* ============ CHOICES SIDEBAR ============ */
.dialogue-choices {
  grid-area: choices;
  display: flex;
  flex-direction: column;
  background: rgba(248,250,255,.98);
  border-left: 1px solid rgba(15,23,42,.08);
  overflow: hidden; /* children scroll individually */
}
/* Hide sidebar border/bg when no content yet (doctor is still speaking) */
.dialogue-choices:empty {
  background: transparent;
  border-left: none;
}
/* Scrollable options zone — takes all available space between banner and footer */
.dialogue-choices__grid {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: var(--sp-5);
  padding: var(--sp-5) var(--sp-5) var(--sp-5);
  scrollbar-width: thin;
  scrollbar-color: var(--c-border) transparent;
}
.dialogue-choices__grid::-webkit-scrollbar { width: 4px; }
.dialogue-choices__grid::-webkit-scrollbar-thumb {
  background: var(--c-border);
  border-radius: var(--r-full);
}
.dialogue-choice-btn {
  width: 100%;
  padding: var(--sp-5) var(--sp-5);
  background: #fff;
  border: 1.5px solid var(--c-border);
  border-radius: var(--r-lg);
  font-size: var(--fs-base);
  font-family: var(--f-sans);
  color: var(--c-text);
  cursor: pointer;
  text-align: left;
  line-height: 1.7;
  transition: all var(--t-fast);
  position: relative;
  padding-left: calc(var(--sp-5) + 1.6rem);
  box-shadow: 0 2px 8px rgba(15,23,42,.06);
  min-height: 70px;
}
.dialogue-choice-btn::before {
  content: '→';
  position: absolute;
  left: var(--sp-5);
  top: 50%;
  transform: translateY(-50%);
  color: var(--c-primary);
  font-weight: 700;
  transition: transform var(--t-fast);
}
.dialogue-choice-btn:hover:not(:disabled) {
  border-color: var(--c-primary);
  background: var(--c-primary-pale);
  color: var(--c-primary);
  transform: translateX(3px);
  box-shadow: 0 3px 14px rgba(37,99,235,.12);
}
.dialogue-choice-btn:hover:not(:disabled)::before {
  transform: translateY(-50%) translateX(4px);
}
.dialogue-choice-btn:disabled { opacity: .5; pointer-events: none; }
.dialogue-choice-btn--selected {
  border-color: var(--c-accent) !important;
  background: var(--c-accent-pale) !important;
  color: var(--c-accent) !important;
  box-shadow: 0 0 0 3px rgba(124,58,237,.1) !important;
  opacity: 1 !important;
  pointer-events: none;
}
.dialogue-choice-btn--selected::before {
  content: '✓';
  color: var(--c-accent);
}

/* ============ SPEECH MODE ============ */

/* Speak button footer — always pinned to bottom of sidebar */
.dialogue-speak-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.65rem;
  flex-shrink: 0;
  margin: var(--sp-2) var(--sp-5) var(--sp-4);
  padding: 1rem 2rem;
  background: var(--grad-card);
  color: #fff;
  border: none;
  border-radius: var(--r-full);
  font-size: 1.05rem;
  font-weight: 700;
  font-family: var(--f-sans);
  cursor: pointer;
  box-shadow: 0 6px 24px rgba(37,99,235,.36);
  transition: all var(--t-spring);
  letter-spacing: .01em;
}
.dialogue-speak-btn:hover:not(:disabled) {
  transform: translateY(-2px) scale(1.02);
  box-shadow: 0 8px 28px rgba(37,99,235,.42);
}
.dialogue-speak-btn:disabled { opacity: .7; cursor: default; pointer-events: none; }
.dialogue-speak-btn.listening {
  background: linear-gradient(135deg, #dc2626, #b91c1c);
  box-shadow: 0 0 0 0 rgba(220,38,38,.5);
  animation: pulse-mic 1.2s ease infinite;
}
@keyframes pulse-mic {
  0%   { box-shadow: 0 0 0 0 rgba(220,38,38,.5); }
  70%  { box-shadow: 0 0 0 14px rgba(220,38,38,0); }
  100% { box-shadow: 0 0 0 0 rgba(220,38,38,0); }
}

/* Hint cards (read-only speech prompts) */
.dialogue-choice-hint {
  padding: var(--sp-5) var(--sp-5);
  padding-left: calc(var(--sp-5) + 1.6rem);
  background: #fff;
  border: 1.5px solid var(--c-border);
  border-radius: var(--r-lg);
  font-size: var(--fs-base);
  color: var(--c-text-2);
  font-style: italic;
  line-height: 1.7;
  position: relative;
  user-select: none;
  box-shadow: 0 2px 8px rgba(15,23,42,.06);
  min-height: 70px;
  display: flex;
  align-items: center;
}
.dialogue-choice-hint::before {
  content: '💬';
  position: absolute;
  left: var(--sp-4);
  top: 50%;
  transform: translateY(-50%);
  font-style: normal;
  font-size: 0.95rem;
  line-height: 1;
}
.dialogue-choice-hint--matched {
  border-color: var(--c-accent);
  border-style: solid;
  background: var(--c-accent-pale);
  color: var(--c-accent);
  font-style: normal;
  font-weight: 600;
  box-shadow: 0 0 0 2px rgba(124,58,237,.12);
}
.dialogue-choice-hint--matched::before { content: '✓'; }

/* Speech error — lives inside .dialogue-choices__grid so margin is 0 */
.dialogue-speech-error {
  text-align: center;
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--c-error);
  margin: 0;
  padding: var(--sp-3) var(--sp-4);
  border-radius: var(--r-lg);
  border: 1.5px solid rgba(220,38,38,.2);
  background: var(--c-error-light);
  line-height: 1.5;
  animation: msgIn var(--t-base) ease;
}

/* ============ KARAOKE WORD HIGHLIGHT ============ */
/* Each word in a doctor's spoken bubble is wrapped in .msg__word.
   When audio plays, .msg__word--active moves through them like a cursor. */
.msg__word {
  display: inline;
  border-radius: 3px;
  padding: 0 1px;
  transition: background-color 0.08s ease, color 0.1s ease;
}
.msg__word--active {
  background: rgba(37, 99, 235, 0.18);
  color: var(--c-primary);
}

/* Loading bubble */
.msg__bubble--loading {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: var(--sp-3) var(--sp-5);
  min-width: 60px;
}

/* Live speech transcript — aria-live region for screen readers, NOT a visual element.
   Recording/transcribing status is shown in the button label instead. */
.dialogue-live-transcript {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

/* ============ DOCTOR SPEAKING PLACEHOLDER ============ */
.dialogue-waiting {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--sp-4);
  padding: var(--sp-8) var(--sp-6);
  text-align: center;
}
.dialogue-waiting__icon {
  font-size: 2.75rem;
  line-height: 1;
  animation: float-icon 2s ease-in-out infinite alternate;
}
.dialogue-waiting__text {
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--c-text-3);
  letter-spacing: .02em;
}
@keyframes float-icon {
  0%   { transform: translateY(0); }
  100% { transform: translateY(-8px); }
}

/* ============ PAGE LAYOUT WHEN DIALOGUE IS VISIBLE ============ */
/* Collapse the 96px dead space (container padding + footer margin + footer padding)
   that appears below the wrapper on dialogue pages */
#app-root:has(.dialogue-wrapper) .container {
  padding-top: var(--sp-2);
  padding-bottom: var(--sp-2);
  padding-left: var(--sp-3);
  padding-right: var(--sp-3);
}
#app-root:has(.dialogue-wrapper) ~ .site-footer {
  margin-top: var(--sp-3);
  padding-top: var(--sp-4);
}

/* ============ RESPONSIVE ============ */
@media (max-width: 700px) {
  /* Stack vertically on mobile — restore column layout */
  .dialogue-wrapper {
    grid-template-areas:
      "scenario"
      "messages"
      "choices"
      "footer";
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr auto auto;
    height: calc(100vh - 130px);
    min-height: 480px;
  }
  .dialogue-choices {
    border-left: none;
    border-top: 1px solid rgba(15,23,42,.08);
    max-height: 66vh;
  }
  .dialogue-choices__grid { gap: var(--sp-3); padding: var(--sp-4) var(--sp-4) var(--sp-2); }
  .dialogue-speak-btn { margin: var(--sp-3) var(--sp-4) var(--sp-4); padding: 1rem 1.5rem; }
  .dialogue-messages {
    padding: var(--sp-4) var(--sp-3);
    gap: var(--sp-4);
  }
  .msg { gap: var(--sp-3); max-width: 88%; }
  .msg__avatar { width: 40px; height: 40px; font-size: 0.9rem; }
  .msg__bubble { padding: 0.75rem 1rem; }
  .dialogue-choice-btn {
    font-size: var(--fs-sm);
    padding: var(--sp-4) var(--sp-4);
    padding-left: calc(var(--sp-4) + 1.4rem);
    line-height: 1.6;
  }
  .dialogue-choice-hint {
    font-size: var(--fs-sm);
    padding: var(--sp-3) var(--sp-4);
    padding-left: calc(var(--sp-4) + 1.5rem);
  }
  .dialogue-your-turn { padding: 1.25rem var(--sp-4) 1rem; }
  .dialogue-scenario { padding: var(--sp-3) var(--sp-4); }
}
