:root {
  --color-bg: #f4f5f7;
  --color-surface: #ffffff;
  --color-text: #1a1d23;
  --color-muted: #6b7280;
  --color-accent: #2563eb;
  --color-error: #dc2626;
  --color-ok: #16a34a;
  --radius: 10px;
  --font-sans: -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);
}

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

.topnav {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 20px;
  background: var(--color-surface);
  border-bottom: 1px solid #e5e7eb;
  flex-wrap: wrap;
}

.brand { font-weight: 700; color: var(--color-text); font-size: 1.1rem; }

.topnav-center { display: flex; gap: 4px; flex-wrap: wrap; }

.navlink {
  color: var(--color-muted);
  padding: 8px 12px;
  border-radius: var(--radius);
  font-size: 0.92rem;
}
.navlink:hover { background: var(--color-bg); text-decoration: none; }
.navlink.active { color: var(--color-accent); font-weight: 600; background: #eef2ff; }

.topnav-right { display: flex; align-items: center; gap: 8px; position: relative; }

.user-menu { position: relative; }
.user-menu-btn {
  background: none; border: none; cursor: pointer;
  font-size: 0.92rem; color: var(--color-text); padding: 8px 10px; border-radius: var(--radius);
}
.user-menu-btn:hover { background: var(--color-bg); }
.user-menu-dropdown {
  position: absolute; right: 0; top: calc(100% + 4px);
  background: var(--color-surface); border: 1px solid #e5e7eb; border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,.08); min-width: 160px; padding: 6px; display: flex; flex-direction: column;
}
.user-menu-dropdown a, .user-menu-dropdown .link-button {
  padding: 8px 10px; border-radius: 6px; text-align: left; color: var(--color-text);
}
.user-menu-dropdown a:hover, .user-menu-dropdown .link-button:hover { background: var(--color-bg); text-decoration: none; }

.link-button {
  background: none; border: none; cursor: pointer; font: inherit; width: 100%;
}

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

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

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

.page-header { position: relative; }
.back-link {
  position: absolute; left: -56px; top: 50%; transform: translateY(-50%);
  display: flex; align-items: center; justify-content: center;
  width: 48px; height: 48px; border-radius: var(--radius);
  color: var(--color-text); font-size: 1.8rem; line-height: 1;
}
.back-link:hover { background: var(--color-bg); text-decoration: none; }

@media (max-width: 900px) {
  /* Мало места слева — стрелка возвращается в поток перед заголовком. */
  .page-header { display: flex; align-items: center; gap: 8px; }
  .back-link { position: static; transform: none; }
}

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

.login-wrap { display: flex; justify-content: center; padding-top: 10vh; }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; }
.login-card h1 { margin: 0 0 6px; font-size: 1.3rem; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 0.9rem; color: var(--color-muted); }
.login-card input {
  padding: 10px 12px; border: 1px solid #d1d5db; border-radius: var(--radius); font: inherit;
}

.btn {
  padding: 10px 16px; border-radius: var(--radius); border: none; cursor: pointer; font: inherit; font-weight: 600;
}
.btn-primary { background: var(--color-accent); color: #fff; }
.btn-primary:hover { opacity: 0.92; }

.form-error { color: var(--color-error); font-size: 0.9rem; margin: 0; }

.settings-block { margin-bottom: 20px; }
.settings-block h2 { margin-top: 0; font-size: 1.05rem; }

.form-grid { display: flex; flex-direction: column; gap: 12px; max-width: 420px; }
.form-grid label { display: flex; flex-direction: column; gap: 6px; font-size: 0.9rem; color: var(--color-muted); }
.form-grid input {
  padding: 9px 12px; border: 1px solid #d1d5db; border-radius: var(--radius); font: inherit; color: var(--color-text);
}
.form-grid input:disabled { background: var(--color-bg); color: var(--color-muted); }
.hint { font-size: 0.78rem; color: var(--color-muted); }

.btn:not(.btn-primary) { background: var(--color-surface); border: 1px solid #d1d5db; color: var(--color-text); }
.btn:not(.btn-primary):hover { background: var(--color-bg); }

.check-connection-row { display: flex; align-items: center; gap: 10px; margin-top: 12px; }

.htmx-indicator { opacity: 0; transition: opacity .15s; font-size: 0.85rem; color: var(--color-muted); }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { opacity: 1; }

.text-ok { color: var(--color-ok); font-weight: 600; }
.text-error { color: var(--color-error); font-weight: 600; }

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

.stats-row { display: flex; gap: 12px; flex-wrap: wrap; margin: 16px 0; }
.stat-tile {
  flex: 1 1 160px; background: var(--color-surface); border: 1px solid #e5e7eb; border-radius: var(--radius);
  padding: 14px 16px; text-align: left; color: inherit;
}
.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; }
.stat-value { font-size: 1.5rem; font-weight: 700; }
.stat-label { font-size: 0.82rem; color: var(--color-muted); margin-top: 2px; }

.sync-row { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }

.filters-row { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.filters-row select, .search-input {
  padding: 8px 10px; border: 1px solid #d1d5db; border-radius: var(--radius); font: inherit; background: var(--color-surface);
}
.search-input { flex: 1 1 240px; min-width: 200px; }

.table-wrap { background: var(--color-surface); border: 1px solid #e5e7eb; border-radius: var(--radius); overflow-x: auto; }
.costs-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.costs-table th { text-align: left; padding: 10px 12px; border-bottom: 1px solid #e5e7eb; color: var(--color-muted); font-weight: 600; font-size: 0.8rem; white-space: nowrap; }
.costs-table td { padding: 8px 12px; border-bottom: 1px solid #f1f2f4; vertical-align: middle; }
.cost-row.no-cost { box-shadow: inset 3px 0 0 var(--color-accent); }
.col-photo { width: 56px; }
.col-chevron { width: 32px; text-align: right; }

.thumb { width: 44px; height: 44px; border-radius: 6px; object-fit: cover; display: block; }
.thumb-placeholder { background: #e5e7eb; }

.cost-value { cursor: pointer; padding: 4px 6px; border-radius: 6px; }
.cost-value:hover { background: var(--color-bg); }
.cost-edit-form { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.cost-input { width: 100px; padding: 6px 8px; border: 1px solid #d1d5db; border-radius: 6px; font: inherit; }
.date-input { padding: 6px 8px; border: 1px solid #d1d5db; border-radius: 6px; font: inherit; }
.btn-small { padding: 5px 9px; font-size: 0.82rem; }

.chevron-btn {
  background: none; border: none; cursor: pointer; font-size: 0.9rem; color: var(--color-muted); transition: transform .15s;
}
.chevron-btn.open { transform: rotate(180deg); }

.history-row { display: none; background: var(--color-bg); }
.history-row.open { display: table-row; }
.history-content { padding: 8px 4px; }
.history-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.history-table th, .history-table td { padding: 6px 10px; text-align: left; }
.history-current { font-weight: 600; }
.badge { font-size: 0.72rem; background: #dcfce7; color: var(--color-ok); padding: 2px 6px; border-radius: 999px; }

.empty-state { text-align: center; padding: 30px; }

.import-block {
  display: none; background: var(--color-surface); border: 1px solid #e5e7eb; border-radius: var(--radius);
  padding: 16px; margin-bottom: 16px;
}
.import-block.open { display: block; }
.import-block form { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.import-preview { margin-top: 12px; }
.import-errors { font-size: 0.85rem; color: var(--color-error); max-height: 200px; overflow-y: auto; padding-left: 18px; }

.admin-actions { display: flex; gap: 6px; }
.admin-actions form { display: inline; }

@media (max-width: 640px) {
  .costs-table thead { display: none; }
  .costs-table, .costs-table tbody, .costs-table tr, .costs-table td { display: block; width: 100%; }
  .costs-table tr.cost-row { border: 1px solid #e5e7eb; border-radius: var(--radius); margin-bottom: 10px; padding: 8px; }
  .costs-table td { border: none; padding: 4px 6px; }
  .col-photo, .col-name, .col-offer, .col-cost, .col-updated, .col-chevron { width: auto; }
}

#toast-container {
  position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 100;
}
.toast {
  background: var(--color-error); color: #fff; padding: 10px 16px; border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,.15); max-width: 320px; font-size: 0.9rem;
}

@media (max-width: 640px) {
  .topnav { flex-direction: column; align-items: stretch; }
  .topnav-center { justify-content: center; }
  .topnav-right { justify-content: center; }
  .page { padding: 16px 12px 40px; }
}
