/* 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-logo {
    width: 100%;
    height: auto;
    display: block;
    margin-bottom: 16px;
}

.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;
}

/* ===== Subject picker (login) ===== */
.subject-picker {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}

/* Специфичность .subject-picker .subject-card бьёт .login-box button,
   иначе кнопки предметов красятся как главная (фиолетовый фон). */
.subject-picker .subject-card {
    flex: 1;
    width: auto;
    padding: 14px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: border-color 0.15s, background 0.15s;
}

.subject-picker .subject-card:hover { opacity: 1; }

.subject-picker .subject-card.selected {
    text-decoration: underline;
    text-decoration-color: var(--accent);
    text-underline-offset: 4px;
    text-decoration-thickness: 3px;
}

/* ===== 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, .header-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

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

/* Переключатель предмета (заменяет заголовок в хедере) */
.subject-switch {
    font-size: 1.1rem !important;
    font-weight: 600;
    color: var(--text) !important;
}

.subject-switch::after {
    content: ' ▾';
    font-size: 0.7em;
    color: var(--accent);
}

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;
    background: var(--bg) center / cover no-repeat;
}

/* Фон чата свой для каждого предмета (data-subject ставит app.js на <body>) */
body[data-subject="physik"] main#messages { background-image: url('background.jpg'); }
body[data-subject="mathe"]  main#messages { background-image: url('background-mathe.jpg'); }

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

.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;
}

/* ===== Modals ===== */
#topic-modal[hidden], #progress-modal[hidden] { display: none; }

.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 200;
}

.modal-box {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: var(--surface);
    border-radius: var(--radius);
    padding: 20px;
    width: min(90vw, 420px);
    max-height: 80vh;
    overflow-y: auto;
    z-index: 201;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.modal-header h2 { font-size: 1.1rem; font-weight: 700; }

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

#topic-start-btn {
    margin-top: 12px;
    padding: 12px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.2s;
}
#topic-start-btn:disabled { opacity: 0.35; cursor: default; }
#topic-start-btn:not(:disabled):hover { opacity: 0.85; }

/* Topic / progress list items */
.topic-chapter-header {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-dim);
    padding: 14px 8px 4px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.topic-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 8px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background 0.15s;
}
.topic-item:hover { background: var(--accent-light); }
.topic-item.selected { background: var(--accent-light); }
.topic-item .topic-date {
    margin-left: auto;
    font-size: 0.78em;
    color: var(--text-dim);
    white-space: nowrap;
}

/* Grade badge (circle with number 1–6) */
.grade-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    font-size: 0.82rem;
    font-weight: 700;
    flex-shrink: 0;
}
.grade-1, .grade-2 { background: #34c759; color: #fff; }
.grade-3            { background: #ffcc00; color: #000; }
.grade-4            { background: #ff9500; color: #fff; }
.grade-5, .grade-6  { background: #ff3b30; color: #fff; }
.grade-null         { background: var(--border); color: var(--text-dim); }

/* Grade toast notification */
.grade-toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px 20px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.18);
    z-index: 300;
    text-align: center;
    font-size: 0.95rem;
    animation: toastIn 0.25s ease;
    white-space: nowrap;
}
@keyframes toastIn {
    from { opacity: 0; transform: translateX(-50%) translateY(16px); }
    to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ===== Quiz ===== */
/* Segmented-тумблер Lernen|Quiz в модалке выбора темы */
.mode-toggle {
    display: flex;
    gap: 4px;
    padding: 3px;
    margin: 0 0 12px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}
.mode-toggle button {
    flex: 1;
    padding: 8px 10px;
    border: none;
    border-radius: 8px;
    background: none;
    color: var(--text-dim);
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
}
.mode-toggle button.active {
    background: var(--accent);
    color: #fff;
}

/* Кнопка «Quiz» в хедере */
.quiz-btn {
    font-size: 0.9rem !important;
    font-weight: 600;
    border: 1px solid var(--accent) !important;
    color: var(--accent) !important;
}

/* Ряд тап-вариантов под вопросом квиза */
.quiz-options {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin: -4px 0 4px;
    align-self: flex-start;
    max-width: 85%;
}
.quiz-option {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 44px;
    padding: 8px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font-size: 1rem;
    text-align: left;
    cursor: pointer;
    transition: border-color 0.15s, opacity 0.15s;
}
.quiz-option:hover { border-color: var(--accent); }
.quiz-letter {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    flex-shrink: 0;
    border-radius: 50%;
    background: var(--accent-light);
    color: var(--accent);
    font-weight: 700;
    font-size: 0.85rem;
}
.quiz-options.answered .quiz-option { opacity: 0.5; cursor: default; }
