/* MAFS Coach widget — brand-locked palette (confirmed by Tina 2026-04-16).
   Rendered as isolated .mafs-coachbox shell so we don't collide with the classroom CSS. */

.mafs-coachbox,
.mafs-coachbox * {
  box-sizing: border-box;
}

.mafs-coachbox {
  --mc-cream: #f5f0e8;
  --mc-charcoal: #2d2a26;
  /* Theme-bridged accents — track the classroom's active palette via
     --wisteria/--wisteria-deep/--wisteria-soft (set on .mafs-root by
     MAFS_applyPalette, and mirrored to this panel by syncThemeColor in
     chat-widget.js since the panel is mounted on body, not inside .mafs-root).
     Names kept as --mc-wisteria* for backwards compat with existing rules.
     Fallbacks are the original wisteria values for standalone widget loads. */
  --mc-wisteria: var(--wisteria, #8b7da8);
  --mc-wisteria-dark: var(--wisteria-deep, #6b5f8a);
  --mc-wisteria-soft: var(--wisteria-soft, #e7e2ed);
  /* Kept hardcoded — semantic colors that should NOT track the theme:
     sage = "active mic" (always green), terracotta = "warning/error" (always red-orange) */
  --mc-sage: #7d9b76;
  --mc-terracotta: #c0614a;
  --mc-line: #d9d2c5;
  --mc-ink-soft: #5a544c;
  --mc-shadow: 0 20px 60px rgba(45, 42, 38, 0.18);
  /* Tracks the classroom's active palette (--accent-soft on .mafs-root) so
     the usage ring follows whatever theme color the user picks in Settings.
     Falls back to wisteria if the widget is loaded outside the classroom. */
  --mc-ring-color: var(--accent-soft, var(--mc-wisteria));

  position: fixed;
  inset: 0 0 0 auto;
  width: min(440px, 100vw);
  background: var(--mc-cream);
  color: var(--mc-charcoal);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  z-index: 2147483646;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.28s cubic-bezier(0.22, 1, 0.36, 1);
  box-shadow: var(--mc-shadow);
  border-left: 1px solid var(--mc-line);
}

.mafs-coachbox[data-open="true"] {
  transform: translateX(0);
}

.mafs-coachbox__backdrop {
  position: fixed;
  inset: 0;
  background: rgba(45, 42, 38, 0.18);
  z-index: 2147483645;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.mafs-coachbox__backdrop[data-open="true"] {
  opacity: 1;
  pointer-events: auto;
}

.mafs-coachbox__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 20px 14px;
  background: linear-gradient(135deg, var(--mc-wisteria-dark), var(--mc-wisteria));
  color: #fff;
  flex-shrink: 0;
}

.mafs-coachbox__title {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.mafs-coachbox__title b {
  font-weight: 600;
  font-size: 16px;
  letter-spacing: 0.01em;
}

.mafs-coachbox__title span {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.85);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 320px;
}

.mafs-coachbox__close {
  all: unset;
  cursor: pointer;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  font-size: 20px;
  line-height: 1;
  transition: background 0.15s ease;
}

.mafs-coachbox__close:hover,
.mafs-coachbox__close:focus-visible {
  background: rgba(255, 255, 255, 0.18);
}

.mafs-coachbox__body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scrollbar-width: thin;
  scrollbar-color: var(--mc-line) transparent;
}

.mafs-coachbox__body::-webkit-scrollbar {
  width: 8px;
}
.mafs-coachbox__body::-webkit-scrollbar-thumb {
  background: var(--mc-line);
  border-radius: 4px;
}

.mafs-coachbox__intro {
  background: var(--mc-wisteria-soft);
  border: 1px solid rgba(139, 125, 168, 0.25);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 13px;
  color: var(--mc-charcoal);
}

.mafs-coachbox__intro b {
  color: var(--mc-wisteria-dark);
}

.mafs-coachbox__msg {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: 94%;
}

.mafs-coachbox__msg[data-role="user"] {
  align-self: flex-end;
}

.mafs-coachbox__msg[data-role="assistant"] {
  align-self: flex-start;
}

.mafs-coachbox__bubble {
  padding: 10px 14px;
  border-radius: 14px;
  word-wrap: break-word;
  white-space: pre-wrap;
  font-size: 15px;
  line-height: 1.55;
}

.mafs-coachbox__msg[data-role="user"] .mafs-coachbox__bubble {
  background: var(--mc-wisteria);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.mafs-coachbox__msg[data-role="assistant"] .mafs-coachbox__bubble {
  background: #fff;
  border: 1px solid var(--mc-line);
  color: var(--mc-charcoal);
  border-bottom-left-radius: 4px;
}

.mafs-coachbox__bubble[data-kind="error"] {
  background: #fdf1ee !important;
  border: 1px solid var(--mc-terracotta);
  color: var(--mc-terracotta);
}

.mafs-coachbox__bubble[data-kind="rate"] {
  background: #fbf6eb !important;
  border: 1px solid #d9b86a;
  color: #7a5a14;
}

.mafs-coachbox__typing {
  display: inline-flex;
  gap: 4px;
  align-items: center;
}

.mafs-coachbox__typing span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--mc-ink-soft);
  opacity: 0.6;
  animation: mafs-coach-pulse 1.2s infinite ease-in-out;
}
.mafs-coachbox__typing span:nth-child(2) {
  animation-delay: 0.15s;
}
.mafs-coachbox__typing span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes mafs-coach-pulse {
  0%, 100% { transform: translateY(0); opacity: 0.4; }
  50% { transform: translateY(-3px); opacity: 1; }
}

.mafs-coachbox__input {
  display: flex;
  gap: 8px;
  padding: 12px 16px 16px;
  border-top: 1px solid var(--mc-line);
  background: var(--mc-cream);
  flex-shrink: 0;
}

.mafs-coachbox__textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--mc-line);
  border-radius: 12px;
  padding: 10px 12px;
  font: inherit;
  color: var(--mc-charcoal);
  background: #fff;
  max-height: 160px;
  min-height: 42px;
  line-height: 1.4;
  outline: none;
  transition: border-color 0.15s ease;
}

.mafs-coachbox__textarea:focus {
  border-color: var(--mc-wisteria);
}

.mafs-coachbox__textarea::placeholder {
  color: var(--mc-ink-soft);
  opacity: 0.7;
}

.mafs-coachbox__send {
  all: unset;
  cursor: pointer;
  padding: 0 18px;
  border-radius: 12px;
  background: var(--mc-wisteria);
  color: #fff;
  font-weight: 600;
  font-size: 14px;
  min-height: 42px;
  transition: background 0.15s ease, transform 0.1s ease;
  display: grid;
  place-items: center;
  user-select: none;
}

.mafs-coachbox__send:hover:not([disabled]) {
  background: var(--mc-wisteria-dark);
}
.mafs-coachbox__send:active:not([disabled]) {
  transform: translateY(1px);
}

.mafs-coachbox__send[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Daily usage indicator — circular SVG ring inline with the disclaimer.
   Always visible (per user request). Color/intensity ramps via data-stage:
   default (0-79%) → warning (80-94%) → critical (95-99%) → exhausted (100%).
   Ring style: small dark filled circle with a thin glowing arc that draws
   clockwise as percentUsed climbs. */
.mafs-coachbox__usage {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
.mafs-coachbox__usage-text {
  font-size: 11px;
  color: var(--mc-ink-soft);
  text-align: right;
  line-height: 1.3;
  white-space: nowrap;
}
.mafs-coachbox__usage-text:empty {
  display: none;
}
.mafs-coachbox__usage-ring {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  border-radius: 50%;
  /* Soft shadow look — barely-visible backdrop, lets the colored arc breathe.
     Was a hard dark fill (#1f1c18); now a faint charcoal wash. */
  background: rgba(45, 42, 38, 0.08);
  padding: 2px;
  box-sizing: content-box;
  transition: box-shadow 0.3s ease;
}
.mafs-coachbox__usage-ring-bg {
  fill: none;
  stroke: rgba(255, 255, 255, 0.08);
  stroke-width: 3;
}
.mafs-coachbox__usage-ring-fill {
  fill: none;
  stroke: var(--mc-ring-color);
  stroke-width: 3;
  stroke-linecap: round;
  /* Start from 12 o'clock by rotating -90deg around the circle's center. */
  transform: rotate(-90deg);
  transform-origin: 18px 18px;
  transition: stroke-dashoffset 0.4s ease, stroke 0.3s ease;
}

/* Stage ramps */
.mafs-coachbox__usage[data-stage="default"] .mafs-coachbox__usage-ring {
  box-shadow: 0 0 6px color-mix(in srgb, var(--mc-ring-color) 35%, transparent);
}
.mafs-coachbox__usage[data-stage="warning"] .mafs-coachbox__usage-ring-fill {
  stroke: var(--mc-terracotta);
}
.mafs-coachbox__usage[data-stage="warning"] .mafs-coachbox__usage-ring {
  box-shadow: 0 0 8px rgba(192, 97, 74, 0.5);
}
.mafs-coachbox__usage[data-stage="warning"] .mafs-coachbox__usage-text {
  color: var(--mc-terracotta);
}
.mafs-coachbox__usage[data-stage="critical"] .mafs-coachbox__usage-ring-fill {
  stroke: #d8442a;
}
.mafs-coachbox__usage[data-stage="critical"] .mafs-coachbox__usage-ring {
  box-shadow: 0 0 12px rgba(216, 68, 42, 0.7);
  animation: mafs-coachbox-pulse 1.6s ease-in-out infinite;
}
.mafs-coachbox__usage[data-stage="critical"] .mafs-coachbox__usage-text {
  color: #d8442a;
  font-weight: 600;
}
.mafs-coachbox__usage[data-stage="exhausted"] .mafs-coachbox__usage-ring-fill {
  stroke: #8a8a8a;
}
.mafs-coachbox__usage[data-stage="exhausted"] .mafs-coachbox__usage-ring {
  box-shadow: none;
  opacity: 0.7;
}
.mafs-coachbox__usage[data-stage="exhausted"] .mafs-coachbox__usage-text {
  color: var(--mc-ink-soft);
}
@keyframes mafs-coachbox-pulse {
  0%, 100% { box-shadow: 0 0 8px rgba(216, 68, 42, 0.5); }
  50%      { box-shadow: 0 0 14px rgba(216, 68, 42, 0.85); }
}

.mafs-coachbox__footnote {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 11px;
  color: var(--mc-ink-soft);
  padding: 6px 16px 10px;
  background: var(--mc-cream);
  flex-shrink: 0;
}
.mafs-coachbox__footnote-text {
  flex: 1 1 auto;
  text-align: left;
  line-height: 1.4;
}

@media (max-width: 520px) {
  .mafs-coachbox {
    width: 100vw;
    max-width: 100vw;
  }
}

/* ─── Voice UI ────────────────────────────────────────────────────────── */
.mafs-coachbox__headbtns {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.mafs-coachbox__mic {
  all: unset;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px 6px 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
  font-size: 12.5px;
  font-weight: 600;
  line-height: 1;
  border: 1px solid rgba(255, 255, 255, 0.24);
  transition: background 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease;
  white-space: nowrap;
}

.mafs-coachbox__mic:hover,
.mafs-coachbox__mic:focus-visible {
  background: rgba(255, 255, 255, 0.28);
}

.mafs-coachbox__mic:active {
  transform: translateY(1px);
}

.mafs-coachbox__mic-dot {
  display: none;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ff4d4d;
  box-shadow: 0 0 0 0 rgba(255, 77, 77, 0.7);
}

.mafs-coachbox__mic[data-state="active"] {
  background: rgba(192, 97, 74, 0.9);
  border-color: rgba(255, 255, 255, 0.45);
}

.mafs-coachbox__mic[data-state="active"] .mafs-coachbox__mic-dot {
  display: inline-block;
  animation: mafs-coach-pulsedot 1.2s infinite ease-out;
}

.mafs-coachbox__mic[data-state="active"] .mafs-coachbox__mic-icon {
  display: none;
}

.mafs-coachbox__mic[data-state="starting"],
.mafs-coachbox__mic[data-state="ending"] {
  opacity: 0.75;
  pointer-events: none;
}

.mafs-coachbox__mic-icon {
  font-size: 14px;
  line-height: 1;
}

.mafs-coachbox__mic-label {
  font-family: inherit;
}

@keyframes mafs-coach-pulsedot {
  0%   { box-shadow: 0 0 0 0 rgba(255, 77, 77, 0.8); }
  70%  { box-shadow: 0 0 0 8px rgba(255, 77, 77, 0); }
  100% { box-shadow: 0 0 0 0 rgba(255, 77, 77, 0); }
}

/* Voice indicator row — sits below header, above body */
.mafs-coachbox__voice {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 8px 20px;
  background: var(--mc-wisteria-soft);
  border-bottom: 1px solid var(--mc-line);
  font-size: 12.5px;
  color: var(--mc-wisteria-dark);
  font-weight: 600;
  flex-shrink: 0;
}

.mafs-coachbox__voice[data-state="user"],
.mafs-coachbox__voice[data-state="ai"] {
  display: flex;
}

.mafs-coachbox__voice-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
  animation: mafs-coach-voice-breathe 1.6s infinite ease-in-out;
}

.mafs-coachbox__voice[data-state="user"] .mafs-coachbox__voice-dot {
  background: var(--mc-sage);
  box-shadow: 0 0 0 0 rgba(125, 155, 118, 0.6);
}

.mafs-coachbox__voice[data-state="ai"] .mafs-coachbox__voice-dot {
  background: var(--mc-wisteria);
  box-shadow: 0 0 0 0 rgba(139, 125, 168, 0.6);
}

@keyframes mafs-coach-voice-breathe {
  0%, 100% { transform: scale(1); opacity: 0.8; }
  50%      { transform: scale(1.35); opacity: 1; box-shadow: 0 0 0 6px rgba(139, 125, 168, 0); }
}

/* Inline toast — subtle, auto-hides */
.mafs-coachbox__toast {
  position: absolute;
  left: 50%;
  bottom: 82px;
  transform: translateX(-50%) translateY(12px);
  max-width: 88%;
  background: var(--mc-charcoal);
  color: var(--mc-cream);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.4;
  box-shadow: 0 6px 22px rgba(0, 0, 0, 0.22);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.22s cubic-bezier(0.22, 1, 0.36, 1);
  z-index: 2;
  text-align: center;
}

.mafs-coachbox__toast[data-visible="true"] {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Hide mic label on very narrow panels to keep header tidy */
@media (max-width: 380px) {
  .mafs-coachbox__mic-label { display: none; }
  .mafs-coachbox__mic { padding: 6px 9px; }
}

/* ─────────────────────────────────────────────────────────────────────
   DARK MODE — auto-flip when the classroom is in dark mode.
   The chat widget lives outside .mafs-root in the DOM, so we use
   body:has(...) to detect the classroom's data-mode and re-map the
   widget's CSS custom properties.
   ───────────────────────────────────────────────────────────────────── */
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox {
  --mc-cream:         #1F1C18;
  --mc-charcoal:      #EFE9DE;
  --mc-wisteria:      #B5A8D0;
  --mc-wisteria-dark: #8B7DA8;
  --mc-wisteria-soft: rgba(139, 125, 168, 0.18);
  --mc-sage:          #9DBA94;
  --mc-terracotta:    #E29280;
  --mc-line:          rgba(239, 233, 222, 0.12);
  --mc-ink-soft:      #B5ACA0;
  --mc-shadow:        0 20px 60px rgba(0, 0, 0, 0.55);
}

/* Inset surfaces that hardcoded white/cream — re-map to a slightly
   lighter dark tone so bubbles and inputs lift off the panel.
   The light-mode rules in this file use [data-role="..."] attribute
   selectors (line 171, 165 etc.), so dark overrides must match the
   same selector shape to win the cascade. */
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox__msg[data-role="assistant"] .mafs-coachbox__bubble,
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox__bubble--assistant {
  background: #2A2622;
  color: var(--mc-charcoal);
  border-color: var(--mc-line);
}
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox__msg[data-role="user"] .mafs-coachbox__bubble,
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox__bubble--user {
  background: rgba(125, 155, 118, 0.20);
  color: var(--mc-charcoal);
  border-color: rgba(125, 155, 118, 0.32);
}
/* Error and rate-limit bubbles use !important — match it in dark too,
   tone the cream toward terracotta-on-dark and yellow-on-dark. */
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox__bubble[data-kind="error"] {
  background: rgba(192, 97, 74, 0.18) !important;
  color: var(--mc-terracotta);
  border-color: rgba(192, 97, 74, 0.45);
}
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox__bubble[data-kind="rate"] {
  background: rgba(217, 184, 106, 0.16) !important;
  color: #E0C57E;
  border-color: rgba(217, 184, 106, 0.42);
}

/* Input + textarea — beat any hardcoded #fff */
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox__input,
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox textarea,
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox__feedback textarea,
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox__feedback-input {
  background: #2A2622 !important;
  color: var(--mc-charcoal);
  border-color: var(--mc-line);
}
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox__input::placeholder,
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox textarea::placeholder {
  color: var(--mc-ink-soft);
}
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox__input:focus,
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox textarea:focus {
  background: #2A2622 !important;
  border-color: var(--mc-terracotta);
}

/* Daily usage indicator — dark mode: faint white wash so the ring still reads as a circle without going hard-dark again */
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox__usage-ring {
  background: rgba(255, 255, 255, 0.06);
}
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox__usage-ring-bg {
  stroke: rgba(255, 255, 255, 0.12);
}
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox__usage[data-stage="exhausted"] .mafs-coachbox__usage-text {
  color: #B5ACA0;
}

/* Backdrop scrim slightly darker for dark mode */
body:has(.mafs-root[data-mode="dark"]) .mafs-coachbox__backdrop {
  background: rgba(0, 0, 0, 0.55);
}

/* Floating "Ask Freedom Systems Coach" pill — flip charcoal/cream so
   it stays readable on a dark page. */
body:has(.mafs-root[data-mode="dark"]) .mafs-aicoach-fab {
  background: var(--mc-charcoal);
  color: #1F1C18;
}
body:has(.mafs-root[data-mode="dark"]) .mafs-aicoach-fab__icon {
  background: rgba(0, 0, 0, 0.20);
}
