/**
 * Marky Mark — Chat
 * Chat log, messages, input bar, math keyboard, top controls, zero state,
 * markdown rendering overrides, idle footer, offline banner, error/limit messages.
 */

/* --- Chat Log --- */
#chat-log {
    flex: 1;
    display: none;
    overscroll-behavior: none;
    flex-direction: column;
    overflow-y: auto;
    width: 100%;
    padding: 40px 0 20px;
    box-sizing: border-box;
    scrollbar-width: thin;
    scrollbar-color: rgba(0, 0, 0, 0.1) auto;
}

#chat-log::-webkit-scrollbar {
    width: 6px;
}

#chat-log::-webkit-scrollbar-track {
    background: transparent;
}

#chat-log::-webkit-scrollbar-thumb {
    background-color: rgba(0, 0, 0, 0.1);
    border-radius: 20px;
}

body.is-active #chat-log {
    display: flex;
}

/* --- Message Styling --- */
.message-row {
    display: flex;
    flex-direction: column;
    margin-bottom: 24px;
    width: 100%;
    max-width: var(--max-width);
    margin-left: auto;
    margin-right: auto;
    padding: 0 20px;
    box-sizing: border-box;
}

.message {
    font-size: 1.00rem;
    line-height: 1.6;
    max-width: 100%;
}

.user-row {
    align-items: flex-end;
}

.user-message {
    background: var(--bg-tertiary);
    padding: 10px 18px;
    border-radius: 20px;
}

.tutor-row {
    align-items: flex-start;
}

/* --- Copy Button --- */
.msg-actions {
    margin-top: 3px;
    opacity: 0;
    transition: opacity 0.15s;
}

.message-row:hover .msg-actions {
    opacity: 1;
}

.copy-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 4px 5px;
    border-radius: 6px;
    color: var(--text-tertiary);
    display: flex;
    align-items: center;
    transition: color 0.15s, background 0.15s;
}

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

.copy-btn.copied {
    color: #22c55e;
}

/* --- Inline Graph Snapshot --- */
.graph-snapshot {
    width: 100%;
    max-width: 280px;
    margin-top: 10px;
    border-radius: 12px;
    overflow: hidden;
    border: 1px solid #e0e0e0;
    cursor: pointer;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    position: relative;
}

.graph-snapshot:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.graph-snapshot img {
    width: 100%;
    display: block;
    border-radius: 12px 12px 0 0;
}

.snapshot-label {
    display: block;
    text-align: center;
    font-size: 0.8rem;
    color: #666;
    padding: 6px 0;
    background: #fafafa;
    border-top: 1px solid #eee;
}

/* Hide snapshots when the graph panel is visible */
body.is-visual-open .graph-snapshot {
    display: none;
}

/* --- Typing Indicator (three pulsing dots) --- */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 12px 18px;
}

.typing-indicator .dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-primary);
    opacity: 0.3;
    animation: typingPulse 1.4s infinite ease-in-out;
}

.typing-indicator .dot:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator .dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingPulse {
    0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
    40% { opacity: 1; transform: scale(1); }
}

/* --- Closing options: "— or —" divider + label (rendered in chat log) --- */
.closing-or-divider {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 14px 0 0;
}

.closing-or-divider::before,
.closing-or-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border-color);
}

.closing-or-divider span {
    font-size: 13px;
    color: var(--text-muted);
    font-weight: 500;
    white-space: nowrap;
}

.closing-input-label {
    font-size: 13px;
    color: var(--text-muted);
    font-weight: 500;
    padding: 6px 0 0;
}

/* --- Input Positioning --- */
#input-anchor {
    position: relative;
    width: 90%;
    max-width: var(--max-width);
    margin: 0 auto;
    transition: margin var(--transition);
    z-index: 100;
    flex-shrink: 0;
}

body.is-idle #input-anchor {
    position: relative;
    margin-bottom: 8px;
    border-radius: 28px;
    padding: 10px;
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 20px var(--shadow-sm);
    background: var(--bg-color);
}

body.is-idle .input-wrapper {
    border-radius: 18px;
}

body.is-active #input-anchor {
    margin-bottom: 30px;
}

body.is-visual-open #input-anchor {
    width: 90%;
}

.input-wrapper {
    background: var(--input-bg);
    background-image:
        linear-gradient(rgba(59, 124, 244, 0.04) 1px, transparent 1px),
        linear-gradient(90deg, rgba(59, 124, 244, 0.04) 1px, transparent 1px);
    background-size: 24px 24px;
    border: 1px solid var(--border-color);
    border-radius: 24px;
    padding: 8px 8px 8px 14px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.input-top-row {
    display: flex;
    align-items: center;
    width: 100%;
}

.input-bottom-row {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 6px;
    padding: 0 2px 2px;
}

/* Send button always on the far right */
.input-bottom-row #send-btn {
    margin-left: auto;
}

#chat-input {
    flex: 1;
    border: none;
    outline: none;
    padding: 10px;
    font-size: 1rem;
    background: transparent;
    min-height: 20px;
    max-height: 120px;
    overflow-y: auto;
    word-wrap: break-word;
    white-space: pre-wrap;
    line-height: 1.5;
}

/* Placeholder for empty contenteditable */
#chat-input:empty::before {
    content: attr(data-placeholder);
    color: var(--text-tertiary);
    pointer-events: none;
}

/* Math island wrapper (holds math-field + delete button) */
#chat-input .math-island-wrapper {
    display: inline-block;
    vertical-align: middle;
    position: relative;
    margin: 0 2px;
}

/* Inline math-field islands (the blue rectangles) */
#chat-input .inline-math {
    display: inline-block;
    vertical-align: middle;
    border: 1.5px solid #3b7cf4;
    border-radius: 6px;
    background: #f0f6ff;
    padding: 2px 6px;
    font-size: 0.95rem;
    min-width: 30px;
    max-width: 300px;
    outline: none;
}

#chat-input .inline-math:focus-within,
#chat-input .math-island-wrapper:focus-within .inline-math {
    border-color: #2a70c2;
    box-shadow: 0 0 0 2px rgba(74, 144, 226, 0.2);
}

/* Hover delete X button on islands */
.math-island-delete {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #ccc;
    color: #fff;
    font-size: 13px;
    line-height: 18px;
    text-align: center;
    cursor: pointer;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s, background 0.15s;
    z-index: 1;
}

.math-island-wrapper:hover .math-island-delete {
    opacity: 1;
    pointer-events: auto;
}

.math-island-delete:hover {
    background: #e74c3c;
}

/* Strip MathLive's default chrome inside islands */
#chat-input .inline-math::part(container) {
    border: none !important;
    outline: none !important;
    box-shadow: none !important;
    padding: 0 !important;
    min-height: unset !important;
}

/* Hide ALL MathLive UI chrome — keyboard toggle, menu, status bar */
#chat-input .inline-math::part(virtual-keyboard-toggle),
#chat-input .inline-math::part(menu-toggle),
#chat-input .inline-math::part(content) + div,
#chat-input .inline-math::part(keyboard-toggle),
#chat-input .inline-math::part(actions) {
    display: none !important;
    width: 0 !important;
    height: 0 !important;
    overflow: hidden !important;
}

/* Fallback: hide via MathLive's internal classes and attribute selectors */
#chat-input .inline-math .ML__toolbar,
#chat-input .inline-math .ML__virtual-keyboard-toggle,
#chat-input .inline-math .ML__status-bar,
#chat-input .inline-math [part="virtual-keyboard-toggle"],
#chat-input .inline-math [part="menu-toggle"],
#chat-input .inline-math [part="keyboard-toggle"],
#chat-input .inline-math [part="actions"] {
    display: none !important;
}

/* Also target within the wrapper */
.math-island-wrapper .inline-math::part(virtual-keyboard-toggle),
.math-island-wrapper .inline-math::part(menu-toggle),
.math-island-wrapper .inline-math::part(keyboard-toggle),
.math-island-wrapper .inline-math::part(actions) {
    display: none !important;
    width: 0 !important;
    height: 0 !important;
}

#send-btn {
    background: var(--bg-tertiary);
    color: var(--text-tertiary);
    border: none;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.18s ease, color 0.18s ease;
    flex-shrink: 0;
}

#input-anchor.has-content #send-btn {
    background: var(--primary-color);
    color: var(--bg-color);
}

#send-btn:disabled {
    background: #ccc;
    color: #aaa;
}

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

#send-btn .stop-icon {
    display: none;
    width: 13px;
    height: 13px;
    background: #fff;
    border-radius: 2px;
}

#send-btn.sending {
    background: #888;
    color: #fff;
}

#send-btn.sending svg {
    display: none;
}

#send-btn.sending .stop-icon {
    display: block;
}

/* --- Math Keyboard Toggle --- */
#math-kb-toggle {
    background: var(--bg-tertiary);
    border: none;
    height: 32px;
    border-radius: 16px;
    padding: 0 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 5px;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.8rem;
    font-weight: 500;
    transition: color 0.2s, background 0.2s;
    flex-shrink: 0;
    white-space: nowrap;
}

#math-kb-toggle .sigma-icon {
    font-size: 1rem;
    font-weight: 700;
    line-height: 1;
}

#math-kb-toggle:hover {
    background: var(--bg-hover);
}

#input-anchor.math-kb-open #math-kb-toggle {
    background: var(--primary-color);
    color: var(--bg-color);
}

#calc-toggle {
    background: var(--bg-tertiary);
    border: none;
    height: 32px;
    border-radius: 16px;
    padding: 0 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 5px;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.8rem;
    font-weight: 500;
    transition: color 0.2s, background 0.2s;
    flex-shrink: 0;
    white-space: nowrap;
}

#calc-toggle:hover {
    background: var(--bg-hover);
}

#calc-toggle.calc-open {
    background: var(--primary-color);
    color: var(--bg-color);
}

/* --- Math Keyboard Panel --- */
#math-keyboard {
    display: none;
    flex-direction: column;
    gap: 0;
    padding: 0;
    margin-top: 8px;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    box-shadow: 0 2px 12px var(--shadow-sm);
    overflow: hidden;
    /* Float below input without shifting layout */
    position: absolute;
    left: 0;
    right: 0;
    top: 100%;
    z-index: 200;
}

#input-anchor.math-kb-open #math-keyboard {
    display: flex;
    animation: mkSlideUp 200ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* In chat mode, open the keyboard ABOVE the input (input is at the bottom) */
body.is-active #math-keyboard {
    top: auto;
    bottom: 100%;
    margin-top: 0;
    margin-bottom: 8px;
}

body.is-active #input-anchor.math-kb-open #math-keyboard {
    animation: mkSlideDown 200ms cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes mkSlideDown {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Keyboard tabs */
.mk-tabs {
    display: flex;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-secondary);
}

.mk-tab {
    flex: 1;
    padding: 8px 0;
    border: none;
    background: transparent;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-secondary);
    cursor: pointer;
    transition: color 0.15s, background 0.15s;
    border-bottom: 2px solid transparent;
}

.mk-tab:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}

.mk-tab.active {
    color: var(--text-primary);
    border-bottom-color: var(--primary-color);
    background: var(--bg-color);
}

/* Keyboard panels */
.mk-panel {
    display: none;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
    padding: 10px;
}

.mk-panel.active {
    display: grid;
}

@keyframes mkSlideUp {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.mk-btn {
    height: 44px;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background: var(--bg-secondary);
    font-size: 1rem;
    font-family: inherit;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, border-color 0.15s;
    color: var(--text-primary);
}

.mk-btn:hover {
    background: var(--bg-hover);
    border-color: var(--border-color);
}

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

/* --- Controls --- */
.top-left-controls {
    position: fixed;
    top: 20px;
    left: 20px;
    z-index: 3000;
}

.top-center-controls {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 3000;
    display: flex;
    align-items: center;
    pointer-events: none; /* click-through when hidden */
    transition: left 220ms cubic-bezier(0.4, 0, 0.2, 1);
}
.top-center-controls > * { pointer-events: auto; }

/* Desktop: always offset by the 56px collapsed rail */
@media (min-width: 769px) {
    .top-center-controls {
        left: calc(50% + 28px); /* 56px rail / 2 */
    }
}

/* Account for sidebar when fully open */
body.is-sidebar-open .top-center-controls {
    left: calc(50% + var(--sidebar-width) / 2);
}

/* Hide centered upgrade pill once the user has started chatting */
body.is-active .top-center-controls { display: none; }

.top-right-controls {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 3000;
    display: flex;
    align-items: center;
    gap: 4px;
}


#menu-btn,
#visual-toggle {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0;
    border-radius: 8px;
    box-shadow: none;
    transition: transform 0.2s ease;
}

#menu-btn {
    transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275), opacity 0.2s;
}

#menu-btn:hover {
    transform: scale(1.1);
    opacity: 0.8;
    cursor: pointer;
}

.menu-icon {
    width: 32px;
    height: 32px;
    display: block;
    object-fit: contain;
}

#visual-toggle {
    display: none;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    padding: 0;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.92);
    border: 1px solid rgba(0, 0, 0, 0.09);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.10);
    color: #444;
    cursor: pointer;
    transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}

#visual-toggle span { display: none; }

#visual-toggle svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

#visual-toggle:hover {
    background: #fff;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.14);
    transform: translateY(-1px);
}

#visual-toggle:active {
    transform: translateY(0) scale(0.97);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.10);
}

[data-theme="dark"] #visual-toggle {
    background: rgba(30, 41, 59, 0.95);
    border-color: rgba(255, 255, 255, 0.10);
    color: #94a3b8;
}
[data-theme="dark"] #visual-toggle:hover {
    background: rgba(51, 65, 85, 0.98);
}

/* Hide when graph is open */
body.has-visual-state.is-visual-open #visual-toggle { display: none; }

/* On phones: the graph swipe-handle replaces the toggle — keep hidden.
   Shown via the media query below on tablet/desktop. */
body.has-visual-state:not(.is-visual-open) #visual-toggle { display: none; }

/* Tablet + Desktop (≥ 768px): pull-tab anchored to right edge of sidebar mini-rail */
@media (min-width: 768px) {
    body.has-visual-state:not(.is-visual-open) #visual-toggle {
        position: fixed;
        left: 56px;   /* right edge of sidebar mini-rail */
        top: 12px;    /* matches visual-panel-header top position */
        transform: none;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        width: 36px;
        height: auto;
        padding: 14px 6px;
        border-radius: 0 10px 10px 0;
        background: rgba(255, 255, 255, 0.97);
        border: 1px solid rgba(0, 0, 0, 0.09);
        border-left: none;
        box-shadow: 3px 0 10px rgba(0, 0, 0, 0.10);
        gap: 8px;
        z-index: 200;
        color: #555;
        transition: left 220ms cubic-bezier(0.4, 0, 0.2, 1), transform 0.18s ease, box-shadow 0.18s ease, background 0.18s ease, border-color 0.18s ease;
    }

    /* Track the sidebar's expanded edge */
    body.has-visual-state:not(.is-visual-open).is-sidebar-open #visual-toggle {
        left: var(--sidebar-width);
    }

    body.has-visual-state:not(.is-visual-open) #visual-toggle span {
        display: block;
        writing-mode: vertical-rl;
        transform: rotate(180deg);
        font-size: 11px;
        font-weight: 500;
        letter-spacing: 0.05em;
        color: #666;
        white-space: nowrap;
    }

    body.has-visual-state:not(.is-visual-open) #visual-toggle:hover {
        background: #fff;
        box-shadow: 5px 0 16px rgba(74, 144, 226, 0.18);
        border-color: rgba(74, 144, 226, 0.25);
        transform: translateX(3px);
    }

    body.has-visual-state:not(.is-visual-open) #visual-toggle:active {
        transform: translateX(1px);
        box-shadow: 2px 0 6px rgba(0, 0, 0, 0.10);
    }

    /* Dark mode overrides for the pull-tab */
    [data-theme="dark"] body.has-visual-state:not(.is-visual-open) #visual-toggle {
        background: rgba(30, 41, 59, 0.95);
        border-color: rgba(255, 255, 255, 0.10);
        color: #94a3b8;
    }
    [data-theme="dark"] body.has-visual-state:not(.is-visual-open) #visual-toggle span {
        color: #94a3b8;
    }
    [data-theme="dark"] body.has-visual-state:not(.is-visual-open) #visual-toggle:hover {
        background: rgba(51, 65, 85, 0.98);
        border-color: rgba(99, 179, 237, 0.25);
        box-shadow: 5px 0 16px rgba(0, 0, 0, 0.4);
    }
}

#new-chat-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 10px 12px;
    margin-top: 60px;
    margin-bottom: 20px;
    background: transparent;
    color: var(--text-primary);
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.95rem;
    font-weight: 500;
    transition: background 150ms ease;
}

#new-chat-btn:hover {
    background: var(--bg-hover);
}

#new-chat-btn:active {
    transform: scale(0.98);
    background: var(--bg-hover);
    transition: transform 100ms ease, background 100ms ease;
}

/* --- Zero State --- */
.zero-state {
    text-align: center;
    width: 100%;
    max-width: var(--max-width);
    padding: 0 20px;
    box-sizing: border-box;
}

/* Fixed viewport-relative top offset so topic rows expand down without moving the input */
body.is-idle .zero-state {
    margin-top: calc(50vh - 230px);
    margin-bottom: 24px;
}

body.is-active .zero-state {
    display: none;
}

.zero-state-title-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 14px;
    margin-bottom: 0;
}

.logo-icon {
    width: 52px;
    height: auto;
    display: block;
}

.zero-state h1 {
    font-size: 2.8rem;
    font-weight: 800;
    color: var(--text-primary);
    letter-spacing: -0.03em;
    margin: 0;
}

.zero-state-rule {
    display: none;
}

.zero-state-preview {
    display: block;
    margin: 18px auto 0;
    opacity: 0.38;
}

.zero-state p {
    color: var(--text-secondary);
    font-size: 1.05rem;
    font-weight: 400;
}

/* --- Markdown Overrides inside messages --- */
.message h1, .message h2, .message h3 {
    margin-top: 24px;
    margin-bottom: 12px;
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: -0.01em;
}

.message h3:first-child {
    margin-top: 0;
}

.message h1 {
    font-size: 1.4rem;
}

.message h2 {
    font-size: 1.3rem;
}

.message p {
    margin-bottom: 14px;
    line-height: 1.7;
    color: var(--text-primary);
}

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

.message ul, .message ol {
    padding-left: 28px;
    margin-bottom: 16px;
    line-height: 1.7;
}

.message ul {
    list-style-type: disc;
}

.message li {
    margin-bottom: 8px;
    line-height: 1.7;
    color: var(--text-primary);
}

.message li::marker {
    color: var(--text-secondary);
}

.message li > ul,
.message li > ol {
    margin-top: 8px;
    margin-bottom: 4px;
}

.message strong {
    font-weight: 700;
    color: var(--text-primary);
}

.message em {
    font-style: italic;
    color: var(--text-primary);
}

.message hr {
    border: none;
    border-top: 2px solid #e5e5e5;
    margin: 24px 0;
    opacity: 0.6;
}

/* Coordinate pills: visually distinct badge for (x, y) pairs in chat */
.coord-pill {
    display: inline-block;
    background: #f0f4ff;
    border: 1px solid #c8d6f0;
    border-radius: 999px;
    padding: 1px 8px;
    font-size: 0.88em;
    font-family: 'SF Mono', 'Fira Code', 'Fira Mono', monospace;
    letter-spacing: 0.01em;
    white-space: nowrap;
    color: #1a3a6e;
}

/* Equation inline display: underline accent instead of a badge.
   Uses --eq-color CSS variable (set inline by JS) when the equation matches a graphed line.
   Falls back to a subtle black underline; transitions smoothly to the line color when the graph arrives. */
.eq-pill {
    display: inline;
    background: none;
    border: none;
    border-bottom: 2px solid var(--eq-color, rgba(0, 0, 0, 0.25));
    border-radius: 0;
    padding: 0 1px 1px;
    vertical-align: baseline;
    white-space: nowrap;
    transition: border-bottom-color 0.3s ease;
}

/* Dark mode: coord pills — swap light blue chip for neutral dark chip */
[data-theme="dark"] .coord-pill {
    background: var(--bg-tertiary);
    border-color: var(--border-color);
    color: var(--text-primary);
}

[data-theme="dark"] .eq-pill {
    border-bottom-color: var(--eq-color, rgba(255, 255, 255, 0.3));
}

/* When the pill wraps a <code> element (backtick equations), use violet text */
.message code.eq-pill {
    font-family: 'SF Mono', 'Fira Code', 'Fira Mono', monospace;
    font-size: 0.88em;
    font-style: normal;
    color: #5b21b6;
    background: none;
}

[data-theme="dark"] .message code.eq-pill {
    color: #a78bfa;
    background: none;
}

/* "Try this" / "Your turn" callout blocks */
.callout-try {
    margin: 14px 0;
}

.callout-try > p {
    background: #f0f7ff;
    border-left: 3px solid #3b7cf4;
    border-radius: 0 8px 8px 0;
    padding: 10px 16px;
    margin: 0;
    color: #1e3a5f;
    line-height: 1.65;
}

/* Callout may contain a coord-pill or eq-pill — keep them readable on the tinted background */
.callout-try .coord-pill {
    background: #e4eeff;
    border-color: #afc3e8;
}

.callout-try .eq-pill {
    border-bottom-color: var(--eq-color, rgba(0, 0, 0, 0.25));
}

/* Closing prompt — floating card at the end of every response */
.closing-prompt-block {
    margin-top: 14px;
    padding: 12px 16px;
    background: var(--bg-color);
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(74, 144, 226, 0.18), 0 0 0 1px rgba(74, 144, 226, 0.1);
    color: #1a365d;
    font-weight: 600;
    line-height: 1.6;
    font-size: 0.95em;
}

[data-theme="dark"] .closing-prompt-block {
    color: var(--text-primary);
}

.closing-prompt-block p {
    margin: 0;
}

/* Inline code */
.message code {
    background: var(--bg-tertiary);
    padding: 3px 8px;
    border-radius: 5px;
    font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
    font-size: 0.92em;
    color: #d63031;
    border: 1px solid var(--border-color);
}

/* Code blocks */
.message pre {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 16px;
    overflow-x: auto;
    margin: 16px 0;
}

.message pre code {
    background: transparent;
    padding: 0;
    border: none;
    color: var(--text-primary);
    font-size: 0.9em;
}

/* Blockquotes */
.message blockquote {
    border-left: 4px solid #3b7cf4;
    margin: 16px 0;
    padding-left: 20px;
    padding-top: 4px;
    padding-bottom: 4px;
    color: var(--text-secondary);
    font-style: italic;
    background: var(--bg-secondary);
    border-radius: 0 4px 4px 0;
}

/* --- Idle Footer (countdown + privacy link) --- */
.idle-footer {
    position: absolute;
    bottom: 28px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 10;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    pointer-events: none;
    white-space: nowrap;
}

.idle-footer .privacy-link,
.idle-footer .privacy-link a {
    pointer-events: auto;
}

body.is-active .idle-footer {
    display: none;
}


.countdowns-row {
    display: flex;
    flex-direction: row;
    gap: 20px;
    align-items: flex-start;
    justify-content: center;
}

.countdown-card {
    background: rgba(255, 255, 255, 0.7);
    border: 1px solid rgba(0, 0, 0, 0.07);
    border-radius: 16px;
    padding: 18px 24px 14px;
    box-shadow: 0 2px 16px rgba(0, 0, 0, 0.07);
}

[data-theme="dark"] .countdown-card {
    background: rgba(15, 23, 42, 0.6);
    border-color: rgba(255, 255, 255, 0.08);
    box-shadow: 0 2px 16px rgba(0, 0, 0, 0.3);
}

/* --- Custom FlipClock --- */

.fc-wrap {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.fc-label {
    font-size: 13px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: var(--text-primary);
}

.fc-units {
    display: flex;
    align-items: center;
    gap: 4px;
}

.fc-unit {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
}

.fc-unit-label {
    font-size: 9px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-tertiary);
    opacity: 0.7;
}

.fc-digits {
    display: flex;
    gap: 2px;
}

/* Each individual digit tile */
.fc-digit {
    position: relative;
    width: 15px;
    height: 25px;
    border-radius: 5px;
    overflow: hidden;
    background: #1e293b;
    box-shadow: 0 3px 0 rgba(0,0,0,0.35), inset 0 1px 0 rgba(255,255,255,0.05);
}

[data-theme="dark"] .fc-digit {
    background: #0f172a;
    box-shadow: 0 3px 0 rgba(0,0,0,0.6), inset 0 1px 0 rgba(255,255,255,0.04);
}

/* Hairline across the middle */
.fc-digit::after {
    content: '';
    position: absolute;
    left: 0; right: 0;
    top: calc(50% - 0.5px);
    height: 1px;
    background: rgba(0,0,0,0.45);
    z-index: 10;
    pointer-events: none;
}

/* Static top/bottom halves + animated flip halves */
.fc-half {
    position: absolute;
    left: 0; right: 0;
    height: 50%;
    overflow: hidden;
    display: flex;
    justify-content: center;
}

.fc-top         { top: 0;    align-items: flex-start; }
.fc-bottom      { bottom: 0; align-items: flex-end; }
.fc-flip-top    { top: 0;    align-items: flex-start; background: #1e293b; transform-origin: bottom center; }
.fc-flip-bottom { bottom: 0; align-items: flex-end;   background: #1e293b; transform-origin: top center;    transform: rotateX(90deg); }

[data-theme="dark"] .fc-flip-top,
[data-theme="dark"] .fc-flip-bottom { background: #0f172a; }

.fc-half span {
    display: block;
    font-size: 15px;
    font-weight: 800;
    color: #f1f5f9;
    line-height: 25px; /* full tile height so the number is centred across both halves */
    font-variant-numeric: tabular-nums;
    user-select: none;
}

/* The overlay wrapper — hidden when not animating */
.fc-flip-wrap {
    position: absolute;
    inset: 0;
    pointer-events: none;
    perspective: 160px;
}

/* Flip animations — triggered by adding .fc-flipping */
.fc-flip-wrap.fc-flipping .fc-flip-top {
    animation: fc-top-away 0.18s ease-in forwards;
}

.fc-flip-wrap.fc-flipping .fc-flip-bottom {
    animation: fc-bottom-in 0.18s ease-out 0.18s forwards;
}

@keyframes fc-top-away {
    from { transform: rotateX(0deg); }
    to   { transform: rotateX(-90deg); }
}

@keyframes fc-bottom-in {
    from { transform: rotateX(90deg); }
    to   { transform: rotateX(0deg); }
}

/* Dot separators between groups */
.fc-colon {
    display: flex;
    flex-direction: column;
    gap: 7px;
    /* align dots with digit centre, offset for the unit label below */
    margin-bottom: 14px;
}

.fc-colon span {
    display: block;
    width: 3px;
    height: 3px;
    border-radius: 50%;
    background: var(--text-tertiary);
    opacity: 0.4;
}

/* --- Privacy Link --- */
.privacy-link {
    margin: 4px 0 0;
    text-align: center;
    font-size: 0.75rem;
}

.privacy-link a {
    color: var(--text-tertiary);
    text-decoration: none;
}

.privacy-link a:hover {
    color: var(--text-secondary);
    text-decoration: underline;
}

/* --- Offline Banner --- */
#offline-banner {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 9999;
    background: #c0392b;
    color: #fff;
    text-align: center;
    padding: 10px 16px;
    font-size: 0.88rem;
    font-weight: 500;
    letter-spacing: 0.01em;
    transform: translateY(-100%);
    transition: transform 250ms cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: none;
}

#offline-banner.visible {
    transform: translateY(0);
}

/* --- Inline Error Message --- */
.error-row {
    justify-content: center !important;
}

.error-message {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 16px;
    background: #fff5f5;
    border: 1px solid #fecaca;
    border-radius: 12px;
    font-size: 0.87rem;
    color: #b91c1c;
    max-width: 480px;
}

.error-icon {
    font-size: 1rem;
    flex-shrink: 0;
}

.error-text {
    flex: 1;
}

.error-retry {
    background: transparent;
    border: 1px solid #b91c1c;
    color: #b91c1c;
    border-radius: 8px;
    padding: 4px 12px;
    font-size: 0.82rem;
    font-family: inherit;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s, color 0.15s;
    flex-shrink: 0;
}

.error-retry:hover {
    background: #b91c1c;
    color: #fff;
}

/* --- Message Counter --- */
#msg-counter {
    display: block;
    text-align: right;
    font-size: 0.77rem;
    color: var(--text-tertiary);
    padding: 4px 4px 0;
    transition: color 0.2s ease;
}

#msg-counter.urgent {
    color: #d97706;
    font-weight: 500;
}

/* --- Daily Limit Bubble (friendly tutor message) --- */
.dlm-bubble {
    font-size: 1.05rem;
    line-height: 1.6;
    color: var(--text-primary);
}

.dlm-bubble p { margin: 0 0 10px; }
.dlm-bubble p:last-of-type { margin-bottom: 0; }

/* --- Session Limit Reached Message (legacy) --- */
.limit-row {
    justify-content: center !important;
}

.limit-message {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 18px;
    background: #fffbeb;
    border: 1px solid #fde68a;
    border-radius: 12px;
    font-size: 0.87rem;
    color: #92400e;
    max-width: 480px;
}

.limit-text {
    flex: 1;
    line-height: 1.5;
}

.limit-new-chat {
    background: #000;
    border: none;
    color: #fff;
    border-radius: 8px;
    padding: 6px 14px;
    font-size: 0.82rem;
    font-family: inherit;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;
    transition: background 0.15s;
}

.limit-new-chat:hover {
    background: #333;
}

/* Standalone closing question/task on closing turns (wrapper in chatLog, not inside a bubble) */
.lesson-standalone-closing {
    width: 100%;
    max-width: var(--max-width);
    margin: 0 auto 8px;
    padding: 0 20px;
    box-sizing: border-box;
}

/* Inner block inside standalone wrapper — reset the top margin (wrapper handles spacing) */
.lesson-standalone-closing .closing-prompt-block {
    margin-top: 0;
}

/* --- Closing options (two-button choice: harder example or new lesson) --- */
.closing-options {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.closing-option-btn {
    flex: 1;
    min-width: 160px;
    padding: 12px 14px;
    border: 1.5px solid var(--border-color);
    border-radius: 12px;
    background: var(--bg-secondary);
    cursor: pointer;
    font-family: inherit;
    display: flex;
    align-items: center;
    gap: 12px;
    text-align: left;
    transition: border-color 0.15s, background 0.15s;
    color: var(--text-primary);
}

.closing-option-btn-icon {
    width: 34px;
    height: 34px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
}

.closing-option-btn-text {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.closing-option-btn-title {
    font-size: 13.5px;
    font-weight: 600;
    line-height: 1.3;
    color: var(--text-primary);
}

.closing-option-btn-sub {
    font-size: 11.5px;
    line-height: 1.3;
    overflow-wrap: break-word;
}

.closing-option-btn--extension .closing-option-btn-icon {
    background: rgba(59, 124, 244, 0.12);
    color: #3b7cf4;
}
.closing-option-btn--extension .closing-option-btn-sub {
    color: #3b7cf4;
}
.closing-option-btn--extension:hover {
    border-color: #3b7cf4;
    background: rgba(59, 124, 244, 0.05);
}

.closing-option-btn--bridge .closing-option-btn-icon {
    background: rgba(22, 163, 74, 0.12);
    color: #16a34a;
}
.closing-option-btn--bridge .closing-option-btn-sub {
    color: #16a34a;
}
.closing-option-btn--bridge:hover {
    border-color: #16a34a;
    background: rgba(22, 163, 74, 0.05);
}

[data-theme="dark"] .closing-option-btn--extension .closing-option-btn-icon {
    background: rgba(102, 153, 255, 0.15);
    color: #6699ff;
}
[data-theme="dark"] .closing-option-btn--extension .closing-option-btn-sub {
    color: #6699ff;
}
[data-theme="dark"] .closing-option-btn--extension:hover {
    border-color: #6699ff;
    background: rgba(102, 153, 255, 0.08);
}
[data-theme="dark"] .closing-option-btn--bridge .closing-option-btn-icon {
    background: rgba(34, 197, 94, 0.12);
    color: #4ade80;
}
[data-theme="dark"] .closing-option-btn--bridge .closing-option-btn-sub {
    color: #4ade80;
}
[data-theme="dark"] .closing-option-btn--bridge:hover {
    border-color: #4ade80;
    background: rgba(34, 197, 94, 0.06);
}

/* --- Lesson Banner (opening chapter divider) --- */
.lesson-banner {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    max-width: var(--max-width);
    margin: 4px auto 20px;
    padding: 0 20px;
    box-sizing: border-box;
}

.lesson-banner::before,
.lesson-banner::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border-color);
}

.lesson-banner-text {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-tertiary);
    letter-spacing: 0.06em;
    text-transform: uppercase;
    white-space: nowrap;
}

/* --- Lesson Summary Card (closing turn) --- */
.lesson-summary-card {
    width: 100%;
    max-width: var(--max-width);
    margin: 4px auto 24px;
    padding: 0 20px;
    box-sizing: border-box;
}

.lesson-summary-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    background: #f0fdf4;
    border: 1px solid #bbf7d0;
    border-bottom: none;
    border-radius: 12px 12px 0 0;
    font-size: 0.82rem;
    font-weight: 700;
    color: #166534;
    letter-spacing: 0.01em;
}

.lesson-summary-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    background: #22c55e;
    color: #fff;
    border-radius: 50%;
    font-size: 0.65rem;
    font-weight: 700;
    flex-shrink: 0;
}

.lesson-summary-body {
    padding: 14px 16px;
    background: var(--bg-color);
    border: 1px solid #bbf7d0;
    border-radius: 0 0 12px 12px;
    font-size: 0.95rem;
    line-height: 1.6;
    color: var(--text-primary);
}

.lesson-summary-body p:last-child {
    margin-bottom: 0;
}

.lesson-summary-body ul {
    padding-left: 20px;
    margin: 8px 0 0;
}

.lesson-summary-body li {
    margin-bottom: 6px;
}

[data-theme="dark"] .lesson-summary-header {
    background: rgba(34, 197, 94, 0.1);
    border-color: rgba(34, 197, 94, 0.22);
    color: #86efac;
}

[data-theme="dark"] .lesson-summary-body {
    border-color: rgba(34, 197, 94, 0.22);
}

/* --- Dark Mode Zero State --- */

/* Glassmorphism input card — idle dark mode only */
[data-theme="dark"] body.is-idle #input-anchor {
    background: rgba(255,255,255,0.06);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 28px;
    padding: 10px;
    box-shadow: 0 4px 24px rgba(0,0,0,0.3), inset 0 1px 0 rgba(255,255,255,0.08);
}

[data-theme="dark"] body.is-idle .input-wrapper {
    border-color: rgba(255,255,255,0.08);
    border-radius: 18px;
    background-image:
        linear-gradient(rgba(59,124,244,0.07) 1px, transparent 1px),
        linear-gradient(90deg, rgba(59,124,244,0.07) 1px, transparent 1px);
}

/* Ensure conversation-wrapper sits above the Lorenz background */
[data-theme="dark"] #conversation-wrapper {
    position: relative;
    z-index: 1;
}

/* Search input text color in dark mode */
[data-theme="dark"] #search-input {
    color: var(--text-primary);
}
