:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --text: #1a1d21;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #e11d48;
  --primary-hover: #be123c;
  --secondary: #334155;
  --secondary-hover: #1e293b;
  --pending-bg: #ffffff;
  --purchased-bg: #d1fae5;
  --purchased-border: #6ee7b7;
  --price: #dc2626;
  --low: #059669;
  --focus: #2563eb;
  --danger: #b91c1c;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 4px 12px rgba(15, 23, 42, 0.04);
  --radius: 12px;
  font-family: "Segoe UI", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

.page {
  max-width: 1400px;
  margin: 0 auto;
  padding: 24px 20px 48px;
}

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

.header h1 {
  margin: 0 0 4px;
  font-size: 1.6rem;
  letter-spacing: 0.02em;
}

.subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.status {
  font-size: 0.85rem;
  color: var(--muted);
  min-height: 1.2em;
}

.status.ok {
  color: var(--low);
}

.status.err {
  color: var(--danger);
}

.add-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 14px;
}

.add-form {
  display: flex;
  gap: 10px;
}

.add-form input {
  flex: 1;
  min-width: 0;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 16px; /* 避免 iOS 聚焦時自動放大 */
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.add-form input:focus {
  border-color: var(--focus);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.form-error {
  margin: 10px 0 0;
  color: var(--danger);
  font-size: 0.9rem;
}

.btn {
  border: none;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, transform 0.05s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.btn:active {
  transform: translateY(1px);
}

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

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

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--secondary);
  color: #fff;
}

.btn-secondary:hover:not(:disabled) {
  background: var(--secondary-hover);
}

.btn-ghost {
  background: transparent;
  color: var(--secondary);
  border: 1px solid var(--border);
  padding: 8px 12px;
  font-size: 0.85rem;
}

.btn-ghost:hover:not(:disabled) {
  background: #f8fafc;
}

.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid #fecaca;
  padding: 8px 12px;
  font-size: 0.85rem;
}

.btn-danger:hover:not(:disabled) {
  background: #fef2f2;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 16px;
  margin-bottom: 10px;
  font-size: 0.85rem;
  color: var(--muted);
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 4px;
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.swatch-pending {
  background: var(--pending-bg);
}

.swatch-purchased {
  background: var(--purchased-bg);
  border-color: var(--purchased-border);
}

.table-wrap {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.items-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 1100px;
  font-size: 0.9rem;
}

.items-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #f8fafc;
  text-align: left;
  padding: 12px 10px;
  border-bottom: 1px solid var(--border);
  color: #475569;
  font-weight: 600;
  white-space: nowrap;
}

.items-table td {
  padding: 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.items-table tbody tr {
  background: var(--pending-bg);
  transition: background 0.2s;
}

.items-table tbody tr.purchased {
  background: var(--purchased-bg);
}

.items-table tbody tr:hover {
  filter: brightness(0.985);
}

.empty-row td {
  text-align: center;
  color: var(--muted);
  padding: 36px 12px;
}

.col-id {
  width: 48px;
  color: var(--muted);
}

.col-name {
  min-width: 220px;
  max-width: 320px;
}

.col-price,
.col-low {
  width: 100px;
  white-space: nowrap;
}

.col-time {
  width: 150px;
  white-space: nowrap;
  color: var(--muted);
  font-size: 0.85rem;
}

.col-note,
.col-account,
.col-amount,
.col-ship {
  min-width: 110px;
}

.col-actions {
  width: 120px;
  white-space: nowrap;
}

.goods-name {
  font-weight: 600;
  margin-bottom: 4px;
  word-break: break-word;
}

.goods-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
}

.goods-link {
  display: inline-block;
  font-size: 0.8rem;
  color: #2563eb;
  text-decoration: none;
  padding: 2px 0;
}

.goods-link:hover {
  text-decoration: underline;
}

.goods-code {
  font-size: 0.75rem;
  color: var(--muted);
}

.price {
  color: var(--price);
  font-weight: 700;
  font-size: 1.05rem;
}

.price-empty {
  color: var(--muted);
  font-weight: 500;
}

.market-price {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  text-decoration: line-through;
}

.low-price {
  color: var(--low);
  font-weight: 700;
}

.low-time {
  display: block;
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 2px;
}

.cell-input,
.cell-textarea {
  width: 100%;
  border: 1px solid transparent;
  background: rgba(255, 255, 255, 0.55);
  border-radius: 8px;
  padding: 10px;
  font: inherit;
  font-size: 16px; /* 避免 iOS 聚焦縮放 */
  color: inherit;
  resize: vertical;
  min-height: 42px;
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
}

.cell-textarea {
  min-height: 56px;
}

.cell-input:hover,
.cell-textarea:hover {
  border-color: #cbd5e1;
}

.cell-input:focus,
.cell-textarea:focus {
  outline: none;
  border-color: var(--focus);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

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

.footer {
  margin-top: 18px;
  text-align: center;
  color: var(--muted);
  font-size: 0.82rem;
}

.footer code {
  background: #e2e8f0;
  padding: 1px 6px;
  border-radius: 4px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* ---------- 手機／平板：卡片式 ---------- */
@media (max-width: 900px) {
  .page {
    padding: 14px 12px 28px;
  }

  .header h1 {
    font-size: 1.35rem;
  }

  .subtitle {
    font-size: 0.85rem;
  }

  .header-actions {
    width: 100%;
    justify-content: space-between;
  }

  .header-actions .status {
    flex: 1;
    min-width: 0;
    order: 2;
    width: 100%;
  }

  .header-actions .btn {
    order: 1;
    margin-left: auto;
  }

  .add-form {
    flex-direction: column;
  }

  .add-form .btn {
    width: 100%;
    min-height: 44px;
  }

  .table-wrap {
    background: transparent;
    border: none;
    box-shadow: none;
    overflow: visible;
  }

  .items-table {
    min-width: 0;
    border-collapse: separate;
    border-spacing: 0 12px;
  }

  .items-table thead {
    display: none;
  }

  .items-table tbody tr {
    display: block;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 12px 12px 8px;
    margin: 0;
    background: var(--pending-bg);
  }

  .items-table tbody tr.purchased {
    background: var(--purchased-bg);
    border-color: var(--purchased-border);
  }

  .items-table tbody tr:hover {
    filter: none;
  }

  .items-table tbody tr.empty-row {
    box-shadow: none;
    border-style: dashed;
  }

  .items-table td {
    display: grid;
    grid-template-columns: 88px 1fr;
    gap: 8px 10px;
    align-items: start;
    padding: 8px 4px;
    border-bottom: 1px dashed #e5e7eb;
    width: 100%;
    max-width: none;
    min-width: 0;
    white-space: normal;
  }

  .items-table td:last-child {
    border-bottom: none;
  }

  .items-table td::before {
    content: attr(data-label);
    font-size: 0.75rem;
    font-weight: 600;
    color: #64748b;
    padding-top: 10px;
    line-height: 1.3;
  }

  .items-table td.col-id {
    grid-template-columns: 1fr;
    padding-top: 0;
    padding-bottom: 4px;
    border-bottom: none;
    font-weight: 700;
    color: var(--muted);
  }

  .items-table td.col-id::before {
    display: none;
  }

  .items-table td.col-name {
    grid-template-columns: 1fr;
    border-bottom: 1px solid var(--border);
    padding-bottom: 12px;
    margin-bottom: 4px;
  }

  .items-table td.col-name::before {
    display: none;
  }

  .items-table td.col-name .goods-name {
    font-size: 1rem;
    margin-bottom: 8px;
  }

  /* 價格列並排：目前 / 低價 / 時間 */
  .items-table td.col-price,
  .items-table td.col-low,
  .items-table td.col-time {
    grid-template-columns: 88px 1fr;
  }

  .items-table td.col-price .price {
    font-size: 1.15rem;
  }

  .items-table td.col-actions {
    grid-template-columns: 1fr;
    padding-top: 10px;
  }

  .items-table td.col-actions::before {
    display: none;
  }

  .actions {
    flex-direction: row;
    gap: 8px;
  }

  .actions .btn {
    flex: 1;
    min-height: 44px;
  }

  .empty-row td {
    display: block;
    text-align: center;
    padding: 28px 12px;
  }

  .empty-row td::before {
    display: none;
  }

  .footer {
    font-size: 0.78rem;
    padding: 0 4px;
  }
}

@media (max-width: 420px) {
  .items-table td {
    grid-template-columns: 76px 1fr;
  }

  .legend {
    font-size: 0.8rem;
  }
}
