/* QUANTA -- Physics Tutor */

:root {
    --bg: #f7f9fb;
    --bg-secondary: #eef2f6;
    --text: #1c2530;
    --text-muted: #5a6573;   /* >= 4.5:1 on --bg and #fff (WCAG AA) */
    --border: #d9e0e8;
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --header-bg: #1e3a5f;
    --header-text: #f2f6fb;
    --header-muted: rgba(242, 246, 251, 0.72);
    --header-line: rgba(242, 246, 251, 0.28);
    --success: #16a34a;
    --warning: #d97706;
    --error: #dc2626;
    --canvas-panel-width: 70%;
    --header-height: 40px;
    --input-height: 64px;
    --msg-user-bg: #e8f0fe;
    --msg-assistant-bg: #fff;
    --radius: 6px;
    --font-mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* Keyboard focus: one visible ring everywhere (WCAG 2.4.7) */
:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.app-header :focus-visible {
    outline-color: var(--header-text);
}

/* The hidden attribute must win even on elements whose class sets a
   display (e.g. display: flex on .session-banner) -- without this the
   attribute is silently overridden and JS toggling it does nothing. */
[hidden] {
    display: none !important;
}

/* Screen-reader-only content (e.g. the chat live region) */
.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-size: 15px;
    line-height: 1.6;
    color: var(--text);
    background: var(--bg);
    overflow: hidden;
    overscroll-behavior: none;
}

/* Header */
.app-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    background: var(--header-bg);
}

.logo {
    font-weight: 700;
    letter-spacing: 0.06em;
    color: var(--header-text);
    text-decoration: none;
    font-size: 14px;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.course-chip {
    display: inline-flex;
    align-items: baseline;
    gap: 6px;
    padding: 4px 10px;
    border: 1px solid var(--header-line);
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.1);
    font-size: 12px;
    line-height: 1;
    max-width: 60vw;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.course-chip-number {
    font-weight: 600;
    color: var(--header-text);
}

.course-chip-title {
    color: var(--header-text);
    overflow: hidden;
    text-overflow: ellipsis;
}

.course-chip-instructor {
    color: var(--header-muted);
}

.course-chip-switch {
    margin-left: 6px;
    font-size: 11px;
    color: #a8c7ff;
    text-decoration: none;
}

.course-chip-switch:hover {
    text-decoration: underline;
}

/* Picker page */
.picker-page {
    max-width: 900px;
    margin: 0 auto;
    padding: 32px 24px;
    height: 100%;
    overflow-y: auto;
}

.picker-page h1 {
    font-size: 24px;
    margin: 0 0 8px;
}

.picker-page > .muted {
    color: var(--text-muted);
    margin: 0 0 24px;
}

.picker-empty {
    padding: 24px;
    border: 1px dashed var(--border);
    border-radius: var(--radius);
    color: var(--text-muted);
}

.course-list {
    list-style: none;
    margin: 0;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: #fff;
    overflow: hidden;
}

.course-list li + li {
    border-top: 1px solid var(--border);
}

.course-row {
    display: flex;
    align-items: baseline;
    gap: 10px;
    padding: 10px 14px;
    color: var(--text);
    text-decoration: none;
}

.course-row:hover {
    background: var(--bg);
}

.course-row-number {
    font-weight: 600;
    font-size: 13px;
    min-width: 88px;
}

.course-row-title {
    flex: 1;
    font-size: 14px;
}

.course-row-instructor {
    font-size: 13px;
}

.course-row-hidden {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    padding: 1px 6px;
    border: 1px solid var(--border);
    border-radius: 999px;
    color: var(--text-muted);
}

.header-user {
    font-size: 13px;
    color: var(--header-muted);
    padding-right: 8px;
    border-right: 1px solid var(--header-line);
    margin-right: 4px;
    max-width: 18ch;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.login-error {
    color: #c0392b;
    background: #fdecea;
    border: 1px solid #f5b7b1;
    padding: 8px 12px;
    border-radius: var(--radius);
    margin-bottom: 12px;
    font-size: 13px;
}

.header-right {
    display: flex;
    gap: 16px;
}

.header-right a {
    color: var(--header-muted);
    text-decoration: none;
    font-size: 14px;
}

.header-right a:hover {
    color: var(--header-text);
}

.header-btn {
    background: none;
    border: 1px solid var(--header-line);
    border-radius: var(--radius);
    padding: 4px 10px;
    font-size: 13px;
    color: var(--header-muted);
    cursor: pointer;
}

.header-btn:hover {
    color: var(--header-text);
    border-color: var(--header-text);
}

.header-btn.active {
    color: var(--header-text);
    border-color: var(--header-text);
    background: rgba(255, 255, 255, 0.15);
}

/* Main layout */
.app-main {
    height: calc(100vh - var(--header-height));
    height: calc(100dvh - var(--header-height));
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Chat layout */
.chat-layout {
    display: flex;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

/* Slim edge handle: pulls the canvas back in when it is hidden */
.canvas-restore {
    flex-shrink: 0;
    width: 22px;
    border: none;
    border-right: 1px solid var(--border);
    background: var(--bg-secondary);
    color: var(--text-muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.canvas-restore:hover {
    color: var(--text);
    background: var(--border);
}

.canvas-restore[hidden] {
    display: none;
}

/* Chat container */
.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
}

/* Messages */
.messages {
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.message {
    max-width: 720px;
    width: 100%;
    margin: 0 auto;
    padding: 12px 16px;
    border-radius: var(--radius);
    line-height: 1.6;
    min-width: 0;
    flex-shrink: 0;
}

.message.user {
    background: var(--msg-user-bg);
}

.message.assistant {
    background: var(--msg-assistant-bg);
    border: 1px solid var(--border);
    box-shadow: 0 1px 2px rgba(28, 37, 48, 0.05);
}

.message-content p {
    margin-bottom: 8px;
}

.message-content p:last-child {
    margin-bottom: 0;
}

.message-content pre {
    background: var(--bg-secondary);
    padding: 12px;
    border-radius: var(--radius);
    overflow-x: auto;
    font-family: var(--font-mono);
    font-size: 13px;
    margin: 8px 0;
}

.message-content code {
    font-family: var(--font-mono);
    font-size: 13px;
    background: var(--bg-secondary);
    padding: 1px 4px;
    border-radius: 3px;
}

.message-content pre code {
    background: none;
    padding: 0;
}

.message-image {
    max-width: 400px;
    max-height: 300px;
    border-radius: var(--radius);
    margin: 8px 0;
}

/* Streaming indicator */
.streaming-cursor {
    display: inline-block;
    width: 2px;
    height: 1em;
    background: var(--accent);
    margin-left: 2px;
    animation: blink 1s infinite;
}

@keyframes blink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0; }
}

/* Pre-response placeholder: three pulsing dots in the assistant bubble
   until the first token arrives. */
.thinking {
    display: inline-flex;
    gap: 8px;
    align-items: center;
    height: 1.4em;
    color: var(--text-muted);
}

.thinking-label {
    animation: thinking-blink 1.2s infinite ease-in-out;
}

.thinking-dots {
    display: inline-flex;
    gap: 5px;
    align-items: center;
}

.thinking-dots span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--accent);
    opacity: 0.35;
    animation: thinking-pulse 1.2s infinite ease-in-out;
}

.thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes thinking-blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.35; }
}

@keyframes thinking-pulse {
    0%, 80%, 100% { opacity: 0.35; transform: scale(0.85); }
    40% { opacity: 1; transform: scale(1); }
}

@media (prefers-reduced-motion: reduce) {
    .thinking-label { animation: none; }
    .thinking-dots span { animation: none; opacity: 0.6; }
}

.chat-error {
    background: #fee;
    border: 1px solid #f99;
    border-left: 3px solid #c33;
    color: #800;
    padding: 0.5rem 0.75rem;
    border-radius: 4px;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: 0.9rem;
    white-space: pre-wrap;
    word-break: break-word;
}

/* Input area */
.input-area {
    border-top: 1px solid var(--border);
    background: #fff;
    padding: 12px 16px;
}

.upload-preview {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
    padding: 8px;
    background: var(--bg-secondary);
    border-radius: var(--radius);
}

.upload-preview[hidden] {
    display: none;
}

.upload-preview img {
    max-height: 60px;
    border-radius: 4px;
}

.upload-remove {
    background: none;
    border: none;
    font-size: 18px;
    cursor: pointer;
    color: var(--text-muted);
}

.input-form {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    max-width: 720px;
    margin: 0 auto;
}

.upload-btn {
    cursor: pointer;
    color: var(--text-muted);
    padding: 8px;
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.upload-btn:hover {
    background: var(--bg-secondary);
    color: var(--accent);
}

.mic-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    padding: 8px;
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    touch-action: auto;
}

.mic-btn:hover {
    background: var(--bg-secondary);
    color: var(--accent);
}

.mic-btn.recording {
    color: var(--error);
    background: #fef2f2;
    animation: mic-pulse 1.5s infinite;
}

@keyframes mic-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.mic-btn.transcribing {
    color: var(--warning);
    opacity: 0.6;
    pointer-events: none;
}

.mic-status {
    margin: 4px 12px 0;
    font-size: 12px;
    color: var(--text-muted);
}

/* Shown while the chat has no server session: sends are refused and the
   student must know, because a sessionless turn would be unrecorded. */
.session-banner {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin: 8px 12px 0;
    padding: 0.5rem 0.75rem;
    background: #fff7e6;
    border: 1px solid #e6c37a;
    border-left: 3px solid #c77f00;
    border-radius: 4px;
    font-size: 13px;
    color: #7a5200;
}

.session-banner button {
    margin-left: auto;
    flex-shrink: 0;
    padding: 0.25rem 0.75rem;
    border: 1px solid #c77f00;
    background: #fff;
    color: #7a5200;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
}

.feedback-modal .small {
    font-size: 12px;
    line-height: 1.5;
}

/* Long-form notice pages (privacy). Narrower than the app shell: this is
   prose to be read, not an interface to be used. */
.notice-page {
    max-width: 40rem;
    margin: 0 auto;
    padding: 32px 24px 64px;
    height: 100%;
    overflow-y: auto;
    line-height: 1.6;
}

.notice-page h1 { font-size: 24px; margin: 0 0 4px; }
.notice-page h2 { font-size: 17px; margin: 28px 0 8px; }
.notice-page p { margin: 0 0 12px; }

.notice-continue {
    margin-top: 24px;
}

.notice-foot {
    margin-top: 28px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
    font-size: 13px;
}



#message-input {
    flex: 1;
    resize: none;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px 12px;
    font-family: inherit;
    font-size: 15px;
    line-height: 1.5;
    max-height: 200px;
    outline: none;
}

#message-input:focus {
    border-color: var(--accent);
}

.send-btn {
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    padding: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.send-btn:hover {
    background: var(--accent-hover);
}

.send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Buttons */
.btn {
    display: inline-block;
    padding: 8px 16px;
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    border: 1px solid var(--border);
    background: #fff;
    color: var(--text);
    text-decoration: none;
}

.btn:hover {
    background: var(--bg-secondary);
}

.btn-primary {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-secondary {
    font-size: 13px;
    padding: 6px 12px;
}

/* Login. The app shell disables page scrolling (html/body overflow:
   hidden), so the container scrolls itself; auto margins on the card
   center it without clipping the top when it is taller than the
   viewport (flex centering would). */
.login-container {
    display: flex;
    justify-content: center;
    height: 100%;
    padding: 16px;
    overflow-y: auto;
}

.login-card {
    max-width: 400px;
    width: 100%;
    height: fit-content;
    margin: auto 0;
    padding: 32px;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.login-card h1 {
    font-size: 20px;
    margin-bottom: 4px;
}

.login-card > p {
    color: var(--text-muted);
    font-size: 14px;
    margin-bottom: 24px;
}

.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 4px;
}

.form-group input {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 15px;
    outline: none;
}

.form-group input:focus {
    border-color: var(--accent);
}

.login-form .btn-primary {
    width: 100%;
    padding: 10px;
    margin-top: 8px;
}

.login-note {
    margin-top: 16px;
    font-size: 12px;
    color: var(--text-muted);
}

/* SSO entry: an <a> styled as the primary action above the password form */
.btn-sso {
    display: block;
    width: 100%;
    padding: 10px;
    text-align: center;
}

.btn-sso:hover {
    background: var(--accent-hover);
    color: #fff;
}

/* Collapsed password section under the SSO button; students never need it */
.login-alt {
    margin-top: 20px;
}

.login-alt summary {
    cursor: pointer;
    font-size: 12px;
    color: var(--text-muted);
}

.login-alt[open] summary {
    margin-bottom: 16px;
}

/* About page: the public description of QUANTA, with one screenshot of
   the tutor. */
.login-about {
    margin-top: 6px;
    font-size: 13px;
}

.about-page {
    max-width: 46rem;
}

.about-page .notice-foot {
    color: var(--text-muted);
}

.about-figure {
    margin: 16px 0 20px;
}

.about-figure img {
    display: block;
    width: 100%;
    height: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.about-figure figcaption {
    margin-top: 6px;
    font-size: 13px;
    color: var(--text-muted);
}

/* The message box shares its row with four buttons, so on a narrow
   screen the placeholder is cut short on one line and does not wrap. */
#message-input::placeholder {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Earlier conversations (in the feedback modal's frame) */
.history-modal {
    display: flex;
    flex-direction: column;
    max-height: 80vh;
}

.history-list {
    list-style: none;
    margin: 8px 0 0;
    padding: 0;
    overflow-y: auto;
}

.history-entry {
    display: block;
    width: 100%;
    text-align: left;
    padding: 10px 12px;
    margin-bottom: 6px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    color: inherit;
    font: inherit;
    cursor: pointer;
}

.history-entry:hover,
.history-entry:focus-visible {
    border-color: var(--accent);
}

.history-entry-meta {
    display: block;
    font-size: 12px;
    color: var(--text-muted);
}

.history-entry-preview {
    display: block;
    margin-top: 2px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Utilities */
.muted {
    color: var(--text-muted);
    font-size: 14px;
}

/* KaTeX overrides */
.katex {
    font-size: 1em;
}

/* Display math wider than the bubble already scrolls sideways, but macOS
   and iOS hide overlay scrollbars until a scroll starts, so nothing says
   "there is more". Two cues: a thin scrollbar that is always drawn, and a
   right-edge shade that appears only while content extends past the edge
   (Lea Verou's scroll-shadow trick: the cover gradient scrolls with the
   content, the shade does not). The cover must match the bubble color, so
   the rule is set per bubble. */
.katex-display {
    margin: 12px 0;
    overflow-x: auto;
    max-width: 100%;
    padding-bottom: 6px;
    scrollbar-width: thin;                              /* Firefox */
    scrollbar-color: var(--text-muted) transparent;
    background-repeat: no-repeat;
    background-size: 48px 100%, 48px 100%, 16px 100%, 16px 100%;
    background-attachment: local, local, scroll, scroll;
}
.message.assistant .katex-display {
    background-image:
        linear-gradient(to right, var(--msg-assistant-bg) 40%, rgba(255,255,255,0)),
        linear-gradient(to left,  var(--msg-assistant-bg) 40%, rgba(255,255,255,0)),
        linear-gradient(to right, rgba(28,37,48,0.16), rgba(28,37,48,0)),
        linear-gradient(to left,  rgba(28,37,48,0.16), rgba(28,37,48,0));
    background-position: 0 0, 100% 0, 0 0, 100% 0;
}
.message.user .katex-display {
    background-image:
        linear-gradient(to right, var(--msg-user-bg) 40%, rgba(232,240,254,0)),
        linear-gradient(to left,  var(--msg-user-bg) 40%, rgba(232,240,254,0)),
        linear-gradient(to right, rgba(28,37,48,0.16), rgba(28,37,48,0)),
        linear-gradient(to left,  rgba(28,37,48,0.16), rgba(28,37,48,0));
    background-position: 0 0, 100% 0, 0 0, 100% 0;
}
.katex-display::-webkit-scrollbar { height: 6px; }     /* Safari, Chrome */
.katex-display::-webkit-scrollbar-thumb { background: var(--text-muted); border-radius: 3px; }
.katex-display::-webkit-scrollbar-track { background: transparent; }

/* Phones: shrink display math a little before it has to scroll at all. */
@media (max-width: 600px) {
    .katex-display > .katex { font-size: 0.92em; }
}

.message-content {
    overflow-x: auto;
    max-width: 100%;
    min-width: 0;
}

.katex-display > .katex {
    white-space: normal;
}

/* Responsive */
@media (max-width: 900px) {
    .message {
        max-width: 100%;
    }

    .input-form {
        max-width: 100%;
    }

    /* Let iOS manage viewport naturally on phones */
    html, body {
        overflow: auto;
        overscroll-behavior: auto;
    }

    .input-area {
        flex-shrink: 0;
    }
}

/* Feedback modal */
.feedback-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
    z-index: 40;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
}

.feedback-backdrop[hidden] {
    display: none;
}

.feedback-modal {
    position: relative;
    background: #fff;
    width: 100%;
    max-width: 460px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
    padding: 24px;
}

.feedback-modal h2 {
    font-size: 18px;
    margin-bottom: 4px;
}

.feedback-close {
    position: absolute;
    top: 10px;
    right: 10px;
    background: none;
    border: none;
    cursor: pointer;
    font-size: 22px;
    line-height: 1;
    width: 30px;
    height: 30px;
    color: var(--text-muted);
    border-radius: var(--radius);
}

.feedback-close:hover {
    background: var(--bg-secondary);
    color: var(--text);
}

.feedback-modal label {
    display: block;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    margin: 14px 0 6px;
}

.feedback-modal select,
.feedback-modal textarea {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font: inherit;
    color: var(--text);
    background: #fff;
}

.feedback-modal textarea {
    resize: vertical;
    min-height: 90px;
}

.feedback-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 16px;
}

.feedback-actions #feedback-submit {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

.feedback-actions #feedback-submit:hover {
    background: var(--accent-hover);
}

.feedback-actions #feedback-submit:disabled {
    opacity: 0.6;
    cursor: default;
}

.feedback-status {
    margin-top: 12px;
    font-size: 13px;
}

.feedback-status.error {
    color: var(--error);
}

.feedback-status.success {
    color: var(--success);
}

/* Problem figures (side panel) */
.problem-figure {
    max-width: 100%;
    border: 1px solid var(--border-color, #ddd);
    border-radius: 4px;
    margin-top: 8px;
    background: #fff;
}
