:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --line: #dde3e9;
  --text: #1c2733;
  --muted: #66788a;
  --accent: #0e7c6b;
  --accent-dark: #0a5f52;
  --accent-light: #e3f2ef;
  --red: #d64545;
  --red-bg: #fdeaea;
  --amber: #b98a00;
  --amber-bg: #fdf6e0;
  --shadow: 0 2px 10px rgba(20, 40, 60, .08);
  --radius: 10px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.spacer { flex: 1; }
a { color: var(--accent); }

/* ---------- Кнопки, поля ---------- */
.btn {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 8px;
  padding: 7px 14px;
  font-size: 13.5px;
  cursor: pointer;
  white-space: nowrap;
  transition: background .12s, border-color .12s;
}
.btn:hover { background: #f0f4f3; border-color: #c3ccd4; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-dark); }
.btn.danger { color: var(--red); border-color: #eec3c3; }
.btn.danger:hover { background: var(--red-bg); }
.btn.small { padding: 4px 10px; font-size: 12.5px; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }

input, select, textarea {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 13.5px;
  font-family: inherit;
  color: var(--text);
  background: #fff;
  width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-light); border-color: var(--accent); }
textarea { resize: vertical; min-height: 60px; }
label { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 10px; }
label input, label select, label textarea { margin-top: 4px; }
.error { color: var(--red); background: var(--red-bg); border-radius: 8px; padding: 8px 12px; margin: 8px 0; font-size: 13px; }

/* ---------- Вход ---------- */
#view-login { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-box {
  background: var(--panel); border-radius: 14px; box-shadow: var(--shadow);
  padding: 36px; width: 380px;
}
.login-box h1 { font-size: 20px; margin-bottom: 4px; }
.login-box p { margin-bottom: 20px; }

/* ---------- Шапка ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px;
  background: var(--panel); border-bottom: 1px solid var(--line);
  padding: 10px 16px; position: sticky; top: 0; z-index: 50;
  flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 15px; cursor: pointer; margin-right: 6px; }
.topbar input[type="search"] { width: 300px; }
.topbar select { width: auto; }
.rates { font-size: 12px; color: var(--muted); white-space: nowrap; }
/* Фильтры с галочками */
.mf-wrap { position: relative; display: inline-block; }
.mf-panel {
  position: absolute; top: calc(100% + 4px); left: 0; z-index: 60;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: var(--shadow); padding: 10px 12px; min-width: 220px; max-height: 340px; overflow-y: auto;
}
.mf-item { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text); margin: 0 0 6px; white-space: nowrap; }
.userbox { font-size: 13px; color: var(--muted); }
.userbox a { margin-left: 6px; }

main { padding: 16px; max-width: 1500px; margin: 0 auto; }

/* ---------- Таблица клиентов ---------- */
.table-wrap { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); overflow-x: auto; }
table.clients { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.clients th {
  text-align: left; padding: 10px 12px; border-bottom: 2px solid var(--line);
  color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .3px;
  cursor: pointer; user-select: none; white-space: nowrap;
}
table.clients th:hover { color: var(--accent); }
table.clients td { padding: 9px 12px; border-bottom: 1px solid #edf1f4; white-space: nowrap; }
table.clients tbody tr { cursor: pointer; transition: background .1s; }
table.clients tbody tr:hover { background: #f5f9f8; }
tr.overdue td { background: var(--red-bg); }
tr.overdue:hover td { background: #fbdcdc !important; }
tr.overdue td:first-child { border-left: 4px solid var(--red); }
/* Непрочитанное письмо — желтая строка, пока письмо не откроют */
tr.unread-mail:not(.overdue) td { background: var(--amber-bg); }
tr.unread-mail:not(.overdue):hover td { background: #f8edc4 !important; }
tr.unread-mail:not(.overdue) td:first-child { border-left: 4px solid var(--amber); }
.new-mail-badge {
  display: inline-block; margin-left: 8px; padding: 2px 8px; border-radius: 12px;
  background: var(--amber); color: #fff; font-size: 11.5px; font-weight: 700; white-space: nowrap;
}
.status-pill {
  display: inline-block; padding: 3px 10px; border-radius: 20px;
  font-size: 12px; font-weight: 600; background: var(--accent-light); color: var(--accent-dark);
}
.status-pill.s-closed { background: #e8e8e8; color: #666; }
.status-pill.s-warn { background: var(--amber-bg); color: var(--amber); }
.status-pill.s-red { background: var(--red-bg); color: var(--red); }
.reminder-flag { color: var(--red); font-weight: 600; font-size: 12.5px; }

/* ---------- Карточка клиента ---------- */
/* Закрепленный блок: шапка карточки + почтовое окно (не прокручиваются) */
.card-sticky {
  position: sticky; top: 57px; z-index: 40;
  background: var(--bg);
  padding-bottom: 2px;
}
.card-header {
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px 18px; margin-bottom: 12px;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
}
.card-header .fio { font-size: 18px; font-weight: 700; }
.card-header .meta { color: var(--muted); font-size: 13px; }
.card-header .status-pill { font-size: 13.5px; padding: 5px 14px; }

.stage {
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow);
  margin-bottom: 14px; border-left: 5px solid var(--line);
  scroll-margin-top: 130px;
}
.stage.current { border-left-color: var(--accent); background: #f7fbfa; }
.stage.past { opacity: .85; }
.stage-head {
  display: flex; align-items: center; gap: 12px; padding: 12px 18px;
  border-bottom: 1px solid #eef2f5; cursor: pointer; user-select: none;
}
.stage-head h3 { font-size: 14.5px; letter-spacing: .4px; }
.stage.current .stage-head h3 { color: var(--accent-dark); }
.stage-head .chev { margin-left: auto; color: var(--muted); transition: transform .15s; }
.stage.collapsed .stage-body { display: none; }
.stage.collapsed .chev { transform: rotate(-90deg); }
.stage-body { padding: 14px 18px; }
.fields-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 8px 14px; }
.fields-grid label { margin-bottom: 2px; }
.stage-section { margin-top: 14px; }
.stage-section > .sec-title { font-size: 12px; text-transform: uppercase; color: var(--muted); letter-spacing: .4px; margin-bottom: 8px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

.upload-slot {
  display: flex; align-items: center; gap: 10px; padding: 7px 10px;
  border: 1px dashed var(--line); border-radius: 8px; margin-bottom: 6px;
  background: #fafcfc;
}
.upload-slot .slot-name { font-size: 13px; min-width: 220px; }
.upload-slot .files-list { display: flex; gap: 6px; flex-wrap: wrap; }
.file-chip {
  display: inline-flex; align-items: center; gap: 5px;
  background: var(--accent-light); color: var(--accent-dark);
  border-radius: 6px; padding: 3px 8px; font-size: 12.5px;
}
.file-chip a { text-decoration: none; color: inherit; }
.file-chip .del { cursor: pointer; color: var(--red); font-weight: 700; }

/* ---------- Почта клиента в карточке ---------- */
.mail-panel {
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow);
  margin-bottom: 14px; border-left: 5px solid #4a7fb5;
}
.mail-panel.collapsed .stage-body { display: none; }
.mail-panel.collapsed .chev { transform: rotate(-90deg); }
.mail-list { max-height: 220px; overflow-y: auto; }
.mail-item {
  display: flex; gap: 10px; align-items: baseline; padding: 6px 4px;
  border-bottom: 1px solid #f0f3f5; cursor: pointer; font-size: 13.5px;
}
.mail-item:hover { background: #f5f8fb; }
.mail-date { color: var(--muted); font-size: 12px; white-space: nowrap; }
.mail-att { color: var(--accent-dark); font-size: 12px; white-space: nowrap; }
.mail-item.unread { background: var(--amber-bg); border-radius: 6px; padding-left: 8px; padding-right: 8px; }
.mail-dot { color: var(--amber); width: 12px; font-size: 14px; }
.mail-read-mark { margin-left: auto; color: var(--muted); font-size: 11.5px; white-space: nowrap; }
.mail-unread-mark { margin-left: auto; color: var(--amber); font-weight: 700; font-size: 11.5px; white-space: nowrap; }
.mail-body-view {
  white-space: pre-wrap; font-family: inherit; font-size: 13.5px; background: #f7f9fa;
  border: 1px solid var(--line); border-radius: 8px; padding: 12px; max-height: 320px; overflow-y: auto;
}
.file-chip.draggable { cursor: grab; }
.file-chip.draggable:active { cursor: grabbing; }

/* ---------- Крупные карточки файлов с превью ---------- */
.file-card {
  display: inline-flex; flex-direction: column; align-items: center; gap: 4px;
  width: 96px; padding: 6px; border: 1px solid var(--line); border-radius: 8px;
  background: #fff; cursor: pointer; position: relative; vertical-align: top;
}
.file-card:hover { border-color: var(--accent); }
.file-thumb { width: 82px; height: 64px; object-fit: cover; border-radius: 5px; background: #eef2f4; }
.file-ext {
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: var(--accent-dark); font-size: 14px;
}
.file-card .file-name {
  font-size: 10.5px; line-height: 1.25; max-height: 27px; overflow: hidden;
  text-align: center; word-break: break-word; color: var(--text);
}
.file-card .del {
  position: absolute; top: -6px; right: -6px; background: var(--red); color: #fff;
  border-radius: 50%; width: 18px; height: 18px; line-height: 17px; text-align: center;
  font-size: 12px; cursor: pointer; display: none;
}
.file-card:hover .del { display: block; }
.upload-slot.drag-over { border-color: var(--accent); background: var(--accent-light); }
.report-box table.pays td { padding: 6px; }

/* ---------- Редактор документа ---------- */
.doc-editor {
  border: 1px solid var(--line); border-radius: 8px; background: #fff;
  padding: 24px 28px; margin: 10px 0; max-height: 55vh; overflow-y: auto;
  font-family: 'Times New Roman', serif; font-size: 14px; line-height: 1.45;
}
.doc-editor:focus { outline: 2px solid var(--accent-light); border-color: var(--accent); }
.doc-editor p { margin: 0 0 8px; }
.doc-editor table { border-collapse: collapse; }
.doc-editor td { border: 1px solid #ddd; padding: 4px 8px; }
.doc-editor img { max-width: 100%; }

.reminder-box { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; }
.reminder-box label { margin: 0; }
.reminder-item { display: flex; align-items: center; gap: 8px; font-size: 13px; padding: 4px 0; }
.reminder-item.due { color: var(--red); font-weight: 600; }
.reminder-item.done-item { color: var(--muted); text-decoration: line-through; }

/* ---------- Оплаты ---------- */
.pay-tables { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 14px; }
.pay-block h4 { font-size: 13px; margin-bottom: 6px; color: var(--muted); text-transform: uppercase; letter-spacing: .3px; }
table.pays { width: 100%; border-collapse: collapse; font-size: 13px; }
table.pays th { text-align: right; color: var(--muted); font-weight: 500; font-size: 11.5px; padding: 4px 6px; border-bottom: 1px solid var(--line); }
table.pays th:first-child, table.pays td:first-child { text-align: left; }
table.pays td { padding: 4px 6px; text-align: right; border-bottom: 1px solid #f0f3f5; }
table.pays tfoot td { font-weight: 700; border-top: 2px solid var(--line); }
.calc-summary { display: flex; gap: 18px; flex-wrap: wrap; margin-top: 12px; font-size: 14px; }
.calc-summary .item b { font-size: 16px; }
.calc-summary .debt-pos b { color: var(--red); }
.calc-summary .debt-neg b { color: var(--accent-dark); }

/* ---------- Модальные окна ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 30, 40, .45); z-index: 100;
  display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px;
  overflow-y: auto;
}
.modal {
  background: var(--panel); border-radius: 14px; box-shadow: 0 10px 40px rgba(0,0,0,.2);
  padding: 22px; width: 640px; max-width: 100%;
}
.modal.wide { width: 900px; }
.modal h2 { font-size: 17px; margin-bottom: 14px; }
.modal .modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.modal textarea.mail-body { min-height: 300px; font-family: inherit; font-size: 13.5px; }

/* ---------- Тосты ---------- */
#toast-root { position: fixed; bottom: 20px; right: 20px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: #24313d; color: #fff; border-radius: 10px; padding: 11px 18px;
  font-size: 13.5px; box-shadow: var(--shadow); max-width: 420px;
  animation: slide-in .2s ease;
}
.toast.err { background: var(--red); }
@keyframes slide-in { from { transform: translateY(10px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- Админка ---------- */
.admin-tabs { display: flex; gap: 6px; margin-bottom: 14px; flex-wrap: wrap; }
.admin-tabs .btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.panel { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 14px; }
.panel h3 { font-size: 15px; margin-bottom: 12px; }
table.simple { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.simple th { text-align: left; padding: 8px 10px; border-bottom: 2px solid var(--line); color: var(--muted); font-size: 12px; }
table.simple td { padding: 8px 10px; border-bottom: 1px solid #edf1f4; vertical-align: middle; }
.perm-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 4px; }
.perm-grid label { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text); margin: 0; }
.perm-grid input { width: auto; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 8px 14px; }

.comment-item { padding: 8px 0; border-bottom: 1px solid #f0f3f5; font-size: 13.5px; }
.comment-item .who { color: var(--muted); font-size: 12px; }

.history-list { font-size: 13px; }
.history-list li { margin-bottom: 4px; list-style: none; }

@media (max-width: 900px) {
  .topbar input[type="search"] { width: 100%; order: 10; }
  .card-sticky { position: static; }
}
