:root {
  --bg: #f3efe6;
  --ink: #1c1914;
  --muted: #6b6458;
  --card: #fffdf8;
  --line: #ddd4c4;
  --accent: #0f5c4c;
  --accent-soft: #d8efe8;
  --warn: #9a3412;
  --mark: #b45309;
  --st-active-bg: #fef6d8;
  --st-active-bd: #e0b44a;
  --st-active-fg: #8a5a12;
  --st-done-bg: #e4f5ea;
  --st-done-bd: #3d9a5f;
  --st-done-fg: #1a7f37;
  --st-overdue-bg: #fde8e4;
  --st-overdue-bd: #d4533c;
  --st-overdue-fg: #9a3412;
  --st-late-badge: #e0b44a;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Segoe UI", "Trebuchet MS", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at top left, #efe4cf 0, transparent 40%),
    linear-gradient(180deg, #f7f2e8, var(--bg));
  /* в iframe Bitrix 100vh даёт лишний скролл и мешает fitWindow */
  min-height: 0;
}
body.in-bitrix {
  overflow: hidden;
}

.page {
  max-width: none;
  width: 100%;
  margin: 0;
  padding: 12px 16px 20px;
  box-sizing: border-box;
}
.top {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: end;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.eyebrow {
  margin: 0 0 4px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 12px;
  color: var(--muted);
}
h1 { margin: 0; font-size: 28px; font-weight: 700; }
h2 { margin: 0 0 12px; font-size: 16px; }

.controls {
  display: flex;
  gap: 8px;
  align-items: end;
  flex-wrap: wrap;
}
label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
}
input[type="number"] {
  width: 96px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  font: inherit;
}
select {
  min-width: 220px;
  max-width: 360px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  font: inherit;
  color: var(--ink);
}

/* Searchable client combobox (trigger + panel with search) */
.client-field { position: relative; min-width: min(360px, 72vw); }
.client-combobox { position: relative; width: 100%; }
.client-trigger {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: 40px;
  margin: 0;
  padding: 0 0 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
  color: var(--ink);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.65);
  text-align: left;
  gap: 0;
}
.client-trigger:hover { filter: none; background: #fff; border-color: #cfc4b0; }
.client-trigger:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.client-trigger[aria-expanded="true"] {
  border-color: var(--accent);
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  background: #fff;
}
.client-trigger:disabled {
  opacity: 0.72;
  cursor: not-allowed;
  background: #f3efe6;
}
.client-value {
  flex: 1;
  min-width: 0;
  padding: 8px 8px 8px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
}
.client-value.is-placeholder { color: var(--muted); }
.client-chevron {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 40px;
  align-self: stretch;
  width: 40px;
  color: var(--muted);
  border-left: 1px solid var(--line);
  background: linear-gradient(180deg, #faf6ee, #f0e8d8);
  border-radius: 0 9px 9px 0;
}
.client-trigger[aria-expanded="true"] .client-chevron {
  border-radius: 0 9px 0 0;
  color: var(--accent);
  background: var(--accent-soft);
}
.client-trigger[aria-expanded="true"] .client-chevron svg {
  transform: rotate(180deg);
}
.client-chevron svg { transition: transform 0.15s ease; display: block; }

.client-panel {
  position: absolute;
  z-index: 40;
  top: calc(100% - 1px);
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  /* ~8 строк списка + поиск; дальше только скролл */
  max-height: min(300px, 42vh);
  border: 1px solid var(--accent);
  border-top: 1px solid var(--line);
  border-radius: 0 0 10px 10px;
  background: #fffdf8;
  box-shadow: 0 14px 28px rgba(40, 28, 12, 0.14);
  overflow: hidden;
}
.client-panel[hidden] { display: none; }
.client-search-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  background: #f7f2e8;
}
.client-search-icon { color: var(--muted); flex-shrink: 0; }
.client-search {
  flex: 1;
  min-width: 0;
  width: auto;
  margin: 0;
  padding: 7px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  font: inherit;
  font-size: 13px;
  color: var(--ink);
}
.client-search:focus {
  outline: 2px solid var(--accent);
  outline-offset: 0;
  border-color: transparent;
}
.client-options {
  list-style: none;
  margin: 0;
  padding: 4px;
  flex: 1 1 auto;
  min-height: 0;
  max-height: 220px;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  -webkit-overflow-scrolling: touch;
}
.client-options::-webkit-scrollbar {
  width: 8px;
}
.client-options::-webkit-scrollbar-thumb {
  background: #d2c6b0;
  border-radius: 8px;
}
.client-options::-webkit-scrollbar-track {
  background: transparent;
}
.client-option {
  display: block;
  width: 100%;
  margin: 0;
  padding: 8px 10px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
.client-option:hover,
.client-option.is-active {
  background: var(--accent-soft);
  color: var(--accent);
}
.client-option.is-selected {
  font-weight: 650;
}
.client-option-empty {
  padding: 12px 10px;
  color: var(--muted);
  font-size: 13px;
}
button {
  border: 0;
  border-radius: 8px;
  padding: 10px 14px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  cursor: pointer;
}
button.secondary { background: #5b5347; }
button:hover { filter: brightness(1.05); }

.status {
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent);
  margin-bottom: 16px;
  font-size: 14px;
}
.status.error { background: #ffe8e0; color: var(--warn); }

.layout {
  display: grid;
  /* Календарь забирает свободную ширину; боковые панели компактнее */
  grid-template-columns: minmax(168px, 200px) minmax(0, 1fr) minmax(260px, 300px);
  gap: 12px;
  align-items: start;
}
.calendar-card {
  min-width: 0;
}
.legend-card, .calendar-card, .events-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 16px;
}
.legend-card ul {
  list-style: none;
  margin: 0 0 16px;
  padding: 0;
  display: grid;
  gap: 6px;
  font-size: 13px;
}
.legend-card li {
  display: flex;
  gap: 8px;
  align-items: center;
}
.dot {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 12px;
  background: #f3e7d3;
  color: var(--mark);
  border: 1px solid #e7c48a;
  position: relative;
  flex-shrink: 0;
}
.dot.st-active,
.mark-chip.st-active,
.day .marks span.st-active {
  background: var(--st-active-bg);
  border-color: var(--st-active-bd);
  color: var(--st-active-fg);
}
.dot.st-done,
.mark-chip.st-done,
.day .marks span.st-done {
  background: var(--st-done-bg);
  border-color: var(--st-done-bd);
  color: var(--st-done-fg);
}
.dot.st-overdue,
.mark-chip.st-overdue,
.day .marks span.st-overdue {
  background: var(--st-overdue-bg);
  border-color: var(--st-overdue-bd);
  color: var(--st-overdue-fg);
}
.dot.st-done-late,
.mark-chip.st-done-late,
.day .marks span.st-done-late {
  background: var(--st-done-bg);
  border-color: var(--st-done-bd);
  color: var(--st-done-fg);
}
.dot.st-done-late::after,
.mark-chip.st-done-late::after,
.day .marks span.st-done-late::after {
  content: "";
  position: absolute;
  right: -2px;
  top: -2px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--st-late-badge);
  border: 1px solid #fff;
  box-shadow: 0 0 0 1px rgba(138, 90, 18, 0.25);
}
.status-legend .leg-text {
  display: flex;
  flex-direction: column;
  gap: 1px;
  line-height: 1.25;
}
.status-legend .hint-inline {
  color: var(--muted);
  font-size: 11px;
}
.filters { display: grid; gap: 6px; font-size: 13px; }
.filters label {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  color: var(--ink);
}
.hint { color: var(--muted); font-size: 12px; margin-top: 16px; }

.month-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.month-nav h2 { margin: 0; font-size: 20px; }
.month-nav button {
  width: 40px;
  height: 40px;
  padding: 0;
  background: #efe7d8;
  color: var(--ink);
}
.weekdays {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}
.grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 8px;
  align-items: stretch;
}
.day {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  height: auto;
  min-height: 88px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 6px;
  background: #fff;
  overflow: hidden;
  isolation: isolate;
}
.day.empty {
  background: transparent;
  border-color: transparent;
  pointer-events: none;
}
.day.has {
  background: #fff8ee;
  cursor: pointer;
}
.day.has:hover, .day.selected {
  outline: 2px solid var(--accent);
}
.day .num {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  font-size: clamp(36px, 5.5vw, 64px);
  font-weight: 800;
  line-height: 1;
  letter-spacing: -0.04em;
  color: var(--ink);
  opacity: 0.18;
  pointer-events: none;
  user-select: none;
  z-index: 0;
}
.day.has .num { opacity: 0.22; color: #8a5a12; }
.day.holiday .num { opacity: 0.24; color: #a33; }
.day .marks {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, 22px);
  grid-auto-rows: 22px;
  gap: 4px;
  align-content: start;
  justify-content: start;
  overflow: hidden;
}
.day .marks span {
  display: inline-flex;
  width: 22px;
  height: 22px;
  align-items: center;
  justify-content: center;
  border-radius: 5px;
  background: #fff;
  border: 1px solid #e7c48a;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.04);
  color: var(--mark);
  font-size: 12px;
  font-weight: 800;
  position: relative;
  flex-shrink: 0;
}

.day-events { display: grid; gap: 10px; max-height: 70vh; overflow: auto; }
.event {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  background: #fff;
}
.event.st-overdue { border-color: #f0b4a8; background: #fff8f6; }
.event.st-done, .event.st-done-late { border-color: #b7dfc4; background: #f7fcf8; }
.event .meta {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
  font-size: 12px;
  color: var(--muted);
  align-items: center;
}
.event .meta-right {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.event .mark-chip {
  font-weight: 700;
  width: 22px;
  height: 22px;
  border-radius: 5px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #e7c48a;
  background: #fff;
  position: relative;
  font-size: 12px;
  flex-shrink: 0;
}
.event .ev-status {
  font-size: 11px;
  color: var(--muted);
}
.event .ev-status.st-active { color: var(--st-active-fg); }
.event .ev-status.st-done,
.event .ev-status.st-done-late { color: var(--st-done-fg); }
.event .ev-status.st-overdue { color: var(--st-overdue-fg); }
.event .task-link {
  display: inline-flex;
  width: 22px;
  height: 22px;
  align-items: center;
  justify-content: center;
  border: 1px solid #c45c5c;
  border-radius: 4px;
  color: #b33;
  background: #fff;
  flex-shrink: 0;
  text-decoration: none;
}
.event .task-link:hover {
  background: #fdeeee;
  border-color: #a33;
}
.event .task-link svg {
  width: 14px;
  height: 14px;
  fill: currentColor;
}
.event .title { font-weight: 650; margin-bottom: 6px; }
.event .text { font-size: 13px; line-height: 1.4; color: #333; }

@media (max-width: 980px) {
  .layout { grid-template-columns: 1fr; }
  .day {
    aspect-ratio: auto;
    min-height: 72px;
  }
  .day .num { font-size: 36px; }
  .day .marks {
    grid-template-columns: repeat(auto-fill, 18px);
    grid-auto-rows: 18px;
    gap: 3px;
  }
  .day .marks span {
    width: 18px;
    height: 18px;
    font-size: 10px;
  }
}
