:root {
  /* Brand — from client logo: navy #003366, orange gradient FEA42A→F86A01 */
  --ink: #0a1524;
  --ink-2: #0f1e33;
  --ink-line: rgba(255, 255, 255, 0.08);
  --ink-text: #e8edf3;
  --ink-text-muted: rgba(232, 237, 243, 0.56);
  --ink-text-faint: rgba(232, 237, 243, 0.36);

  --paper: #f4f6f7;
  --surface: #ffffff;
  --line: #dde2e6;
  --line-soft: #eef1f3;
  --text: #16202b;
  --text-muted: #5b6874;
  --text-faint: #8b96a1;

  --accent: #003366;
  --accent-strong: #001f3f;
  --accent-soft: #e7edf4;

  --brand-orange: #f47b12;
  --brand-orange-strong: #c95f07;
  --brand-orange-soft: #fdecdb;

  --warn: #a8641c;
  --warn-soft: #faf0df;
  --danger: #a3372a;
  --danger-soft: #fbeae6;
  --success: #2f7a52;
  --success-soft: #e6f3ea;

  --radius: 6px;
  --radius-sm: 4px;
  --font-sans: "IBM Plex Sans", system-ui, -apple-system, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, "SFMono-Regular", monospace;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--paper);
  color: var(--text);
  line-height: 1.5;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

img { max-width: 100%; }

a {
  color: var(--accent);
  text-decoration: none;
}
a:hover { text-decoration: underline; }

h1, h2, h3 {
  font-family: var(--font-sans);
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.01em;
  margin: 0 0 1rem;
}

h1 {
  font-size: 1.375rem;
  padding-bottom: 0.85rem;
  border-bottom: 1px solid var(--line);
  margin-bottom: 1.25rem;
}

h2 {
  font-size: 1rem;
  margin-top: 2.25rem;
}

/* ---------------------------------------------------------------- */
/* Chrome — light branded masthead, dark navy nav rail, paper        */
/* workspace.                                                        */
/* ---------------------------------------------------------------- */

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.7rem 1.5rem;
  background: var(--surface);
  color: var(--text);
  border-bottom: 1px solid var(--line);
  font-size: 0.85rem;
}

/* The brand link ("ЭкоГрупп · Управленческий учёт") is replaced with
   the client's real horizontal logo via a CSS background-image, so
   every page that repeats this header markup picks it up for free. */
.topbar span:first-child a {
  display: inline-block;
  width: 176px;
  height: 28px;
  background: url("/static/logo-horizontal.svg") left center / contain no-repeat;
  text-indent: -9999px;
  overflow: hidden;
}

.topbar span:last-child { color: var(--text-muted); font-size: 0.82rem; }
.topbar span:last-child a { color: var(--accent); font-weight: 500; }
.topbar span:last-child a:hover { color: var(--accent-strong); }

.layout {
  display: flex;
  align-items: stretch;
  min-height: calc(100vh - 50px);
}

.sidebar {
  min-width: 216px;
  padding: 1.5rem 1rem 2rem;
  background: var(--ink-2);
  border-right: 1px solid var(--ink-line);
}

.sidebar h2 {
  font-family: var(--font-mono);
  font-size: 0.66rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--ink-text-faint);
  margin: 1.5rem 0 0.4rem;
  padding: 0 0.6rem;
  border: none;
}
.sidebar h2:first-child { margin-top: 0; }

.sidebar a {
  display: block;
  padding: 0.4rem 0.6rem;
  margin: 1px 0;
  border-radius: var(--radius-sm);
  color: var(--ink-text-muted);
  text-decoration: none;
  font-size: 0.85rem;
  border-left: 2px solid transparent;
}
.sidebar a:hover {
  color: var(--ink-text);
  background: rgba(255, 255, 255, 0.05);
  text-decoration: none;
}
.sidebar a.active {
  color: #fff;
  background: rgba(244, 123, 18, 0.16);
  border-left-color: var(--brand-orange);
  font-weight: 500;
}

/* Multi-level nav — "Справочники" collapses into one item that reveals
   its children as a flyout on hover (or keyboard focus), instead of
   listing all reference types inline all the time. */
.nav-group {
  position: relative;
}

.nav-group-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.4rem 0.6rem;
  margin: 1px 0;
  border-radius: var(--radius-sm);
  color: var(--ink-text-muted);
  font-size: 0.85rem;
  border-left: 2px solid transparent;
  cursor: default;
}
.nav-group-label::after {
  content: "\203A";
  color: var(--ink-text-faint);
  font-family: var(--font-mono);
}
.nav-group:hover .nav-group-label,
.nav-group:focus-within .nav-group-label {
  color: var(--ink-text);
  background: rgba(255, 255, 255, 0.05);
}
.nav-group-label.active {
  color: #fff;
  background: rgba(244, 123, 18, 0.16);
  border-left-color: var(--brand-orange);
  font-weight: 500;
}

.submenu {
  display: none;
  position: absolute;
  left: 100%;
  top: 0;
  min-width: 208px;
  background: var(--ink-2);
  border: 1px solid var(--ink-line);
  border-radius: var(--radius-sm);
  padding: 0.4rem;
  box-shadow: 12px 12px 32px rgba(0, 0, 0, 0.4);
  z-index: 20;
}
.nav-group:hover .submenu,
.nav-group:focus-within .submenu {
  display: block;
}
.submenu a { white-space: nowrap; }

.content {
  flex: 1;
  padding: 2rem 2.5rem 3rem;
  min-width: 0;
}

/* ---------------------------------------------------------------- */
/* Tables                                                            */
/* ---------------------------------------------------------------- */

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}

th, td {
  padding: 0.6rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid var(--line-soft);
  vertical-align: middle;
}

th {
  font-family: var(--font-mono);
  font-size: 0.66rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-faint);
  background: var(--line-soft);
  border-bottom: 1px solid var(--line);
}

tbody tr:hover { background: var(--paper); }
tbody tr:last-child td { border-bottom: none; }

.num {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  text-align: right;
  white-space: nowrap;
}

/* ---------------------------------------------------------------- */
/* Forms                                                             */
/* ---------------------------------------------------------------- */

form.ref-form,
form[method="get"] {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.9rem;
  margin-top: 1.5rem;
  padding: 1.25rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.78rem;
  color: var(--text-muted);
}

input, select, textarea {
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-family: var(--font-sans);
  color: var(--text);
  background: var(--surface);
}

textarea {
  /* По ширине тянется за меткой, по высоте — только вручную:
     горизонтальное растягивание ломало бы раскладку модалки. */
  resize: vertical;
  line-height: 1.4;
}

input:focus, select:focus, textarea:focus, button:focus, a:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Поле во всю ширину формы. .modal-body и .ref-form — flex с переносом,
   поэтому базис 100% выносит метку на отдельную строку целиком. */
.field--full {
  flex: 1 0 100%;
}

input[type="checkbox"] {
  width: 16px;
  height: 16px;
  padding: 0;
  align-self: flex-start;
}

input[type="search"] {
  width: 260px;
  margin-bottom: 1rem;
}

button {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 500;
  font-family: var(--font-sans);
  cursor: pointer;
}
button:hover { background: var(--accent-strong); }

/* Small inline forms embedded in table cells (status-transition
   controls, activate/deactivate toggles). */
.inline-form {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}
.inline-form input {
  padding: 0.3rem 0.5rem;
  font-size: 0.8rem;
}
.inline-form button {
  padding: 0.35rem 0.7rem;
  font-size: 0.78rem;
}

/* Row-level admin actions (изменить/деактивировать/восстановить) —
   icon-only, with the Russian label carried in title/aria-label for
   screen readers and for text-based checks against the same string.
   The flex layout lives on an INNER .row-actions-group wrapper, never
   on the <td> itself — overriding a <td>'s display away from
   table-cell knocks it out of the table's column-width algorithm, so
   the column stops stretching and the icons end up stranded next to
   the previous column instead of flush with the table's right edge. */
.row-actions {
  text-align: right;
  white-space: nowrap;
}
.row-actions-group {
  display: inline-flex;
  align-items: center;
  gap: 0.15rem;
}
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}
.icon-btn svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}
a.icon-btn { color: var(--text-muted); }
.icon-btn:hover {
  background: var(--line-soft);
  color: var(--accent);
  text-decoration: none;
}
.icon-btn.icon-btn--danger:hover { color: var(--danger); }

/* Toolbar "+ Добавить …" button that opens the creation dialog — sits
   near the page heading, above the table, instead of a form permanently
   inline at the bottom of the page. */
.btn-add {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  margin-bottom: 1.25rem;
  background: var(--surface);
  color: var(--accent);
  border: 1px solid var(--accent);
}
.btn-add:hover { background: var(--accent-soft); }

/* Toolbar holding several .btn-add triggers side by side (Доход/Расход/
   Перевод) — the toolbar owns the shared bottom margin, buttons don't
   double it. */
.btn-toolbar {
  display: flex;
  gap: 0.6rem;
  margin-bottom: 1.25rem;
}
.btn-toolbar .btn-add { margin-bottom: 0; }

/* ---------------------------------------------------------------- */
/* Modals — creation forms live in a <dialog>, opened by .btn-add.    */
/* ---------------------------------------------------------------- */

dialog.modal {
  border: none;
  border-radius: var(--radius);
  padding: 0;
  width: min(560px, calc(100vw - 2.5rem));
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.28);
  /* Per-row edit dialogs sit nested inside <td class="row-actions"> in
     the DOM (see money/_rows.html, reference/_rows.html) — showModal()
     only escapes that ancestry for POSITIONING (top layer), not for CSS
     inheritance, so any inherited property set on that <td> (e.g.
     text-align: right, for the icons) would otherwise leak into every
     field inside the dialog. Reset inherited text properties here so a
     dialog's content never depends on where it happens to be nested. */
  text-align: initial;
}
dialog.modal::backdrop {
  background: rgba(10, 21, 36, 0.55);
}
dialog.modal form.ref-form {
  margin: 0;
  border: none;
  border-radius: 0;
  flex-direction: column;
  align-items: stretch;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.1rem 1.25rem;
  border-bottom: 1px solid var(--line);
}
.modal-header h2 { margin: 0; font-size: 1.05rem; }
.modal-body { padding: 1.25rem; display: flex; flex-wrap: wrap; gap: 0.9rem; }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--line);
  background: var(--paper);
}
.btn-secondary {
  background: var(--surface);
  color: var(--text-muted);
  border: 1px solid var(--line);
}
.btn-secondary:hover { background: var(--line-soft); color: var(--text); }

a.btn-secondary {
  display: inline-flex;
  align-items: center;
  padding: 0.5rem 1rem;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 500;
  font-family: var(--font-sans);
  text-decoration: none;
}
a.btn-secondary:hover { text-decoration: none; }

/* ---------------------------------------------------------------- */
/* Combobox — searchable dropdown with inline "create new", used      */
/* right now for the counterparty field in Операция ДДС.              */
/* ---------------------------------------------------------------- */

.combobox {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
}
.combobox-input { width: 220px; }
.combobox-create {
  padding: 0.45rem 0.7rem;
  font-size: 0.82rem;
  background: var(--surface);
  color: var(--accent);
  border: 1px solid var(--line);
  white-space: nowrap;
}
.combobox-create:hover { background: var(--accent-soft); }
.combobox-list {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 30;
  margin: 0.2rem 0 0;
  padding: 0.25rem;
  list-style: none;
  width: 220px;
  max-height: 200px;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.16);
}
.combobox-list li {
  padding: 0.35rem 0.5rem;
  font-size: 0.82rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.combobox-list li:hover,
.combobox-list li.is-highlighted { background: var(--line-soft); }

.doc-combobox { position: relative; display: flex; flex-direction: column; gap: 0.3rem; }
.doc-combobox-input { width: 260px; }
.doc-combobox-list {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 30;
  margin: 0.2rem 0 0;
  padding: 0.25rem;
  list-style: none;
  width: 260px;
  max-height: 200px;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.16);
}
.doc-combobox-list li {
  padding: 0.35rem 0.5rem;
  font-size: 0.82rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.doc-combobox-list li:hover,
.doc-combobox-list li.is-highlighted { background: var(--line-soft); }

dialog.modal--nested {
  border: none;
  border-radius: var(--radius);
  padding: 0;
  width: min(360px, calc(100vw - 3rem));
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.32);
}
dialog.modal--nested::backdrop { background: rgba(10, 21, 36, 0.35); }
dialog.modal--nested .modal-body { flex-direction: column; }

/* ---------------------------------------------------------------- */
/* Messaging                                                         */
/* ---------------------------------------------------------------- */

p.error {
  color: var(--danger);
  background: var(--danger-soft);
  border: 1px solid rgba(163, 55, 42, 0.22);
  padding: 0.6rem 0.85rem;
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  margin: 0 0 1rem;
}

/* Non-blocking business warnings ("продажа сверх остатка", etc.) —
   deliberately amber, not the validation-error red above: this app's
   own philosophy is "warn, don't block," and the color should say so. */
.warning-chip {
  display: inline-block;
  margin-left: 0.5rem;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  background: var(--warn-soft);
  color: var(--warn);
  font-family: var(--font-mono);
  font-size: 0.68rem;
}

/* ---------------------------------------------------------------- */
/* Status badges                                                     */
/* ---------------------------------------------------------------- */

.badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  white-space: nowrap;
}
.badge--neutral { background: var(--line-soft); color: var(--text-muted); }
.badge--progress { background: var(--brand-orange-soft); color: var(--brand-orange-strong); }
.badge--done { background: var(--success-soft); color: var(--success); }
.badge--out { background: var(--warn-soft); color: var(--warn); }

/* ---------------------------------------------------------------- */
/* Login                                                              */
/* ---------------------------------------------------------------- */

.login-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--ink);
}

.login-card {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  width: 340px;
  padding: 2.25rem;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.35);
}

.login-logo {
  height: 34px;
  width: auto;
  margin-bottom: 0.5rem;
}

.login-card h1 { border: none; padding: 0; margin: 0 0 0.25rem; font-size: 1.3rem; }
.login-card label { font-size: 0.82rem; }
.login-card button { margin-top: 0.4rem; }

/* ---------------------------------------------------------------- */
/* Misc                                                               */
/* ---------------------------------------------------------------- */

.subtitle { color: var(--text-muted); }

#chart {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem;
}

/* ---------------------------------------------------------------- */
/* Отчёт ДДС — переключатель периода, сворачиваемое дерево статей   */
/* ---------------------------------------------------------------- */

.period-toggle {
  display: flex;
  gap: 0.3rem;
  margin-bottom: 0.75rem;
}
.btn-toggle {
  background: var(--surface);
  color: var(--text-muted);
  border: 1px solid var(--line);
}
.btn-toggle:hover { background: var(--paper); color: var(--text); }
.btn-toggle.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.btn-toggle.active:hover { background: var(--accent-strong); }

.checkbox-label {
  flex-direction: row !important;
  align-items: center;
  gap: 0.4rem !important;
}
/* input[type=checkbox]'s align-self: flex-start below is meant for a
   checkbox sitting alone in a normal COLUMN-direction <label> (stops it
   stretching sideways to the label's full width). .checkbox-label is
   ROW-direction, so that same align-self pins the box to the label's
   top edge instead of centering it against the text — override back to
   the row's natural vertical centering. */
.checkbox-label input[type="checkbox"] {
  align-self: center;
}

.tree-toggle {
  display: inline-flex;
  align-items: center;
  background: none;
  border: none;
  color: var(--text-muted);
  padding: 0 0.2rem 0 0;
  cursor: pointer;
  vertical-align: middle;
}
.tree-toggle:hover { color: var(--text); background: none; }
.tree-toggle-icon {
  display: inline-flex;
  transition: transform 0.15s ease;
  transform: rotate(90deg);
}
.tree-toggle.collapsed .tree-toggle-icon { transform: rotate(0deg); }

.flow-type-header td {
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-weight: 700;
  text-transform: uppercase;
  font-size: 0.75rem;
  letter-spacing: 0.04em;
}
.flow-type-subtotal td { background: var(--line-soft); font-weight: 700; }
.section-header td {
  background: var(--line-soft);
  font-weight: 600;
  padding-left: 2rem;
}
.section-subtotal td { background: var(--paper); font-weight: 600; }
.grand-total td { background: var(--line-soft); font-weight: 700; }

.num.positive { color: #2f7a52; }
.num.negative { color: #a3372a; }
.num.neutral { color: var(--text-muted); }

.sub-text {
  color: var(--text-muted);
  font-style: italic;
  font-size: 0.8em;
}

.row-inactive td:not(.row-actions) {
  text-decoration: line-through;
  color: var(--text-faint);
}

/* ---------------------------------------------------------------- */
/* Дашборд — карточки с графиками ECharts                            */
/* ---------------------------------------------------------------- */

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 1.25rem;
}

.dashboard-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: 0 1px 2px rgba(10, 21, 36, 0.04);
}
.dashboard-card--wide { grid-column: 1 / -1; }
.dashboard-card h2 { margin-top: 0; }

.dashboard-total {
  font-size: 1.75rem;
  font-weight: 700;
  margin: 0 0 1rem;
}

.dashboard-chart { height: 260px; }

.dashboard-accordion {
  margin-top: 1rem;
  border-top: 1px solid var(--line-soft);
}
.dashboard-accordion details {
  border-bottom: 1px solid var(--line-soft);
}
.dashboard-accordion summary {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 0.2rem;
  cursor: pointer;
  list-style: none;
}
.dashboard-accordion summary::-webkit-details-marker { display: none; }
.dashboard-accordion summary::before {
  content: "\203A";
  display: inline-block;
  margin-right: 0.5rem;
  font-family: var(--font-mono);
  color: var(--text-faint);
  transition: transform 0.15s ease;
}
.dashboard-accordion details[open] summary::before { transform: rotate(90deg); }

.dashboard-account-list {
  list-style: none;
  margin: 0 0 0.6rem;
  padding: 0 0 0 1.4rem;
}
.dashboard-account-list li {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.35rem 0;
  font-size: 0.85rem;
  border-bottom: 1px solid var(--line-soft);
}
.dashboard-account-list li:last-child { border-bottom: none; }
