:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --ink: #1f2733;
  --muted: #6b7785;
  --line: #e3e8ef;
  --accent: #2f6df0;
  --accent-ink: #ffffff;
  --user-bg: #2f6df0;
  --user-ink: #ffffff;
  --bot-bg: #ffffff;
  --code-bg: #f0f2f6;
  --ok: #1f9d57;
  --danger: #c0392b;
  --radius: 14px;
  --maxw: 880px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--ink); font-size: 16px; line-height: 1.55; }
.hidden { display: none !important; }

/* --- Ворота входа --- */
.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px 24px;
  box-shadow: 0 10px 40px rgba(20, 40, 80, 0.08);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-card h1 { margin: 0; font-size: 26px; }
.login-sub { margin: 0 0 6px; color: var(--muted); font-size: 14px; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.login-card input {
  font-size: 16px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fbfcfe;
}
.login-card input:focus { outline: 2px solid var(--accent); border-color: var(--accent); }
.login-card button {
  margin-top: 4px;
  font-size: 16px;
  font-weight: 600;
  padding: 12px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-ink);
  cursor: pointer;
}
.login-card button:disabled { opacity: .6; cursor: default; }
.login-error { color: var(--danger); font-size: 14px; min-height: 18px; }

/* --- Каркас приложения --- */
.app { display: flex; flex-direction: column; height: 100%; }
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.topbar-title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#lesson-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-sub { font-size: 12px; color: var(--muted); }
.icon-btn {
  border: none; background: transparent; font-size: 20px; line-height: 1;
  width: 38px; height: 38px; border-radius: 9px; cursor: pointer; color: var(--ink);
}
.icon-btn:hover { background: var(--code-bg); }
.text-btn { border: none; background: transparent; color: var(--accent); cursor: pointer; font-size: 14px; padding: 6px 8px; border-radius: 8px; }
.text-btn:hover { background: var(--code-bg); }
.text-btn.small { font-size: 13px; }
.text-btn.danger { color: var(--danger); }

/* --- Боковая панель уроков --- */
.drawer {
  position: fixed; top: 0; left: 0; bottom: 0; width: 320px; max-width: 84vw;
  background: var(--panel); border-right: 1px solid var(--line);
  z-index: 20; padding: 14px; overflow-y: auto;
  box-shadow: 6px 0 30px rgba(20,40,80,.12);
}
.drawer-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.drawer-subhead { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 16px 4px 6px; }
.materials-dl {
  display: block; text-align: center; text-decoration: none;
  padding: 11px 12px; border-radius: 10px;
  background: var(--accent); color: #fff; font-weight: 600; font-size: 14px;
}
.materials-dl:hover { filter: brightness(1.05); }
.lesson-list { list-style: none; margin: 0; padding: 0; }
.lesson-list li {
  padding: 11px 12px; border-radius: 10px; cursor: pointer; display: flex; gap: 10px; align-items: baseline;
  border: 1px solid transparent;
}
.lesson-list li:hover { background: var(--code-bg); }
.lesson-list li.current { border-color: var(--accent); background: #eef3fe; }
.lesson-list li.bonus { opacity: .92; }
.lesson-num { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 22px; }
.lesson-name { flex: 1; }
.lesson-check { color: var(--ok); font-weight: 700; }
.lesson-tag { font-size: 11px; color: var(--muted); border: 1px solid var(--line); border-radius: 6px; padding: 1px 5px; }
.backdrop { position: fixed; inset: 0; background: rgba(15,25,45,.35); z-index: 15; }

/* --- Модальное окно конспекта --- */
.modal {
  position: fixed; inset: 0; z-index: 25; padding: 16px;
  display: flex; align-items: center; justify-content: center;
  background: rgba(15,25,45,.45);
}
.modal-card {
  background: var(--bot-bg); border-radius: 14px; width: 100%; max-width: 720px;
  max-height: 86vh; display: flex; flex-direction: column;
  box-shadow: 0 12px 40px rgba(15,25,45,.3); overflow: hidden;
}
.modal-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; border-bottom: 1px solid var(--line); gap: 8px;
}
.modal-actions { display: flex; gap: 4px; align-items: center; }
.modal-body {
  max-width: none; overflow-y: auto; padding: 16px 18px; margin: 0;
  border: none; border-radius: 0; background: transparent;
}

/* --- Чат --- */
.chat {
  flex: 1; overflow-y: auto; padding: 18px 14px 8px;
  display: flex; flex-direction: column; gap: 14px;
  width: 100%; max-width: var(--maxw); margin: 0 auto;
}
.msg { display: flex; }
.msg.user { justify-content: flex-end; }
.bubble {
  max-width: 86%; padding: 11px 14px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--bot-bg);
  overflow-wrap: anywhere;
}
.msg.user .bubble { background: var(--user-bg); color: var(--user-ink); border-color: var(--user-bg); }
.msg.user .bubble a { color: #d8e4ff; }
.bubble img.shot { max-width: 260px; border-radius: 10px; margin-top: 6px; display: block; border: 1px solid rgba(255,255,255,.4); }

/* Markdown внутри пузыря */
.bubble h1, .bubble h2, .bubble h3, .bubble h4 { margin: .5em 0 .35em; line-height: 1.3; }
.bubble h1 { font-size: 1.3em; } .bubble h2 { font-size: 1.18em; }
.bubble h3 { font-size: 1.07em; } .bubble h4 { font-size: 1em; }
.bubble p { margin: .5em 0; }
.bubble p:first-child { margin-top: 0; } .bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: .4em 0; padding-left: 1.4em; }
.bubble li { margin: .2em 0; }
.bubble code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; font-size: .9em; font-family: ui-monospace, "SFMono-Regular", Consolas, "Liberation Mono", monospace; }
.msg.user .bubble code { background: rgba(255,255,255,.2); }
.bubble pre { background: var(--code-bg); color: #1f2733; padding: 12px; border-radius: 10px; overflow-x: auto; margin: .5em 0; }
.bubble pre code { background: none; padding: 0; font-size: .88em; line-height: 1.5; }
.bubble blockquote { margin: .5em 0; padding: .3em .9em; border-left: 3px solid var(--accent); color: var(--muted); }
.bubble table { border-collapse: collapse; margin: .6em 0; font-size: .92em; display: block; overflow-x: auto; max-width: 100%; }
.bubble th, .bubble td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; vertical-align: top; }
.bubble th { background: var(--code-bg); font-weight: 600; white-space: nowrap; }
.msg.user .bubble th, .msg.user .bubble td { border-color: rgba(255,255,255,.4); }

/* Кнопки-варианты теста (A/Б/В/Г) */
.choice-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.choice-btn {
  min-width: 46px; padding: 9px 16px; border: 1px solid var(--accent);
  background: #eef3fe; color: var(--accent); border-radius: 10px; cursor: pointer;
  font-size: 16px; font-weight: 700;
}
.choice-btn:hover { background: var(--accent); color: #fff; }
.choice-btn:active { transform: translateY(1px); }

.typing { color: var(--muted); font-style: italic; }
.dots::after { content: "…"; animation: blink 1.2s steps(4) infinite; }
@keyframes blink { 0%,100%{opacity:.3} 50%{opacity:1} }

/* Пустой урок */
.empty {
  text-align: center; color: var(--muted); margin: auto; max-width: 420px;
  display: flex; flex-direction: column; gap: 14px; align-items: center; padding: 24px;
}
.empty h2 { color: var(--ink); margin: 0; }
.start-btn {
  font-size: 16px; font-weight: 600; padding: 12px 22px; border: none;
  border-radius: 12px; background: var(--accent); color: var(--accent-ink); cursor: pointer;
}

/* --- Композер --- */
.composer {
  border-top: 1px solid var(--line); background: var(--panel);
  padding: 8px 14px 10px; width: 100%; max-width: var(--maxw); margin: 0 auto;
}
.composer-row { display: flex; align-items: flex-end; gap: 8px; }
#input {
  flex: 1; resize: none; max-height: 180px;
  font-size: 16px; font-family: inherit; line-height: 1.45;
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: #fbfcfe;
}
#input:focus { outline: 2px solid var(--accent); border-color: var(--accent); }
.send-btn {
  border: none; background: var(--accent); color: #fff; font-size: 18px;
  width: 44px; height: 44px; border-radius: 12px; cursor: pointer; flex: none;
}
.send-btn:disabled { opacity: .5; cursor: default; }
.composer-actions { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 6px; justify-content: flex-end; }
.image-chip { position: relative; display: inline-block; margin-bottom: 8px; }
.image-chip img { max-height: 80px; border-radius: 8px; border: 1px solid var(--line); display: block; }
.chip-x {
  position: absolute; top: -8px; right: -8px; width: 22px; height: 22px; border-radius: 50%;
  border: none; background: var(--danger); color: #fff; cursor: pointer; font-size: 12px;
}

@media (max-width: 600px) {
  body { font-size: 15px; }
  .bubble { max-width: 92%; }
}
