:root {
  --bg: #f6f7f9;
  --surface: #fff;
  --text: #1d2330;
  --muted: #6b7280;
  --line: #e3e6eb;
  --accent: #1f5fd6;
  --accent-dark: #194fb3;
  --danger: #c0392b;
  --ebay: #e8f0fe;
  --ai: #f1eafd;
  --manual: #fff4e0;
  --calc: #e7f6ec;
  --edit: #fffbe6;
  --group: #f3f7fe;
  font-size: 13px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  display: flex;
  flex-direction: column;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}

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

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  line-height: 1.2;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: #b8bec8; background: #fafbfc; }
.btn:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn:disabled { opacity: .45; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-dark); border-color: var(--accent-dark); }
.btn-danger { color: var(--danger); }
.btn-danger-solid, .btn-danger-solid:hover:not(:disabled) { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-link { border-color: transparent; background: none; color: var(--accent); }
.btn-link:hover:not(:disabled) { background: none; border-color: transparent; text-decoration: underline; }
.btn-sm { padding: 3px 7px; font-size: 12px; }
.btn-attention { border-color: #e0a100; background: #fff6d6; font-weight: 600; }
.btn-attention:hover:not(:disabled) { border-color: #c48c00; background: #ffefbf; }

/* ---- Top bar ---- */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 16px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { margin: 0; font-size: 16px; white-space: nowrap; }
.toolbar, .toolbar-right { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; }
.toolbar { flex: 1; }
.toolbar input[type=search] {
  width: 260px;
  max-width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font: inherit;
}
.check { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; cursor: pointer; }
.filters { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; }
.user { display: flex; align-items: center; gap: 4px; margin: 0; }

.menu-wrap { position: relative; }
.menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 40;
  min-width: 220px;
  max-height: 70vh;
  overflow: auto;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .12);
}
.menu-right { left: auto; right: 0; }
.menu-hint { padding: 6px 8px 8px; font-size: 12px; color: var(--muted); max-width: 260px; }
.menu-item {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  padding: 5px 8px;
  border: 0;
  border-radius: 4px;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
}
.menu-item:hover { background: #f3f5f8; }
.menu-item.danger { color: var(--danger); }
.menu-sep { height: 1px; margin: 4px 0; background: var(--line); }
.col-item { justify-content: space-between; cursor: default; }
.col-item label { display: flex; align-items: center; gap: 6px; flex: 1; cursor: pointer; }
.move { display: inline-flex; gap: 2px; }
.icon-btn {
  width: 22px;
  height: 22px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
}
.icon-btn:disabled { opacity: .3; cursor: default; }
.menu-actions { display: flex; gap: 6px; padding: 8px; border-top: 1px solid var(--line); margin-top: 6px; }

.progress {
  padding: 8px 16px;
  background: #eef4ff;
  border-bottom: 1px solid #cddcfb;
  color: #1a3f8a;
}

/* ---- Table ---- */
.table-wrap { flex: 1; min-height: 0; overflow: auto; }
table {
  border-collapse: separate;
  border-spacing: 0;
  min-width: 100%;
  background: var(--surface);
}
th, td {
  padding: 5px 8px;
  border-bottom: 1px solid var(--line);
  border-right: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
  white-space: nowrap;
}
td.loading { padding: 40px; color: var(--muted); border: 0; }

thead th { position: sticky; z-index: 5; background: #fafbfc; font-weight: 600; }
thead tr.groups th { top: 0; height: 25px; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
thead tr.labels th { top: 25px; cursor: pointer; user-select: none; }
thead th.dragging { opacity: .4; }
thead th.drop-before { box-shadow: inset 3px 0 0 var(--accent); }
thead th.drop-after { box-shadow: inset -3px 0 0 var(--accent); }
#grid.no-groups thead tr.labels th { top: 0; }
thead th[aria-sort=ascending]::after { content: " ▲"; font-size: 9px; }
thead th[aria-sort=descending]::after { content: " ▼"; font-size: 9px; }
th.src-ebay { background: var(--ebay); }
th.src-ai { background: var(--ai); }
th.src-manual { background: var(--manual); }
th.src-calc { background: var(--calc); }
#grid.no-groups th { background: #fafbfc; }

tfoot td {
  position: sticky;
  bottom: 0;
  z-index: 3;
  background: #f0f2f5;
  font-weight: 600;
  border-top: 1px solid #cfd4db;
}
tfoot td.sticky-col { z-index: 4; background: #f0f2f5; }

.sticky-col {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--surface);
  box-shadow: 1px 0 0 var(--line);
}
thead .sticky-col { z-index: 6; background: #fafbfc; }
.action-group { display: flex; align-items: center; gap: 4px; }
.lead { display: inline-block; min-width: 0; color: var(--muted); font-size: 12px; }
tr.child .lead { min-width: 44px; padding-left: 8px; }

tbody tr:hover td { background: #fafcff; }
tbody tr.editing td { background: var(--edit); }
tbody tr.is-sold td:not(.actions) { color: var(--muted); }
tbody tr.is-damaged td.col-damaged { background: #fdecea; }

/* Expandable purchase with several laptops */
tr.group-row td { background: var(--group); cursor: pointer; }
tr.group-row:hover td { background: #e9f0fd; }
tr.group-row.open td { border-bottom-color: #c9d8f5; font-weight: 600; }
tr.child td { background: #fcfdff; }
tr.child td.actions { box-shadow: 1px 0 0 var(--line), inset 3px 0 0 #b9cdf3; }
.toggle { font-weight: 600; min-width: 92px; }
.toggle-icon {
  display: inline-grid;
  place-items: center;
  width: 16px;
  height: 16px;
  border-radius: 4px;
  background: var(--accent);
  color: #fff;
  font-size: 13px;
  line-height: 1;
}

td.t-money, td.t-int, td.t-number, td.t-pct, th.t-money, th.t-int, th.t-number, th.t-pct { text-align: right; font-variant-numeric: tabular-nums; }
td.calc { font-weight: 600; }
td.negative { color: var(--danger); }
td.t-bool { text-align: center; }
td .clip { display: inline-block; max-width: 340px; overflow: hidden; text-overflow: ellipsis; vertical-align: middle; }
td .note { margin-left: 6px; }
.error-note { color: var(--danger); font-size: 12px; cursor: help; }

td input:not([type=checkbox]), td select {
  width: 100%;
  min-width: 70px;
  padding: 3px 5px;
  border: 1px solid #c9ced6;
  border-radius: 4px;
  font: inherit;
  background: #fff;
}
td input[type=checkbox] { width: 15px; height: 15px; }
td.col-ebay_title input, td.col-item_description input { min-width: 320px; }
td.col-listing_url input { min-width: 260px; }
td.col-serial_number input, td.col-distributor input, td.col-end_customer input { min-width: 130px; }
td.col-purchase_date input, td.col-delivered_to_distributor input { min-width: 130px; }
td select { min-width: 90px; }
td.t-money input { min-width: 90px; text-align: right; }

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

/* ---- Modal ---- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(20, 24, 33, .45);
}
.modal {
  width: min(440px, 100%);
  padding: 20px 22px;
  background: var(--surface);
  border-radius: 10px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, .25);
}
.modal h2 { margin: 0 0 8px; font-size: 16px; }
.modal p { margin: 0 0 18px; color: #3d4452; line-height: 1.45; }
.modal-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
.modal-input { display: block; width: 100%; margin: -6px 0 18px; padding: 8px 10px; border: 1px solid #c9ced6; border-radius: 6px; font: inherit; }

/* ---- Toast ---- */
.toast {
  position: fixed;
  bottom: 56px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: min(640px, 92vw);
  padding: 10px 10px 10px 14px;
  border-radius: 8px;
  background: #1d2330;
  color: #fff;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .2);
}
.toast.error { background: var(--danger); }
.toast .btn { background: transparent; color: #fff; border-color: rgba(255, 255, 255, .5); }
.toast-close { border: 0; background: none; color: inherit; font-size: 18px; line-height: 1; cursor: pointer; opacity: .8; }

/* ---- Login ---- */
.login-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card {
  display: grid;
  gap: 14px;
  width: min(360px, 100%);
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.login-card h1 { margin: 0 0 4px; font-size: 18px; }
.login-card label { display: grid; gap: 4px; font-weight: 500; }
.login-card input { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; font: inherit; }
.login-card .btn { justify-content: center; padding: 9px 12px; }
.form-error { margin: 0; padding: 8px 10px; border-radius: 6px; background: #fdecea; color: var(--danger); }

/* ---- Small screens ---- */
@media (max-width: 720px) {
  :root { font-size: 14px; }
  .topbar { padding: 8px 10px; }
  .toolbar input[type=search] { width: 100%; }
  .toolbar { order: 3; flex-basis: 100%; }
  .toolbar-right { margin-left: auto; }
  #ebay-status { display: none; }
  .sticky-col { position: static; box-shadow: none; }
  td .clip { max-width: 220px; }
}
