:root {
  --bg: #f4f6ff;
  --card: #ffffff;
  --ink: #1f2440;
  --muted: #6b7192;
  --brand: #5b7cfa;
  --brand-2: #8a63f4;
  --accent: #ffb020;
  --user: #5b7cfa;
  --bot: #eef1fb;
  --ok: #23b26d;
  --danger: #e5484d;
  --radius: 16px;
  --shadow: 0 6px 24px rgba(40,50,110,.10);
  font-size: 16px;
}
* { box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}
.hidden { display: none !important; }
button { font-family: inherit; cursor: pointer; }
h1, h2 { margin: .2em 0; }
.muted { color: var(--muted); font-size: .9rem; }

/* ---------- Экран входа ---------- */
.login-screen {
  min-height: 100dvh; display: grid; place-items: center;
  background: linear-gradient(160deg, var(--brand), var(--brand-2));
  padding: 20px;
}
.login-card {
  background: var(--card); border-radius: 24px; padding: 32px 26px;
  width: 100%; max-width: 340px; text-align: center; box-shadow: var(--shadow);
}
.login-logo { font-size: 52px; }
.login-sub { color: var(--muted); margin-top: 0; }
.pin-dots { display: flex; gap: 12px; justify-content: center; height: 22px; margin: 18px 0 6px; }
.pin-dots .dot { width: 16px; height: 16px; border-radius: 50%; background: #d7ddf5; transition: .15s; }
.pin-dots .dot.on { background: var(--brand); transform: scale(1.1); }
.pin-error { color: var(--danger); font-size: .9rem; min-height: 20px; }
.pin-pad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 10px; }
.pin-pad button {
  aspect-ratio: 1/1; border: none; border-radius: 50%; background: #f0f3ff; color: var(--ink);
  font-size: 1.5rem; font-weight: 600; transition: .12s;
}
.pin-pad button:active { transform: scale(.92); background: #e2e8ff; }

/* ---------- Верхняя панель ---------- */
.app { position: fixed; inset: 0; display: flex; flex-direction: column; overflow: hidden; }
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px;
  background: var(--card); box-shadow: 0 2px 10px rgba(40,50,110,.06); z-index: 5;
}
.brand { font-weight: 800; font-size: 1.1rem; display: flex; align-items: center; gap: 6px; }
.brand-logo { font-size: 1.3rem; }
.topbar .stats { margin-left: auto; display: flex; gap: 12px; }
.stat { background: #f0f3ff; padding: 5px 10px; border-radius: 999px; font-size: .9rem; }
.icon-btn {
  border: none; background: #f0f3ff; width: 40px; height: 40px; border-radius: 12px;
  font-size: 1.15rem; display: grid; place-items: center; color: var(--ink);
}
.icon-btn.ghost { background: transparent; }
.icon-btn:active { transform: scale(.94); }

/* ---------- Раскладка ---------- */
.layout { display: flex; flex: 1; min-height: 0; }
.subjects {
  width: 220px; background: var(--card); padding: 12px; overflow-y: auto;
  border-right: 1px solid #eef0f8; display: flex; flex-direction: column; gap: 6px;
}
.subject-item {
  display: flex; align-items: center; gap: 10px; padding: 11px 12px; border-radius: 12px;
  border: none; background: transparent; text-align: left; font-size: 1rem; color: var(--ink); width: 100%;
}
.subject-item .emoji { font-size: 1.3rem; }
.subject-item:hover { background: #f4f6ff; }
.subject-item.active { background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; box-shadow: var(--shadow); }

/* ---------- Чат ---------- */
.chat-area { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-header { padding: 12px 18px; font-weight: 700; border-bottom: 1px solid #eef0f8; background: var(--card); display: flex; align-items: center; gap: 8px; }
.messages { flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; padding: 18px; display: flex; flex-direction: column; gap: 14px; }
.msg .katex-display { overflow-x: auto; overflow-y: hidden; margin: .6em 0; max-width: 100%; }
.msg .katex { max-width: 100%; }
.msg { max-width: min(680px, 88%); padding: 12px 15px; border-radius: 16px; line-height: 1.5; white-space: normal; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--user); color: #fff; border-bottom-right-radius: 5px; }
.msg.bot { align-self: flex-start; background: var(--bot); color: var(--ink); border-bottom-left-radius: 5px; }
.msg.bot .speak {
  margin-top: 8px; font-size: .8rem; color: var(--brand); background: #fff; border: 1px solid #dfe4fb;
  border-radius: 999px; padding: 3px 10px; display: inline-flex; gap: 5px; align-items: center;
}
.msg p:first-child { margin-top: 0; } .msg p:last-child { margin-bottom: 0; }
.msg pre { background: #1f2440; color: #eef; padding: 10px; border-radius: 10px; overflow-x: auto; }
.msg code { background: rgba(0,0,0,.06); padding: 1px 5px; border-radius: 5px; }
.msg.user code { background: rgba(255,255,255,.2); }
.empty-hint { color: var(--muted); text-align: center; margin: auto; max-width: 420px; }
.empty-hint .big { font-size: 46px; }
.typing { display: inline-flex; gap: 4px; }
.typing span { width: 7px; height: 7px; background: var(--muted); border-radius: 50%; animation: blink 1.2s infinite both; }
.typing span:nth-child(2){animation-delay:.2s} .typing span:nth-child(3){animation-delay:.4s}
@keyframes blink { 0%,80%,100%{opacity:.3} 40%{opacity:1} }

/* ---------- Композитор ---------- */
.composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 14px; border-top: 1px solid #eef0f8; background: var(--card); }
.composer textarea {
  flex: 1; resize: none; border: 1.5px solid #e2e7fb; border-radius: 14px; padding: 11px 14px;
  font-size: 1rem; max-height: 140px; outline: none; color: var(--ink);
}
.composer textarea:focus { border-color: var(--brand); }
.send-btn { border: none; background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; width: 46px; height: 46px; border-radius: 14px; font-size: 1.2rem; }
.send-btn:disabled { opacity: .5; }
#mic-btn.recording { background: #ffe0e0; color: var(--danger); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .5; } }

/* ---------- Модалки ---------- */
.modal { position: fixed; inset: 0; background: rgba(25,30,60,.45); display: grid; place-items: center; padding: 16px; z-index: 20; }
.modal-card { background: var(--card); border-radius: 20px; padding: 24px; width: 100%; max-width: 420px; max-height: 90dvh; overflow-y: auto; position: relative; box-shadow: var(--shadow); }
.modal-card.wide { max-width: 640px; }
.modal-close { position: absolute; top: 14px; right: 14px; border: none; background: #f0f3ff; width: 32px; height: 32px; border-radius: 10px; font-size: 1rem; }
.modal-card label { display: block; margin: 12px 0 4px; font-size: .9rem; font-weight: 600; }
.modal-card input, .modal-card textarea, .modal-card select {
  width: 100%; border: 1.5px solid #e2e7fb; border-radius: 12px; padding: 10px 12px; font-size: 1rem; margin-top: 4px; outline: none; color: var(--ink); font-weight: 400;
}
.modal-card label.checkbox { display: flex; align-items: center; gap: 8px; font-weight: 400; }
.modal-card label.checkbox input { width: auto; margin: 0; }
.btn-primary { background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; border: none; border-radius: 12px; padding: 12px 18px; font-size: 1rem; font-weight: 600; margin-top: 14px; width: 100%; }
.btn-secondary { background: #eef1fb; color: var(--ink); border: none; border-radius: 12px; padding: 10px 16px; font-weight: 600; margin-top: 12px; }
.btn-danger { background: #fde8e8; color: var(--danger); border: none; border-radius: 12px; padding: 10px 16px; font-weight: 600; }
.btn-text { background: none; border: none; color: var(--muted); text-decoration: underline; margin-top: 12px; display: block; }
.saved-note { color: var(--ok); font-size: .85rem; margin-left: 8px; }

.tabs { display: flex; gap: 6px; margin-bottom: 14px; flex-wrap: wrap; }
.tab { border: none; background: #f0f3ff; padding: 9px 14px; border-radius: 10px; font-weight: 600; color: var(--muted); }
.tab.active { background: var(--brand); color: #fff; }
.tab-content { display: none; } .tab-content.active { display: block; }
.report-out { margin-top: 16px; background: #f8f9ff; border-radius: 14px; padding: 16px; line-height: 1.55; max-height: 55dvh; overflow-y: auto; }
.report-out:empty { display: none; }
.editor-actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-top: 6px; }

.subj-row { display: flex; align-items: center; gap: 10px; padding: 10px; border: 1px solid #eef0f8; border-radius: 12px; margin-bottom: 8px; }
.subj-row .emoji { font-size: 1.3rem; } .subj-row .grow { flex: 1; }
.subj-row .badge { font-size: .72rem; background: #f0f3ff; color: var(--muted); padding: 2px 8px; border-radius: 999px; }
.subj-row button { border: none; background: #f0f3ff; border-radius: 8px; padding: 6px 10px; }

/* ---------- Мобильная ---------- */
#menu-btn { display: none; }
@media (max-width: 720px) {
  #menu-btn { display: grid; }
  .subjects {
    position: fixed; top: 60px; bottom: 0; left: 0; z-index: 8; width: 78%; max-width: 300px;
    transform: translateX(-105%); transition: transform .22s; box-shadow: var(--shadow);
  }
  .subjects.open { transform: none; }
  .msg { max-width: 92%; }
  .topbar .stats { margin-left: 0; }
  .brand { font-size: 1rem; }
}

/* ---------- Геймификация: шапка ---------- */
.stats { border: none; background: transparent; display: flex; align-items: center; gap: 8px; margin-left: auto; cursor: pointer; padding: 4px; border-radius: 12px; max-width: 60%; overflow-x: auto; }
.stats:active { transform: scale(.97); }
.header-badges { display: inline-flex; gap: 2px; font-size: 1rem; }
.header-badges:empty { display: none; }

/* ---------- Экран достижений ---------- */
.ach-card { max-width: 460px; text-align: center; }
.ach-photo-wrap { display: flex; justify-content: center; margin: 6px 0 14px; }
.ach-photo {
  width: 150px; height: 150px; border-radius: 20px; object-fit: cover;
  box-shadow: var(--shadow); border: 3px solid #fff;
  transform-origin: 50% 90%;
  animation: sway 1.8s ease-in-out infinite;
}
@keyframes sway {
  0%   { transform: rotate(-5deg) translateY(0); }
  25%  { transform: rotate(3deg)  translateY(-4px); }
  50%  { transform: rotate(-3deg) translateY(0); }
  75%  { transform: rotate(5deg)  translateY(-4px); }
  100% { transform: rotate(-5deg) translateY(0); }
}
@media (prefers-reduced-motion: reduce) { .ach-photo { animation: none; } }

.ach-counters { display: flex; gap: 10px; justify-content: center; margin-bottom: 14px; }
.ach-counter { flex: 1; background: #f4f6ff; border-radius: 14px; padding: 12px 6px; }
.ach-counter .c-val { font-size: 1.6rem; font-weight: 800; color: var(--brand); }
.ach-counter .c-lbl { font-size: .72rem; color: var(--muted); margin-top: 2px; }

.ach-balance { background: linear-gradient(135deg, #fff6e0, #ffe9c7); border-radius: 16px; padding: 14px; margin-bottom: 16px; }
.ach-balance .bal-val { font-size: 1.9rem; font-weight: 800; color: #c9821a; }
.ach-balance .bal-note { font-size: .82rem; color: #8a6a2f; margin-top: 4px; line-height: 1.4; }
.ach-balance .bal-sub { font-size: .74rem; opacity: .8; }

.ach-h { margin: 4px 0 10px; }
.ach-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.ach-badge { background: #f4f6ff; border-radius: 14px; padding: 12px 6px; opacity: .38; filter: grayscale(1); transition: .2s; }
.ach-badge.earned { opacity: 1; filter: none; background: linear-gradient(135deg, #eef1fb, #e6ecff); }
.ach-badge .b-ico { font-size: 1.8rem; }
.ach-badge .b-title { font-size: .72rem; color: var(--ink); margin-top: 4px; line-height: 1.2; }

/* ---------- Всплывающие награды ---------- */
.toast-layer { position: fixed; left: 0; right: 0; top: 64px; display: flex; flex-direction: column; align-items: center; gap: 8px; z-index: 40; pointer-events: none; }
.toast {
  background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff;
  padding: 10px 18px; border-radius: 999px; font-weight: 700; box-shadow: var(--shadow);
  animation: toast-pop 2.4s ease forwards; font-size: 1rem;
}
.toast.gold { background: linear-gradient(135deg, #f7b733, #fc8f28); }
@keyframes toast-pop {
  0% { opacity: 0; transform: translateY(-12px) scale(.9); }
  12% { opacity: 1; transform: translateY(0) scale(1); }
  80% { opacity: 1; transform: translateY(0) scale(1); }
  100% { opacity: 0; transform: translateY(-8px) scale(.98); }
}
