/* eSign SAMTDB
   Hướng thiết kế: công cụ tài liệu doanh nghiệp, sạch, rõ trạng thái.
   Màu theo nhận diện SAM: xanh dương chỉ làm điểm nhấn (~10%), nền trắng + xám thép.
   Không dùng màu vàng. */

@font-face {
  font-family: "Inter";
  font-style: normal;
  font-weight: 100 900;
  font-display: swap;
  src: url("/static/vendor/fonts/inter-viet.woff2") format("woff2");
  unicode-range: U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1,
    U+01AF-01B0, U+0300-0301, U+0303-0304, U+0308-0309, U+0323, U+0329, U+1EA0-1EF9, U+20AB;
}
@font-face {
  font-family: "Inter";
  font-style: normal;
  font-weight: 100 900;
  font-display: swap;
  src: url("/static/vendor/fonts/inter-latin.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
    U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215,
    U+FEFF, U+FFFD;
}

:root {
  /* Màu */
  --blue: #2166b0;
  --blue-ink: #184f8c;
  --blue-deep: #123d6e;
  --blue-tint: #eaf2fb;
  --blue-tint-2: #d5e5f6;

  --ink: #242a31;
  --ink-2: #454d57;
  --ink-soft: #5f6873;
  --ink-mute: #8a929b;
  --line: #e3e8ed;
  --line-strong: #cfd6de;
  --surface: #ffffff;
  --bg: #f6f8fa;
  --canvas: #e8ecf0; /* nền vùng xem tài liệu */

  --danger: #b42318;
  --danger-tint: #fef3f2;
  --danger-line: #fecdca;
  --success: #067647;
  --success-tint: #ecfdf3;

  /* Nhận diện công ty: mỗi yêu cầu luôn mang màu của công ty sở hữu.
     SAM theo xanh thương hiệu; TDB xanh mòng két, cùng họ lạnh nhưng phân biệt được ngay. */
  --co-sam: var(--blue);
  --co-sam-tint: var(--blue-tint);
  --co-tdb: #0d6e6e;
  --co-tdb-tint: #e3f2f1;

  /* Chữ */
  --font: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  --text-xs: 0.75rem;
  --text-sm: 0.8125rem;
  --text-base: 0.9375rem;
  --text-md: 1.0625rem;
  --text-lg: 1.25rem;
  --text-xl: 1.625rem;
  --text-2xl: 2rem;

  /* Khoảng cách (gốc 4px) */
  --s-1: 4px;
  --s-2: 8px;
  --s-3: 12px;
  --s-4: 16px;
  --s-5: 24px;
  --s-6: 32px;
  --s-7: 48px;
  --s-8: 64px;

  /* Bo góc: một họ duy nhất */
  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 14px;
  --r-pill: 999px;

  /* Độ nổi: bóng nhiều lớp, ngả xanh xám */
  --sh-1: 0 1px 2px rgba(18, 38, 63, 0.05), 0 1px 3px rgba(18, 38, 63, 0.06);
  --sh-2: 0 4px 10px -3px rgba(18, 38, 63, 0.1), 0 2px 4px -2px rgba(18, 38, 63, 0.06);
  --sh-3: 0 18px 40px -12px rgba(18, 38, 63, 0.22), 0 6px 12px -6px rgba(18, 38, 63, 0.1);

  /* Chuyển động */
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-in-out: cubic-bezier(0.65, 0, 0.35, 1);
  --dur-fast: 140ms;
  --dur-base: 220ms;

  --header-h: 56px;
  --rail-w: 384px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

/* Luật mặc định của trình duyệt cho [hidden] rất yếu, class nào đặt display
   là đè mất. Ép lại để thuộc tính hidden luôn ẩn thật. */
[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: var(--font);
  font-size: var(--text-base);
  line-height: 1.55;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  font-feature-settings: "cv11", "ss01";
}

h1,
h2,
h3 {
  margin: 0;
  line-height: 1.2;
  letter-spacing: -0.015em;
  color: var(--ink);
}

::selection {
  background: var(--blue-tint-2);
}

a {
  color: var(--blue);
  text-decoration: none;
}
a:hover {
  color: var(--blue-ink);
}

.icon {
  width: 18px;
  height: 18px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.icon-sm {
  width: 16px;
  height: 16px;
}

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
}

/* ---------- Nút ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s-2);
  min-height: 40px;
  padding: 0 var(--s-4);
  border: 1px solid transparent;
  border-radius: var(--r-md);
  font: inherit;
  font-size: var(--text-sm);
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color var(--dur-fast) var(--ease-out),
    border-color var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out),
    transform var(--dur-fast) var(--ease-out);
}
.btn:active:not(:disabled) {
  transform: scale(0.98);
}
.btn:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.btn-primary {
  background: var(--blue);
  color: #fff;
}
.btn-primary:hover:not(:disabled) {
  background: var(--blue-ink);
}

.btn-secondary {
  background: var(--surface);
  border-color: var(--line-strong);
  color: var(--ink);
}
.btn-secondary:hover:not(:disabled) {
  border-color: var(--ink-mute);
  background: var(--bg);
}

.btn-ghost {
  background: transparent;
  color: var(--ink-soft);
}
.btn-ghost:hover:not(:disabled) {
  background: var(--bg);
  color: var(--ink);
}

.btn-block {
  width: 100%;
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: var(--r-sm);
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}
.icon-btn:hover:not(:disabled) {
  background: var(--bg);
  color: var(--ink);
}
.icon-btn.danger:hover:not(:disabled) {
  background: var(--danger-tint);
  color: var(--danger);
}
.icon-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.spin {
  animation: spin 0.9s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ---------- Ô nhập ---------- */

.field {
  display: grid;
  gap: 6px;
}
.field + .field {
  margin-top: var(--s-4);
}
.label {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink-2);
}
.hint {
  font-size: var(--text-xs);
  color: var(--ink-soft);
}

.input,
.select {
  width: 100%;
  min-height: 40px;
  padding: 0 var(--s-3);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-md);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: var(--text-base);
  transition: border-color var(--dur-fast) var(--ease-out), box-shadow var(--dur-fast) var(--ease-out);
}
.input::placeholder {
  color: var(--ink-mute);
}
.input:hover,
.select:hover {
  border-color: var(--ink-mute);
}
.input:focus,
.select:focus {
  outline: none;
  border-color: var(--blue);
  box-shadow: 0 0 0 3px var(--blue-tint-2);
}
.input:disabled,
.select:disabled {
  background: var(--bg);
  color: var(--ink-soft);
}
.input-sm,
.select-sm {
  min-height: 34px;
  font-size: var(--text-sm);
  border-radius: var(--r-sm);
}

.check {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  font-size: var(--text-sm);
  color: var(--ink-2);
  cursor: pointer;
  user-select: none;
}
.check input {
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--blue);
}

/* ---------- Thông báo lỗi / trạng thái ---------- */

.alert {
  display: flex;
  gap: var(--s-2);
  align-items: flex-start;
  padding: var(--s-3);
  border-radius: var(--r-md);
  font-size: var(--text-sm);
}
.alert-danger {
  background: var(--danger-tint);
  border: 1px solid var(--danger-line);
  color: var(--danger);
}
.alert-success {
  background: var(--success-tint);
  color: var(--success);
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 10px;
  border-radius: var(--r-pill);
  font-size: var(--text-xs);
  font-weight: 600;
  line-height: 1.6;
  white-space: nowrap;
}
.chip-draft,
.chip-cancelled,
.chip-superseded {
  background: #eef1f4;
  color: var(--ink-2);
}
.chip-pending,
.chip-in_progress {
  background: var(--blue-tint);
  color: var(--blue-ink);
}
.chip-completed {
  background: var(--success-tint);
  color: var(--success);
}
.chip-rejected,
.chip-revision_required {
  background: var(--danger-tint);
  color: var(--danger);
}
.chip-company {
  background: transparent;
  border: 1px solid var(--line-strong);
  color: var(--ink-2);
  font-weight: 600;
  letter-spacing: 0.02em;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: var(--s-5);
  z-index: 80;
  display: flex;
  align-items: center;
  gap: var(--s-2);
  max-width: min(92vw, 440px);
  padding: var(--s-3) var(--s-4);
  border-radius: var(--r-md);
  background: var(--ink);
  color: #fff;
  font-size: var(--text-sm);
  box-shadow: var(--sh-3);
  transform: translate(-50%, 12px);
  opacity: 0;
  transition: opacity var(--dur-base) var(--ease-out), transform var(--dur-base) var(--ease-out);
  pointer-events: none;
}
.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}
.toast.danger {
  background: var(--danger);
}

/* ---------- Khung ứng dụng ---------- */

.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: var(--s-4);
  height: var(--header-h);
  padding: 0 var(--s-5);
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  font-weight: 700;
  letter-spacing: -0.01em;
}
.brand:hover {
  color: var(--ink);
}
.brand-mark {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: var(--r-sm);
  background: var(--blue);
  color: #fff;
}
.brand-sub {
  font-weight: 500;
  color: var(--ink-soft);
  font-size: var(--text-sm);
}
.header-spacer {
  flex: 1;
}
.user-name {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink-2);
}

.page {
  max-width: 1120px;
  margin: 0 auto;
  padding: var(--s-6) var(--s-5) var(--s-8);
}
.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--s-4);
  margin-bottom: var(--s-5);
}
.page-title {
  font-size: var(--text-2xl);
  font-weight: 700;
}
.page-lead {
  margin: var(--s-1) 0 0;
  color: var(--ink-soft);
}

/* ---------- Đăng nhập ---------- */

.auth {
  display: grid;
  min-height: 100dvh;
  grid-template-columns: minmax(0, 1fr);
  place-items: center;
  padding: var(--s-5);
  background: radial-gradient(1200px 600px at 10% -10%, #eef4fb 0%, transparent 60%), var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 400px;
  padding: var(--s-6);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--sh-2);
}
.auth-card .brand {
  margin-bottom: var(--s-6);
}
.auth-title {
  font-size: var(--text-xl);
  font-weight: 700;
}
.auth-lead {
  margin: var(--s-2) 0 var(--s-5);
  color: var(--ink-soft);
  font-size: var(--text-sm);
}
.auth-card form .btn {
  margin-top: var(--s-5);
}
.auth-card .alert {
  margin-bottom: var(--s-4);
}

/* ---------- Danh sách hồ sơ ---------- */

.tabs {
  display: inline-flex;
  padding: 3px;
  margin-bottom: var(--s-4);
  border-radius: var(--r-md);
  background: #eceff3;
}
.tab {
  min-height: 32px;
  padding: 0 var(--s-4);
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink-soft);
  font: inherit;
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}
.tab:hover {
  color: var(--ink);
}
.tab[aria-selected="true"] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--sh-1);
}
.tab:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 1px;
}
.list-filters {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: var(--s-2) var(--s-4);
  margin-bottom: var(--s-4);
}
.list-filters .tabs {
  margin-bottom: 0;
}
.tab-short,
.mobile-only {
  display: none;
}

.doc-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  overflow: hidden;
}
.doc-table th {
  padding: var(--s-3) var(--s-4);
  text-align: left;
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--ink-soft);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
  background: #fafbfc;
  border-bottom: 1px solid var(--line);
}
.doc-table td {
  padding: var(--s-3) var(--s-4);
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
.doc-table tr:last-child td {
  border-bottom: 0;
}
.doc-table tbody tr {
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease-out);
}
.doc-table tbody tr:hover {
  background: #f8fafc;
}
.doc-number {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--ink-2);
  white-space: nowrap;
}
.doc-title a {
  color: var(--ink);
  font-weight: 600;
}
.doc-title a:hover {
  color: var(--blue);
}
.doc-date {
  color: var(--ink-soft);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.empty {
  display: grid;
  justify-items: start;
  gap: var(--s-2);
  padding: var(--s-7) var(--s-6);
  background: var(--surface);
  border: 1px dashed var(--line-strong);
  border-radius: var(--r-lg);
}
.empty-title {
  font-size: var(--text-md);
  font-weight: 700;
}
.empty p {
  margin: 0 0 var(--s-3);
  color: var(--ink-soft);
  max-width: 52ch;
}

.skeleton-row {
  height: 52px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(90deg, #f4f6f8 0%, #eceff3 50%, #f4f6f8 100%);
  background-size: 200% 100%;
  animation: shimmer 1.2s ease-in-out infinite;
}
@keyframes shimmer {
  to {
    background-position: -200% 0;
  }
}

/* ---------- Hộp thoại ---------- */

dialog {
  width: min(560px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--sh-3);
}
dialog[open] {
  animation: dialog-in var(--dur-base) var(--ease-out);
}
dialog::backdrop {
  background: rgba(24, 33, 45, 0.42);
}
@keyframes dialog-in {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.98);
  }
}
.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--s-5) var(--s-5) 0;
}
.dialog-title {
  font-size: var(--text-lg);
  font-weight: 700;
}
.dialog-body {
  padding: var(--s-5);
}
.dialog-foot {
  display: flex;
  justify-content: flex-end;
  gap: var(--s-2);
  padding: var(--s-4) var(--s-5);
  border-top: 1px solid var(--line);
  background: #fafbfc;
  border-radius: 0 0 var(--r-lg) var(--r-lg);
}
.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--s-3);
  margin-bottom: var(--s-4);
}
.field-row .field + .field {
  margin-top: 0;
}

.dropzone {
  display: grid;
  justify-items: center;
  gap: var(--s-1);
  padding: var(--s-5);
  border: 1.5px dashed var(--line-strong);
  border-radius: var(--r-md);
  background: #fafbfc;
  color: var(--ink-soft);
  text-align: center;
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease-out), background-color var(--dur-fast) var(--ease-out);
}
.dropzone:hover,
.dropzone.drag {
  border-color: var(--blue);
  background: var(--blue-tint);
}
.dropzone strong {
  color: var(--ink);
  font-size: var(--text-sm);
}
.dropzone .icon {
  width: 22px;
  height: 22px;
  color: var(--blue);
}
.dropzone.has-file {
  border-style: solid;
  border-color: var(--blue-tint-2);
  background: var(--blue-tint);
}

/* ---------- Soạn vùng ký ---------- */

.editor-body {
  height: 100dvh;
  overflow: hidden;
}
.editor {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--rail-w);
  height: calc(100dvh - var(--header-h));
}

.editor-title {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  min-width: 0;
}
.editor-title h1 {
  font-size: var(--text-md);
  font-weight: 700;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.editor-title .doc-number {
  font-size: var(--text-sm);
}
.save-state {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: var(--text-sm);
  color: var(--ink-soft);
  white-space: nowrap;
}
.save-state.error {
  color: var(--danger);
}
.save-state button {
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  font-weight: 600;
  text-decoration: underline;
  cursor: pointer;
}

.viewer {
  overflow: auto;
  padding: var(--s-5) var(--s-5) var(--s-8);
  background: var(--canvas);
  overscroll-behavior: contain;
}
.pages {
  display: grid;
  gap: var(--s-5);
  max-width: 880px;
  margin: 0 auto;
}
.page-label {
  margin-bottom: var(--s-2);
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--ink-soft);
  letter-spacing: 0.03em;
}
.pdf-page {
  position: relative;
  width: 100%;
  background: #fff;
  box-shadow: var(--sh-2);
  border-radius: 2px;
}
.pdf-page canvas {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 2px;
}
.overlay {
  position: absolute;
  inset: 0;
  /* Cho cuộn và phóng to bằng hai ngón trên điện thoại; chỉ khoá khi đang vẽ khung. */
  touch-action: pan-x pan-y pinch-zoom;
}
.placing .overlay {
  cursor: crosshair;
  touch-action: none;
}
.placing .pdf-page {
  outline: 2px dashed var(--blue);
  outline-offset: 4px;
}

.box {
  position: absolute;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-width: 0;
  padding: 4px 6px;
  border: 1.5px solid var(--blue);
  border-radius: 4px;
  background: rgba(33, 102, 176, 0.08);
  color: var(--blue-deep);
  font-size: 11px;
  line-height: 1.3;
  cursor: grab;
  touch-action: none;
  user-select: none;
  overflow: hidden;
  container-type: inline-size;
  transition: background-color var(--dur-fast) var(--ease-out), border-color var(--dur-fast) var(--ease-out);
}
/* Khung nhỏ (điện thoại, ô ký hẹp): chỉ còn số bước. Ô ký trên biểu mẫu đã có nhãn sẵn. */
@container (max-width: 96px) {
  .box-name,
  .box-signer {
    display: none;
  }
  .box-head {
    justify-content: center;
    height: 100%;
  }
}
.readonly .box {
  cursor: default;
}
.box:hover {
  background: rgba(33, 102, 176, 0.13);
}
.box.selected {
  border-width: 2px;
  border-color: var(--blue-ink);
  background: rgba(33, 102, 176, 0.16);
  z-index: 2;
}
.box.no-signer {
  border-style: dashed;
  border-color: var(--ink-mute);
  background: rgba(95, 104, 115, 0.08);
  color: var(--ink-2);
}
.box.dragging {
  cursor: grabbing;
  box-shadow: var(--sh-2);
}
.box-head {
  display: flex;
  align-items: center;
  gap: 5px;
  min-width: 0;
  font-weight: 700;
}
.box-num {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--blue);
  color: #fff;
  font-size: 10px;
}
.no-signer .box-num {
  background: var(--ink-mute);
}
.box-name,
.box-signer {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.box-signer {
  font-weight: 500;
  opacity: 0.85;
}
.box-handle {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 18px;
  height: 18px;
  cursor: nwse-resize;
  touch-action: none;
}
.box-handle::after {
  content: "";
  position: absolute;
  right: 3px;
  bottom: 3px;
  width: 8px;
  height: 8px;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  opacity: 0.7;
}
.readonly .box-handle {
  display: none;
}
.draft-rect {
  position: absolute;
  border: 1.5px dashed var(--blue);
  background: rgba(33, 102, 176, 0.1);
  border-radius: 4px;
  pointer-events: none;
}

.rail {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface);
  border-left: 1px solid var(--line);
}
.rail-head {
  padding: var(--s-5) var(--s-5) var(--s-4);
  border-bottom: 1px solid var(--line);
}
.rail-title {
  font-size: var(--text-lg);
  font-weight: 700;
}
.rail-lead {
  margin: var(--s-1) 0 var(--s-4);
  color: var(--ink-soft);
  font-size: var(--text-sm);
}
.rail-actions {
  display: flex;
  gap: var(--s-2);
}
.rail-actions .btn {
  flex: 1;
}
.place-banner {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  margin-top: var(--s-3);
  padding: var(--s-2) var(--s-3);
  border-radius: var(--r-md);
  background: var(--blue-tint);
  color: var(--blue-ink);
  font-size: var(--text-sm);
  font-weight: 500;
}
.place-banner .btn {
  margin-left: auto;
  min-height: 30px;
  padding: 0 var(--s-3);
}

.rail-scroll {
  flex: 1;
  overflow: auto;
  padding: var(--s-4) var(--s-5) var(--s-6);
}
.blocks {
  display: grid;
  gap: var(--s-3);
  margin: 0;
  padding: 0;
  list-style: none;
}
.block-card {
  padding: var(--s-3);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--surface);
  transition: border-color var(--dur-fast) var(--ease-out), box-shadow var(--dur-fast) var(--ease-out);
}
.block-card:hover {
  border-color: var(--line-strong);
}
.block-card.selected {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px var(--blue-tint);
}
.block-top {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  margin-bottom: var(--s-2);
}
.block-num {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--blue-tint);
  color: var(--blue-ink);
  font-size: var(--text-xs);
  font-weight: 700;
}
.block-card.parallel .block-num {
  background: #eef1f4;
  color: var(--ink-2);
}
.block-top .input {
  flex: 1;
  min-width: 0;
  font-weight: 600;
}
/* Chọn vai trò: hiện ở lượt đang chọn hoặc lượt chưa có vai trò. */
.role-picks {
  display: none;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0 0 var(--s-2) 32px;
}
.block-card.selected .role-picks,
.block-card.needs-role .role-picks {
  display: flex;
}
.block-card.needs-role:not(.selected) {
  border-color: var(--line-strong);
}
.role-pick {
  padding: 3px 10px;
  border: 1px solid var(--line);
  border-radius: var(--r-pill);
  background: var(--surface);
  color: var(--ink-2);
  font: inherit;
  font-size: var(--text-xs);
  font-weight: 600;
  line-height: 1.6;
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease-out), background-color var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}
.role-pick:hover {
  border-color: var(--blue);
  color: var(--blue-ink);
}
.role-pick:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
}
.role-pick:active {
  transform: scale(0.97);
}
.role-pick[aria-pressed="true"] {
  border-color: var(--blue);
  background: var(--blue-tint);
  color: var(--blue-ink);
}
.block-tools {
  display: flex;
  flex: none;
}
.block-opts {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--s-2) var(--s-4);
  margin-top: var(--s-3);
}
.block-opts .select-sm {
  width: auto;
}
.parallel-note {
  margin: 0 0 var(--s-2) 32px;
  font-size: var(--text-xs);
  color: var(--ink-soft);
}

.problems {
  display: grid;
  gap: var(--s-2);
  margin-top: var(--s-5);
}
.problems-title {
  font-size: var(--text-sm);
  font-weight: 700;
  color: var(--ink-2);
}
.problems ul {
  display: grid;
  gap: var(--s-2);
  margin: 0;
  padding: 0;
  list-style: none;
}

.rail-foot {
  padding: var(--s-4) var(--s-5);
  border-top: 1px solid var(--line);
  background: var(--surface);
}
.rail-foot-note {
  margin: var(--s-2) 0 0;
  color: var(--ink-soft);
  font-size: var(--text-xs);
}
.block-top .chip {
  margin-left: auto;
  flex: none;
}

/* Chuông thông báo */
.bell {
  position: relative;
}
.bell-count {
  position: absolute;
  top: 2px;
  right: 2px;
  display: grid;
  place-items: center;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: var(--r-pill);
  background: var(--danger);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  line-height: 1;
}
.bell-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 30;
  width: min(360px, calc(100vw - 32px));
  max-height: 70vh;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: var(--surface);
  box-shadow: var(--sh-3);
}
.bell-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--s-4) var(--s-4) var(--s-2);
}
.btn-link {
  border: 0;
  background: none;
  color: var(--blue);
  font: inherit;
  font-size: var(--text-sm);
  cursor: pointer;
}
.btn-link:hover {
  text-decoration: underline;
}
.bell-list {
  margin: 0;
  padding: 0 0 var(--s-2);
  list-style: none;
}
.bell-item {
  display: grid;
  grid-template-columns: 16px minmax(0, 1fr);
  column-gap: 10px;
  padding: var(--s-3) var(--s-4);
  border-top: 1px solid var(--line);
  color: var(--ink);
  text-decoration: none;
}
.bell-item:hover {
  background: var(--bg);
}
.bell-item.unread {
  border-left: 3px solid var(--blue);
}
.bell-item > .icon {
  grid-row: 1 / span 3;
  margin-top: 2px;
  color: var(--ink-soft);
}
.bell-item.unread > .icon {
  color: var(--blue);
}
.bell-item strong {
  display: block;
  font-size: var(--text-sm);
}
.bell-item span {
  display: block;
  grid-column: 2;
  color: var(--ink-soft);
  font-size: var(--text-xs);
}
.bell-empty {
  padding: var(--s-5) var(--s-4);
  color: var(--ink-soft);
  font-size: var(--text-sm);
  text-align: center;
}

.tab-count {
  display: inline-grid;
  place-items: center;
  min-width: 20px;
  height: 20px;
  margin-left: var(--s-2);
  padding: 0 6px;
  border-radius: var(--r-pill);
  background: var(--blue);
  color: #fff;
  font-size: var(--text-xs);
  font-weight: 700;
}

/* Trang chi tiết hồ sơ */
.rail-section {
  margin-top: var(--s-6);
  padding-top: var(--s-5);
  border-top: 1px solid var(--line);
}
.rail-subtitle {
  margin-bottom: var(--s-3);
  font-size: var(--text-sm);
  font-weight: 700;
  color: var(--ink-2);
}
.block-card.mine {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px var(--blue-tint);
}
.block-signer {
  font-weight: 600;
}
.attach-list {
  display: grid;
  gap: var(--s-1);
  margin: 0 0 var(--s-3);
  padding: 0;
  list-style: none;
}
.attach-item {
  display: flex;
  align-items: center;
  gap: var(--s-2);
}
.attach-item .link-row {
  flex: 1;
  min-width: 0;
}
.links {
  display: grid;
  gap: var(--s-1);
}
.link-row {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  padding: var(--s-2);
  border-radius: var(--r-sm);
  color: var(--ink-2);
  font-size: var(--text-sm);
  text-decoration: none;
  transition: background var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}
.link-row:hover {
  background: var(--bg);
  color: var(--blue-ink);
}
.link-row span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.decision-row {
  display: flex;
  gap: var(--s-2);
  margin-top: var(--s-2);
}
.decision-row .btn {
  flex: 1;
}
.btn-secondary.danger {
  color: var(--danger);
}
.btn-secondary.danger:hover:not(:disabled) {
  background: var(--danger-tint);
  border-color: var(--danger-line);
}
.block-reassign {
  margin-top: var(--s-2);
  padding: 0;
  font-size: var(--text-sm);
}
.block-reason {
  margin-top: var(--s-2);
  padding: var(--s-2) var(--s-3);
  border-left: 2px solid var(--danger-line);
  background: var(--danger-tint);
  color: var(--danger);
  font-size: var(--text-sm);
}
textarea.input {
  min-height: 76px;
  padding: var(--s-3);
  line-height: 1.5;
  resize: vertical;
}

.dialog-lead {
  margin-top: 0;
  margin-bottom: var(--s-4);
  color: var(--ink-soft);
  font-size: var(--text-sm);
}
.pin-input {
  font-size: var(--text-lg);
  text-align: center;
}
.pin-input:not(:placeholder-shown) {
  letter-spacing: 0.35em;
}
.pin-input::-ms-reveal {
  display: none;
}

.rail-empty {
  display: grid;
  gap: var(--s-2);
  padding: var(--s-5);
  border: 1px dashed var(--line-strong);
  border-radius: var(--r-md);
  color: var(--ink-soft);
  font-size: var(--text-sm);
}
.rail-empty strong {
  color: var(--ink);
  font-size: var(--text-base);
}

.viewer-loading {
  display: grid;
  place-items: center;
  gap: var(--s-2);
  padding: var(--s-8) 0;
  color: var(--ink-soft);
}

.mobile-bar {
  display: none;
}

/* ---------- Công ty ---------- */

.co-sam {
  --co: var(--co-sam);
  --co-tint: var(--co-sam-tint);
}
.co-tdb {
  --co: var(--co-tdb);
  --co-tint: var(--co-tdb-tint);
}
.co-badge {
  display: inline-grid;
  place-items: center;
  min-width: 44px;
  height: 22px;
  padding: 0 7px;
  border-radius: var(--r-sm);
  background: var(--co, var(--ink-2));
  color: #fff;
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.04em;
  line-height: 1;
  white-space: nowrap;
}

.title-stack {
  display: grid;
  min-width: 0;
  line-height: 1.25;
}
.editor-title .title-stack .doc-number {
  overflow: hidden;
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--ink-soft);
  white-space: nowrap;
  text-overflow: ellipsis;
}

.co-strip {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  margin-bottom: var(--s-4);
  padding: var(--s-2) var(--s-3);
  border-radius: var(--r-md);
  background: var(--co-tint);
  box-shadow: inset 3px 0 0 var(--co);
}
.co-strip > div {
  display: grid;
  min-width: 0;
  line-height: 1.35;
}
.co-strip-label {
  font-size: var(--text-xs);
  color: var(--ink-soft);
}
.co-strip strong {
  font-size: var(--text-sm);
  color: var(--ink);
}

/* Chọn công ty khi tạo hồ sơ: hai thẻ lớn, không phải danh sách thả xuống dễ bấm nhầm. */
.dialog-body > .alert {
  margin-bottom: var(--s-4);
}
fieldset.field {
  margin: 0 0 var(--s-4);
  padding: 0;
  border: 0;
  min-width: 0;
}
.co-options {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--s-2);
}
.co-option {
  position: relative;
  display: grid;
  justify-items: start;
  gap: var(--s-2);
  padding: var(--s-3) var(--s-4) var(--s-4);
  border: 1.5px solid var(--line-strong);
  border-radius: var(--r-md);
  background: var(--surface);
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease-out), background-color var(--dur-fast) var(--ease-out);
}
.co-option:hover {
  border-color: var(--co);
}
.co-option:has(input:focus-visible) {
  outline: 2px solid var(--co);
  outline-offset: 2px;
}
.co-option:has(input:checked) {
  border-color: var(--co);
  background: var(--co-tint);
}
.co-option-text {
  display: grid;
  min-width: 0;
  line-height: 1.35;
}
.co-option-text strong {
  font-size: var(--text-sm);
}
.co-option-text span {
  font-size: var(--text-xs);
  color: var(--ink-soft);
}
.co-option-check {
  position: absolute;
  top: var(--s-3);
  right: var(--s-3);
  color: var(--co);
  opacity: 0;
  transform: scale(0.8);
  transition: opacity var(--dur-fast) var(--ease-out), transform var(--dur-fast) var(--ease-out);
}
.co-option:has(input:checked) .co-option-check {
  opacity: 1;
  transform: none;
}

/* Loại yêu cầu: hai nút lớn cạnh nhau, không chọn sẵn. */
.type-options {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: var(--s-2);
}
.type-option {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  min-height: 44px;
  padding: 0 var(--s-4);
  border: 1.5px solid var(--line-strong);
  border-radius: var(--r-md);
  background: var(--surface);
  color: var(--ink-2);
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease-out), background-color var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}
.type-option:hover {
  border-color: var(--blue);
}
.type-option:has(input:focus-visible) {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
}
.type-option:has(input:checked) {
  border-color: var(--blue);
  background: var(--blue-tint);
  color: var(--blue-ink);
}

/* Danh sách: tên hồ sơ là dòng chính, số hồ sơ dòng phụ. */
.doc-title .doc-number {
  display: block;
  margin-top: 2px;
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--ink-soft);
}
.doc-co {
  width: 64px;
}
.doc-person {
  font-size: var(--text-sm);
  color: var(--ink-2);
}

/* ---------- In ---------- */

.print-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--s-2);
  margin-top: var(--s-3);
}
.print-actions .btn {
  padding: 0 var(--s-2);
}
.print-note {
  margin: 0 0 var(--s-2);
}
/* Khung nạp PDF để in: phải có kích thước thật (display:none thì trình duyệt in trang trắng). */
.print-frame {
  position: fixed;
  right: 0;
  bottom: 0;
  width: 1px;
  height: 1px;
  border: 0;
  opacity: 0;
  pointer-events: none;
}

/* ---------- Điều hướng đầu trang ---------- */

.header-nav {
  display: flex;
  gap: var(--s-1);
}
.nav-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 32px;
  padding: 0 var(--s-3);
  border-radius: var(--r-sm);
  color: var(--ink-soft);
  font-size: var(--text-sm);
  font-weight: 600;
  transition: background-color var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}
.nav-link:hover {
  background: var(--bg);
  color: var(--ink);
}
.nav-link[aria-current="page"] {
  background: var(--blue-tint);
  color: var(--blue-ink);
}

/* ---------- Quản trị ---------- */

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--s-2);
  margin-bottom: var(--s-4);
}
.toolbar .btn-primary {
  margin-left: auto;
}
.search {
  position: relative;
  flex: 1 1 260px;
  max-width: 360px;
}
.search .icon {
  position: absolute;
  top: 50%;
  left: var(--s-3);
  color: var(--ink-mute);
  transform: translateY(-50%);
  pointer-events: none;
}
.search .input {
  padding-left: 36px;
}
.select-inline {
  flex: 0 0 auto;
  width: auto;
  min-width: 160px;
}

.icon-xs {
  width: 13px;
  height: 13px;
}
.user-cell-name {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  font-weight: 600;
}
.member-lines {
  display: grid;
  gap: 6px;
}
.member-line {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  font-size: var(--text-sm);
  color: var(--ink-2);
}
.chip-stack {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s-1);
}
.chip-admin {
  background: var(--blue-tint);
  color: var(--blue-ink);
}
.row-actions {
  width: 1%;
  white-space: nowrap;
  text-align: right;
}
.table-note {
  margin: var(--s-3) 0 0;
}
.audit-lead {
  margin: 0 0 var(--s-3);
}
.audit-table td {
  font-size: var(--text-sm);
}
.audit-table tbody tr {
  cursor: default;
}
.audit-table tbody tr.row-warn {
  background: var(--danger-tint);
}
.audit-doc {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.audit-detail {
  max-width: 360px;
  color: var(--ink-2);
}
.load-more {
  display: flex;
  justify-content: center;
  margin-top: var(--s-4);
}

#user-dialog {
  width: min(640px, calc(100vw - 32px));
}
.dialog-sm {
  width: min(440px, calc(100vw - 32px));
}
.dialog-section {
  margin: var(--s-5) 0 0;
  padding: var(--s-5) 0 0;
  border: 0;
  border-top: 1px solid var(--line);
  min-width: 0;
}
.dialog-section legend {
  float: left;
  width: 100%;
  margin-bottom: var(--s-1);
  padding: 0;
}
.dialog-section > .hint {
  display: block;
  clear: both;
  margin: 0 0 var(--s-3);
}
.membership-list {
  display: grid;
  gap: var(--s-2);
  clear: both;
}
.membership {
  padding: var(--s-3);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  transition: border-color var(--dur-fast) var(--ease-out), background-color var(--dur-fast) var(--ease-out);
}
.membership.on {
  border-color: var(--co);
  background: var(--co-tint);
}
.membership-head {
  width: 100%;
  font-weight: 600;
  color: var(--ink);
}
.membership-head input {
  accent-color: var(--co);
}
.membership-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--s-2);
  margin-top: var(--s-3);
}
.membership:not(.on) .membership-fields {
  display: none;
}
.check-block {
  display: flex;
  align-items: flex-start;
  width: 100%;
  clear: both;
}
.check-block + .check-block {
  margin-top: var(--s-3);
}
.check-block input {
  margin-top: 3px;
}
.check-block > span {
  display: grid;
  gap: 2px;
}
.check-block strong {
  color: var(--ink);
}
.check input:disabled + span {
  opacity: 0.6;
}
.pin-field {
  margin-top: var(--s-4);
}

/* Thiết bị dùng từ ngoài công ty (hộp sửa người dùng) */
.btn-sm {
  min-height: 32px;
  padding: 0 var(--s-3);
}
.device-list {
  display: grid;
  gap: var(--s-2);
  clear: both;
  margin: 0;
  padding: 0;
  list-style: none;
}
.device-item {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: var(--s-3);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--surface);
}
.device-item.pending {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px var(--blue-tint);
}
.device-icon {
  display: grid;
  place-items: center;
  flex: none;
  width: 36px;
  height: 36px;
  border-radius: var(--r-md);
  background: var(--bg);
  color: var(--ink-soft);
}
.device-main {
  display: grid;
  gap: 2px;
  flex: 1;
  min-width: 0;
}
.device-top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--s-2);
}
.device-actions {
  display: flex;
  flex: none;
  gap: var(--s-2);
}
@media (max-width: 520px) {
  .device-item {
    flex-wrap: wrap;
  }
  .device-actions {
    width: 100%;
    justify-content: flex-end;
  }
}
.pin-row {
  display: flex;
  align-items: center;
  gap: var(--s-3);
}
.pin-row .input-pin {
  max-width: 11rem;
}
.pin-row .input-pin:not(:placeholder-shown) {
  letter-spacing: 0.2em;
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}
.btn-danger:hover:not(:disabled) {
  background: #912018;
}

.co-summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(320px, 100%), 1fr));
  gap: var(--s-3);
  margin-bottom: var(--s-5);
}
.co-panel {
  min-width: 0;
  padding: var(--s-4) var(--s-5);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: var(--surface);
  box-shadow: inset 0 3px 0 var(--co), var(--sh-1);
}
.co-panel-head {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  margin-bottom: var(--s-4);
}
.co-panel-name {
  min-width: 0;
  overflow: hidden;
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink-2);
  white-space: nowrap;
  text-overflow: ellipsis;
}
.co-panel-total {
  margin-left: auto;
  font-size: var(--text-sm);
  color: var(--ink-soft);
  white-space: nowrap;
}
.co-panel-total strong {
  font-size: var(--text-lg);
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}
.co-panel-stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--s-3);
  margin: 0;
}
.co-panel-stats div {
  display: grid;
  align-content: start;
  gap: 2px;
}
.co-panel-stats dt {
  font-size: var(--text-xs);
  color: var(--ink-soft);
  line-height: 1.3;
}
.co-panel-stats dd {
  margin: 0;
  font-size: var(--text-xl);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ---------- Tra cứu công khai ---------- */

.verify {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  justify-items: center;
  align-content: start;
  gap: var(--s-5);
  min-height: 100dvh;
  padding: var(--s-6) var(--s-4) var(--s-7);
}
.verify-card {
  width: 100%;
  max-width: 560px;
  padding: var(--s-5);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--sh-2);
}
.verify-status {
  display: flex;
  gap: var(--s-3);
  margin-bottom: var(--s-5);
  padding: var(--s-3) var(--s-4);
  border-radius: var(--r-md);
}
.verify-status .icon {
  width: 24px;
  height: 24px;
  margin-top: 2px;
}
.verify-status h1 {
  font-size: var(--text-md);
  font-weight: 700;
  color: inherit;
}
.verify-status p {
  margin: 2px 0 0;
  font-size: var(--text-sm);
}
.verify-status.ok {
  background: var(--success-tint);
  color: var(--success);
}
.verify-status.bad {
  background: var(--danger-tint);
  color: var(--danger);
}
.verify-doc {
  display: flex;
  align-items: flex-start;
  gap: var(--s-3);
  margin-bottom: var(--s-4);
}
.verify-doc .co-badge {
  margin-top: 2px;
}
.verify-doc-title {
  font-size: var(--text-md);
}
.verify-doc .doc-number {
  white-space: normal;
  font-size: var(--text-xs);
  color: var(--ink-soft);
}
.verify-facts {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--s-3) var(--s-4);
  margin: 0 0 var(--s-5);
  padding: var(--s-4) 0;
  border-block: 1px solid var(--line);
}
.verify-facts dt {
  font-size: var(--text-xs);
  color: var(--ink-soft);
}
.verify-facts dd {
  margin: 0;
  font-weight: 600;
}
.verify-signers {
  display: grid;
  gap: var(--s-3);
  margin: 0 0 var(--s-5);
  padding: 0;
  list-style: none;
}
.verify-signers li {
  display: flex;
  align-items: flex-start;
  gap: var(--s-3);
}
.verify-signers li > div {
  display: grid;
}
.verify-note {
  margin: 0 0 var(--s-2);
  font-size: var(--text-xs);
  color: var(--ink-soft);
}
.verify-form {
  display: flex;
  gap: var(--s-2);
}
.mono {
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  letter-spacing: 0.02em;
}
.break {
  word-break: break-all;
}

/* ---------- Màn hình nhỏ ---------- */

@media (max-width: 960px) {
  :root {
    --rail-w: 340px;
  }
}

@media (max-width: 820px) {
  .app-header {
    padding: 0 var(--s-4);
    gap: var(--s-3);
  }
  .brand-sub,
  .user-name {
    display: none;
  }
  .page {
    padding: var(--s-5) var(--s-4) var(--s-7);
  }
  .page-head {
    align-items: center;
  }
  .page-title {
    font-size: var(--text-xl);
  }
  .page-lead {
    display: none;
  }
  .tab-long {
    display: none;
  }
  .tab-short {
    display: inline;
  }
  .tab {
    padding: 0 var(--s-3);
  }

  .doc-table,
  .doc-table tbody,
  .doc-table tr,
  .doc-table td {
    display: block;
  }
  .doc-table thead {
    display: none;
  }
  /* Mỗi dòng thành một thẻ: hàng đầu là công ty + ngày, rồi tên hồ sơ, rồi người lập + trạng thái. */
  .doc-table tr {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 6px var(--s-3);
    padding: var(--s-3) var(--s-4);
    border-bottom: 1px solid var(--line);
  }
  .doc-table tr:last-child {
    border-bottom: 0;
  }
  .doc-table td {
    order: 4;
    min-width: 0;
    padding: 0;
    border: 0;
  }
  .doc-table .doc-co {
    order: 0;
  }
  .doc-table .doc-date {
    order: 1;
    justify-self: end;
    font-size: var(--text-xs);
  }
  .doc-table .doc-title {
    order: 2;
    grid-column: 1 / -1;
  }
  .doc-table .doc-person {
    order: 3;
  }
  /* Bảng người dùng: tên + nút sửa/xoá cùng hàng đầu, thông tin còn lại xuống dưới. */
  .doc-table tr:has(> .row-actions) > td {
    grid-column: 1 / -1;
  }
  .doc-table tr:has(> .row-actions) > .doc-title {
    order: 0;
    grid-column: 1;
  }
  .doc-table tr:has(> .row-actions) > .row-actions {
    order: 1;
    grid-column: 2;
    align-self: start;
    margin: -6px -8px 0 0;
  }
  .row-actions {
    display: flex;
    gap: 2px;
  }
  /* Nhật ký: loại sự kiện + giờ trên cùng, IP ẩn cho gọn. */
  .audit-table tr > td {
    grid-column: 1 / -1;
  }
  .audit-table tr > td:nth-child(2) {
    order: 0;
    grid-column: 1;
  }
  .audit-table tr > td:nth-child(1) {
    order: 1;
    grid-column: 2;
  }
  .audit-table tr > td:nth-child(6) {
    display: none;
  }
  .audit-detail {
    max-width: none;
  }

  .field-row {
    grid-template-columns: 1fr;
  }
  .header-nav .nav-link span {
    display: none;
  }
  .toolbar .btn-primary {
    margin-left: 0;
  }
  .search {
    max-width: none;
  }
  .select-inline {
    flex: 1 1 140px;
    min-width: 0;
  }
  .membership-fields {
    grid-template-columns: 1fr;
  }
  .co-summary {
    grid-template-columns: minmax(0, 1fr);
  }
  .co-panel-stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .doc-co {
    width: auto;
  }
  .row-actions {
    width: auto;
  }

  .editor-body {
    height: auto;
    overflow: visible;
  }
  .editor {
    display: block;
    height: auto;
  }
  .viewer {
    padding: var(--s-4) var(--s-3) var(--s-5);
    overflow: visible;
  }
  .rail {
    border-left: 0;
    border-top: 1px solid var(--line);
    padding-bottom: 72px;
  }
  .rail-scroll {
    overflow: visible;
  }
  /* Thanh dưới cùng đã có nút này. */
  .rail-actions [data-action="place"] {
    display: none;
  }
  .mobile-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 25;
    display: flex;
    gap: var(--s-2);
    padding: var(--s-3) var(--s-4) calc(var(--s-3) + env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--line);
    box-shadow: 0 -6px 16px -10px rgba(18, 38, 63, 0.25);
  }
  .mobile-bar .btn {
    flex: 1;
  }

  /* Điện thoại chỉ để xem và ký: tạo hồ sơ, in hồ sơ làm trên máy tính. */
  .desktop-only {
    display: none !important;
  }
  .mobile-only {
    display: revert;
  }

  /* ---- Chạm & chữ ---- */
  /* iPhone tự phóng to trang khi ô nhập có chữ dưới 16px. */
  .input,
  .select,
  textarea.input {
    min-height: 44px;
    font-size: 16px;
  }
  .input-sm,
  .select-sm {
    min-height: 40px;
    font-size: 16px;
  }
  .pin-input {
    font-size: var(--text-lg);
  }
  .btn {
    min-height: 44px;
  }
  .toast {
    left: var(--s-4);
    right: var(--s-4);
    bottom: calc(var(--s-4) + env(safe-area-inset-bottom));
    max-width: none;
    transform: translateY(12px);
  }
  .toast.show {
    transform: none;
  }
  body:has(.mobile-bar) .toast,
  body:has(.readonly .rail-foot:not([hidden])) .toast {
    bottom: calc(84px + env(safe-area-inset-bottom));
  }
  .icon-btn {
    width: 40px;
    height: 40px;
  }
  .nav-link {
    min-height: 40px;
    padding: 0 var(--s-3);
  }
  .tab {
    min-height: 36px;
  }
  .check input {
    width: 20px;
    height: 20px;
  }

  /* ---- Đầu trang ---- */
  .app-header {
    gap: var(--s-2);
    padding: 0 var(--s-2) 0 var(--s-3);
  }
  .app-header .brand {
    margin-left: var(--s-1);
  }
  .app-header .bell-count {
    top: 4px;
    right: 4px;
  }
  /* Trang hồ sơ: chỉ còn nút quay lại, tên hồ sơ 2 dòng, trạng thái, chuông. Công ty đã có ngay dưới tài liệu. */
  .has-back [data-action="logout"],
  .has-back .editor-title .co-badge {
    display: none;
  }
  .has-back .editor-title {
    flex: 1;
    gap: 0;
  }
  .has-back .header-spacer {
    display: none;
  }
  .editor-title h1 {
    font-size: var(--text-base);
  }
  .editor-title .title-stack .doc-number {
    display: block;
  }
  .has-back .chip {
    padding: 2px 8px;
  }
  /* Chỉ giữ biểu tượng trạng thái lưu; khi lỗi mới hiện chữ. */
  .save-state:not(.error) {
    font-size: 0;
    gap: 0;
  }
  .save-state.error {
    font-size: var(--text-xs);
    white-space: normal;
  }
  .role-picks {
    margin-left: 0;
    gap: var(--s-2);
  }
  .role-pick {
    min-height: 36px;
    padding: 0 var(--s-3);
  }

  /* ---- Thông báo: bám hai mép màn hình ---- */
  .bell-panel {
    position: fixed;
    top: calc(var(--header-h) + 6px);
    left: var(--s-2);
    right: var(--s-2);
    width: auto;
    max-height: calc(100dvh - var(--header-h) - 24px);
  }
  .bell-item {
    padding: var(--s-3);
  }

  /* ---- Hộp thoại thành tấm trượt từ đáy lên ---- */
  dialog,
  #user-dialog,
  .dialog-sm {
    width: 100%;
    max-width: 100%;
    max-height: calc(100dvh - 16px);
    margin: auto 0 0;
    border-bottom: 0;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    overflow: hidden;
  }
  dialog[open] {
    display: flex;
    flex-direction: column;
    animation: sheet-in var(--dur-base) var(--ease-out);
  }
  dialog > form {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
  }
  .dialog-head {
    padding: var(--s-4) var(--s-2) var(--s-2) var(--s-4);
  }
  .dialog-body {
    flex: 1;
    min-height: 0;
    padding: var(--s-2) var(--s-4) var(--s-4);
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  .dialog-foot {
    padding: var(--s-3) var(--s-4) calc(var(--s-3) + env(safe-area-inset-bottom));
    border-radius: 0;
  }
  .dialog-foot .btn {
    flex: 1;
  }
  .dialog-foot .btn-ghost {
    flex: 0 0 auto;
  }
  .co-option {
    padding: var(--s-3);
  }
  .type-options {
    grid-template-columns: minmax(0, 1fr);
  }
  .dropzone {
    padding: var(--s-4) var(--s-3);
  }
  .pin-row .input-pin {
    max-width: none;
    flex: 1;
  }

  /* ---- Trang hồ sơ: nút ký luôn nằm ở đáy màn hình ---- */
  .readonly .rail-foot {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 25;
    display: flex;
    gap: var(--s-2);
    padding: var(--s-3) var(--s-4) calc(var(--s-3) + env(safe-area-inset-bottom));
    box-shadow: 0 -6px 16px -10px rgba(18, 38, 63, 0.25);
  }
  .readonly .rail-foot .decision-row {
    display: contents;
  }
  .readonly .rail-foot .btn {
    flex: 0 0 auto;
    margin: 0;
    padding: 0 var(--s-3);
  }
  .readonly .rail-foot #sign-btn {
    order: 3;
    flex: 1;
    width: auto;
  }
  .readonly .rail-foot-note {
    display: none;
  }
  .readonly .rail:has(.rail-foot:not([hidden])) {
    padding-bottom: calc(76px + env(safe-area-inset-bottom));
  }
  .readonly .rail:not(:has(.rail-foot:not([hidden]))) {
    padding-bottom: env(safe-area-inset-bottom);
  }
  .rail-head {
    padding: var(--s-4);
  }
  .rail-scroll {
    padding: var(--s-4) var(--s-4) var(--s-5);
  }
  .rail-section {
    margin-top: var(--s-5);
    padding-top: var(--s-4);
  }
  .link-row {
    min-height: 44px;
  }

  /* ---- Quản trị ---- */
  .toolbar .search {
    flex-basis: 100%;
  }
  .toolbar .btn-primary {
    flex: 1 1 100%;
  }
  .chip-stack:empty {
    display: none;
  }
}

/* Màn cảm ứng: trạng thái hover bị "dính" sau khi chạm, trông như đã chọn. */
@media (hover: none) {
  /* Mở hộp thoại thì trình duyệt tự đặt con trỏ vào nút đóng; trên cảm ứng viền đó chỉ gây rối mắt. */
  dialog [data-close]:focus-visible {
    outline: none;
  }
  .role-pick:hover:not([aria-pressed="true"]) {
    border-color: var(--line);
    color: var(--ink-2);
  }
  .btn-secondary:hover:not(:disabled) {
    border-color: var(--line-strong);
    background: var(--surface);
  }
  .type-option:hover,
  .co-option:hover {
    border-color: var(--line-strong);
  }
  .type-option:has(input:checked),
  .co-option:has(input:checked) {
    border-color: var(--blue);
  }
  .co-option:has(input:checked) {
    border-color: var(--co);
  }
}

@keyframes sheet-in {
  from {
    transform: translateY(24px);
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
