:root {
  --bg: #f5f1ea;
  --card: #ffffff;
  --line: #e6dfd4;
  --fg: #2b2419;
  --fg-muted: #7d7465;
  --accent: #b56a3e;
  --accent-ink: #ffffff;
  --green: #1d7a4d;
  --green-bg: #e7f5ec;
  --yellow: #8a6a1f;
  --yellow-bg: #faf3dd;
  --red: #b3402f;
  --red-bg: #fbe9e6;
  --blue: #2f5f9e;
  --blue-bg: #e9f0fa;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

.topbar {
  background: #2b2218;
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar-inner {
  max-width: 1400px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.brand { font-weight: 600; letter-spacing: 0.02em; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }

.wrap { max-width: 1400px; margin: 0 auto; padding: 16px 12px 60px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 24px;
}
.login-card { max-width: 420px; margin: 40px auto; }
.login-card h1 { margin: 0 0 4px; font-size: 20px; }
.login-card .muted { margin: 0 0 16px; }

h1 { font-size: 20px; margin: 0; }
.muted { color: var(--fg-muted); }
.small { font-size: 12px; }
.error { color: var(--red); font-size: 13px; margin-top: 10px; }

input[type="text"], input[type="password"], input[type="number"] {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 15px;
  background: #fff;
  color: var(--fg);
  font-family: inherit;
}
input:focus { outline: none; border-color: var(--accent); }
label { display: block; font-size: 13px; color: var(--fg-muted); margin-top: 12px; }
label input { margin-top: 6px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--fg);
  border-radius: 8px;
  padding: 9px 14px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  transition: border-color 0.15s, background 0.15s, opacity 0.15s;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: #9d5a33; color: #fff; }
.btn-block { width: 100%; margin-top: 14px; }
.btn-sm { padding: 7px 12px; font-size: 13px; }
.btn-xs { padding: 4px 10px; font-size: 12px; border-radius: 6px; }

/* ── Filters ─────────────────────────────────────────────────────────────── */
.filters {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--fg-muted);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 500; }
.toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--fg);
  margin: 0;
  cursor: pointer;
  white-space: nowrap;
}
.toggle input { width: auto; margin: 0; }
.filters #search { width: 190px; padding: 7px 10px; font-size: 13px; }

/* ── Table ───────────────────────────────────────────────────────────────── */
.table-scroll {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  -webkit-overflow-scrolling: touch;
}
.orders-table {
  border-collapse: collapse;
  min-width: 100%;
  font-size: 13px;
}
.orders-table th, .orders-table td {
  border-bottom: 1px solid var(--line);
  border-right: 1px solid var(--line);
  padding: 8px 10px;
  text-align: left;
  vertical-align: top;
  min-width: 96px;
  max-width: 220px;
}
.orders-table tr:last-child td { border-bottom: 0; }
.orders-table thead th {
  background: #faf7f2;
  color: var(--fg-muted);
  font-weight: 500;
  font-size: 12px;
  white-space: nowrap;
}
.orders-table tr.order-row { cursor: pointer; }
.orders-table tr.order-row:hover td { background: #fcf8f2; }
.orders-table tr.order-row.expanded td { background: #fdf6ee; }

.expand-col { width: 34px; min-width: 34px !important; text-align: center !important; }
.expand-btn {
  background: none;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--fg-muted);
  width: 26px;
  height: 26px;
  line-height: 1;
  font-size: 13px;
  cursor: pointer;
}
.expand-btn:hover { border-color: var(--accent); color: var(--accent); }

.order-id-short { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; font-weight: 600; color: var(--fg); }
.order-date { font-size: 11px; color: var(--fg-muted); margin-top: 2px; }
.badges { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 6px; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 600;
  border-radius: 999px;
  padding: 2px 8px;
  white-space: nowrap;
}
.badge-green { background: var(--green-bg); color: var(--green); }
.badge-yellow { background: var(--yellow-bg); color: var(--yellow); }
.badge-red { background: var(--red-bg); color: var(--red); }
.badge-blue { background: var(--blue-bg); color: var(--blue); }
.badge-gray { background: #efece6; color: var(--fg-muted); }

.cell-sub { font-size: 11px; color: var(--fg-muted); margin-top: 2px; }
.cell-mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; word-break: break-all; }
.cell-empty { color: var(--fg-muted); }

/* Expanded detail row (rendered under the order row) */
tr.detail-row td {
  background: #fdf6ee;
  border-top: 2px solid var(--accent);
  border-bottom: 1px solid var(--line);
  padding: 0;
}
.detail-inner {
  max-width: 860px;
  padding: 14px 16px;
}

/* ── Detail panel ────────────────────────────────────────────────────────── */
.detail {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  margin-top: 14px;
  padding: 18px;
}
.detail-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.detail-head h3 { margin: 0; font-size: 16px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.detail-close { background: none; border: 0; font-size: 18px; cursor: pointer; color: var(--fg-muted); line-height: 1; padding: 2px 6px; }

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px 16px;
  margin-bottom: 14px;
}
.detail-grid .dg-item .dg-label { font-size: 11px; color: var(--fg-muted); text-transform: none; }
.detail-grid .dg-item .dg-value { font-size: 14px; font-weight: 500; }

.field-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.field-row .label { width: 84px; flex-shrink: 0; font-size: 13px; color: var(--fg-muted); margin: 0; }
.field-row input[type="text"], .field-row input[type="number"] {
  flex: 1;
  min-width: 120px;
  max-width: 260px;
}
.divider { border-top: 1px dashed var(--line); margin: 12px 0; }
.info-line { font-size: 13px; color: var(--fg-muted); margin-bottom: 6px; }
.info-line .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--fg); }
.ship-inputs { display: flex; gap: 10px; flex-wrap: wrap; }
.history { font-size: 12px; color: var(--fg-muted); }
.history details summary { cursor: pointer; color: var(--fg-muted); font-size: 12px; }
.history .h-row { margin: 3px 0; }

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: #2b2218;
  color: #fff;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 100;
  box-shadow: 0 6px 24px rgba(0,0,0,0.25);
  max-width: 90vw;
}

@media (max-width: 520px) {
  .filters #search { width: 100%; }
  .field-row .label { width: 100%; }
  .field-row input[type="text"], .field-row input[type="number"] { max-width: 100%; }
}

/* ── Print thumbnail ─────────────────────────────────────────────────────── */
.thumb-cell { min-width: 62px !important; }
.thumb-btn { background: none; border: 0; padding: 0; cursor: pointer; display: inline-block; }
.thumb-btn img {
  width: 46px;
  height: 46px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #f3eee6;
  display: block;
}
.thumb-btn img:hover { border-color: var(--accent); }
.thumb-placeholder {
  display: inline-flex;
  width: 46px;
  height: 46px;
  align-items: center;
  justify-content: center;
  border: 1px dashed var(--line);
  border-radius: 8px;
  font-size: 11px;
  color: var(--fg-muted);
}
.detail-thumb { margin-bottom: 12px; }
.thumb-big img {
  width: 190px;
  height: 190px;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #f3eee6;
}

/* ── Amount calculation popover ──────────────────────────────────────────── */
.amount-clickable {
  cursor: pointer;
  border-bottom: 1px dashed var(--fg-muted);
}
.amount-input-wrap {
  flex: 1;
  min-width: 120px;
  max-width: 260px;
  cursor: pointer;
}
.amount-input-wrap input { width: 100%; }
.amount-pop {
  position: fixed;
  z-index: 200;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
  padding: 12px 14px;
  min-width: 230px;
  font-size: 13px;
}
.amount-pop-title { font-weight: 600; margin-bottom: 8px; }
.amount-pop-row {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 2px 0;
  color: var(--fg-muted);
}
.amount-pop-row .val { color: var(--fg); font-weight: 500; }
.amount-pop-total {
  border-top: 1px dashed var(--line);
  margin-top: 6px;
  padding-top: 6px;
  display: flex;
  justify-content: space-between;
  font-weight: 600;
}
