/* 顏色一律用變數；深色模式在「跟著系統」與「手動選深色」兩處給同一組值。 */
:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafb;
  --text: #17212b;
  --muted: #5d6b79;
  --faint: #8794a1;
  --line: #e1e6eb;
  --accent: #0e7490;
  --accent-strong: #0b5f76;
  --accent-soft: #e2f3f7;
  --accent-text: #ffffff;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .04);
  --done: #1a7f4b;
  --done-bg: #e3f4ea;
  --active: #1d64d8;
  --active-bg: #e5eefc;
  --attention: #965700;
  --attention-bg: #fdf0d8;
  --stopped: #66707c;
  --stopped-bg: #eceef1;
  --todo: #8a94a1;
  --todo-bg: #f0f2f5;
  --fail: #b42318;
  --fail-bg: #fde8e6;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1419;
    --surface: #171d24;
    --surface-2: #1c232b;
    --text: #e5eaf0;
    --muted: #9ba7b4;
    --faint: #6f7b88;
    --line: #2a333d;
    --accent: #3fb6cf;
    --accent-strong: #67c9dd;
    --accent-soft: #15323a;
    --accent-text: #061418;
    --shadow: none;
    --done: #4cc38a;
    --done-bg: #16301f;
    --active: #74a8ff;
    --active-bg: #19284a;
    --attention: #f2b14a;
    --attention-bg: #372a12;
    --stopped: #a0a9b3;
    --stopped-bg: #252c34;
    --todo: #75818e;
    --todo-bg: #1f262e;
    --fail: #ff8278;
    --fail-bg: #3a1c1a;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1419;
  --surface: #171d24;
  --surface-2: #1c232b;
  --text: #e5eaf0;
  --muted: #9ba7b4;
  --faint: #6f7b88;
  --line: #2a333d;
  --accent: #3fb6cf;
  --accent-strong: #67c9dd;
  --accent-soft: #15323a;
  --accent-text: #061418;
  --shadow: none;
  --done: #4cc38a;
  --done-bg: #16301f;
  --active: #74a8ff;
  --active-bg: #19284a;
  --attention: #f2b14a;
  --attention-bg: #372a12;
  --stopped: #a0a9b3;
  --stopped-bg: #252c34;
  --todo: #75818e;
  --todo-bg: #1f262e;
  --fail: #ff8278;
  --fail-bg: #3a1c1a;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  -webkit-text-size-adjust: 100%;
}

button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: progress; opacity: .6; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ── 頂列 ─────────────────────────────────────────── */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
  flex: none;
}
.brand-name { display: flex; align-items: baseline; gap: 8px; white-space: nowrap; }
.brand-name strong { letter-spacing: .04em; }
.brand-name span { color: var(--muted); }
.top-actions { display: flex; align-items: center; gap: 8px; }
.who { display: flex; align-items: center; gap: 8px; }
.account {
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 13px;
}

.ghost {
  padding: 6px 10px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: none;
  color: var(--muted);
}
.ghost:hover { background: var(--surface-2); color: var(--text); border-color: var(--line); }
.ghost.small { padding: 3px 8px; font-size: 13px; }
.theme-toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; }
.theme-icon { font-size: 15px; line-height: 1; }

.primary {
  width: 100%;
  margin-top: 20px;
  padding: 11px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
.primary:hover:not(:disabled) { background: var(--accent-strong); }
.secondary {
  flex: none;
  padding: 7px 14px;
  border: 1px solid currentColor;
  border-radius: 8px;
  background: none;
  font-weight: 600;
}

main { max-width: 1120px; margin: 0 auto; padding: 20px; }

/* ── 登入 ─────────────────────────────────────────── */
.login { display: flex; justify-content: center; padding-top: 7vh; }
.login-card {
  width: 100%;
  max-width: 380px;
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.hint { margin: 0 0 18px; color: var(--muted); font-size: 14px; }
.fine { margin: 16px 0 0; color: var(--faint); font-size: 13px; text-align: center; }
label { display: block; margin: 14px 0 6px; font-size: 14px; color: var(--muted); }
input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.password-field { position: relative; }
.password-field input { padding-right: 64px; }
.password-field button { position: absolute; top: 50%; right: 6px; transform: translateY(-50%); }
.captcha { margin-top: 16px; }
.error { margin: 14px 0 0; color: var(--fail); font-size: 14px; }

/* ── 總覽與提醒 ───────────────────────────────────── */
.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.tile {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: left;
}
.tile:hover { border-color: var(--accent); }
.tile[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); }
.tile-label { color: var(--muted); font-size: 13px; }
.tile-value { font-size: 28px; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; }
.tile.attention .tile-value { color: var(--attention); }

.notice {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-top: 14px;
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--attention-bg);
  color: var(--attention);
}
.notice p { margin: 2px 0 0; font-size: 14px; color: var(--text); opacity: .85; }

/* ── 工具列 ───────────────────────────────────────── */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; margin-top: 20px; }
.segmented, .filters {
  display: flex;
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
}
.segmented button, .filters button {
  padding: 6px 14px;
  border: 0;
  border-radius: 7px;
  background: none;
  color: var(--muted);
  white-space: nowrap;
}
.segmented button:hover, .filters button:hover { color: var(--text); }
.segmented button[aria-selected="true"], .filters button[aria-pressed="true"] {
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-weight: 600;
}
.filters .count { margin-left: 4px; font-variant-numeric: tabular-nums; opacity: .8; }
#search { flex: 1 1 220px; max-width: 340px; margin-left: auto; background: var(--surface); }
.sort-select { display: flex; align-items: center; gap: 8px; margin: 0; color: var(--muted); font-size: 14px; }
.sort-select select {
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.status { min-height: 1.4em; margin: 12px 0 6px; color: var(--muted); font-size: 14px; }
.status.warn { color: var(--attention); }
.empty { color: var(--muted); text-align: center; padding: 40px 0; }

/* ── 案件卡片 ─────────────────────────────────────── */
.cases { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.case, .panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.case { padding: 16px 18px; }
.case-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.case-title { min-width: 0; }
.po { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.po small { margin-left: 6px; color: var(--muted); font-size: 13px; font-weight: 400; }
.meta { margin-top: 2px; color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }

.badge, .chip {
  display: inline-block;
  border-radius: 999px;
  font-weight: 600;
  white-space: nowrap;
}
.badge { flex: none; padding: 3px 12px; font-size: 13px; }
.chip { padding: 1px 8px; font-size: 12px; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }

.tone-done { color: var(--done); background: var(--done-bg); }
.tone-active { color: var(--active); background: var(--active-bg); }
.tone-attention { color: var(--attention); background: var(--attention-bg); }
.tone-stopped { color: var(--stopped); background: var(--stopped-bg); }
.tone-todo { color: var(--todo); background: var(--todo-bg); }
.tone-fail { color: var(--fail); background: var(--fail-bg); }

/* 進度步驟：圓點＋連線，完成的線段跟著上色 */
.stepper { list-style: none; display: flex; margin: 18px 0 2px; padding: 0; }
.step { position: relative; flex: 1 1 0; min-width: 0; padding-right: 8px; }
.step::after {
  content: "";
  position: absolute;
  top: 11px;
  left: 30px;
  right: 6px;
  height: 2px;
  border-radius: 2px;
  background: var(--line);
}
.step:last-child::after { display: none; }
.step.tone-done::after { background: var(--done); }
.step.tone-done, .step.tone-active, .step.tone-attention, .step.tone-stopped, .step.tone-todo { background: none; }
.dot {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 2px solid currentColor;
  background: var(--surface);
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
}
.step.tone-done .dot { background: var(--done); border-color: var(--done); color: var(--surface); }
.step.tone-active .dot::before { content: ""; width: 10px; height: 10px; border-radius: 50%; background: currentColor; }
.step.tone-attention .dot { background: var(--attention); border-color: var(--attention); color: var(--surface); }
.step.tone-todo .dot { border-color: var(--line); }
.step-label { margin-top: 6px; color: var(--text); font-size: 13px; font-weight: 600; }
.step-state { font-size: 12px; overflow-wrap: anywhere; }
.step-date { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }

/* ── 樣本表 ───────────────────────────────────────── */
.samples { margin-top: 14px; border-top: 1px solid var(--line); padding-top: 10px; }
.samples > summary {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  color: var(--muted);
  font-size: 13px;
  list-style: none;
}
.samples > summary::-webkit-details-marker { display: none; }
.samples > summary::before { content: "▸"; transition: transform .15s; }
.samples[open] > summary::before { transform: rotate(90deg); }
.samples > summary:hover { color: var(--text); }
.table-wrap { margin-top: 10px; overflow-x: auto; }
.panel .table-wrap { margin-top: 0; }

.sample-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.sample-table th {
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-weight: 600;
  text-align: left;
  white-space: nowrap;
}
.sample-table th .sort {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font-weight: 600;
  white-space: nowrap;
}
.sample-table th .sort:hover, .sample-table th .sort.active { color: var(--accent-strong); }
.sample-table th .arrow { font-size: 10px; opacity: .45; }
.sample-table th .sort.active .arrow { opacity: 1; }
.sample-table td { padding: 8px 10px; border-top: 1px solid var(--line); vertical-align: top; }
.sample-table tbody tr:first-child td { border-top: 0; }
.sample-table tr.detail td { border-top: 0; padding-top: 0; color: var(--muted); font-size: 12px; }
.sample-table .name { font-weight: 600; overflow-wrap: anywhere; }
.sample-table .num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.sample-table .sub { display: block; color: var(--muted); font-size: 12px; }
.sample-table .missing { color: var(--attention); }
.sample-table .note { display: block; margin-top: 2px; overflow-wrap: anywhere; }
.po-link { padding: 0; border: 0; background: none; color: var(--accent); font-weight: 600; text-align: left; }
.po-link:hover { text-decoration: underline; }

.foot {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 8px 16px;
  margin-top: 20px;
  color: var(--muted);
  font-size: 13px;
}
.foot-actions { display: flex; align-items: center; gap: 12px; }

/* ── 窄螢幕 ───────────────────────────────────────── */
@media (max-width: 720px) {
  main { padding: 14px; }
  .topbar { padding: 10px 14px; }
  .brand-name span, .theme-toggle #theme-label { display: none; }
  .summary { gap: 8px; }
  .tile { padding: 10px 12px; }
  .tile-value { font-size: 22px; }
  .notice { flex-direction: column; align-items: stretch; }
  .toolbar { flex-direction: column; align-items: stretch; }
  .segmented button, .filters button { flex: 1; padding: 6px 4px; }
  #search { flex: none; max-width: none; margin-left: 0; }
  .sort-select select { flex: 1; }
  .case { padding: 14px; }

  /* 步驟改成直排 */
  .stepper { flex-direction: column; gap: 0; margin-top: 14px; }
  .step { display: grid; grid-template-columns: 24px 1fr auto; column-gap: 10px; padding: 0 0 14px; }
  .step::after { top: 26px; bottom: 2px; left: 11px; right: auto; width: 2px; height: auto; }
  .step .dot { grid-row: span 2; }
  .step-label { margin-top: 0; }
  .step-state { grid-column: 2; }
  .step-date { grid-column: 3; grid-row: 1; }

  /* 表格改成一列一張小卡 */
  .sample-table thead { display: none; }
  .sample-table, .sample-table tbody, .sample-table tr, .sample-table td { display: block; }
  .sample-table tr.sample { padding: 10px 0 8px; border-top: 1px solid var(--line); }
  .sample-table tbody tr.sample:first-child { border-top: 0; }
  .sample-table td { display: flex; gap: 10px; padding: 2px 0; border: 0; }
  .sample-table td::before { content: attr(data-label); flex: none; width: 4.5em; color: var(--muted); }
  .sample-table td.name { font-size: 14px; }
  .sample-table td.name::before { content: none; }
  .sample-table tr.detail { padding-bottom: 10px; }
  .sample-table tr.detail td { display: block; }
  .sample-table tr.detail td::before { content: none; }
  .panel { padding: 4px 14px; }

  /* 「依樣本」在手機上把表頭變成一排排序按鈕；案件卡片裡的小表格不顯示 */
  .panel .sample-table thead { display: block; padding: 10px 0; border-bottom: 1px solid var(--line); }
  .panel .sample-table thead tr { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
  .panel .sample-table thead tr::before { content: "排序"; color: var(--muted); font-size: 12px; }
  .panel .sample-table th { padding: 0; border: 0; }
  .panel .sample-table th .sort { padding: 2px 9px; border: 1px solid var(--line); border-radius: 999px; font-size: 12px; }
  .panel .sample-table th .sort.active { border-color: var(--accent); }
}
