/* chat: shared styles; retain order with the other component sheets. */
@layer components {
/* ---------- 9. 對話區 ---------- */
.chat-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  
  
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.field {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

.field.grow {
  flex: 1 1 180px;
  min-width: 0;
}

.field > span {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap;
}

.chat-head .field > span {
  
  font-weight: 500;
}

.field select,
.field input,
textarea {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  min-height: 38px;
  min-width: 0;
}

.field select:focus,
.field input:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.field.grow select {
  flex: 1;
}

.chat-model {
  align-self: center;
  white-space: nowrap;
  max-width: 100%;
  margin-left: auto;
  overflow: hidden;
  text-overflow: ellipsis;
}

.usage-remaining {
  align-self: center;
  white-space: nowrap;
}

.chat-history {
  position: relative;
  display: flex;
  flex: 1;
  min-height: 0;
  min-width: 0;
}

.question-nav {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  max-height: 80%;
  width: 32px;
  z-index: 2;
}

.question-nav[hidden] { display: none; }

.question-nav::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 32px;
  height: 64px;
  border-radius: 50%;
  background: radial-gradient(ellipse, color-mix(in srgb, var(--accent) 6%, transparent), transparent 70%);
  transform: translateY(calc(var(--nav-pointer-y, 0px) - 50%));
  opacity: 0;
  pointer-events: none;
  transition: opacity 250ms ease;
}

.question-nav.pointer-active::before { opacity: 1; }

.question-nav-rail {
  display: flex;
  flex-direction: column;
  max-height: min(50vh, 400px);
  overflow-y: auto;
  scrollbar-width: none;
}

.question-nav-rail::-webkit-scrollbar { display: none; }

.question-nav-item {
  position: relative;
  flex: 0 0 20px;
  width: 32px;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
  --nav-color: var(--muted);
}

.question-nav-item::before {
  content: '';
  display: block;
  width: 24px;
  height: 2px;
  margin: auto 4px auto auto;
  background: var(--nav-color);
  opacity: max(var(--nav-base-opacity, .4), var(--nav-opacity, .4));
  border-radius: 2px;
  transform-origin: right center;
  transform: translate(var(--nav-shift, 0px), var(--nav-lift, 0px)) scaleX(max(var(--nav-base-scale, .58), var(--nav-scale, .58)));
  transition: transform 250ms cubic-bezier(.16,1,.3,1), opacity 180ms ease, background-color 160ms ease;
}

.question-nav.pointer-active .question-nav-item::before { transition: opacity 100ms ease, background-color 160ms ease; }

.question-nav-item[aria-current="true"]::before,
.question-nav-item:focus-visible::before {
  --nav-base-scale: 1;
  --nav-base-opacity: 1;
  --nav-color: var(--accent);
}

.question-nav-item[data-bookmarked="true"]::before {
  --nav-base-opacity: 1;
  --nav-base-scale: 1;
  --nav-color: #465563;
  height: 4px;
}

.question-nav::after {
  content: '';
  position: absolute;
  left: var(--nav-pulse-x, 16px);
  top: var(--nav-pulse-y, 0px);
  width: 18px;
  height: 18px;
  box-sizing: border-box;
  border: 1px solid color-mix(in srgb, var(--accent) 55%, transparent);
  border-radius: 50%;
  transform: translate(-50%, -50%) scale(.45);
  opacity: 0;
  pointer-events: none;
}

.question-nav.nav-ripple::after { animation: question-nav-pulse 360ms cubic-bezier(.16,1,.3,1); }
.question-nav-item.nav-pulse::before { animation: question-nav-press 360ms cubic-bezier(.16,1,.3,1); }

@keyframes question-nav-pulse {
  0% { opacity: .45; transform: translate(-50%, -50%) scale(.45); }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(1.4); }
}

@keyframes question-nav-press {
  0% { scale: 1; }
  20% { scale: .78 1; }
  55% { scale: 1.08 1; }
  100% { scale: 1; }
}

.question-nav-preview {
  visibility: hidden;
  opacity: 0;
  position: absolute;
  right: 36px;
  top: 0;
  width: min(260px, calc(100vw - 90px));
  padding: 12px;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 12px #0000000d;
  text-align: left;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: 13px;
  line-height: 1.5;
  max-height: 180px;
  overflow: hidden;
  pointer-events: none;
  transform: translateY(calc(var(--preview-y, 0px) - 50%)) translateX(6px) rotate(var(--preview-tilt, 0deg)) scale(.97);
  transform-origin: right center;
  transition: transform 180ms cubic-bezier(.2,.8,.2,1), opacity 120ms ease, visibility 0s linear 180ms;
}

.question-nav-preview.visible {
  visibility: visible;
  opacity: 1;
  transform: translateY(calc(var(--preview-y, 0px) - 50%)) translateX(0) rotate(var(--preview-tilt, 0deg)) scale(1);
  transition-delay: 0s;
}

.question-nav.pointer-active .question-nav-preview.visible { transition: opacity 120ms ease; }

@media (prefers-reduced-motion: reduce) {
  .question-nav::before, .question-nav::after { display: none; }
  .question-nav-item::before, .question-nav-preview { transition: none !important; animation: none !important; }
}

.messages {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: auto;
  
  display: flex;
  flex-direction: column;
  gap: 14px;
  background: var(--panel);
}

.messages:empty::before {
  content: var(--ui-start-explanation, "點擊 PDF 上的任一題開始講解");
  margin: auto;
  color: var(--muted);
  font-size: 14px;
}

.conv {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  max-width: 100%;
  font-size: 15px;
  line-height: 1.7;
  animation: fade-in var(--dur) var(--ease);
}

/* 使用者：淡底、1px 邊線、右對齊 */
.msg.user {
  align-self: flex-end;
  max-width: 80%;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  
  padding: 9px 14px;
  white-space: pre-wrap;
}

/* 助理：文件式，無氣泡 */
.msg.model {
  font-family: var(--font-sans);
  align-self: flex-start;
  width: 100%;
  background: none;
}

.msg.model .msg-text {
  font-size: var(--answer-font-size, 15px);
}

.msg.model .msg-text.markdown pre {
  font-size: calc(var(--answer-font-size, 15px) * 13 / 15);
}

.msg.model .msg-text.markdown table {
  font-size: calc(var(--answer-font-size, 15px) * 14 / 15);
}

.msg.notice {
  align-self: center;
  color: var(--muted);
  font-size: 13px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 4px 12px;
}

.msg-text.markdown > :first-child {
  margin-top: 0;
}

.msg-text.markdown > :last-child {
  margin-bottom: 0;
}

.msg-text.markdown p {
  margin: 0 0 0.9em;
}

.msg-text.markdown h1,
.msg-text.markdown h2,
.msg-text.markdown h3,
.msg-text.markdown h4 {
  font-weight: 600;
  line-height: 1.4;
  margin: 1.1em 0 0.5em;
}

.msg-text.markdown h1,
.msg-text.markdown h2,
.msg-text.markdown h3 {
  font-family: var(--font-serif);
}

.msg-text.markdown ul,
.msg-text.markdown ol {
  margin: 0.5em 0 0.9em;
  padding-left: 1.5em;
}

.msg-text.markdown li {
  margin: 0.25em 0;
}

.msg-text.markdown blockquote {
  margin: 0.9em 0;
  padding: 0.4em 1em;
  
  color: var(--muted);
}

.msg-text.markdown pre {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  overflow: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.6;
}

.msg-text.markdown code {
  background: var(--accent-soft);
  color: var(--text);
  padding: 1px 5px;
  border-radius: var(--radius-sm);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.92em;
}

.msg-text.markdown pre code {
  background: transparent;
  padding: 0;
}

.msg-text.markdown table {
  border-collapse: collapse;
  margin: 0.9em 0;
  width: 100%;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

.msg-text.markdown th,
.msg-text.markdown td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}

.msg-text.markdown th {
  background: var(--panel-2);
  font-weight: 600;
}

.msg-text.markdown a {
  color: var(--accent);
}

.msg-thumbs {
  display: flex;
  gap: 6px;
  margin-top: 6px;
  flex-wrap: wrap;
}

.msg-thumbs img {
  max-width: 120px;
  max-height: 120px;
  width: auto;
  height: auto;
  align-self: flex-start;
  object-fit: contain;
  background: var(--panel-2);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  cursor: zoom-in;
}

.thinking {
  color: var(--muted);
  animation: soft-blink 1.2s ease-in-out infinite;
}

.cursor {
  animation: soft-blink 1.2s ease-in-out infinite;
  color: var(--accent);
}

.msg-note {
  color: var(--muted);
  font-size: 12px;
}

.msg-model-switch {
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 4px;
}

.msg-error {
  margin-top: 6px;
  padding: 10px 12px;
  border: 1px solid var(--danger-soft);
  background: var(--danger-soft);
  color: var(--danger);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  font-size: 13px;
}

.msg-error-text {
  flex: 1;
  min-width: 140px;
}

.hint-more-btn {
  align-self: flex-start;
  margin: 2px 0 6px;
  padding: 6px 12px;
  font-size: 13px;
  color: var(--accent);
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease);
}

.hint-more-btn:hover {
  background: var(--accent);
  color: var(--on-ink);
}

.hint-more-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* 提示模式的兩級提示：較輕的「給我一點提示」在前，用描邊；「寫出下一步」維持淡底 */
.hint-actions {
  display: grid;
  grid-template-columns: repeat(2, max-content);
  gap: 8px;
  align-self: flex-start;
  margin: 2px 0 6px;
}

.hint-actions .hint-more-btn {
  margin: 0;
}

.hint-more-btn.is-full-solution {
  grid-column: 1;
  justify-self: start;
}

.hint-more-btn.is-nudge {
  background: transparent;
}

.hint-more-btn.is-nudge:hover {
  background: var(--accent-soft);
  color: var(--accent);
}

.msg-error-detail {
  flex-basis: 100%;
  color: var(--muted);
  font-size: 12px;
}

/* ---------- 10. 輸入區 ---------- */
.ask-form {

  display: flex;
  flex-direction: column;
  
  
  border: 1px solid var(--border);
  
  transition: border-color var(--dur-fast) var(--ease);
}

.math-input-help { margin: 0;  line-height: 1.6; }
.math-editor-status { margin: 0; }
.math-editor-status:empty { display: none; }
.math-chat-editor {
  
  max-height: 220px;
  overflow: auto;
  outline: none;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.8;
  padding: 6px 2px;
  cursor: text;
  font-size: 15px;
}
.math-chat-editor[data-empty="true"]::before {
  content: attr(data-placeholder);
  color: var(--muted);
  pointer-events: none;
}
.math-atom {
  display: inline-block;
  vertical-align: middle;
  max-width: 100%;
  overflow-x: auto;
  padding: 2px 5px;
  margin: 0 2px;
  border-radius: 4px;
  cursor: text;
  white-space: normal;
}
.math-atom:hover, .math-atom:focus { background: var(--bg); outline: 1px solid var(--border); }
.math-entry {
  border-bottom: 1px solid var(--muted);
  background: var(--bg);
  font-family: var(--font-ui);
  padding: 2px 4px;
  border-radius: 3px;
}
.math-chat-editor .katex { font-size: 1.2em; }

.ask-form textarea {
  resize: vertical;
  width: 100%;
  min-height: 44px;
  border: none;
  background: transparent;
  padding: 4px 2px;
}

.ask-form textarea:focus {
  border-color: transparent;
}

.ask-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.ask-actions .muted {
  margin-right: auto;
}

#send-btn {

  flex: none;
}

#send-btn svg {
  width: 16px;
  height: 16px;
}

#send-btn:disabled {
  background: var(--border-strong);
  border-color: var(--border-strong);
  color: var(--muted);
  opacity: 1;
}

/* ---------- 10.1 附件（學生解答照片） ---------- */
.attach-btn {
  flex: none;
  font-size: 16px;
  line-height: 1;
  padding: 8px 10px;
}

.attach-btn:disabled {
  opacity: 0.5;
}

.attach-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 2px 0 4px;
}

.attach-thumb {
  position: relative;
  flex: none;
  line-height: 0;
}

.attach-thumb img {
  width: 64px;
  height: 64px;
  object-fit: cover;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: zoom-in;
}

.attach-thumb img:hover {
  border-color: var(--accent);
}

.attach-remove {
  position: absolute;
  top: -7px;
  right: -7px;
  width: 20px;
  height: 20px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  line-height: 1;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border-strong);
  border-radius: 50%;
  cursor: pointer;
}

.attach-remove:hover {
  color: var(--danger);
  border-color: var(--danger);
}

/* 拖曳圖片到輸入框時把框線點亮 */
.ask-form.drop-active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.attach-dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 10px;
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
  max-width: calc(100vw - 24px);
  max-height: calc(100dvh - 24px);
}

.attach-dialog::backdrop {
  background: var(--backdrop);
}

.attach-full {
  display: block;
  max-width: min(92vw, 900px);
  max-height: calc(100dvh - 96px);
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: var(--radius-sm);
  cursor: zoom-out;
}

.attach-close {
  display: block;
  margin: 8px 0 0 auto;
}

.banner.error {
  background: var(--danger-soft);
  color: var(--danger);
  border-bottom: 1px solid var(--danger-soft);
  padding: 8px 12px;
  font-size: 13px;
}

}
