:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --ink: #17212b;
  --muted: #64748b;
  --line: #d9e0e8;
  --line-strong: #bdc7d3;
  --nav: #102033;
  --nav-soft: #1d334c;
  --blue: #2563eb;
  --green: #0f8a5f;
  --red: #c2410c;
  --amber: #a16207;
  --violet: #6d28d9;
  --input: #f8fafc;
  --shadow: 0 1px 2px rgba(15, 23, 42, .08);
}

* {
  box-sizing: border-box;
}

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

button,
input,
select {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: 236px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  background: var(--nav);
  color: #e8eef6;
  padding: 18px 14px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 6px 18px;
  border-bottom: 1px solid rgba(255,255,255,.12);
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 6px;
  background: #e8eef6;
  color: var(--nav);
  font-weight: 800;
}

.brand strong,
.brand span {
  display: block;
}

.brand span {
  color: #9fb0c3;
  margin-top: 2px;
  font-size: 12px;
}

.nav {
  display: grid;
  gap: 6px;
  margin-top: 18px;
}

.nav-item {
  width: 100%;
  border: 0;
  border-radius: 6px;
  padding: 10px 12px;
  text-align: left;
  color: #d9e4ef;
  background: transparent;
  cursor: pointer;
}

.nav-item:hover,
.nav-item.active {
  background: var(--nav-soft);
  color: #fff;
}

.sidebar-status {
  margin-top: 22px;
  padding: 10px;
  border-radius: 6px;
  color: #bed0e4;
  background: rgba(255,255,255,.06);
  font-size: 12px;
  line-height: 1.35;
}

.logout-form {
  margin-top: 10px;
}

.logout-button {
  color: #bed0e4;
}

.main {
  min-width: 0;
  padding: 22px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: center;
  margin-bottom: 16px;
}

h1 {
  margin: 0;
  font-size: 24px;
  letter-spacing: 0;
}

.muted {
  color: var(--muted);
  margin-top: 4px;
}

.top-actions,
.toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.button {
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  min-height: 36px;
  padding: 0 13px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  box-shadow: var(--shadow);
}

.button:hover {
  border-color: #94a3b8;
}

.button.primary {
  border-color: #1d4ed8;
  background: var(--blue);
  color: #fff;
}

.button.secondary {
  background: #fff;
}

.button.small {
  min-height: 30px;
  padding: 0 9px;
  font-size: 12px;
}

.switch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  box-shadow: var(--shadow);
}

.switch input {
  width: 16px;
  height: 16px;
}

.metrics {
  display: grid;
  grid-template-columns: repeat(5, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}

.metric {
  min-height: 76px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  padding: 12px;
  box-shadow: var(--shadow);
}

.metric span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.metric strong {
  display: block;
  margin-top: 6px;
  font-size: 22px;
}

.toolbar {
  margin-bottom: 12px;
}

.search,
select {
  height: 38px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--input);
  color: var(--ink);
  padding: 0 11px;
}

.search {
  width: min(420px, 100%);
}

.table-wrap {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  overflow: auto;
  box-shadow: var(--shadow);
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 1240px;
}

.product-table {
  table-layout: fixed;
  min-width: 1180px;
}

.product-table th:nth-child(1),
.product-table td:nth-child(1) {
  width: 24%;
}

.product-table th:nth-child(2),
.product-table td:nth-child(2) {
  width: 18%;
}

.product-table th:nth-child(3),
.product-table td:nth-child(3) {
  width: 20%;
}

.product-table th:nth-child(4),
.product-table td:nth-child(4) {
  width: 12%;
}

.product-table th:nth-child(5),
.product-table td:nth-child(5) {
  width: 15%;
}

.product-table th:nth-child(6),
.product-table td:nth-child(6) {
  width: 11%;
}

.stock-table {
  min-width: 1280px;
}

.stock-table th:nth-child(3),
.stock-table td:nth-child(3) {
  min-width: 780px;
}

.purchase-table {
  min-width: 1120px;
  table-layout: fixed;
}

.purchase-table th:nth-child(1),
.purchase-table td:nth-child(1) {
  width: 30%;
}

.purchase-table th:nth-child(2),
.purchase-table td:nth-child(2) {
  width: 22%;
}

.purchase-table th:nth-child(3),
.purchase-table td:nth-child(3),
.purchase-table th:nth-child(4),
.purchase-table td:nth-child(4),
.purchase-table th:nth-child(5),
.purchase-table td:nth-child(5) {
  width: 12%;
}

.purchase-table th:nth-child(6),
.purchase-table td:nth-child(6) {
  width: 12%;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 8px 9px;
  vertical-align: middle;
  text-align: left;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #e9eef5;
  color: #263445;
  font-size: 12px;
  font-weight: 700;
}

tbody tr:hover {
  background: #f8fbff;
}

.sku {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}

.name-cell {
  min-width: 220px;
}

.name-cell strong {
  display: block;
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-link {
  color: inherit;
  text-decoration: none;
}

.product-link:hover {
  color: #1f5f9f;
  text-decoration: underline;
}

.stock-table .name-cell {
  min-width: 280px;
}

.stock-table .name-cell strong {
  max-width: 420px;
}

.row-sub {
  color: var(--muted);
  font-size: 12px;
  margin-top: 2px;
}

.price-cell {
  min-width: 176px;
}

.price-field {
  display: grid;
  grid-template-columns: 44px minmax(92px, 118px);
  align-items: center;
  column-gap: 8px;
}

.price-field + .price-field {
  margin-top: 7px;
}

.price-field span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.price-field .cell-input {
  width: 100%;
}

.sale-discount {
  min-height: 16px;
  margin: 5px 0 0 52px;
  color: #075f46;
  font-size: 12px;
  font-weight: 700;
}

.discount-cell {
  min-width: 138px;
}

.discount-list {
  display: grid;
  gap: 5px;
}

.discount-list-single {
  margin-bottom: 5px;
}

.discount-tier {
  display: grid;
  grid-template-columns: 28px minmax(54px, 1fr) 44px;
  align-items: center;
  gap: 6px;
  min-height: 26px;
  padding: 3px 6px;
  border: 1px solid #cfe8dc;
  border-radius: 5px;
  background: #f0fdf6;
  color: #075f46;
  font-size: 12px;
}

.discount-tier-base {
  border-color: #cbd5e1;
  background: #f8fafc;
  color: #334155;
}

.discount-tier-min {
  border-color: #f4d78c;
  background: #fff8e6;
  color: #8a3b04;
}

.discount-tier span,
.discount-tier small {
  color: inherit;
  font-weight: 700;
}

.discount-tier strong {
  color: var(--ink);
  font-size: 12px;
}

.discount-empty {
  max-width: 120px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.3;
}

.purchase-field {
  display: grid;
  grid-template-columns: 88px minmax(0, 1fr);
  align-items: center;
  gap: 8px;
}

.purchase-source {
  min-width: 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.25;
}

.purchase-source a {
  display: block;
  color: #1f5f9f;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.purchase-table a {
  color: #1f5f9f;
  font-weight: 700;
}

.preview-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  min-width: 210px;
}

.readonly-value {
  display: inline-flex;
  align-items: center;
  min-width: 88px;
  min-height: 30px;
  font-weight: 700;
}

.cell-input,
.cell-select {
  width: 88px;
  height: 30px;
  border: 1px solid var(--line-strong);
  border-radius: 5px;
  background: var(--input);
  padding: 0 7px;
}

.cell-select {
  width: 104px;
}

.cell-input.wide,
.cell-select.wide {
  width: 138px;
}

.cell-input.mini {
  width: 70px;
}

.url-input {
  width: 260px;
}

.note-input {
  width: 180px;
}

.flags {
  display: grid;
  grid-template-columns: repeat(2, 64px);
  gap: 5px;
}

.flag {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 5px;
  padding: 4px 6px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #f8fafc;
  font-size: 12px;
}

.flag.one-line {
  display: inline-flex;
  min-width: 62px;
}

.flag input {
  margin: 0;
}

.source-list {
  display: grid;
  gap: 8px;
}

.source-row {
  display: flex;
  align-items: center;
  gap: 7px;
  flex-wrap: wrap;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #f8fafc;
}

.source-row.single-source {
  background: transparent;
  border-color: transparent;
  padding: 0;
}

.source-role {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-width: 82px;
  font-size: 12px;
  font-weight: 700;
}

.source-role input {
  margin: 0;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border-radius: 5px;
  padding: 0 7px;
  font-size: 12px;
  font-weight: 700;
}

.status-ok {
  color: #075f46;
  background: #dff7ec;
}

.status-risk,
.status-blocked {
  color: #8a3b04;
  background: #fff0cf;
}

.status-draft {
  color: #4c1d95;
  background: #eee7ff;
}

.status-empty {
  color: var(--muted);
  background: #eef2f7;
}

.backup-toggle {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 34px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #f8fafc;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.backup-toggle input {
  margin: 0;
}

.margin-ok {
  color: var(--green);
  font-weight: 700;
}

.margin-risk {
  color: var(--red);
  font-weight: 700;
}

.settings-panel {
  display: grid;
  gap: 14px;
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(260px, 1fr));
  gap: 14px;
}

.settings-section {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.settings-section h2 {
  margin: 0;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  background: #e9eef5;
  color: #263445;
  font-size: 14px;
}

.settings-fields {
  display: grid;
  gap: 0;
}

.setting-field {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 160px;
  gap: 14px;
  align-items: center;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}

.setting-field:last-child {
  border-bottom: 0;
}

.setting-field strong,
.setting-field small {
  display: block;
}

.setting-field small {
  color: var(--muted);
  margin-top: 3px;
  font-size: 12px;
}

.setting-field input {
  width: 100%;
  height: 34px;
  border: 1px solid var(--line-strong);
  border-radius: 5px;
  background: var(--input);
  color: var(--ink);
  padding: 0 8px;
}

.setting-toggle input {
  justify-self: end;
  width: 18px;
  height: 18px;
}

.settings-actions {
  display: flex;
  justify-content: flex-end;
}

.login-body {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 22px;
  background: var(--bg);
}

.login-shell {
  width: min(100%, 380px);
}

.login-card {
  display: grid;
  gap: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 22px;
  box-shadow: 0 14px 32px rgba(15, 23, 42, .12);
}

.login-brand {
  padding: 0 0 16px;
  border-bottom-color: var(--line);
  color: var(--ink);
}

.login-brand .brand-mark {
  background: var(--nav);
  color: #fff;
}

.login-brand span {
  color: var(--muted);
}

.login-field {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-weight: 700;
}

.login-field input {
  width: 100%;
  height: 42px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--input);
  color: var(--ink);
  padding: 0 11px;
  font-weight: 500;
}

.login-error {
  border: 1px solid #fed7aa;
  border-radius: 6px;
  background: #fff7ed;
  color: var(--red);
  padding: 9px 10px;
  font-weight: 700;
}

.login-button {
  width: 100%;
}

.hidden {
  display: none;
}

.toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  max-width: 420px;
  padding: 12px 14px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: #fff;
  box-shadow: 0 12px 28px rgba(15, 23, 42, .16);
  color: var(--ink);
  opacity: 0;
  transform: translateY(12px);
  transition: .18s ease;
  pointer-events: none;
}

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

@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .metrics {
    grid-template-columns: repeat(2, minmax(140px, 1fr));
  }

  .settings-grid,
  .setting-field {
    grid-template-columns: 1fr;
  }
}
