:root {
  --teal: #13B0B6;
  --blue: #2563B5;
  --grad: linear-gradient(120deg, #13B0B6 0%, #2563B5 100%);
  --bg: #f4f7f9;
  --card-bg: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e2e8f0;
  --danger: #d64545;
  --success: #1a9e5c;
  --warn: #b8860b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Microsoft JhengHei", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  /* 安全網：任何元素意外過寬時，只裁切而不讓整頁橫向捲動 */
  overflow-x: hidden;
}

a { color: var(--blue); }

/* ======== 頂部導覽列（gradient banner + white logo） ======== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--grad);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 16px;
  padding: 0 28px;
  height: 56px;
  box-shadow: 0 2px 12px rgba(20,40,70,.18);
}
.topbar .topbar-logo {
  display: flex;
  align-items: center;
}
.topbar .topbar-logo img {
  height: 30px;
  display: block;
}
.topbar .topbar-title {
  color: rgba(255,255,255,.85);
  font-size: 13.5px;
  font-weight: 600;
  letter-spacing: .02em;
}
.topbar .topbar-spacer { flex: 1; }
.topbar .topbar-user {
  color: rgba(255,255,255,.88);
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.topbar button.logout {
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.5);
  color: #fff;
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 13px;
}
.topbar button.logout:hover { background: rgba(255,255,255,0.3); }
.topbar .topbar-link {
  color: rgba(255,255,255,.88);
  text-decoration: none;
  font-size: 13px;
}
.topbar .topbar-link:hover { color: #fff; text-decoration: underline; }
.role-switcher-select {
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.5);
  color: #fff;
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 13px;
  cursor: pointer;
}
.role-switcher-select option { color: #1f2937; }

/* ======== Topbar RWD：窄螢幕讓 topbar-user 區塊自然換行，避免橫向溢出 ======== */
@media (max-width: 640px) {
  .topbar {
    height: auto;
    min-height: 56px;
    padding: 10px 14px;
    gap: 8px;
  }
  .topbar .topbar-title {
    font-size: 12px;
  }
  .topbar .topbar-spacer {
    flex: 1 1 100%;
    height: 0;
  }
  .topbar .topbar-user {
    flex-wrap: wrap;
    gap: 6px;
    font-size: 12px;
    width: 100%;
    justify-content: flex-end;
  }
  .topbar button.logout,
  .topbar .topbar-link,
  .role-switcher-select {
    font-size: 12px;
    padding: 5px 8px;
  }
}

/* ======== 登入頁專屬 gradient logo 區 ======== */
.login-topbar {
  background: var(--grad);
  padding: 28px 24px 26px;
  text-align: center;
  box-shadow: 0 2px 12px rgba(20,40,70,.18);
}
.login-topbar img.logo {
  height: 34px;
  display: block;
  margin: 0 auto 8px;
}
.login-topbar .title {
  color: rgba(255,255,255,.9);
  font-size: 14px;
  font-weight: 600;
  letter-spacing: .02em;
}

main {
  max-width: 1080px;
  margin: 24px auto;
  padding: 0 16px 48px;
}

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
}

.card h2 {
  margin-top: 0;
  font-size: 16px;
  color: var(--blue);
  border-bottom: 2px solid var(--teal);
  padding-bottom: 8px;
}

.login-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: calc(100vh - 60px);
}

.login-card {
  width: 360px;
  max-width: 100%;
}

label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin: 10px 0 4px;
}

input, select, textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  background: #fff;
  color: var(--text);
}

textarea { resize: vertical; }

button, .btn {
  cursor: pointer;
  border: none;
  border-radius: 6px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  background: var(--teal);
  color: #fff;
  margin-top: 4px;
}
button:hover, .btn:hover { background: var(--blue); }
button.secondary { background: #fff; color: var(--blue); border: 1px solid var(--blue); }
button.secondary:hover { background: #eef4ff; }
button.danger { background: var(--danger); }
button.danger:hover { background: #b23a3a; }
button:disabled { background: #b9c1c9; cursor: not-allowed; }

/* 表格在窄螢幕改為容器內部橫向捲動，而不撐開整頁造成頁面橫向捲動 */
.table-wrap {
  width: 100%;
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
th, td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
th { color: var(--muted); font-weight: 600; background: #f9fbfc; }

.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  color: #fff;
}
.badge.draft { background: #9ca3af; }
.badge.submitted { background: var(--blue); }
.badge.sales_approved { background: var(--warn); }
.badge.returned { background: var(--danger); }
.badge.approved { background: var(--success); }

.badge.pending { background: #9ca3af; }
.badge.docs_ready { background: #4c9be8; }
.badge.submitted_upstream { background: var(--blue); }
.badge.reviewing_upstream { background: var(--warn); }
.badge.activated { background: var(--success); }
.badge.rejected { background: var(--danger); }

.error-msg {
  color: var(--danger);
  font-size: 13px;
  margin-top: 8px;
  min-height: 18px;
}
.info-msg {
  color: var(--success);
  font-size: 13px;
  margin-top: 8px;
  min-height: 18px;
}

#returnedReasonNote.error-msg:not(:empty) {
  background: #fdecea;
  border: 1px solid var(--danger);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 14px;
  font-weight: 600;
}

.review-log-reason {
  margin-left: 6px;
  color: var(--danger);
}

.grid-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 12px;
}
.stat-box {
  background: #f9fbfc;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  text-align: center;
}
.stat-box .num {
  font-size: 22px;
  font-weight: 700;
  color: var(--blue);
}
.stat-box .label {
  font-size: 12px;
  color: var(--muted);
  margin-top: 4px;
}

.inline-form {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
}
.inline-form > div { flex: 1; min-width: 120px; }

.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }

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

.section-divider { margin: 24px 0; border-top: 1px dashed var(--border); }

/* ======== 進件核心欄位表單（動態渲染） ======== */
.form-section {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px 20px 20px;
  margin-bottom: 18px;
  background: #fbfdfe;
}
.form-section-title {
  margin: 0 0 16px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
  font-size: 15px;
  font-weight: 700;
  color: var(--blue);
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.form-section-title .field-local {
  font-size: 12px;
}

/* Responsive field grid: 1 col mobile, 2 col desktop (~>=900px) */
.form-field-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px 24px;
  align-items: start;
}
@media (min-width: 900px) {
  .form-field-grid { grid-template-columns: repeat(2, 1fr); }
}

.form-field {
  min-width: 0;
  display: flex;
  flex-direction: column;
}
/* Long-form fields (textarea etc.) always span the full row so they
   never leave a ragged half-empty column next to them. */
.form-field.field-span-full { grid-column: 1 / -1; }

.form-field label {
  display: flex;
  flex-direction: column;
  gap: 2px;
  /* reserve room for the local/English sub-label so 1-line and 2-line
     labels still line up their inputs at the same vertical position */
  min-height: 34px;
  justify-content: flex-end;
  margin-bottom: 6px;
}
.form-field label .label-zh {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text);
}
.field-local {
  display: block;
  color: var(--muted);
  font-weight: 400;
  font-size: 11.5px;
  line-height: 1.3;
}
.req-mark { color: var(--danger); font-weight: 700; margin-left: 2px; }

/* Uniform control styling shared by all field types */
.form-field input,
.form-field select,
.form-field textarea {
  width: 100%;
  min-height: 38px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  background: #fff;
  color: var(--text);
  font-family: inherit;
}
.form-field textarea {
  min-height: 72px;
  resize: vertical;
  line-height: 1.5;
}
.form-field input:focus,
.form-field select:focus,
.form-field textarea:focus {
  outline: none;
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(19,176,182,.15);
}
.form-field input:disabled,
.form-field select:disabled,
.form-field textarea:disabled {
  background: #f4f7f9;
  color: var(--text);
  opacity: 1;
  -webkit-text-fill-color: var(--text);
}

.field-readonly {
  display: flex;
  align-items: center;
  min-height: 38px;
  padding: 8px 10px;
  font-size: 14px;
  color: var(--text);
  background: #f4f7f9;
  border: 1px solid var(--border);
  border-radius: 6px;
  word-break: break-word;
}

.radio-group {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  min-height: 38px;
  align-items: center;
  padding: 0 2px;
}
.radio-option { display: inline-flex; align-items: center; gap: 4px; font-size: 14px; color: var(--text); font-weight: 400; }

.checkbox-field {
  display: flex;
  align-items: center;
  min-height: 38px;
}
.checkbox-field input[type="checkbox"] {
  width: auto;
  min-height: 0;
  margin: 0;
}

.input-with-unit { position: relative; display: flex; align-items: center; }
.input-with-unit input { padding-right: 46px; }
.input-with-unit .unit-hint {
  position: absolute;
  right: 10px;
  font-size: 12px;
  color: var(--muted);
  pointer-events: none;
}
.form-readonly-note {
  font-size: 13px;
  color: var(--muted);
  background: #f4f7f9;
  border: 1px dashed var(--border);
  border-radius: 6px;
  padding: 8px 12px;
  margin-bottom: 14px;
}

/* ======== 文件清單勾稽（已附／待補） ======== */
.doc-checklist { list-style: none; margin: 0; padding: 0; }
.doc-checklist-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 4px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.doc-checklist-item:last-child { border-bottom: none; }
.doc-checklist-label { flex: 1; min-width: 160px; font-size: 14px; }
.doc-status {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  color: #fff;
}
.doc-status.ok { background: var(--success); }
.doc-status.missing { background: var(--warn); }
.doc-upload-inline {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  min-width: 0;
}
.doc-upload-inline input[type="file"] {
  font-size: 12px;
  padding: 4px;
  width: auto;
  max-width: 100%;
  min-width: 0;
  flex: 1 1 auto;
}
.doc-upload-inline .checklist-upload-btn { flex: 0 0 auto; }
.missing-list { margin: 6px 0 0; padding-left: 18px; font-size: 13px; }

/* ======== 案件狀態進度條（比照合約系統 stepper 視覺；app.js renderStatusStepper 注入） ======== */
.case-stepper {
  display: flex;
  align-items: flex-start;
  flex-wrap: wrap;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 18px 10px;
  margin: 10px 0 18px;
  box-shadow: 0 1px 6px rgba(20, 40, 70, .05);
}
.case-stepper-step {
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-width: 64px;
}
.case-stepper-step .dot {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 700;
  background: #fff;
  border: 2px solid var(--border);
  color: var(--muted);
}
.case-stepper-step .lbl { font-size: 12.5px; color: var(--muted); white-space: nowrap; }
.case-stepper-step.done .dot { background: var(--teal); border-color: var(--teal); color: #fff; }
.case-stepper-step.done .lbl { color: #0e8489; }
.case-stepper-step.current .dot {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
  box-shadow: 0 0 0 4px rgba(37, 99, 181, .18);
}
.case-stepper-step.current .lbl { color: var(--blue); font-weight: 700; }
.case-stepper-bar {
  flex: 1;
  min-width: 18px;
  height: 2px;
  border-radius: 1px;
  background: var(--border);
  margin: 14px 8px 0;
}
.case-stepper-bar.done { background: var(--teal); }
.case-stepper-note {
  flex: none;
  align-self: center;
  font-size: 12.5px;
  font-weight: 600;
  color: #fff;
  background: var(--warn);
  border-radius: 999px;
  padding: 4px 12px;
  margin-left: 14px;
}
@media (max-width: 640px) {
  .case-stepper { padding: 10px 10px 8px; }
  .case-stepper-step { min-width: 52px; }
  .case-stepper-step .dot { width: 24px; height: 24px; font-size: 12px; }
  .case-stepper-step .lbl { font-size: 11px; }
  .case-stepper-bar { margin-top: 11px; min-width: 8px; }
}
@media print { .case-stepper { display: none; } }

/* ======== 使用流程頁（guide.html） ======== */
.guide-intro { color: var(--text); line-height: 1.7; margin-bottom: 14px; }
.role-hint {
  background: #eaf8f8; border-left: 4px solid var(--teal);
  padding: 10px 14px; border-radius: 6px; margin-bottom: 22px;
  font-size: 14px; color: var(--text);
}
.flow-steps { list-style: none; margin: 0; padding: 0; }
.flow-step {
  position: relative; display: flex; gap: 14px;
  padding: 0 0 20px 0;
}
/* 連接線 */
.flow-step:not(:last-child)::before {
  content: ""; position: absolute; left: 17px; top: 36px; bottom: -2px;
  width: 2px; background: var(--border);
}
.flow-step-num {
  flex: 0 0 auto; width: 36px; height: 36px; border-radius: 50%;
  background: var(--border); color: #fff; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  z-index: 1; font-size: 15px;
}
.flow-step-done .flow-step-num { background: var(--success); }
.flow-step-body { flex: 1 1 auto; min-width: 0; padding-top: 4px; }
.flow-step-head {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 4px;
}
.flow-step-title { font-weight: 700; color: var(--blue); font-size: 15px; }
.flow-actors {
  font-size: 12px; color: var(--muted); background: #f1f5f9;
  padding: 1px 8px; border-radius: 999px; white-space: nowrap;
}
.flow-step-body p { margin: 0; color: var(--text); font-size: 14px; line-height: 1.6; }
/* 高亮當前身分負責的步驟 */
.flow-step-mine .flow-step-num { background: var(--grad); }
.flow-step-mine .flow-step-body {
  background: #f0fbfb; border: 1px solid #bfeceb;
  border-radius: 8px; padding: 8px 12px;
}
.flow-step-mine .flow-actors { background: var(--teal); color: #fff; }

/* 狀態小標籤 */
.status-chip {
  display: inline-block; padding: 1px 8px; border-radius: 999px;
  font-size: 12px; font-weight: 600; color: #fff; white-space: nowrap;
}
.status-chip.status-draft { background: var(--muted); }
.status-chip.status-submitted { background: var(--blue); }
.status-chip.status-sales { background: var(--teal); }
.status-chip.status-returned { background: var(--warn); }
.status-chip.status-approved { background: var(--success); }

.guide-subhead { margin: 26px 0 10px; color: var(--blue); font-size: 16px; }
.guide-role-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.guide-role-table th, .guide-role-table td {
  border: 1px solid var(--border); padding: 7px 10px; text-align: center;
}
.guide-role-table th { background: var(--blue); color: #fff; }
.guide-role-table td:first-child, .guide-role-table th:first-child { text-align: left; }
.guide-role-table tr:nth-child(even) td { background: #f8fafc; }
.guide-foot { margin-top: 10px; font-size: 13px; }
