:root, :root.light {
    --paper: #fbf7ef;
    --surface: #ece5d8;
    --ink: #263951;
    --muted: #526175;
    --rule: #d1c5b2;
    --accent-strong: #315982;
    --deep: #233b56;
    --action: #315982;
    --danger: #b42318;
    --white: #fff;
    --apple-bg: var(--paper);
    --apple-sidebar: var(--surface);
    --apple-card: var(--white);
    --subtle-bg: var(--surface);
    --hover-bg: #e1d9cb;
    --apple-blue: var(--accent-strong);
    --btn-dark: var(--accent-strong);
    --text-primary: var(--ink);
    --text-secondary: var(--muted);
    --glass-border: var(--rule);
    --bg: var(--paper);
    --card: var(--white);
    --sheet: var(--surface);
    --subtle: var(--surface);
    --hover: var(--hover-bg);
    --accent: var(--accent-strong);
    --btn: var(--accent-strong);
    --ink2: var(--muted);
    --line: var(--rule);
}
body, textarea, button, input, select {
    font-family: "Helvetica Neue", Helvetica, "Noto Sans CJK TC", "PingFang TC", Arial, sans-serif;
}
:focus-visible { outline: 2px solid var(--accent-strong); outline-offset: 3px; }
.study-brand { font-size: 1.4rem; font-weight: 750; letter-spacing: -.04em; }
.study-start { width: 100%; max-width: var(--read-col, 700px); margin-inline: auto; }
.study-welcome {
    position: relative; padding: 28px 30px 24px 48px;
    background-color: var(--paper);
    background-image:
        linear-gradient(90deg, transparent 24px, #b957515c 24px, #b957515c 25px, transparent 25px),
        repeating-linear-gradient(0deg, transparent 0 23px, #31598212 23px 24px),
        repeating-linear-gradient(90deg, transparent 0 23px, #31598212 23px 24px);
    border: 1px solid var(--rule); border-radius: 16px;
}
.study-kicker { color: var(--muted); font-size: .875rem; font-weight: 700; margin-bottom: 8px; }
.study-intro h1 { font-size: clamp(2rem, 4vw, 3.25rem); line-height: 1.2; letter-spacing: -.045em; margin-bottom: 12px; }
.study-description { color: var(--muted); line-height: 1.65; }
.study-modes {
    display: flex; flex-wrap: wrap; gap: 8px 20px; margin-top: 18px;
    font-size: .875rem; line-height: 1.6;
}
.study-modes p { color: var(--muted); }
.study-modes strong { color: var(--ink); margin-right: .45em; }

#sidebar { width: 248px; background: var(--deep); color: white; }
#sidebar.collapsed { width: 0; }
.sidebar-header { padding: 24px 16px 16px; }
.sidebar-header .study-brand { display: block; margin: 0 0 22px 4px; }
.sidebar-heading { color: #dce9fa; font-size: .875rem; padding: 10px 20px 0; border-top: 1px solid #617b97; }
.new-chat-btn { background: #e4ecf7; border-color: transparent; color: var(--deep); text-align: left; padding: 10px 12px; font-weight: 700; }
.session-item { font-size: .875rem; color: #dfebf9; }
.session-item:hover { background: #304b69; color: white; }
.session-item.active { background: white; color: var(--ink); box-shadow: inset 2px 0 var(--accent-strong); }
.delete-btn { color: inherit; }
.session-item:focus-within .delete-btn { opacity: 1; }
#main { min-width: 0; }
#main header { background: white; padding-block: 12px; min-height: 64px; }
.header-title { white-space: nowrap; }
.mode-toggle { gap: 0; margin-right: 0; }
.mode-btn { padding: 7px 10px; white-space: nowrap; font-size: .875rem; }
.mode-toggle .mode-btn + .mode-btn { margin-left: 6px; }
.action-btn, #speed-select { min-height: 34px; }
#chat-container { min-height: 0; padding-top: clamp(32px, 8vh, 90px); }
#chat-container:not(:has(> .study-start:not([style*="none"]))) { padding-top: 28px; }
#chat-container > .study-start { flex-shrink: 0; }
.assistant .msg-bubble { background: white; border-left: 2px solid var(--rule); padding: 8px 16px; }
.user .msg-bubble { background: var(--surface); border: 1px solid var(--rule); }
.msg-bubble mjx-container[display="true"] { max-width: 100%; overflow: auto hidden; }
.input-area-container { padding-bottom: 24px; }
.input-bar, .input-bar:focus-within { background: white; }
.input-bar { padding: 8px; gap: 6px; border-color: var(--ink); }
.input-bar:focus-within, .row:focus-within { border-color: var(--accent-strong); }
#user-input, #inp { min-width: 0; }
#user-input { padding: 9px 6px; }
#send-btn, .send { background: var(--action); color: white; min-width: 52px; height: 44px; padding-inline: 8px; flex-shrink: 0; font-size: .875rem; font-weight: 700; }
#send-btn:not(:disabled):hover, .send:not(:disabled):hover { color: white; background: var(--ink); }
#send-btn:disabled, .send:disabled { background: var(--rule); color: var(--muted); }
.login-overlay { align-items: flex-start; padding-inline: clamp(24px, 10%, 128px); }
.login-overlay h2, .login-overlay p { text-align: left; }
.sidebar-expand-btn {
    display: inline-grid; place-items: center; min-width: 34px; height: 34px;
    background: white; color: var(--ink); border: 1px solid var(--rule); cursor: pointer;
}
#sidebar:not(.collapsed) + #main .sidebar-expand-btn svg:not([style]) { transform: rotate(180deg); }
.input-bar .attach-btn, .input-bar .voice-btn, .row .iconbtn {
    min-width: 44px; height: 44px; padding: 5px; font-size: .875rem; line-height: 1; white-space: nowrap;
}
.input-bar .icon-btn:not(#send-btn):hover, .row .iconbtn:hover { background: var(--surface); }

#top { background: white; gap: 10px; }
#top .t { text-align: left; }
#top .iconbtn { min-width: 44px; min-height: 44px; font-size: .875rem; border: 1px solid var(--rule); }
#modes { gap: 8px; padding: 10px 14px; }
.mode { min-height: 44px; }
#chat { padding: 24px 14px 4px; }
#chat .study-start { margin-bottom: 12px; }
#chat .study-welcome { padding: 22px 14px 18px 36px; }
#chat .study-intro h1 { font-size: clamp(1.65rem, 7vw, 2.2rem); }
#chat .study-modes { gap: 4px 12px; margin-top: 12px; }
#bar { background: var(--surface); }
.row { background: white; padding-left: 5px; gap: 2px; border-color: var(--ink); }
#drawer.on { border-right: 1px solid var(--rule); }
#who button { font-size: .875rem; }
.new-chat-btn, .session-item, .msg-bubble, .input-bar,
#speed-select, .mode-btn, .action-btn, .modal, .modal-input, .modal-btn, .auth-btn,
#top .iconbtn, .mode, .row, .send, #send-btn, .bub, #newchat, .sitem, #who button,
.fld, .primary {
    border-radius: 10px;
    box-shadow: none;
}
@media (max-width: 768px) {
    #main header { min-height: 52px; }
    .sidebar-expand-btn { display: none; }
}
@media (prefers-reduced-motion: reduce) {
    #sidebar, #drawer, #veil { transition: none; }
}

#drawer .del, #who button { min-width: 44px; min-height: 44px; }
#sidebar :focus-visible { outline-color: #dce9fa; }
#chat .bub, #chat-container .msg-bubble { overflow-wrap: anywhere; }
#inp::placeholder, #user-input::placeholder { color: var(--muted); }
#top button:active, .row button:active, .mode:active, .new-chat-btn:active { filter: brightness(.88); }
/* The same composer stays mounted when the first message changes the layout. */
#main:has(#welcome-screen:not([style*="none"])) #chat-container {
    flex: 0 1 auto;
    padding-bottom: 0;
}
#main:has(#welcome-screen:not([style*="none"])) .input-area-container {
    padding-top: 12px;
}
#app:has(#chat > .study-start):not(:has(#chat > .msg)) #chat {
    flex: 0 1 auto;
}
#app:has(#chat > .study-start):not(:has(#chat > .msg)) #bar {
    background: transparent;
    border-top-color: transparent;
}
#chat:has(> .msg) > .study-start { display: none; }
.input-bar, .row { border-color: var(--accent-strong); }
#chat-container .assistant .msg-bubble, #chat .msg.bot .bub {
    border-left: 3px solid var(--accent-strong);
}
@media (max-height: 480px) {
    #app #chat { padding-top: 8px; }
    #chat .study-welcome { padding-block: 12px; }
}

.input-bar .attach-btn, .row .iconbtn:has(.upload-icon) {
    display: inline-grid;
    place-items: center;
}
.upload-icon { display: block; }

/* The rain occupies the unused area below the welcome and composer. */
.math-rain {
    position: fixed;
    inset: 53% 0 0 0;
    overflow: hidden;
    pointer-events: none;
    user-select: none;
    contain: paint;
    color: var(--accent-strong);
    mask-image: linear-gradient(transparent, #000 20%, #000 80%, transparent);
}
#main .math-rain { left: 248px; }
#sidebar.collapsed + #main .math-rain { left: 0; }
.math-rain span {
    position: absolute;
    top: -24px;
    left: var(--lane);
    opacity: .24;
    font: 600 var(--size, 22px)/1 Georgia, serif;
    animation: math-rain-fall var(--duration, 24s) linear var(--offset) infinite;
}
.math-rain span:nth-child(1) { --lane: 7%; --offset: -9s; }
.math-rain span:nth-child(2) { --lane: 17%; --offset: -18s; --size: 17px; }
.math-rain span:nth-child(3) { --lane: 29%; --offset: -4s; --duration: 29s; }
.math-rain span:nth-child(4) { --lane: 40%; --offset: -14s; --size: 26px; }
.math-rain span:nth-child(5) { --lane: 51%; --offset: -7s; --size: 19px; }
.math-rain span:nth-child(6) { --lane: 63%; --offset: -21s; --duration: 30s; }
.math-rain span:nth-child(7) { --lane: 72%; --offset: -11s; --size: 18px; }
.math-rain span:nth-child(8) { --lane: 83%; --offset: -20s; --duration: 27s; }
.math-rain span:nth-child(9) { --lane: 92%; --offset: -5s; --size: 16px; }
@keyframes math-rain-fall {
    from { transform: translateY(0); }
    to { transform: translateY(52vh); }
}
.input-area-container, #bar { position: relative; }
@media (max-width: 768px) {
    #main .math-rain { left: 0; }
    .math-rain { top: 57%; }
    .math-rain span:nth-child(even) { display: none; }
}
@media (max-height: 550px), (prefers-reduced-motion: reduce) {
    .math-rain { display: none; }
}
