:root {
  --bg: #f4efe7;
  --panel: #fffaf4;
  --panel-strong: #fff;
  --line: #d7c8b4;
  --text: #2d241d;
  --muted: #6d5e52;
  --accent: #875b2f;
  --accent-soft: #f0e2cf;
  --danger: #b04b38;
  --ok: #2d7a4b;
  --warn: #b58528;
  --shadow: 0 18px 45px rgba(74, 51, 31, 0.1);
  --font-ui: "Noto Sans SC", "PingFang SC", "Microsoft YaHei", sans-serif;
  --font-head: "Source Han Serif SC", "Songti SC", serif;
}

html[data-locale="zh-Hant"] {
  --font-ui: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  --font-head: "Noto Serif TC", "Songti TC", serif;
}

html[data-font="serif"] {
  --font-ui: var(--font-head);
}

html[data-font="mono"] {
  --font-ui: "JetBrains Mono", "SFMono-Regular", "Consolas", monospace;
  --font-head: "JetBrains Mono", "SFMono-Regular", "Consolas", monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(circle at top left, rgba(255, 255, 255, 0.9), transparent 32%),
    linear-gradient(180deg, #f8f2ea 0%, var(--bg) 100%);
  color: var(--text);
  font-family: var(--font-ui);
}

body {
  padding: 20px;
}

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

.app-shell {
  display: grid;
  grid-template-columns: minmax(840px, 1fr) 380px;
  gap: 18px;
  align-items: start;
}

.panel {
  background: var(--panel);
  border: 1px solid rgba(135, 91, 47, 0.18);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

.entry-panel {
  padding: 22px;
}

.login-panel {
  max-width: 520px;
  margin: 0 auto 18px;
  padding: 22px;
}

.login-form {
  display: grid;
  gap: 14px;
}

.index-panel {
  position: sticky;
  top: 20px;
  padding: 18px;
  max-height: calc(100vh - 40px);
  overflow: auto;
}

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

.panel-header h1,
.panel-header h2,
.panel-header h3 {
  margin: 0;
  font-family: var(--font-head);
}

.panel-header-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

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

.meta-strip {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 12px;
  background: var(--accent-soft);
  color: var(--accent);
}

.editing-note {
  margin-bottom: 16px;
  padding: 12px 14px;
  border-radius: 14px;
  background: rgba(240, 226, 207, 0.7);
  border: 1px solid rgba(135, 91, 47, 0.18);
  color: var(--muted);
  font-size: 13px;
}

.restore-banner {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 12px 16px;
  background: #fff4df;
  border: 1px solid #f0cb81;
  border-radius: 14px;
  margin-bottom: 16px;
}

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

.restore-actions {
  display: flex;
  gap: 10px;
}

.ui-settings {
  display: flex;
  gap: 10px;
  align-items: end;
  flex-wrap: wrap;
}

.ui-settings.compact {
  gap: 8px;
  align-items: center;
}

.ui-settings label {
  display: grid;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
}

.ui-settings-group {
  display: grid;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
}

.ui-settings.compact label {
  min-width: 96px;
}

.ui-settings.compact .ui-settings-group {
  min-width: 120px;
}

.ui-settings select.field {
  min-width: 108px;
  padding: 8px 10px;
}

.ui-locale-switch {
  display: inline-flex;
  gap: 6px;
}

.ui-locale-button {
  padding: 8px 12px;
  min-width: 52px;
}

.field-grid {
  display: grid;
  grid-template-columns: minmax(260px, 1.2fr) repeat(5, minmax(96px, 1fr));
  gap: 10px;
  align-items: center;
}

.line-items-head,
.line-item-row {
  display: grid;
  grid-template-columns: minmax(260px, 1.4fr) 120px minmax(200px, 1.2fr) 140px 100px 140px 120px 88px;
  gap: 10px;
  align-items: center;
}

.line-items-head {
  padding: 0 0 10px;
  margin-top: 18px;
  color: var(--muted);
  font-size: 13px;
}

.line-items {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.line-item-row {
  position: relative;
  padding: 10px;
  border-radius: 14px;
  border: 1px solid rgba(135, 91, 47, 0.16);
  background: rgba(255, 255, 255, 0.72);
}

.line-item-row.pending {
  border-color: rgba(181, 133, 40, 0.3);
}

.line-item-row.unmatched {
  border-color: rgba(176, 75, 56, 0.35);
  background: rgba(255, 244, 242, 0.95);
}

.line-item-row.matched {
  border-color: rgba(45, 122, 75, 0.24);
}

.field-label {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}

.field,
.line-item-row input,
.line-item-row textarea,
.filters input,
.filters select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  background: var(--panel-strong);
  color: var(--text);
}

.line-item-row textarea {
  min-height: 42px;
  resize: vertical;
}

.field:focus,
.line-item-row input:focus,
.line-item-row textarea:focus,
.filters input:focus,
.filters select:focus {
  outline: 2px solid rgba(135, 91, 47, 0.2);
  border-color: var(--accent);
}

.readonly {
  background: #f6f1ea;
}

.search-wrap {
  position: relative;
}

.search-dropdown {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(100% + 6px);
  z-index: 5;
  background: var(--panel-strong);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.search-option {
  padding: 10px 12px;
  cursor: pointer;
  border-bottom: 1px solid #f0e4d5;
}

.search-option:last-child {
  border-bottom: 0;
}

.search-option.active,
.search-option:hover {
  background: #f6eadb;
}

.search-option strong {
  display: block;
}

.search-meta {
  color: var(--muted);
  font-size: 12px;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 88px;
  padding: 7px 10px;
  border-radius: 999px;
  font-size: 12px;
  border: 1px solid transparent;
}

.status-matched {
  color: var(--ok);
  background: rgba(45, 122, 75, 0.12);
  border-color: rgba(45, 122, 75, 0.18);
}

.status-unmatched {
  color: var(--danger);
  background: rgba(176, 75, 56, 0.12);
  border-color: rgba(176, 75, 56, 0.18);
}

.status-pending {
  color: var(--warn);
  background: rgba(181, 133, 40, 0.12);
  border-color: rgba(181, 133, 40, 0.18);
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 18px;
  gap: 12px;
}

.button-row {
  display: flex;
  gap: 10px;
}

button {
  border: 0;
  border-radius: 12px;
  padding: 10px 16px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}

button.secondary {
  background: #e8dccf;
  color: var(--text);
}

button.ghost {
  background: transparent;
  color: var(--accent);
  border: 1px solid rgba(135, 91, 47, 0.3);
}

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

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

.filters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 14px 0 12px;
}

.index-actions {
  display: flex;
  gap: 10px;
  margin-bottom: 12px;
}

.order-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.order-card {
  border: 1px solid rgba(135, 91, 47, 0.16);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.8);
  padding: 12px;
}

.order-card.selected {
  border-color: rgba(135, 91, 47, 0.45);
  box-shadow: 0 8px 24px rgba(135, 91, 47, 0.12);
}

.order-card-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: flex-start;
}

.order-card-title {
  font-weight: 700;
}

.order-card-meta {
  color: var(--muted);
  font-size: 12px;
  margin-top: 6px;
}

.order-card-actions {
  display: flex;
  gap: 10px;
  margin-top: 12px;
}

.preview-box {
  margin-top: 18px;
  border: 1px dashed rgba(135, 91, 47, 0.28);
  border-radius: 14px;
  padding: 14px;
  background: rgba(255, 255, 255, 0.55);
}

.preview-box h3 {
  margin: 0 0 12px;
  font-family: var(--font-head);
}

.preview-list {
  margin: 0;
  padding-left: 18px;
}

.preview-list li {
  margin-bottom: 8px;
}

.preview-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 12px;
}

.preview-table th,
.preview-table td {
  border: 1px solid #e5d7c4;
  padding: 8px 10px;
  text-align: left;
  font-size: 13px;
}

.delivery-shell {
  display: none;
  margin-top: 20px;
  padding: 18px;
  background: #fff;
  border-radius: 18px;
  border: 1px solid rgba(135, 91, 47, 0.18);
}

.delivery-shell.visible {
  display: block;
}

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

.delivery-sheet {
  background: #fff;
  color: #1f1a15;
}

.delivery-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  border-bottom: 2px solid #32281d;
  padding-bottom: 10px;
  margin-bottom: 18px;
}

.delivery-header h2 {
  margin: 0;
  font-family: var(--font-head);
}

.delivery-group {
  margin-bottom: 24px;
  break-inside: avoid;
}

.delivery-group h3 {
  margin: 0 0 10px;
  padding-bottom: 8px;
  border-bottom: 1px solid #ddd2c6;
}

.delivery-table {
  width: 100%;
  border-collapse: collapse;
}

.delivery-table th,
.delivery-table td {
  border: 1px solid #d7c8b4;
  padding: 8px 10px;
  text-align: left;
  vertical-align: top;
  font-size: 13px;
}

.empty-state {
  padding: 18px;
  text-align: center;
  color: var(--muted);
  border: 1px dashed rgba(135, 91, 47, 0.22);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.45);
}

.badge-warn {
  color: var(--warn);
  background: rgba(181, 133, 40, 0.14);
}

.danger-text {
  color: var(--danger);
  border-color: rgba(176, 75, 56, 0.3);
}

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

  .index-panel {
    position: static;
    max-height: none;
  }
}
