:root {
  --bg: #f5f3ee;
  --ink: #202624;
  --muted: #68736f;
  --line: #ddd7cc;
  --paper: #fffdfa;
  --paper-2: #f8f1e7;
  --paper-3: #f1ece3;
  --dark: #17231f;
  --primary: #0f766e;
  --primary-dark: #115e59;
  --accent: #9f5b14;
  --danger: #b91c1c;
  --good: #15803d;
  --warn: #b45309;
  --shadow: 0 12px 30px rgba(35, 29, 20, 0.07);
  --shadow-soft: 0 6px 18px rgba(35, 29, 20, 0.05);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: linear-gradient(180deg, #faf8f3 0, var(--bg) 260px);
  color: var(--ink);
  font-family: "Microsoft YaHei", "PingFang SC", system-ui, sans-serif;
}

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

button {
  cursor: pointer;
}

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

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: auto;
  padding: 24px 18px;
  background: var(--dark);
  color: #f8faf7;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-bottom: 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  margin-bottom: 18px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  border-radius: 8px;
  background: #e4bd63;
  color: #15201d;
  font-weight: 900;
}

.brand h1,
.brand p,
.topbar h2,
.topbar p,
.card h3,
.section-title h3 {
  margin: 0;
}

.brand h1 {
  font-size: 18px;
  line-height: 1.2;
}

.brand p {
  margin-top: 4px;
  color: #b9c4be;
  font-size: 12px;
}

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

.nav-group {
  display: grid;
  gap: 5px;
}

.nav-group-title {
  padding: 4px 10px 2px;
  color: #90a39a;
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0;
}

.nav button {
  min-height: 36px;
  border: 0;
  border-radius: 8px;
  padding: 0 12px;
  background: transparent;
  color: #dce6e1;
  text-align: left;
}

.nav button:hover,
.nav button.is-active {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
}

.main {
  min-width: 0;
  padding: 30px;
}

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

.eyebrow {
  color: var(--accent);
  font-size: 13px;
  font-weight: 800;
}

.topbar h2 {
  margin-top: 4px;
  font-size: clamp(28px, 3vw, 42px);
  line-height: 1.08;
  letter-spacing: 0;
}

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

.user-chip {
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  background: var(--paper);
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.row-actions {
  gap: 6px;
}

.grid {
  display: grid;
  gap: 16px;
}

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

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

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

.card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  box-shadow: var(--shadow);
}

form.card {
  background: #fffefb;
}

.storage-warning {
  border: 1px solid #f0c36c;
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 14px;
  background: #fff7df;
  color: #7a4b00;
  font-size: 14px;
  font-weight: 800;
}

.storage-good {
  border: 1px solid #b7dfc4;
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 14px;
  background: #f0fdf4;
  color: #166534;
  font-size: 14px;
  font-weight: 800;
}

.auth-mode .app-shell {
  grid-template-columns: 1fr;
}

.auth-mode .sidebar {
  display: none;
}

.auth-mode .main {
  display: grid;
  align-content: start;
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
}

.login-panel {
  max-width: 520px;
  width: 100%;
}

.login-panel h3 {
  margin: 0 0 4px;
  font-size: 24px;
}

.login-version {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.04em;
}

.save-conflict-warning {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.save-conflict-warning strong {
  display: block;
  margin-bottom: 4px;
}

.save-conflict-warning p {
  margin: 0;
  font-weight: 600;
  line-height: 1.6;
}

.save-conflict-actions {
  display: flex;
  flex: 0 0 auto;
  gap: 8px;
}

.login-form {
  grid-template-columns: 1fr;
}

.user-admin {
  margin-top: 16px;
}

.pad {
  padding: 18px;
}

.metric {
  min-height: 112px;
  padding: 18px;
  box-shadow: var(--shadow-soft);
}

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

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

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 22px 0 12px;
}

.section-title.compact {
  margin-top: 0;
}

form.card,
.form-grid {
  display: grid;
  gap: 12px;
}

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

.form-grid .span-2 {
  grid-column: span 2;
}

.form-grid .span-4 {
  grid-column: 1 / -1;
}

.line-editor {
  display: grid;
  gap: 12px;
  border: 1px solid #ebe4d9;
  border-radius: 8px;
  padding: 14px;
  background: #fffdf9;
}

.line-editor-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.line-editor-head h3 {
  margin: 0;
  font-size: 16px;
}

.line-list {
  display: grid;
  gap: 10px;
}

.line-row {
  display: grid;
  grid-template-columns: minmax(180px, 1.4fr) minmax(110px, 0.7fr) minmax(110px, 0.7fr) auto;
  gap: 10px;
  align-items: end;
  border-radius: 8px;
  padding: 10px;
  background: var(--paper-2);
  border: 1px solid transparent;
}

.line-row:focus-within {
  border-color: rgba(15, 118, 110, 0.35);
  background: #fffaf0;
}

.line-row.product-unmatched [data-line-product-name] {
  border-color: var(--danger);
  background: #fff5f5;
}

.line-row.purchase {
  grid-template-columns: minmax(180px, 1.35fr) minmax(140px, 0.95fr) minmax(92px, 0.6fr) minmax(110px, 0.72fr) minmax(110px, 0.72fr) minmax(116px, 0.7fr) auto;
}

.line-row.stocktake {
  grid-template-columns: minmax(180px, 1.5fr) minmax(120px, 0.8fr) minmax(150px, 1fr) auto;
}

.line-row.simple {
  grid-template-columns: minmax(180px, 1.6fr) minmax(120px, 0.8fr) minmax(150px, 1fr) auto;
}

.line-hint {
  align-self: center;
  color: var(--muted);
  font-size: 13px;
}

.line-hint.bad {
  color: var(--danger);
  font-weight: 800;
}

.line-total {
  color: var(--muted);
  font-size: 13px;
}

.line-check {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding-bottom: 1px;
  color: #34403c;
}

.line-check input {
  width: 16px;
  min-height: 16px;
  height: 16px;
  margin: 0;
}

.table-input,
.table-select {
  min-height: 34px;
  border-radius: 6px;
  padding: 0 8px;
  font-size: 13px;
}

label {
  display: grid;
  gap: 7px;
  color: #34403c;
  font-size: 13px;
  font-weight: 800;
}

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

textarea {
  min-height: 78px;
  padding-top: 9px;
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  outline: 3px solid rgba(15, 118, 110, 0.16);
}

.primary-button,
.ghost-button,
.danger-button,
.small-button {
  min-height: 40px;
  border-radius: 8px;
  border: 1px solid transparent;
  padding: 0 14px;
  font-weight: 800;
  white-space: nowrap;
}

.primary-button {
  background: var(--primary);
  color: #fff;
}

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

.ghost-button,
.small-button {
  border-color: #cbc3b6;
  background: #fff;
  color: #24302c;
}

.danger-button {
  border-color: #efc6c6;
  background: #fff;
  color: var(--danger);
}

.small-button {
  min-height: 32px;
  padding: 0 9px;
  font-size: 13px;
}

.file-button {
  display: inline-grid;
  place-items: center;
}

.file-button input {
  display: none;
}

.table-wrap {
  overflow: auto;
}

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

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

th {
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
  background: #fbf7ee;
}

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

tbody tr:hover td {
  background: #fffaf0;
}

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

.badge.good {
  color: var(--good);
}

.badge.bad {
  color: var(--danger);
}

.badge.warn {
  color: var(--warn);
}

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

.list {
  display: grid;
  gap: 10px;
}

.list-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  border: 1px solid #ebe4d9;
  border-radius: 8px;
  padding: 11px 12px;
  background: #fff;
}

.history-panel {
  margin-top: 22px;
  box-shadow: var(--shadow-soft);
}

.history-panel .table-wrap {
  border: 1px solid #ebe4d9;
  border-radius: 8px;
  background: #fff;
}

.history-tools {
  display: grid;
  grid-template-columns: 170px minmax(220px, 1fr) auto auto;
  gap: 10px;
  align-items: end;
  min-width: min(620px, 100%);
}

.history-summary {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin: 12px 0;
}

.mini-metric {
  border: 1px solid #ebe4d9;
  border-radius: 8px;
  padding: 12px;
  background: #fff;
}

.mini-metric span,
.mini-metric small {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.mini-metric strong {
  display: block;
  margin: 5px 0 2px;
  font-size: 22px;
  line-height: 1.1;
}

.row-actions {
  justify-content: flex-start;
}

.modal-root {
  display: none;
}

.modal-root.is-open {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 24px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(23, 35, 31, 0.42);
}

.modal-card {
  position: relative;
  z-index: 1;
  width: min(1120px, 100%);
  max-height: min(86vh, 860px);
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
  background: var(--paper);
  box-shadow: 0 24px 80px rgba(12, 18, 16, 0.28);
}

.modal-head {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  border-bottom: 1px solid #ebe4d9;
  padding-bottom: 14px;
  margin-bottom: 14px;
}

.modal-head h3 {
  margin: 4px 0 8px;
  font-size: 28px;
  line-height: 1.1;
}

.modal-status {
  display: flex;
}

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

.detail-meta-item {
  border: 1px solid #ebe4d9;
  border-radius: 8px;
  padding: 10px;
  background: #fff;
}

.detail-meta-item span {
  display: block;
  color: var(--muted);
  font-size: 12px;
}

.detail-meta-item strong {
  display: block;
  margin-top: 5px;
  font-size: 14px;
  word-break: break-word;
}

.detail-table {
  border: 1px solid #ebe4d9;
  border-radius: 8px;
  background: #fff;
}

.detail-note {
  margin-top: 14px;
  border: 1px solid #ebe4d9;
  border-radius: 8px;
  padding: 12px;
  background: #fff;
}

.detail-note p {
  margin: 6px 0 0;
  color: var(--muted);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 14px;
}

.money-plus {
  color: var(--good);
  font-weight: 900;
}

.money-minus {
  color: var(--danger);
  font-weight: 900;
}

.toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  max-width: 360px;
  transform: translateY(120px);
  border-radius: 8px;
  padding: 12px 14px;
  background: #15201d;
  color: #fff;
  box-shadow: var(--shadow);
  transition: transform 0.2s ease;
}

.toast.is-visible {
  transform: translateY(0);
}

#printArea {
  display: none;
}

.print-doc {
  color: #111;
  font-family: "Microsoft YaHei", "SimSun", serif;
}

.print-doc h1 {
  text-align: center;
  font-size: 24px;
  margin: 0 0 18px;
}

.print-meta {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px 18px;
  margin-bottom: 14px;
  font-size: 13px;
}

.print-doc table {
  min-width: 0;
  border: 1px solid #222;
}

.print-doc th,
.print-doc td {
  border: 1px solid #222;
  padding: 7px;
  font-size: 12px;
}

.signatures {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 18px;
  margin-top: 34px;
  font-size: 13px;
}

@media (max-width: 1080px) {
  .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;
  }

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

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

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

@media (max-width: 680px) {
  .main {
    padding: 18px;
  }

  .topbar,
  .grid.two,
  .grid.three,
  .grid.four,
  .form-grid,
  .nav {
    grid-template-columns: 1fr;
  }

  .topbar {
    display: grid;
  }

  .form-grid .span-2,
  .form-grid .span-4 {
    grid-column: auto;
  }

  .line-row,
  .line-row.purchase,
  .line-row.stocktake,
  .line-row.simple {
    grid-template-columns: 1fr;
  }

  .history-tools,
  .history-summary,
  .detail-grid {
    grid-template-columns: 1fr;
  }

  .modal-root.is-open {
    padding: 12px;
  }

  .modal-head {
    display: grid;
  }

  .save-conflict-warning,
  .save-conflict-actions {
    align-items: stretch;
    flex-direction: column;
  }
}

@media print {
  body > *:not(#printArea) {
    display: none !important;
  }

  #printArea {
    display: block;
  }

  @page {
    size: A4;
    margin: 16mm;
  }
}
