:root {
  --bg: #f4f2ee;
  --surface: #ffffff;
  --surface-2: #f8f7f4;
  --ink: #1d2321;
  --ink-2: #57605c;
  --line: #e2ded6;
  --line-strong: #cfc9bd;
  --accent: #1f6f5c;
  --accent-ink: #ffffff;
  --accent-soft: #e3f0ec;
  --danger: #b4382f;
  --warn: #9a6a00;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(29, 35, 33, .06), 0 2px 8px rgba(29, 35, 33, .04);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151a18;
    --surface: #1d2321;
    --surface-2: #232a28;
    --ink: #e9ece9;
    --ink-2: #a3aca8;
    --line: #2f3734;
    --line-strong: #414b47;
    --accent: #4fb397;
    --accent-ink: #0f1513;
    --accent-soft: #1f3530;
    --danger: #ef7d73;
    --warn: #e0b04a;
    --shadow: none;
  }
}

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

body { margin: 0; background: var(--bg); font-size: 14px; line-height: 1.4; }
h2 { font-size: 15px; margin: 0; font-weight: 650; }
h3 { font-size: 14px; margin: 0 0 8px; }
.muted { color: var(--ink-2); }
.small-text { font-size: 12.5px; }
.error { color: var(--danger); margin: 6px 0 0; }
.spacer { flex: 1; }

input, button, select { font: inherit; color: inherit; }
select {
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  padding: 6px 8px;
  min-width: 0;
}
select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
input:disabled { color: var(--ink); opacity: 1; background: transparent; }
input {
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  padding: 7px 9px;
  min-width: 0;
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent); }
label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--ink-2); }
label input { color: var(--ink); font-size: 14px; }

.btn {
  border: 1px solid var(--line-strong);
  background: var(--surface);
  border-radius: 7px;
  padding: 7px 12px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: var(--ink-2); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--ink-2); }
.btn.ghost:hover { background: var(--surface-2); color: var(--ink); }
.btn.small { padding: 4px 9px; font-size: 13px; }
.btn.wide { width: 100%; padding: 10px; }
.btn.danger { color: var(--danger); }

.icon-btn {
  border: 0; background: transparent; cursor: pointer; color: var(--ink-2);
  width: 28px; height: 28px; border-radius: 6px; font-size: 15px; line-height: 1;
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.icon-btn.danger:hover { color: var(--danger); }
.icon-btn:disabled { opacity: .3; cursor: default; background: transparent; }

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; letter-spacing: -.01em; }
.brand-mark { width: 20px; height: 20px; border-radius: 5px; background: var(--accent); position: relative; }
.brand-mark::before, .brand-mark::after {
  content: ""; position: absolute; left: 5px; height: 2px; border-radius: 1px; background: var(--accent-ink);
}
.brand-mark::before { top: 6px; width: 10px; }
.brand-mark::after { top: 11px; width: 7px; }
.brand-large { font-size: 22px; }

/* Вход */
.login-view { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 340px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; padding: 28px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px;
}
.login-card p { margin: -8px 0 4px; }

/* Каркас */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 6px;
  padding: 10px 18px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.user-name { color: var(--ink-2); margin-right: 6px; }
.layout { display: grid; grid-template-columns: 260px minmax(0, 1fr); min-height: calc(100vh - 53px); }
.sidebar { border-right: 1px solid var(--line); padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; }
.search { width: 100%; }
.main { padding: 16px 20px 24px; display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.only-narrow { display: none; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin-bottom: 10px; }

/* Список регламентов */
.schedule-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; overflow-y: auto; }
.schedule-item {
  display: flex; align-items: center; gap: 4px; border-radius: 8px; padding: 7px 6px 7px 10px; cursor: pointer;
}
.schedule-item:hover { background: var(--surface); }
.schedule-item.active { background: var(--accent-soft); }
.schedule-item-text { flex: 1; min-width: 0; }
.schedule-item-name { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.schedule-item-meta { font-size: 12px; color: var(--ink-2); font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.schedule-item .icon-btn { visibility: hidden; }
.schedule-item:hover .icon-btn, .schedule-item.active .icon-btn { visibility: visible; }

/* Мероприятие */
.event-card { padding: 14px 16px; }
.event-grid { display: grid; grid-template-columns: minmax(0, 1fr) 160px 110px; gap: 12px; }
.event-name input { font-size: 17px; font-weight: 600; }
.save-state { margin-top: 8px; font-size: 12.5px; color: var(--ink-2); min-height: 17px; }
.save-state.dirty { color: var(--warn); }

/* Рабочая область */
.workspace { display: grid; grid-template-columns: minmax(240px, 320px) minmax(0, 1fr); gap: 14px; align-items: start; }
.library, .schedule { padding: 14px 16px; }

.library-form { display: grid; grid-template-columns: minmax(0, 1fr) 64px auto; gap: 6px; margin-bottom: 10px; }
.library-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; max-height: 52vh; overflow-y: auto; }
.library-item {
  display: flex; align-items: center; gap: 6px; padding: 5px 4px 5px 10px; border: 1px solid var(--line);
  border-radius: 8px; background: var(--surface-2); cursor: grab;
}
.library-item:hover { border-color: var(--line-strong); }
.library-item.dragging { opacity: .4; }
.library-item-name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.library-item-duration { color: var(--ink-2); font-size: 12.5px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.library-item .add { color: var(--accent); font-weight: 700; font-size: 17px; }
.hint { color: var(--ink-2); font-size: 12px; margin: 10px 0 0; }

.total { font-weight: 600; font-variant-numeric: tabular-nums; }

.table-wrap { overflow-x: auto; }
.schedule-table { width: 100%; border-collapse: collapse; }
.schedule-table th {
  text-align: left; font-size: 12px; font-weight: 600; color: var(--ink-2);
  padding: 0 6px 6px; border-bottom: 1px solid var(--line);
}
.schedule-table td { padding: 3px 4px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.schedule-table tr:last-child td { border-bottom: 0; }
.schedule-table input { width: 100%; border-color: transparent; background: transparent; padding: 6px 7px; }
.schedule-table input:hover { border-color: var(--line); }
.schedule-table input:focus { background: var(--surface); }
.col-handle { width: 22px; }
.col-time { width: 64px; }
.col-duration { width: 74px; }
.col-responsible { width: 28%; }
.col-actions { width: 92px; }
.time-cell { font-variant-numeric: tabular-nums; font-weight: 600; padding-left: 6px !important; white-space: nowrap; }
.handle { cursor: grab; color: var(--line-strong); text-align: center; user-select: none; }
.schedule-table tr:hover .handle { color: var(--ink-2); }
.actions-cell { white-space: nowrap; text-align: right; }
.schedule-table tr.dragging { opacity: .4; }
.schedule-table tr.drop-before td { box-shadow: inset 0 2px 0 var(--accent); }

.drop-zone {
  margin-top: 8px; padding: 14px; border: 1.5px dashed var(--line-strong); border-radius: 8px;
  color: var(--ink-2); text-align: center; font-size: 13px;
}
.drop-zone.compact { padding: 8px; display: none; }
body.dragging-block .drop-zone.compact { display: block; }
.drop-zone.over { border-color: var(--accent); background: var(--accent-soft); color: var(--ink); }

.actions {
  position: sticky; bottom: 0; display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  padding: 12px 0; background: linear-gradient(to top, var(--bg) 75%, transparent);
}

/* Диалоги */
.dialog {
  border: 1px solid var(--line); border-radius: 14px; padding: 20px; background: var(--surface); color: var(--ink);
  width: min(420px, calc(100vw - 32px)); box-shadow: 0 10px 40px rgba(0, 0, 0, .2);
}
.dialog::backdrop { background: rgba(20, 24, 22, .4); }
.dialog h2 { margin-bottom: 12px; }
.wide-dialog { width: min(720px, calc(100vw - 32px)); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.dialog-head h2 { margin: 0; }
.dialog-buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
#prompt-fields { display: flex; flex-direction: column; gap: 10px; }

.users-table { width: 100%; border-collapse: collapse; margin-bottom: 16px; }
.users-table th { text-align: left; font-size: 12px; color: var(--ink-2); font-weight: 600; padding: 6px; border-bottom: 1px solid var(--line); }
.users-table td { padding: 6px; border-bottom: 1px solid var(--line); }
.users-table td:last-child { text-align: right; white-space: nowrap; }
.user-form { border-top: 1px solid var(--line); padding-top: 14px; }
.user-form-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.user-form-row input[name="username"], .user-form-row input[name="password"] { flex: 1 1 160px; }
.check { flex-direction: row; align-items: center; gap: 6px; color: var(--ink); font-size: 13.5px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--bg); padding: 9px 16px; border-radius: 8px; transition: .2s; z-index: 50;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); color: #fff; }

/* Узкие экраны */
/* До 1200px таблица расписания рядом с блоками слишком узкая - ставим их друг под другом */
@media (max-width: 1200px) {
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .library-list { max-height: 30vh; }
}
@media (max-width: 760px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .only-narrow { display: inline-block; }
  .sidebar { display: none; border-right: 0; border-bottom: 1px solid var(--line); }
  .sidebar.open { display: flex; }
  .main { padding: 12px 16px 16px; }
  .event-grid { grid-template-columns: 1fr 1fr; }
  .event-name { grid-column: 1 / -1; }
  .topbar { padding: 8px 12px; flex-wrap: wrap; }
  .user-name { display: none; }
  .actions { position: static; background: none; padding-top: 4px; }
  .col-responsible { width: auto; }
  .schedule-table, .schedule-table tbody, .schedule-table tr, .schedule-table td { display: block; }
  .schedule-table thead { display: none; }
  .schedule-table tr {
    display: grid; grid-template-columns: 52px minmax(0, 1fr) 92px; gap: 2px 4px;
    padding: 6px 0; border-bottom: 1px solid var(--line);
  }
  .schedule-table td { border: 0; padding: 0; }
  .schedule-table td.handle { display: none; }
  .time-cell { align-self: center; }
  .schedule-table td.resp-cell { grid-column: 2 / 3; }
  .schedule-table td.actions-cell { grid-column: 3 / 4; grid-row: 2; }
}
.offscreen { position: fixed; left: -9999px; top: 0; opacity: 0; }

/* Загрузка файлов */
.import-button { width: 100%; }
.link-btn {
  border: 0; background: none; padding: 0; cursor: pointer; color: var(--accent); font-size: 12.5px;
}
.link-btn:hover { text-decoration: underline; }
.card-head .link-btn + .link-btn { margin-left: 4px; }
#confirm-text { white-space: pre-line; }
.file-drop {
  position: fixed; inset: 12px; z-index: 60; display: grid; place-items: center; text-align: center; padding: 16px;
  border: 2px dashed var(--accent); border-radius: 16px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  font-size: 18px; font-weight: 600; color: var(--accent); pointer-events: none;
}

/* Площадки и права */
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.location-picker { flex-direction: row; margin-left: 10px; }
.location-picker select { font-weight: 600; color: var(--ink); max-width: 220px; }
.role-badge {
  font-size: 12px; color: var(--warn); border: 1px solid currentColor; border-radius: 999px; padding: 2px 9px;
  white-space: nowrap;
}
body.readonly .edit-only { display: none !important; }
body.readonly .library-item { cursor: default; }
body.readonly .handle { visibility: hidden; }
body.readonly .schedule-table input:hover { border-color: transparent; }
.no-location { display: grid; place-items: center; padding: 60px 16px; }
.no-location .card { padding: 24px 28px; max-width: 420px; }
.no-location p { margin: 8px 0 0; }

.status-row { display: flex; flex-wrap: wrap; gap: 4px 16px; align-items: baseline; }
.presence { margin-top: 8px; font-size: 12.5px; color: var(--accent); }
.presence::before { content: "● "; }
.presence.editing { color: var(--warn); }
.remote-change {
  margin-top: 8px; padding: 8px 10px; border-radius: 8px; font-size: 13px;
  background: color-mix(in srgb, var(--warn) 12%, transparent); color: var(--ink);
}
.remote-change .link-btn { font-size: 13px; }

.locations-list { display: flex; flex-direction: column; gap: 10px; margin: 12px 0 16px; max-height: 60vh; overflow-y: auto; }
.location-card { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; background: var(--surface-2); }
.location-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.location-head h3 { margin: 0; font-size: 15px; }
.member-list { list-style: none; margin: 8px 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.member-list li { display: flex; align-items: center; gap: 8px; }
.member-name { flex: 1; min-width: 0; }
.member-add { display: flex; flex-wrap: wrap; gap: 6px; }
.member-add select:first-child { flex: 1 1 200px; }
.places-cell { font-size: 13px; color: var(--ink-2); }

.history { display: grid; grid-template-columns: 200px minmax(0, 1fr); gap: 14px; margin-top: 8px; }
.history-list { list-style: none; margin: 0; padding: 0; max-height: 60vh; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.history-item { padding: 7px 10px; border-radius: 8px; cursor: pointer; }
.history-item:hover { background: var(--surface-2); }
.history-item.active { background: var(--accent-soft); }
.history-item-name { font-weight: 550; font-variant-numeric: tabular-nums; }
.history-detail { min-width: 0; max-height: 60vh; overflow-y: auto; }
.history-detail-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; margin-bottom: 10px; }
.history-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.history-table td { padding: 5px 6px; border-bottom: 1px solid var(--line); }
@media (max-width: 640px) {
  .history { grid-template-columns: minmax(0, 1fr); }
  .history-list { max-height: 25vh; }
}
@media (max-width: 760px) {
  .location-picker { margin-left: 0; }
}
