:root {
  --bg: #f4f2ee;
  --surface: #ffffff;
  --ink: #1d1b18;
  --muted: #706a61;
  --line: #ded8cf;
  --accent: #0f766e;
  --accent-dark: #0b5f59;
  --warm: #b7791f;
  --danger: #b42318;
  --ok: #287d3c;
  --shadow: 0 14px 34px rgba(34, 28, 20, 0.08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  grid-template-columns: 248px 1fr;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

button, input, select {
  font: inherit;
}

.sidebar {
  min-height: 100vh;
  padding: 20px 16px;
  background: #211e1a;
  color: #fff;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px;
}

.mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.28);
  background: #0f766e;
  font-weight: 800;
  font-size: 22px;
}

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

.brand span {
  margin-top: 3px;
  color: rgba(255, 255, 255, 0.62);
  font-size: 12px;
}

.nav {
  display: grid;
  gap: 8px;
}

.nav-item {
  width: 100%;
  border: 0;
  background: transparent;
  color: rgba(255, 255, 255, 0.74);
  text-align: left;
  padding: 12px 14px;
  border-radius: 8px;
  cursor: pointer;
}

.nav-item:hover,
.nav-item.active {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.operator-panel {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.06);
}

.operator-panel label {
  color: rgba(255, 255, 255, 0.62);
}

.operator-panel select {
  color: #fff;
  background: #2e2a24;
  border-color: rgba(255, 255, 255, 0.16);
}

.operator-panel select:disabled {
  opacity: 1;
}

.account-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  color: rgba(255, 255, 255, 0.78);
  font-size: 13px;
}

.logout-button {
  min-height: 30px;
  padding: 0 10px;
  color: #fff;
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.16);
}

.sidebar-note {
  margin-top: auto;
  padding: 14px;
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.06);
}

.sidebar-note span,
.sidebar-note strong {
  display: block;
}

.sidebar-note span {
  color: rgba(255, 255, 255, 0.58);
  font-size: 12px;
}

.sidebar-note strong {
  margin-top: 3px;
  font-size: 14px;
}

.app {
  min-width: 0;
  padding: 24px;
}

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

h1, h2, p {
  margin: 0;
}

h1 {
  font-size: 26px;
  line-height: 1.2;
}

.topbar p {
  margin-top: 6px;
  color: var(--muted);
  font-size: 14px;
}

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

button {
  min-height: 38px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: 8px;
  padding: 0 14px;
  cursor: pointer;
  white-space: nowrap;
}

button:hover {
  border-color: #bdb3a5;
}

.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  font-weight: 700;
}

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

.ghost {
  background: #f8f6f2;
}

.icon-button {
  width: 40px;
  padding: 0;
  display: grid;
  place-items: center;
}

.icon-button svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

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

.metrics.compact {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.metric {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  box-shadow: var(--shadow);
}

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

.metric strong {
  display: block;
  margin-top: 8px;
  font-size: 26px;
  line-height: 1.1;
}

.metric small {
  display: block;
  margin-top: 8px;
  color: var(--muted);
}

.content-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(360px, 0.8fr);
  gap: 16px;
}

.settings-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.panel-head h2 {
  font-size: 16px;
}

.panel-head span {
  color: var(--muted);
  font-size: 13px;
}

.panel-actions {
  justify-content: flex-end;
  flex-wrap: wrap;
}

.form-panel {
  padding-bottom: 2px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  padding: 16px;
}

.compact-form {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

input,
select {
  width: 100%;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 0 10px;
}

.field-with-button {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}

.compact-button {
  min-height: 38px;
  padding: 0 12px;
  white-space: nowrap;
}

input:focus,
select:focus {
  outline: 2px solid rgba(15, 118, 110, 0.18);
  border-color: var(--accent);
}

.wide {
  grid-column: span 2;
}

.form-actions {
  grid-column: 1 / -1;
  justify-content: flex-end;
}

.search {
  max-width: 260px;
}

.table-wrap {
  overflow: auto;
}

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

th,
td {
  padding: 12px 14px;
  border-bottom: 1px solid #eee9e2;
  text-align: left;
  vertical-align: top;
  font-size: 14px;
}

th {
  color: var(--muted);
  background: #fbfaf7;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

td {
  color: #2a2723;
}

.subline {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.25;
}

.product-thumb {
  width: 54px;
  height: 54px;
  border-radius: 8px;
  border: 1px solid var(--line);
  object-fit: cover;
  background: #f1eee8;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-weight: 700;
}

.product-thumb.placeholder {
  font-size: 18px;
}

.row-actions {
  display: flex;
  gap: 8px;
}

.row-actions button {
  min-height: 30px;
  padding: 0 10px;
  font-size: 12px;
}

.tag {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  background: #f1eee8;
  color: #4b453d;
  font-size: 12px;
  white-space: nowrap;
}

.tag.danger {
  background: #fff0ed;
  color: var(--danger);
}

.tag.ok {
  background: #eef8f0;
  color: var(--ok);
}

.timeline {
  padding: 8px 16px 16px;
}

.event {
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid #eee9e2;
}

.event:last-child {
  border-bottom: 0;
}

.event time {
  color: var(--muted);
  font-size: 12px;
}

.event strong {
  display: block;
  font-size: 14px;
}

.event span {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
}

.report-toolbar {
  justify-content: flex-end;
  margin-bottom: 14px;
}

.report-toolbar label {
  width: 170px;
}

.toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  min-width: 220px;
  max-width: 320px;
  padding: 12px 14px;
  background: #211e1a;
  color: #fff;
  border-radius: 8px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.24);
  opacity: 0;
  transform: translateY(10px);
  pointer-events: none;
  transition: 0.18s ease;
}

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

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: #f4f2ee;
}

.login-screen.show {
  display: flex;
}

.login-card {
  width: min(420px, 100%);
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 14px;
}

.login-card .mark {
  color: #fff;
}

.login-card h2 {
  font-size: 22px;
}

.login-card p,
.login-hint {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(24, 21, 18, 0.46);
}

.modal.show {
  display: flex;
}

.modal-card {
  width: min(980px, 100%);
  max-height: calc(100vh - 40px);
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.26);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
}

.modal-head h2 {
  font-size: 18px;
}

.modal-head span {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
}

.modal-head .icon-button {
  font-size: 24px;
  line-height: 1;
}

.product-form {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 0;
}

.photo-box {
  padding: 16px;
  border-right: 1px solid var(--line);
  background: #fbfaf7;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.photo-preview {
  width: 100%;
  aspect-ratio: 1;
  border: 1px dashed #c9c0b3;
  border-radius: 8px;
  background: #fff;
  display: grid;
  place-items: center;
  color: var(--muted);
  overflow: hidden;
}

.photo-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.upload-button {
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.upload-button input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.editor-grid {
  padding: 16px;
}

.scanner-card {
  width: min(560px, 100%);
}

.scanner-body {
  padding: 16px;
  display: grid;
  gap: 14px;
}

#scannerVideo {
  width: 100%;
  aspect-ratio: 4 / 3;
  background: #161412;
  border-radius: 8px;
  object-fit: cover;
}

.scanner-fallback {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: end;
}

#scannerStatus {
  color: var(--muted);
  font-size: 13px;
}

.empty {
  padding: 18px 14px;
  color: var(--muted);
}

.label-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
  padding: 16px;
}

.label-card {
  min-height: 118px;
  border: 1px dashed #b9afa1;
  border-radius: 8px;
  padding: 12px;
  display: grid;
  align-content: start;
  gap: 6px;
  background: #fff;
}

.label-card strong {
  font-size: 18px;
}

.label-card span,
.label-card small {
  color: var(--muted);
}

.barcode-text {
  min-height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  background: repeating-linear-gradient(90deg, #1d1b18 0 2px, #fff 2px 5px, #1d1b18 5px 6px, #fff 6px 10px);
  color: transparent;
  font-size: 1px;
}

@media print {
  body {
    display: block;
    background: #fff;
  }

  .sidebar,
  .topbar,
  .view:not(#labels),
  #labels .panel-head,
  .toast,
  .login-screen,
  .modal {
    display: none !important;
  }

  .app {
    padding: 0;
  }

  #labels,
  #labels.view {
    display: block !important;
  }

  .panel {
    border: 0;
    box-shadow: none;
  }

  .label-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    padding: 0;
  }
}

@media (max-width: 980px) {
  body {
    grid-template-columns: 1fr;
  }

  .sidebar {
    min-height: auto;
    position: sticky;
    top: 0;
    z-index: 4;
    padding: 12px;
  }

  .brand,
  .sidebar-note {
    display: none;
  }

  .nav {
    display: flex;
    overflow-x: auto;
  }

  .nav-item {
    white-space: nowrap;
  }

  .operator-panel {
    grid-template-columns: 1fr 1fr;
    padding: 10px;
  }

  .app {
    padding: 16px;
  }

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

  .metrics,
  .metrics.compact,
  .content-grid,
  .form-grid,
  .compact-form,
  .product-form {
    grid-template-columns: 1fr;
  }

  .photo-box {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .photo-preview {
    aspect-ratio: 16 / 9;
    max-height: 220px;
  }

  .wide,
  .form-actions {
    grid-column: auto;
  }

  .form-actions,
  .panel-actions,
  .report-toolbar {
    justify-content: stretch;
    flex-wrap: wrap;
  }

  .form-actions button,
  .panel-actions button,
  .report-toolbar button,
  .top-actions button {
    flex: 0 0 auto;
  }

  .search {
    max-width: none;
    flex: 1 1 100%;
  }

  .scanner-fallback {
    grid-template-columns: 1fr;
  }

  .report-toolbar label {
    width: calc(50% - 5px);
  }
}
