:root {
  --bg: #f4f6f9;
  --surface: #fff;
  --surface-soft: #f8fafc;
  --line: #dfe5ee;
  --line-soft: #edf1f6;
  --text: #182230;
  --muted: #667085;
  --muted-2: #98a2b3;
  --primary: #206bc4;
  --primary-dark: #1554a1;
  --primary-soft: #eaf2ff;
  --success: #0f8a5f;
  --danger: #c93243;
  --danger-soft: #fff0f2;
  --sidebar: #182433;
  --sidebar-2: #202f42;
  --shadow: 0 14px 34px rgba(21, 33, 54, .08);
}

* { box-sizing: border-box; }

html { min-width: 320px; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
}

a { color: inherit; text-decoration: none; }

.app {
  min-height: 100vh;
  display: flex;
  background:
    linear-gradient(180deg, rgba(255,255,255,.75), rgba(255,255,255,0) 260px),
    var(--bg);
}

.sidebar {
  width: 252px;
  background: var(--sidebar);
  color: #e6edf7;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  border-right: 1px solid rgba(255,255,255,.08);
  box-shadow: 12px 0 24px rgba(18, 36, 56, .10);
}

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

.brand-mark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  border-radius: 8px;
  background: #2b7de0;
  color: #fff;
  font-weight: 800;
  box-shadow: inset 0 -1px 0 rgba(0,0,0,.12);
}

.brand strong {
  display: block;
  font-size: 17px;
  letter-spacing: 0;
}

.brand small,
.account small {
  display: block;
  margin-top: 2px;
  color: #9fb0c5;
  font-size: 12px;
}

.sidebar nav {
  padding: 14px 12px 16px;
  display: grid;
  gap: 4px;
  overflow-y: auto;
}

.nav-group {
  margin: 14px 10px 6px;
  color: #8ea0b7;
  font-size: 12px;
  font-weight: 700;
}

.nav-group:first-child { margin-top: 4px; }

.sidebar nav a {
  min-height: 42px;
  padding: 9px 10px;
  border-radius: 7px;
  color: #cbd6e2;
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 650;
}

.sidebar nav a:hover { background: rgba(255,255,255,.07); color: #fff; }

.sidebar nav a.active {
  background: #edf5ff;
  color: #113b68;
  box-shadow: 0 8px 20px rgba(0,0,0,.18);
}

.nav-icon {
  width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  border-radius: 6px;
  background: rgba(255,255,255,.09);
  color: inherit;
  font-size: 12px;
  font-weight: 800;
}

.active .nav-icon { background: #d8eaff; color: var(--primary); }

.account {
  margin-top: auto;
  padding: 14px 16px;
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  color: #d5deea;
  border-top: 1px solid rgba(255,255,255,.08);
  background: var(--sidebar-2);
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: #344b66;
  color: #fff;
  font-weight: 800;
}

.account strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #fff;
}

.account a {
  height: 30px;
  padding: 0 10px;
  display: inline-flex;
  align-items: center;
  border: 1px solid rgba(255,255,255,.15);
  border-radius: 6px;
  color: #dfe9f6;
  font-size: 12px;
  font-weight: 700;
}

.main {
  flex: 1;
  min-width: 0;
  padding: 22px 24px 34px;
}

.main.public {
  display: grid;
  place-items: center;
  padding: 24px;
}

.topbar {
  min-height: 62px;
  margin: -22px -24px 22px;
  padding: 12px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: rgba(255,255,255,.92);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 1px 0 rgba(16, 24, 40, .02);
}

.topbar strong {
  display: block;
  margin-top: 2px;
  font-size: 18px;
}

.crumb,
.topbar-meta {
  color: var(--muted);
  font-size: 12px;
}

.page-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin: 0 0 14px;
}

h1 {
  margin: 0;
  font-size: 22px;
  line-height: 1.25;
  font-weight: 750;
  letter-spacing: 0;
}

h2 {
  margin: 0 0 13px;
  font-size: 16px;
  line-height: 1.25;
  font-weight: 750;
}

.auth-panel {
  width: min(430px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 30px;
  box-shadow: var(--shadow);
}

.auth-panel h1 { font-size: 24px; }
.auth-panel p { color: var(--muted); margin: 8px 0 22px; }

.form,
.entry-form,
.filters,
.stack-form,
.bill-form {
  display: grid;
  gap: 12px;
}

.entry-form {
  grid-template-columns: repeat(4, minmax(150px, 1fr));
  align-items: end;
}

.entry-form .wide { grid-column: span 3; }
.stack-form { grid-template-columns: 1fr; }
.bill-form { gap: 14px; }

.bill-head {
  display: grid;
  grid-template-columns: minmax(150px, 190px) minmax(260px, 1.2fr) minmax(220px, 1fr);
  gap: 12px;
  align-items: end;
}

.line-table {
  overflow: visible;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
}

#sale-lines {
  display: grid;
  gap: 0;
}

.line-head,
.line-row {
  display: grid;
  grid-template-columns: 160px minmax(260px, 2fr) 88px 176px 110px minmax(150px, 1fr) 72px;
  column-gap: 8px;
  min-width: 1120px;
}

.bill-form {
  overflow-x: auto;
  overflow-y: visible;
  padding-bottom: 2px;
}

.bill-form .line-table {
  min-width: 1120px;
}

.line-head {
  align-items: center;
  min-height: 38px;
  padding: 0 10px;
  color: #475467;
  font-size: 12px;
  font-weight: 750;
  background: #eef3f9;
  border-bottom: 1px solid var(--line);
}

.line-row {
  padding: 10px;
  align-items: start;
  background: var(--surface);
  border-bottom: 1px solid var(--line-soft);
}

.line-row:last-child { border-bottom: 0; }

.line-money {
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f8fafc;
  color: #1f2a3d;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
}

.part-picker {
  position: relative;
  min-width: 0;
}

.filter-picker {
  position: relative;
  min-width: 0;
}

.part-results,
.filter-results {
  position: fixed;
  z-index: 20;
  display: none;
  max-height: 280px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  box-shadow: 0 12px 28px rgba(17, 24, 39, .16);
}

.part-results.open,
.filter-results.open { display: block; }

.part-result {
  width: 100%;
  min-height: 50px;
  padding: 8px 10px;
  border: 0;
  border-bottom: 1px solid var(--line-soft);
  background: #fff;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.part-result:hover { background: #edf5ff; }

.part-result strong,
.part-result span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.part-result strong {
  font-size: 13px;
  font-weight: 750;
}

.part-result span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.part-empty {
  padding: 10px;
  color: var(--muted);
  font-size: 12px;
}

.price-picker {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 64px;
  gap: 6px;
  min-width: 0;
}

.price-toggle {
  height: 36px;
  padding: 0 8px;
  border: 1px solid #b9d7ff;
  border-radius: 6px;
  background: var(--primary-soft);
  color: #174f92;
  cursor: pointer;
  font-size: 12px;
  font-weight: 750;
  white-space: nowrap;
}

.price-toggle:hover {
  border-color: var(--primary);
  background: #dcecff;
}

.price-options {
  position: fixed;
  z-index: 22;
  display: none;
  max-height: 260px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  box-shadow: 0 12px 28px rgba(17, 24, 39, .16);
}

.price-options.open { display: block; }

.price-option {
  width: 100%;
  min-height: 46px;
  padding: 8px 10px;
  border: 0;
  border-bottom: 1px solid var(--line-soft);
  background: #fff;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.price-option:hover { background: #edf5ff; }

.price-option strong,
.price-option span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.price-option strong {
  font-size: 14px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.price-option span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.price-empty {
  padding: 10px;
  color: var(--muted);
  font-size: 12px;
}

.bill-summary {
  display: grid;
  grid-template-columns: repeat(2, minmax(140px, 180px)) minmax(260px, 1fr);
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.bill-summary > div:not(.actions) {
  display: grid;
  gap: 2px;
}

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

.bill-summary strong {
  font-size: 18px;
  font-weight: 850;
  font-variant-numeric: tabular-nums;
}

.bill-summary .actions {
  justify-content: flex-end;
}

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

.panel-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.toggle-panel {
  display: none;
  padding-top: 12px;
  border-top: 1px solid var(--line-soft);
}

.toggle-panel.open {
  display: grid;
}

.link {
  color: var(--primary);
  font-weight: 750;
}

.link:hover { color: var(--primary-dark); text-decoration: underline; }

.button-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 34px;
  padding: 0 13px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-weight: 750;
  box-shadow: 0 1px 2px rgba(16, 24, 40, .04);
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(180px, 1fr));
  gap: 12px;
}

.detail-grid div {
  display: grid;
  gap: 5px;
  padding: 12px;
  border: 1px solid var(--line-soft);
  border-radius: 7px;
  background: var(--surface-soft);
}

.detail-grid span { color: var(--muted); font-size: 12px; }
.detail-grid strong { font-size: 15px; overflow-wrap: anywhere; }
.detail-grid .wide { grid-column: 1 / -1; }

.filters {
  grid-template-columns: repeat(4, minmax(150px, 240px));
  align-items: end;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  margin-bottom: 14px;
  box-shadow: 0 1px 2px rgba(16, 24, 40, .03);
}

.filters.compact {
  grid-template-columns: minmax(170px, 260px) minmax(220px, 1fr) auto;
}

label {
  display: grid;
  gap: 6px;
  min-width: 0;
  color: #4a5568;
  font-size: 12px;
  font-weight: 700;
}

input,
select {
  width: 100%;
  height: 36px;
  min-width: 0;
  border: 1px solid #cfd8e3;
  border-radius: 6px;
  padding: 0 10px;
  color: var(--text);
  font: inherit;
  font-weight: 500;
  background: #fff;
  outline: none;
  box-shadow: inset 0 1px 1px rgba(16, 24, 40, .03);
}

select {
  padding-right: 28px;
}

input::placeholder { color: #98a2b3; }

input:focus,
select:focus {
  border-color: #7fb2f0;
  box-shadow: 0 0 0 3px rgba(32, 107, 196, .13);
}

button.primary,
button.secondary,
button.small {
  height: 36px;
  border-radius: 6px;
  font: inherit;
  font-weight: 750;
  cursor: pointer;
  padding: 0 15px;
  white-space: nowrap;
}

button.primary {
  border: 1px solid var(--primary);
  background: var(--primary);
  color: #fff;
  box-shadow: 0 2px 5px rgba(32, 107, 196, .22);
}

button.primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }

button.secondary,
button.small {
  border: 1px solid var(--line);
  background: #fff;
  color: #253244;
}

button.secondary:hover,
button.small:hover { background: #f8fafc; border-color: #b9c5d4; }

button.small {
  padding: 0 10px;
  font-size: 12px;
}

button.danger {
  color: var(--danger);
  border-color: #f0bec6;
  background: var(--danger-soft);
}

button.danger:hover { background: #ffe4e8; border-color: #e79aa6; }

.switch { margin-top: 18px; color: var(--primary); font-weight: 700; }

.alert,
.notice {
  padding: 11px 13px;
  border-radius: 7px;
  margin-bottom: 14px;
  font-weight: 650;
}

.alert { background: var(--danger-soft); color: var(--danger); border: 1px solid #f0bec6; }
.notice { background: #edfdf7; color: var(--success); border: 1px solid #bcebd8; }

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

.metrics div,
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  box-shadow: 0 1px 2px rgba(16, 24, 40, .04);
}

.metrics div {
  position: relative;
  overflow: hidden;
}

.metrics div::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 4px;
  background: var(--primary);
}

.metrics span {
  color: var(--muted);
  display: block;
  margin-bottom: 9px;
  font-size: 12px;
  font-weight: 700;
}

.metrics strong {
  font-size: 26px;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

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

.split.wide-left {
  grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
  align-items: start;
}

.big {
  font-size: 28px;
  font-weight: 800;
  margin: 0;
  color: var(--success);
  font-variant-numeric: tabular-nums;
}

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

.pager {
  margin-top: 12px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
}

.panel .table-wrap {
  margin: 0 -1px -1px;
}

table {
  width: 100%;
  min-width: 980px;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 13px;
}

.inventory-table {
  table-layout: fixed;
}

.inventory-table th:nth-child(1) { width: 130px; }
.inventory-table th:nth-child(2) { width: 35%; }
.inventory-table th:nth-child(3) { width: 110px; }
.inventory-table th:nth-child(4) { width: 150px; }
.inventory-table th:nth-child(5) { width: 150px; }
.inventory-table th:nth-child(6) { width: 120px; }
.inventory-table th:nth-child(7) { width: 100px; }

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

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #f1f5f9;
  color: #475467;
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

td {
  color: #27364a;
  overflow-wrap: anywhere;
}

td.num {
  text-align: right;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.stock-cell {
  display: grid;
  grid-template-columns: minmax(84px, 1fr) minmax(28px, auto);
  gap: 6px;
  align-items: baseline;
}

.stock-qty {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.stock-unit {
  text-align: left;
  color: var(--muted);
}

tbody tr:nth-child(even) { background: #fbfcfe; }
tbody tr:hover { background: #edf5ff; }
tbody tr:last-child td { border-bottom: 0; }

@media (max-width: 980px) {
  .app { display: block; }
  .sidebar {
    position: relative;
    width: auto;
    height: auto;
    box-shadow: none;
  }
  .brand { min-height: 64px; }
  .sidebar nav {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    padding: 10px;
  }
  .nav-group {
    grid-column: 1 / -1;
    margin-top: 10px;
  }
  .account { grid-template-columns: 36px minmax(0, 1fr) auto; }
  .main { padding: 18px; }
  .topbar {
    margin: -18px -18px 18px;
    padding: 12px 18px;
  }
  .metrics,
  .split,
  .split.wide-left,
  .filters,
  .filters.compact,
  .entry-form,
  .bill-head,
  .bill-summary,
  .detail-grid {
    grid-template-columns: 1fr;
  }
  .bill-summary .actions { justify-content: flex-start; }
  .entry-form .wide { grid-column: auto; }
}

@media (max-width: 560px) {
  .main { padding: 14px; }
  .topbar {
    margin: -14px -14px 14px;
    align-items: flex-start;
    flex-direction: column;
    gap: 4px;
  }
  .page-head { align-items: flex-start; flex-direction: column; }
  .auth-panel { padding: 22px; }
}
