:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #1b2330;
  --muted: #5f6b7a;
  --line: #dde2e9;
  --accent: #1f5f99;
  --accent-2: #2b77bd;
  --accent-text: #ffffff;
  --yellow: #ffd43b;
  --ok: #1d8a4e;
  --ok-bg: #e5f5ec;
  --fail: #c0392b;
  --fail-bg: #fdecea;
  --warn: #9a6700;
  --warn-bg: #fff6d6;
  --code-bg: #f3f5f8;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 30, 50, .06), 0 2px 8px rgba(20, 30, 50, .05);
  --mono: ui-monospace, "Cascadia Code", "JetBrains Mono", Consolas, "Liberation Mono", monospace;
  /* подсветка кода */
  --c-keyword: #8b2fc9;
  --c-def: #1f5f99;
  --c-string: #1d7a3f;
  --c-number: #b35c00;
  --c-comment: #8a94a3;
  --c-builtin: #0b7285;
  --c-operator: #5f6b7a;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161c;
    --surface: #1a2029;
    --surface-2: #232b36;
    --text: #e3e8ef;
    --muted: #97a3b3;
    --line: #2e3844;
    --accent: #4f9be0;
    --accent-2: #6aaeea;
    --accent-text: #0d1117;
    --ok: #4cc38a;
    --ok-bg: #173325;
    --fail: #f07166;
    --fail-bg: #3a1c1a;
    --warn: #e3b341;
    --warn-bg: #3a3017;
    --code-bg: #151a21;
    --shadow: none;
    --c-keyword: #d19bff;
    --c-def: #79b8ff;
    --c-string: #85e89d;
    --c-number: #ffab70;
    --c-comment: #6a7585;
    --c-builtin: #56d4dd;
    --c-operator: #97a3b3;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
/* без лигатур: != и <= должны выглядеть как набраны, а не как ≠ и ≤ */
code, pre, textarea, .CodeMirror, .symbar button, .tab, .chip { font-variant-ligatures: none; font-feature-settings: "liga" 0, "calt" 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  min-height: 100vh;
}
body.has-symbar { padding-bottom: 52px; }
a { color: var(--accent); }
h1 { font-size: 1.6rem; line-height: 1.25; margin: .2em 0 .6em; }
h2 { font-size: 1.25rem; margin: 1.6em 0 .6em; }
h3 { font-size: 1.05rem; margin: 1.2em 0 .5em; }
code { font-family: var(--mono); font-size: .92em; background: var(--code-bg); padding: .1em .35em; border-radius: 4px; }
pre { font-family: var(--mono); font-size: 14px; }
.muted { color: var(--muted); }
.small { font-size: .88rem; }

/* ---------- шапка */
.top {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 8px max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.logo { font-weight: 800; font-size: 1.25rem; color: var(--text); text-decoration: none; display: flex; align-items: center; gap: 4px; }
.logo-mark { background: var(--accent); color: var(--yellow); border-radius: 7px; padding: 0 6px; }
#nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; scrollbar-width: none; }
#nav a { padding: 6px 10px; border-radius: 8px; text-decoration: none; color: var(--muted); white-space: nowrap; font-weight: 500; }
#nav a:hover { background: var(--surface-2); color: var(--text); }
#nav a.active { background: var(--surface-2); color: var(--text); }
#account { display: flex; align-items: center; gap: 8px; }
#account .who { max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: .9rem; }

/* ---------- страницы */
main { padding: 0 16px 40px; }
.page { max-width: 1100px; margin: 0 auto; padding-top: 20px; }
.page.narrow { max-width: 420px; }
.page.loading { color: var(--muted); }
.crumbs { font-size: .9rem; color: var(--muted); margin-bottom: 6px; }
.crumbs a { color: var(--muted); }

/* ---------- кнопки */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 42px; padding: 0 16px; border-radius: 9px; border: 1px solid var(--accent);
  background: var(--accent); color: var(--accent-text); font: inherit; font-weight: 600;
  text-decoration: none; cursor: pointer; white-space: nowrap; touch-action: manipulation;
}
.btn:hover { background: var(--accent-2); border-color: var(--accent-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { background: var(--fail); border-color: var(--fail); color: #fff; }
.btn.small { min-height: 34px; padding: 0 12px; font-size: .9rem; }
.btn.tiny { min-height: 28px; padding: 0 10px; font-size: .8rem; }
.btn:focus-visible, a:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- главная */
.hero { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 24px; box-shadow: var(--shadow); }
.hero h1 { font-size: 2rem; margin-top: 0; }
.hero p { color: var(--muted); max-width: 640px; }
.hero-actions { display: flex; gap: 10px; flex-wrap: wrap; margin: 16px 0 8px; }
.overall { margin-top: 14px; max-width: 420px; }
.overall small { color: var(--muted); }
.bar { display: block; height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--ok); border-radius: 3px; }

.lesson-list { list-style: none; padding: 0; margin: 20px 0 0; display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.lesson-card {
  display: grid; grid-template-columns: 36px 1fr auto; align-items: center; gap: 4px 12px;
  padding: 14px 16px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  text-decoration: none; color: var(--text); box-shadow: var(--shadow); min-height: 72px;
}
.lesson-card:hover { border-color: var(--accent); }
.lesson-card .num { grid-row: span 2; width: 36px; height: 36px; border-radius: 50%; background: var(--surface-2); display: grid; place-items: center; font-weight: 700; color: var(--accent); }
.lesson-card.complete .num { background: var(--ok); color: #fff; }
.lesson-card .title { font-weight: 600; }
.lesson-card .count { color: var(--muted); font-size: .85rem; }
.lesson-card .bar { grid-column: 2 / 4; }

/* ---------- теория */
.theory { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 8px 24px 16px; box-shadow: var(--shadow); max-width: 820px; }
.theory pre.code { position: relative; background: var(--code-bg); border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px 40px; overflow-x: auto; }
.theory pre.code code { background: none; padding: 0; font-size: 14px; }
.theory pre.code .try { position: absolute; right: 8px; bottom: 8px; }
.theory .note { background: var(--warn-bg); border-left: 3px solid var(--warn); padding: 8px 12px; border-radius: 6px; }
.theory table.ops { border-collapse: collapse; margin: 8px 0; }
.theory table.ops td { border-bottom: 1px solid var(--line); padding: 6px 10px 6px 0; vertical-align: top; }

.task-list { list-style: none; padding: 0; margin: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); max-width: 820px; }
.task-list li + li { border-top: 1px solid var(--line); }
.task-list a { display: flex; align-items: center; gap: 12px; padding: 12px 16px; text-decoration: none; color: var(--text); min-height: 48px; }
.task-list a:hover { background: var(--surface-2); }
.task-list .n { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 34px; }
.st { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--line); display: grid; place-items: center; font-size: 13px; font-weight: 700; flex: none; }
.st.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.st.tried { border-color: var(--warn); color: var(--warn); }
.pager { display: flex; justify-content: space-between; gap: 10px; margin-top: 20px; flex-wrap: wrap; }

/* ---------- задача */
.task-grid { display: grid; grid-template-columns: minmax(0, 5fr) minmax(0, 6fr); gap: 20px; align-items: start; }
.statement { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 16px 20px; box-shadow: var(--shadow); }
.statement h1 { font-size: 1.35rem; margin-top: 0; }
.statement .text p:first-child { margin-top: 0; }
.statement pre { background: var(--code-bg); padding: 8px 10px; border-radius: 6px; overflow-x: auto; margin: 4px 0; }
.example { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.example pre { min-height: 2.4em; white-space: pre-wrap; word-break: break-word; }
.label { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
.badge { display: inline-block; vertical-align: middle; font-size: .78rem; font-weight: 600; padding: 2px 8px; border-radius: 20px; background: var(--surface-2); color: var(--muted); }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.workspace { position: sticky; top: 70px; display: flex; flex-direction: column; gap: 10px; min-width: 0; }

.editor { border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: var(--surface); }
.editor .CodeMirror { height: auto; min-height: 260px; font-family: var(--mono); font-size: 15px; line-height: 1.5; }
.editor .CodeMirror-scroll { min-height: 260px; max-height: 60vh; }
.editor.tall .CodeMirror, .editor.tall .CodeMirror-scroll { min-height: 340px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.actions .spacer { flex: 1; }
.stdin-box summary { cursor: pointer; color: var(--muted); font-size: .92rem; padding: 4px 0; }
.stdin { width: 100%; font-family: var(--mono); font-size: 15px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--text); resize: vertical; margin-top: 6px; }
.output {
  margin: 0; min-height: 90px; max-height: 50vh; overflow: auto; padding: 10px 12px; border-radius: var(--radius);
  background: #0f141a; color: #e6edf3; font-size: 14px; white-space: pre-wrap; word-break: break-word;
}
.output.empty { color: #8b98a8; font-family: system-ui, sans-serif; }
.output .err { color: #ff8f85; }
.output .note-line { font-family: system-ui, sans-serif; font-size: 13px; margin-top: 6px; }
.output .note-line.muted { color: #8b98a8; }
.output .note-line.warn { color: #f2cc60; }
.output .note-line.hint { color: #9ecbff; }
.verdict { padding: 12px 14px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface); }
.verdict.running { color: var(--muted); }
.verdict.ok { background: var(--ok-bg); border-color: var(--ok); color: var(--ok); }
.verdict.ok .btn { margin-left: 10px; }
.verdict.fail { background: var(--fail-bg); border-color: var(--fail); color: var(--text); }
.verdict.fail > strong { color: var(--fail); }
.diff { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-top: 10px; }
.diff pre { background: var(--surface); border: 1px solid var(--line); margin: 2px 0 0; padding: 6px 8px; border-radius: 6px; white-space: pre-wrap; word-break: break-word; min-height: 2em; font-size: 13px; }

/* ---------- подсветка CodeMirror (тема pygo) */
.cm-s-pygo.CodeMirror { background: var(--surface); color: var(--text); }
.cm-s-pygo .CodeMirror-gutters { background: var(--surface-2); border-right: 1px solid var(--line); }
.cm-s-pygo .CodeMirror-linenumber { color: var(--muted); }
.cm-s-pygo .CodeMirror-cursor { border-left: 2px solid var(--text); }
.cm-s-pygo .CodeMirror-selected, .cm-s-pygo.CodeMirror-focused .CodeMirror-selected { background: rgba(79, 155, 224, .25); }
.cm-s-pygo .CodeMirror-matchingbracket { color: inherit !important; background: rgba(255, 212, 59, .35); }
.cm-s-pygo .cm-keyword { color: var(--c-keyword); font-weight: 600; }
.cm-s-pygo .cm-def { color: var(--c-def); font-weight: 600; }
.cm-s-pygo .cm-string, .cm-s-pygo .cm-string-2 { color: var(--c-string); }
.cm-s-pygo .cm-number { color: var(--c-number); }
.cm-s-pygo .cm-comment { color: var(--c-comment); font-style: italic; }
.cm-s-pygo .cm-builtin { color: var(--c-builtin); }
.cm-s-pygo .cm-operator { color: var(--c-operator); }
.cm-s-pygo .cm-variable-2 { color: var(--text); }
.cm-s-pygo .cm-meta { color: var(--c-number); }

/* ---------- панель символов (телефон) */
.symbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50; display: none;
  gap: 4px; padding: 5px 6px calc(5px + env(safe-area-inset-bottom)); overflow-x: auto; scrollbar-width: none;
  background: var(--surface-2); border-top: 1px solid var(--line);
}
.symbar::-webkit-scrollbar { display: none; }
.symbar.show { display: flex; }
.symbar button {
  flex: none; min-width: 40px; height: 40px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); color: var(--text); font: 600 17px var(--mono); touch-action: manipulation;
  -webkit-user-select: none; user-select: none;
}
.symbar button:active { background: var(--accent); color: var(--accent-text); }

/* ---------- песочница */
.sandbox-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.sandbox-head h1 { margin: 0; }
.file-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.tabs { display: flex; gap: 4px; overflow-x: auto; margin: 12px 0 0; scrollbar-width: thin; }
.tab {
  flex: none; padding: 8px 14px; border: 1px solid var(--line); border-bottom: none; border-radius: 8px 8px 0 0;
  background: var(--surface-2); color: var(--muted); font: 14px var(--mono); cursor: pointer; min-height: 38px;
}
.tab.active { background: var(--surface); color: var(--text); font-weight: 600; }
.tab.add { font-family: system-ui; font-weight: 700; color: var(--accent); }
.sandbox .editor { border-top-left-radius: 0; }
.sandbox .actions, .sandbox .stdin-box, .sandbox .output { margin-top: 10px; }

/* ---------- вход */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 20px; box-shadow: var(--shadow); }
.login label { display: block; margin: 12px 0; font-weight: 600; font-size: .92rem; }
.login select { display: block; width: 100%; margin-top: 4px; min-height: 44px; padding: 8px 12px; font: inherit; font-weight: 400; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--text); }
.login .switch { text-align: center; margin: 14px 0 0; font-size: .95rem; }
.login input { display: block; width: 100%; margin-top: 4px; min-height: 44px; padding: 8px 12px; font: inherit; font-weight: 400; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--text); }
.login .btn { width: 100%; margin-top: 8px; }
.login-page { padding-top: 8vh; }
.login-logo { font-weight: 800; font-size: 1.6rem; display: flex; align-items: center; gap: 4px; margin-bottom: 8px; }
.form-error { color: var(--fail); min-height: 1.4em; font-size: .92rem; }

/* ---------- преподаватель */
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.search { min-height: 40px; padding: 6px 12px; font: inherit; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--text); flex: 1 1 200px; max-width: 360px; }
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
table.grid { width: 100%; border-collapse: collapse; }
table.grid th, table.grid td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); }
table.grid th { font-size: .82rem; color: var(--muted); font-weight: 600; background: var(--surface-2); }
table.grid tbody tr { cursor: pointer; }
table.grid tbody tr:hover { background: var(--surface-2); }
table.grid .num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { min-width: 46px; height: 34px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--muted); font: 600 13px var(--mono); cursor: pointer; }
.chip.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.chip.tried { background: var(--warn-bg); border-color: var(--warn); color: var(--warn); }
.chip.none { cursor: default; opacity: .6; }
.legend .chip { display: inline-grid; place-items: center; min-width: 26px; height: 22px; vertical-align: middle; cursor: default; }
.code-view { margin-top: 20px; }
.code-view .editor { margin-bottom: 12px; }

/* ---------- модальное окно */
.modal-overlay { position: fixed; inset: 0; z-index: 90; background: rgba(10, 14, 20, .55); display: grid; place-items: center; padding: 16px; }
.modal { width: 100%; max-width: 400px; background: var(--surface); color: var(--text); border: 1px solid var(--line); border-radius: 14px; padding: 18px 18px 14px; box-shadow: 0 10px 40px rgba(0, 0, 0, .3); }
.modal h3 { margin: 0 0 8px; }
.modal p { margin: 0 0 10px; }
.modal input { display: block; width: 100%; min-height: 44px; padding: 8px 12px; font: 16px var(--mono); border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--text); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }

/* ---------- уведомления и статус */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; transition: .25s;
  background: #1b2330; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 100; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--fail); }
.toast.ok { background: var(--ok); }
#pystatus { position: fixed; right: 12px; bottom: 12px; font-size: 12px; color: var(--muted); background: var(--surface); border: 1px solid var(--line); border-radius: 20px; padding: 3px 10px; opacity: 0; transition: opacity .3s; pointer-events: none; }
#pystatus[data-state="loading"], #pystatus[data-state="error"] { opacity: 1; }
#pystatus[data-state="error"] { color: var(--fail); }
body.has-symbar #pystatus { display: none; }

/* ---------- телефоны и планшеты */
@media (max-width: 900px) {
  .task-grid { grid-template-columns: 1fr; }
  .workspace { position: static; }
}
@media (max-width: 640px) {
  main { padding: 0 12px 32px; }
  .top { gap: 8px; padding-top: 6px; padding-bottom: 6px; flex-wrap: wrap; }
  #nav { order: 3; flex-basis: 100%; }
  #account { margin-left: auto; }
  #account .who { max-width: 120px; }
  h1 { font-size: 1.35rem; }
  .hero { padding: 18px; }
  .hero h1 { font-size: 1.6rem; }
  .theory { padding: 4px 14px 12px; border-radius: 10px; }
  .statement { padding: 14px; border-radius: 10px; }
  .example { grid-template-columns: 1fr; gap: 4px; }
  .diff { grid-template-columns: 1fr; }
  .editor .CodeMirror { font-size: 16px; }
  .editor .CodeMirror-scroll { max-height: none; }
  .actions .btn { flex: 1 1 auto; }
  .actions .spacer { display: none; }
  .hide-mobile { display: none; }
  .file-actions .btn { flex: 1 1 auto; }
  .lesson-list { grid-template-columns: 1fr; }
  /* 16px в полях ввода — чтобы iOS не увеличивал страницу при фокусе */
  .stdin, .login input, .login select, .search { font-size: 16px; }
}
