/* Physik-Tutor — Mobile-first chat UI */

:root {
    --bg:        #f5f5f7;
    --surface:   #ffffff;
    --text:      #1d1d1f;
    --text-dim:  #86868b;
    --accent:    #5e5ce6;
    --accent-light: #e8e7fc;
    --user-bg:   #5e5ce6;
    --user-text: #ffffff;
    --bot-bg:    #ffffff;
    --bot-text:  #1d1d1f;
    --border:    #e5e5ea;
    --danger:    #ff3b30;
    --radius:    16px;
    --radius-sm: 10px;
    --shadow:    0 1px 3px rgba(0,0,0,0.08);
    --font:      -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg:        #1c1c1e;
        --surface:   #2c2c2e;
        --text:      #f5f5f7;
        --text-dim:  #98989d;
        --accent-light: #2d2d4e;
        --bot-bg:    #2c2c2e;
        --bot-text:  #f5f5f7;
        --border:    #3a3a3c;
        --shadow:    0 1px 3px rgba(0,0,0,0.3);
    }
}

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

html, body {
    height: 100%;
    overflow: hidden;
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    -webkit-text-size-adjust: 100%;
}

.screen { height: 100%; }

/* ===== Login ===== */
#login-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.login-box {
    text-align: center;
    max-width: 320px;
    width: 100%;
}

.login-box h1 {
    font-size: 2rem;
    font-weight: 700;
    margin-bottom: 4px;
}

.login-box p {
    color: var(--text-dim);
    margin-bottom: 24px;
    font-size: 0.95rem;
}

.login-box input {
    width: 100%;
    padding: 14px 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 1rem;
    background: var(--surface);
    color: var(--text);
    margin-bottom: 12px;
    outline: none;
    transition: border-color 0.2s;
}

.login-box input:focus {
    border-color: var(--accent);
}

.login-box button {
    width: 100%;
    padding: 14px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.2s;
}

.login-box button:hover { opacity: 0.85; }

.error {
    color: var(--danger);
    font-size: 0.85rem;
    margin-top: 10px;
}

/* ===== Chat layout ===== */
#chat-screen {
    display: flex;
    flex-direction: column;
}

header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

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

header h1 {
    font-size: 1.1rem;
    font-weight: 600;
}

header button {
    background: none;
    border: none;
    font-size: 1.3rem;
    cursor: pointer;
    color: var(--accent);
    padding: 4px 8px;
    border-radius: 6px;
    transition: background 0.15s;
}

header button:hover {
    background: var(--accent-light);
}

/* ===== Sidebar ===== */
#sidebar[hidden] { display: none; }

#sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.35);
    z-index: 99;
}
#sidebar-backdrop[hidden] { display: none; }

#sidebar {
    position: fixed;
    top: 0; left: 0;
    width: 280px;
    height: 100%;
    background: var(--surface);
    border-right: 1px solid var(--border);
    z-index: 100;
    display: flex;
    flex-direction: column;
    box-shadow: 4px 0 20px rgba(0,0,0,0.1);
}

.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
    font-weight: 600;
    border-bottom: 1px solid var(--border);
}

.sidebar-header button {
    background: none;
    border: none;
    font-size: 1.2rem;
    cursor: pointer;
    color: var(--text-dim);
}

#conv-list {
    list-style: none;
    overflow-y: auto;
    flex: 1;
    padding: 8px;
}

#conv-list li {
    padding: 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: background 0.15s;
}

#conv-list li:hover { background: var(--accent-light); }
#conv-list li.active { background: var(--accent-light); font-weight: 600; }

/* ===== Messages ===== */
main#messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
}

.welcome-msg {
    text-align: center;
    color: var(--text-dim);
    margin: auto;
    padding: 40px 20px;
    max-width: 400px;
    line-height: 1.6;
}

.welcome-msg p:first-child {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 8px;
}

.msg {
    max-width: 85%;
    padding: 12px 16px;
    border-radius: var(--radius);
    line-height: 1.55;
    font-size: 0.95rem;
    word-wrap: break-word;
    box-shadow: var(--shadow);
}

.msg.user {
    align-self: flex-end;
    background: var(--user-bg);
    color: var(--user-text);
    border-bottom-right-radius: 4px;
}

.msg.assistant {
    align-self: flex-start;
    background: var(--bot-bg);
    color: var(--bot-text);
    border-bottom-left-radius: 4px;
}

/* Markdown-like styling inside bot messages */
.msg.assistant p { margin-bottom: 8px; }
.msg.assistant p:last-child { margin-bottom: 0; }
.msg.assistant h1 { font-size: 1.15em; font-weight: 700; margin: 10px 0 4px; }
.msg.assistant h2 { font-size: 1.05em; font-weight: 700; margin: 10px 0 4px; }
.msg.assistant h3 { font-size: 0.98em; font-weight: 600; margin: 8px 0 4px; }
.msg.assistant strong { font-weight: 600; }
.msg.assistant code {
    background: rgba(0,0,0,0.06);
    padding: 2px 5px;
    border-radius: 4px;
    font-size: 0.88em;
}
.msg.assistant ul, .msg.assistant ol {
    margin: 6px 0 6px 20px;
}

/* Loading dots */
.msg.loading::after {
    content: '';
    display: inline-block;
    width: 18px;
    animation: dots 1.2s steps(4) infinite;
}
@keyframes dots {
    0%   { content: ''; }
    25%  { content: '.'; }
    50%  { content: '..'; }
    75%  { content: '...'; }
}

/* ===== Input ===== */
footer {
    padding: 10px 16px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.input-row {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

#msg-input {
    flex: 1;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 1rem;
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    resize: none;
    outline: none;
    max-height: 120px;
    line-height: 1.4;
    transition: border-color 0.2s;
}

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

#send-btn {
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    font-size: 1.2rem;
    cursor: pointer;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.2s;
}

#send-btn:hover { opacity: 0.85; }
#send-btn:disabled { opacity: 0.4; cursor: default; }

/* ===== KaTeX overrides ===== */
.katex { font-size: 1em; }
.katex-display { margin: 10px 0; overflow-x: visible; }

/* ===== Markdown tables ===== */
.msg.assistant table {
    border-collapse: collapse;
    width: 100%;
    font-size: 0.88em;
    margin: 8px 0;
}
.msg.assistant th,
.msg.assistant td {
    border: 1px solid var(--border);
    padding: 6px 10px;
    text-align: left;
}
.msg.assistant th {
    background: var(--accent-light);
    font-weight: 600;
}
