/* =========================================================
   LUMINA3ai — chat page
   Builds on style.css tokens. Element IDs and message classes are used by lumina3.js
   (#chatContainer, #userInput, #generateButton, .message, .user-message, .assistant-message,
   .message-content, .typing-indicator): restyle them, never rename them.
   The physical feel: the textarea grows on a spring, the send button arms when there is text,
   and messages rise out of the input they came from.
   ========================================================= */

html,
body.chatPage {
    height: 100%;
}

body.chatPage {
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* ---------------- Layout ---------------- */
#appContainer {
    flex: 1;
    min-height: 0;
    display: flex;
    width: 100%;
    max-width: 60rem;
    margin: 0 auto;
    padding: calc(var(--nav-h) + 1.25rem) 2rem 1.25rem;
    /* the shell widens a little as the paper panel docks on wide screens */
    transition: max-width 0.6s var(--spring);
}

.chatShell {
    flex: 1;
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg-panel);
    border: var(--edge);
    border-radius: var(--radius-l);
    box-shadow: var(--shadow-lift);
    overflow: hidden;
}

.chatBar {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.875rem 1.25rem;
    border-bottom: var(--edge);
}

.chatTitle {
    font-family: var(--display);
    font-weight: 700;
    font-size: var(--fs-callout);
    line-height: var(--lh-callout);
    letter-spacing: var(--tr-callout);
}

.chatBadge {
    color: var(--accent);
    background: var(--accent-fill);
    border-radius: var(--radius-pill);
    padding: 0.125rem 0.75rem;
    font-size: var(--fs-caption);
    line-height: var(--lh-caption);
    letter-spacing: var(--tr-caption);
}


.chatBarEnd {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

/* ---------------- Body: chat on the left, the paper panel docks on the right ---------------- */
.chatBody {
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 0px;
    /* the panel's column opens on the critically damped spring: no overshoot */
    transition: grid-template-columns 0.6s var(--spring);
}

.chatMain {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
}

.chatTabs {
    display: none;
}

.paperToggle[hidden] {
    display: none;
}

/* ---------------- The paper panel ----------------
   Fixed-width inner sheet inside a column that grows from 0, so the sheet never reflows while it
   opens: it just slides in, un-blurring. Closed means inert (set by paper.js), so it is also out of
   the keyboard order. */
.paperPanel {
    min-width: 0;
    overflow: hidden;
}

.paperInner {
    width: 26rem;
    height: 100%;
    overflow-y: auto;
    padding: 1.25rem 1.5rem 1.5rem;
    background: var(--bg-deep);
    border-left: var(--edge);
    scrollbar-width: thin;
    scrollbar-color: var(--fill-3) transparent;
    opacity: 0;
    transform: translateX(1.5rem);
    filter: blur(6px);
    transition: opacity 0.35s ease, transform 0.6s var(--spring), filter 0.45s ease;
}

#appContainer[data-paper="open"] {
    max-width: 84rem;
}

#appContainer[data-paper="open"] .chatBody {
    grid-template-columns: minmax(0, 1fr) 26rem;
}

#appContainer[data-paper="open"] .paperInner {
    opacity: 1;
    transform: none;
    filter: none;
}

.paperHead {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1.25rem;
}

.paperKicker {
    font-family: var(--mono);
    text-transform: uppercase;
    color: var(--accent);
    font-weight: 500;
    font-size: var(--fs-caption);
    line-height: var(--lh-caption);
    letter-spacing: var(--tr-caption);
}

.paperTitle {
    font-family: var(--display);
    font-weight: 700;
    font-size: var(--fs-headline);
    line-height: var(--lh-headline);
    letter-spacing: var(--tr-headline);
    outline: none;
}

.paperClose {
    flex-shrink: 0;
    width: 2rem;
    height: 2rem;
    border: 0;
    border-radius: 50%;
    background: var(--fill);
    color: var(--label-2);
    font-size: var(--fs-headline);
    line-height: 1;
    cursor: pointer;
    transition: transform var(--spring-dur) var(--spring), background-color 0.25s ease, color 0.25s ease;
}

.paperClose:active {
    transform: scale(0.92);
    background: var(--fill-3);
    transition-duration: 0.09s;
    transition-timing-function: ease-out;
}

@media (hover: hover) {
    .paperClose:hover {
        background: var(--fill-2);
        color: var(--label);
    }
}

.paperEmpty {
    color: var(--label-3);
    font-size: var(--fs-callout);
    line-height: var(--lh-callout);
    letter-spacing: var(--tr-callout);
}

.paperEmpty[hidden],
.paperNote[hidden] {
    display: none;
}

/* the lines a student writes, in order, on faintly ruled paper */
.paperLines {
    list-style: none;
}

.pLine,
.pAnswer {
    animation: sheetIn 0.6s var(--spring) both;
    animation-delay: calc(var(--i, 0) * 45ms);
}

@keyframes sheetIn {
    from {
        opacity: 0;
        transform: translateY(0.75rem);
    }

    to {
        opacity: 1;
        transform: none;
    }
}

.pLine {
    padding: 0.75rem 0;
    border-bottom: 1px solid var(--separator-faint);
}

.pText {
    display: block;
    color: var(--label-2);
    font-size: var(--fs-callout);
    line-height: var(--lh-callout);
    letter-spacing: var(--tr-callout);
    margin-bottom: 0.25rem;
}

.pMath {
    display: block;
    overflow-x: auto;
    overflow-y: hidden;
    font-size: var(--fs-headline);
    line-height: var(--lh-headline);
    letter-spacing: var(--tr-headline);
}

/* the conclusion: ដូចនេះ, then the answer in a box */
.pAnswer {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.75rem;
    margin-top: 1.25rem;
}

.pLead {
    font-size: var(--fs-body);
    line-height: var(--lh-body);
    letter-spacing: var(--tr-body);
}

.pBox {
    max-width: 100%;
    overflow-x: auto;
    padding: 0.375rem 1rem;
    border: 1.5px solid var(--accent);
    border-radius: var(--radius-s);
    font-size: var(--fs-headline);
    line-height: var(--lh-headline);
    letter-spacing: var(--tr-headline);
}

.paperNote {
    margin-top: 1.5rem;
    color: var(--label-3);
    font-size: var(--fs-caption);
    line-height: var(--lh-caption);
    letter-spacing: var(--tr-caption);
}

/* "generating" state: a status line under the reply and placeholder lines in the panel.
   Both move while waiting (the dot and the shimmer) and rest under reduced motion. */
.paperStatus {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    margin-top: 0.75rem;
    color: var(--label-3);
    font-size: var(--fs-small);
    line-height: var(--lh-small);
    letter-spacing: var(--tr-small);
}

.paperStatus::before {
    content: "";
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 50%;
    background: var(--accent);
    animation: waiting 1.2s ease-in-out infinite;
}

.pSkel {
    height: 1rem;
    width: var(--w, 70%);
    margin: 1.25rem 0;
    border-radius: var(--radius-s);
    background: var(--fill-2);
    animation: shimmer 1.6s ease-in-out infinite;
}

@keyframes shimmer {

    0%,
    100% {
        opacity: 0.45;
    }

    50% {
        opacity: 1;
    }
}

/* the chip under a reply that reopens its sheet */
.paperChip {
    margin-top: 0.75rem;
}

/* ---------------- Phones and narrow windows: the panel is a second tab ---------------- */
@media (max-width: 63.99rem) {
    .paperToggle {
        display: none;
    }

    #appContainer[data-paper="open"] {
        max-width: 60rem;
    }

    .chatTabs {
        position: relative;
        flex-shrink: 0;
        display: grid;
        grid-template-columns: 1fr 1fr;
        margin: 0.75rem 1rem 0;
        padding: 0.1875rem;
        background: var(--bg-deep);
        border: var(--edge);
        border-radius: var(--radius-pill);
    }

    /* the thumb slides between the two tabs on the spring */
    .tabThumb {
        position: absolute;
        top: 0.1875rem;
        bottom: 0.1875rem;
        left: 0.1875rem;
        width: calc(50% - 0.1875rem);
        background: var(--bg-raised);
        border-radius: var(--radius-pill);
        transition: transform 0.5s var(--spring);
    }

    #appContainer[data-view="paper"] .tabThumb {
        transform: translateX(100%);
    }

    .chatTab {
        position: relative;
        padding: 0.4375rem 0.75rem;
        border: 0;
        border-radius: var(--radius-pill);
        background: transparent;
        color: var(--label-3);
        font-family: var(--body);
        font-weight: 500;
        font-size: var(--fs-small);
        line-height: var(--lh-small);
        letter-spacing: var(--tr-small);
        cursor: pointer;
        transition: color 0.3s ease, transform var(--spring-dur) var(--spring);
    }

    .chatTab[aria-pressed="true"] {
        color: var(--label);
    }

    .chatTab:active {
        transform: scale(0.96);
        transition-duration: 0.09s;
        transition-timing-function: ease-out;
    }

    /* a new sheet arrived while you were reading the chat */
    .tabDot {
        display: inline-block;
        width: 0.4375rem;
        height: 0.4375rem;
        margin-left: 0.4375rem;
        border-radius: 50%;
        background: var(--accent);
        vertical-align: middle;
    }

    .tabDot[hidden] {
        display: none;
    }

    .chatBody {
        display: block;
        transition: none;
    }

    .chatBody>* {
        height: 100%;
    }

    #appContainer[data-view="chat"] .paperPanel,
    #appContainer[data-view="paper"] .chatMain {
        display: none;
    }

    /* the visible side arrives with a short rise, like a page of paper turning up */
    #appContainer[data-view="chat"] .chatMain,
    #appContainer[data-view="paper"] .paperPanel {
        animation: tabIn 0.5s var(--spring) both;
    }

    .paperInner {
        width: 100%;
        opacity: 1;
        transform: none;
        filter: none;
        border-left: 0;
        background: transparent;
        transition: none;
    }

    .paperClose {
        display: none;
    }
}

@keyframes tabIn {
    from {
        opacity: 0;
        transform: translateY(0.5rem);
    }

    to {
        opacity: 1;
        transform: none;
    }
}

/* ---------------- Messages ---------------- */
/* the scroll area fades out under the bar and the input instead of ending on a hard line */
#chatContainer {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 0.875rem;
    padding: 1.5rem 1.25rem;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--fill-3) transparent;
    -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 1.25rem, #000 calc(100% - 1.25rem), transparent 100%);
    mask-image: linear-gradient(to bottom, transparent 0, #000 1.25rem, #000 calc(100% - 1.25rem), transparent 100%);
}

.message {
    max-width: 88%;
    padding: 0.875rem 1.125rem;
    border-radius: 1.25rem;
    font-size: var(--fs-body);
    line-height: var(--lh-body);
    letter-spacing: var(--tr-body);
    word-wrap: break-word;
    word-break: break-word;
    /* a message grows out of the corner it came from, on the critically damped spring */
    animation: messageIn 0.6s var(--spring) both;
}

@keyframes messageIn {
    from {
        opacity: 0;
        transform: translateY(1.25rem) scale(0.96);
    }

    to {
        opacity: 1;
        transform: none;
    }
}

.user-message {
    align-self: flex-end;
    transform-origin: bottom right;
    background: var(--accent);
    color: var(--accent-ink);
    border-bottom-right-radius: 0.375rem;
}

.assistant-message {
    align-self: flex-start;
    transform-origin: bottom left;
    background: var(--bg-raised);
    color: var(--label);
    border-bottom-left-radius: 0.375rem;
}

/* replies mix English and Khmer under an English page: use the Khmer-safe leading */
.message-content {
    --lh-body: 1.75;
    line-height: var(--lh-body);
    overflow-wrap: break-word;
}

.typing-indicator {
    align-self: flex-start;
    display: flex;
    align-items: center;
    gap: 0.625rem;
    background: transparent;
    color: var(--label-3);
    padding: 0.5rem 0.25rem;
    font-size: var(--fs-small);
    line-height: var(--lh-small);
    letter-spacing: var(--tr-small);
}

/* a waiting indicator is allowed to move; it rests under reduced motion */
.typing-indicator::before {
    content: "";
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 50%;
    background: var(--accent);
    animation: waiting 1.2s ease-in-out infinite;
}

@keyframes waiting {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.3;
    }
}

/* math */
.katex {
    color: inherit;
}

.katex-display {
    margin: 0.875rem 0;
    padding: 0.375rem 0;
    overflow-x: auto;
    overflow-y: hidden;
}

/* ---------------- Input ---------------- */
#inputSection {
    flex-shrink: 0;
    padding: 1rem 1.25rem 0.875rem;
    border-top: var(--edge);
}

.par {
    display: block;
    margin-bottom: 0.5rem;
    color: var(--label-3);
    font-size: var(--fs-small);
    line-height: var(--lh-small);
    letter-spacing: var(--tr-small);
}

#userInputDiv {
    display: flex;
    align-items: flex-end;
    gap: 0.75rem;
}

#userInput {
    flex: 1;
    min-width: 0;
    height: 3.125rem;
    min-height: 3.125rem;
    max-height: 8.75rem;
    padding: 0.75rem 1rem;
    resize: none;
    background: var(--bg-deep);
    color: var(--label);
    border: var(--edge);
    border-radius: 1.5rem;
    font-family: var(--body);
    font-size: var(--fs-body);
    line-height: var(--lh-body);
    letter-spacing: var(--tr-body);
    outline: none;
    /* lumina3.js sets an inline transition while typing: the spring below must win, so the box
       follows the text on the same spring as everything else */
    transition: height 0.45s var(--spring), border-color 0.25s ease, box-shadow 0.25s ease !important;
}

#userInput::placeholder {
    color: var(--label-3);
    /* one line, cut with an ellipsis on narrow screens instead of wrapping into the scrollbar */
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#userInput:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-fill);
}

/* no scrollbar while empty (rows="5" would otherwise show one); a quiet thin one once text outgrows it */
#userInput {
    scrollbar-width: thin;
    scrollbar-color: var(--fill-3) transparent;
}

#userInput:placeholder-shown {
    overflow: hidden;
}

#userInput:disabled {
    opacity: 0.6;
}

/* the button is "armed" when there is text: it settles up to full size on the spring, and drops
   back when the box empties or a reply is on its way */
#generateButton {
    flex-shrink: 0;
    height: 3.125rem;
    padding: 0 1.5rem;
}

#generateButton:disabled {
    opacity: 0.4;
    transform: scale(0.94);
    filter: none;
}

#warning {
    margin-top: 0.625rem;
    color: var(--label-3);
    font-size: var(--fs-caption);
    line-height: var(--lh-caption);
    letter-spacing: var(--tr-caption);
}

/* ---------------- Responsive ---------------- */
@media (max-width: 640px) {
    #appContainer {
        padding: calc(var(--nav-h) + 0.75rem) 0.875rem 0.75rem;
    }

    #chatContainer {
        padding: 1rem 0.75rem;
    }

    .message {
        max-width: 94%;
        padding: 0.75rem 1rem;
        font-size: var(--fs-callout);
        line-height: var(--lh-callout);
        letter-spacing: var(--tr-callout);
    }

    #inputSection {
        padding: 0.75rem 0.75rem 0.625rem;
    }

    #userInputDiv {
        gap: 0.5rem;
    }

    #generateButton {
        padding: 0 1.125rem;
    }
}

@media (prefers-reduced-motion: reduce) {

    .typing-indicator::before,
    .paperStatus::before,
    .pSkel {
        animation: none;
    }

    #generateButton:disabled {
        transform: none;
    }
}
