:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --ink: #221f1b;
  --ink-muted: #6b6459;
  --border: #ddd6c8;
  --sidebar: #1d1a16;
  --sidebar-ink: #efe9dd;
  --gold: #b6863c;
  --teal: #2f6f68;
  --amber: #b6863c;
  --green: #4c7a4f;
  --red: #8a3b32;
  --red-bg: #fbeceA;
  font-family:
    'Noto Sans TC',
    system-ui,
    -apple-system,
    sans-serif;
  color: var(--ink);
  background: var(--bg);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

a {
  color: inherit;
}

/* Global "← 上一頁" back link (public/js/ui.js backLinkHtml/wireBackLink).
   Compact ghost-button treatment per Global Back Navigation restyle:
   lighter/less prominent than primary or secondary action buttons, but
   still a real focusable/keyboard-operable <a>, never icon-only. */
.back-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-bottom: 12px;
  padding: 5px 10px;
  border-radius: 6px;
  border: 1px solid transparent;
  font-size: 0.9em;
  color: var(--ink-muted);
  text-decoration: none;
  cursor: pointer;
  transition:
    background-color 0.12s ease,
    color 0.12s ease,
    border-color 0.12s ease;
}

.back-link:hover {
  background: var(--bg);
  border-color: var(--border);
  color: var(--ink);
}

.back-link:focus-visible {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}

.btn {
  font: inherit;
  padding: 8px 16px;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition:
    background-color 0.12s ease,
    border-color 0.12s ease,
    color 0.12s ease;
}

.btn:focus-visible {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}

.btn--primary {
  background: var(--sidebar);
  color: var(--sidebar-ink);
}

.btn--primary:hover {
  background: #33302a;
}

.btn--secondary {
  background: var(--surface);
  border-color: var(--border);
  color: var(--ink);
}

.btn--secondary:hover {
  background: var(--bg);
  border-color: var(--ink-muted);
}

/* Danger — reserved for an already-existing approved destructive action
   (e.g. cancelling a WORKING record). Never applied to a routine
   secondary/navigation action. */
.btn--danger {
  background: var(--red-bg);
  border-color: var(--red);
  color: var(--red);
}

.btn--danger:hover {
  background: var(--red);
  color: #fff;
}

/* Text/ghost — low-emphasis navigation and utility actions (e.g. the
   global "← 上一頁" back link). No fill, no border, understated until
   hovered/focused. */
.btn--text {
  background: transparent;
  border-color: transparent;
  color: var(--ink-muted);
  padding: 6px 10px;
}

.btn--text:hover {
  background: var(--bg);
  color: var(--ink);
}

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

/* --- Login / password-change gate --- */
.login {
  max-width: 420px;
  margin: 12vh auto;
  padding: 32px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.login h1 {
  margin: 0 0 20px;
  font-size: 20px;
}

.login__form {
  display: grid;
  gap: 14px;
}

.login__form label {
  display: grid;
  gap: 6px;
  font-size: 13px;
  color: var(--ink-muted);
}

.login__form input {
  font: inherit;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
}

/* --- Shell --- */
.shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 240px 1fr;
}

.shell__sidebar {
  background: var(--sidebar);
  color: var(--sidebar-ink);
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.shell__brand {
  font-weight: 600;
  padding: 4px 10px;
}

.nav {
  display: grid;
  gap: 2px;
}

.nav__item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-radius: 6px;
  text-decoration: none;
  color: var(--sidebar-ink);
  font-size: 14px;
}

.nav__item:hover {
  background: rgba(255, 255, 255, 0.08);
}

.nav__item--planned {
  opacity: 0.5;
  cursor: default;
}

.nav__item--planned:hover {
  background: none;
}

.nav__code {
  font-size: 11px;
  color: var(--gold);
  min-width: 18px;
}

.nav__tag {
  margin-left: auto;
  font-size: 11px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 10px;
  padding: 1px 8px;
}

.nav__group-label {
  margin: 10px 10px 2px;
  padding-top: 10px;
  border-top: 1px solid rgba(255, 255, 255, 0.12);
  font-size: 11px;
  letter-spacing: 0.04em;
  color: rgba(255, 255, 255, 0.45);
}

.nav__group-label:first-child {
  margin-top: 0;
  padding-top: 0;
  border-top: none;
}

.shell__main {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.shell__topbar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 16px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

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

.shell__content {
  padding: 24px;
  flex: 1;
}

/* --- Page layout --- */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.page-header h2 {
  margin: 0;
  font-size: 18px;
}

.filters {
  margin-bottom: 16px;
}

.filters-form {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-bottom: 16px;
}

.filters-form input,
.filters-form select {
  font: inherit;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 6px;
}

.inline-label {
  font-size: 13px;
  color: var(--ink-muted);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn--small {
  padding: 4px 10px;
  font-size: 12px;
}

/* --- Tables --- */
.table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

.table th,
.table td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.table th {
  background: var(--bg);
  color: var(--ink-muted);
  font-weight: 500;
}

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

/* --- Forms --- */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
  margin-bottom: 14px;
}

.field {
  display: grid;
  gap: 6px;
  font-size: 13px;
  color: var(--ink-muted);
}

.field--wide {
  grid-column: 1 / -1;
}

.field input,
.field select,
.field textarea {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--ink);
}

.checkbox-group {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  margin: 0 0 14px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px 20px;
}

.checkbox-group legend {
  padding: 0 6px;
  font-size: 13px;
  color: var(--ink-muted);
}

.checkbox-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
}

.form-actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}

/* --- Detail / view --- */
.detail-list {
  display: grid;
  grid-template-columns: 160px 1fr;
  row-gap: 10px;
  margin: 0 0 20px;
}

.detail-list dt {
  color: var(--ink-muted);
  font-size: 13px;
}

.detail-list dd {
  margin: 0;
}

/* --- Card grid (landing pages) --- */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
}

.card--link {
  text-decoration: none;
  display: block;
}

.card--link h3 {
  margin: 0 0 6px;
  font-size: 16px;
}

.card--link p {
  margin: 0;
  color: var(--ink-muted);
  font-size: 13px;
}

/* --- Shared states --- */
.state {
  padding: 32px;
  text-align: center;
  border-radius: 10px;
  border: 1px dashed var(--border);
  color: var(--ink-muted);
}

/* --- Errors --- */
.error {
  background: var(--red-bg);
  color: var(--red);
  border: 1px solid rgba(138, 59, 50, 0.25);
  border-radius: 8px;
  padding: 14px 16px;
  margin: 12px 0;
}

.error__title {
  margin: 0 0 4px;
}

.error__hint,
.error__meta {
  margin: 4px 0 0;
  font-size: 13px;
  opacity: 0.85;
}

/* --- Status badges --- */
.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 12px;
  color: white;
}

.badge.tone-neutral {
  background: #8a8578;
}

.badge.tone-amber {
  background: var(--amber);
}

.badge.tone-teal {
  background: var(--teal);
}

.badge.tone-green {
  background: var(--green);
}

.badge.tone-red {
  background: var(--red);
}

/* --- Dialog --- */
.cas-dialog {
  border: none;
  border-radius: 10px;
  padding: 0;
  max-width: 420px;
}

.cas-dialog::backdrop {
  background: rgba(34, 31, 27, 0.4);
}

.cas-dialog__form {
  padding: 20px;
  display: grid;
  gap: 10px;
}

.cas-dialog__actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 10px;
}

.password-reveal {
  display: flex;
  gap: 8px;
  align-items: center;
}

.password-reveal input[data-password-field] {
  flex: 1;
  font-family: ui-monospace, 'SFMono-Regular', Consolas, monospace;
}

.password-reveal__warning {
  color: var(--red);
  font-size: 0.9em;
  margin: 0;
}

.password-reveal__copy-status {
  color: var(--teal);
  font-size: 0.85em;
  margin: 0;
}

@media (max-width: 720px) {
  .shell {
    grid-template-columns: 1fr;
  }
  .shell__sidebar {
    padding: 12px;
  }
}

/* --- Traceability: genealogy graph (Phase 6 compliance patch) --- */
.trace-graph {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.trace-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: flex-start;
}

.trace-node {
  display: block;
  min-width: 160px;
  max-width: 260px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  text-decoration: none;
  color: var(--ink);
}

.trace-node__title {
  font-weight: 600;
}

.trace-node__subtitle {
  font-size: 0.85em;
  color: var(--ink-muted);
}

.trace-node--searched {
  border: 2px solid var(--gold);
  box-shadow: 0 0 0 3px rgba(182, 134, 60, 0.15);
}

.trace-node--linked {
  cursor: pointer;
}

.trace-node--linked:hover {
  border-color: var(--teal);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

.trace-edges {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-left: 4px;
}

.trace-edge {
  font-size: 0.85em;
  color: var(--ink-muted);
}

/* --- Internal Audit (UI Q2): fixed checklist sections + compact System Reference cards --- */
.table-scroll {
  overflow-x: auto;
  margin-bottom: 20px;
}

.ia-section {
  margin-bottom: 20px;
}

.ia-section__title {
  margin: 0 0 10px;
  font-size: 15px;
  color: var(--ink-muted);
  border-bottom: 1px solid var(--border);
  padding-bottom: 6px;
}

.ia-question {
  margin-bottom: 12px;
}

.ia-question__text {
  margin: 0 0 10px;
  font-weight: 500;
}

.ia-reference {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  margin: 0 0 14px;
  background: var(--bg);
}

.ia-reference__label {
  font-size: 12px;
  color: var(--ink-muted);
  margin-bottom: 4px;
}

.ia-reference__line {
  font-size: 13px;
  margin-bottom: 4px;
}

/* --- Reporting (UI R1) — additive only; no existing rule above is changed --- */

.report-preview {
  margin: 16px 0;
}

.report-preview__actions {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.report-preview__note {
  font-size: 12px;
  color: var(--ink-muted);
  margin: 4px 0 12px;
}

.report-table th,
.report-table td {
  font-size: 12px;
  white-space: nowrap;
}

@media print {
  .shell__sidebar,
  .shell__topbar,
  .page-header__actions,
  .filters,
  #filters-region,
  .back-link,
  .report-preview__actions {
    display: none !important;
  }
  .shell__content {
    padding: 0 !important;
  }
  .report-table th,
  .report-table td {
    white-space: normal;
  }
}
