:root {
  color-scheme: light;
  --canvas: #f4f6f5;
  --surface: #ffffff;
  --surface-subtle: #f8faf9;
  --text: #202723;
  --text-muted: #68736d;
  --border: #dbe2de;
  --border-strong: #c8d2cd;
  --accent: #087f5b;
  --accent-hover: #066a4c;
  --accent-soft: #e8f5ef;
  --blue: #2563a6;
  --blue-soft: #edf4fb;
  --warning: #a86612;
  --warning-soft: #fff7e6;
  --danger: #b83a35;
  --danger-hover: #9f2f2b;
  --danger-soft: #fff0ef;
  --focus: #2f80ed;
  --radius: 6px;
  --shadow-dialog: 0 20px 60px rgba(27, 38, 32, 0.18);
  --fast: 140ms;
  --normal: 200ms;
  font-family: Inter, "Segoe UI", "Microsoft YaHei UI", "Microsoft YaHei", sans-serif;
  font-synthesis: none;
  letter-spacing: 0;
}

* { box-sizing: border-box; }

html { min-width: 320px; background: var(--canvas); }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--canvas);
  color: var(--text);
  font-size: 14px;
  line-height: 1.55;
  letter-spacing: 0;
}

button, input, select, textarea { font: inherit; letter-spacing: 0; }
button { color: inherit; }
a { color: inherit; }

button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
a:focus-visible {
  outline: 3px solid rgba(47, 128, 237, 0.26);
  outline-offset: 1px;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(12px);
}

.header-inner {
  width: min(1540px, calc(100% - 40px));
  height: 68px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
}

.brand-block, .login-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  flex: 0 0 38px;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
}

.brand-mark svg { width: 22px; height: 22px; }

.brand-block h1, .login-brand h1 {
  margin: 0;
  font-size: 17px;
  line-height: 1.25;
  font-weight: 700;
}

.brand-block p, .login-brand p {
  margin: 2px 0 0;
  color: var(--text-muted);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions, .inline-actions, .form-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.button, .icon-button {
  border: 1px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background-color var(--fast), border-color var(--fast), color var(--fast), opacity var(--fast);
}

.button {
  min-height: 38px;
  padding: 8px 13px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  font-weight: 600;
  white-space: nowrap;
}

.button svg { width: 16px; height: 16px; }
.button.primary { color: #fff; background: var(--accent); border-color: var(--accent); }
.button.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.button.secondary { background: var(--surface); border-color: var(--border-strong); }
.button.secondary:hover, .button.ghost:hover { background: var(--surface-subtle); border-color: var(--border-strong); }
.button.ghost { background: transparent; border-color: transparent; color: var(--text-muted); }
.button.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.button.danger:hover { background: var(--danger-hover); border-color: var(--danger-hover); }
.button:disabled, .icon-button:disabled { cursor: not-allowed; opacity: 0.52; }

.icon-button {
  width: 38px;
  height: 38px;
  padding: 0;
  display: inline-grid;
  place-items: center;
  background: transparent;
  color: var(--text-muted);
}

.icon-button:hover { color: var(--text); background: #edf1ef; }
.icon-button svg { width: 18px; height: 18px; pointer-events: none; }

.app-main {
  width: min(1540px, calc(100% - 40px));
  margin: 0 auto;
  padding: 18px 0 48px;
}

.tabs {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border);
}

.tab {
  position: relative;
  min-height: 42px;
  padding: 8px 14px;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  display: inline-flex;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  font-weight: 600;
}

.tab::after {
  content: "";
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: -1px;
  height: 2px;
  background: transparent;
}

.tab svg { width: 17px; height: 17px; }
.tab:hover { color: var(--text); }
.tab.is-active { color: var(--accent); }
.tab.is-active::after { background: var(--accent); }

.tab-panel[hidden] { display: none; }

.filter-band {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.filter-grid {
  display: grid;
  grid-template-columns: minmax(220px, 1.45fr) repeat(4, minmax(130px, 0.75fr));
  gap: 12px;
}

.field {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field > span, .field legend {
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
}

.field input, .field select, .field textarea {
  width: 100%;
  min-height: 38px;
  padding: 8px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  background: var(--surface);
  color: var(--text);
  transition: border-color var(--fast), box-shadow var(--fast);
}

.field textarea { min-height: 128px; resize: vertical; line-height: 1.55; }
.field input:hover, .field select:hover, .field textarea:hover { border-color: #aebbb4; }
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--focus); outline: 0; box-shadow: 0 0 0 3px rgba(47, 128, 237, 0.12); }
.field input::placeholder, .field textarea::placeholder { color: #9aa49f; }

.input-with-icon { position: relative; }
.input-with-icon svg { position: absolute; left: 10px; top: 10px; width: 17px; height: 17px; color: var(--text-muted); pointer-events: none; }
.input-with-icon input { padding-left: 34px; }

.price-range { display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); align-items: center; gap: 6px; }
.price-range > span { color: var(--text-muted); font-size: 12px; }

.filter-footer {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.checkbox-control, .toggle-control, .checkbox-group label {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--text-muted);
  cursor: pointer;
}

input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent); }

.metrics {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  margin: 14px 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.metric { min-width: 0; padding: 14px 16px; border-right: 1px solid var(--border); }
.metric:last-child { border-right: 0; }
.metric span, .metric small { display: block; color: var(--text-muted); }
.metric span { font-size: 12px; font-weight: 600; }
.metric strong { display: block; margin: 4px 0 1px; font-size: 21px; line-height: 1.25; font-weight: 700; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.metric small { font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.data-section, .section-toolbar, .account-panel { background: var(--surface); }
.data-section, .section-toolbar { border: 1px solid var(--border); border-radius: var(--radius); }

.section-heading, .section-toolbar {
  min-height: 64px;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.section-heading { border-bottom: 1px solid var(--border); }
.section-toolbar { margin-bottom: 12px; }
.section-heading h2, .section-toolbar h2, .dialog-header h2 { margin: 0; font-size: 15px; line-height: 1.35; }
.section-heading p, .section-toolbar p, .dialog-header p { margin: 2px 0 0; color: var(--text-muted); font-size: 12px; }

.sync-health, .account-state {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--text-muted);
  font-size: 12px;
}

.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #9aa49f; box-shadow: 0 0 0 3px #edf1ef; }
.sync-health.is-healthy .status-dot, .account-state.is-connected .status-dot { background: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.sync-health.is-warning .status-dot, .account-state.is-warning .status-dot { background: var(--warning); box-shadow: 0 0 0 3px var(--warning-soft); }
.sync-health.is-error .status-dot, .account-state.is-error .status-dot { background: var(--danger); box-shadow: 0 0 0 3px var(--danger-soft); }

.table-wrap { width: 100%; overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; table-layout: fixed; }
.data-table th { height: 40px; padding: 9px 12px; border-bottom: 1px solid var(--border); background: var(--surface-subtle); color: var(--text-muted); text-align: left; font-size: 11px; font-weight: 700; }
.data-table td { min-height: 54px; padding: 10px 12px; border-bottom: 1px solid #e8ecea; vertical-align: middle; }
.data-table tbody tr:last-child td { border-bottom: 0; }
.data-table tbody tr:hover { background: #fafcfb; }
.data-table .number { text-align: right; font-variant-numeric: tabular-nums; }
.data-table .actions-column { width: 104px; text-align: right; }
.product-table th:nth-child(1) { width: 31%; }
.product-table th:nth-child(2) { width: 14%; }
.product-table th:nth-child(3) { width: 9%; }
.product-table th:nth-child(4) { width: 9%; }
.product-table th:nth-child(5) { width: 8%; }
.product-table th:nth-child(6) { width: 9%; }
.product-table th:nth-child(7) { width: 13%; }
.source-table th:nth-child(1) { width: 22%; }
.source-table th:nth-child(2) { width: 20%; }

.product-name { display: block; font-weight: 600; line-height: 1.45; overflow-wrap: anywhere; }
.product-category, .source-meta, .cell-subline { display: block; margin-top: 3px; color: var(--text-muted); font-size: 11px; overflow-wrap: anywhere; }
.price-value { font-weight: 700; color: var(--text); font-variant-numeric: tabular-nums; }
.price-value::before { content: "¥"; margin-right: 1px; color: var(--text-muted); font-size: 11px; }
.stock-value { display: block; font-weight: 650; font-variant-numeric: tabular-nums; }
.stock-label { display: block; margin-top: 1px; color: var(--text-muted); font-size: 11px; }

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 3px 7px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 650;
  white-space: nowrap;
}

.badge.success { color: #08633f; background: var(--accent-soft); }
.badge.warning { color: #8a560d; background: var(--warning-soft); }
.badge.danger { color: #9d2f2b; background: var(--danger-soft); }
.badge.info { color: #245a90; background: var(--blue-soft); }
.badge.neutral { color: #5f6964; background: #eef1ef; }

.delta { font-size: 12px; font-variant-numeric: tabular-nums; }
.delta.up { color: var(--danger); }
.delta.down { color: var(--accent); }
.delta.flat { color: var(--text-muted); }

.row-actions { display: flex; align-items: center; justify-content: flex-end; gap: 2px; }
.row-actions .icon-button { width: 32px; height: 32px; }
.row-actions .icon-button svg { width: 16px; height: 16px; }
.row-actions .danger-action:hover { color: var(--danger); background: var(--danger-soft); }

.loading-row td, .empty-row td { height: 180px; text-align: center; color: var(--text-muted); }
.spinner { display: inline-block; width: 16px; height: 16px; margin-right: 8px; border: 2px solid var(--border-strong); border-top-color: var(--accent); border-radius: 50%; vertical-align: -3px; animation: spin 700ms linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.pagination { min-height: 54px; padding: 8px 14px; border-top: 1px solid var(--border); display: flex; align-items: center; justify-content: center; gap: 12px; }
.pagination span { color: var(--text-muted); font-size: 12px; font-variant-numeric: tabular-nums; }

.settings-form {
  max-width: 700px;
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}
.settings-form .security-note, .settings-form .form-actions { grid-column: 1 / -1; }
.security-note { padding: 12px; border-left: 3px solid var(--blue); background: var(--blue-soft); display: flex; align-items: flex-start; gap: 9px; color: #37546e; }
.security-note svg { width: 18px; height: 18px; flex: 0 0 18px; margin-top: 1px; }
.security-note p { margin: 0; font-size: 12px; }

.dialog { width: min(680px, calc(100% - 28px)); max-height: calc(100dvh - 40px); padding: 0; border: 1px solid var(--border-strong); border-radius: 8px; background: var(--surface); box-shadow: var(--shadow-dialog); color: var(--text); }
.dialog::backdrop { background: rgba(23, 31, 27, 0.45); }
.dialog-shell { margin: 0; display: flex; flex-direction: column; max-height: calc(100dvh - 42px); }
.dialog-header, .dialog-footer { flex: 0 0 auto; padding: 14px 16px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.dialog-header { border-bottom: 1px solid var(--border); }
.dialog-header p { max-width: 540px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dialog-body { padding: 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
.dialog-footer { border-top: 1px solid var(--border); justify-content: flex-end; }
.form-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.checkbox-group { margin: 0; padding: 0; border: 0; flex-direction: row; flex-wrap: wrap; align-items: center; gap: 10px 16px; }
.checkbox-group legend { width: 100%; margin-bottom: 2px; }
.toggle-control { align-self: end; min-height: 38px; }
.form-error { padding: 10px 12px; border: 1px solid #efc1be; border-radius: 5px; background: var(--danger-soft); color: #922e2a; font-size: 12px; white-space: pre-line; }
.confirm-dialog { width: min(440px, calc(100% - 28px)); }

.toast-region { position: fixed; right: 18px; bottom: 18px; z-index: 100; display: flex; flex-direction: column; gap: 8px; pointer-events: none; }
.toast { width: min(360px, calc(100vw - 36px)); padding: 11px 13px; border: 1px solid var(--border-strong); border-left: 4px solid var(--accent); border-radius: var(--radius); background: var(--surface); box-shadow: 0 8px 26px rgba(27, 38, 32, 0.14); animation: toast-in var(--normal) ease-out; }
.toast.error { border-left-color: var(--danger); }
.toast.warning { border-left-color: var(--warning); }
.toast strong { display: block; font-size: 13px; }
.toast span { display: block; margin-top: 2px; color: var(--text-muted); font-size: 12px; overflow-wrap: anywhere; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

.login-page { display: grid; place-items: center; padding: 24px; }
.login-shell { width: min(420px, 100%); }
.login-brand { margin-bottom: 18px; }
.login-brand .brand-mark { width: 42px; height: 42px; flex-basis: 42px; }
.login-brand h1 { font-size: 20px; }
.login-form { padding: 22px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); display: flex; flex-direction: column; gap: 16px; }
.login-submit { width: 100%; margin-top: 2px; justify-content: space-between; }

@media (max-width: 1180px) {
  .filter-grid { grid-template-columns: repeat(4, minmax(140px, 1fr)); }
  .field-wide { grid-column: span 2; }
  .product-table th:nth-child(1) { width: 290px; }
  .product-table th:nth-child(2) { width: 150px; }
}

@media (max-width: 820px) {
  .header-inner, .app-main { width: min(100% - 24px, 1540px); }
  .filter-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .field-wide { grid-column: 1 / -1; }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metric:nth-child(2) { border-right: 0; }
  .metric:nth-child(-n + 2) { border-bottom: 1px solid var(--border); }
  .settings-form { grid-template-columns: 1fr; }
  .settings-form .security-note, .settings-form .form-actions { grid-column: 1; }
}

@media (max-width: 620px) {
  body { font-size: 13px; }
  .header-inner { height: 62px; }
  .brand-block p { max-width: 150px; }
  .header-actions .icon-button { display: none; }
  .header-actions .button { min-width: 42px; padding: 8px 10px; }
  .header-actions .button span { display: none; }
  .app-main { padding-top: 10px; }
  .tabs { overflow-x: auto; }
  .tab { flex: 1 0 auto; justify-content: center; padding-inline: 10px; }
  .filter-band { padding: 12px; }
  .filter-grid { grid-template-columns: 1fr 1fr; gap: 10px; }
  .field-wide, .price-field { grid-column: 1 / -1; }
  .filter-footer { align-items: flex-start; flex-direction: column; }
  .inline-actions { width: 100%; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .inline-actions .button { padding-inline: 8px; }
  .metrics { margin-block: 10px; }
  .metric { padding: 12px; }
  .metric strong { font-size: 18px; }
  .section-heading, .section-toolbar { min-height: 58px; padding: 10px 12px; }
  .section-toolbar { align-items: flex-start; }
  .sync-health { display: none; }

  .product-table, .source-table { min-width: 0; }
  .product-table thead, .source-table thead { display: none; }
  .product-table tbody, .product-table tr, .product-table td,
  .source-table tbody, .source-table tr, .source-table td { display: block; width: 100%; }
  .product-table tr, .source-table tr { padding: 12px; border-bottom: 1px solid var(--border); }
  .product-table tr:last-child, .source-table tr:last-child { border-bottom: 0; }
  .product-table td, .source-table td { min-height: 0; padding: 4px 0; border: 0; text-align: left; }
  .product-table td[data-label], .source-table td[data-label] { display: grid; grid-template-columns: 82px minmax(0, 1fr); gap: 8px; align-items: start; }
  .product-table td[data-label]::before, .source-table td[data-label]::before { content: attr(data-label); color: var(--text-muted); font-size: 11px; font-weight: 650; }
  .product-table td:first-child, .source-table td:first-child { margin-bottom: 7px; }
  .product-table .number { text-align: left; }
  .row-actions { justify-content: flex-start; padding-left: 82px; }
  .loading-row td, .empty-row td { height: 140px; display: grid; place-items: center; }
  .form-row { grid-template-columns: 1fr; }
  .dialog-body { padding: 14px; }
  .toast-region { right: 12px; bottom: 12px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}
