:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f0f1f6;
  --text: #1c1d26;
  --muted: #6b6f80;
  --line: #e3e5ee;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --today: #eef0ff;
  --link: #1d5fc4;
  --day-label: #2f5a8a;
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111218;
    --surface: #1b1c24;
    --surface-2: #252733;
    --text: #ecedf3;
    --muted: #9a9eb0;
    --line: #2d2f3c;
    --accent: #818cf8;
    --accent-text: #111218;
    --danger: #f87171;
    --today: #262a4a;
    --link: #8ab4f8;
    --day-label: #2a4a70;
    color-scheme: dark;
  }
}

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

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button, input, select, textarea { font: inherit; color: inherit; }

/* ---------- кнопки и поля ---------- */
.btn {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 10px;
  padding: 9px 14px;
  cursor: pointer;
  font-weight: 500;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: 0.5; }

.icon-btn {
  border: 0;
  background: transparent;
  font-size: 22px;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }

.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); margin-bottom: 14px; flex: 1; }
.field input, .field select, .field textarea {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-size: 16px; /* иначе iOS увеличивает страницу при фокусе */
  color: var(--text);
  min-height: 44px;
}
.row { display: flex; gap: 10px; }
.error { color: var(--danger); min-height: 1em; margin: 4px 0 10px; font-size: 14px; }
.muted { color: var(--muted); font-size: 13px; margin: 6px 0 10px; }

/* ---------- вход ---------- */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.auth-card { width: 100%; max-width: 360px; background: var(--surface); padding: 28px 22px; border-radius: 20px; box-shadow: 0 10px 40px rgb(0 0 0 / 0.08); }
.auth-logo { width: 64px; height: 64px; border-radius: 16px; display: block; margin: 0 auto 10px; }
.auth h1 { text-align: center; font-size: 22px; margin: 0 0 18px; }
.tabs { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; margin-bottom: 18px; }
.tab { flex: 1; border: 0; background: transparent; padding: 8px; border-radius: 8px; cursor: pointer; }
.tab.active { background: var(--surface); box-shadow: 0 1px 3px rgb(0 0 0 / 0.1); font-weight: 600; }

/* ---------- шапка ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.nav { display: flex; align-items: center; gap: 2px; }
.nav h2 { margin: 0; font-size: 20px; min-width: 150px; text-align: center; text-transform: capitalize; }
.actions { display: flex; align-items: center; gap: 6px; }
.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.seg-btn { border: 0; background: transparent; padding: 6px 10px; border-radius: 8px; cursor: pointer; font-size: 14px; }
.seg-btn.active { background: var(--surface); box-shadow: 0 1px 3px rgb(0 0 0 / 0.1); font-weight: 600; }

.banner {
  margin: 0 12px 8px;
  padding: 10px 14px;
  border-radius: 12px;
  background: var(--today);
  font-size: 14px;
}
.banner button { margin-left: 8px; }

/* ---------- месяц ---------- */
.layout { display: grid; grid-template-columns: 1fr; gap: 12px; padding: 0 12px calc(90px + env(safe-area-inset-bottom)); max-width: 1200px; margin: 0 auto; }
@media (min-width: 900px) {
  .layout { grid-template-columns: 1fr 340px; }
  #list-view { grid-column: 1 / -1; }
}

.weekdays { display: grid; grid-template-columns: repeat(7, 1fr); text-align: center; font-size: 12px; color: var(--muted); padding: 4px 0; }
.grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 1px; background: var(--line); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.cell {
  background: var(--surface);
  min-height: 92px;
  padding: 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  border: 0;
  text-align: left;
}
.cell.other { background: var(--surface-2); color: var(--muted); }
.cell.selected { outline: 2px solid var(--accent); outline-offset: -2px; }
.cell .num { font-size: 13px; width: 24px; height: 24px; display: grid; place-items: center; border-radius: 50%; }
.cell.today .num { background: var(--accent); color: var(--accent-text); font-weight: 700; }
.chip {
  font-size: 11px;
  line-height: 16px;
  padding: 0 4px;
  border-radius: 4px;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.more { font-size: 11px; color: var(--muted); padding-left: 4px; }

@media (max-width: 600px) {
  .cell { min-height: 54px; align-items: center; }
  .chips-wrap { display: flex; flex-wrap: wrap; gap: 2px; justify-content: center; }
  .chip { width: 6px; height: 6px; padding: 0; border-radius: 50%; font-size: 0; }
  .more { display: none; }
  .nav h2 { font-size: 17px; min-width: 120px; }
  .actions .btn.ghost { padding: 6px 8px; }
}

/* ---------- неделя («дневник») ---------- */
.week { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr; gap: 20px; align-items: start; }
@media (min-width: 1100px) {
  .week { grid-template-columns: 1fr 1fr; }
}
.day-card {
  display: flex;
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.06);
  border: 1px solid var(--line);
}
.day-label {
  flex: none;
  width: 44px;
  background: var(--day-label);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}
.day-label span {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  padding: 12px 0;
}
.day-card.is-today .day-label { background: var(--accent); }
.day-table { flex: 1; min-width: 0; border-collapse: collapse; table-layout: fixed; }
.day-table th {
  font-weight: 600;
  font-size: 14px;
  padding: 10px 12px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--line);
}
.day-table th + th, .day-table td + td { border-left: 1px solid var(--line); }
.day-table th.notes-col, .day-table td.notes-col { width: 36%; }
.day-table th.who-col, .day-table td.who-col { width: 24%; }
.day-table td { padding: 10px 12px; vertical-align: top; border-bottom: 1px solid var(--line); font-size: 14px; }
.day-table tr:last-child td { border-bottom: 0; }
.day-table .lesson { display: flex; gap: 12px; }
.day-table .lesson > div { min-width: 0; }
.day-table .n { color: var(--muted); min-width: 12px; padding-top: 1px; }
.day-table .link {
  border: 0;
  background: none;
  padding: 0;
  color: var(--link);
  font-weight: 600;
  font-size: 15px;
  text-align: left;
  cursor: pointer;
  overflow-wrap: anywhere;
}
.day-table .link:hover { text-decoration: underline; }
.day-table .time { color: var(--muted); font-size: 14px; margin-top: 1px; }
.row-del {
  order: 3;
  margin-left: auto;
  flex: none;
  border: 0;
  background: none;
  cursor: pointer;
  font-size: 15px;
  padding: 2px 4px;
  border-radius: 6px;
  opacity: 0.45;
  align-self: flex-start;
}
.row-del:hover, .row-del:focus-visible { opacity: 1; background: var(--surface-2); }
@media (hover: none) { .row-del { opacity: 0.7; } }

dialog.sheet.ask { width: min(400px, 100vw); }
.ask h3 { margin: 0 0 4px; font-size: 17px; }
.ask-buttons { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }

/* Свободная высота карточки (из-за вертикальной даты) уходит в строку «Добавить», а не в события */
.day-table tbody tr:not(.add-row) { height: 1px; }
.day-table .add-row td { padding: 8px 12px; vertical-align: bottom; }
.day-table .add-row .link { font-weight: 500; font-size: 14px; color: var(--muted); }
.notes { position: relative; padding-right: 30px; }
.notes-text { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; white-space: pre-line; overflow-wrap: anywhere; }
.notes.open .notes-text { display: block; }
.more-btn {
  position: absolute;
  right: 0;
  bottom: 0;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--link);
  border-radius: 999px;
  font-size: 9px;
  letter-spacing: 1px;
  padding: 3px 6px;
  cursor: pointer;
}
.who { display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: center; font-size: 13px; }
.who .person { display: inline-flex; align-items: center; gap: 5px; }
.who .dot { width: 8px; height: 8px; border-radius: 50%; }
.who .icons { display: inline-flex; gap: 4px; }
.inline-only { display: none; margin-top: 4px; }
.inline-only .notes { margin-bottom: 4px; }
@media (max-width: 600px) {
  .day-table .who-col, .day-table .notes-col { display: none; }
  .inline-only { display: block; }
  .day-label { width: 36px; }
  .day-label span { font-size: 14px; }
}

/* ---------- списки событий ---------- */
#side h3 { margin: 6px 4px 10px; font-size: 16px; }
.event-list { display: flex; flex-direction: column; gap: 8px; }
.day-head { font-weight: 600; margin: 14px 4px 2px; font-size: 14px; color: var(--muted); }
.event {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 4px solid var(--c, var(--accent));
  border-radius: 12px;
  padding: 10px 12px;
  cursor: pointer;
  text-align: left;
  width: 100%;
}
.event .time { font-size: 13px; color: var(--muted); min-width: 64px; }
.event .t { font-weight: 600; }
.event .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.empty { color: var(--muted); padding: 16px 4px; }

.fab {
  position: fixed;
  right: 20px;
  bottom: calc(20px + env(safe-area-inset-bottom));
  width: 58px;
  height: 58px;
  border-radius: 50%;
  border: 0;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 32px;
  line-height: 1;
  box-shadow: 0 8px 24px rgb(79 70 229 / 0.4);
  cursor: pointer;
}

/* ---------- диалоги ---------- */
dialog.sheet {
  border: 0;
  padding: 0;
  width: min(560px, 100vw);
  max-height: 92dvh;
  border-radius: 20px;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}
dialog.sheet[open] { display: flex; flex-direction: column; }
dialog.sheet form { display: flex; flex-direction: column; min-height: 0; flex: 1; }
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
@media (max-width: 600px) {
  dialog.sheet { margin: auto 0 0; max-width: 100vw; border-radius: 20px 20px 0 0; }
}
.sheet-head {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 10px 8px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.sheet-head h3 { margin: 0; font-size: 16px; }
.sheet-head > :first-child { justify-self: start; }
.sheet-head > :last-child { justify-self: end; }
.sheet-body { padding: 16px 16px calc(24px + env(safe-area-inset-bottom)); overflow-y: auto; }
.title-input {
  width: 100%;
  font-size: 20px;
  font-weight: 600;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  padding: 8px 2px;
  margin-bottom: 14px;
  outline: none;
}
.switch-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.switch-row input { width: 22px; height: 22px; accent-color: var(--accent); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 14px;
  cursor: pointer;
  color: var(--text);
}
.chips button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.chips button .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: var(--c); }

.card { background: var(--surface); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.card h4 { margin: 0; }
.card .field { margin: 0; width: 100%; }
.colors { display: flex; gap: 8px; flex-wrap: wrap; }
.colors button { width: 30px; height: 30px; border-radius: 50%; border: 3px solid transparent; cursor: pointer; }
.colors button.on { border-color: var(--text); }
.people { list-style: none; padding: 0; margin: 0; width: 100%; }
.people li { display: flex; align-items: center; gap: 8px; padding: 6px 0; }
.people .dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.people .grow { flex: 1; min-width: 0; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.invite-form { display: flex; gap: 8px; width: 100%; }
.invite-form input {
  flex: 1;
  min-width: 0;
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-size: 16px;
}
.center { text-align: center; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(96px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 14px;
  z-index: 100;
  max-width: calc(100vw - 32px);
}
