* { box-sizing: border-box; }
:root {
  --bg: #f3eee1;
  --paper: #fdfbf4;
  --card: #fdfbf4;
  --ink: #2b2620;
  --muted: #7a7164;
  --border: #e2d9c6;
  --accent: #2e6b4e;
  --accent-dark: #245741;
  --accent-ink: #f8f5ec;
  --user-bubble: #e7ecdc;
  --danger: #a33b2d;
  --warn: #8a5a10;
  --ok: #34611f;
  --radius: 10px;
  --serif: "Literata", Georgia, "Times New Roman", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 17px/1.75 var(--serif);
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.center { text-align: center; }
.err { color: var(--danger); font-size: 14px; font-family: var(--sans); }
.row.gap { display: flex; gap: 10px; margin-top: 14px; flex-wrap: wrap; }

.view { min-height: 100dvh; display: flex; flex-direction: column; }
.center-view { justify-content: center; align-items: center; padding: 20px; }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 18px; background: var(--paper);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.topbar h1 { font-size: 21px; margin: 0; flex: 1; font-weight: 600; }
.topbar h2 { font-size: 17px; margin: 0; flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.btn {
  border: 1px solid var(--border); background: var(--paper); color: var(--ink);
  padding: 8px 16px; border-radius: var(--radius); font-size: 14.5px; cursor: pointer;
  font-family: var(--sans);
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { background: var(--accent-dark); }
.btn.ghost { background: transparent; }
.btn.big { font-size: 17px; padding: 13px 28px; }
.btn.small { font-size: 13px; padding: 5px 10px; }
.btn:disabled { opacity: .5; cursor: wait; }
.btn.quiz-btn { border-color: var(--warn); color: var(--warn); }

.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px;
  box-shadow: 0 1px 3px rgba(90, 75, 50, .06);
}
.login-card { width: 350px; text-align: center; }
.login-card h1 { margin: 0 0 4px; font-weight: 600; }
.login-card p { font-size: 15px; }
.login-card input { width: 100%; margin: 14px 0 10px; }

input[type="text"], input[type="password"], textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: var(--radius); font: 15px/1.5 var(--sans);
  background: var(--paper); color: var(--ink);
}
input:focus, textarea:focus { outline: 2px solid #b9cfc2; border-color: var(--accent); }
textarea { resize: vertical; }
label { display: block; margin: 18px 0 6px; font: 600 13.5px var(--sans); letter-spacing: .02em; color: #55503f; }

.profile-wrap { max-width: 760px; margin: 40px auto; padding: 0 20px; width: 100%; }
.profile-grid { display: flex; flex-wrap: wrap; gap: 18px; justify-content: center; margin-top: 26px; }
.profile-card {
  position: relative; width: 150px; padding: 22px 12px 16px; text-align: center;
  background: var(--paper); border: 1px solid var(--border); border-radius: 14px;
  cursor: pointer; box-shadow: 0 1px 3px rgba(90, 75, 50, .06);
}
.profile-card:hover { border-color: var(--accent); }
.profile-card.last { border-color: var(--accent); box-shadow: 0 0 0 2px #b9cfc2; }
.profile-card .pf-avatar { font-size: 46px; line-height: 1.2; }
.profile-card .pf-name { font-weight: 600; margin-top: 8px; font-size: 16px; }
.profile-card .pf-meta { font: 12.5px var(--sans); color: var(--muted); margin-top: 3px; }
.profile-card .pf-actions {
  position: absolute; top: 6px; right: 6px; display: none; gap: 2px;
}
.profile-card:hover .pf-actions { display: flex; }
.pf-actions button {
  border: none; background: none; color: var(--muted); cursor: pointer; font-size: 14px; padding: 2px 5px;
}
.pf-actions .edit:hover { color: var(--accent); }
.pf-actions .del:hover { color: var(--danger); }
.profile-card.add { border-style: dashed; color: var(--muted); }
.profile-card.add .pf-avatar { color: var(--accent); }
.avatar-chips label { font-size: 20px; padding: 6px 10px; }

.course-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px; padding: 22px; max-width: 1100px; margin: 0 auto; width: 100%;
}
.course-card { cursor: pointer; position: relative; }
.course-card h3 { margin: 0 0 8px; font-size: 18px; font-weight: 600; padding-right: 22px; }
.course-card .progress {
  height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; margin: 10px 0 6px;
}
.course-card .progress > div { height: 100%; background: var(--accent); }
.course-card .muted { font-family: var(--sans); }
.course-card .del {
  position: absolute; top: 10px; right: 10px; border: none; background: none;
  color: var(--muted); cursor: pointer; font-size: 15px;
}
.course-card .del:hover { color: var(--danger); }

.narrow { max-width: 720px; margin: 0 auto; padding: 22px; width: 100%; }
.welcome {
  background: var(--user-bubble); padding: 16px 18px; border-radius: var(--radius);
  font-size: 16px; border: 1px solid #d8e0cb;
}

.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chips label {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border); border-radius: 18px;
  padding: 7px 14px; cursor: pointer; background: var(--paper);
  margin: 0; font: 400 14px var(--sans); color: var(--ink); letter-spacing: 0;
}
.chips label:hover { border-color: var(--accent); }
.chips label:has(input:checked) {
  border-color: var(--accent); background: var(--user-bubble); font-weight: 600;
}
.chips input { position: absolute; opacity: 0; pointer-events: none; }

.lesson-row { display: flex; gap: 8px; margin-bottom: 8px; align-items: center; }
.lesson-row input { flex: 1; }
.lesson-row button { border: none; background: none; color: var(--muted); cursor: pointer; font-size: 16px; }
.lesson-row button:hover { color: var(--danger); }

.course-layout { display: flex; flex: 1; min-height: 0; }
#sidebar {
  width: 300px; flex-shrink: 0; border-right: 1px solid var(--border);
  background: #f8f4e9; padding: 18px 16px; overflow-y: auto;
  font-family: var(--sans);
}
#sidebar h3 {
  margin: 0 0 12px; font-size: 12px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .09em; color: var(--muted);
}
#lesson-list { list-style: none; margin: 0 0 18px; padding: 0; }
#lesson-list li {
  padding: 9px 10px; border-radius: 8px; cursor: pointer; font-size: 14px;
  display: flex; gap: 8px; align-items: baseline; margin-bottom: 2px; line-height: 1.45;
}
#lesson-list li:hover { background: #efe9d9; }
#lesson-list li.active { background: var(--user-bubble); font-weight: 600; }
#lesson-list li .st { flex-shrink: 0; font-size: 12px; }
#lesson-list li .score {
  margin-left: auto; font-size: 12px; background: var(--paper);
  border: 1px solid var(--border); border-radius: 10px; padding: 1px 7px;
}
#note-box summary { cursor: pointer; font-size: 13.5px; font-weight: 600; color: #55503f; }
#note-content { font-size: 13px; white-space: pre-wrap; margin-top: 8px; line-height: 1.6; }

#chat-pane { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--paper); }
#chat-scroll { flex: 1; overflow-y: auto; padding: 30px 22px 8px; }
#chat-messages { max-width: 760px; margin: 0 auto; }
.msg { margin-bottom: 8px; display: flex; }
.msg.user { justify-content: flex-end; margin: 22px 0; }
.msg .bubble { min-width: 0; overflow-wrap: break-word; }
.msg.user .bubble {
  max-width: 82%; padding: 10px 16px; border-radius: 14px;
  background: var(--user-bubble); font-size: 15.5px; line-height: 1.6;
}
.msg.model { display: block; }
.msg.model .bubble { max-width: 100%; padding: 0; background: none; border: none; }
.msg.model + .msg.model { margin-top: 26px; }
.msg .bubble > .md > :first-child { margin-top: 0; }
.msg .bubble > .md > :last-child { margin-bottom: 0; }

.md p { margin: 0 0 1em; }
.md h1, .md h2, .md h3, .md h4 {
  font-weight: 600; line-height: 1.35; margin: 1.5em 0 .55em;
}
.md h1 { font-size: 1.45em; }
.md h2 { font-size: 1.3em; border-bottom: 1px solid var(--border); padding-bottom: .25em; }
.md h3 { font-size: 1.15em; }
.md h4 { font-size: 1em; text-transform: uppercase; letter-spacing: .05em; font-family: var(--sans); font-size: .85em; color: var(--muted); }
.md ul, .md ol { margin: 0 0 1em; padding-left: 1.5em; }
.md li { margin: .4em 0; }
.md li::marker { color: var(--accent); font-weight: 600; }
.md strong { color: #171310; }
.md em { color: #4c463c; }
.md blockquote {
  margin: 1.1em 0; padding: .2em 0 .2em 1em;
  border-left: 3px solid var(--accent); color: #4c463c; font-style: italic;
}
.md hr { border: none; border-top: 1px solid var(--border); margin: 1.6em 0; }
.md a { color: var(--accent); }
.md table { border-collapse: collapse; margin: 14px 0; display: block; overflow-x: auto; font-size: 15px; }
.md th, .md td { border: 1px solid var(--border); padding: 7px 13px; }
.md th { background: #f3eee1; font-family: var(--sans); font-size: 13.5px; }
.md pre { background: #f2ecdd; padding: 12px 14px; border-radius: 8px; overflow-x: auto; font-size: 13.5px; }
.md code { font-family: ui-monospace, "Cascadia Mono", Menlo, monospace; font-size: .85em; background: #f2ecdd; padding: .1em .35em; border-radius: 4px; }
.md pre code { background: none; padding: 0; }
.md img, .md svg { max-width: 100%; height: auto; }
.katex { font-size: 1.04em; }

.figure {
  margin: 18px 0; background: #fffef9; border: 1px solid var(--border);
  border-radius: 8px; padding: 12px;
}
.figure svg { display: block; width: 100%; height: auto; }
.figure canvas { max-width: 100%; }
.fig-broken { border-color: var(--warn); background: #faf4e4; padding: 12px; font-size: 14px; font-family: var(--sans); }

.typing { color: var(--muted); animation: blink 1.2s infinite; font-family: var(--sans); font-size: 14px; }
@keyframes blink { 50% { opacity: .35; } }

#lesson-start { padding: 46px 0 64px; }
#chat-form {
  padding: 12px 18px 16px; border-top: 1px solid var(--border); background: var(--paper);
}
#chat-form textarea { margin-bottom: 8px; max-width: 760px; display: block; margin-left: auto; margin-right: auto; }
.chat-actions { display: flex; gap: 10px; justify-content: space-between; max-width: 760px; margin: 0 auto; }

.quiz-q { margin-bottom: 22px; }
.quiz-q .qtext { font-weight: 600; margin-bottom: 10px; line-height: 1.55; }
.quiz-q .opt {
  display: block; padding: 9px 13px; border: 1px solid var(--border); border-radius: 8px;
  margin-bottom: 6px; cursor: pointer; background: var(--paper);
  font: 15px/1.5 var(--sans);
}
.quiz-q .opt:hover { border-color: var(--accent); }
.quiz-q .opt:has(input:checked) { border-color: var(--accent); background: var(--user-bubble); }
.quiz-q .opt input { margin-right: 8px; }
.quiz-q textarea { font-family: var(--serif); font-size: 15.5px; }
.verdict { padding: 11px 14px; border-radius: 8px; margin-top: 10px; font-size: 14.5px; line-height: 1.6; }
.verdict.dogru { background: #e9f0dc; color: var(--ok); }
.verdict.kismi { background: #f6ecd2; color: var(--warn); }
.verdict.yanlis { background: #f7e4de; color: var(--danger); }
.score-hero { text-align: center; font-size: 52px; font-weight: 600; margin: 12px 0 0; }
.weak-chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0; }
.weak-chips span {
  background: #f7e4de; color: var(--danger); border-radius: 14px;
  padding: 4px 12px; font: 13px var(--sans);
}

#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #f6f1e5; padding: 10px 18px; border-radius: 8px;
  font: 14px var(--sans); z-index: 100; max-width: 90vw;
}

#btn-toggle-sidebar { display: none; }
@media (max-width: 900px) {
  body { font-size: 16.5px; }
  #btn-toggle-sidebar { display: block; }
  .course-layout { position: relative; }
  #sidebar {
    position: absolute; inset: 0 22% 0 0; z-index: 20; display: none;
    box-shadow: 4px 0 20px rgba(60, 48, 28, .15);
  }
  #sidebar.open { display: block; }
  #chat-scroll { padding: 22px 16px 8px; }
  .msg.user .bubble { max-width: 92%; }
}
