/* ═══════════════════════════════════════════
   BUS 361 Course Assistant — Design System
   Warm monochrome · DM Sans + Instrument Serif
   ═══════════════════════════════════════════ */

/* --- Design Tokens --- */
:root {
    --bg: #f5f5f4;
    --surface: #ffffff;
    --surface-alt: #ededec;
    --text-1: #0c0a09;
    --text-2: #3f3b37;
    --text-3: #6e6a65;
    --border: #d4d1cd;
    --border-hover: #a8a29e;
    --accent: #1d4ed8;
    --accent-light: rgba(29, 78, 216, 0.1);
    --user-bg: #1c1917;
    --user-text: #fafaf9;
    --error: #dc2626;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);

    --font-sans: 'DM Sans', system-ui, -apple-system, sans-serif;
    --font-serif: 'Instrument Serif', Georgia, serif;
    --leading: 1.6;
    --text-sm: 0.875rem;
    --text-base: 1rem;
    --text-lg: 1.125rem;

    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 20px;
    --radius-full: 9999px;

    --ease: cubic-bezier(0.4, 0, 0.2, 1);
    --dur: 0.2s;
    --header-h: 56px;
    --max-w: 720px;
}

html[data-theme="dark"] {
    --bg: #0c0a09;
    --surface: #1c1917;
    --surface-alt: #292524;
    --text-1: #fafaf9;
    --text-2: #d6d3d1;
    --text-3: #a8a29e;
    --border: #33302d;
    --border-hover: #57534e;
    --accent: #60a5fa;
    --accent-light: rgba(96, 165, 250, 0.12);
    --user-bg: #fafaf9;
    --user-text: #1c1917;
    --error: #ef4444;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.25);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.35);
}

/* --- Reset --- */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html {
    height: 100%;
    -webkit-text-size-adjust: 100%;
}

body {
    height: 100%;
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: var(--leading);
    color: var(--text-1);
    background: var(--bg);
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overscroll-behavior: none;
    transition: background-color var(--dur) var(--ease), color var(--dur) var(--ease);
}

button {
    font: inherit;
    border: none;
    background: none;
    cursor: pointer;
    color: inherit;
}

textarea {
    font: inherit;
    border: none;
    background: none;
    resize: none;
    color: inherit;
}

ul { list-style: none; }

/* --- App Shell --- */
.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    max-width: 860px;
    margin: 0 auto;
}

/* --- Header --- */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--header-h);
    padding: 0 24px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    background: var(--bg);
    position: sticky;
    top: 0;
    z-index: 100;
    transition: background-color var(--dur) var(--ease), border-color var(--dur) var(--ease);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.logo {
    font-weight: 600;
    font-size: var(--text-lg);
    letter-spacing: -0.01em;
    color: var(--text-1);
}

.sep {
    color: var(--text-3);
    font-weight: 300;
    font-size: var(--text-lg);
    user-select: none;
}

.label {
    color: var(--text-2);
    font-size: var(--text-base);
    font-weight: 400;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 2px;
}

.icon-btn {
    width: 38px;
    height: 38px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    color: var(--text-2);
    transition: all var(--dur) var(--ease);
}

.icon-btn:hover {
    background: var(--surface-alt);
    color: var(--text-1);
}

/* Theme icon visibility */
html[data-theme="light"] .icon-sun { display: none; }
html[data-theme="dark"] .icon-moon { display: none; }

/* --- Main Area --- */
.main {
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    position: relative;
}

/* --- Welcome State --- */
.welcome {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px 24px;
    gap: 48px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.welcome-content {
    text-align: center;
    max-width: 520px;
}

.welcome-title {
    font-family: var(--font-sans);
    font-size: 2.75rem;
    font-weight: 600;
    line-height: 1.12;
    letter-spacing: -0.035em;
    color: var(--text-1);
    margin-bottom: 20px;
    animation: fadeUp 0.6s var(--ease) both;
}

.welcome-title em {
    font-family: var(--font-serif);
    font-style: italic;
    font-weight: 400;
    letter-spacing: -0.01em;
}

.welcome-desc {
    font-size: var(--text-lg);
    color: var(--text-2);
    line-height: 1.65;
    animation: fadeUp 0.5s var(--ease) 0.08s both;
}

.suggestions {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
    width: 100%;
    max-width: 560px;
}

.suggestion-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 16px 18px;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface);
    color: var(--text-1);
    font-size: var(--text-base);
    line-height: 1.4;
    text-align: left;
    cursor: pointer;
    transition: all var(--dur) var(--ease);
    animation: fadeUp 0.4s var(--ease) both;
}

.suggestion-card:nth-child(1) { animation-delay: 0.18s; }
.suggestion-card:nth-child(2) { animation-delay: 0.22s; }
.suggestion-card:nth-child(3) { animation-delay: 0.26s; }
.suggestion-card:nth-child(4) { animation-delay: 0.30s; }

.suggestion-card:hover {
    border-color: var(--border-hover);
    color: var(--text-1);
    box-shadow: var(--shadow-sm);
    transform: translateY(-1px);
}

.suggestion-card:active {
    transform: translateY(0);
}

.suggestion-card svg {
    flex-shrink: 0;
    opacity: 0;
    transform: translateX(-4px);
    transition: all var(--dur) var(--ease);
    color: var(--text-3);
}

.suggestion-card:hover svg {
    opacity: 0.5;
    transform: translateX(0);
}

/* --- Chat Log --- */
.chat-log {
    flex: 1;
    overflow-y: auto;
    padding: 24px 24px 80px;
    display: flex;
    flex-direction: column;
    gap: 24px;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}

.chat-log::-webkit-scrollbar { width: 5px; }
.chat-log::-webkit-scrollbar-track { background: transparent; }
.chat-log::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 999px;
}

.hidden { display: none !important; }

/* --- Messages --- */
.msg {
    display: flex;
    max-width: var(--max-w);
    width: 100%;
    animation: msgIn 0.3s var(--ease) both;
}

.msg-user {
    align-self: flex-end;
    justify-content: flex-end;
}

.msg-bot {
    align-self: flex-start;
}

.msg-bubble {
    padding: 12px 18px;
    border-radius: var(--radius-lg);
    max-width: 85%;
    word-break: break-word;
    font-size: var(--text-base);
    line-height: 1.6;
}

.msg-user .msg-bubble {
    background: var(--user-bg);
    color: var(--user-text);
    border-bottom-right-radius: 6px;
}

.msg-bot-wrap {
    display: flex;
    flex-direction: column;
    gap: 6px;
    max-width: 85%;
}

.msg-bot .msg-text {
    line-height: 1.65;
    color: var(--text-1);
    word-break: break-word;
}

.msg-bot .msg-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 2px;
}

.msg-time {
    font-size: var(--text-sm);
    color: var(--text-3);
}

.copy-btn {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    color: var(--text-3);
    opacity: 0;
    transition: all var(--dur) var(--ease);
}

.msg-bot-wrap:hover .copy-btn {
    opacity: 1;
}

.copy-btn:hover {
    background: var(--surface-alt);
    color: var(--text-1);
}

/* --- Markdown in Messages --- */
.msg-text p {
    margin-bottom: 0.5em;
}

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

.msg-text ul,
.msg-text ol {
    margin: 0.5em 0;
    padding-left: 1.5em;
}

.msg-text ul {
    list-style-type: disc;
}

.msg-text ol {
    list-style-type: decimal;
}

.msg-text li {
    margin-bottom: 0.3em;
    line-height: 1.55;
}

.msg-text li:last-child {
    margin-bottom: 0;
}

.msg-text .msg-heading {
    font-weight: 600;
    margin: 0.75em 0 0.25em;
}

.msg-text .msg-heading:first-child {
    margin-top: 0;
}

.msg-text strong {
    font-weight: 600;
}

/* --- Citations --- */
.citation-ref {
    color: var(--accent);
    font-weight: 600;
    font-size: 0.75em;
    cursor: default;
    vertical-align: super;
    margin: 0 1px;
}

.msg-sources {
    margin-top: 4px;
}

.msg-sources summary {
    font-size: var(--text-sm);
    color: var(--accent);
    cursor: pointer;
    font-weight: 500;
    padding: 4px 0;
    user-select: none;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 6px;
}

.msg-sources summary::-webkit-details-marker { display: none; }

.msg-sources summary::before {
    content: '';
    display: inline-block;
    width: 0;
    height: 0;
    border-left: 5px solid currentColor;
    border-top: 3.5px solid transparent;
    border-bottom: 3.5px solid transparent;
    transition: transform var(--dur) var(--ease);
}

.msg-sources[open] summary::before {
    transform: rotate(90deg);
}

.msg-sources ul {
    padding: 8px 0 4px 2px;
}

.msg-sources li {
    font-size: var(--text-sm);
    color: var(--text-2);
    padding: 4px 0;
    line-height: 1.5;
}

.msg-sources li strong {
    color: var(--accent);
    font-weight: 600;
}

.citation-snippet {
    color: var(--text-3);
    font-style: italic;
}

/* --- Typing Indicator --- */
.typing-dots {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 8px 0;
}

.typing-dots span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--text-3);
    animation: typingPulse 1.4s ease-in-out infinite;
}

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

/* --- Error --- */
.msg-error {
    color: var(--error);
    font-weight: 500;
}

/* --- Scroll Button --- */
.scroll-btn {
    position: absolute;
    bottom: 16px;
    right: 24px;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-full);
    background: var(--surface);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-md);
    color: var(--text-2);
    z-index: 10;
    transition: all var(--dur) var(--ease);
}

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

/* --- Footer / Input --- */
.footer {
    flex-shrink: 0;
    padding: 16px 24px 20px;
    background: var(--bg);
    transition: background-color var(--dur) var(--ease);
}

.input-wrap {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 10px 10px 18px;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface);
    transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}

.input-wrap:focus-within {
    border-color: var(--border-hover);
}

.input-wrap textarea {
    flex: 1;
    min-height: 24px;
    max-height: 160px;
    overflow-y: auto;
    line-height: 1.5;
    padding: 2px 0;
    outline: none;
    -webkit-appearance: none;
}

.input-wrap textarea::placeholder {
    color: var(--text-3);
}

.send-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    background: var(--text-1);
    color: var(--bg);
    flex-shrink: 0;
    transition: all var(--dur) var(--ease);
}

.send-btn:disabled {
    opacity: 0.2;
    cursor: default;
}

.send-btn:not(:disabled):hover {
    opacity: 0.8;
}

.send-btn:not(:disabled):active {
    transform: scale(0.92);
}

.disclaimer {
    font-size: var(--text-sm);
    color: var(--text-3);
    text-align: center;
    margin-top: 10px;
    line-height: 1.4;
}

/* --- Toast --- */
.toast {
    position: fixed;
    bottom: 32px;
    left: 50%;
    transform: translateX(-50%) translateY(8px);
    background: var(--text-1);
    color: var(--bg);
    padding: 8px 16px;
    border-radius: var(--radius-sm);
    font-size: var(--text-sm);
    font-weight: 500;
    opacity: 0;
    pointer-events: none;
    z-index: 1000;
    transition: all var(--dur) var(--ease);
}

.toast.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* --- Focus --- */
.icon-btn:focus-visible,
.send-btn:focus-visible,
.suggestion-card:focus-visible,
.scroll-btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* --- Animations --- */
@keyframes fadeUp {
    from { opacity: 0; transform: translateY(12px); }
    to { opacity: 1; transform: translateY(0); }
}

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

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

/* --- Responsive --- */
@media (max-width: 640px) {
    .header {
        padding: 0 16px;
    }

    .label, .sep {
        display: none;
    }

    .welcome {
        justify-content: flex-start;
        padding: 32px 16px;
        gap: 24px;
    }

    .welcome-title {
        font-size: 2rem;
    }

    .welcome-desc {
        font-size: var(--text-base);
    }

    .suggestions {
        grid-template-columns: 1fr;
    }

    .chat-log {
        padding: 16px 16px 80px;
        gap: 20px;
    }

    .msg-bubble,
    .msg-bot-wrap {
        max-width: 90%;
    }

    .footer {
        padding: 12px 16px 16px;
    }

    .copy-btn {
        opacity: 1;
    }

    .scroll-btn {
        right: 16px;
        bottom: 12px;
    }
}

@media (max-width: 400px) {
    .welcome-title {
        font-size: 1.75rem;
    }

    .suggestion-card {
        padding: 12px 14px;
        font-size: var(--text-sm);
    }

    .input-wrap {
        padding: 8px 8px 8px 14px;
    }
}

/* --- iOS safe areas --- */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .footer {
        padding-bottom: calc(20px + env(safe-area-inset-bottom));
    }
}

/* --- Accessibility --- */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* --- Print --- */
@media print {
    .header, .footer, .scroll-btn, .welcome { display: none; }
    .chat-log {
        overflow: visible;
        height: auto;
        padding: 0;
    }
    .copy-btn { display: none; }
}