:root {
  --color-bg: #f4f5f7;
  --color-surface: #ffffff;
  --color-text: #1f2430;
  --color-muted: #6b7280;
  --color-border: #e3e5e9;
  --color-accent: #2563eb;
  --color-accent-hover: #1d4ed8;
  --color-error: #dc2626;
  --color-error-bg: #fef2f2;
  --color-ok: #16a34a;
  --color-ok-bg: #f0fdf4;
  --radius: 10px;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-sans);
  background: var(--color-bg);
  color: var(--color-text);
  min-height: 100vh;
}

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

a:hover {
  text-decoration: underline;
}

/* ---------- Навигация ---------- */

.nav {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 24px;
  width: 100%;
  padding: 0 20px;
  height: 56px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.nav__brand {
  font-weight: 600;
  white-space: nowrap;
  flex-shrink: 0;
}

.nav__tabs {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  flex: 1;
  scrollbar-width: none;
}

.nav__tabs::-webkit-scrollbar {
  display: none;
}

.nav__tab {
  padding: 8px 12px;
  border-radius: var(--radius);
  color: var(--color-muted);
  white-space: nowrap;
  font-size: 14px;
}

.nav__tab:hover {
  background: var(--color-bg);
  text-decoration: none;
}

.nav__tab--active {
  color: var(--color-accent);
  background: #eef2ff;
  font-weight: 600;
}

.nav__side {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-shrink: 0;
  position: relative;
}

.nav__settings-link {
  font-size: 14px;
  color: var(--color-muted);
  white-space: nowrap;
}

.nav__user {
  position: relative;
}

.nav__user-toggle {
  background: none;
  border: none;
  font: inherit;
  font-size: 14px;
  color: var(--color-text);
  cursor: pointer;
  padding: 6px 4px;
}

.nav__menu {
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  min-width: 160px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  padding: 6px;
  display: flex;
  flex-direction: column;
}

.nav__menu a,
.nav__logout {
  display: block;
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 14px;
  color: var(--color-text);
  text-align: left;
  width: 100%;
  background: none;
  border: none;
  font: inherit;
  cursor: pointer;
}

.nav__menu a:hover,
.nav__logout:hover {
  background: var(--color-bg);
  text-decoration: none;
}

.nav__logout-form {
  margin: 0;
}

[x-cloak] {
  display: none !important;
}

/* ---------- Контент ---------- */

.content {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 20px 64px;
}

.stub {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 48px 24px;
  text-align: center;
  color: var(--color-muted);
}

.stub__title {
  margin: 0 0 8px;
  color: var(--color-text);
}

.stub__text {
  margin: 0;
}

/* ---------- Форма входа ---------- */

.auth-page {
  display: flex;
  justify-content: center;
  padding-top: 10vh;
}

.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 32px;
}

.auth-card__title {
  margin: 0 0 20px;
  font-size: 20px;
  text-align: center;
}

.form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

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

.form__field input {
  font: inherit;
  font-size: 15px;
  color: var(--color-text);
  padding: 9px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface);
}

.form__field input:focus {
  outline: none;
  border-color: var(--color-accent);
}

.form__error {
  margin: 0;
  padding: 10px 12px;
  border-radius: var(--radius);
  background: var(--color-error-bg);
  color: var(--color-error);
  font-size: 14px;
}

.btn {
  font: inherit;
  font-size: 15px;
  padding: 10px 16px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  cursor: pointer;
}

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

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

/* ---------- Тосты ---------- */

.toast-container {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 360px;
}

.toast {
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--color-text);
  color: #fff;
  font-size: 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.16);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast--visible {
  opacity: 1;
  transform: translateY(0);
}

.toast--error {
  background: var(--color-error);
}

.toast--ok {
  background: var(--color-ok);
}

/* ---------- Настройки ---------- */

.settings {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 24px;
}

.card__title {
  margin: 0 0 16px;
  font-size: 17px;
}

.form--grid {
  max-width: 420px;
}

.form__hint {
  color: var(--color-muted);
  font-size: 12px;
}

.btn--secondary {
  background: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border);
}

.btn--secondary:hover {
  background: var(--color-bg);
}

.settings__help {
  margin: 14px 0 0;
  font-size: 13px;
  color: var(--color-muted);
}

.settings__meta {
  margin: 0 0 8px;
  font-size: 14px;
  color: var(--color-muted);
}

.connection-status {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 10px;
}

#catalog-status {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
}

#catalog-status button {
  margin-top: 6px;
}

.status {
  margin: 0;
  padding: 8px 12px;
  border-radius: var(--radius);
  font-size: 14px;
}

.status--ok {
  background: var(--color-ok-bg);
  color: var(--color-ok);
}

.status--error {
  background: var(--color-error-bg);
  color: var(--color-error);
}

.status--muted {
  color: var(--color-muted);
}

.htmx-indicator {
  opacity: 0;
  font-size: 13px;
  color: var(--color-muted);
  transition: opacity 0.15s ease;
}

.htmx-request.htmx-indicator,
.htmx-request .htmx-indicator {
  opacity: 1;
}

/* ---------- Себестоимость ---------- */

.costs-page {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.costs-header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}

.stat-tiles {
  display: grid;
  grid-template-columns: repeat(4, minmax(120px, 1fr));
  gap: 12px;
  flex: 1;
  min-width: 280px;
}

.stat-tile {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 14px 16px;
  color: inherit;
  display: block;
}

.stat-tile__value {
  font-size: 22px;
  font-weight: 700;
}

.stat-tile__label {
  margin-top: 4px;
  font-size: 12px;
  color: var(--color-muted);
}

.stat-tile--link {
  cursor: pointer;
}

.stat-tile--link:hover {
  border-color: var(--color-accent);
  text-decoration: none;
}

.stat-tile--active {
  border-color: var(--color-accent);
  background: #eef2ff;
}

.costs-header__sync {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  font-size: 13px;
  color: var(--color-muted);
}

.costs-header__sync form {
  margin: 0;
}

.costs-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.costs-filters__search {
  flex: 1;
  min-width: 200px;
  padding: 9px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font: inherit;
  font-size: 14px;
}

.costs-filters__select {
  padding: 9px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font: inherit;
  font-size: 14px;
  background: var(--color-surface);
}

.costs-table-wrap {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  overflow-x: auto;
}

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

.costs-table th {
  text-align: left;
  font-size: 12px;
  color: var(--color-muted);
  font-weight: 600;
  padding: 10px 12px;
  border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
}

.costs-table__toggle-col {
  width: 32px;
}

.costs-row td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
  font-size: 14px;
}

.costs-row-group--no-cost .costs-row {
  box-shadow: inset 3px 0 0 var(--color-error);
}

.costs-row__name {
  max-width: 320px;
}

.costs-row__article {
  color: var(--color-muted);
  font-variant-numeric: tabular-nums;
}

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

.chevron {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 14px;
  color: var(--color-muted);
  transition: transform 0.15s ease;
  padding: 4px;
}

.chevron--open {
  transform: rotate(90deg);
}

.thumb {
  width: 40px;
  height: 40px;
  border-radius: 6px;
  object-fit: cover;
  display: block;
  background: var(--color-bg);
}

.thumb--empty {
  border: 1px dashed var(--color-border);
}

.cost-editor__value {
  background: none;
  border: none;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  color: var(--color-text);
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
}

.cost-editor__value:hover {
  background: var(--color-bg);
}

.cost-editor__value--empty {
  color: var(--color-accent);
  font-weight: 600;
}

.cost-editor__value--readonly {
  cursor: default;
  color: var(--color-text);
}

.cost-editor__value--readonly:hover {
  background: none;
}

.cost-editor__form {
  display: flex;
  align-items: center;
  gap: 6px;
}

.cost-editor__input {
  width: 90px;
  padding: 5px 8px;
  border: 1px solid var(--color-accent);
  border-radius: 6px;
  font: inherit;
  font-size: 14px;
}

.cost-editor__date {
  width: 132px;
  padding: 5px 6px;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  font: inherit;
  font-size: 13px;
}

.cost-editor__submit {
  background: var(--color-accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  width: 28px;
  height: 28px;
  cursor: pointer;
}

.costs-history-row td {
  background: var(--color-bg);
  padding: 12px;
}

.costs-history {
  font-size: 13px;
}

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

.history-table th {
  text-align: left;
  font-size: 12px;
  color: var(--color-muted);
  padding: 4px 8px;
}

.history-table td {
  padding: 6px 8px;
  border-top: 1px solid var(--color-border);
}

.history-row--current td {
  font-weight: 600;
}

.badge {
  display: inline-block;
  margin-left: 6px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--color-ok-bg);
  color: var(--color-ok);
  font-size: 11px;
  font-weight: 600;
}

.costs-history__empty {
  color: var(--color-muted);
  margin: 0;
}

.costs-io {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 14px 16px;
}

.costs-io__actions {
  display: flex;
  gap: 10px;
}

.costs-io__form {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.costs-io__form-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.costs-io__preview {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
}

.admin-table-wrap {
  overflow-x: auto;
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.admin-table th {
  text-align: left;
  font-size: 12px;
  color: var(--color-muted);
  padding: 8px 10px;
  border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
}

.admin-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
}

.admin-inline-form {
  margin: 0;
  display: inline-flex;
}

.admin-inline-form select {
  font: inherit;
  font-size: 13px;
  padding: 4px 6px;
  border: 1px solid var(--color-border);
  border-radius: 6px;
}

.admin-table__actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.import-errors {
  margin: 0;
  padding-left: 18px;
  font-size: 13px;
  color: var(--color-error);
  max-height: 160px;
  overflow-y: auto;
}

/* ---------- Мобильная адаптация ---------- */

@media (max-width: 640px) {
  .nav {
    gap: 12px;
    padding: 0 12px;
  }

  .nav__brand {
    display: none;
  }

  .content {
    padding: 16px 12px 48px;
  }

  .auth-page {
    padding-top: 6vh;
  }

  .stat-tiles {
    grid-template-columns: repeat(2, 1fr);
  }

  .costs-header__sync {
    align-items: flex-start;
    width: 100%;
  }

  /* Таблица себестоимости -> карточки */
  .costs-table thead {
    display: none;
  }

  .costs-table,
  .costs-table tbody,
  .costs-row,
  .costs-row td {
    display: block;
    width: 100%;
  }

  .costs-row-group {
    display: block;
    border-bottom: 1px solid var(--color-border);
    padding: 10px 0;
  }

  .costs-row {
    display: grid;
    grid-template-columns: auto auto 1fr;
    grid-template-areas:
      "toggle photo name"
      "toggle photo article"
      "toggle photo cost"
      "toggle photo changed";
    row-gap: 2px;
    column-gap: 10px;
    border-bottom: none;
  }

  .costs-table__toggle-col {
    grid-area: toggle;
    width: auto;
    padding: 0;
  }

  .costs-row td[data-label="Фото"] {
    grid-area: photo;
    padding: 0;
  }

  .costs-row td[data-label="Название"] {
    grid-area: name;
    font-weight: 600;
    padding: 0;
    max-width: none;
  }

  .costs-row td[data-label="Артикул"] {
    grid-area: article;
    padding: 0;
    font-size: 12px;
  }

  .costs-row td[data-label="Себестоимость"] {
    grid-area: cost;
    padding: 2px 0 0;
  }

  .costs-row td[data-label="Изменена"] {
    grid-area: changed;
    padding: 0;
    font-size: 12px;
    color: var(--color-muted);
  }

  .costs-row td[data-label]::before {
    content: attr(data-label);
    display: block;
    font-size: 11px;
    color: var(--color-muted);
  }

  .costs-row td[data-label="Название"]::before,
  .costs-row td[data-label="Себестоимость"]::before {
    content: "";
    display: none;
  }

  .costs-row-group--no-cost .costs-row {
    box-shadow: none;
    border-left: 3px solid var(--color-error);
    padding-left: 8px;
  }
}
