:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f3f7;
  --ink: #17202a;
  --muted: #637083;
  --line: #d9e0ea;
  --blue: #2563eb;
  --green: #17803d;
  --orange: #c76a16;
  --red: #c73535;
  --purple: #7c3aed;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

button,
input,
select {
  font: inherit;
}

button {
  min-height: 36px;
  border: 0;
  border-radius: 6px;
  padding: 0 14px;
  background: var(--blue);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.primary {
  background: var(--blue);
}

.ghost {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
}

.danger {
  background: var(--red);
  color: #fff;
}

.success {
  background: var(--green);
  color: #fff;
}

.import {
  background: var(--purple);
  color: #fff;
}

.backup {
  background: #1f3a8a;
  color: #fff;
}

.reject {
  background: var(--orange);
  color: #fff;
}

.warning {
  background: var(--orange);
  color: #fff;
}

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

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card {
  width: min(420px, 100%);
  display: grid;
  gap: 16px;
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 18px 45px rgba(23, 32, 42, 0.08);
}

.login-footnote {
  text-align: center;
  color: var(--muted);
  font-size: 12px;
}

.food-search-cell {
  position: relative;
  min-width: 0;
}

.food-suggestions,
.food-search-message {
  position: absolute;
  z-index: 20;
  top: calc(100% + 4px);
  left: 0;
  width: 320px;
  max-height: 240px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.16);
}

.food-suggestions button {
  display: block;
  width: 100%;
  padding: 8px 10px;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: #fff;
  color: var(--ink);
  text-align: left;
}

.food-suggestions button:hover {
  background: #eff6ff;
}

.food-suggestions span,
.food-search-message {
  display: block;
  padding: 8px 10px;
  color: var(--muted);
  font-size: 12px;
}

.floating-food-suggestions {
  position: fixed;
  z-index: 1000;
}

.file-button {
  display: inline-grid;
  min-height: 36px;
  place-items: center;
  border-radius: 6px;
  padding: 0 14px;
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}

.file-button input {
  display: none;
}

.file-button.disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.import-progress-card {
  width: min(360px, 100%);
  justify-items: center;
  text-align: center;
}

.spinner {
  width: 34px;
  height: 34px;
  border: 4px solid #dbe4f0;
  border-top-color: var(--purple);
  border-radius: 999px;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.old-value {
  color: var(--muted);
  text-decoration: line-through;
}

.new-value {
  color: var(--red);
  font-weight: 800;
}

.error-text {
  color: var(--red);
}

.metric-grid.compact {
  grid-template-columns: repeat(5, minmax(120px, 1fr));
  padding: 0 0 12px;
}

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

.brand-mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--blue);
  color: #fff;
  font-weight: 900;
}

h1,
h2,
h3,
p {
  margin: 0;
}

.brand-row h1 {
  font-size: 18px;
}

.brand-row p,
.topbar p,
.hint,
.panel-heading span,
.form-status {
  color: var(--muted);
  font-size: 13px;
}

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

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

input:focus,
select:focus {
  outline: 2px solid rgba(37, 99, 235, 0.16);
  border-color: var(--blue);
}

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

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 22px;
  border-right: 1px solid var(--line);
  background: #fbfcfe;
}

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

.nav button {
  justify-content: flex-start;
  text-align: left;
  background: transparent;
  color: var(--ink);
}

.nav button.active,
.nav button:hover {
  background: #eaf1ff;
  color: var(--blue);
}

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

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

.role-chip,
.tag {
  display: inline-grid;
  min-height: 26px;
  place-items: center;
  border-radius: 999px;
  padding: 0 10px;
  background: #eaf1ff;
  color: var(--blue);
  font-size: 12px;
  font-weight: 800;
}

.tag.green {
  background: #e8f7ee;
  color: var(--green);
}

.tag.blue {
  background: #eaf1ff;
  color: var(--blue);
}

.tag.orange {
  background: #fff2e5;
  color: var(--orange);
}

.tag.red {
  background: #ffebeb;
  color: var(--red);
}

.tag.dark {
  background: #3f4652;
  color: #fff;
}

.tag.gray {
  background: #eef1f5;
  color: var(--muted);
}

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

.metric,
.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.metric {
  display: grid;
  gap: 8px;
  padding: 16px;
}

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

.metric strong {
  font-size: 24px;
}

.panel {
  padding: 16px;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}

.tabs button {
  background: #fff;
  border: 1px solid var(--line);
  color: var(--ink);
}

.tabs button.active {
  border-color: var(--blue);
  background: var(--blue);
  color: #fff;
}

.split {
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
  gap: 14px;
  align-items: start;
}

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

.check {
  display: flex;
  align-items: center;
  gap: 8px;
}

.check input {
  width: 18px;
  min-height: 18px;
}

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

.danger-text {
  color: #b42318;
}

.edit-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(180px, 1fr));
  gap: 10px;
}

.edit-grid .full {
  grid-column: 1 / -1;
}

.table-wrap {
  width: 100%;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
}

table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  font-size: 13px;
}

th,
td {
  min-width: 92px;
  border: 1px solid var(--line);
  padding: 8px 10px;
  text-align: left;
  white-space: nowrap;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-2);
  font-weight: 800;
}

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

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

.form-status.success {
  color: var(--green);
}

.form-status.error {
  color: var(--red);
}

.purchase-panel {
  display: grid;
  gap: 12px;
}

.purchase-toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.filter-bar {
  display: grid;
  grid-template-columns: repeat(6, minmax(140px, 1fr));
  gap: 10px;
  align-items: end;
}

.export-filter-bar {
  grid-template-columns: repeat(4, minmax(160px, 1fr));
}

.export-preview {
  display: grid;
  gap: 10px;
}

.export-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.export-summary span {
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  font-size: 13px;
  font-weight: 700;
}

.import-help {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 13px;
}

.import-preview-table .diff-old {
  color: var(--muted);
  text-decoration: line-through;
}

.import-preview-table .diff-new {
  color: var(--red);
  font-weight: 900;
}

.import-preview-table .diff-arrow {
  color: var(--red);
}

.import-row-error td {
  background: #fff7f7;
}

.error-cell {
  color: var(--red);
  font-weight: 700;
}

.purchase-wrap {
  max-height: calc(100vh - 230px);
  --purchase-zoom: 1;
}

.purchase-table th,
.purchase-table td {
  min-width: 48px;
  padding: calc(8px * var(--purchase-zoom));
  font-size: calc(13px * var(--purchase-zoom));
  vertical-align: middle;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.purchase-table {
  table-layout: fixed;
}

.purchase-table th:first-child,
.purchase-table td:first-child {
  position: sticky;
  left: 0;
  z-index: 2;
  background: #fff;
}

.purchase-table th:first-child {
  background: var(--surface-2);
  z-index: 3;
}

.purchase-table input,
.purchase-table select {
  width: 100%;
  min-width: 0;
  min-height: calc(30px * var(--purchase-zoom));
  font-size: calc(13px * var(--purchase-zoom));
}

.purchase-table .row-actions {
  min-width: 180px;
}

.purchase-table th {
  position: relative;
  user-select: none;
}

.purchase-table th:first-child {
  position: sticky;
}

.column-resizer {
  position: absolute;
  top: 0;
  right: -4px;
  width: 8px;
  height: 100%;
  cursor: col-resize;
  z-index: 5;
}

.column-resizer:hover {
  background: rgba(37, 99, 235, 0.16);
}

.resizing-column {
  cursor: col-resize;
  user-select: none;
}

.zoom-control {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
}

.zoom-control select {
  min-height: 32px;
  width: auto;
}

.autosave-status {
  margin-top: 4px;
  font-size: 12px;
  color: var(--muted);
}

.autosave-status.success {
  color: var(--green);
}

.autosave-status.error {
  color: var(--red);
  white-space: normal;
}

.finance-actions {
  min-width: 230px;
}

.number-cell {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.code-note {
  color: var(--muted);
  font-size: 12px;
  cursor: help;
}

.readonly-row {
  background: #f3f6fa;
  color: #5d6878;
}

.readonly-row input,
.readonly-row select,
.readonly-row button {
  background: #eef2f6;
  color: #6b7280;
}

.panel.empty {
  display: grid;
  gap: 10px;
}

.modal.hidden {
  display: none;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(23, 32, 42, 0.35);
}

.modal-card {
  width: min(1100px, 100%);
  max-height: 86vh;
  overflow: auto;
  display: grid;
  gap: 12px;
  padding: 18px;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 18px 50px rgba(23, 32, 42, 0.22);
}

.log-detail-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(160px, 1fr));
  gap: 8px;
}

pre {
  max-height: 220px;
  overflow: auto;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f8fafc;
  white-space: pre-wrap;
}

@media (max-width: 900px) {
  .layout,
  .split {
    grid-template-columns: 1fr;
  }

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

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

@media (max-width: 768px) {
  body {
    min-height: 100dvh;
    overflow-x: hidden;
  }

  .login-page {
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  }

  .login-card {
    width: 100%;
    max-width: 420px;
    gap: 18px;
    padding: 24px 20px;
    border-radius: 10px;
  }

  .login-card .brand-row {
    align-items: flex-start;
  }

  .login-card .brand-mark {
    width: 44px;
    height: 44px;
    border-radius: 10px;
    flex: 0 0 auto;
  }

  .login-card .brand-row h1 {
    font-size: 22px;
    line-height: 1.25;
  }

  .login-card .brand-row p {
    margin-top: 4px;
    font-size: 14px;
  }

  .login-card label {
    gap: 8px;
    font-size: 14px;
  }

  .login-card input {
    width: 100%;
    min-height: 46px;
    padding: 0 12px;
    font-size: 16px;
    border-radius: 8px;
  }

  .login-card button {
    width: 100%;
    min-height: 46px;
    font-size: 16px;
    border-radius: 8px;
  }

  .login-card .form-status {
    min-height: 20px;
    font-size: 14px;
    line-height: 1.45;
  }

  .login-footnote {
    font-size: 12px;
  }
}

@media (max-width: 560px) {
  .metric-grid {
    grid-template-columns: 1fr;
  }
}
