:root {
  --color-bg: #f4f5f8;
  --color-surface: #ffffff;
  --color-surface-hover: #f9fafb;
  --color-text: #12141c;
  --color-muted: #6b7280;
  --color-muted-2: #9aa1ae;
  --color-border: #e6e8ee;
  --color-border-strong: #d7dae2;
  --color-accent: #4f46e5;
  --color-accent-hover: #4338ca;
  --color-accent-soft: #eef0fe;
  --color-error: #dc2626;
  --color-error-bg: #fef2f2;
  --color-ok: #16a34a;
  --color-ok-bg: #f0fdf4;
  --color-warn: #d97706;
  --color-warn-bg: #fffbeb;
  --radius: 10px;
  --radius-sm: 7px;
  --shadow-xs: 0 1px 2px rgba(16, 24, 40, 0.04);
  --shadow-sm: 0 1px 3px rgba(16, 24, 40, 0.06), 0 1px 2px rgba(16, 24, 40, 0.04);
  --shadow-md: 0 8px 20px rgba(16, 24, 40, 0.08);
  --shadow-lg: 0 16px 40px rgba(16, 24, 40, 0.14);
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

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

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

::selection { background: var(--color-accent-soft); color: var(--color-accent-hover); }

/* --- Навигация --- */
.topnav {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--color-border);
  box-shadow: var(--shadow-xs);
  padding: 0 20px;
  height: 58px;
}

.topnav-left .brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  font-size: 17px;
  letter-spacing: -0.01em;
}

.topnav-left .brand::before {
  content: "";
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 3px;
  background: linear-gradient(135deg, var(--color-accent), #8b7ff0);
  box-shadow: var(--shadow-sm);
}

.topnav-center {
  display: flex;
  gap: 2px;
  flex-wrap: wrap;
  justify-content: center;
  flex: 1;
}

.topnav-center a {
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  color: var(--color-muted);
  font-weight: 500;
  font-size: 14px;
  white-space: nowrap;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.topnav-center a:hover { background: var(--color-surface-hover); color: var(--color-text); text-decoration: none; }

.topnav-center a.active {
  color: var(--color-accent);
  background: var(--color-accent-soft);
  font-weight: 600;
}

.topnav-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.topnav-right > a { color: var(--color-muted); font-size: 14px; font-weight: 500; transition: color 0.15s ease; }
.topnav-right > a:hover { color: var(--color-accent); text-decoration: none; }

.user-menu { position: relative; }

.user-menu-btn {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 7px 14px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  color: var(--color-text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.user-menu-btn:hover { border-color: var(--color-border-strong); box-shadow: var(--shadow-xs); }

.user-menu-list {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  min-width: 180px;
  padding: 6px;
  display: flex;
  flex-direction: column;
}

.user-menu-list a,
.user-menu-list button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  border: none;
  background: none;
  font-family: inherit;
  font-size: 14px;
  cursor: pointer;
  color: var(--color-text);
  transition: background-color 0.15s ease;
}

.user-menu-list a:hover,
.user-menu-list button:hover { background: var(--color-surface-hover); text-decoration: none; }

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

/* --- Разметка страницы --- */
.page {
  max-width: 1080px;
  margin: 0 auto;
  padding: 28px 20px 64px;
}

.page-auth {
  max-width: 380px;
  padding-top: 88px;
}

h1 { font-size: 23px; font-weight: 700; letter-spacing: -0.015em; margin: 0 0 20px; }
h2 { font-size: 15px; font-weight: 600; margin: 0 0 14px; }

/* --- Карточки --- */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-xs);
  border-radius: var(--radius);
  padding: 22px;
  margin-bottom: 20px;
}

.auth-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  padding: 32px;
}
.auth-card h1 { text-align: center; font-size: 19px; margin-bottom: 24px; }

.stub-card {
  background: var(--color-surface);
  border: 1.5px dashed var(--color-border-strong);
  border-radius: var(--radius);
  padding: 56px 20px;
  text-align: center;
}
.stub-card h1 { color: var(--color-muted); }

/* --- Формы --- */
.form { display: flex; flex-direction: column; gap: 14px; }
.form-inline { flex-direction: row; flex-wrap: wrap; align-items: end; gap: 12px; }

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

input[type="text"],
input[type="password"],
input[type="number"],
input[type="date"],
select {
  font-family: inherit;
  font-size: 14px;
  padding: 9px 12px;
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  color: var(--color-text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input:hover, select:hover { border-color: var(--color-muted-2); }

input:focus, select:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px var(--color-accent-soft);
}

.btn {
  align-self: flex-start;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  padding: 9px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border-strong);
  background: var(--color-surface);
  color: var(--color-text);
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease, transform 0.1s ease;
}
.btn:hover { background: var(--color-surface-hover); border-color: var(--color-muted-2); }
.btn:active { transform: translateY(1px); }

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

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

.btn-small { padding: 5px 11px; font-size: 12.5px; }

.inline-form { display: inline-block; margin: 0 6px 6px 0; }

/* --- Уведомления --- */
.alert {
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin-bottom: 12px;
  border: 1px solid transparent;
}
.alert-error { background: var(--color-error-bg); color: var(--color-error); border-color: rgba(220, 38, 38, 0.15); }
.alert-ok { background: var(--color-ok-bg); color: var(--color-ok); border-color: rgba(22, 163, 74, 0.15); }

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

/* --- Таблица --- */
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td {
  text-align: left;
  padding: 11px 12px;
  border-bottom: 1px solid var(--color-border);
}
.table th { color: var(--color-muted); font-weight: 600; font-size: 11.5px; letter-spacing: 0.03em; text-transform: uppercase; }
.table tbody tr { transition: background-color 0.12s ease; }
.table tbody tr:hover { background: var(--color-surface-hover); }
.table-actions { white-space: nowrap; }

/* --- Себестоимость: шапка, плитки, фильтры --- */
.costs-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}
.costs-header h1 { margin-bottom: 4px; }

.tiles {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-bottom: 22px;
}

.tile {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-xs);
  border-radius: var(--radius);
  padding: 16px 18px;
  color: var(--color-text);
  display: block;
  transition: box-shadow 0.15s ease, border-color 0.15s ease, transform 0.15s ease;
}
a.tile:hover { text-decoration: none; border-color: var(--color-accent); box-shadow: var(--shadow-md); transform: translateY(-1px); }

.tile-value { font-size: 24px; font-weight: 700; letter-spacing: -0.02em; }
.tile-label { font-size: 12.5px; color: var(--color-muted); margin-top: 3px; }

.tile-active { border-color: var(--color-accent); box-shadow: 0 0 0 1.5px var(--color-accent); }
.tile-warn .tile-value { color: var(--color-warn); }

.filters-bar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.filters-bar .search-input { flex: 1; min-width: 200px; }
.filters-bar select { min-width: 160px; }

/* --- Себестоимость: таблица товаров --- */
.costs-table .thumb {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-sm);
  object-fit: cover;
  display: block;
  box-shadow: var(--shadow-xs);
}
.costs-table .thumb-empty { background: var(--color-bg); border: 1px solid var(--color-border); }

.name-cell { display: flex; align-items: center; gap: 8px; font-weight: 500; }

.chevron-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--color-muted-2);
  font-size: 12px;
  padding: 4px;
  border-radius: 4px;
  transition: transform 0.15s ease, color 0.15s ease;
}
.chevron-btn:hover { color: var(--color-accent); }
.chevron-btn.chevron-open { transform: rotate(90deg); color: var(--color-accent); }

.row-no-cost td:first-child { border-left: 3px solid var(--color-warn); }

.cost-cell { position: relative; min-width: 140px; font-variant-numeric: tabular-nums; }
.cost-value { cursor: pointer; border-radius: 4px; padding: 2px 4px; margin: -2px -4px; transition: background-color 0.12s ease; }
.cost-value:hover { background: var(--color-accent-soft); text-decoration: none; }

.cost-edit-form { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.cost-input { width: 92px; padding: 6px 8px; }
.cost-date-input { width: 142px; padding: 6px 8px; font-size: 13px; }

.history-row { display: none; }
.history-row.history-open { display: table-row; }
.history-row td { background: var(--color-surface-hover); }
.history-box { padding: 10px 4px; }

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

.empty-state { text-align: center; color: var(--color-muted); padding: 40px 0 !important; }

/* --- Импорт CSV --- */
.import-preview { margin-top: 14px; }
.import-preview p { margin: 6px 0; }
.import-preview code {
  background: var(--color-surface-hover);
  border: 1px solid var(--color-border);
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 12.5px;
}

/* --- Тосты --- */
#toast-container {
  position: fixed;
  right: 20px;
  bottom: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 100;
}

.toast {
  background: #1a1d27;
  color: #fff;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  max-width: 320px;
  box-shadow: var(--shadow-lg);
  animation: toast-in 0.2s ease;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* --- Мобильная адаптация --- */
@media (max-width: 720px) {
  .topnav { flex-wrap: wrap; height: auto; padding: 10px 14px; }
  .topnav-center {
    order: 3;
    flex: 0 0 100%;
    justify-content: flex-start;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .topnav-center a { flex-shrink: 0; }
  .page { padding: 16px 14px 40px; }
  .form-inline { flex-direction: column; align-items: stretch; }
  .table-actions { display: flex; flex-direction: column; gap: 6px; }
  .inline-form { margin: 0; }

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

  .costs-table thead { display: none; }
  .costs-table, .costs-table tbody { display: block; width: 100%; }
  .costs-table tr.cost-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 12px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    box-shadow: var(--shadow-xs);
    border-radius: var(--radius);
    margin-bottom: 10px;
    padding: 12px;
  }
  .costs-table tr.cost-row td { display: block; border: none; padding: 0; }
  .costs-table tr.cost-row td[data-label]::before {
    content: attr(data-label);
    display: block;
    font-size: 11px;
    color: var(--color-muted);
  }
  .costs-table tr.cost-row td[data-label="Фото"] { order: -1; }
  .costs-table tr.cost-row td[data-label="Название"] { flex: 1 1 100%; }
  .row-no-cost td:first-child { border-left: none; }
  .row-no-cost.cost-row { border-left: 3px solid var(--color-warn); }

  .history-row.history-open { display: block; }
  .history-row td { display: block; border: none; padding: 4px 0 10px; background: transparent; }
}
