/* ============================================================
   Design tokens
   ============================================================ */
:root {
  --c-primary:        #1a73e8;
  --c-primary-dark:   #1557b0;
  --c-primary-light:  #e8f0fe;
  --c-success:        #0f5132;
  --c-success-bg:     #d1e7dd;
  --c-warning:        #86670f;
  --c-warning-bg:     #fff3cd;
  --c-danger:         #b3261e;
  --c-danger-bg:      #fce8e6;

  --c-text:           #111827;
  --c-text-muted:     #4b5563;
  --c-text-faint:     #9ca3af;

  --c-border:         #e5e7eb;
  --c-border-hover:   #c7d2fe;
  --c-bg:             #f5f6f8;
  --c-surface:        #ffffff;
  --c-surface-alt:    #f9fafb;

  --radius-sm:   8px;
  --radius-md:   12px;
  --radius-lg:   16px;
  --radius-pill: 999px;

  --shadow-sm:   0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-md:   0 4px 14px rgba(15, 23, 42, 0.08);
  --shadow-focus: 0 0 0 4px rgba(26, 115, 232, 0.18);

  --h-input:   52px;
  --h-btn:     52px;
  --h-btn-sm:  44px;
  --h-tab:     56px;

  --s-1:  4px;
  --s-2:  8px;
  --s-3:  12px;
  --s-4:  16px;
  --s-5:  20px;
  --s-6:  24px;
  --s-8:  32px;
}

/* ============================================================
   Base
   ============================================================ */
* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text",
               "Segoe UI", "PingFang TC", "Microsoft JhengHei", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: var(--c-text);
  background: var(--c-bg);
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h1, h2, h3, h4 { margin: 0; color: var(--c-text); }

/* ============================================================
   Tab bar
   ============================================================ */
.tab-bar {
  display: flex;
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--c-surface);
  box-shadow: var(--shadow-sm);
  border-bottom: 1px solid var(--c-border);
}
.tab-btn {
  flex: 1;
  height: var(--h-tab);
  font-size: 15px;
  font-weight: 500;
  background: none;
  border: none;
  color: var(--c-text-muted);
  cursor: pointer;
  position: relative;
  transition: color 0.15s;
}
.tab-btn::after {
  content: '';
  position: absolute;
  left: 20%;
  right: 20%;
  bottom: 0;
  height: 3px;
  background: transparent;
  border-radius: 2px 2px 0 0;
  transition: background 0.15s;
}
.tab-btn.active {
  color: var(--c-primary);
  font-weight: 600;
}
.tab-btn.active::after { background: var(--c-primary); }
.tab-btn:active { background: var(--c-surface-alt); }

/* ============================================================
   Layout
   ============================================================ */
main {
  max-width: 640px;
  margin: 0 auto;
  padding: var(--s-5) var(--s-4) var(--s-8);
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

h1 {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: var(--s-5);
  letter-spacing: -0.01em;
}

/* ============================================================
   Form controls (shared)
   ============================================================ */
form {
  display: flex;
  flex-direction: column;
  gap: var(--s-4);
}

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--c-text-muted);
  letter-spacing: 0.02em;
}

input, select, textarea {
  width: 100%;
  min-height: var(--h-input);
  padding: 14px 16px;
  font-size: 16px;
  line-height: 1.4;
  font-family: inherit;
  color: var(--c-text);
  background: var(--c-surface);
  border: 1.5px solid var(--c-border);
  border-radius: var(--radius-sm);
  transition: border-color 0.15s, box-shadow 0.15s;
  -webkit-appearance: none;
  appearance: none;
}
input:hover, select:hover, textarea:hover { border-color: var(--c-border-hover); }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--c-primary);
  box-shadow: var(--shadow-focus);
}

textarea {
  resize: vertical;
  min-height: 80px;
  padding-top: 12px;
}

select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 14 14'%3E%3Cpath d='M3.5 5.5l3.5 3.5 3.5-3.5' stroke='%236b7280' stroke-width='1.6' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  padding-right: 44px;
  cursor: pointer;
}

input[type="date"],
input[type="search"],
input[type="number"] {
  font-variant-numeric: tabular-nums;
}

input::placeholder,
textarea::placeholder {
  color: var(--c-text-faint);
}

/* ============================================================
   Field wrapper (for non-<label> fields)
   ============================================================ */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.field-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--c-text-muted);
  letter-spacing: 0.02em;
}

/* ============================================================
   Segmented controls (radio + label)
   ============================================================ */
.segmented {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s-2);
}
.segmented > input[type="radio"] {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  width: 1px;
  height: 1px;
  min-height: 0;
  padding: 0;
  border: 0;
  margin: 0;
}
.segmented > label {
  flex: 1 1 0;
  min-width: 72px;
  min-height: 48px;
  padding: 0 12px;
  margin: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1.5px solid var(--c-border);
  background: var(--c-surface);
  color: var(--c-text);
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-align: center;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.05s;
  -webkit-tap-highlight-color: transparent;
}
.segmented > label:hover { border-color: var(--c-border-hover); }
.segmented > label:active { transform: scale(0.97); }
.segmented > input[type="radio"]:checked + label {
  background: var(--c-primary);
  border-color: var(--c-primary);
  color: #fff;
}
.segmented > input[type="radio"]:focus-visible + label {
  box-shadow: var(--shadow-focus);
}

/* ============================================================
   Segmented buttons (dynamic, JS-controlled)
   Used inside dynamically rendered forms (Tab 2 edit panel)
   ============================================================ */
.segmented-btn {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s-2);
}
.seg-btn {
  flex: 1 1 0;
  min-width: 72px;
  min-height: 44px;
  padding: 0 12px;
  border: 1.5px solid var(--c-border);
  background: var(--c-surface);
  color: var(--c-text);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-align: center;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.05s;
  -webkit-tap-highlight-color: transparent;
}
.seg-btn:hover { border-color: var(--c-border-hover); }
.seg-btn:active { transform: scale(0.97); }
.seg-btn.active {
  background: var(--c-primary);
  border-color: var(--c-primary);
  color: #fff;
}

/* ============================================================
   Quick input (quick buttons + number input)
   ============================================================ */
.quick-input {
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
}
.quick-buttons {
  display: flex;
  gap: var(--s-2);
}
.quick-btn {
  flex: 1;
  min-height: 44px;
  padding: 0 14px;
  border: 1.5px solid var(--c-border);
  background: var(--c-surface);
  color: var(--c-text);
  border-radius: var(--radius-sm);
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.05s;
  -webkit-tap-highlight-color: transparent;
}
.quick-btn:hover { border-color: var(--c-border-hover); }
.quick-btn:active { transform: scale(0.97); }
.quick-btn.active {
  background: var(--c-primary);
  border-color: var(--c-primary);
  color: #fff;
}

/* ============================================================
   Buttons
   ============================================================ */
.submit-btn {
  height: var(--h-btn);
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  background: var(--c-primary);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  margin-top: var(--s-2);
  transition: background 0.15s, transform 0.05s;
  letter-spacing: 0.02em;
}
.submit-btn:hover { background: var(--c-primary-dark); }
.submit-btn:active { transform: scale(0.98); }
.submit-btn:disabled {
  background: #9ab8e0;
  cursor: not-allowed;
  transform: none;
}

.secondary-btn {
  height: var(--h-btn-sm);
  padding: 0 18px;
  font-size: 15px;
  font-weight: 600;
  color: var(--c-primary);
  background: var(--c-surface);
  border: 1.5px solid var(--c-primary);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
}
.secondary-btn:hover { background: var(--c-primary-light); }
.secondary-btn:active { transform: scale(0.98); }

/* ============================================================
   Messages
   ============================================================ */
.message {
  margin-top: var(--s-4);
  padding: 14px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  display: none;
  border: 1px solid transparent;
}
.message.success {
  display: block;
  background: var(--c-success-bg);
  color: var(--c-success);
  border-color: #b7dfc2;
}
.message.error {
  display: block;
  background: var(--c-danger-bg);
  color: var(--c-danger);
  border-color: #f3b8b4;
}

/* ============================================================
   Placeholders
   ============================================================ */
.placeholder {
  color: var(--c-text-faint);
  font-style: italic;
  padding: var(--s-8) 0;
  text-align: center;
}
.empty {
  text-align: center;
  padding: var(--s-8) var(--s-4);
  color: var(--c-text-faint);
  font-size: 14px;
  background: var(--c-surface);
  border: 1px dashed var(--c-border);
  border-radius: var(--radius-md);
}

/* ============================================================
   Dashboard (共用 Tab 2 + Tab 3)
   ============================================================ */
.dashboard {
  margin-bottom: var(--s-5);
  display: flex;
  flex-direction: column;
  gap: var(--s-3);
}
.stat-section {
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
}
.stat-section-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--c-text-faint);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 0 2px;
}
.stat-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--s-2);
}
.stat-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--s-2);
  min-height: 76px;
  padding: 12px 14px;
  background: var(--c-surface);
  border: 1.5px solid var(--c-border);
  border-radius: var(--radius-md);
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
  box-shadow: var(--shadow-sm);
}
.stat-card:hover { border-color: var(--c-border-hover); }
.stat-card:active { transform: scale(0.98); }
.stat-card.active {
  background: var(--c-primary-light);
  border-color: var(--c-primary);
}
.stat-card.active .stat-label { color: var(--c-primary-dark); }
.stat-card.active .stat-count { color: var(--c-primary); }

.stat-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--c-text-muted);
  letter-spacing: 0.01em;
  line-height: 1.2;
}
.stat-count {
  font-size: 24px;
  font-weight: 700;
  color: var(--c-text);
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.dashboard-summary {
  font-size: 13px;
  color: var(--c-text-muted);
  padding: 0 4px;
  font-weight: 500;
}
.dashboard-summary:empty { display: none; }

@media (min-width: 640px) {
  .stat-count { font-size: 28px; }
  .stat-card { min-height: 88px; }
}

/* ============================================================
   Tab 2 — 訂單列表
   ============================================================ */
.filters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--s-3);
  margin-bottom: var(--s-5);
  padding: var(--s-4);
  background: var(--c-surface);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
}
.filters input[type="search"],
.filters button {
  grid-column: 1 / -1;
}

.list-status {
  color: var(--c-text-faint);
  font-size: 14px;
  text-align: center;
  padding: var(--s-5);
  display: none;
}
.list-status.active { display: block; }

.order-list,
.shipment-list {
  display: flex;
  flex-direction: column;
  gap: var(--s-3);
}

.order-card,
.shipment-card {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius-md);
  padding: var(--s-4);
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.15s, border-color 0.15s, transform 0.05s;
}
.order-card.expanded,
.shipment-card.expanded {
  box-shadow: var(--shadow-md);
  border-color: var(--c-border-hover);
}

.order-card-summary,
.shipment-summary {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  margin: -4px;
  padding: 4px;
}

.order-card-row,
.shipment-card-row {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  margin: 5px 0;
  flex-wrap: wrap;
}

.order-card-title,
.shipment-title {
  font-size: 17px;
  font-weight: 600;
  color: var(--c-text);
  flex: 1;
  min-width: 0;
  word-break: break-word;
}

.order-card-row.muted,
.shipment-card-row.muted {
  color: var(--c-text-muted);
  font-size: 14px;
}
.order-card-row.small,
.shipment-card-row.small {
  color: var(--c-text-faint);
  font-size: 12px;
  justify-content: space-between;
  margin-top: var(--s-2);
}

.order-card-amount {
  margin-left: auto;
  font-weight: 700;
  color: var(--c-primary);
  font-size: 15px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* ============================================================
   Badges
   ============================================================ */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: var(--radius-pill);
  font-size: 12px;
  font-weight: 600;
  background: #eef2f6;
  color: #4b5563;
  white-space: nowrap;
  letter-spacing: 0.02em;
}
.badge.status-pending  { background: #fff3cd; color: #86670f; }
.badge.status-ordered  { background: #cfe2ff; color: #084298; }
.badge.status-shipped  { background: #d1e7dd; color: #0f5132; }
.badge.source-IG       { background: #fde4f1; color: #a83278; }
.badge.source-FB       { background: #d9e7ff; color: #1554a8; }
.badge.source-shopee   { background: #ffe6d6; color: #a85a13; }
.badge.source-Line     { background: #d6f1db; color: #177b2a; }

/* ============================================================
   Expanded card details
   ============================================================ */
.order-card-detail,
.shipment-detail {
  margin-top: var(--s-4);
  padding-top: var(--s-4);
  border-top: 1px solid var(--c-border);
}

.detail-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px 14px;
  font-size: 14px;
  margin: 0 0 var(--s-5);
  padding: var(--s-3) var(--s-4);
  background: var(--c-surface-alt);
  border-radius: var(--radius-sm);
}
.detail-grid dt {
  color: var(--c-text-faint);
  font-weight: 500;
}
.detail-grid dd {
  margin: 0;
  color: var(--c-text);
  font-weight: 500;
  word-break: break-word;
}

.edit-form {
  display: flex;
  flex-direction: column;
  gap: var(--s-3);
  padding-top: var(--s-4);
  border-top: 1px dashed var(--c-border);
}
.edit-form h3 {
  margin: var(--s-2) 0 0;
  font-size: 12px;
  font-weight: 700;
  color: var(--c-text-faint);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.edit-form h3:first-child { margin-top: 0; }
.edit-form label {
  font-size: 12px;
  font-weight: 600;
  color: var(--c-text-muted);
}

.edit-actions {
  display: flex;
  gap: var(--s-2);
  margin-top: var(--s-2);
}
.edit-actions button { flex: 1; }

.edit-save {
  height: var(--h-btn-sm);
  font-size: 15px;
  font-weight: 600;
  color: #fff;
  background: var(--c-primary);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
}
.edit-save:hover { background: var(--c-primary-dark); }
.edit-save:active { transform: scale(0.98); }
.edit-save:disabled { background: #9ab8e0; cursor: not-allowed; transform: none; }

.edit-feedback:empty { display: none; }

/* ============================================================
   Tab 3 — 集運批次
   ============================================================ */
.new-shipment {
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius-md);
  padding: var(--s-2) var(--s-4);
  margin-bottom: var(--s-4);
  box-shadow: var(--shadow-sm);
}
.new-shipment > summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 15px;
  color: var(--c-primary);
  padding: var(--s-3) 0;
  list-style: none;
  -webkit-tap-highlight-color: transparent;
}
.new-shipment > summary::-webkit-details-marker { display: none; }
.new-shipment[open] > summary { margin-bottom: var(--s-3); }
.new-shipment[open] > summary::before {
  content: '− ';
  display: inline-block;
  width: 1em;
}
.new-shipment:not([open]) > summary::before {
  content: '+ ';
  display: inline-block;
  width: 1em;
}

/* Progress bar */
.progress-bar {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 4px;
  margin-top: var(--s-3);
}
.progress-step {
  height: 10px;
  background: var(--c-border);
  border-radius: 5px;
  transition: background 0.2s;
}
.progress-step.done    { background: #a7c7f5; }
.progress-step.current { background: var(--c-primary); }

.progress-labels {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 4px;
  margin-top: 6px;
  font-size: 10px;
  color: var(--c-text-faint);
  text-align: center;
  letter-spacing: 0.02em;
}
.progress-labels span.current {
  color: var(--c-primary);
  font-weight: 700;
}

/* Advance button */
.advance-row {
  margin-bottom: var(--s-4);
}
.advance-btn {
  width: 100%;
  height: var(--h-btn);
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  background: var(--c-success);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  letter-spacing: 0.02em;
  transition: background 0.15s, transform 0.05s;
}
.advance-btn:hover { background: #0b3d26; }
.advance-btn:active { transform: scale(0.98); }
.advance-btn:disabled {
  background: var(--c-border);
  color: var(--c-text-faint);
  cursor: not-allowed;
  transform: none;
}

/* Related orders */
.related-orders {
  margin-top: var(--s-5);
  padding-top: var(--s-4);
  border-top: 1px dashed var(--c-border);
}
.related-orders h3 {
  margin: 0 0 var(--s-3);
  font-size: 13px;
  font-weight: 700;
  color: var(--c-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.related-orders ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
}
.related-orders li {
  display: flex;
  gap: var(--s-2);
  align-items: center;
  padding: var(--s-3) var(--s-4);
  background: var(--c-surface-alt);
  border-radius: var(--radius-sm);
  font-size: 14px;
  color: var(--c-text);
  flex-wrap: wrap;
}
.related-orders li .amount {
  margin-left: auto;
  color: var(--c-primary);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.related-orders .empty-inline {
  color: var(--c-text-faint);
  font-size: 14px;
  font-style: italic;
  margin: 0;
  padding: var(--s-4);
  background: var(--c-surface-alt);
  border-radius: var(--radius-sm);
}

/* ============================================================
   Responsive — wider screens
   ============================================================ */
@media (min-width: 640px) {
  main { padding: var(--s-6) var(--s-5) var(--s-8); }
  h1 { font-size: 24px; margin-bottom: var(--s-6); }

  .filters {
    grid-template-columns: 1fr 1fr 2fr auto;
  }
  .filters input[type="search"],
  .filters button {
    grid-column: auto;
  }
  .filters button { min-width: 120px; }
}

/* ============================================================
   Accessibility — reduce motion
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  * {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}
