:root {
  --bg: #f6f7f4;
  --surface: #ffffff;
  --surface-2: #eef3ed;
  --line: #d9ded7;
  --text: #1f2a24;
  --muted: #637066;
  --primary: #24745a;
  --primary-strong: #165c45;
  --amber: #b7791f;
  --red: #b83f3f;
  --blue: #315f96;
  --shadow: 0 10px 24px rgba(31, 42, 36, 0.08);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "PingFang SC", "Microsoft YaHei", sans-serif;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

button:disabled,
input:disabled,
select:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: 0.62;
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    linear-gradient(135deg, rgba(36, 116, 90, 0.1), rgba(49, 95, 150, 0.08)),
    var(--bg);
}

.login-panel {
  width: min(100%, 430px);
  display: grid;
  gap: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 24px;
}

.login-brand .brand-subtitle,
.login-brand .brand-title {
  color: var(--text);
}

.login-copy {
  display: grid;
  gap: 8px;
}

.login-copy h1 {
  font-size: 24px;
}

.login-copy p,
.login-help {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

.login-notice {
  margin-bottom: 0;
}

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

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding: 24px 18px;
  background: #17231d;
  color: #f7faf6;
}

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

.brand-mark {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border-radius: 8px;
  background: #d7f0df;
  color: #174b38;
  font-weight: 800;
}

.brand-title {
  font-size: 17px;
  font-weight: 800;
}

.brand-subtitle {
  margin-top: 2px;
  color: #b7c4bc;
  font-size: 12px;
}

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

.nav-button {
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: #d7e2db;
  text-align: left;
  padding: 0 13px;
}

.nav-button:hover,
.nav-button.is-active {
  background: #25362d;
  color: #ffffff;
  border-color: #395044;
}

.sidebar-footer {
  margin-top: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  color: #b7c4bc;
  font-size: 13px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: #78d394;
}

.workspace {
  min-width: 0;
  padding: 26px;
}

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

.topbar-actions {
  display: flex;
  align-items: flex-end;
  gap: 12px;
}

.account-chip {
  min-height: 40px;
  display: grid;
  align-content: center;
  gap: 2px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 6px 12px;
}

.account-chip strong {
  font-size: 14px;
}

.account-chip span {
  color: var(--muted);
  font-size: 12px;
}

.role-switcher {
  min-width: 180px;
}

h1,
h2,
p {
  margin: 0;
}

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

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

.ghost-button,
.primary-button {
  min-height: 40px;
  border-radius: 8px;
  padding: 0 15px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
}

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

.primary-button:hover {
  background: var(--primary-strong);
}

.ghost-button:hover {
  border-color: #b8c1b6;
}

.notice {
  margin-bottom: 16px;
  padding: 12px 14px;
  border: 1px solid #badac9;
  border-radius: 8px;
  background: #edf8f1;
  color: #174b38;
}

.notice.is-error {
  background: #fff1f0;
  border-color: #f2b6b1;
  color: #87302d;
}

.form-warning {
  margin: 14px 18px 0;
  border: 1px solid #edd8aa;
  border-radius: 8px;
  background: #fff9e8;
  color: #6f4f12;
  padding: 11px 12px;
  font-size: 13px;
  line-height: 1.5;
}

.view {
  display: none;
}

.view.is-active {
  display: grid;
  gap: 18px;
}

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

.metric {
  min-height: 116px;
  display: grid;
  gap: 8px;
  align-content: center;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

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

.metric strong {
  font-size: 32px;
  line-height: 1;
}

.metric:nth-child(1) {
  border-top: 4px solid var(--primary);
}

.metric:nth-child(2) {
  border-top: 4px solid var(--blue);
}

.metric:nth-child(3) {
  border-top: 4px solid var(--amber);
}

.metric:nth-child(4) {
  border-top: 4px solid #6c5f90;
}

.metric:nth-child(5) {
  border-top: 4px solid #6e7f4f;
}

.metric:nth-child(6) {
  border-top: 4px solid #9a6b4f;
}

.two-column {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
}

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

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

.panel h2 {
  font-size: 17px;
}

.form-panel {
  overflow: visible;
}

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

.stack-form {
  display: grid;
  gap: 16px;
  padding: 18px;
}

.stack-form .form-grid {
  padding: 0;
}

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

input,
select,
textarea {
  width: 100%;
  min-height: 40px;
  border: 1px solid #cdd4ca;
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 8px 10px;
}

textarea {
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid rgba(36, 116, 90, 0.2);
  border-color: var(--primary);
}

.wide {
  grid-column: 1 / -1;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

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

.toolbar select {
  min-width: 150px;
}

.role-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
  padding: 18px;
}

.role-card {
  min-height: 164px;
  display: grid;
  gap: 10px;
  align-content: start;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfa;
  padding: 14px;
}

.role-card div {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.role-card strong {
  font-size: 16px;
}

.role-card span,
.role-card em {
  color: var(--muted);
  font-size: 12px;
  font-style: normal;
}

.role-card p {
  color: #405047;
  font-size: 13px;
  line-height: 1.55;
}

.recipe-builder {
  display: grid;
  gap: 10px;
}

.recipe-row {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) 140px auto;
  align-items: end;
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfa;
  padding: 12px;
}

.subsection-title {
  font-weight: 800;
  color: #26362d;
}

.table-wrap {
  width: 100%;
  overflow: auto;
}

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

th,
td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
  font-size: 14px;
}

th {
  color: #526157;
  background: #f7f9f6;
  font-size: 12px;
  font-weight: 800;
}

tr:last-child td {
  border-bottom: 0;
}

.code-button {
  border: 0;
  background: transparent;
  color: var(--blue);
  padding: 0;
  font-weight: 800;
  text-align: left;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  border-radius: 999px;
  padding: 0 9px;
  background: var(--surface-2);
  color: #435046;
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.badge.good {
  background: #e2f4e9;
  color: #17603d;
}

.badge.warn {
  background: #fff4d8;
  color: #825c14;
}

.badge.bad {
  background: #ffe5e2;
  color: #9b332f;
}

.trace-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  padding: 18px;
}

.quick-codes {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 18px 18px;
}

.quick-codes button {
  min-height: 32px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: #405047;
  padding: 0 10px;
}

.print-hint {
  padding: 13px 18px;
  border-bottom: 1px solid var(--line);
  background: #f7fbf8;
  color: #526157;
  font-size: 13px;
}

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

.qr-label {
  min-height: 152px;
  display: grid;
  gap: 10px;
  align-content: space-between;
  border: 1px solid #cfd8cf;
  border-radius: 8px;
  background: #fff;
  padding: 12px;
  break-inside: avoid;
}

.qr-label-main {
  display: grid;
  grid-template-columns: 82px minmax(0, 1fr);
  gap: 12px;
  align-items: center;
}

.qr-label-main img {
  width: 82px;
  height: 82px;
  border: 1px solid #e5e9e4;
}

.qr-label-main span,
.qr-label-meta span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.qr-label-main strong {
  display: block;
  margin-top: 4px;
  overflow-wrap: anywhere;
  font-size: 18px;
}

.qr-label-main em {
  display: block;
  margin-top: 4px;
  color: #425047;
  font-style: normal;
}

.qr-label-meta {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.qr-label-meta span {
  overflow: hidden;
  min-height: 26px;
  border-radius: 6px;
  background: #f3f6f2;
  padding: 6px 7px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.scan-layout {
  display: grid;
  grid-template-columns: minmax(280px, 420px) minmax(0, 1fr);
  gap: 18px;
  padding: 18px;
}

.scan-camera {
  display: grid;
  gap: 10px;
}

.scan-camera video {
  width: 100%;
  aspect-ratio: 4 / 3;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #101812;
  object-fit: cover;
}

.scan-status {
  min-height: 42px;
  border-radius: 8px;
  background: #eef5f0;
  color: #405047;
  padding: 11px 12px;
  font-size: 13px;
}

.scan-manual {
  display: grid;
  align-content: start;
  gap: 12px;
}

.production-scan-dock {
  display: grid;
  grid-template-columns: minmax(220px, 360px) minmax(0, 1fr);
  gap: 14px;
  align-items: start;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fbf8;
  padding: 12px;
}

.production-scan-dock .scan-camera video {
  max-height: 240px;
}

#production-consumptions tr.is-scanned td {
  background: #ecf8ef;
}

.trace-panel {
  padding: 18px;
}

.trace-heading {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 14px;
  margin-bottom: 18px;
}

.trace-heading h2 {
  margin-bottom: 5px;
}

.trace-chain {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 10px;
  margin-bottom: 18px;
}

.trace-node {
  position: relative;
  min-height: 96px;
  padding: 14px;
  border: 1px solid #ccd9d0;
  border-left: 4px solid var(--primary);
  border-radius: 8px;
  background: #fbfdfb;
}

.trace-node span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.trace-node strong {
  display: block;
  margin-top: 6px;
  overflow-wrap: anywhere;
}

.trace-node em {
  display: block;
  margin-top: 6px;
  color: #526157;
  font-style: normal;
  font-size: 13px;
}

.trace-sections {
  display: grid;
  gap: 18px;
}

.mini-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.mini-panel h3 {
  margin: 0;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  background: #f7f9f6;
  font-size: 15px;
}

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

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

  .sidebar {
    position: static;
    height: auto;
  }

  .nav {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .nav-button {
    text-align: center;
  }

  .summary-grid,
  .two-column,
  .form-grid,
  .scan-layout,
  .production-scan-dock,
  .recipe-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .recipe-row .recipe-remove {
    grid-column: 1 / -1;
  }
}

@media (max-width: 640px) {
  .workspace {
    padding: 16px;
  }

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

  .topbar-actions {
    width: 100%;
    align-items: stretch;
    flex-direction: column;
  }

  .role-switcher {
    min-width: 0;
  }

  .summary-grid,
  .two-column,
  .form-grid,
  .trace-form,
  .scan-layout,
  .production-scan-dock,
  .recipe-row {
    grid-template-columns: 1fr;
  }

  .nav {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .form-actions,
  .toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .form-actions .primary-button,
  .form-actions .ghost-button,
  .toolbar .primary-button,
  .toolbar .ghost-button,
  .trace-form .primary-button {
    width: 100%;
  }

  table {
    min-width: 640px;
  }
}

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

  .sidebar,
  .topbar,
  .notice,
  .print-hint,
  .toolbar,
  .qr-label-actions,
  .view:not(#view-labels),
  #view-labels .panel-header {
    display: none !important;
  }

  .app-shell,
  .workspace,
  #view-labels,
  #view-labels .panel {
    display: block;
    min-height: auto;
    padding: 0;
    border: 0;
    box-shadow: none;
  }

  .label-sheet {
    grid-template-columns: repeat(2, 70mm);
    gap: 4mm;
    padding: 0;
  }

  .qr-label {
    width: 70mm;
    min-height: 50mm;
    border: 1px solid #000;
    border-radius: 0;
    box-shadow: none;
    padding: 4mm;
  }

  .qr-label-main {
    grid-template-columns: 26mm minmax(0, 1fr);
  }

  .qr-label-main img {
    width: 26mm;
    height: 26mm;
  }
}
