/* ============ Course tutor — centered modal chat + voice ============
   Accent vars (--accent / --accent-2 / --accent-soft / --accent-line) are set
   inline per course on .tw so each tutor takes on its course's colour. */

.tw-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: grid;
  place-items: center;
  padding: clamp(0px, 2vw, 28px);
  background: rgba(8, 18, 42, 0.55);
  backdrop-filter: blur(6px);
  animation: tw-fade var(--dur) var(--ease) both;
}
@keyframes tw-fade { from { opacity: 0; } to { opacity: 1; } }

.tw {
  --accent: var(--magenta);
  --accent-2: var(--magenta-300);
  --accent-soft: #fdeaf3;
  --accent-line: #f6cfe1;
  position: relative;
  display: flex;
  flex-direction: column;
  width: min(440px, 100%);
  height: min(720px, 92vh);
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  font-family: var(--font);
  animation: tw-open var(--dur) var(--ease) both;
}
@keyframes tw-open {
  from { opacity: 0; transform: translateY(18px) scale(0.97); }
  to { opacity: 1; transform: none; }
}

/* header */
.tw__head {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.85rem 0.9rem;
  color: #fff;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-2) 130%);
}
.tw__head::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 3px;
  background: linear-gradient(90deg, rgba(255, 255, 255, 0.5), transparent);
}
.tw__avatar {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 13px;
  display: grid;
  place-items: center;
  font-size: 1.5rem;
  background: rgba(255, 255, 255, 0.2);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.28);
}
.tw__id {
  flex: 1;
  min-width: 0;
  line-height: 1.25;
}
.tw__id b {
  display: block;
  font-size: 1rem;
  font-weight: 700;
}
.tw__id .tw__course {
  display: block;
  font-size: 0.74rem;
  opacity: 0.92;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tw__id .tw__status {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.72rem;
  opacity: 0.92;
}
.tw__status::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #36d27a;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.3);
}
.tw__head-btn {
  flex: none;
  width: 34px;
  height: 34px;
  border-radius: 9px;
  border: 0;
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 1.1rem;
  transition: background var(--dur-fast);
}
.tw__head-btn:hover { background: rgba(255, 255, 255, 0.3); }
.tw__head-btn svg { width: 18px; height: 18px; }

/* body */
.tw__body {
  flex: 1;
  overflow-y: auto;
  overflow-anchor: none;
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  background:
    radial-gradient(560px 200px at 100% 0, var(--accent-soft) 0%, transparent 72%),
    var(--bg-tint);
}
.tw__body::-webkit-scrollbar { width: 8px; }
.tw__body::-webkit-scrollbar-thumb { background: #d3dbe9; border-radius: 8px; }

.msg {
  display: flex;
  gap: 0.5rem;
  max-width: 92%;
  animation: tw-msg var(--dur) var(--ease) both;
}
@keyframes tw-msg { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.msg__ava {
  flex: none;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 0.95rem;
  align-self: flex-end;
  background: var(--accent-soft);
  border: 1px solid var(--accent-line);
}
.msg__bubble {
  padding: 0.62rem 0.85rem;
  border-radius: 15px;
  font-size: 0.93rem;
  line-height: 1.62;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg--bot { align-self: flex-start; }
.msg--bot .msg__bubble {
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line);
  border-bottom-left-radius: 5px;
}
.msg--user { align-self: flex-end; flex-direction: row-reverse; }
.msg--user .msg__bubble {
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-2) 130%);
  color: #fff;
  border-bottom-right-radius: 5px;
}
.msg__bubble strong { font-weight: 700; }
.msg--bot .msg__bubble a { color: var(--accent); text-decoration: underline; }
.msg__bubble ol, .msg__bubble ul { margin: 0.3rem 0; padding-inline-start: 1.3rem; }
.msg__bubble li { margin: 0.15rem 0; }
.msg__bubble code {
  background: #f0f3f9;
  padding: 0 4px;
  border-radius: 4px;
  font-size: 0.88em;
}

/* welcome card */
.tw__welcome {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 15px;
  padding: var(--space-3);
  box-shadow: var(--shadow-sm);
}
.tw__welcome h3 { margin: 0 0 0.3rem; font-size: 1.04rem; color: var(--navy); }
.tw__welcome p { margin: 0 0 var(--space-2); font-size: 0.87rem; color: var(--ink-soft); }
.tw__label { font-size: 0.74rem; font-weight: 700; color: var(--muted); margin-bottom: 0.45rem; }

/* chips */
.tw__chips { display: flex; flex-wrap: wrap; gap: 0.45rem; }
.tw__chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.5rem 0.8rem;
  font-size: 0.83rem;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  border: 1px solid var(--accent-line);
  border-radius: var(--radius-pill);
  transition: transform var(--dur-fast), background var(--dur-fast), box-shadow var(--dur-fast), border-color var(--dur-fast);
}
.tw__chip:hover {
  background: #fff;
  transform: translateY(-1px);
  box-shadow: var(--shadow-sm);
  border-color: var(--accent);
}
.tw__chip .ic { font-size: 1rem; }
.tw__suggest { display: flex; flex-direction: column; gap: 0.4rem; align-self: flex-start; max-width: 94%; }
.tw__suggest-label { font-size: 0.72rem; color: var(--muted); padding-inline-start: 0.2rem; }
.tw__suggest .tw__chip { animation: tw-chipin var(--dur) var(--ease) both; }
@keyframes tw-chipin { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* typing + streaming */
.tw__typing { display: inline-flex; gap: 4px; padding: 0.7rem 0.9rem; }
.tw__typing span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--muted);
  animation: tw-bounce 1.2s var(--ease) infinite;
}
.tw__typing span:nth-child(2) { animation-delay: 0.18s; }
.tw__typing span:nth-child(3) { animation-delay: 0.36s; }
@keyframes tw-bounce { 0%, 60%, 100% { transform: translateY(0); opacity: 0.5; } 30% { transform: translateY(-5px); opacity: 1; } }

.msg__bubble.is-streaming { min-height: 1.15em; }
.msg__txt { white-space: pre-wrap; }
.tw__cursor {
  display: inline-block;
  width: 7px;
  height: 1.02em;
  margin-left: 2px;
  vertical-align: -2px;
  border-radius: 2px;
  background: var(--accent);
  animation: tw-caret 1.05s steps(1) infinite;
}
@keyframes tw-caret { 0%, 49% { opacity: 0.85; } 50%, 100% { opacity: 0.12; } }
.msg__bubble.is-final { animation: tw-fadein var(--dur) var(--ease) both; }
@keyframes tw-fadein { from { opacity: 0.6; } to { opacity: 1; } }

/* footer / composer */
.tw__foot {
  border-top: 1px solid var(--line);
  background: #fff;
  padding: 0.6rem 0.7rem 0.5rem;
}
.tw__composer { display: flex; align-items: flex-end; gap: 0.5rem; }
.tw__input {
  flex: 1;
  resize: none;
  max-height: 120px;
  padding: 0.62rem 0.8rem;
  font-family: inherit;
  font-size: 0.93rem;
  line-height: 1.5;
  color: var(--ink);
  border: 1.5px solid var(--line);
  border-radius: 14px;
  background: var(--bg-tint);
  transition: border-color var(--dur-fast), box-shadow var(--dur-fast), background var(--dur-fast);
}
.tw__input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); background: #fff; }
.tw__mic {
  flex: none;
  width: 42px; height: 42px;
  border-radius: 50%;
  border: 1.5px solid var(--line);
  background: #fff;
  color: var(--accent);
  display: grid; place-items: center;
  transition: border-color var(--dur-fast), background var(--dur-fast), color var(--dur-fast);
}
.tw__mic svg { width: 20px; height: 20px; }
.tw__mic:hover { border-color: var(--accent); }
.tw__mic.is-listening {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  border-color: transparent;
  animation: tw-pulse 1.3s var(--ease) infinite;
}
@keyframes tw-pulse {
  0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 50%, transparent); }
  50% { box-shadow: 0 0 0 11px transparent; }
}
.tw__send {
  flex: none;
  width: 42px; height: 42px;
  border: 0; border-radius: 50%;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: grid; place-items: center;
  box-shadow: 0 4px 12px color-mix(in srgb, var(--accent) 32%, transparent);
  transition: transform var(--dur-fast), filter var(--dur-fast), opacity var(--dur-fast);
}
.tw__send svg { width: 20px; height: 20px; }
.tw__send:hover:not(:disabled) { transform: scale(1.06); filter: brightness(1.06); }
.tw__send:disabled { opacity: 0.45; cursor: not-allowed; }
.tw__disclaimer { margin: 0.4rem 0 0; text-align: center; font-size: 0.66rem; color: var(--muted); }

/* read-aloud button on a finished bot bubble */
.tw__speak {
  display: flex; align-items: center; justify-content: center;
  width: 26px; height: 26px;
  margin-top: 0.45rem;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
  transition: color var(--dur-fast), border-color var(--dur-fast), background var(--dur-fast);
}
.tw__speak svg { width: 15px; height: 15px; }
.tw__speak:hover { color: var(--accent); border-color: var(--accent); }
.tw__speak.is-playing {
  color: #fff; background: var(--accent); border-color: var(--accent);
  animation: tw-pulse 1.3s var(--ease) infinite;
}

/* voice call overlay */
.tw__voice {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.1rem;
  padding: 1.5rem;
  text-align: center;
  color: #fff;
  background:
    radial-gradient(circle at 50% 30%, color-mix(in srgb, var(--accent) 70%, #1a0a18) 0%, #14082a 62%, #080418 100%);
  animation: tw-fadein var(--dur) var(--ease) both;
}
.tw__voice-title { font-size: 1.05rem; font-weight: 700; opacity: 0.92; }
.tw__voice-sub { font-size: 0.82rem; opacity: 0.7; margin-top: -0.7rem; }
.tw__orb {
  position: relative;
  width: 152px; height: 152px;
  border-radius: 50%;
  border: 0;
  display: grid; place-items: center;
  font-size: 3.4rem;
  background: rgba(255, 255, 255, 0.08);
  cursor: pointer;
}
.tw__orb-ring { position: absolute; inset: 0; border-radius: 50%; border: 3px solid rgba(255, 255, 255, 0.3); }
.tw__orb[data-mode="listening"] .tw__orb-ring { border-color: #36d27a; animation: tw-orb-listen 1.5s var(--ease) infinite; }
.tw__orb[data-mode="thinking"] .tw__orb-ring { border-color: rgba(255, 255, 255, 0.25); border-top-color: #fff; animation: tw-orb-spin 0.9s linear infinite; }
.tw__orb[data-mode="speaking"] .tw__orb-ring { border-color: var(--gold); animation: tw-orb-speak 0.7s var(--ease) infinite; }
@keyframes tw-orb-listen { 0%, 100% { box-shadow: 0 0 0 0 rgba(54, 210, 122, 0.45); } 50% { box-shadow: 0 0 0 24px rgba(54, 210, 122, 0); } }
@keyframes tw-orb-spin { to { transform: rotate(360deg); } }
@keyframes tw-orb-speak { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.07); } }
.tw__voice-status { font-size: 1.04rem; font-weight: 700; }
.tw__voice-caption { min-height: 2.6em; max-width: 88%; font-size: 0.95rem; line-height: 1.55; opacity: 0.92; }
.tw__voice-hang {
  margin-top: 0.4rem;
  padding: 0.7rem 2.2rem;
  border: 0;
  border-radius: var(--radius-pill);
  background: #fff;
  color: var(--accent);
  font-weight: 800;
  font-size: 1rem;
  box-shadow: var(--shadow);
  transition: transform var(--dur-fast);
}
.tw__voice-hang:hover { transform: translateY(-1px); }

@media (max-width: 480px) {
  .tw-overlay { padding: 0; }
  .tw { width: 100%; height: 100%; border-radius: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .tw-overlay, .tw, .msg, .tw__typing span, .msg__bubble.is-final,
  .tw__suggest .tw__chip, .tw__voice, .tw__orb-ring, .tw__mic.is-listening,
  .tw__speak.is-playing, .ccard {
    animation: none !important;
    transition: none !important;
    opacity: 1 !important;
    transform: none !important;
  }
  .tw__cursor { animation: none !important; opacity: 0.85; }
}
