:root {
  --bg: #f5f7fa;
  --surface: #ffffff;
  --ink: #212121;
  --muted: #5f6b76;
  --line: #e3e8ee;
  --primary: #1e88e5;
  --primary-soft: #e3f2fd;
  --danger: #c62828;
  --shadow: 0 8px 24px rgba(28, 37, 38, 0.06);
  --nav-h: 74px;
  --radius: 16px;
  color-scheme: light;
}

html[data-theme="dark"] {
  --bg: #1c2526;
  --surface: #2a3435;
  --ink: #e0e0e0;
  --muted: #a8b4b6;
  --line: #3d4a4b;
  --primary: #42a5f5;
  --primary-soft: #16344a;
  --danger: #ef9a9a;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  font: 16px/1.45 "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
}

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; opacity: 0.55; }
a { color: var(--primary); }

.app {
  min-height: 100dvh;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto 1fr auto;
}

header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--bg);
}

.header-title {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
}

.icon-button {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  display: grid;
  place-items: center;
}

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

main {
  padding: 4px 16px calc(var(--nav-h) + 16px);
  width: min(820px, 100%);
  min-width: 0;
}

h2 { margin: 0; font-size: 22px; }
h3 { margin: 0; font-size: 16px; }
p { margin: 0; }

.stack { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.muted { color: var(--muted); }
.date { color: var(--muted); text-transform: capitalize; }
.meta { color: var(--muted); font-size: 13px; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}

button.card {
  width: 100%;
  text-align: left;
  border: 0;
}

.field {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 12px;
  padding: 12px 14px;
}

.field:focus {
  outline: 2px solid var(--primary);
  border-color: transparent;
}

.combo { position: relative; }
.suggest {
  position: absolute;
  z-index: 20;
  left: 0;
  right: 0;
  top: calc(100% + 4px);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  max-height: 260px;
  overflow: auto;
}

.suggest button {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  text-align: left;
  background: none;
  border: 0;
  padding: 10px 12px;
}

.suggest button small { color: var(--muted); }
.suggest button:hover,
.suggest button:focus { background: var(--primary-soft); }

.primary, .ghost, .danger {
  border-radius: 12px;
  padding: 10px 14px;
}

.primary { background: var(--primary); color: #fff; border: 0; }
.ghost { background: transparent; color: var(--primary); border: 1px solid var(--line); }
.danger { background: transparent; color: var(--danger); border: 1px solid var(--line); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--line);
  background: var(--primary-soft);
  color: var(--primary);
  border-radius: 999px;
  padding: 6px 12px;
}
.chip[aria-pressed="true"] { background: var(--primary); color: #fff; border-color: transparent; }

.saved {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  font-weight: 650;
}

.time {
  color: var(--primary);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.slot { display: flex; flex-direction: column; gap: 6px; }
.lesson-part {
  border-left: 3px solid var(--accent, var(--primary));
  padding-left: 10px;
}
.lesson-part.split {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
}
.lesson-name { font-weight: 650; }
.is-off { opacity: 0.55; }

.mark-round, .mark-tri { display: inline-block; margin-right: 6px; vertical-align: -1px; }
.mark-round { width: 10px; height: 10px; border-radius: 50%; background: var(--primary); }
.mark-tri {
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-bottom: 10px solid var(--primary);
}
.mark-round.is-off { background: #9aa3ab; }
.mark-tri.is-off { border-bottom-color: #9aa3ab; }

.day-toggle {
  width: 100%;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  background: transparent;
  border: 0;
  padding: 12px 2px 6px;
  font-weight: 700;
}
.day.is-today .day-toggle { color: var(--primary); }
.day-body { display: flex; flex-direction: column; gap: 8px; }

.news-row {
  display: flex;
  gap: 12px;
  width: 100%;
  min-width: 0;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  padding-bottom: 4px;
}
.news-card {
  flex: 0 0 min(280px, 80vw);
  scroll-snap-align: start;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.news-card img,
.dialog-body img {
  width: 100%;
  height: 140px;
  object-fit: cover;
  border-radius: 12px;
  background: var(--line);
}
.clamp {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

details.subject { padding: 12px 14px; }
details.subject summary {
  cursor: pointer;
  list-style: none;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}
details.subject summary > span:first-child { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
details.subject summary::-webkit-details-marker { display: none; }
.subject-title { font-weight: 700; }
.grade-lesson { padding: 8px 0; border-top: 1px solid var(--line); }
.grade-top { display: flex; justify-content: space-between; gap: 8px; }

.mark {
  border-radius: 8px;
  padding: 2px 8px;
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 700;
  white-space: nowrap;
}
.mark.good { background: #e8f5e9; color: #2e7d32; }
.mark.ok { background: #e3f2fd; color: #1565c0; }
.mark.mid { background: #fff8e1; color: #ef6c00; }
.mark.bad { background: #ffebee; color: #c62828; }

.steps { margin: 0; padding-left: 18px; color: var(--muted); }
.steps li { margin: 4px 0; }
details.help summary { cursor: pointer; color: var(--primary); }

.tabs {
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--line);
  position: sticky;
  bottom: 0;
  z-index: 6;
}
.brand { display: none; margin: 0; }
.tabs a {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  text-align: center;
  text-decoration: none;
  color: var(--muted);
  font-size: 12px;
  padding: 8px 4px calc(8px + env(safe-area-inset-bottom));
}
.tabs a[aria-current="page"] { color: var(--primary); font-weight: 700; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-h) + 12px);
  transform: translateX(-50%);
  background: #1c2526;
  color: #fff;
  padding: 10px 14px;
  border-radius: 12px;
  max-width: min(440px, calc(100vw - 24px));
  z-index: 40;
}

html[data-theme="dark"] #toast { background: #e8eef0; color: #1c2526; }

dialog {
  border: 0;
  padding: 0;
  width: min(560px, calc(100vw - 24px));
  border-radius: 18px;
  background: var(--surface);
  color: var(--ink);
}
dialog::backdrop { background: rgba(18, 22, 26, 0.48); }
.dialog-body { padding: 16px 16px 20px; max-height: min(80dvh, 720px); overflow: auto; display: flex; flex-direction: column; gap: 12px; }
.dialog-top { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }

@media (min-width: 960px) {
  .app { grid-template-columns: 240px minmax(0, 1fr); grid-template-rows: auto 1fr; }
  header, main { grid-column: 2; }
  .header-title { position: absolute; width: 1px; height: 1px; overflow: hidden; }
  main { padding: 8px 28px 48px; justify-self: start; }
  .tabs {
    grid-column: 1;
    grid-row: 1 / span 2;
    position: sticky;
    top: 0;
    height: 100dvh;
    flex-direction: column;
    align-items: stretch;
    border-top: 0;
    border-right: 1px solid var(--line);
    padding: 18px 12px;
  }
  .brand { display: block; font-weight: 750; font-size: 18px; padding: 8px 12px 18px; }
  .tabs a {
    flex: 0;
    flex-direction: row;
    justify-content: flex-start;
    gap: 10px;
    font-size: 15px;
    text-align: left;
    padding: 12px;
    border-radius: 12px;
  }
  .tabs a[aria-current="page"] { background: var(--primary-soft); }
  #toast { bottom: 24px; }
}
