:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --border: #d0d7de;
  --accent: #1f6feb;
  --accent-dark: #1a5fd0;
  --text: #1f2328;
  --muted: #57606a;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ── Верхнее меню ─────────────────────────── */
.toolbar {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.brand {
  font-weight: 700;
  font-size: 18px;
  color: var(--accent);
  margin-right: 8px;
}

.brand-dot {
  color: #FFB7C5;
  font-size: 14px;
  margin-left: 0;
}

.toolbar-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: var(--muted);
}

.toolbar-item select {
  padding: 5px 8px;
  font-size: 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
}

/* Растягивает правую часть тулбара */
.toolbar-spacer {
  flex: 1;
}

.btn {
  padding: 6px 12px;
  font-size: 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.btn:hover {
  background: #eef1f4;
  border-color: #b6bdc6;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn.primary:hover {
  background: var(--accent-dark);
  border-color: var(--accent-dark);
}

/* ── Раскладка с боковой панелью ──────────── */
.layout {
  flex: 1;
  display: flex;
  min-height: 0;
}

/* Боковая панель со списком таблиц */
.sidebar {
  width: 220px;
  min-width: 220px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 12px 10px;
}

.sidebar-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  padding: 0 6px 8px;
}

.sidebar-group {
  margin-bottom: 10px;
}

.sidebar-group-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
  padding: 6px 8px;
  background: #eef1f4;
  border-radius: 6px;
  margin-bottom: 4px;
  cursor: pointer;
  transition: background 0.12s ease;
}

.sidebar-group-title:hover {
  background: #e0e9ff;
}

.sidebar-group-title.active {
  background: var(--accent);
  color: #fff;
}

.sidebar-item {
  padding: 5px 8px;
  font-size: 14px;
  color: var(--text);
  border-radius: 5px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background 0.12s ease;
}

.sidebar-item:hover {
  background: #eef1f4;
}

.sidebar-item.active {
  background: var(--accent);
  color: #fff;
}

/* ── Таблица ──────────────────────────────── */
.sheet {
  flex: 1;
  padding: 0; /*20px 16px;*/
  overflow: auto;
}

.table-wrap {
  display: inline-block;
  position: relative;
  background: var(--panel);
  border: 0px solid var(--border);
  border-radius: 8px;
  overflow: visible;
  max-width: 100%;
}

table {
  border-collapse: collapse;
  min-width: 100%;
}

th, td {
  border: 1px solid var(--border);
  padding: 0;
  min-width: 90px;
  height: 30px;
  border-top:0;
}

th {
  background: #eef1f4;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-align: center;
  position: sticky;
  top: 0;
}

/* Наложенная кнопка добавления столбца (правый верх, правее заголовков) */
.add-col-btn {
  position: absolute;
  top: 0;
  right: -30px;
  width: 30px;
  height: 30px;
  border: 0.5px solid var(--border);
  border-left: none;
  border-radius: 18px;
  background: #eef1f4;
  color: var(--accent);
  font-size: 18px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
  padding-left:1px;
}

.add-col-btn:hover {
  background: #e0e9ff;
  color: var(--accent-dark);
}

/* Наложенная кнопка добавления строки (левый низ, под номерами строк) */
.add-row-btn {
  position: absolute;
  left: 0;
  bottom: -30px;
  width: 36px;
  height: 30px;
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 0 8px;
  background: #eef1f4;
  color: var(--accent);
  font-size: 18px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.add-row-btn:hover {
  background: #e0e9ff;
  color: var(--accent-dark);
}

/* Маленькая кнопка удаления пустого столбца (рядом с названием в шапке) */
.del-col-btn {
  margin-left: 4px;
  width: 16px;
  height: 16px;
  line-height: 1;
  border: none;
  border-radius: 3px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  vertical-align: middle;
  transition: background 0.15s ease, color 0.15s ease;
}

.del-col-btn:hover {
  background: #f3c6cb;
  color: #d1242f;
}

/* Маленькая кнопка удаления пустой строки (рядом с номером строки) */
.del-row-btn {
  margin-left: 4px;
  width: 16px;
  height: 16px;
  line-height: 1;
  border: none;
  border-radius: 3px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  vertical-align: middle;
  transition: background 0.15s ease, color 0.15s ease;
}

.del-row-btn:hover {
  background: #f3c6cb;
  color: #d1242f;
}

/* Ячейка: контейнер с отображением и скрытым полем ввода */
.cell {
  position: relative;
  width: 100%;
  height: 100%;
}

/* Рамка активной ячейки */
.cell.active {
  box-shadow: inset 0 0 0 2px var(--accent);
}

.cell .cell-display {
  width: 100%;
  height: 100%;
  padding: 4px 6px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  cursor: text;
  white-space: pre-wrap;
  overflow: hidden;
}

.cell .cell-input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  padding: 4px 6px;
  font-size: 14px;
  font-family: inherit;
  background: #f0f6ff;
  color: var(--text);
}

.cell .cell-input:focus {
  box-shadow: inset 0 0 0 2px var(--accent);
}

/* Ручка перетаскивания активной ячейки (за её рамку).
   По умолчанию скрыта и видна только у активной ячейки (.cell.active),
   поэтому при снятии активности ручка исчезает вместе с рамкой. */
.cell-drag-handle {
  position: absolute;
  right: -6px;
  bottom: -6px;
  width: 14px;
  height: 14px;
  background: var(--accent);
  border: 2px solid #fff;
  border-radius: 4px;
  cursor: grab;
  z-index: 6;
  box-shadow: 0 1px 3px rgba(27, 31, 35, 0.3);
  display: none;
}

.cell.active .cell-drag-handle {
  display: block;
}

.cell-drag-handle:hover {
  background: var(--accent-dark);
}

/* Подсветка целевой ячейки при перетаскивании */
.cell.drag-target {
  outline: 2px dashed var(--accent);
  outline-offset: -2px;
}

/* Чекбокс-теги (#ch0 → ☐, #ch1 → ☑) в начале строки */
.checkbox {
  margin-right: 4px;
  font-weight: 400;
  vertical-align: baseline;
  font-size:20px;
}

/* Заголовок ячейки (строка, начинающаяся с "## ") */
.cell-heading {
  display: block;
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
}

/* Цвета чисел */
.num-positive {
  padding:1px 3px;
  background-color: #9feeb5;
  color: #1a7f37;
}

.num-negative {
  padding:1px 3px;
  background-color: #e9a8ad;
  color: #d1242f;
}

/* Жирное начертание для результатов вычислений формул */
.num-result {
  font-weight: 700;
}

/* Стилизация результата формулы НДС: тёмный фон, белый текст, скругление */
.num-nds {
  padding: 1px 3px;
  background-color: #555555;
  color: #ffffff;
  border-radius: 5px;
  font-weight: 700;
}

/* Компактная метка «с ндс» — заменяет тег #ндс в тексте ячейки */
.nds-tag {
  display: inline-block;
  background-color: #555555;
  color: #ffffff;
  font-size: 8px;
  font-weight: 400;
  line-height: normal;
  padding: 1px 4px;
  border-radius: 3px;
  vertical-align: middle;
  margin-left: 2px;
}

.date-tag {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  margin-left: 3px;
  border: 1px solid #8c959f;
  border-radius: 3px;
  background: #f6f8fa;
  color: var(--muted);
  font-size: 12px;
  line-height: 1;
  vertical-align: middle;
  cursor: help;
}

.cashflow-table th {
  min-width: 110px;
  padding: 6px 8px;
  white-space: nowrap;
}

.cashflow-table tbody th {
  position: static;
  min-width: 160px;
  padding: 7px 9px;
  text-align: left;
}

.cashflow-table td {
  height: auto;
  min-height: 34px;
  padding: 4px;
  vertical-align: top;
}

.cashflow-cell {
  max-width: 220px;
}

.cashflow-item {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: 28px;
  max-width: 160px;
  height: 36px;
  margin: 2px;
  padding: 3px 4px;
  overflow: hidden;
  border: 1px solid #afb8c1;
  border-radius: 3px;
  background: #f6f8fa;
  color: var(--text);
  font-size: 10px;
  line-height: 1;
  white-space: nowrap;
  cursor: help;
}

.cashflow-item-number {
  flex: none;
  font-weight: 600;
  text-align: center;
}

.cashflow-item-label {
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
  text-align: center;
  text-overflow: ellipsis;
}

.cashflow-item.positive,
.cashflow-total-row td.positive,
.cashflow-cumulative-row td.positive {
  background: #dafbe1;
  color: #1a7f37;
}

.cashflow-item.negative,
.cashflow-total-row td.negative,
.cashflow-cumulative-row td.negative {
  background: #ffebe9;
  color: #d1242f;
}

.cashflow-item.empty {
  color: var(--muted);
}

.cashflow-total-row th,
.cashflow-total-row td,
.cashflow-cumulative-row th,
.cashflow-cumulative-row td {
  height: 30px;
  padding: 6px 8px;
  background: #f6f8fa;
  font-weight: 700;
  text-align: right;
  white-space: nowrap;
}

.cashflow-total-row th,
.cashflow-cumulative-row th {
  text-align: left;
}

.cashflow-cumulative-row th,
.cashflow-cumulative-row td {
  border-top: 2px solid var(--border);
}

.cashflow-empty {
  padding: 24px !important;
  color: var(--muted);
  text-align: center;
}

/* Бледные варианты для ячеек с тегом #ch1 (checked) */
.num-positive.num-faded {
  background-color: #d9f3e0;
  color: #7bb98a;
}

.num-negative.num-faded {
  background-color: #f3dcde;
  color: #c98e92;
}

/* ── Нижняя подсказка ─────────────────────── */
.hint {
  padding: 10px 16px;
  font-size: 13px;
  color: var(--muted);
  border-top: 1px solid var(--border);
  background: var(--panel);
}

/* ── Контекстное меню (правый клик по заголовку столбца) ─── */
.context-menu {
  position: fixed;
  z-index: 1000;
  min-width: 180px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 16px rgba(27, 31, 35, 0.15);
  padding: 4px;
}

.context-menu-item {
  padding: 7px 10px;
  font-size: 14px;
  color: var(--text);
  border-radius: 5px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s ease;
}

.context-menu-item:hover {
  background: var(--accent);
  color: #fff;
}

/* ── Страницы входа / регистрации ─────────── */
.auth-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 16px;
}

.auth-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 6px 16px rgba(27, 31, 35, 0.08);
  padding: 28px;
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.auth-card h2 {
  font-size: 20px;
  color: var(--text);
  margin-bottom: 4px;
}

.auth-card label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 13px;
  color: var(--muted);
}

.auth-card input {
  padding: 8px 10px;
  font-size: 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
}

.auth-card input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(31, 111, 235, 0.15);
}

.auth-submit {
  margin-top: 4px;
  width: 100%;
}

.auth-error {
  color: #d1242f;
  font-size: 13px;
  min-height: 16px;
}

.auth-links {
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}

.auth-links a {
  color: var(--accent);
  text-decoration: none;
}

.auth-links a:hover {
  text-decoration: underline;
}

/* ── Админка ──────────────────────────────── */
.admin-wrap {
  flex: 1;
  padding: 24px 16px;
  max-width: 720px;
  margin: 0 auto;
  width: 100%;
}

.admin-wrap h2 {
  font-size: 20px;
  margin-bottom: 16px;
}

.admin-create {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.admin-create input,
.admin-create select {
  padding: 7px 10px;
  font-size: 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
}

.admin-create input {
  flex: 1;
  min-width: 140px;
}

.admin-msg {
  font-size: 13px;
  color: #1a7f37;
  min-height: 18px;
  margin-bottom: 8px;
}

.admin-msg-error {
  color: #d1242f;
}

.admin-table {
  width: 100%;
}

.admin-table th {
  text-align: left;
  padding: 6px 8px;
  min-width: 0;
  height: auto;
}

.admin-table td {
  padding: 6px 8px;
  min-width: 0;
  height: auto;
}

.admin-action {
  margin-right: 6px;
  font-size: 13px;
  padding: 4px 8px;
}

/* ── Модальное окно создания таблицы ──────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(27, 31, 35, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 6px 16px rgba(27, 31, 35, 0.15);
  padding: 20px;
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal h3 {
  font-size: 18px;
  color: var(--text);
}

.modal label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 13px;
  color: var(--muted);
}

.modal input,
.modal select {
  padding: 8px 10px;
  font-size: 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
}

.modal input:focus,
.modal select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(31, 111, 235, 0.15);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}