:root {
    color-scheme: light;
    --ink: #17213b;
    --muted: #64708a;
    --line: #dfe4ef;
    --surface: #ffffff;
    --canvas: #f7f8fc;
    --primary: #5046e5;
    --primary-dark: #3e35c8;
    --primary-soft: #efefff;
    --teal: #087f79;
    --danger: #b42318;
    --shadow: 0 18px 55px rgba(31, 40, 73, .09);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    color: var(--ink);
    background: var(--canvas);
}

* { box-sizing: border-box; }
html { min-width: 320px; }
body { min-height: 100vh; margin: 0; background: var(--canvas); }
a { color: var(--primary); }
button, input, select, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
:focus-visible { outline: 3px solid rgba(80, 70, 229, .35); outline-offset: 3px; }

.skip-link { position: fixed; z-index: 100; top: 8px; left: 8px; transform: translateY(-150%); padding: 10px 14px; border-radius: 10px; background: var(--ink); color: #fff; }
.skip-link:focus { transform: translateY(0); }
.shell { width: min(100% - 32px, 1180px); margin-inline: auto; }
.topbar { position: sticky; z-index: 30; top: 0; border-bottom: 1px solid rgba(223, 228, 239, .85); background: rgba(247, 248, 252, .92); backdrop-filter: blur(18px); }
.topbar__inner { display: flex; min-height: 68px; align-items: center; justify-content: space-between; gap: 20px; }
.brand { display: inline-flex; align-items: center; gap: 10px; color: var(--ink); font-weight: 800; letter-spacing: -.03em; text-decoration: none; }
.brand__mark { display: grid; width: 34px; height: 34px; place-items: center; border-radius: 11px; color: #fff; background: linear-gradient(145deg, var(--primary), #756cf2); box-shadow: 0 8px 22px rgba(80, 70, 229, .28); }
.brand__accent { color: var(--primary); }
.desktop-nav { display: flex; align-items: center; gap: 22px; }
.desktop-nav a, .text-button { color: #45516c; font-weight: 700; text-decoration: none; }
.desktop-nav a:hover, .text-button:hover { color: var(--primary); }
.desktop-nav a.is-active { color: var(--primary); }
.desktop-nav form { margin: 0; }
.text-button { min-height: 44px; padding: 0; border: 0; background: transparent; cursor: pointer; }
.credit-pill, .mobile-credit { display: inline-flex; min-height: 36px; align-items: center; gap: 7px; padding: 7px 11px; border: 1px solid #d9d8fb; border-radius: 999px; color: var(--primary-dark); background: var(--primary-soft); font-size: 14px; font-weight: 800; text-decoration: none; }
.mobile-credit { display: none; }
.page-main { padding-block: clamp(28px, 5vw, 64px) 96px; }

.button, button.primary {
    display: inline-flex;
    min-height: 48px;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 11px 18px;
    border: 1px solid transparent;
    border-radius: 14px;
    color: #fff;
    background: var(--primary);
    font-weight: 800;
    text-decoration: none;
    cursor: pointer;
}
.button:hover, button.primary:hover { background: var(--primary-dark); }
.button--soft, .button.secondary { border-color: #dcdbfb; color: var(--primary-dark); background: var(--primary-soft); }
.button--soft:hover, .button.secondary:hover { background: #e3e2ff; }
.button--small { min-height: 40px; padding: 8px 14px; border-radius: 11px; }
.actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.link-button { min-height: 44px; padding: 0; border: 0; color: var(--primary); background: transparent; font-weight: 800; cursor: pointer; }

.card { width: min(100%, 760px); margin-inline: auto; padding: clamp(24px, 6vw, 52px); border: 1px solid var(--line); border-radius: 26px; background: var(--surface); box-shadow: var(--shadow); }
.narrow { width: min(100%, 560px); }
.badge { display: inline-flex; padding: 7px 12px; border-radius: 999px; color: var(--primary-dark); background: var(--primary-soft); font-size: 13px; font-weight: 800; }
h1 { margin: 20px 0 14px; font-size: clamp(36px, 7vw, 64px); line-height: .98; letter-spacing: -.055em; }
h2 { margin-top: 0; letter-spacing: -.025em; }
h3 { margin: 0; }
p { color: var(--muted); font-size: 16px; line-height: 1.6; }
.eyebrow { margin: 0 0 8px; color: var(--teal); font-size: 12px; font-weight: 900; letter-spacing: .12em; text-transform: uppercase; }
.muted { color: var(--muted); }

form p { margin: 0 0 18px; }
label { display: block; margin-bottom: 7px; font-weight: 800; }
input, select, textarea { width: 100%; min-height: 48px; padding: 11px 13px; border: 1px solid #bdc7d9; border-radius: 12px; color: var(--ink); background: #fff; }
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 4px rgba(80, 70, 229, .1); outline: 0; }
.helptext { display: block; margin-top: 6px; color: var(--muted); font-size: 13px; }
ul.errorlist { padding-left: 20px; color: var(--danger); }
dl { display: grid; grid-template-columns: minmax(125px, max-content) 1fr; gap: 14px 20px; }
dt { color: var(--muted); }
dd { margin: 0; font-weight: 750; }
.messages { display: grid; gap: 8px; margin-bottom: 16px; }
.notice { padding: 13px 16px; border: 1px solid #c9d9ff; border-radius: 12px; color: var(--ink); background: #f1f5ff; }
.notice.error { border-color: #f5c2bd; color: #7a271a; background: #fff3f2; }
.telegram-login-card { text-align: center; }
.telegram-login-card h2 { margin-top: 20px; }
.telegram-widget { display: flex; min-height: 54px; align-items: center; justify-content: center; margin: 28px 0 18px; }
.telegram-privacy { margin-bottom: 0; font-size: 13px; }

.app-layout { display: grid; min-height: calc(100vh - 164px); grid-template-columns: 290px minmax(0, 1fr); gap: 18px; }
.history-panel, .workspace-panel { border: 1px solid var(--line); border-radius: 24px; background: var(--surface); box-shadow: 0 12px 35px rgba(31, 40, 73, .06); }
.history-panel { padding: 18px; }
.history-panel__head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 16px; }
.new-chat-button { display: grid; width: 38px; height: 38px; flex: 0 0 auto; place-items: center; border: 1px solid #dcdbfb; border-radius: 12px; color: var(--primary); background: var(--primary-soft); font-size: 24px; font-weight: 500; line-height: 1; text-decoration: none; }
.history-list { display: grid; gap: 7px; }
.history-item { display: block; min-width: 0; padding: 12px; border: 1px solid transparent; border-radius: 13px; color: var(--ink); background: #fafbfe; text-decoration: none; }
.history-item:hover { border-color: #dddffc; background: #f5f5ff; }
.history-item.is-active { border-color: #cccafa; background: var(--primary-soft); }
.history-item strong { display: block; overflow: hidden; margin-bottom: 4px; text-overflow: ellipsis; white-space: nowrap; }
.history-item small { color: var(--muted); }
.history-item span { display: block; overflow: hidden; margin-top: 6px; color: var(--muted); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.workspace-panel { display: grid; align-content: center; padding: clamp(24px, 5vw, 64px); }
.workspace-panel--chat { align-content: stretch; padding: 0; overflow: hidden; }
.workspace-empty { width: min(100%, 720px); margin-inline: auto; text-align: center; }
.workspace-icon { display: grid; width: 66px; height: 66px; margin: 0 auto 20px; place-items: center; border-radius: 22px; color: #fff; background: linear-gradient(145deg, var(--primary), #746bef); box-shadow: 0 16px 35px rgba(80, 70, 229, .25); font-size: 29px; }
.workspace-empty h1 { margin: 0 0 12px; font-size: clamp(30px, 5vw, 48px); }
.textbook-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-top: 28px; text-align: left; }
.textbook-tile { min-width: 0; padding: 16px; border: 1px solid var(--line); border-radius: 16px; background: #fbfcff; }
.textbook-tile strong { display: block; margin-bottom: 6px; }
.textbook-meta { color: var(--muted); font-size: 13px; }
.empty-callout { margin-top: 26px; padding: 18px; border: 1px dashed #c9cee0; border-radius: 16px; color: var(--muted); background: #fafbfe; text-align: left; }
.conversation-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; padding: 24px 28px; border-bottom: 1px solid var(--line); }
.conversation-head h1 { margin: 0 0 7px; font-size: clamp(24px, 4vw, 34px); line-height: 1.1; }
.conversation-head p { margin: 0; font-size: 14px; }
.status-dot { display: inline-flex; flex: 0 0 auto; align-items: center; gap: 7px; padding: 7px 11px; border-radius: 999px; color: #7a5d16; background: #fff6d9; font-size: 12px; font-weight: 800; }
.status-dot::before { width: 7px; height: 7px; border-radius: 50%; background: currentColor; content: ""; }
.status-dot--active { color: #087f5b; background: #e9f8f1; }
.message-list { display: grid; align-content: start; gap: 22px; min-height: 300px; padding: 28px; }
.message { max-width: min(88%, 720px); }
.message p { margin: 5px 0 0; }
.message__role { color: var(--muted); font-size: 12px; font-weight: 900; letter-spacing: .06em; text-transform: uppercase; }
.message--question { justify-self: end; padding: 14px 17px; border-radius: 18px 18px 5px 18px; background: var(--primary-soft); }
.message--question p { color: var(--ink); }
.message--answer { justify-self: start; width: min(100%, 720px); }
.answer-copy { margin-top: 8px; color: var(--ink); font-size: 16px; line-height: 1.7; }
.answer-note { padding-left: 12px; border-left: 3px solid #c8cad8; font-size: 13px; }
.answer-state { margin-top: 8px; padding: 15px; border-radius: 14px; }
.answer-state p { font-size: 14px; }
.answer-state--error { border: 1px solid #f0cbc6; color: #7a271a; background: #fff5f4; }
.pending-copy { display: flex; align-items: center; gap: 10px; }
.spinner { width: 18px; height: 18px; border: 2px solid #d7d5fc; border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
.source-list { display: grid; gap: 10px; margin-top: 18px; }
.source-card { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 11px 18px; padding: 15px; border: 1px solid var(--line); border-radius: 15px; background: #fbfcff; }
.source-card strong, .source-card small { display: block; }
.source-card small { margin-top: 4px; color: var(--muted); }
.source-card__label { display: block; margin-bottom: 4px; color: var(--teal); font-size: 11px; font-weight: 900; letter-spacing: .08em; text-transform: uppercase; }
.source-card blockquote { grid-column: 1 / -1; margin: 0; padding-left: 12px; border-left: 3px solid #d6d5f9; color: var(--muted); font-size: 13px; line-height: 1.55; }
.source-card .button { grid-column: 2; grid-row: 1; align-self: center; }
.question-composer { margin: auto 20px 20px; padding: 18px; border: 1px solid #d9def0; border-radius: 19px; background: #fbfcff; box-shadow: 0 12px 28px rgba(31, 40, 73, .08); }
.workspace-empty + .question-composer { width: min(calc(100% - 40px), 760px); margin: 32px auto 0; }
.composer-field { margin-bottom: 13px; }
.composer-field label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; }
.composer-field textarea { min-height: 82px; resize: vertical; }
.composer-field--book select { background-color: #fff; }
.composer-actions { display: flex; align-items: center; justify-content: space-between; gap: 14px; color: var(--muted); font-size: 13px; }
.composer-actions strong { color: var(--primary); }
.submit-pending { display: none; }
.primary.is-pending [data-submit-label] { display: none; }
.primary.is-pending .submit-pending { display: inline; }
.primary:disabled { cursor: wait; opacity: .72; }
.read-only-callout { margin: 20px; padding: 16px; border: 1px solid #ecd99c; border-radius: 14px; color: #715d1b; background: #fff9e8; font-size: 14px; line-height: 1.55; }
.mobile-nav { display: none; }

@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 760px) {
    .shell { width: min(100% - 24px, 1180px); }
    .topbar__inner { min-height: 60px; }
    .desktop-nav { display: none; }
    .mobile-credit { display: inline-flex; }
    .page-main { padding-block: 18px calc(92px + env(safe-area-inset-bottom)); }
    .card { padding: 24px 20px; border-radius: 20px; }
    .app-layout { min-height: auto; grid-template-columns: 1fr; }
    .history-panel { order: 2; border-radius: 18px; }
    .workspace-panel { min-height: calc(100vh - 170px); padding: 30px 18px; border-radius: 20px; }
    .workspace-panel--chat { min-height: 0; padding: 0; }
    .conversation-head { display: grid; padding: 20px; }
    .message-list { gap: 18px; padding: 20px 16px; }
    .message { max-width: 94%; }
    .message--answer { max-width: 100%; }
    .source-card { grid-template-columns: 1fr; }
    .source-card .button { grid-column: 1; grid-row: auto; width: 100%; }
    .question-composer { position: static; margin: auto 10px 10px; padding: 13px; }
    .workspace-empty + .question-composer { width: 100%; margin: 24px 0 0; }
    .composer-actions span { font-size: 11px; }
    .textbook-grid { grid-template-columns: 1fr; }
    .mobile-nav { position: fixed; z-index: 40; right: 0; bottom: 0; left: 0; display: grid; grid-template-columns: repeat(3, 1fr); padding: 7px 12px calc(7px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: rgba(255, 255, 255, .96); backdrop-filter: blur(18px); }
    .mobile-nav a { display: flex; min-height: 49px; flex-direction: column; align-items: center; justify-content: center; gap: 2px; border-radius: 12px; color: #59647c; font-size: 11px; font-weight: 800; text-decoration: none; }
    .mobile-nav a.is-active { color: var(--primary); background: var(--primary-soft); }
    dl { grid-template-columns: 1fr; gap: 5px; }
    dd { margin-bottom: 12px; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
