:root {
  color-scheme: light;
  --bg: #f6f7f8;
  --panel: #ffffff;
  --line: #dfe3e6;
  --text: #1e252b;
  --muted: #68717a;
  --blue: #1d6fb8;
  --green: #168157;
  --red: #b42318;
  --amber: #a15c00;
}

* { box-sizing: border-box; }
html { overflow-x: hidden; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  display: grid;
  grid-template-columns: 220px 1fr;
  min-height: 100vh;
  min-width: 0;
  overflow-x: hidden;
}
body.logged-out { grid-template-columns: 1fr; }
body.logged-out .sidebar, body.logged-out header { display: none; }
body.logged-out main { min-height: 100vh; display: grid; }
body.logged-out #app { padding: 24px; display: grid; }

.sidebar {
  background: #17202a;
  color: white;
  padding: 18px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.brand { padding: 8px 10px 20px; display: grid; gap: 4px; }
.brand span { color: #aab4bf; font-size: 13px; }
.nav {
  border: 0;
  background: transparent;
  color: #d7dee5;
  text-align: left;
  padding: 11px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 15px;
}
.nav.active, .nav:hover { background: #263545; color: white; }

main { min-width: 0; }
header {
  height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  background: white;
  padding: 0 24px;
}
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 18px; margin: 0 0 14px; }
h3 { font-size: 15px; margin: 0 0 10px; }
#app { padding: 22px 24px 40px; }
.grid { display: grid; gap: 16px; }
.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.cols-5 { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.panel, dialog {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
}
.metric { background: white; border: 1px solid var(--line); border-radius: 8px; padding: 14px; }
.metric span { color: var(--muted); font-size: 13px; }
.metric strong { display: block; font-size: 24px; margin-top: 5px; }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
button, input, select, textarea {
  font: inherit;
}
input, select, textarea {
  width: 100%;
  border: 1px solid #cfd6dd;
  border-radius: 6px;
  padding: 9px 10px;
  background: white;
}
textarea { min-height: 140px; resize: vertical; }
button.primary, button.secondary, button.danger {
  border: 0;
  border-radius: 6px;
  padding: 9px 13px;
  cursor: pointer;
  white-space: nowrap;
}
button.primary { background: var(--blue); color: white; }
button.secondary { background: #eef2f5; color: #25313b; border: 1px solid var(--line); }
button.danger { background: #fff1f0; color: var(--red); border: 1px solid #ffd0cc; }
button:disabled { opacity: .45; cursor: not-allowed; }
button.copy-success {
  background: var(--green) !important;
  border-color: var(--green) !important;
  color: white !important;
  box-shadow: 0 3px 10px rgb(22 129 87 / .28);
  transform: translateY(-1px);
  transition: background-color .18s ease, border-color .18s ease, box-shadow .18s ease, transform .18s ease;
}
.form { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; align-items: end; }
.form .wide { grid-column: 1 / -1; }
label { display: grid; gap: 6px; color: var(--muted); font-size: 13px; }
label span { color: var(--muted); }
table { width: 100%; border-collapse: collapse; background: white; }
th, td { border-bottom: 1px solid var(--line); padding: 10px 8px; text-align: left; vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 13px; background: #fafbfc; }
td.num, th.num { text-align: right; }
.tag { display: inline-flex; border-radius: 999px; padding: 2px 8px; font-size: 12px; background: #eef2f5; }
.tag.green { background: #e8f6ef; color: var(--green); }
.tag.red { background: #fff1f0; color: var(--red); }
.tag.amber { background: #fff5df; color: var(--amber); }
.bill, .cards-out {
  white-space: pre-wrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: #f8fafb;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px;
  line-height: 1.6;
}
#toast { color: var(--green); font-size: 14px; min-height: 20px; }
dialog { width: min(760px, calc(100vw - 30px)); }
dialog::backdrop { background: rgb(23 32 42 / .35); }
.icon-close { float: right; border: 0; background: transparent; font-size: 24px; cursor: pointer; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.muted { color: var(--muted); }
.warn { color: var(--red); font-weight: 600; }
.search-results { display: grid; gap: 6px; margin-top: 6px; }
.search-results button { text-align: left; border: 1px solid var(--line); background: white; padding: 8px; border-radius: 6px; cursor: pointer; }
.inventory-filters input { flex: 2 1 260px; }
.inventory-filters select { flex: 1 1 150px; width: auto; }
.order-filters input { flex: 2 1 220px; }
.order-filters select { flex: 1 1 145px; width: auto; }
.customer-list-filters input { flex: 2 1 240px; }
.customer-list-filters select { flex: 1 1 190px; width: auto; }
.customer-list-filters .muted { margin-left: auto; white-space: nowrap; }
.customer-action-cell { width: 150px; }
.purchase-more { justify-content: center; margin-top: 14px; margin-bottom: 0; }
.action-menu { width: 100%; }
.action-menu summary {
  list-style: none;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px 12px;
  background: #eef2f5;
  color: #25313b;
  text-align: center;
  cursor: pointer;
  white-space: nowrap;
}
.action-menu summary::-webkit-details-marker { display: none; }
.action-menu[open] summary { border-color: #9fbad3; background: #e8f1f8; color: #155a96; }
.action-menu-list { display: grid; gap: 6px; margin-top: 7px; }
.action-menu-list button { width: 100%; text-align: center; }
.inventory-actions { justify-content: flex-end; border-top: 1px solid var(--line); padding-top: 14px; }
.inventory-actions strong { margin-right: auto; }
.export-skip { display: inline-flex; grid-template-columns: none; align-items: center; gap: 7px; color: var(--text); white-space: nowrap; }
.export-skip input { width: 16px; height: 16px; min-height: 0; }
.card-pagination { justify-content: center; margin: 14px 0 0; }
.card-pagination span { min-width: 100px; text-align: center; }
.table-scroll { overflow-x: auto; }
.check-cell { width: 42px; text-align: center; }
.check-cell input { width: 16px; height: 16px; }
.card-content { min-width: 220px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; word-break: break-all; }
.metric-status { font-size: 17px !important; }
.negative { color: var(--red); }
.login-shell { width: 100%; min-height: calc(100vh - 48px); display: grid; place-items: center; }
.login-panel { width: min(400px, 100%); padding: 24px; }
.login-brand { display: grid; gap: 5px; margin-bottom: 24px; }
.login-brand strong { font-size: 22px; }
.login-brand span { color: var(--muted); font-size: 13px; }
.login-form { display: grid; gap: 14px; }
.login-error { color: var(--red); background: #fff1f0; border: 1px solid #ffd0cc; border-radius: 6px; padding: 9px 10px; }
.logout-button { margin-left: auto; }
.field-label { display: block; color: var(--muted); font-size: 13px; margin-bottom: 6px; }
.refund-card-list { border: 1px solid var(--line); border-radius: 6px; max-height: 240px; overflow: auto; }
.refund-card { display: flex; grid-template-columns: none; align-items: center; gap: 10px; padding: 9px 10px; border-bottom: 1px solid var(--line); color: var(--text); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.refund-card:last-child { border-bottom: 0; }
.refund-card input { width: 16px; height: 16px; flex: 0 0 auto; }
.refund-card span { color: inherit; overflow-wrap: anywhere; }

@media (max-width: 900px) {
  body { grid-template-columns: 1fr; }
  .sidebar { position: sticky; top: 0; z-index: 2; flex-direction: row; overflow-x: auto; padding: 8px 10px; gap: 4px; scrollbar-width: none; }
  .sidebar::-webkit-scrollbar { display: none; }
  .brand { display: none; }
  .nav { flex: 0 0 auto; padding: 10px 12px; white-space: nowrap; font-size: 14px; }
  .cols-2, .cols-3, .cols-4, .cols-5, .form { grid-template-columns: 1fr; }
  header { padding: 0 16px; }
  #app { padding: 16px; }
}

@media (max-width: 640px) {
  .sidebar { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); overflow: visible; }
  .nav { width: 100%; padding: 9px 3px; text-align: center; font-size: 13px; }
  header { height: 58px; }
  h1 { font-size: 20px; }
  #app { padding: 12px; }
  .panel, dialog { padding: 13px; }
  .panel { border-radius: 7px; }
  .form { gap: 10px; }
  .form > button { width: 100%; min-height: 42px; }
  input, select, textarea { font-size: 16px; min-height: 42px; }
  textarea { min-height: 120px; }
  dialog { width: calc(100vw - 16px); max-height: calc(100dvh - 16px); overflow: auto; }
  .toolbar { gap: 8px; }
  .toolbar > h2 { flex: 1 1 auto; }
  .table-scroll { overflow: visible; }

  #app[data-view="products"], #app[data-view="customers"] { display: flex; flex-direction: column; }
  #app[data-view="products"] .product-list-section { order: 1; }
  #app[data-view="products"] .product-import-section { order: 2; }
  #app[data-view="products"] .purchase-list-section { order: 3; }
  #app[data-view="products"] .product-create-section { order: 4; }
  #app[data-view="customers"] .customer-list-section { order: 1; }
  #app[data-view="customers"] .customer-create-section { order: 2; }
  #app[data-view="products"] .product-list-section,
  #app[data-view="customers"] .customer-list-section { margin-top: 0 !important; }
  #app[data-view="products"] .product-create-section,
  #app[data-view="customers"] .customer-create-section { margin-top: 16px !important; }

  .responsive-table, .responsive-table tbody, .responsive-table tr, .responsive-table td { display: block; width: 100%; }
  .responsive-table { background: transparent; }
  .responsive-table thead { display: none; }
  .responsive-table tbody { display: grid; gap: 10px; }
  .responsive-table tr { overflow: hidden; background: white; border: 1px solid var(--line); border-radius: 7px; }
  .responsive-table td {
    display: grid;
    grid-template-columns: 88px minmax(0, 1fr);
    gap: 10px;
    align-items: center;
    min-height: 40px;
    padding: 9px 10px;
    text-align: left !important;
    overflow-wrap: anywhere;
  }
  .responsive-table td::before { content: attr(data-label); color: var(--muted); font-size: 12px; font-weight: 600; }
  .responsive-table td:first-child { font-weight: 700; }
  .responsive-table td:last-child { border-bottom: 0; }
  .responsive-table td[colspan] { display: block; min-height: auto; text-align: center !important; }
  .responsive-table td[colspan]::before { display: none; }
  .responsive-table td.actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); padding-top: 10px; }
  .responsive-table td.actions::before { grid-column: 1 / -1; }
  .responsive-table td.actions button { width: 100%; min-height: 40px; padding: 8px 6px; }
  .responsive-table td.actions button:only-of-type { grid-column: 1 / -1; }
  .responsive-table .tag { justify-self: start; }
  .customer-list-section .toolbar > button { width: 100%; min-height: 42px; }
  .customer-list-filters input, .customer-list-filters select { flex: 1 1 100%; width: 100%; }
  .customer-list-filters .muted { width: 100%; margin-left: 0; }
  .purchase-more button { width: 100%; min-height: 42px; }
  .inventory-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .inventory-actions > strong, .inventory-actions > span, .inventory-actions > label { grid-column: 1 / -1; }
  .inventory-actions > button { width: 100%; min-height: 42px; white-space: normal; }
  .inventory-actions > #deleteSelectedCards { grid-column: 1 / -1; }
  .card-pagination { display: grid; grid-template-columns: 1fr auto 1fr; }
  .card-pagination button { width: 100%; }
  .responsive-table td.customer-action-cell { display: grid; grid-template-columns: 88px minmax(0, 1fr); width: 100%; }
  .responsive-table td.customer-action-cell::before { content: attr(data-label); color: var(--muted); font-size: 12px; font-weight: 600; }
  .action-menu-list { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .action-menu-list button { min-height: 40px; padding: 8px 5px; }
}
