* {
  box-sizing: border-box;
}

:root {
  --background: #f6f7f8;
  --border: #d9dee3;
  --card: #ffffff;
  --foreground: #182026;
  --muted: #65717c;
  --muted-bg: #eef1f4;
  --primary: #1f4f46;
  --primary-foreground: #ffffff;
  --sidebar: #101820;
  --sidebar-muted: #a8b3bd;
}

body {
  background: var(--background);
  color: var(--foreground);
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  min-height: 100vh;
}

.app-shell {
  display: grid;
  grid-template-columns: 16rem minmax(0, 1fr);
  grid-template-rows: auto 1fr;
}

.sidebar {
  background: var(--sidebar);
  border-right: 1px solid #26313a;
  grid-row: 1 / span 2;
  min-height: 100vh;
  padding: 1.25rem;
}

.brand {
  align-items: center;
  color: var(--primary-foreground);
  display: flex;
  gap: 0.75rem;
  margin-bottom: 2rem;
  text-decoration: none;
}

.brand-mark {
  align-items: center;
  background: var(--primary);
  border-radius: 0.5rem;
  color: var(--primary-foreground);
  display: inline-flex;
  font-weight: 700;
  height: 2.25rem;
  justify-content: center;
  width: 2.25rem;
}

.brand small {
  color: var(--sidebar-muted);
  display: block;
  font-size: 0.75rem;
  margin-top: 0.125rem;
}

.nav-list {
  display: grid;
  gap: 0.35rem;
}

.nav-item {
  align-items: center;
  border-radius: 0.5rem;
  color: #dce3e8;
  display: flex;
  gap: 0.75rem;
  min-height: 2.5rem;
  padding: 0 0.75rem;
  text-decoration: none;
}

.nav-item.active {
  background: #20303a;
}

.nav-item.disabled {
  color: #7f8b95;
}

.nav-icon {
  color: var(--sidebar-muted);
  text-align: center;
  width: 1.25rem;
}

.topbar {
  align-items: center;
  background: rgba(255, 255, 255, 0.92);
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  padding: 1rem 2rem;
}

.topbar h1,
.page-header h2,
.panel h3,
.login-card h1 {
  margin: 0;
}

.topbar h1 {
  font-size: 1.1rem;
}

.user-menu {
  align-items: center;
  display: flex;
  gap: 0.75rem;
}

.user-name {
  color: var(--muted);
  font-size: 0.9rem;
}

.role-badge,
.status-pill {
  background: var(--muted-bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  color: #35414b;
  font-size: 0.8rem;
  padding: 0.35rem 0.65rem;
}

.content {
  padding: 2rem;
}

.content > * + * {
  margin-top: 1.5rem;
}

.page-header {
  align-items: flex-start;
  display: flex;
  justify-content: space-between;
}

.page-header h2 {
  font-size: clamp(1.75rem, 3vw, 2.4rem);
  letter-spacing: 0;
}

.page-header p,
.panel p,
.login-copy p {
  color: var(--muted);
  line-height: 1.55;
}

.eyebrow {
  color: var(--primary);
  font-size: 0.76rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  margin: 0 0 0.35rem;
  text-transform: uppercase;
}

.summary-grid {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  margin-bottom: 1rem;
}

.summary-grid.two-columns {
  grid-template-columns: minmax(18rem, 24rem) minmax(0, 1fr);
}

.panel,
.login-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  box-shadow: 0 1px 2px rgba(16, 24, 32, 0.04);
}

.panel {
  padding: 1.25rem;
}

.panel-icon {
  align-items: center;
  background: #e7efed;
  border-radius: 0.45rem;
  color: var(--primary);
  display: inline-flex;
  font-weight: 700;
  height: 2rem;
  justify-content: center;
  margin-bottom: 1rem;
  width: 2rem;
}

.wide-panel {
  align-items: center;
  display: flex;
  min-height: 8rem;
}

.settings-panel {
  margin-bottom: 1rem;
}

/* Метка нужна скринридеру, но в шапке дублирует видимый контекст */
.visually-hidden {
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  height: 1px;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
  width: 1px;
}

.inline-form {
  align-items: end;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 0.75rem;
}

.inline-form .form-field,
.inline-form .field {
  margin-bottom: 0;
  min-width: 9rem;
}

/* Поля вне .field остаются без оформления, поэтому съезжают по высоте относительно кнопок */
.inline-form input,
.inline-form select {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 0.45rem;
  color: var(--foreground);
  font: inherit;
  min-height: 2.5rem;
  min-width: 8rem;
  padding: 0 0.75rem;
}

.inline-form input[type="submit"] {
  min-width: 0;
  width: auto;
}

.button,
input[type="submit"] {
  border: 0;
  border-radius: 0.45rem;
  cursor: pointer;
  font: inherit;
  font-weight: 650;
  min-height: 2.25rem;
  padding: 0 0.9rem;
}

.button.primary,
input[type="submit"].primary {
  background: var(--primary);
  color: var(--primary-foreground);
  width: 100%;
}

.home-actions {
  display: grid;
  gap: 0.75rem;
  grid-template-columns: 1fr 1fr;
  margin-bottom: 1rem;
}

.wide-button {
  align-items: center;
  display: flex;
  font-size: 1.05rem;
  justify-content: center;
  min-height: 3rem;
  text-decoration: none;
}

.available-amount {
  font-size: 1.75rem;
  margin: 0.25rem 0 0.5rem;
}

.section-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.section-tabs .nav-item {
  border: 1px solid var(--border);
  color: var(--foreground);
  min-height: 2.25rem;
}

.section-tabs .nav-item.active {
  background: var(--primary);
  color: var(--primary-foreground);
}

.week-row + .week-row {
  border-top: 1px solid var(--border);
  padding-top: 0.5rem;
}

.inline-button {
  align-items: center;
  display: inline-flex;
  justify-content: center;
  text-decoration: none;
  width: auto !important;
}

.button.ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--foreground);
}

.auth-shell {
  display: grid;
  min-height: 100vh;
  place-items: center;
}

.login-card {
  max-width: 26rem;
  padding: 2rem;
  width: min(100% - 2rem, 26rem);
}

.login-copy {
  margin-bottom: 1.5rem;
}

.login-copy .brand-mark {
  margin-bottom: 1rem;
}

.form-field {
  display: grid;
  gap: 0.35rem;
  margin-bottom: 1rem;
}

.form-field label {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 650;
}

.form-field input,
.form-field select {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 0.45rem;
  color: var(--foreground);
  font: inherit;
  min-height: 2.5rem;
  padding: 0 0.75rem;
  width: 100%;
}

.form-errors {
  background: #fff3f0;
  border: 1px solid #f0b8aa;
  border-radius: 0.45rem;
  color: #8a2f1f;
  margin-bottom: 1rem;
  padding: 0.75rem;
}

.form-errors p {
  margin: 0.25rem 0;
}

.notice {
  background: #e7efed;
  border: 1px solid #b9cec8;
  border-radius: 0.45rem;
  color: var(--primary);
  padding: 0.75rem;
}

.preview-list {
  display: grid;
  gap: 0.35rem;
  grid-template-columns: 9rem minmax(0, 1fr);
  line-height: 1.5;
}

.preview-list dt {
  color: var(--muted);
  font-weight: 700;
}

.preview-list dd {
  margin: 0;
}

.data-table {
  border-collapse: collapse;
  width: 100%;
}

.data-table th,
.data-table td {
  border-bottom: 1px solid var(--border);
  padding: 0.65rem;
  text-align: left;
}

.data-table th {
  color: var(--muted);
  font-size: 0.8rem;
}

.form-stack {
  display: grid;
  gap: 1rem;
}

.field {
  display: grid;
  gap: 0.4rem;
}

.field label {
  color: #35414b;
  font-size: 0.9rem;
  font-weight: 650;
}

.field input,
.field select,
.field textarea {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 0.45rem;
  color: var(--foreground);
  font: inherit;
  min-height: 2.5rem;
  padding: 0 0.75rem;
  width: 100%;
}

.field textarea {
  padding: 0.65rem 0.75rem;
  resize: vertical;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(31, 79, 70, 0.14);
  outline: none;
}

.checkbox-field {
  align-items: center;
  color: #35414b;
  display: flex;
  font-weight: 650;
  gap: 0.5rem;
}

.alert {
  background: #fff1f1;
  border: 1px solid #f2b8b8;
  border-radius: 0.45rem;
  color: #8f1d1d;
  padding: 0.75rem;
}

.filters {
  align-items: end;
  display: grid;
  gap: 1rem;
  grid-template-columns: 8rem 10rem 12rem minmax(12rem, 1fr) auto;
}

.form-panel {
  max-width: 42rem;
}

/* Прокрутка живет на самой таблице (.table-scroll): overflow на карточке обрезал бы
   всплывающие памятки и любой другой раскрывающийся блок внутри нее. */
.table-panel {
  padding: 0;
}

.table-panel > .form-stack,
.table-panel > p,
.table-panel > h4 {
  padding: 0 1rem;
}

.table-panel > .form-stack {
  margin: 1rem 0;
}

.table-panel > h4 {
  margin: 1.25rem 0 0.5rem;
}

.table-panel > p {
  margin: 0.75rem 0;
}

.data-table td .inline-form {
  gap: 0.4rem 0.5rem;
}

.data-table td .inline-form input,
.data-table td .inline-form select {
  min-height: 2.25rem;
  min-width: 6.5rem;
}

/* Ячейки не переносятся, поэтому таблица внутри панели скроллится сама */
.table-scroll {
  overflow-x: auto;
}

.panel-toolbar {
  align-items: center;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  padding: 1rem;
}

.upload-form {
  align-items: center;
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 0.75rem;
  padding: 1rem;
}

.actions-cell {
  align-items: center;
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
}

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

th,
td {
  border-bottom: 1px solid var(--border);
  padding: 0.85rem 1rem;
  text-align: left;
  white-space: nowrap;
}

th {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
}

td input,
td select {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 0.45rem;
  color: var(--foreground);
  font: inherit;
  min-height: 2.25rem;
  padding: 0 0.6rem;
  width: 100%;
}

td a {
  color: var(--primary);
  font-weight: 700;
  text-decoration: none;
}

code {
  background: var(--muted-bg);
  border-radius: 0.35rem;
  color: #35414b;
  font-size: 0.85em;
  padding: 0.15rem 0.35rem;
}

.empty-state {
  color: var(--muted);
  margin: 0;
  padding: 1rem;
}

.pagination {
  align-items: center;
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
  margin: 0 0 1rem;
}

.pagination .button.ghost {
  background: var(--card);
  border-color: #b9c2ca;
  box-shadow: 0 1px 2px rgba(16, 24, 32, 0.06);
}

.pagination span {
  color: var(--muted);
  font-weight: 650;
}

.print-totals {
  margin-top: 1rem;
  max-width: 42rem;
}

/* Кнопки ввода в журнале и строки платежей: ежедневные сценарии не должны требовать таблиц */
.quick-actions {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.quick-actions .eyebrow {
  flex-basis: 100%;
  margin: 0;
}

.payment-row .payment-labels {
  align-items: center;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  font-size: 0.85rem;
  gap: 0.5rem;
  margin: 0.75rem 0;
}

.payment-row .panel-toolbar strong {
  font-size: 1.15rem;
  white-space: nowrap;
}

/* Toolbar задуман для .table-panel с нулевым padding; внутри обычной панели он
   иначе дает двойной отступ и лишнюю линию поперек карточки. */
.panel:not(.table-panel) > .panel-toolbar,
.panel:not(.table-panel) > * > .panel-toolbar {
  border-bottom: 0;
  gap: 1rem;
  padding: 0 0 0.75rem;
}

/* button_to рендерит форму: без этого соседние кнопки слипаются в блочный столбец */
form.button_to {
  display: inline-block;
  margin: 0 0.5rem 0.5rem 0;
}

form.button_to:last-child {
  margin-right: 0;
}

form.button_to .button,
form.button_to input[type="submit"] {
  width: auto;
}

/* Вложенные карточки: строки платежей, счета, цели */
.panel > .panel,
.panel > * > .panel {
  box-shadow: none;
  margin-top: 1rem;
}

.panel > .panel:first-child {
  margin-top: 0;
}

/* Раскрывающиеся блоки: кликабельная строка и воздух вокруг содержимого */
details > summary {
  cursor: pointer;
  font-weight: 650;
  padding: 0.35rem 0;
}

details[open] > summary {
  margin-bottom: 0.75rem;
}

.panel > details,
.panel > * > details {
  margin-top: 1rem;
}

.table-panel > .table-scroll {
  margin-bottom: 0.25rem;
}

.table-panel > details,
.table-panel > .inline-form,
.table-panel > .pagination {
  padding: 0 1rem 1rem;
}

.table-panel > .inline-form {
  padding-top: 1rem;
}

.table-panel > .panel {
  margin: 1rem;
}

/* Заголовок-одиночка: разделительная линия под ним не отделяет ничего */
.table-panel > .panel-toolbar:only-child {
  border-bottom: 0;
}

/* .form-stack уже держит вертикальный ритм — второй отступ от сетки лишний */
.form-stack > .summary-grid,
details > .summary-grid {
  margin-bottom: 0;
}

.data-table td small {
  color: var(--muted);
  display: block;
  margin-top: 0.15rem;
  white-space: normal;
}

.data-table td .button {
  white-space: nowrap;
}

/* Список внутри панели без собственных отступов слипается с текстом */
.panel > ul,
.panel > * > ul {
  line-height: 1.6;
  margin: 0.75rem 0;
  padding-left: 1.15rem;
}

.settings-panel li + li {
  margin-top: 0.5rem;
}

/* Памятка раздела: «?» в шапке, раскрывается поверх контента без JS */
.header-aside,
.block-heading {
  align-items: center;
  display: flex;
  gap: 0.5rem;
  position: relative;
}

/* Заголовок блока с памяткой внутри карточки без собственного toolbar */
.block-heading {
  flex-basis: 100%;
  justify-content: space-between;
  margin-bottom: 0.25rem;
}

.block-heading .eyebrow,
.block-heading h3 {
  margin: 0;
}

/* Памятка блока раскрывается поверх содержимого, а не раздвигает его */
.panel-toolbar {
  position: relative;
}

.panel-toolbar .screen-help,
.block-heading .screen-help {
  flex: 0 0 auto;
}

.screen-help > summary {
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  font-weight: 600;
  height: 1.75rem;
  justify-content: center;
  list-style: none;
  user-select: none;
  width: 1.75rem;
}

.screen-help > summary::-webkit-details-marker {
  display: none;
}

.screen-help[open] > summary {
  background: var(--card);
  color: var(--foreground);
}

/* Памятка всплывает от края своего контейнера и не шире экрана.
   Внутри карточки контейнер — сам toolbar, поэтому текст идет от левого края панели. */
.screen-help-body {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  box-shadow: 0 12px 32px rgb(0 0 0 / 18%);
  font-weight: 400;
  max-width: min(30rem, calc(100vw - 2rem));
  min-width: min(18rem, calc(100vw - 2rem));
  padding: 1rem 1.25rem;
  position: absolute;
  right: 0;
  top: calc(100% + 0.35rem);
  width: max-content;
  z-index: 20;
}

.panel-toolbar .screen-help-body,
.block-heading .screen-help-body {
  left: 0;
  right: auto;
}

/* Внутри toolbar точкой отсчета остается сам toolbar, иначе памятка у правого
   края карточки уезжала бы за ее пределы */
.panel-toolbar .header-aside {
  position: static;
}

/* Заголовок и памятка держатся вместе, но не создают своей системы координат */
.toolbar-title {
  align-items: center;
  display: flex;
  gap: 0.5rem;
}

.screen-help-body h3 {
  margin-top: 0;
}

.screen-help-body ul {
  margin-bottom: 0;
  padding-left: 1.1rem;
}

.screen-help-body li + li {
  margin-top: 0.35rem;
}

@media (max-width: 760px) {
  .app-shell {
    display: block;
  }

  .sidebar {
    min-height: auto;
    padding: 1rem;
  }

  .brand {
    margin-bottom: 1rem;
  }

  /* Меню превращается в горизонтальную ленту, чтобы не съедать первый экран */
  .nav-list {
    grid-auto-flow: column;
    grid-auto-columns: max-content;
    overflow-x: auto;
  }

  .nav-item {
    white-space: nowrap;
  }

  .topbar,
  .page-header,
  .user-menu {
    align-items: stretch;
    flex-direction: column;
  }

  .topbar,
  .page-header {
    gap: 1rem;
  }

  .summary-grid,
  .summary-grid.two-columns,
  .filters,
  .preview-list {
    grid-template-columns: 1fr;
  }

  /* .inline-form — flex, поэтому grid-template-columns на нее не действовала
     и формы уезжали за край экрана. */
  .inline-form {
    align-items: stretch;
    flex-direction: column;
  }

  .inline-form .form-field,
  .inline-form .field,
  .inline-form input,
  .inline-form select,
  .inline-form input[type="submit"] {
    min-width: 0;
    width: 100%;
  }

  .quick-actions .button {
    flex: 1 1 100%;
  }

  /* На телефоне «Можно тратить» должно быть на первом экране. */
  .brand small {
    display: none;
  }

  .home-actions {
    grid-template-columns: 1fr;
  }

  .payment-row .panel-toolbar {
    align-items: flex-start;
    gap: 0.35rem;
  }

  .pagination {
    justify-content: space-between;
  }

  form.button_to,
  form.button_to .button,
  form.button_to input[type="submit"] {
    width: 100%;
  }

  .upload-form,
  .panel-toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .content,
  .topbar {
    padding: 1rem;
  }

  /* Пальцем в 36px попадать неудобно: поднимаем контролы до 44px */
  .button,
  input[type="submit"],
  .field input,
  .field select,
  .field textarea,
  .form-field input,
  .form-field select,
  .nav-item {
    min-height: 2.75rem;
  }

  th,
  td {
    padding: 0.65rem 0.75rem;
  }

  .actions-cell {
    flex-wrap: wrap;
  }

  /* На узком экране памятка занимает ширину страницы, а не висит карточкой у края */
  .screen-help-body {
    left: 0;
    max-width: none;
    right: 0;
    width: auto;
  }
}

@media print {
  :root {
    --background: #ffffff;
    --border: #c8c8c8;
    --card: #ffffff;
    --foreground: #000000;
    --muted: #444444;
    --primary: #000000;
  }

  body,
  .app-shell {
    background: #ffffff;
    color: #000000;
    display: block;
  }

  .sidebar,
  .topbar,
  .no-print {
    display: none !important;
  }

  .content {
    padding: 0;
  }

  .page-header,
  .summary-grid,
  .panel {
    break-inside: avoid;
  }

  .page-header {
    border-bottom: 1px solid #000000;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
  }

  .page-header h2 {
    font-size: 1.6rem;
  }

  .summary-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .panel {
    border-color: #c8c8c8;
    box-shadow: none;
  }

  .table-panel,
  .table-scroll {
    overflow: visible;
  }

  th,
  td {
    padding: 0.45rem 0.5rem;
    white-space: normal;
  }

  a {
    color: #000000;
    text-decoration: none;
  }
}
