:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --text: #1c1f26;
  --muted: #6b7280;
  --line: #e3e6ec;
  --accent: #2481cc;
  --accent-text: #ffffff;
  --today: #eaf4fd;
  --danger: #d33;
  --card: #ffffff;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
  --hour: 48px;
  --axis: 52px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --panel: #1c1f26;
    --text: #e8eaee;
    --muted: #9aa1ad;
    --line: #2d323c;
    --accent: #4aa3ee;
    --accent-text: #0b1a28;
    --today: #1a2733;
    --danger: #ff6b6b;
    --card: #262b34;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font: 14px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.hidden, [hidden] { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; margin: 0; }

h1, h2 { margin: 0; }

/* ---------- кнопки / поля ---------- */

.btn {
  font: inherit;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn:focus-visible, .block:focus-visible, .card:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.icon { width: 32px; padding: 6px 0; font-size: 16px; }
.btn.small { padding: 2px 8px; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.field > span { font-size: 12px; color: var(--muted); }
input[type="text"], input[type="number"], input[type="date"], input[type="time"], select {
  font: inherit;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
  width: 100%;
}
.check { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }

/* ---------- вход ---------- */

.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%;
  max-width: 340px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 24px;
  box-shadow: var(--shadow);
}
.login-card h1 { margin-bottom: 8px; }
.login-card .btn { width: 100%; }

/* ---------- каркас ---------- */

.app { height: 100%; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 8px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.week-title { font-size: 16px; margin-left: 8px; }

.layout { flex: 1; min-height: 0; display: flex; }

.sidebar {
  width: var(--sidebar-width, 240px);
  max-width: min(600px, 55%);
  flex: none;
  overflow-y: auto;
  padding: 12px;
  background: var(--panel);
}
.sidebar-resize {
  flex: 0 0 8px;
  cursor: ew-resize;
  touch-action: none;
  background: linear-gradient(var(--line), var(--line)) center / 1px 100% no-repeat var(--panel);
}
.sidebar-resize:hover, .sidebar-resize:focus-visible, .sidebar-resizing .sidebar-resize {
  background-image: linear-gradient(var(--accent), var(--accent));
}
.sidebar-resizing { user-select: none; }
.side-head { display: flex; justify-content: space-between; align-items: center; margin-top: 6px; }
.side-head h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.hint { font-size: 12px; color: var(--muted); margin: 4px 0 8px; }
.side-list { list-style: none; margin: 0 0 16px; padding: 0; display: flex; flex-direction: column; gap: 6px; min-height: 8px; }

/* ---------- доска: заголовки, «без времени», сетка времени ---------- */

.board {
  flex: 1;
  min-width: 0;
  overflow: auto;
  position: relative;
  display: grid;
  grid-template-columns: var(--axis) repeat(7, minmax(120px, 1fr));
  grid-template-rows: auto auto auto;
  align-content: start;
}

.corner, .day-head, .allday-label, .allday-col {
  position: sticky;
  background: var(--panel);
  z-index: 3;
}
.corner, .day-head { top: 0; border-bottom: 1px solid var(--line); }
.day-head { padding: 6px 8px; border-left: 1px solid var(--line); }
.day-head .dow { font-weight: 600; }
.day-head .sum { font-size: 12px; color: var(--muted); }
.day-head.today { background: var(--today); }
.day-head.today .dow { color: var(--accent); }

.allday-label, .allday-col { top: var(--head-h, 48px); border-bottom: 2px solid var(--line); }
.allday-label { font-size: 11px; color: var(--muted); padding: 6px 4px; text-align: right; }
.allday-col { border-left: 1px solid var(--line); padding: 4px; min-height: 40px; display: flex; flex-direction: column; gap: 4px; }
.allday-col.today { background: var(--today); }
.allday-col.drop-target, .day-col.drop-target { box-shadow: inset 0 0 0 2px var(--accent); }

.axis { position: relative; height: calc(var(--hour) * 24); }
.axis .tick {
  position: absolute;
  right: 6px;
  transform: translateY(-50%);
  font-size: 11px;
  color: var(--muted);
}

.day-col {
  position: relative;
  height: calc(var(--hour) * 24);
  border-left: 1px solid var(--line);
  background-image: repeating-linear-gradient(to bottom, var(--line) 0, var(--line) 1px, transparent 1px, transparent var(--hour));
}
.day-col.today { background-color: var(--today); }

.now-line { position: absolute; left: 0; right: 0; height: 2px; background: var(--danger); z-index: 2; pointer-events: none; }
.now-line::before { content: ""; position: absolute; left: -4px; top: -3px; width: 8px; height: 8px; border-radius: 50%; background: var(--danger); }

/* ---------- карточки и блоки задач ---------- */

.card {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  background: var(--card);
  border: 1px solid var(--line);
  border-left: 4px solid var(--cat, #9ca3af);
  border-radius: 6px;
  box-shadow: var(--shadow);
  cursor: grab;
  list-style: none;
  user-select: none;
}
.card .title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .meta { font-size: 11px; color: var(--muted); flex: none; }
.card.done .title, .block.done .b-title { text-decoration: line-through; opacity: 0.55; }

.block {
  position: absolute;
  padding: 2px 6px;
  overflow: hidden;
  background: var(--card);
  border: 1px solid var(--line);
  border-left: 4px solid var(--cat, #9ca3af);
  border-radius: 5px;
  box-shadow: var(--shadow);
  font-size: 12px;
  cursor: grab;
  touch-action: none;
  user-select: none;
  z-index: 1;
}
.block .b-time { color: var(--muted); font-size: 11px; }
.board.read-only .block { cursor: pointer; touch-action: auto; }
.board.read-only .block.busy { cursor: default; border-left-color: var(--muted); }
.app.viewing-partner :is(#btn-new, #btn-references, #task-sidebar, #sidebar-resize) { display: none; }
.block .b-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.block.dragging { z-index: 20; opacity: 0.85; cursor: grabbing; box-shadow: 0 6px 16px rgba(0, 0, 0, 0.3); }
.resize-handle { position: absolute; left: 0; right: 0; bottom: 0; height: 7px; cursor: ns-resize; }
.resize-handle::after { content: ""; position: absolute; left: 40%; right: 40%; bottom: 2px; height: 2px; border-radius: 1px; background: var(--muted); opacity: 0.5; }

.badge { font-size: 11px; color: var(--accent); }
.drag-tip {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  padding: 3px 8px;
  border-radius: 4px;
  background: var(--text);
  color: var(--bg);
  font-size: 12px;
}

.sortable-ghost { opacity: 0.4; }
.sortable-fallback { z-index: 60; opacity: 0.9; }

/* ---------- попап ---------- */

.dialog {
  width: min(520px, calc(100vw - 24px));
  max-height: calc(100vh - 24px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
.dialog form { padding: 16px 20px; display: flex; flex-direction: column; }
.dialog h2 { font-size: 16px; margin-bottom: 12px; }
.dialog .actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.dialog .actions .spacer { flex: 1; }
.reference-list { display: grid; gap: 4px; margin: 8px 0; padding: 0; list-style: none; }
.reference-list button { width: 100%; text-align: start; overflow-wrap: anywhere; }
.team-list { display: grid; gap: 12px; padding: 0; margin: 8px 0; list-style: none; }
.shared-task-details { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; }
.shared-task-details dt { color: var(--muted); }
.shared-task-details dd { margin: 0; overflow-wrap: anywhere; }
.shift-pins { display: grid; gap: 6px; max-height: 35vh; overflow: auto; }
.scope { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; margin: 0 0 10px; }
.scope legend { font-size: 12px; color: var(--muted); padding: 0 4px; }
.scope label { display: flex; gap: 8px; align-items: center; padding: 2px 0; }
.weekday-picker { display: flex; gap: 4px; margin-bottom: 10px; flex-wrap: wrap; }
.weekday-picker label {
  display: grid;
  place-items: center;
  width: 36px;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
}
.weekday-picker input { position: absolute; opacity: 0; pointer-events: none; }
.weekday-picker label:has(input:checked) { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.weekday-picker label:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 1px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  max-width: 90vw;
  padding: 8px 14px;
  border-radius: 8px;
  background: var(--text);
  color: var(--bg);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 100;
}
.toast.show { opacity: 1; }
.toast.error { background: var(--danger); color: #fff; }

/* ---------- узкие экраны ---------- */

@media (max-width: 900px) {
  .layout { flex-direction: column; }
  .sidebar { width: auto; max-width: none; max-height: 34vh; border-bottom: 1px solid var(--line); }
  .sidebar-resize { display: none; }
  .board { grid-template-columns: var(--axis) repeat(7, minmax(140px, 1fr)); }
  .topbar { padding: 8px; }
}
