:root {
  --accent: #1f6feb;
  --text: #1b1f24;
  --muted: #5b6672;
  --line: #e2e6ea;
  --bg: #ffffff;
  --card: #f7f9fb;
  --code-bg: #1d2129;
  --code-fg: #e8eaed;
  --ok: #17794c;
  --bad: #b3261e;
  --wait: #9a6700;
  --note-bg: #eef4ff;
  --error-bg: #fdecea;
  --code-inline-bg: #eef1f4;
  --input-bg: #ffffff;
  --bar-bg: #e6ebf1;
  --ok-bg: #eef8f2;
  --bonus: #5b3fb5;
}

html[data-theme="dark"] {
  --accent: #4493f8;
  --text: #e6edf3;
  --muted: #9198a1;
  --line: #30363d;
  --bg: #0d1117;
  --card: #161b22;
  --code-bg: #010409;
  --code-fg: #e6edf3;
  --ok: #3fb950;
  --bad: #f85149;
  --wait: #d29922;
  --note-bg: #12233a;
  --error-bg: #3a1620;
  --code-inline-bg: #262c36;
  --input-bg: #0d1117;
  --bar-bg: #21262d;
  --ok-bg: #10241a;
  --bonus: #b48cff;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.75 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

.wrap { max-width: 820px; margin: 0 auto; padding: 0 20px; }

/* header */
.topbar { position: sticky; top: 0; z-index: 10; border-bottom: 1px solid var(--line); background: var(--bg); }
.topbar-inner { display: flex; flex-wrap: wrap; align-items: center; gap: 14px; padding-top: 14px; padding-bottom: 14px; }
.brand { font-weight: 700; color: var(--text); text-decoration: none; margin-right: auto; }
.nav { display: flex; flex-wrap: wrap; align-items: center; gap: 14px; font-size: 15px; }
.nav a { color: var(--muted); text-decoration: none; }
.nav a:hover, .nav a.active { color: var(--accent); }
.langs { display: flex; gap: 6px; border-left: 1px solid var(--line); padding-left: 14px; }
.linkbtn { background: none; border: 0; padding: 0; font: inherit; color: var(--muted); cursor: pointer; }
.linkbtn:hover { color: var(--accent); }

.theme-toggle { font-size: 16px; line-height: 1; }
.theme-toggle .to-light { display: none; }
html[data-theme="dark"] .theme-toggle .to-dark { display: none; }
html[data-theme="dark"] .theme-toggle .to-light { display: inline; }

main { padding-top: 28px; padding-bottom: 60px; }

@media (prefers-reduced-motion: no-preference) { html { scroll-behavior: smooth; } }

/* chapter table of contents */
.chapter-main { min-width: 0; }
.chapter-main [id] { scroll-margin-top: var(--scroll-offset, 84px); }
.toc-wrap { margin: 0 0 22px; }
.toc-toggle {
  display: inline-flex; align-items: center; gap: 8px; cursor: pointer;
  font: inherit; font-size: 15px; color: var(--text);
  background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 8px 14px;
}
.toc-toggle .toc-burger { color: var(--muted); }
.toc { display: none; margin-top: 12px; }
.toc-wrap.open .toc { display: block; }
.toc-title { display: none; }
.toc ul { list-style: none; margin: 0; padding: 0; border-left: 2px solid var(--line); }
.toc li { margin: 0; }
.toc a {
  display: block; color: var(--muted); text-decoration: none;
  padding: 6px 0 6px 14px; margin-left: -2px;
  border-left: 2px solid transparent; line-height: 1.4; font-size: 14.5px;
}
.toc a:hover { color: var(--text); }
.toc a.active { color: var(--accent); border-left-color: var(--accent); font-weight: 600; }
.toc a.lvl3 { padding-left: 28px; font-size: 14px; }
.toc-extra { margin-top: 8px; border-top: 1px solid var(--line); padding-top: 6px; }

@media (min-width: 992px) {
  body.chapter .wrap { max-width: 1080px; }
  .chapter-grid { display: grid; grid-template-columns: 232px minmax(0, 1fr); gap: 44px; align-items: start; }
  .toc-wrap {
    margin: 0; align-self: start; position: sticky; top: var(--scroll-offset, 84px);
    max-height: calc(100vh - var(--scroll-offset, 84px) - 24px); overflow-y: auto;
  }
  .toc-toggle { display: none; }
  .toc { display: block; }
  .toc-title {
    display: block; margin: 0 0 10px; font-size: 12px; font-weight: 700;
    text-transform: uppercase; letter-spacing: .05em; color: var(--muted);
  }
}

h1 { font-size: 30px; line-height: 1.25; margin: 0 0 12px; }
h2 { font-size: 22px; line-height: 1.3; margin: 34px 0 10px; }
h3 { font-size: 18px; margin: 20px 0 8px; }
p, ul, ol, table { margin: 0 0 16px; }
a { color: var(--accent); }
.lead { font-size: 18px; color: var(--muted); }
.muted { color: var(--muted); font-size: 15px; }
.big { font-size: 20px; }

/* cards, boxes */
.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 18px 20px; margin: 0 0 18px; }
.note { background: var(--note-bg); border-left: 4px solid var(--accent); padding: 12px 16px; border-radius: 6px; }
.error { background: var(--error-bg); border-left: 4px solid var(--bad); color: var(--bad); padding: 12px 16px; border-radius: 6px; }

/* lesson text */
.lesson img { max-width: 100%; }
.lesson blockquote { margin: 16px 0; padding: 10px 16px; border-left: 4px solid var(--accent); background: var(--note-bg); border-radius: 6px; }
.lesson blockquote p:last-child { margin-bottom: 0; }
.lesson table { border-collapse: collapse; width: 100%; font-size: 15px; }
.lesson th, .lesson td { border: 1px solid var(--line); padding: 8px 10px; text-align: left; vertical-align: top; }
.lesson th { background: var(--card); }
.lesson .step { display: flex; gap: 24px; align-items: flex-start; margin: 20px 0; }
.lesson .step.left { flex-direction: row-reverse; }
.lesson .step-text { flex: 1 1 auto; min-width: 0; }
.lesson .step-text > :first-child { margin-top: 0; }
.lesson .step-text > :last-child { margin-bottom: 0; }
.lesson figure.portrait { flex: 0 0 200px; margin: 0; }
.lesson figure.portrait img { display: block; width: 100%; max-width: 200px; height: auto; border-radius: 8px; border: 1px solid var(--line); }
.lesson figure.portrait figcaption { font-size: 14px; color: var(--muted); margin-top: 6px; }
.lesson ul.credits { font-size: 13px; color: var(--muted); }
.lesson ul.credits a { color: var(--muted); }
@media (max-width: 560px) {
  .lesson .step, .lesson .step.left { flex-direction: column; }
  .lesson figure.portrait { flex-basis: auto; align-self: center; width: 240px; max-width: 100%; }
  .lesson figure.portrait img { max-width: 240px; }
  .lesson figure.portrait figcaption { text-align: center; }
}

code, kbd, pre { font-family: "SF Mono", Menlo, Consolas, monospace; }
code { background: var(--code-inline-bg); padding: 1px 5px; border-radius: 4px; font-size: 0.9em; }
pre { background: var(--code-bg); color: var(--code-fg); padding: 14px 16px; border-radius: 8px; overflow-x: auto; line-height: 1.55; font-size: 14.5px; }
pre code { background: none; color: inherit; padding: 0; font-size: inherit; }
pre.answer { white-space: pre-wrap; word-break: break-word; }

/* forms */
.form label { display: block; font-weight: 600; margin: 14px 0 6px; font-size: 15px; }
.form label.check { font-weight: 400; display: flex; align-items: center; gap: 8px; }
input[type=text], input[type=password], input[type=number], select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 7px;
  font: inherit; background: var(--input-bg); color: var(--text);
}
textarea { resize: vertical; }
.btn {
  display: inline-block; background: var(--accent); color: #fff; border: 0; border-radius: 7px;
  padding: 9px 16px; font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; margin-top: 14px;
}
.btn:hover { filter: brightness(1.08); }
.btn.small { padding: 5px 10px; font-size: 14px; margin-top: 0; }
.btn.danger { background: var(--bad); }
.inline { display: inline-block; margin: 0 6px 0 0; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

/* dashboard */
.stats p { margin: 0 0 8px; }
.bar { height: 10px; background: var(--bar-bg); border-radius: 6px; overflow: hidden; margin: 10px 0; }
.bar span { display: block; height: 100%; background: var(--accent); }

.chapters { list-style: none; padding: 0; }
.chapter-row { display: flex; align-items: center; gap: 12px; padding: 11px 0; border-bottom: 1px solid var(--line); }
.daynum { color: var(--muted); font-size: 14px; min-width: 62px; }
.chapter-title { flex: 1; font-weight: 600; }
.chapter-row.locked .chapter-title { color: var(--muted); font-weight: 400; }

.badge { font-size: 13px; padding: 2px 9px; border-radius: 20px; border: 1px solid var(--line); color: var(--muted); }
.badge.open { color: var(--accent); border-color: var(--accent); }
.badge.done { color: var(--ok); border-color: var(--ok); }

/* tasks and quiz */
.task h3 { margin-top: 0; }
.pts { font-size: 13px; color: var(--muted); font-weight: 400; }
.state { font-weight: 600; font-size: 15px; margin: 10px 0 0; }
.state.ok { color: var(--ok); }
.state.bad { color: var(--bad); }
.state.wait { color: var(--wait); }
.question { border: 0; padding: 0; margin: 0 0 22px; }
.card.question { border: 1px solid var(--line); padding: 18px 20px; }
.question legend { font-weight: 600; padding: 0; margin-bottom: 8px; }
.choice { display: block; padding: 4px 0; }
.options { list-style: none; padding: 0; }
.options li { padding: 4px 10px; border-left: 3px solid transparent; }
.options li.right { border-left-color: var(--ok); background: var(--ok-bg); }
.options li.chosen-wrong { border-left-color: var(--bad); background: var(--error-bg); }

/* tables */
.table { border-collapse: collapse; width: 100%; font-size: 15px; }
.table th, .table td { border-bottom: 1px solid var(--line); padding: 9px 10px; text-align: left; }
.table th { color: var(--muted); font-weight: 600; }
.table td.num, .table th.num { text-align: right; }
.table tr.me { background: var(--note-bg); }

.linklist { list-style: none; padding: 0; }
.linklist li { padding: 8px 0; border-bottom: 1px solid var(--line); }
.tasklist { list-style: none; padding: 0; font-size: 15px; }
.tasklist li { padding: 6px 0; border-bottom: 1px solid var(--line); }
.pending { border-top: 1px solid var(--line); padding: 14px 0 0; margin-top: 14px; }
details summary { cursor: pointer; color: var(--accent); font-size: 15px; }

.foot { color: var(--muted); font-size: 14px; border-top: 1px solid var(--line); padding-top: 16px; padding-bottom: 30px; }

@media print {
  .topbar, .foot, .nav, form, .btn, .actions, .linkbtn, details summary, .toc-wrap, .cheat-toggle, .cheat-panel, .no-print { display: none !important; }
  html[data-cheat="open"] body.has-cheat { padding: 0; }
  .lesson .part { background: #f2f2f2; color: #000; }
  .cheat-page { page-break-inside: avoid; }
  .chapter-grid { display: block; }
  body { font-size: 12pt; color: #000; background: #fff; }
  .wrap { max-width: 100%; padding: 0; }
  main { padding: 0; }
  .card { border: 1px solid #ccc; background: none; page-break-inside: avoid; }
  pre { background: #f2f2f2; color: #000; border: 1px solid #ccc; }
  code { background: #f2f2f2; color: #000; }
  .note, .error, .lesson blockquote, .options li.right, .options li.chosen-wrong, .table tr.me {
    background: #f2f2f2 !important; color: #000 !important;
  }
  a { color: #000; text-decoration: none; }
  details { display: block; }
  details .lesson { display: block; }
}

/* --- More than two languages: let the switcher wrap --- */
.langs { flex-wrap: wrap; }

/* --- Fonts for the added scripts --- */
:lang(ar), :lang(fa) { font-family: "Noto Naskh Arabic", "Segoe UI", Tahoma, system-ui, sans-serif; }
:lang(ar) code, :lang(ar) kbd, :lang(ar) pre, :lang(fa) code, :lang(fa) kbd, :lang(fa) pre { font-family: "SF Mono", Menlo, Consolas, monospace; }
:lang(hy) { font-family: "Noto Sans Armenian", "Segoe UI", system-ui, sans-serif; }

/* --- Right to left (Arabic, Persian) --- */
[dir="rtl"] .brand { margin-right: 0; margin-left: auto; }
[dir="rtl"] .langs { border-left: 0; border-right: 1px solid var(--line); padding-left: 0; padding-right: 14px; }
[dir="rtl"] .toc ul { border-left: 0; border-right: 2px solid var(--line); }
[dir="rtl"] .toc a { padding: 6px 14px 6px 0; margin-left: 0; margin-right: -2px; border-left: 0; border-right: 2px solid transparent; }
[dir="rtl"] .toc a.lvl3 { padding-left: 0; padding-right: 28px; }
[dir="rtl"] .note, [dir="rtl"] .lesson blockquote { border-left: 0; border-right: 4px solid var(--accent); }
[dir="rtl"] .error { border-left: 0; border-right: 4px solid var(--bad); }
[dir="rtl"] .lesson th, [dir="rtl"] .lesson td,
[dir="rtl"] .table th, [dir="rtl"] .table td { text-align: right; }
[dir="rtl"] .table td.num, [dir="rtl"] .table th.num { text-align: left; }
[dir="rtl"] .options li { border-left: 0; border-right: 3px solid transparent; }
/* Commands and code stay left to right, even inside Arabic or Persian text. */
[dir="rtl"] pre, [dir="rtl"] code, [dir="rtl"] kbd, [dir="rtl"] .terminal { direction: ltr; unicode-bidi: isolate; }
[dir="rtl"] pre, [dir="rtl"] .terminal { text-align: left; }

/* medals */
.medal { vertical-align: -5px; margin-right: 2px; }
.medal-count { display: inline-flex; align-items: center; gap: 6px; }
a.btn { display: inline-block; text-decoration: none; }

/* End-of-day feedback */
.daily-feedback { margin-top: 40px; }
.feedback-options { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 8px; }
.form .feedback-choice { margin: 0; padding: 10px 8px; border: 1px solid var(--line); border-radius: 7px; background: var(--input-bg); font-weight: 400; font-size: 14px; line-height: 1.5; cursor: pointer; display: flex; flex-direction: column; gap: 7px; align-items: center; text-align: center; }
.feedback-choice input { margin: 0; accent-color: var(--accent); }
.feedback-choice:has(input:checked) { border-color: var(--accent); background: var(--note-bg); }
.feedback-choice:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.feedback-question legend { margin-bottom: 12px; }
.feedback-days { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 24px; }
.feedback-days a { padding: 6px 10px; border: 1px solid var(--line); border-radius: 7px; text-decoration: none; }
.feedback-days a[aria-current] { background: var(--note-bg); border-color: var(--accent); }
.feedback-summary { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.feedback-summary .card { margin: 0; min-width: 0; }
.feedback-summary h3, .feedback-comment h3 { margin-top: 0; font-size: 16px; }
.feedback-bars { list-style: none; padding: 0; margin: 0; }
.feedback-bars li { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr) 2ch; align-items: center; gap: 8px; font-size: 14px; margin: 10px 0; }
.feedback-bars progress { width: 100%; height: 12px; accent-color: var(--accent); }
.feedback-comment p { white-space: pre-wrap; overflow-wrap: anywhere; }
.daily-feedback button:disabled { opacity: .6; cursor: wait; }
@media (max-width: 600px) {
  .feedback-options { grid-template-columns: 1fr; }
  .form .feedback-choice { flex-direction: row; text-align: start; gap: 12px; padding: 10px 12px; }
  .feedback-summary { grid-template-columns: 1fr; }
}
@media print { .daily-feedback, .feedback-days { display: none; } }

/* --- Parts of a day: banner in the text --- */
.lesson .part {
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 14px;
  margin: 40px 0 10px; padding: 10px 16px; border-radius: 8px;
  background: var(--accent); color: #fff; font-weight: 700; font-size: 19px; line-height: 1.4;
}
.lesson .part span { font-weight: 400; font-size: 15px; opacity: .92; }

/* --- Bonus tasks and bonus medal --- */
.badge.bonus { color: var(--bonus); border-color: var(--bonus); vertical-align: middle; font-weight: 600; }
.task.bonus { border-color: var(--bonus); }

/* --- Cheat sheet: floating button and panel --- */
.cheat-toggle {
  position: fixed; right: 20px; bottom: 20px; z-index: 30; cursor: pointer;
  background: var(--accent); color: #fff; border: 0; border-radius: 24px;
  padding: 11px 20px; font: inherit; font-weight: 600; box-shadow: 0 4px 14px rgba(0, 0, 0, .28);
}
.cheat-toggle:hover { filter: brightness(1.08); }
.cheat-panel {
  display: none; position: fixed; z-index: 25; left: 0; right: 0; bottom: 0; max-height: 60vh;
  flex-direction: column; background: var(--bg); border-top: 1px solid var(--line);
  box-shadow: 0 -4px 20px rgba(0, 0, 0, .2);
}
html[data-cheat="open"] .cheat-panel { display: flex; }
html[data-cheat="open"] .cheat-toggle { display: none; }
.cheat-head { display: flex; align-items: center; gap: 16px; padding: 10px 16px; border-bottom: 1px solid var(--line); font-size: 15px; }
.cheat-head strong { margin-right: auto; font-size: 16px; }
[dir="rtl"] .cheat-head strong { margin-right: 0; margin-left: auto; }
.cheat-close { font-size: 18px; line-height: 1; }
.cheat-body { overflow-y: auto; padding: 6px 16px 16px; }
.cheat summary { font-weight: 600; color: var(--text); padding: 8px 0; font-size: 15px; }
.cheat .lesson { font-size: 14.5px; line-height: 1.55; }
.cheat .lesson table { font-size: 14px; margin-bottom: 8px; }
.cheat .lesson th, .cheat .lesson td { padding: 5px 8px; }
.cheat .lesson p { margin-bottom: 8px; }
@media (min-width: 992px) {
  .cheat-panel {
    top: 0; bottom: 0; left: auto; right: 0; width: 440px; max-height: none;
    border-top: 0; border-left: 1px solid var(--line); box-shadow: -4px 0 20px rgba(0, 0, 0, .15);
  }
  /* The text moves left so the open panel covers nothing. Same side in every language. */
  html[data-cheat="open"] body.has-cheat { padding-right: 440px; }
}

/* --- Locked parts of a day --- */
.note.part-locked { margin: 28px 0; font-weight: 600; }
.note.part-admin { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; margin: 32px 0 0; border-left-color: var(--wait); }
.note.part-admin form { margin: 0; }
[dir="rtl"] .note.part-admin { border-right-color: var(--wait); }

/* --- Quiz answers waiting for the teacher --- */
.card.question.wait { border-color: var(--wait); }
.state .wait, span.wait { color: var(--wait); }
.answer-question { font-size: 18px; margin-top: 4px; }
.answer-row p { margin-bottom: 8px; }
.answer-text { font-size: 15px; }

/* The admin looks at a page like a student. */
.student-view-bar { background: var(--note-bg); border-bottom: 3px solid var(--wait); padding: 10px 0; }
.student-view-bar .wrap { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; }

/* --- Flashcards --- */
.flashcard { max-width: 680px; margin: 0 auto; }
.card-progress { display: flex; justify-content: space-between; gap: 12px; color: var(--muted); font-size: 15px; margin-top: 8px; }
.card-question { font-size: 19px; font-weight: 600; }
.card-question p { margin: 0 0 12px; }
.card-question pre { font-size: 15px; font-weight: 400; }
.answers { display: grid; gap: 10px; list-style: none; padding: 0; margin: 14px 0 0; }
.answers button, .answers li {
  display: block; width: 100%; text-align: start; padding: 12px 14px; font: inherit; color: var(--text);
  background: var(--bg); border: 2px solid var(--line); border-radius: 8px;
}
.answers button { cursor: pointer; }
.answers button:hover, .answers button:focus-visible { border-color: var(--accent); }
.answers li.right { border-color: var(--ok); background: var(--ok-bg); }
.answers li.chosen-wrong { border-color: var(--bad); background: var(--error-bg); }
.sizes { display: flex; flex-wrap: wrap; gap: 10px; }
.sizes .btn { margin-top: 0; }
.sizes .btn[disabled] { opacity: .45; cursor: not-allowed; filter: none; }
.records { list-style: none; padding: 0; margin: 0; }
.records li { display: flex; justify-content: space-between; gap: 12px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.flashcard .note > :last-child { margin-bottom: 0; }

/* Admin task list: one closed box per student */
.student-group { scroll-margin-top: 160px; }
.student-group > summary { cursor: pointer; font-size: 18px; padding: 2px 0; }
.student-group[open] > summary { margin-bottom: 12px; }
.student-group > summary .badge { margin-left: 6px; }
[dir="rtl"] .student-group > summary .badge { margin-left: 0; margin-right: 6px; }

/* --- Admin: student page with day boxes, wide student list, quiz results --- */
body.wide .wrap { max-width: 1080px; }
.student-nav { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px 16px; margin: 0 0 8px; font-size: 15px; }
.student-nav-steps { display: flex; flex-wrap: wrap; gap: 8px 20px; }
span.bad { color: var(--bad); font-weight: 600; }
.table.overview td.num { white-space: nowrap; }
.table tfoot th { border-bottom: none; color: var(--text); }
.day-group { scroll-margin-top: 160px; }
.day-group > summary { cursor: pointer; font-size: 18px; padding: 2px 0; }
.day-group > summary .badge { margin-left: 6px; }
[dir="rtl"] .day-group > summary .badge { margin-left: 0; margin-right: 6px; }
.quiz-stats h3 { margin-top: 0; }
.quiz-stats .feedback-bars li { grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr) 3ch; }
table.sortable th { cursor: pointer; user-select: none; }
table.sortable th:hover { color: var(--text); }
table.sortable th[aria-sort="ascending"]::after { content: " \25B2"; font-size: 11px; }
table.sortable th[aria-sort="descending"]::after { content: " \25BC"; font-size: 11px; }
