/* Chat feature: room chat + Owl study buddy. Every class is prefixed .chat- or .owl-. */

.chat-root {
  --chat-soft: color-mix(in srgb, var(--text) 5.5%, transparent);
  --chat-lamp-tint: color-mix(in srgb, var(--lamp) 13%, transparent);
  --chat-lamp-line: color-mix(in srgb, var(--lamp) 28%, transparent);
  --chat-well: color-mix(in srgb, var(--ink-950) 45%, transparent);
  display: flex; flex-direction: column; gap: var(--s-3);
  height: 100%; min-height: 0; padding: var(--s-3);
}
.chat-root [hidden] { display: none !important; }

/* ---------- tabs ---------- */
.chat-tabs {
  position: relative; display: grid; grid-template-columns: 1fr 1fr; padding: 4px;
  background: var(--chat-well); border: 1px solid var(--glass-line); border-radius: var(--r-pill);
  flex: none;
}
.chat-tab-glow {
  position: absolute; top: 4px; bottom: 4px; left: 4px; width: calc(50% - 4px);
  border-radius: var(--r-pill); background: var(--chat-lamp-tint);
  box-shadow: inset 0 0 0 1px var(--chat-lamp-line), 0 0 22px color-mix(in srgb, var(--lamp) 14%, transparent);
  transition: transform var(--t-med) var(--ease);
  pointer-events: none;
}
.chat-tabs[data-active='owl'] .chat-tab-glow { transform: translateX(100%); }
.chat-tab {
  position: relative; z-index: 1; display: inline-flex; align-items: center; justify-content: center; gap: var(--s-2);
  min-height: 36px; padding: 0 var(--s-3); border: 0; border-radius: var(--r-pill); background: transparent;
  color: var(--text-dim); font-size: var(--fs-sm); font-weight: 500; letter-spacing: 0.01em;
  transition: color var(--t-med) var(--ease);
}
.chat-tab:hover { color: var(--text); }
.chat-tab[aria-selected='true'] { color: var(--lamp-soft); }
.chat-badge {
  min-width: 18px; height: 18px; padding: 0 5px; display: inline-grid; place-items: center;
  border-radius: var(--r-pill); background: var(--rose); color: var(--ink-900);
  font: 600 10px/1 var(--font-mono); font-variant-numeric: tabular-nums;
  box-shadow: 0 0 12px var(--rose-glow);
  animation: chat-bloom var(--t-med) var(--ease);
}

/* ---------- panels + logs ---------- */
.chat-panel { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: var(--s-2); }
.chat-log-wrap { position: relative; flex: 1; min-height: 0; }
.chat-log, .owl-log {
  position: absolute; inset: 0; overflow-y: auto; overscroll-behavior: contain;
  padding: var(--s-2) 2px var(--s-3);
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 14px, #000 calc(100% - 8px), transparent 100%);
          mask-image: linear-gradient(to bottom, transparent 0, #000 14px, #000 calc(100% - 8px), transparent 100%);
  transition: opacity var(--t-slow) var(--ease);
}
.chat-log:focus-visible, .owl-log:focus-visible { outline-offset: -2px; }
.chat-root.is-whisper .chat-log { opacity: 0.8; }

.chat-in { animation: chat-bloom calc(var(--t-med) * 1.6) var(--ease) both; }
@keyframes chat-bloom {
  from { opacity: 0; filter: blur(4px); box-shadow: 0 0 26px var(--lamp-glow); }
  to { opacity: 1; filter: blur(0); }
}

/* empty */
.chat-empty { text-align: center; padding: var(--s-7) var(--s-4) var(--s-5); }
.chat-empty-title {
  margin: 0; font-family: var(--font-display); font-variation-settings: 'SOFT' 100;
  font-size: var(--fs-lg); color: var(--text-dim);
}
.chat-empty-sub { margin: var(--s-1) 0 0; font-size: var(--fs-sm); color: var(--text-faint); }

/* groups + lines */
.chat-group { display: flex; flex-direction: column; align-items: flex-start; margin-top: var(--s-3); }
.chat-group:first-child { margin-top: 0; }
.chat-meta { display: flex; align-items: baseline; gap: var(--s-2); padding: 0 4px 3px; max-width: 100%; }
.chat-name { font-size: var(--fs-sm); font-weight: 600; color: var(--lamp-soft); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-time { font: 400 var(--fs-xs) var(--font-mono); font-variant-numeric: tabular-nums; color: var(--text-faint); flex: none; }
.chat-lines { display: flex; flex-direction: column; align-items: flex-start; gap: 3px; max-width: 100%; }
.chat-line {
  margin: 0; max-width: 92%; padding: 6px 11px; border-radius: 4px 14px 14px 14px;
  background: var(--chat-soft); border: 1px solid var(--glass-line);
  font-size: var(--fs-sm); line-height: 1.45; overflow-wrap: anywhere; white-space: pre-wrap;
}
.chat-line + .chat-line { border-radius: 4px 14px 14px 4px; }
.chat-group--me { align-items: flex-end; }
.chat-group--me .chat-meta { flex-direction: row-reverse; }
.chat-group--me .chat-lines { align-items: flex-end; }
.chat-group--me .chat-line { background: var(--chat-lamp-tint); border-color: var(--chat-lamp-line); border-radius: 14px 4px 14px 14px; }
.chat-group--me .chat-line + .chat-line { border-radius: 14px 4px 4px 14px; }
.chat-line--emoji, .chat-group--me .chat-line--emoji {
  background: transparent; border-color: transparent; padding: 0 2px;
  font-size: 2rem; line-height: 1.2; letter-spacing: 0.04em;
}
.chat-link, .owl-link { color: var(--lamp-soft); text-decoration: underline; text-decoration-color: var(--chat-lamp-line); text-underline-offset: 2px; overflow-wrap: anywhere; }
.chat-link:hover, .owl-link:hover { text-decoration-color: currentColor; }

.chat-system {
  display: flex; align-items: center; justify-content: center; gap: var(--s-2);
  margin: var(--s-3) 0 0; font-size: var(--fs-xs); color: var(--text-faint); text-align: center;
}
.chat-system-dot { width: 5px; height: 5px; border-radius: 50%; background: var(--lamp); box-shadow: 0 0 8px var(--lamp-glow); flex: none; }

/* new-messages pill */
.chat-pill {
  position: absolute; left: 50%; bottom: var(--s-2); transform: translateX(-50%); z-index: 2;
  display: inline-flex; align-items: center; gap: 6px; min-height: 32px; padding: 0 14px;
  border-radius: var(--r-pill); border: 1px solid var(--chat-lamp-line);
  background: var(--glass-strong); color: var(--lamp-soft); font-size: var(--fs-xs); font-weight: 500;
  box-shadow: var(--shadow), 0 0 18px color-mix(in srgb, var(--lamp) 18%, transparent);
  animation: chat-bloom var(--t-med) var(--ease);
}
.chat-pill svg { width: 14px; height: 14px; }
.chat-pill:hover { border-color: var(--lamp); }

/* typing */
.chat-typing {
  display: flex; align-items: center; gap: var(--s-2); min-height: 16px; padding: 0 4px;
  font-size: var(--fs-xs); color: var(--text-faint); opacity: 0; transition: opacity var(--t-med) var(--ease);
  flex: none;
}
.chat-typing.is-on { opacity: 1; }
.chat-dots { display: inline-flex; gap: 3px; align-items: center; }
.chat-dots i { width: 4px; height: 4px; border-radius: 50%; background: currentColor; animation: chat-dot 1.3s var(--ease-in-out) infinite; }
.chat-dots i:nth-child(2) { animation-delay: 0.18s; }
.chat-dots i:nth-child(3) { animation-delay: 0.36s; }
@keyframes chat-dot { 0%, 70%, 100% { opacity: 0.25; transform: none; } 35% { opacity: 1; transform: translateY(-2px); } }

/* whisper banner */
.chat-whisper {
  display: flex; align-items: center; gap: var(--s-2); flex: none;
  padding: 8px 12px; border-radius: var(--r-md);
  background: color-mix(in srgb, var(--lamp) 7%, transparent); border: 1px solid color-mix(in srgb, var(--lamp) 20%, transparent);
  color: var(--lamp-soft); font-size: var(--fs-xs); line-height: 1.35;
  animation: chat-bloom var(--t-slow) var(--ease);
}
.chat-whisper strong { font-weight: 600; }
.chat-whisper-icon { width: 16px; height: 16px; flex: none; }

/* composer */
.chat-composer { display: flex; align-items: flex-end; gap: var(--s-2); flex: none; }
.chat-input { flex: 1; min-width: 0; min-height: 44px; }
.chat-send { flex: none; }
.chat-composer:has(.input:not(:placeholder-shown)) .chat-send:not(.is-stop) {
  background: var(--lamp); color: var(--ink-900); border-color: transparent; box-shadow: var(--shadow-lamp);
}
.chat-hint { margin: -2px 4px 0; min-height: 1.1em; font-size: var(--fs-xs); color: var(--text-faint); flex: none; }
.chat-hint:empty { min-height: 0; margin: 0; }

/* ======================= OWL ======================= */
.owl-head { display: flex; align-items: center; gap: var(--s-3); padding: 0 2px var(--s-2); border-bottom: 1px solid var(--glass-line); flex: none; }
.owl-avatar-wrap { width: 42px; height: 42px; flex: none; }
.owl-id { display: flex; flex-direction: column; min-width: 0; }
.owl-name { font-family: var(--font-display); font-variation-settings: 'SOFT' 100; font-size: var(--fs-lg); line-height: 1.1; }
.owl-status { font-size: var(--fs-xs); color: var(--text-faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; transition: color var(--t-med); }
.owl-head:has([data-state='thinking']) .owl-status,
.owl-head:has([data-state='writing']) .owl-status { color: var(--lamp-soft); }
.owl-new { margin-left: auto; display: inline-flex; align-items: center; gap: 6px; min-height: 36px; flex: none; }
.owl-new svg { width: 15px; height: 15px; }

/* the owl itself */
.owl-svg { width: 100%; height: 100%; overflow: visible; }
.owl-glow { fill: var(--lamp-glow); opacity: 0.5; }
.owl-body { fill: var(--ink-600); stroke: color-mix(in srgb, var(--lamp-soft) 35%, transparent); stroke-width: 1.2; stroke-linejoin: round; }
.owl-belly { fill: var(--ink-700); }
.owl-feather { fill: none; stroke: color-mix(in srgb, var(--lamp-soft) 45%, transparent); stroke-width: 1.2; stroke-linecap: round; stroke-linejoin: round; }
.owl-ring { fill: var(--text); }
.owl-iris { fill: var(--lamp); }
.owl-pupil { fill: var(--ink-950); }
.owl-glint { fill: var(--text); }
.owl-beak { fill: var(--lamp-deep); }
.owl-lid {
  fill: var(--ink-600); stroke: var(--ink-950); stroke-width: 0.8;
  transform-box: fill-box; transform-origin: 50% 0; transform: scaleY(0.04);
  transition: transform var(--t-med) var(--ease);
}
.owl-svg:not([data-state]) .owl-lid, .owl-svg[data-state='idle'] .owl-lid, .owl-svg[data-state='writing'] .owl-lid {
  animation: owl-blink 6.5s infinite;
}
.owl-svg[data-state='writing'] .owl-lid { animation-duration: 3.8s; }
.owl-empty-art .owl-lid { animation-delay: 1.7s; }
.owl-svg[data-state='thinking'] .owl-lid { transform: scaleY(0.58); }
.owl-svg[data-state='thinking'] { transform-origin: 50% 90%; animation: owl-sway 2.6s var(--ease-in-out) infinite; }
@keyframes owl-blink { 0%, 92%, 100% { transform: scaleY(0.04); } 95.5% { transform: scaleY(1); } }
@keyframes owl-sway { 0%, 100% { transform: rotate(-3deg); } 50% { transform: rotate(3deg); } }

/* messages */
.owl-msg { display: flex; flex-direction: column; align-items: flex-start; margin-top: var(--s-4); }
.owl-msg:first-child { margin-top: 0; }
.owl-msg--user { align-items: flex-end; }
.owl-meta { display: flex; align-items: center; gap: var(--s-2); padding: 0 4px 4px; }
.owl-msg--user .owl-meta { flex-direction: row-reverse; }
.owl-who { font-size: var(--fs-xs); font-weight: 600; color: var(--text-dim); }
.owl-msg--owl .owl-who { color: var(--lamp-soft); font-family: var(--font-display); font-variation-settings: 'SOFT' 100; font-size: var(--fs-sm); font-weight: 500; }
.owl-tag {
  display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: var(--r-pill);
  border: 1px solid var(--chat-lamp-line); color: var(--lamp-soft); font-size: 11px;
}
.owl-tag svg { width: 12px; height: 12px; }
.owl-body {
  max-width: 100%; padding: 10px 13px; border-radius: 4px 16px 16px 16px;
  background: var(--chat-soft); border: 1px solid var(--glass-line);
  font-size: var(--fs-sm); line-height: 1.55; overflow-wrap: anywhere;
}
.owl-msg--user .owl-body {
  max-width: 88%; white-space: pre-wrap; border-radius: 16px 4px 16px 16px;
  background: var(--chat-lamp-tint); border-color: var(--chat-lamp-line);
}
.owl-msg--error .owl-body { border-color: color-mix(in srgb, var(--danger) 40%, transparent); color: var(--text-dim); }
.owl-dots-wrap { padding: 12px 16px; color: var(--lamp-soft); }
.owl-making { display: flex; align-items: center; gap: var(--s-2); color: var(--lamp-soft); }

/* safe markdown */
.owl-p { margin: 0 0 0.6em; }
.owl-h { margin: 0.2em 0 0.4em; font-weight: 600; color: var(--lamp-soft); }
.owl-body > :last-child { margin-bottom: 0; }
.owl-body strong { font-weight: 600; color: var(--text); }
.owl-body em { font-style: italic; color: var(--lamp-soft); }
.owl-list { margin: 0 0 0.6em; padding-left: 1.25em; }
.owl-list li { margin: 0.2em 0; padding-left: 0.15em; }
.owl-list li::marker { color: var(--lamp); }
.owl-code { font: 0.86em var(--font-mono); padding: 1px 5px; border-radius: 5px; background: var(--chat-well); border: 1px solid var(--glass-line); }
.owl-pre {
  margin: 0 0 0.6em; padding: 10px 12px; border-radius: var(--r-sm); overflow-x: auto;
  background: color-mix(in srgb, var(--ink-950) 70%, transparent); border: 1px solid var(--glass-line);
  font: var(--fs-xs)/1.55 var(--font-mono); white-space: pre;
}
.owl-kicker { margin: 0 0 var(--s-2); }

/* quiz */
.owl-quiz { display: flex; flex-direction: column; gap: var(--s-4); min-width: min(280px, 100%); }
.owl-q { margin: 0; padding: 0; border: 0; min-width: 0; }
.owl-q-text { display: flex; gap: var(--s-2); padding: 0; margin-bottom: var(--s-2); font-weight: 500; line-height: 1.45; }
.owl-q-num {
  flex: none; width: 22px; height: 22px; display: inline-grid; place-items: center; border-radius: 50%;
  font: 500 11px/1 var(--font-mono); color: var(--lamp); border: 1px solid var(--chat-lamp-line);
}
.owl-choices { display: grid; gap: 6px; }
.owl-choice {
  display: flex; align-items: center; gap: 10px; width: 100%; min-height: 44px; padding: 8px 12px;
  text-align: left; border-radius: var(--r-md); border: 1px solid var(--glass-line-strong);
  background: var(--chat-well); color: var(--text); font-size: var(--fs-sm); line-height: 1.35;
  transition: border-color var(--t-fast), background var(--t-med) var(--ease), opacity var(--t-med), box-shadow var(--t-med);
}
.owl-choice:not(:disabled):hover { border-color: var(--chat-lamp-line); background: var(--chat-lamp-tint); }
.owl-choice:disabled { cursor: default; }
.owl-choice-key {
  flex: none; width: 22px; height: 22px; display: inline-grid; place-items: center; border-radius: 50%;
  border: 1px solid var(--glass-line-strong); font: 500 11px/1 var(--font-mono); color: var(--text-dim);
  transition: background var(--t-med), color var(--t-med), border-color var(--t-med);
}
.owl-choice-text { flex: 1; min-width: 0; }
.owl-choice-mark { margin-left: auto; flex: none; display: inline-flex; }
.owl-choice-mark svg { width: 18px; height: 18px; }
.owl-choice:disabled:not(.is-right):not(.is-picked) { opacity: 0.5; }
.owl-choice.is-right {
  border-color: color-mix(in srgb, var(--ok) 60%, transparent); background: color-mix(in srgb, var(--ok) 12%, transparent);
  box-shadow: 0 0 18px color-mix(in srgb, var(--ok) 14%, transparent);
}
.owl-choice.is-right .owl-choice-key { background: var(--ok); border-color: var(--ok); color: var(--ink-900); }
.owl-choice.is-right .owl-choice-mark { color: var(--ok); }
.owl-choice.is-wrong { border-color: color-mix(in srgb, var(--danger) 55%, transparent); background: color-mix(in srgb, var(--danger) 10%, transparent); }
.owl-choice.is-wrong .owl-choice-key { background: var(--danger); border-color: var(--danger); color: var(--ink-900); }
.owl-choice.is-wrong .owl-choice-mark { color: var(--danger); }
.owl-feedback { margin: 6px 4px 0; font-size: var(--fs-xs); color: var(--text-dim); line-height: 1.45; }
.owl-feedback:empty { display: none; }
.owl-feedback.is-right strong { color: var(--ok); }
.owl-feedback.is-wrong strong { color: var(--danger); }
.owl-feedback:not(:empty) { animation: chat-bloom var(--t-med) var(--ease); }
.owl-score { margin: 0; padding-top: var(--s-2); border-top: 1px solid var(--glass-line); font-size: var(--fs-sm); color: var(--text-dim); }
.owl-score strong { font-family: var(--font-mono); font-variant-numeric: tabular-nums; color: var(--lamp); }
.owl-again {
  margin-left: 2px; padding: 2px 10px; min-height: 28px; border-radius: var(--r-pill);
  border: 1px solid var(--chat-lamp-line); background: transparent; color: var(--lamp-soft); font-size: var(--fs-xs);
}
.owl-again:hover { background: var(--chat-lamp-tint); }

/* flashcards */
.owl-deck { min-width: min(280px, 100%); }
.owl-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); gap: 8px; }
.owl-card { position: relative; height: 128px; padding: 0; border: 0; background: none; color: inherit; perspective: 900px; border-radius: var(--r-md); }
.owl-card-inner {
  position: absolute; inset: 0; display: block; transform-style: preserve-3d;
  transition: transform var(--t-slow) var(--ease);
}
.owl-card[aria-pressed='true'] .owl-card-inner { transform: rotateY(180deg); }
.owl-card-face {
  position: absolute; inset: 0; display: grid; place-items: center; padding: 22px 12px 12px;
  border-radius: var(--r-md); border: 1px solid var(--glass-line-strong);
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  font-size: var(--fs-sm); line-height: 1.35; text-align: center; overflow: hidden;
  transition: opacity var(--t-med) var(--ease), border-color var(--t-fast);
}
.owl-card-front { background: color-mix(in srgb, var(--ink-800) 85%, transparent); font-weight: 500; }
.owl-card-back {
  transform: rotateY(180deg); background: color-mix(in srgb, var(--lamp) 12%, var(--ink-850));
  border-color: var(--chat-lamp-line); color: var(--text); font-size: var(--fs-xs); padding-top: 12px;
  box-shadow: inset 0 0 30px color-mix(in srgb, var(--lamp) 10%, transparent);
}
.owl-card:hover .owl-card-front { border-color: var(--chat-lamp-line); }
.owl-card-n { position: absolute; top: 7px; left: 9px; font: 400 10px var(--font-mono); color: var(--text-faint); }
.owl-deck-tip { margin: var(--s-2) 2px 0; font-size: var(--fs-xs); color: var(--text-faint); }

/* empty state */
.owl-empty { display: flex; flex-direction: column; align-items: center; text-align: center; padding: var(--s-5) var(--s-2) var(--s-3); }
.owl-empty-art { width: 84px; height: 84px; margin-bottom: var(--s-3); }
.owl-empty-title { margin: 0; font-family: var(--font-display); font-variation-settings: 'SOFT' 100; font-size: var(--fs-xl); line-height: 1.15; }
.owl-empty-sub { margin: var(--s-2) 0 var(--s-4); max-width: 30ch; font-size: var(--fs-sm); color: var(--text-dim); }
.owl-starters { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
.owl-starter {
  min-height: 36px; padding: 0 12px; border-radius: var(--r-pill); border: 1px dashed var(--glass-line-strong);
  background: transparent; color: var(--text-dim); font-size: var(--fs-xs);
  transition: color var(--t-fast), border-color var(--t-fast), background var(--t-fast);
}
.owl-starter:hover { color: var(--lamp-soft); border-color: var(--chat-lamp-line); border-style: solid; background: var(--chat-lamp-tint); }

/* mode chips */
.owl-chips {
  display: flex; gap: 6px; overflow-x: auto; flex: none; padding: 2px 1px 4px; margin: 0 -1px;
  scrollbar-width: none; scroll-snap-type: x proximity;
  -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 18px), transparent);
          mask-image: linear-gradient(to right, #000 calc(100% - 18px), transparent);
}
.owl-chips::-webkit-scrollbar { display: none; }
.owl-chip {
  display: inline-flex; align-items: center; gap: 6px; flex: none; min-height: 34px; padding: 0 12px;
  border-radius: var(--r-pill); border: 1px solid var(--glass-line-strong); background: transparent;
  color: var(--text-dim); font-size: var(--fs-xs); white-space: nowrap; scroll-snap-align: start;
  transition: background var(--t-med) var(--ease), color var(--t-fast), border-color var(--t-fast), box-shadow var(--t-med);
}
.owl-chip svg { width: 14px; height: 14px; flex: none; }
.owl-chip:hover { color: var(--text); background: var(--chat-soft); }
.owl-chip[aria-pressed='true'] { background: var(--lamp); border-color: var(--lamp); color: var(--ink-900); font-weight: 600; box-shadow: 0 0 18px var(--lamp-glow); }

/* owl composer */
.owl-input { resize: none; min-height: 44px; max-height: 140px; line-height: 1.4; padding-top: 11px; padding-bottom: 11px; overflow-y: auto; }
.owl-send.is-stop { background: color-mix(in srgb, var(--rose) 16%, transparent); border-color: color-mix(in srgb, var(--rose) 50%, transparent); color: var(--rose); box-shadow: 0 0 16px var(--rose-glow); }

/* ---------- phones: the card stacks below the desks, so give it a fixed, scrollable height ---------- */
@media (max-width: 900px) {
  .chat-root { height: min(78dvh, 700px); }
}
@media (max-width: 420px) {
  .chat-root { padding: var(--s-2); }
  .chat-line { max-width: 96%; }
  .owl-new span { display: none; }
  .owl-new { padding: 0; width: 36px; justify-content: center; }
  .owl-cards { grid-template-columns: 1fr 1fr; }
}

/* ---------- reduced motion: no blinking, swaying or 3D flips (cards cross-fade instead) ---------- */
@media (prefers-reduced-motion: reduce) {
  .owl-svg, .owl-svg .owl-lid, .chat-dots i { animation: none !important; }
  .owl-card-inner, .owl-card[aria-pressed='true'] .owl-card-inner { transform: none; transition: none; }
  .owl-card-back { transform: none; opacity: 0; }
  .owl-card[aria-pressed='true'] .owl-card-back { opacity: 1; }
  .owl-card[aria-pressed='true'] .owl-card-front { opacity: 0; }
}
