:root {
  --bg: #ffffff;
  --bg-soft: #f7f7f8;
  --text: #1d1d1f;
  --muted: #8a8a8e;
  --line: #ececec;
  --line-strong: #dcdcdf;
  --today-bg: #f4f8ff;
  --accent: #e5484d;
  --now: #ff3b30;
  --holiday: #f3f3f4;
  --btn: #f2f2f4;
  --btn-active: #ffffff;
  --fill-class: 14%;
  --fill-study: 6%;
  --ink-mix: 42%;
  --tw: 52px;          /* колонка времени */
  --hh: 58px;          /* строка дат */
  --H: 52px;           /* 1 час */
  --cw: 180px;         /* ширина дня, пересчитывается в JS */
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1c1e;
    --bg-soft: #232326;
    --text: #f2f2f7;
    --muted: #8e8e93;
    --line: #2c2c2e;
    --line-strong: #3a3a3c;
    --today-bg: #1f2530;
    --holiday: #262628;
    --btn: #2c2c2e;
    --btn-active: #48484a;
    --fill-class: 24%;
    --fill-study: 10%;
    --ink-mix: 70%;
  }
}
:root[data-theme="dark"] {
  --bg: #1c1c1e; --bg-soft: #232326; --text: #f2f2f7; --muted: #8e8e93; --line: #2c2c2e;
  --line-strong: #3a3a3c; --today-bg: #1f2530; --holiday: #262628; --btn: #2c2c2e; --btn-active: #48484a;
  --fill-class: 24%; --fill-study: 10%; --ink-mix: 70%;
}
@media (max-width: 767px) {
  :root { --tw: 44px; --H: 56px; }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 13px/1.3 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; }

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  padding-left: var(--safe-l);
  padding-right: var(--safe-r);
}

/* ——— Шапка ——— */
.bar {
  flex: none;
  padding-top: env(safe-area-inset-top, 0px);
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
.bar-row {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 52px;
  padding: 0 12px;
}
.btn {
  height: 30px;
  padding: 0 12px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: var(--bg);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
}
.btn:hover { background: var(--bg-soft); }
.btn.icon { width: 30px; padding: 0; font-size: 20px; line-height: 26px; }
.nav { display: flex; gap: 4px; }
.title { flex: 1; min-width: 0; padding-left: 4px; }
.title-main {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.title-sub { color: var(--muted); font-size: 12px; white-space: nowrap; }
.seg {
  display: flex;
  padding: 2px;
  border-radius: 8px;
  background: var(--btn);
}
.seg button {
  width: 32px;
  height: 26px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
}
.seg button[aria-pressed="true"] {
  background: var(--btn-active);
  color: var(--text);
  box-shadow: 0 0 0 0.5px var(--line-strong);
}

/* Полоса ближайших дедлайнов */
.strip {
  display: flex;
  gap: 6px;
  padding: 0 12px 8px;
  overflow-x: auto;
  scrollbar-width: none;
  overscroll-behavior-x: contain;
}
.strip::-webkit-scrollbar { display: none; }
.strip:empty { display: none; }
.chip {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 320px;
  height: 26px;
  padding: 0 10px 0 8px;
  border: 1px solid var(--line-strong);
  border-radius: 13px;
  background: var(--bg);
  cursor: pointer;
  font-size: 12px;
}
.chip .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--c, var(--muted)); flex: none; }
.chip .when { font-weight: 600; white-space: nowrap; }
.chip .when.hot { color: var(--accent); }
.chip .what { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.chip.ghost { color: var(--muted); border-style: dashed; }

/* ——— Лента ——— */
.scroller {
  flex: 1;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  scroll-snap-type: x mandatory;
  scroll-padding-left: var(--tw);
  touch-action: pan-x pan-y;
  -webkit-overflow-scrolling: touch;
  outline: none;
  cursor: grab;
}
.scroller.nosnap { scroll-snap-type: none; }
.scroller.grabbing { cursor: grabbing; }
.scroller.grabbing .ev { cursor: grabbing; }
.grid {
  position: relative;
  display: grid;
  grid-template-rows: var(--hh) auto;
  width: max-content;
  min-width: 100%;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.corner {
  position: sticky;
  top: 0;
  left: 0;
  z-index: 5;
  /* sticky ограничен областью сетки → растягиваем область на всю сетку */
  grid-row: 1 / -1;
  grid-column: 1 / -1;
  align-self: start;
  justify-self: start;
  width: var(--tw);
  height: var(--hh);
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.dhead {
  position: sticky;
  top: 0;
  z-index: 3;
  grid-row: 1 / -1;
  align-self: start;
  height: var(--hh);
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  padding: 6px 6px 0;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  border-left: 1px solid var(--line);
  scroll-snap-align: start;
}
.dhead .d {
  display: flex;
  align-items: center;
  gap: 5px;
  height: 26px;
  white-space: nowrap;
}
.dhead .wd { color: var(--muted); font-size: 12px; }
.dhead .num {
  display: inline-grid;
  place-items: center;
  min-width: 26px;
  height: 26px;
  padding: 0 4px;
  border-radius: 13px;
  font-size: 17px;
  font-weight: 500;
  letter-spacing: -0.02em;
}
.dhead .mon { color: var(--muted); font-size: 12px; }
.dhead.today .num { background: var(--accent); color: #fff; font-weight: 600; }
.dhead.today .wd { color: var(--accent); font-weight: 600; }
.dhead.weekend .num { color: var(--muted); }
.dhead.today.weekend .num { color: #fff; }
.flags { display: flex; gap: 3px; min-width: 0; height: 20px; }
.flag {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
  height: 18px;
  padding: 0 6px 0 5px;
  border-radius: 4px;
  border-left: 3px solid var(--c, var(--muted));
  background: color-mix(in srgb, var(--c, var(--muted)) var(--fill-class), var(--bg));
  font-size: 11px;
  line-height: 18px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
  flex: 0 1 auto;
}
.flag span { overflow: hidden; text-overflow: ellipsis; }
.flag.more { flex: none; border-left-color: transparent; background: var(--btn); color: var(--muted); padding: 0 5px; }
.flag.holiday { --c: var(--muted); background: var(--holiday); color: var(--muted); cursor: default; }
.flag.event { background: var(--bg); box-shadow: inset 0 0 0 1px var(--c); }

.tcol {
  position: sticky;
  left: 0;
  z-index: 4;
  grid-row: 2;
  grid-column: 1 / -1;
  justify-self: start;
  width: var(--tw);
  background: var(--bg);
}
.tick {
  position: absolute;
  right: 6px;
  transform: translateY(-50%);
  color: var(--muted);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}
.now-label {
  position: absolute;
  right: 3px;
  z-index: 2;
  transform: translateY(-50%);
  padding: 1px 4px;
  border-radius: 4px;
  background: var(--now);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.dcol {
  position: relative;
  grid-row: 2;
  border-left: 1px solid var(--line);
  background-image: linear-gradient(to bottom, var(--line) 1px, transparent 1px);
  background-size: 100% var(--H);
}
.dcol.today, .dhead.today { background-color: var(--today-bg); }
.dcol.holiday { background-color: var(--holiday); }

/* Линия «сейчас» */
.now-line {
  position: absolute;
  left: -1px;
  right: 0;
  z-index: 3;
  height: 2px;
  margin-top: -1px;
  background: var(--now);
  pointer-events: none;
}
.now-line::before {
  content: "";
  position: absolute;
  left: -4px;
  top: -4px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--now);
}

/* ——— События ——— */
.ev {
  position: absolute;
  z-index: 1;
  overflow: hidden;
  padding: 3px 6px 3px 8px;
  border-radius: 5px;
  border-left: 3px solid var(--c);
  background: color-mix(in srgb, var(--c) var(--fill-class), var(--bg));
  cursor: pointer;
  font-size: 12px;
  line-height: 15px;
}
.ev .t {
  font-weight: 600;
  color: color-mix(in srgb, var(--c) var(--ink-mix), var(--text));
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ev .m {
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-variant-numeric: tabular-nums;
}
.ev .g {
  color: var(--text);
  opacity: 0.75;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}
.ev.short { padding-top: 1px; padding-bottom: 1px; }
.ev.short .t { display: inline; }
.ev.short .m { display: inline; margin-left: 5px; }
.ev.study, .ev.gym {
  border-left-style: dashed;
  background: color-mix(in srgb, var(--c) var(--fill-study), var(--bg));
}
.ev.event {
  border: 1.5px solid var(--c);
  border-left-width: 3px;
  background: color-mix(in srgb, var(--c) var(--fill-class), var(--bg));
  z-index: 2;
}
.ev.past { opacity: 0.45; }
.ev.live { box-shadow: inset 0 0 0 1.5px var(--c); }
.ev.live.event { box-shadow: none; }
.ev .live-tag { color: var(--now); font-weight: 600; }
.ev.cancelled .t, .ev.cancelled .m { text-decoration: line-through; }
.ev.cancelled { opacity: 0.55; }
.badge {
  display: inline-block;
  margin-left: 4px;
  padding: 0 3px;
  border-radius: 3px;
  border: 1px solid currentColor;
  font-size: 9px;
  font-weight: 600;
  line-height: 12px;
  vertical-align: 1px;
  opacity: 0.8;
}

/* ——— Карточка ——— */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(0, 0, 0, 0.12);
}
.sheet {
  position: fixed;
  z-index: 21;
  width: 340px;
  max-height: min(70dvh, 560px);
  overflow: auto;
  padding: 16px 18px 18px;
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.4;
}
.sheet[hidden], .sheet-backdrop[hidden] { display: none; }
.sheet-grip { display: none; }
.sheet-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 14px;
  background: var(--btn);
  color: var(--muted);
  font-size: 18px;
  line-height: 28px;
  cursor: pointer;
}
.sh-head { display: flex; gap: 10px; padding-right: 30px; }
.sh-bar { flex: none; width: 4px; border-radius: 2px; background: var(--c); }
.sh-bar.dashed { background: none; border-left: 4px dashed var(--c); width: 0; }
.sh-title { font-size: 17px; font-weight: 600; line-height: 1.25; }
.sh-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.sh-rows { margin: 14px 0 0; display: grid; grid-template-columns: 108px 1fr; gap: 8px 10px; }
.sh-rows dt { color: var(--muted); font-size: 13px; }
.sh-rows dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.sh-rows a { color: #0a84ff; text-decoration: none; }
.sh-note { white-space: pre-line; }
.sh-list { margin: 12px 0 0; padding: 0; list-style: none; }
.sh-list li { display: flex; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line); }
.sh-list .dot { width: 8px; height: 8px; margin-top: 6px; border-radius: 50%; background: var(--c, var(--muted)); flex: none; }

@media (max-width: 767px), (pointer: coarse) {
  .scroller { scrollbar-width: none; }
  .scroller::-webkit-scrollbar { display: none; }
}
@media (max-width: 767px) {
  .bar-row { gap: 6px; padding: 0 10px; height: 48px; }
  .btn { padding: 0 9px; }
  .title-main { font-size: 15px; }
  .seg button { width: 28px; }
  .strip { padding: 0 10px 8px; }
  .chip { max-width: 250px; }
  .sheet {
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    max-height: 82dvh;
    padding: 8px 18px calc(20px + env(safe-area-inset-bottom, 0px));
    border-radius: 14px 14px 0 0;
    border-width: 1px 0 0;
    animation: up 0.22s ease-out;
  }
  .sheet-grip { display: block; width: 36px; height: 5px; margin: 0 auto 10px; border-radius: 3px; background: var(--line-strong); }
  .sheet-close { top: 14px; right: 12px; }
  .sheet-backdrop { background: rgba(0, 0, 0, 0.25); }
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .sheet { animation: none; } }
