:root {
  color-scheme: light;
  --text: #1c2430;
  --muted: #5c6b7a;
  --line: #e4e8ee;
  --blue: #1f6feb;
  --button: #1f6feb;
  --red: #b42318;
  --danger: #b42318;
  --bg: #f4f6f8;
  --card: #fff;
  --nav: #fff;
  --input: #fff;
  --input-line: #c5ced8;
  --soft: #e8eef6;
  --good: #17803d;
  --absent: #3d5a80;
  --message: #e8f6ee;
  --message-error: #fdecea;
  --message-info: #eef3f8;
  --card-shadow: 0 1px 2px rgba(28, 36, 48, 0.05);
  --popover-shadow: 0 8px 28px rgba(28, 36, 48, 0.18);
  --focus: #b9d2ff;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --text: #e7edf5;
  --muted: #a3b0c0;
  --line: #2c3644;
  --blue: #8eb7ff;
  --button: #1f6feb;
  --red: #ff8d86;
  --danger: #d92d20;
  --bg: #10151c;
  --card: #1b2330;
  --nav: #161d27;
  --input: #121820;
  --input-line: #3c4858;
  --soft: #2a3546;
  --good: #4ade80;
  --absent: #b7c9e4;
  --message: #163226;
  --message-error: #3a1c1c;
  --message-info: #1c2838;
  --card-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
  --popover-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
  --focus: #3d6cb5;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
  font-size: 1.05rem;
  line-height: 1.45;
}

header,
main,
footer {
  width: min(40rem, calc(100% - 2rem));
  margin-inline: auto;
}

body.wide header,
body.wide main,
body.wide footer {
  width: min(96rem, calc(100% - 1rem));
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 0;
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
}

body.student .brand-row {
  width: 100%;
  justify-content: space-between;
}

header a.brand {
  font-weight: 700;
  color: inherit;
  text-decoration: none;
  font-size: 1.25rem;
}

.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.75rem;
  height: 2.75rem;
  padding: 0;
  border-radius: 999px;
  background: var(--card);
  color: var(--text);
  box-shadow: var(--card-shadow);
  flex: 0 0 auto;
}

.theme-toggle svg {
  width: 1.35rem;
  height: 1.35rem;
}

.theme-toggle .icon-sun { display: none; }

html[data-theme="dark"] .theme-toggle .icon-moon { display: none; }
html[data-theme="dark"] .theme-toggle .icon-sun { display: block; }

.header-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.9rem;
}

main {
  background: var(--card);
  border-radius: 16px;
  padding: 1.25rem;
}

footer {
  color: var(--muted);
  font-size: 0.85rem;
  padding: 1.25rem 0 2rem;
}

h1 { font-size: 1.5rem; margin: 0 0 0.5rem; }
h2 { font-size: 1.15rem; margin: 1.6rem 0 0.4rem; }

label {
  display: block;
  font-weight: 600;
  margin: 0.9rem 0 0.35rem;
}

input,
textarea,
select {
  width: 100%;
  font: inherit;
  padding: 0.7rem 0.8rem;
  border: 1px solid var(--input-line);
  border-radius: 10px;
  background: var(--input);
  color: var(--text);
}

button,
.button {
  display: inline-block;
  font: inherit;
  background: var(--button);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 0.75rem 1rem;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
}

.stack { display: flex; flex-direction: column; gap: 0.65rem; }
.stack button,
.stack .button { width: 100%; }

button.secondary,
a.secondary { background: var(--soft); color: var(--text); }

button.danger { background: var(--danger); color: #fff; }

button.link {
  background: none;
  color: var(--blue);
  padding: 0;
  width: auto;
}

a { color: var(--blue); }

.errors { color: var(--red); margin-top: 0.3rem; }
.errorlist { color: var(--red); margin: 0.3rem 0 0; padding-left: 1.1rem; }

ul.messages { list-style: none; padding: 0; margin: 0 0 1rem; }
ul.messages li {
  background: var(--message);
  border-radius: 10px;
  padding: 0.6rem 0.8rem;
  margin-bottom: 0.5rem;
}
ul.messages li.error { background: var(--message-error); }
ul.messages li.info { background: var(--message-info); }

.card-link,
.student {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  align-items: center;
  padding: 0.85rem 0;
  border-bottom: 1px solid var(--line);
}

.card-link {
  color: inherit;
  text-decoration: none;
  font-weight: 600;
}

.actions { display: flex; gap: 0.8rem; flex-shrink: 0; }
.muted { color: var(--muted); }
.danger-link { margin-top: 2rem; }
.danger-link a { color: var(--red); }

.nav-row { display: flex; flex-wrap: wrap; gap: 0.6rem; margin: 0.8rem 0; }
.nav-row .button { width: auto; }

.filter select { margin-bottom: 0.4rem; }

.lesson-card {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.85rem 0;
  border-bottom: 1px solid var(--line);
  color: inherit;
  text-decoration: none;
}

.files { list-style: none; padding: 0; margin: 0; }
.files li {
  display: flex;
  justify-content: space-between;
  gap: 0.8rem;
  align-items: center;
  padding: 0.55rem 0;
  border-bottom: 1px solid var(--line);
}
.files form { margin: 0; }

.gradebook-scroll {
  overflow-x: auto;
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
}

.gradebook {
  border-collapse: separate;
  border-spacing: 0;
  width: max-content;
  min-width: 100%;
}

.gradebook th,
.gradebook td {
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: var(--card);
  min-width: 3.1rem;
  height: 2.9rem;
  text-align: center;
  padding: 0.2rem;
  vertical-align: middle;
}

.gradebook .student-col {
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--card);
  text-align: left;
  min-width: 10.5rem;
  max-width: 14rem;
  padding: 0.35rem 0.7rem;
  box-shadow: 1px 0 0 var(--line);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.gradebook thead .student-col { z-index: 2; }

.day { display: block; font-size: 1rem; line-height: 1.1; }
.mon { display: block; font-size: 0.75rem; color: var(--muted); font-weight: 500; }

button.absent-all {
  background: none;
  color: var(--blue);
  font-size: 0.72rem;
  padding: 0.15rem;
  min-width: 0;
}

.mark {
  display: inline-block;
  min-width: 1.3rem;
  margin: 0 0.05rem;
  font-weight: 700;
  position: relative;
  padding: 0.12rem 0.28rem;
}

.mark.absent { color: var(--absent); }

.mark.weighted::after {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  border-top: 0.55rem solid #d92d20;
  border-left: 0.55rem solid transparent;
}

.avg.good { color: var(--good); font-weight: 700; }
.avg.bad { color: var(--red); font-weight: 700; }

.mark-cell:focus {
  outline: 3px solid var(--focus);
  outline-offset: -3px;
}

.popover {
  position: fixed;
  z-index: 30;
  width: min(22rem, calc(100vw - 1rem));
  background: var(--card);
  color: var(--text);
  border-radius: 12px;
  box-shadow: var(--popover-shadow);
  padding: 0.7rem;
}

.popover[hidden] { display: none; }

.popover .values { display: flex; flex-wrap: wrap; gap: 0.35rem; }

.popover .values button {
  min-width: 2.75rem;
  min-height: 2.75rem;
  padding: 0.35rem;
}

.popover button[data-value="Н"] { background: var(--soft); color: var(--absent); }

.popover .hint { margin: 0.45rem 0; font-size: 0.9rem; }

.mark-row {
  display: flex;
  gap: 0.35rem;
  align-items: center;
  margin-top: 0.4rem;
}

.mark-row select { width: auto; flex: 1; padding: 0.4rem; }

.popover .add-mark { margin-top: 0.7rem; width: 100%; background: var(--soft); color: var(--text); }

input:focus,
textarea:focus,
button:focus-visible,
a:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

@media (max-width: 420px) {
  .student { align-items: flex-start; flex-direction: column; }
}

.link-box {
  font-family: Consolas, ui-monospace, monospace;
  font-size: 0.95rem;
}

body.student {
  padding-bottom: 5.5rem;
}

body.student main {
  background: transparent;
  padding: 0;
}

.who {
  margin: 0 0 0.85rem;
  font-weight: 700;
}

.week-nav {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.8rem;
}

.week-label {
  text-align: center;
  font-weight: 700;
}

.week-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2.75rem;
  min-height: 2.75rem;
  background: var(--card);
  border-radius: 12px;
  text-decoration: none;
  color: var(--text);
  font-weight: 700;
}

.this-week {
  display: block;
  text-align: center;
  margin: -0.2rem 0 0.85rem;
}

.day-card,
.subject-card {
  background: var(--card);
  border-radius: 18px;
  padding: 1rem 1rem 1.15rem;
  margin: 0 0 0.75rem;
  box-shadow: var(--card-shadow);
}

.day-card h2 {
  margin: 0 0 0.45rem;
  font-size: 1.05rem;
}

.day-card p { margin: 0.35rem 0; }

.day-card.today { box-shadow: inset 3px 0 0 var(--blue); }

.today-mark {
  color: var(--blue);
  font-size: 0.85rem;
  font-weight: 600;
}

.homework { white-space: pre-wrap; }

.day-quiet {
  margin: 0;
  padding: 0.28rem 0.15rem;
  color: var(--muted);
  font-size: 0.95rem;
}

.subject-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
}

.subject-top h1 {
  margin: 0;
  font-size: 1.2rem;
}

.subject-avg {
  margin: 0;
  white-space: nowrap;
}

.subject-avg span {
  color: var(--muted);
  font-weight: 600;
  margin-right: 0.2rem;
}

.subject-avg strong { font-size: 1.35rem; }

.goal {
  margin: 0.35rem 0 0;
  color: var(--muted);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 0.9rem 0.75rem;
  margin-top: 0.9rem;
}

.chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 3.2rem;
  max-width: 6rem;
  gap: 0.12rem;
}

.chip .when {
  font-size: 0.78rem;
  font-weight: 700;
}

.chip .val {
  width: 2.7rem;
  height: 2.7rem;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.45rem;
  font-weight: 700;
  line-height: 1;
}

.chip-note {
  font-size: 0.68rem;
  color: var(--muted);
  text-align: center;
  line-height: 1.15;
}

.chip.m5 .when { color: #1e9d43; }
.chip.m5 .val { background: #3dcf6e; color: #0e5c2c; }
.chip.m4 .when { color: #3aaa4a; }
.chip.m4 .val { background: #a9e3b6; color: #146632; }
.chip.m3 .when { color: #ef8a16; }
.chip.m3 .val { background: #f5a033; color: #fff; }
.chip.m2 .when { color: #e3542f; }
.chip.m2 .val { background: #ef6844; color: #fff; }
.chip.mn .when { color: #9aa3ad; }
.chip.mn .val { background: #e7eaee; color: #6d7680; }

html[data-theme="dark"] .chip.m5 .when { color: #5dce7a; }
html[data-theme="dark"] .chip.m4 .when { color: #8ed39a; }
html[data-theme="dark"] .chip.m3 .when { color: #ffb45a; }
html[data-theme="dark"] .chip.m2 .when { color: #ff8d74; }
html[data-theme="dark"] .chip.mn .when { color: #b7c0ca; }
html[data-theme="dark"] .chip.mn .val { background: #2a3340; color: #d5dde6; }

.forget { margin-top: 1.4rem; }

.student-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  display: flex;
  background: var(--nav);
  border-top: 1px solid var(--line);
}

.student-nav a {
  flex: 1;
  text-align: center;
  text-decoration: none;
  color: var(--muted);
  font-weight: 700;
  padding: 0.9rem 0.4rem calc(0.9rem + env(safe-area-inset-bottom));
}

.student-nav a[aria-current="page"] { color: var(--blue); }

.calc-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  margin: 0.4rem 0 0.8rem;
}

.calc-head h2 { margin: 0; }

.calc-head .button { width: auto; }

.calc-keys {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
}

.calc-keys button {
  min-height: 3rem;
  font-size: 1.3rem;
  font-weight: 700;
}

.calc-keys [data-add="2"] { background: #ef6844; }
.calc-keys [data-add="3"] { background: #f5a033; }
.calc-keys [data-add="4"] { background: #8fd18a; color: #146632; }
.calc-keys [data-add="5"] { background: #3dcf6e; color: #0e5c2c; }

.form-error {
  margin: 0;
  color: var(--red);
  font-weight: 600;
}

.list-actions {
  display: flex;
  gap: 1rem;
  margin: 0.8rem 0 0;
}

.chip[data-remove] { cursor: pointer; }
