/* The helper uses the same per-textbook palette as the reader. */
.reader-shell { position: relative; }
.reader-shell.chat-desktop.chat-open { grid-template-columns: 265px minmax(420px,1fr) 8px var(--topic-chat-width,360px); }
.topic-chat { min-width: 0; min-height: 0; overflow: hidden; display: flex; flex-direction: column; background: var(--surface); color: var(--fg); border-left: 1px solid var(--border); }
.topic-chat-header { display: flex; align-items: center; gap: 10px; padding: 18px 18px 13px; flex: none; }
.topic-chat-header h2 { margin: 0; font-size: 15px; letter-spacing: -.025em; line-height: 1.3; }
.topic-chat-header p { margin: 3px 0 0; color: var(--muted); font-size: 10px; }
.topic-chat-symbol { width: 31px; height: 31px; display: grid; place-items: center; flex: none; border: 1px solid var(--accent-border,var(--border)); color: var(--accent); background: var(--accent-subtle,var(--surface-2)); border-radius: 10px; font-size: 20px; }
.topic-chat-icon { width: 30px; height: 30px; margin-left: auto; flex: none; border: 0; background: transparent; color: var(--muted); font-size: 24px; line-height: 1; border-radius: 7px; }
.topic-chat-icon:hover { color: var(--fg); background: var(--surface-2); }
.topic-chat-topic { margin: 0 18px; padding: 0 0 13px; border-bottom: 1px solid var(--border); font-size: 11px; line-height: 1.5; flex: none; }
.topic-chat-topic span { display: block; color: var(--muted); font-size: 9px; letter-spacing: .05em; text-transform: uppercase; margin-bottom: 3px; }
.topic-chat-topic strong { font-weight: 500; color: var(--fg-soft); display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden; overflow-wrap: anywhere; }
.topic-chat-history { flex: 1; min-height: 80px; overflow-y: auto; overscroll-behavior: contain; padding: 20px 17px; scrollbar-width: thin; }
.topic-chat-history-note { color: var(--muted); font-size: 12px; }
.topic-chat-empty { text-align: center; padding: clamp(14px,5vh,65px) 9px 20px; }
.topic-chat-empty-symbol { display: inline-grid; place-items: center; width: 48px; height: 48px; border-radius: 17px; background: var(--surface-2); color: var(--accent); font-size: 27px; margin-bottom: 14px; }
.topic-chat-empty h3 { margin: 0 0 10px; font-size: 18px; letter-spacing: -.03em; }
.topic-chat-empty p { font-size: 12px; color: var(--muted); line-height: 1.8; margin: 0 auto; max-width: 260px; }
.topic-chat-message { min-width: 0; margin: 0 0 22px; }
.topic-chat-message:last-child { margin-bottom: 2px; }
.topic-chat-message-meta { color: var(--muted); font-size: 10px; margin-bottom: 7px; }
.topic-chat-message.is-assistant .topic-chat-message-meta { color: var(--accent); }
.topic-chat-message.is-user { padding: 12px 14px; border: 1px solid var(--border); border-radius: 12px 12px 3px 12px; background: var(--surface-2); }
.topic-chat-message-text { font-size: 13px; line-height: 1.75; overflow-wrap: anywhere; }
.topic-chat-message-text p { margin: 0 0 10px; white-space: pre-wrap; }
.topic-chat-message-text :last-child { margin-bottom: 0; }
.topic-chat-message-text h4 { font-size: 13px; line-height: 1.5; margin: 15px 0 7px; }
.topic-chat-message-text h4:first-child { margin-top: 0; }
.topic-chat-message-text ul { padding-left: 19px; margin: 10px 0; }
.topic-chat-message-text li { margin: 6px 0; }
.topic-chat-message-text code { font-size: 12px; padding: 1px 4px; border: 1px solid var(--border); border-radius: 4px; background: var(--bg-soft); white-space: pre-wrap; }
.topic-chat-message-text pre { max-width: 100%; white-space: pre-wrap; overflow-wrap: anywhere; padding: 12px; background: var(--bg-soft); border: 1px solid var(--border); border-radius: 8px; font-size: 12px; }
.topic-chat-message-quote { margin: 0 0 10px; padding: 2px 0 2px 10px; border-left: 2px solid var(--accent); font-size: 11px; color: var(--muted); overflow-wrap: anywhere; }
.topic-chat-message-quote small { display: block; margin-bottom: 4px; font-size: 9px; color: var(--accent); }
.topic-chat-message-quote p { margin: 0; line-height: 1.6; max-height: 150px; overflow: auto; }
.topic-chat-message-failed { display: flex; flex-wrap: wrap; gap: 8px; color: var(--muted); font-size: 11px; margin-top: 8px; }
.topic-chat-message-failed button, .topic-chat-error button { border: 0; padding: 0; background: transparent; color: var(--accent); text-decoration: underline; font-size: inherit; }
.topic-chat-pending { display: flex; align-items: center; gap: 8px; color: var(--muted); padding: 6px 18px 9px; font-size: 11px; flex: none; }
.topic-chat-dots { font-size: 17px; letter-spacing: 3px; color: var(--accent); }
.topic-chat-error { margin: 0 15px 10px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 9px; color: var(--warn); font-size: 11px; line-height: 1.65; flex: none; overflow-wrap: anywhere; }
.topic-chat-error button { margin-left: 6px; }
.topic-chat-chips { flex: none; display: flex; flex-wrap: wrap; gap: 5px; padding: 8px 15px 10px; }
.topic-chat-chips button { padding: 5px 8px; border: 1px solid var(--border); border-radius: 15px; background: transparent; color: var(--fg-soft); font-size: 10px; line-height: 1.4; }
.topic-chat-chips button:hover { border-color: var(--accent); color: var(--accent); }
.topic-chat-composer { padding: 0 15px 12px; flex: none; min-width: 0; }
.topic-chat-message-label { display: block; font-size: 10px; color: var(--muted); margin-bottom: 7px; }
.topic-chat-composer textarea { width: 100%; display: block; padding: 11px 12px; min-height: 74px; max-height: min(220px,25dvh); resize: vertical; border: 1px solid var(--border-strong); border-radius: 11px; background: var(--bg-soft); color: var(--fg); font: inherit; font-size: 12px; line-height: 1.7; outline-offset: 2px; }
.topic-chat-composer textarea::placeholder { color: var(--muted); }
.topic-chat-composer-actions { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 8px; }
.topic-chat-composer-actions label { min-width: 0; flex: 1; }
.topic-chat-composer-actions select { width: 100%; max-width: 170px; min-width: 0; color: var(--muted); background: var(--surface); font-size: 10px; border: 0; padding: 6px 0; }
.topic-chat-composer-actions .btn { font-size: 11px; padding: 7px 11px; min-height: 34px; white-space: nowrap; }
.topic-chat-key-hint { margin: 8px 0 0; color: var(--muted); font-size: 9px; }
.topic-chat-quote-draft { display: flex; align-items: flex-start; gap: 9px; margin-bottom: 9px; padding: 9px 11px; border-left: 2px solid var(--accent); border-radius: 0 8px 8px 0; background: var(--surface-2); }
.topic-chat-quote-draft > div { min-width: 0; flex: 1; }
.topic-chat-quote-draft small { color: var(--accent); font-size: 9px; }
.topic-chat-quote-draft p { margin: 4px 0 0; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden; overflow-wrap: anywhere; font-size: 11px; line-height: 1.6; color: var(--fg-soft); }
.topic-chat-quote-draft button { flex: none; color: var(--muted); border: 0; padding: 0 3px; background: transparent; font-size: 19px; }
.topic-chat-resizer { width: 8px; align-self: stretch; cursor: col-resize; background: var(--bg); position: relative; touch-action: none; outline-offset: -2px !important; }
.topic-chat-resizer::after { content: ''; position: absolute; width: 2px; border-radius: 2px; height: 44px; top: calc(50% - 22px); left: 3px; background: var(--border-strong); }
.topic-chat-resizer:hover::after, .topic-chat-resizer:focus-visible::after { background: var(--accent); }
.topic-chat-resizing { cursor: col-resize !important; user-select: none !important; }
.topic-chat-tools { display: flex; gap: 8px; align-items: center; flex: none; margin-left: auto; }
.reader-heading .reader-summary + .reader-menu-toggle + .topic-chat-tools { margin-left: 0; }
.topic-chat-tools .portal-icon-button { min-height: 33px; padding: 6px 9px; font-size: 11px; }
.topic-chat-toggle.is-open { border-color: var(--accent-border,var(--border-strong)); color: var(--accent); background: var(--accent-subtle,var(--surface-2)); }
.topic-chat-explain { color: var(--accent); }
.topic-chat-backdrop { position: fixed; inset: 0; border: 0; border-radius: 0; background: rgb(0 0 0 / 54%); z-index: 110; }
.reader-shell:not(.chat-desktop) .topic-chat { position: fixed; z-index: 111; inset: 12px 12px 12px auto; width: min(440px,calc(100vw - 24px)); max-height: calc(100dvh - 24px); border: 1px solid var(--border-strong); border-radius: 17px; box-shadow: var(--shadow); }
.topic-chat-context-menu { position: fixed; z-index: 210; width: min(260px,calc(100vw - 16px)); max-height: calc(100dvh - 16px); overflow-y: auto; overscroll-behavior: contain; padding: 5px; border: 1px solid var(--border-strong); border-radius: 10px; background: var(--surface); box-shadow: var(--shadow); color: var(--fg); }
.topic-chat-context-menu button { display: block; width: 100%; text-align: left; background: transparent; color: var(--fg); padding: 11px 12px; min-height: 42px; border: 0; border-radius: 6px; font-size: 13px; }
.topic-chat-context-menu button:hover, .topic-chat-context-menu button:focus-visible { background: var(--surface-2); color: var(--accent); outline: 2px solid var(--accent); outline-offset: -2px; }
@media (max-width: 1100px) { .reader-heading .reader-summary { display: none; } }
@media (max-width: 860px) {
  .reader-heading { flex-wrap: wrap; gap: 8px; }
  .reader-heading > div:not(.reader-summary):not(.topic-chat-tools) { flex: 1; min-width: 90px; }
  .reader-heading .topic-chat-tools { margin-left: 0; }
  .topic-chat-tools .portal-icon-button { min-height: 34px; }
  .topic-chat-key-hint { display: none; }
  .topic-chat-composer { padding-bottom: max(13px,env(safe-area-inset-bottom)); }
  .topic-chat-message-text { font-size: 14px; }
}
@media (max-width: 480px) {
  .reader-heading .topic-chat-tools { width: 100%; justify-content: flex-end; }
  .reader-heading .topic-chat-tools .topic-chat-toggle { margin-left: auto; }
  .topic-chat-header { padding: 15px 15px 12px; }
  .topic-chat-history { padding: 16px 14px; }
}


/* Fold complete exchanges without removing any messages from topic memory. */
.topic-chat-turn { margin: 0 0 10px; min-width: 0; }
.topic-chat-turn-summary { display: flex; align-items: flex-start; gap: 8px; padding: 10px 9px; color: var(--fg-soft); background: var(--surface-2); border: 1px solid var(--border); border-radius: 9px; cursor: pointer; list-style: none; font-size: 11px; line-height: 1.6; }
.topic-chat-turn-summary::-webkit-details-marker { display: none; }
.topic-chat-turn-summary:hover, .topic-chat-turn-summary:focus-visible { color: var(--accent); border-color: var(--accent); }
.topic-chat-turn-title { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden; overflow-wrap: anywhere; min-width: 0; }
.topic-chat-turn-chevron { flex: none; font-size: 19px; line-height: 17px; transform-origin: center; margin-top: 1px; }
.topic-chat-turn[open] > .topic-chat-turn-summary .topic-chat-turn-chevron { transform: rotate(90deg); }
.topic-chat-turn[open] > .topic-chat-turn-summary { background: transparent; color: var(--muted); }
.topic-chat-turn-body { padding: 14px 0 12px; }

.topic-chat-attachment-bar { display: flex; align-items: center; gap: 9px; margin: 0 0 8px; }
.topic-chat-attachment-bar button { border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px; background: var(--surface-2); color: var(--fg-soft); font-size: 11px; }
.topic-chat-attachment-bar span { font-size: 9px; color: var(--muted); }
.topic-chat-photo-draft { display: flex; align-items: center; gap: 10px; margin-bottom: 9px; padding: 7px; border: 1px solid var(--border); border-radius: 9px; font-size: 11px; }
.topic-chat-photo-draft img { width: 62px; height: 62px; object-fit: contain; background: #fff; border-radius: 5px; }
.topic-chat-photo-draft button { margin-left: auto; border: 0; background: transparent; color: var(--muted); font-size: 22px; }
.topic-chat-photo { margin-bottom: 12px; }
.topic-chat-photo img { display: block; width: 100%; height: 150px; object-fit: contain; border: 1px solid var(--border); background: #fff; border-radius: 8px; }
.topic-chat-photo button { margin-top: 6px; border: 0; padding: 3px 0; background: transparent; color: var(--muted); text-decoration: underline; font-size: 10px; }
