:root {
  --bg: #f4f6fa;
  --panel: #ffffff;
  --panel-alt: #fafbfc;
  --border: #e3e7ee;
  --border-strong: #a8b0bd;
  --text: #1f2937;
  --muted: #6b7280;
  --accent: #2266ff;
  --accent-dark: #1a4ed8;
  --danger: #e74c3c;
  --shadow: 0 1px 2px rgba(20,30,60,.06), 0 4px 16px rgba(20,30,60,.06);
  --radius: 10px;
  --weekend: #f5f7fb;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: grid;
  grid-template-columns: 280px 1fr;
  grid-template-rows: 60px 1fr;
  grid-template-areas:
    "top top"
    "aside main";
}

.topbar {
  grid-area: top;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 18px;
  gap: 18px;
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 230px; }
.brand h1 { font-size: 16px; margin: 0; font-weight: 600; }
.role-badge {
  font-size: 10px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 10px;
  background: #e2e8f0;
  color: #475569;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.role-badge:empty { display: none; }
.role-badge.admin {
  background: linear-gradient(135deg, #fde68a, #fbbf24);
  color: #78350f;
}
.role-badge.unknown {
  background: #fee2e2;
  color: #991b1b;
}
.role-badge.unknown:hover { background: #fecaca; }
.logo {
  background: linear-gradient(135deg, #2266ff, #5588ff);
  color: white; font-weight: 700; font-size: 11px;
  padding: 5px 8px; border-radius: 6px; letter-spacing: .5px;
}

.view-switch { display: flex; gap: 4px; background: #eef1f7; border-radius: 8px; padding: 3px; }
.view-btn {
  border: 0; background: transparent; padding: 6px 14px;
  border-radius: 6px; cursor: pointer; font-size: 13px; color: var(--muted);
}
.view-btn.active { background: var(--panel); color: var(--text); box-shadow: var(--shadow); }

.period-nav { display: flex; align-items: center; gap: 6px; }
.icon-btn {
  border: 1px solid var(--border); background: var(--panel);
  border-radius: 6px; cursor: pointer; padding: 6px 10px; font-size: 14px;
}
.icon-btn.pill { padding: 6px 12px; font-weight: 500; }
.icon-btn:hover { background: #f5f7fb; }
.period-label { margin-left: 8px; color: var(--muted); font-weight: 500; }
.year-select {
  margin-left: 8px;
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
}
.year-select:hover { background: #f5f7fb; }

.actions { margin-left: auto; display: flex; gap: 8px; }
button.primary {
  background: var(--accent); color: white; border: 0;
  padding: 8px 14px; border-radius: 6px; cursor: pointer; font-weight: 500;
}
button.primary:hover { background: var(--accent-dark); }
button.ghost {
  background: transparent; border: 1px solid var(--border);
  padding: 8px 12px; border-radius: 6px; cursor: pointer;
}
button.danger {
  background: var(--danger); color: white; border: 0;
  padding: 8px 14px; border-radius: 6px; cursor: pointer;
}

.legend {
  grid-area: aside;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 18px;
  overflow-y: auto;
}
.legend h3 { margin: 0 0 12px; font-size: 12px; text-transform: uppercase; color: var(--muted); letter-spacing: .5px; }
.legend ul { list-style: none; padding: 0; margin: 0 0 18px; display: grid; gap: 4px; }
.legend li.legend-item {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px;
  padding: 4px 6px;
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
  transition: background 0.12s, opacity 0.12s;
}
.legend li.legend-item:hover { background: #f0f4fb; }
.legend li.legend-item.off { opacity: 0.45; }
.legend li.legend-item.off .swatch { filter: grayscale(1); }
.legend .legend-cb {
  width: 14px; height: 14px;
  margin: 0;
  cursor: pointer;
  accent-color: var(--accent);
  flex-shrink: 0;
}
.legend .legend-label { flex: 1; line-height: 1.2; }
.swatch { width: 14px; height: 14px; border-radius: 4px; flex-shrink: 0; }
.legend-footer { border-top: 1px solid var(--border); padding-top: 14px; display: grid; gap: 10px; }
.user-count { font-size: 12px; color: var(--muted); padding-bottom: 4px; }
.filter-row { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
.filter-row select, .filter-row input {
  padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--panel); font-size: 13px; color: var(--text);
}

.board {
  grid-area: main;
  position: relative;
  overflow: auto;
}
.loading, .error {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  background: var(--panel); padding: 8px 14px; border-radius: 6px;
  box-shadow: var(--shadow); z-index: 4; font-size: 13px;
}
.error { background: #fdecec; color: var(--danger); }
.hidden { display: none !important; }

.auth-banner {
  position: fixed;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  background: #fff8e1;
  border: 1px solid #f0c040;
  color: #6b4f00;
  padding: 14px 18px;
  border-radius: 10px;
  max-width: 640px;
  box-shadow: 0 8px 32px rgba(20,30,60,.15);
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 13px;
  line-height: 1.4;
}
.auth-banner em { font-style: normal; font-weight: 600; color: #4a3500; }
.auth-banner button { white-space: nowrap; }

.grid {
  display: grid;
  grid-template-rows: 48px;
  grid-auto-rows: 44px;
  background: var(--panel);
  margin: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: clip;
  font-size: 13px;
  width: calc(100% - 24px);
  min-width: max-content;
  box-sizing: border-box;
}
.grid.year-view {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  grid-template-rows: none;
  grid-auto-rows: min-content;
  gap: 14px;
  padding: 14px;
  margin: 12px;
  background: transparent;
  border: 0;
  border-radius: 0;
  overflow: visible;
  min-width: 0;
  width: auto;
}
.grid-header, .grid-row {
  display: grid;
  grid-template-columns: var(--cols);
}
.grid-header {
  position: sticky; top: 0; z-index: 3;
  background: #fafbfd;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  color: var(--muted);
}
.grid-header > div, .grid-row > .day-cell {
  border-left: 1px solid var(--border-strong);
  position: relative;
  box-sizing: border-box;
  min-height: 44px;
}
.grid-header > div.last-day,
.grid-row > .day-cell.last-day {
  border-right: 1px solid var(--border-strong);
}
.grid-header > div {
  padding: 6px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  line-height: 1.2;
  gap: 2px;
}
.day-num { font-size: 13px; font-weight: 600; color: var(--text); }
.day-wd  { font-size: 10px; font-weight: 400; color: var(--muted); text-transform: lowercase; }
.grid-header > div.today .day-num { color: var(--accent); }
.grid-header > div:last-child, .grid-row > div:last-child { border-right: 0; }
.grid-row {
  border-bottom: 1px solid var(--border-strong);
  min-height: 44px;
  align-items: stretch;
  background: var(--panel);
  position: relative;
}
.grid-row:nth-child(even) { background: var(--panel-alt); }
.grid-row:last-child { border-bottom: 0; }
.grid-row:hover { background: #f5f8ff; }
.grid-row > div.user-cell {
  position: sticky; left: 0; z-index: 2;
  background: inherit;
  display: flex; align-items: center; gap: 10px;
  font-weight: 500;
  padding: 6px 12px;
  min-height: 44px;
  box-sizing: border-box;
}
.grid-header > div.user-cell {
  position: sticky; left: 0; z-index: 4;
  background: #fafbfd;
  justify-content: flex-start;
  padding: 8px 12px;
  border-left: 0;
}
.user-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: linear-gradient(135deg, #5b8def, #2266ff);
  color: white; display: grid; place-items: center;
  font-size: 11px; font-weight: 600; flex-shrink: 0;
}
.user-meta { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.user-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 200px; }
.user-pos { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 200px; }

.day-cell {
  cursor: pointer;
  background: transparent;
  transition: background-color 0.1s;
}
.day-cell.weekend { background: rgba(120, 140, 170, 0.08); }
.day-cell.today {
  background: rgba(34, 102, 255, 0.10);
  box-shadow: inset 0 -3px 0 var(--accent);
}
.day-cell:hover { background: rgba(34, 102, 255, 0.06); }

.absence-bar {
  position: absolute;
  top: 9px;
  height: 26px;
  border-radius: 4px;
  color: white;
  display: flex;
  align-items: center;
  padding: 0 10px;
  font-size: 11px;
  font-weight: 500;
  letter-spacing: -0.1px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: pointer;
  z-index: 1;
  box-shadow: 0 1px 3px rgba(0,0,0,.14);
  pointer-events: auto;
  min-width: 0;
  transition: filter 0.12s, box-shadow 0.12s;
}
.absence-bar:hover { filter: brightness(1.08); box-shadow: 0 2px 6px rgba(0,0,0,.18); }
.absence-bar.narrow {
  padding: 0 2px;
  justify-content: center;
  font-size: 10px;
  letter-spacing: -0.3px;
}
.absence-bar.compact {
  padding: 0 5px;
  font-size: 10px;
  justify-content: center;
}

/* Read-only — записи из системного графика Битрикс24:
   диагональные полосы + вертикальные разделители дней */
.absence-bar.read-only {
  cursor: help;
  background-image: repeating-linear-gradient(45deg, rgba(255,255,255,.16) 0 6px, transparent 6px 12px);
}

/* ── Year view: 12-month calendar of per-employee absence markers ────────── */
.month-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 10px 12px 12px;
}
.month-card-h { margin: 0 0 8px; font-size: 14px; font-weight: 600; text-align: center; color: var(--text); }
.mini-cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.mc-wd { font-size: 10px; font-weight: 600; color: var(--muted); text-align: center; padding: 2px 0; }
.mc-wd.we { color: #b0563f; }
.mc-day {
  position: relative;
  min-height: 42px;
  border: 1px solid #eef1f5;
  border-radius: 5px;
  padding: 2px 3px;
  background: var(--panel);
  overflow: hidden;
}
.mc-day:not(.empty) { cursor: pointer; }
.mc-day:not(.empty):hover { border-color: var(--border-strong); }
.mc-day.empty { border: 0; background: transparent; }
.mc-day.weekend { background: var(--weekend); }
.mc-day.today { box-shadow: inset 0 0 0 2px var(--accent); }
.mc-dnum { font-size: 10px; line-height: 1; color: var(--muted); }
.mc-day.today .mc-dnum { color: var(--accent); font-weight: 700; }
/* Split bars: left 30% = absence-type colour, right 70% = employee colour.
   The day cell grows in height to fit every bar (no cap). */
.mc-bars { display: flex; flex-direction: column; gap: 2px; margin-top: 3px; }
.mc-bar {
  height: 16px;
  border-radius: 3px;
  cursor: pointer;
}
.mc-bar:hover { outline: 2px solid var(--border-strong); outline-offset: -1px; }
.mc-bar.ro { opacity: .7; cursor: help; }
.year-hint {
  grid-column: 1 / -1;
  background: #eef4ff;
  border: 1px solid #c7dafe;
  color: #1e40af;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 13px;
}

/* Employee picker — left sidebar, year view only */
.emp-picker { margin-top: 18px; border-top: 1px solid var(--border); padding-top: 14px; }
.emp-picker-actions { display: flex; gap: 12px; margin-bottom: 6px; }
.link-btn { background: none; border: 0; padding: 0; color: var(--accent); font-size: 12px; cursor: pointer; }
.link-btn:hover { text-decoration: underline; }
.emp-hint { margin: 0 0 8px; font-size: 11px; color: var(--muted); line-height: 1.3; }
#emp-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 2px; }
.emp-item { display: flex; align-items: center; gap: 8px; font-size: 13px; padding: 4px 6px; border-radius: 6px; cursor: pointer; user-select: none; }
.emp-item:hover { background: #f0f4fb; }
.emp-cb { width: 14px; height: 14px; margin: 0; accent-color: var(--accent); cursor: pointer; flex-shrink: 0; }
.emp-swatch { width: 13px; height: 13px; border-radius: 50%; border: 2px solid var(--border-strong); flex-shrink: 0; }
.emp-item.on .emp-name { font-weight: 600; }
.emp-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* Modal */
.modal {
  position: fixed; inset: 0; background: rgba(20,30,60,.4);
  display: grid; place-items: center; z-index: 100;
}
.modal-card {
  background: var(--panel); border-radius: 12px; min-width: 420px; max-width: 90vw;
  box-shadow: 0 12px 60px rgba(20,30,60,.25);
  display: flex; flex-direction: column; max-height: 90vh;
}
.modal-card header {
  display: flex; align-items: center; padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-card header h2 { margin: 0; font-size: 17px; flex: 1; }
.close { background: transparent; border: 0; font-size: 22px; cursor: pointer; color: var(--muted); }
.modal-body { padding: 18px 20px; display: grid; gap: 12px; overflow-y: auto; }
.modal-body label { display: grid; gap: 5px; font-size: 13px; color: var(--muted); }
.modal-body input, .modal-body select, .modal-body textarea {
  border: 1px solid var(--border); border-radius: 6px; padding: 8px 10px;
  font-size: 14px; color: var(--text); font-family: inherit;
}
.modal-body textarea { resize: vertical; }
.modal-body .row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.modal-card footer {
  display: flex; align-items: center; padding: 14px 20px;
  border-top: 1px solid var(--border); gap: 10px;
}
.modal-card footer .spacer { flex: 1; }

/* Settings (permissions) modal */
.modal-card.settings-card {
  min-width: 720px;
  max-width: 95vw;
}
.settings-hint {
  font-size: 12px;
  color: var(--muted);
  padding: 8px 12px;
  background: #f5f8fc;
  border-radius: 6px;
  margin-bottom: 6px;
}
.perms-table-wrap {
  max-height: 60vh;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.perms-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.perms-table thead th {
  position: sticky; top: 0;
  background: #f5f7fb;
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  color: var(--muted);
  padding: 10px 8px;
  text-align: center;
  border-bottom: 1px solid var(--border-strong);
}
.perms-table thead th.th-name { text-align: left; }
.perms-table tbody td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: center;
}
.perms-table tbody tr:last-child td { border-bottom: 0; }
.perms-table tbody tr:hover { background: #fafbfd; }
.perms-table .td-name {
  text-align: left;
  display: flex; align-items: center; gap: 10px;
  min-width: 220px;
}
.perms-table .td-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: linear-gradient(135deg, #5b8def, #2266ff);
  color: white; display: grid; place-items: center;
  font-size: 11px; font-weight: 600; flex-shrink: 0;
}
.perms-table .td-pos { font-size: 11px; color: var(--muted); }
.perms-table .td-perm input { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }
.settings-status { font-size: 12px; color: var(--muted); }
