:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --border: #e2e5ea;
  --text: #1c1f26;
  --text-muted: #6b7280;
  --accent: #2f6fed;
  --accent-hover: #2558c4;
  --row-hover: #f0f4ff;
  --dep: #ea580c;
  --dep-soft: rgba(234, 88, 12, 0.10);
  --dot-ro: #9aa0ab;
  --dot-dd: #7c3aed;
  --rule-true: #15803d;
  --rule-false: #b91c1c;
  --decision: #a16207;
  --condition: #004E9A;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1c1f26;
    --surface-2: #262a33;
    --border: #2b2f38;
    --text: #e8eaed;
    --text-muted: #9aa0a8;
    --accent: #5b8dfd;
    --accent-hover: #7fa5ff;
    --row-hover: #22283a;
    --dep: #fb923c;
    --dep-soft: rgba(251, 146, 60, 0.14);
    --dot-ro: #6b7280;
    --dot-dd: #a78bfa;
    --rule-true: #4ade80;
    --rule-false: #f87171;
    --decision: #eab308;
    --condition: #4fa3e3;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

header {
  padding: 24px 32px 8px;
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.app-logo {
  display: block;
  height: 82px;
  width: auto;
}

.header-row-right {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-shrink: 0;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  color: var(--text-muted);
}

.user-info .btn-secondary {
  padding: 5px 12px;
}

.lang-switch {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.auth-wrap {
  position: relative;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 24px;
}

.auth-wrap[hidden] {
  display: none;
}

.auth-lang-switch {
  position: absolute;
  top: 24px;
  right: 32px;
}

.auth-card {
  width: 100%;
  max-width: 380px;
}

.auth-logo {
  display: block;
  max-width: 330px;
  width: 100%;
  height: auto;
  margin: 0 auto 16px;
}

.auth-title {
  margin: 0 0 16px;
  font-size: 22px;
  text-align: center;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.auth-form[hidden] {
  display: none;
}

.auth-form-actions {
  justify-content: stretch;
}

.auth-form-actions .btn-primary {
  width: 100%;
}

.auth-toggle {
  margin-top: 16px;
  text-align: center;
}

.auth-link {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 13px;
  cursor: pointer;
  padding: 4px;
}

.auth-link:hover {
  text-decoration: underline;
}

.auth-link:disabled {
  color: var(--text-muted);
  cursor: not-allowed;
}

.auth-link:disabled:hover {
  text-decoration: none;
}

.auth-early-access {
  margin: 4px 0 0;
  font-size: 13px;
  color: var(--text-muted);
}

.auth-early-access a {
  color: var(--accent);
}

.auth-notice {
  background: rgba(21, 128, 61, 0.10);
  color: var(--rule-true);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 13px;
  margin-bottom: 14px;
}

.auth-notice.error {
  background: rgba(229, 72, 77, 0.12);
  color: #e5484d;
}

.lang-btn {
  padding: 4px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  line-height: 1.6;
}

.lang-btn:hover {
  color: var(--text);
}

.lang-btn.active {
  border-color: var(--accent);
  color: var(--accent);
}

.tabs {
  display: flex;
  gap: 4px;
  padding: 0 32px;
  border-bottom: 1px solid var(--border);
}

.tab-btn {
  padding: 10px 18px;
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 14px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
}

.tab-btn:hover {
  color: var(--text);
}

.tab-btn.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

main {
  padding: 20px 32px 40px;
}

.tab-panel {
  display: none;
}

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

.filter-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
  font-size: 13px;
  color: var(--text-muted);
}

.clear-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  font-size: 12px;
}

.clear-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.pd-extra-filters {
  margin-left: auto;
  display: flex;
  gap: 8px;
}

.multiselect {
  position: relative;
}

.multiselect-btn {
  font-family: inherit;
  font-size: 13px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.multiselect-btn:hover {
  border-color: var(--accent);
}

.multiselect-btn.active {
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}

.multiselect-panel {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  width: 260px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  z-index: 20;
  padding: 8px;
}

.multiselect-search {
  width: 100%;
  font-family: inherit;
  font-size: 13px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  margin-bottom: 6px;
}

.multiselect-search:focus {
  outline: none;
  border-color: var(--accent);
}

.multiselect-actions {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
}

.multiselect-actions button {
  border: none;
  background: none;
  color: var(--accent);
  font-size: 12px;
  cursor: pointer;
  padding: 2px 4px;
}

.multiselect-actions button:hover {
  text-decoration: underline;
}

.multiselect-options {
  max-height: 220px;
  overflow-y: auto;
}

.multiselect-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 4px;
  font-size: 13px;
  color: var(--text);
  border-radius: 4px;
  cursor: pointer;
}

.multiselect-option:hover {
  background: var(--row-hover);
}

.multiselect-option input {
  margin: 0;
}

.multiselect-empty {
  padding: 8px 4px;
  font-size: 12px;
  color: var(--text-muted);
}

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: auto;
  max-height: 70vh;
}

thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface);
}

thead th.sticky-col,
thead th.sticky-col-2,
thead th.sticky-col-3,
thead th.sticky-col-4 {
  z-index: 3;
}

#table-workflowdata th {
  color: var(--text);
}

#table-processdetails th {
  white-space: normal;
  max-width: 130px;
  line-height: 1.25;
  vertical-align: bottom;
  padding: 6px 10px;
}

/* IF_RULE ConditionOperator/ConditionValue - the generic 130px th max-width
   above is wider than their -40% target width, so it (not the input's own
   min-width) was the actual bottleneck keeping these two columns wide. */
#table-processdetails th.col-ifrule-narrow {
  max-width: 78px;
}

/* Excel-style column resize handle - a thin drag strip on the right edge of
   each header cell. thead th already has position:sticky (see the generic
   rule above), which is enough of a positioning context for this absolutely-
   positioned child. Resizing sets inline width/min-width/max-width on that
   column's th/td (see initPdColumnResize in app.js), which wins over every
   CSS width rule above/below regardless of specificity - untouched columns
   keep behaving exactly as before. */
#table-processdetails .col-resize-handle {
  position: absolute;
  top: 0;
  right: -4px;
  width: 8px;
  height: 100%;
  cursor: col-resize;
  z-index: 6;
  touch-action: none;
}

#table-processdetails .col-resize-handle:hover,
#table-processdetails .col-resize-handle.resizing {
  background: var(--accent);
  opacity: 0.4;
}

th.col-validation-rules,
td.col-validation-rules {
  width: 260px;
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  background-color: #FCFBDD;
  color: #1c1f26;
}

td.col-validation-rules {
  font-size: 6.5px;
}

/* Field Type (DB) through DropDown Values - fixed fill regardless of theme,
   so text color is pinned dark here too (var(--text) would go light in dark
   mode and disappear against this light background). */
.col-fieldtype-group {
  background-color: #CCEEFC;
  color: #1c1f26;
}

.col-fieldtype-group .cell-input {
  color: #1c1f26;
}

/* Next Process - same fixed-fill/pinned-text-color treatment as
   .col-fieldtype-group, own color per the requested column. */
.col-nextprocess {
  background-color: #E2ECDC;
  color: #1c1f26;
}

.col-nextprocess .cell-input {
  color: #1c1f26;
}

/* IF_Rule / IF_RULE_TRUE / IF_RULE_FALSE - same fixed-fill/pinned-text-color
   treatment as .col-fieldtype-group, own color per the requested column. */
.col-ifrule-group {
  background-color: #DDB6AB;
  color: #1c1f26;
}

.col-ifrule-group .cell-input {
  color: #1c1f26;
}

/* When Data Then Reset/Compute, After Data Then Snapshot - same fixed-fill/
   pinned-text-color treatment as .col-fieldtype-group, own color per the
   requested column. */
.col-datathen-group {
  background-color: #D2D4B4;
  color: #1c1f26;
}

.col-datathen-group .cell-input {
  color: #1c1f26;
}

/* Formula (readonly case) through the end of the grid (all Formula-,
   Condition- and Else-prefixed columns) - same fixed-fill/pinned-text-color
   treatment as .col-fieldtype-group, own color per the requested column
   group. */
.col-formularesult-group {
  background-color: #E7DEDD;
  color: #1c1f26;
}

.col-formularesult-group .cell-input {
  color: #1c1f26;
}

/* FormulaConditionSourceProcessDetails/FormulaConditionOperator/
   FormulaConditionValue - same background as .col-formularesult-group, but
   with the requested blue font color instead of the group's default. */
.col-formulacondition-blue {
  color: #4120E4;
}

.col-formulacondition-blue .cell-input {
  color: #4120E4;
}

/* FormulaValue through FormulaPattern - same background as
   .col-formularesult-group, but with the requested green font color instead
   of the group's default. */
.col-formulavalue-green {
  color: #3DC327;
}

.col-formulavalue-green .cell-input {
  color: #3DC327;
}

/* FormulaElseValue through the end of the grid - same background as
   .col-formularesult-group, but with the requested dark-red font color
   instead of the group's default. */
.col-formulaelse-darkred {
  color: #BE3A2C;
}

.col-formulaelse-darkred .cell-input {
  color: #BE3A2C;
}

#table-workflowdata td.readonly-col {
  color: var(--text-muted);
}

#table-workflowdata th.active-col {
  background: #e5484d;
  color: #fff;
}

#table-workflowdata td.stage-complete {
  color: var(--rule-true);
  font-weight: 600;
}

td.cell-td {
  padding: 0;
}

.cell-input {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  font-family: inherit;
  font-size: 13px;
  padding: 10px 14px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
}

.cell-input:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--row-hover);
}

.cell-input.cell-error {
  border-color: #e5484d;
  background: var(--dep-soft);
}

.cell-input[type="checkbox"] {
  width: auto;
  height: auto;
  margin: 0 14px;
}

#table-workflowdata .cell-input:disabled {
  color: var(--text-muted);
  background: transparent;
  border-color: transparent;
  cursor: default;
}

#table-processdetails .cell-input:disabled {
  color: var(--text-muted);
  background: transparent;
  border-color: transparent;
  cursor: not-allowed;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th, td {
  text-align: left;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

th {
  color: var(--text-muted);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

tbody tr:hover {
  background: var(--row-hover);
}

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

.link-btn {
  border: none;
  background: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 13px;
  padding: 0;
}

.link-btn:hover {
  color: var(--accent-hover);
  text-decoration: underline;
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}

.badge.yes {
  background: rgba(47, 111, 237, 0.12);
  color: var(--accent);
}

.badge.no {
  background: rgba(107, 114, 128, 0.12);
  color: var(--text-muted);
}

.empty-row td {
  color: var(--text-muted);
  text-align: center;
  padding: 24px;
}

.sticky-col,
.sticky-col-2,
.sticky-col-3,
.sticky-col-4 {
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--surface);
}

.sticky-col-4 {
  box-shadow: 1px 0 0 var(--border);
}

#table-processdetails select.cell-input {
  min-width: 260px;
}

#table-processdetails input.cell-input[data-field="InternalFieldName"],
#table-processdetails input.cell-input[data-field="ClientFieldName"] {
  min-width: 170px;
}

#table-processdetails input.cell-input[data-field="IF_ReadOnly_Formula"] {
  min-width: 420px;
}

/* IF_RULE column group (6 columns: description, condition source/operator/value,
   TRUE/FALSE next process) - width narrowed 40% and data font-size shrunk 30%
   from their previous defaults, per request. */
#table-processdetails input.cell-input[data-field="IF_Rule"] {
  min-width: 252px;
}

#table-processdetails select.cell-input[data-field="IF_Rule_ConditionSourceProcessDetails"],
#table-processdetails select.cell-input[data-field="IF_Rule_TRUE_NextProcessDetails"],
#table-processdetails select.cell-input[data-field="IF_Rule_FALSE_NextProcessDetails"] {
  min-width: 156px;
}

#table-processdetails select.cell-input[data-field="IF_Rule_ConditionOperator"] {
  min-width: 78px;
}

/* Operator Conditie (FormulaConditionOperator) - now a short dropdown
   (=, <, >, <=, >=, <>) rather than free text, so it doesn't need the
   generic select.cell-input 260px min-width above. */
#table-processdetails select.cell-input[data-field="FormulaConditionOperator"] {
  min-width: 10ch;
}

/* Operator (THEN)/(ELSE) (+/-) and Unitate Offset (THEN)/(ELSE) (day/number)
   - same reasoning: short dropdowns that don't need the generic 260px. */
#table-processdetails select.cell-input[data-field="FormulaOperator"],
#table-processdetails select.cell-input[data-field="FormulaElseOperator"] {
  min-width: 6ch;
}

#table-processdetails select.cell-input[data-field="FormulaOffsetUnit"],
#table-processdetails select.cell-input[data-field="FormulaElseOffsetUnit"] {
  min-width: 10ch;
}

#table-processdetails input.cell-input[data-field="IF_Rule_ConditionValue"] {
  min-width: 76px;
}

#table-processdetails .cell-input[data-field="IF_Rule"],
#table-processdetails .cell-input[data-field="IF_Rule_ConditionSourceProcessDetails"],
#table-processdetails .cell-input[data-field="IF_Rule_ConditionOperator"],
#table-processdetails .cell-input[data-field="IF_Rule_ConditionValue"],
#table-processdetails .cell-input[data-field="IF_Rule_TRUE_NextProcessDetails"],
#table-processdetails .cell-input[data-field="IF_Rule_FALSE_NextProcessDetails"] {
  font-size: 9.1px;
}

#table-processdetails input.cell-input[data-field="When_Data_Then_Reset"] {
  min-width: max(11ch, 100px);
}

#table-processdetails input.cell-input[data-field="When_Data_Then_Compute"] {
  min-width: max(11ch, 150px);
}

#table-processdetails input.cell-input[data-field="After_Data_Then_Snapshot"] {
  min-width: max(11ch, 160px);
}

tbody tr:hover .sticky-col,
tbody tr:hover .sticky-col-2,
tbody tr:hover .sticky-col-3,
tbody tr:hover .sticky-col-4 {
  background: var(--row-hover);
}

.toolbar {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-bottom: 12px;
}

.add-btn {
  border: none;
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.add-btn:hover {
  background: var(--accent-hover);
}

.add-btn.secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.add-btn:disabled {
  background: var(--surface-2);
  color: var(--text-muted);
  cursor: not-allowed;
}

.client-select-input {
  margin-right: auto;
  font-family: inherit;
  font-size: 13px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
}

.client-select-input:focus {
  outline: none;
  border-color: var(--accent);
}

.add-btn.secondary:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--surface);
}

.icon-btn {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 13px;
  padding: 2px 6px;
  color: var(--text-muted);
  border-radius: 4px;
}

.icon-btn:hover {
  background: var(--row-hover);
  color: var(--text);
}

.icon-btn.danger:hover {
  color: #e5484d;
}

/* Modal */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: min(480px, 90vw);
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
}

.modal.wide {
  width: min(720px, 90vw);
}

.modal.diagram {
  width: 96vw;
  max-width: 1760px;
  max-height: 92vh;
}

.diagram-scroll {
  overflow: auto;
  max-height: 72vh;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}

.diagram-scroll.fit-mode {
  overflow: hidden;
  height: 72vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.diagram-scroll.fit-mode svg.flow {
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
}

.modal.maximized .diagram-scroll {
  max-height: calc(100vh - 220px);
}

.modal.maximized .diagram-scroll.fit-mode {
  height: calc(100vh - 220px);
}

svg.flow {
  display: block;
  height: auto;
}

.flow .lane-title { font-size: 14px; font-weight: 700; fill: var(--text); }
.flow .lane-caption { font-size: 10px; fill: var(--text-muted); }
.flow .card-rect { fill: var(--surface-2); stroke: var(--border); stroke-width: 1; }
.flow .card-rect.is-target { stroke: var(--dep); stroke-width: 1.4; }
.flow .card-decision { stroke: var(--decision); stroke-width: 1.4; }
.flow .card-decision.is-target { stroke: var(--decision); stroke-width: 1.8; }
.flow .card-name { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 10px; font-weight: 700; fill: var(--text); }
.flow .card-internal { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 8px; fill: var(--text-muted); }
.flow .order-badge { font-size: 8.5px; fill: var(--text-muted); font-variant-numeric: tabular-nums; }
.flow .dot-ro { fill: var(--dot-ro); }
.flow .dot-ed { fill: var(--accent); }
.flow .dot-dd { fill: var(--dot-dd); }
.flow .dep-line { stroke: var(--dep); stroke-width: 0.8; stroke-dasharray: 4 3; fill: none; }
.flow .dep-arrow { fill: var(--dep); }
.flow .dep-label {
  font-size: 9px; fill: var(--dep); font-weight: 600; dominant-baseline: central;
  paint-order: stroke fill; stroke: var(--surface); stroke-width: 3px; stroke-linejoin: round;
}
.flow .dep-line.rule-true, .flow .dep-line.next { stroke: var(--rule-true); }
.flow .dep-line.rule-false { stroke: var(--rule-false); }
.flow .dep-label.rule-true, .flow .dep-label.next { fill: var(--rule-true); }
.flow .dep-label.rule-false { fill: var(--rule-false); }
.flow .dep-arrow.rule-true { fill: var(--rule-true); }
.flow .dep-arrow.rule-false { fill: var(--rule-false); }
.flow .dep-line.condition { stroke: var(--condition); }
.flow .dep-label.condition { fill: var(--condition); }
.flow .dep-arrow.condition { fill: var(--condition); }
.flow .f-badge-bg { fill: var(--dep-soft); stroke: var(--dep); stroke-width: 1; }
.flow .f-badge-txt { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 8.5px; font-weight: 700; fill: var(--dep); }
.flow .empty-lane { fill: none; stroke: var(--border); stroke-width: 1.2; stroke-dasharray: 3 3; }
.flow .empty-txt { font-size: 10.5px; fill: var(--text-muted); font-style: italic; }
.flow .lane-rule { stroke: var(--border); stroke-width: 1; }

.diagram-legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 16px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.diagram-legend-print {
  margin-left: auto;
}

.diagram-legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.diagram-legend-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex: none;
}

.diagram-legend-octagon {
  width: 10px;
  height: 10px;
  flex: none;
  background: var(--surface-2);
  border: 1.4px solid var(--decision);
  clip-path: polygon(30% 0, 70% 0, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0 70%, 0 30%);
}

#import-textarea {
  width: 100%;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12px;
}

#import-results {
  max-height: 220px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
}

#import-results:empty {
  display: none;
}

.modal-diagram-logo {
  display: none;
  padding: 14px 20px 0;
}

.modal.diagram .modal-diagram-logo {
  display: block;
}

.modal-diagram-logo-img {
  display: block;
  height: 57px; /* app header's .app-logo (82px) - 30% */
  width: auto;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

/* Draggable only for the diagram window, and only while it isn't maximized
   (see initModalDrag in app.js) - the cursor hints that up front. */
.modal.diagram:not(.maximized) .modal-header {
  cursor: move;
}

.modal.diagram:not(.maximized) .modal-header-actions button {
  cursor: pointer;
}

.modal-header h2 {
  margin: 0;
  font-size: 16px;
}

.modal-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: none;
}

.modal-close {
  border: none;
  background: none;
  font-size: 20px;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
}

.modal-maximize {
  border: none;
  background: none;
  font-size: 16px;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
  display: none;
}

.modal-maximize:hover {
  color: var(--text);
}

.modal.diagram .modal-maximize {
  display: inline-block;
}

.modal-fit {
  border: none;
  background: none;
  font-size: 16px;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
  display: none;
}

.modal-fit:hover {
  color: var(--text);
}

.modal-fit.active {
  color: var(--accent);
}

.modal.diagram .modal-fit {
  display: inline-block;
}

.modal.maximized {
  width: 100vw !important;
  max-width: 100vw !important;
  height: 100vh;
  max-height: 100vh !important;
  border-radius: 0;
}

.modal-close:hover {
  color: var(--text);
}

#modal-form {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field[hidden] {
  display: none;
}

.field label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.field input[type="text"],
.field input[type="number"],
.field input[type="email"],
.field input[type="password"],
.field input[type="date"],
.field textarea,
.field select {
  font-family: inherit;
  font-size: 14px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
}

.field textarea {
  resize: vertical;
  min-height: 60px;
}

.field.checkbox {
  flex-direction: row;
  align-items: center;
  gap: 8px;
}

.field.checkbox label {
  text-transform: none;
  font-weight: 500;
  font-size: 13px;
  color: var(--text);
}

.field-hint {
  margin: 0;
  font-size: 12px;
  color: var(--text-muted);
}

.access-table-toolbar {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.access-table-toolbar .btn-secondary {
  padding: 5px 12px;
  font-size: 12px;
}

.access-table-wrap {
  max-height: 260px;
}

.access-table td, .access-table th {
  white-space: normal;
}

.access-table td:first-child, .access-table th:first-child {
  width: 32px;
  padding-right: 0;
}

.access-client-row td {
  background: var(--surface-2);
  font-weight: 700;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
}

.wf-edit-row {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 10px 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.wf-edit-row:first-child {
  padding-top: 0;
}

.wf-edit-row:last-child {
  border-bottom: none;
}

.wf-edit-row-label {
  flex: 0 0 130px;
  padding-top: 9px;
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
}

.wf-edit-row-cards {
  flex: 1 1 auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
}

.wf-card {
  flex: 0 0 172px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
}

.wf-card.wf-card-decision {
  border-color: var(--decision);
}

.wf-card.wf-card-active {
  border-color: #e5484d;
  border-width: 2px;
  padding: 7px;
  box-shadow: 0 0 0 2px rgba(229, 72, 77, 0.18);
}

.wf-card-head {
  display: flex;
  align-items: center;
  gap: 5px;
}

.wf-card-dot {
  width: 7px;
  height: 7px;
  min-width: 7px;
  border-radius: 50%;
}

.wf-card-dot.dot-ro { background: var(--dot-ro); }
.wf-card-dot.dot-ed { background: var(--accent); }
.wf-card-dot.dot-dd { background: var(--dot-dd); }

.wf-card-head label {
  flex: 1;
  font-size: 10.5px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.02em;
  line-height: 1.25;
}

.wf-card-f {
  flex: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--dep-soft);
  color: var(--dep);
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 9px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

.wf-card input,
.wf-card select {
  width: 100%;
  font-family: inherit;
  font-size: 13px;
  padding: 5px 6px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--bg);
  color: var(--text);
}

.wf-card.wf-card-readonly {
  background: var(--surface);
}

.wf-card.wf-card-readonly input,
.wf-card.wf-card-readonly select {
  color: var(--text-muted);
  background: var(--surface-2);
  cursor: default;
}

.wf-card .field-hint {
  margin: 0;
  font-size: 10px;
  line-height: 1.3;
  color: var(--text-muted);
}

.field-readonly input,
.field-readonly select,
.field-readonly textarea {
  color: var(--text-muted);
  background: var(--surface-2);
  cursor: default;
}

.rules-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.rule-row {
  position: relative;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 32px 10px 10px;
  background: var(--surface-2);
}

.rule-row-fields {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.rule-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  flex: 1 1 160px;
}

.rule-field-sm {
  flex: 0 1 90px;
}

.rule-field input,
.rule-field select {
  font-family: inherit;
  font-size: 13px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  text-transform: none;
  font-weight: 400;
}

.rule-row-remove {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 22px;
  height: 22px;
  line-height: 1;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
}

.rule-row-remove:hover {
  background: rgba(229, 72, 77, 0.12);
  color: #e5484d;
}

.rules-add-btn {
  align-self: flex-start;
  margin-top: 8px;
}

.form-error {
  background: rgba(229, 72, 77, 0.12);
  color: #e5484d;
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 13px;
}

.form-error:empty {
  display: none;
}

.admin-clone-card {
  max-width: 720px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 20px;
}

.admin-clone-title {
  margin: 0 0 4px;
  font-size: 16px;
}

.admin-clone-hint {
  margin: 0 0 16px;
  font-size: 13px;
  color: var(--text-muted);
}

.admin-clone-row {
  display: flex;
  align-items: flex-start;
  gap: 16px;
}

.admin-clone-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.admin-clone-col h3 {
  margin: 0;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.admin-clone-col select {
  width: 100%;
}

.admin-clone-arrow {
  align-self: center;
  font-size: 20px;
  color: var(--text-muted);
  padding-top: 28px;
}

.admin-clone-toolbar {
  margin-top: 16px;
}

.admin-clone-result {
  background: rgba(21, 128, 61, 0.10);
  color: var(--rule-true);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 13px;
  margin-top: 12px;
}

.admin-clone-result .admin-clone-warning {
  display: block;
  margin-top: 6px;
  color: var(--decision);
}

.admin-mode-toolbar {
  justify-content: flex-start;
  margin-bottom: 16px;
}

.admin-access-header {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: flex-end;
  margin-bottom: 16px;
}

.admin-access-header .field {
  min-width: 220px;
}

.admin-access-copy-field {
  flex: 1;
  min-width: 300px;
}

.admin-access-copy-row {
  display: flex;
  gap: 8px;
}

.admin-access-copy-row select {
  flex: 1;
}

.admin-access-empty {
  padding: 24px 4px;
}

.admin-access-body {
  display: flex;
  gap: 16px;
  align-items: stretch;
}

.admin-access-body[hidden],
.admin-access-footer[hidden] {
  display: none;
}

.admin-access-pane {
  flex: 1;
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  display: flex;
  flex-direction: column;
}

.admin-access-tree {
  flex: 1;
  overflow-y: auto;
  max-height: 55vh;
}

.access-tree-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 6px;
  border-radius: 6px;
  cursor: pointer;
}

.access-tree-row:hover {
  background: var(--row-hover);
}

.access-tree-row.active {
  background: var(--row-hover);
  outline: 1px solid var(--accent);
}

.access-tree-row input[type="checkbox"] {
  flex: none;
}

.access-tree-row .access-tree-label {
  flex: 1;
  font-size: 13px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.access-tree-row.access-tree-client .access-tree-label {
  font-weight: 700;
}

.access-tree-row.access-tree-cbu {
  padding-left: 22px;
}

.access-tree-active-tag {
  flex: none;
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--accent);
}

.admin-access-process-header {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.admin-access-processes {
  flex: 1;
  overflow-y: auto;
  max-height: 55vh;
}

.admin-access-process-hint {
  font-size: 13px;
  color: var(--text-muted);
  padding: 8px 2px;
}

.access-process-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px;
  border-radius: 6px;
  font-size: 13px;
  color: var(--text);
}

.access-process-row:hover {
  background: var(--row-hover);
}

.admin-access-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 16px;
}

.admin-access-status {
  font-size: 12px;
  color: var(--text-muted);
}

.admin-access-status.unsaved {
  color: var(--decision);
}

.admin-access-status.saved {
  color: var(--rule-true);
}

.admin-access-status.error {
  color: var(--rule-false);
}

.admin-mode-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.add-btn.danger {
  background: var(--surface);
  color: var(--rule-false);
  border: 1px solid var(--rule-false);
}

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

.add-btn.danger:disabled {
  background: var(--surface-2);
  color: var(--text-muted);
  border-color: var(--border);
}

.admin-delete-preview {
  background: var(--surface-2);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 4px;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 4px;
}

.btn-primary {
  border: none;
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  padding: 9px 18px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  border: 1px solid var(--border);
  background: none;
  color: var(--text);
  border-radius: 6px;
  padding: 9px 18px;
  font-size: 13px;
  cursor: pointer;
}

.btn-secondary:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* Process Flow diagram printing (Print button in showWorkflowDiagram, app.js)
   - print only the diagram modal itself, in full (not clipped to its
   on-screen scroll/max-height), without the app chrome behind it or the
   header/toolbar controls that don't mean anything on paper. */
@media print {
  body * {
    visibility: hidden;
  }
  #modal-overlay,
  #modal-overlay * {
    visibility: visible;
  }
  #modal-overlay {
    position: absolute;
    inset: 0;
    background: none;
  }
  .modal {
    box-shadow: none;
    border: none;
    max-height: none;
    overflow: visible;
    width: 100% !important;
  }
  .modal-header-actions,
  .diagram-legend-print,
  .form-actions {
    display: none !important;
  }
  .diagram-scroll {
    max-height: none;
    overflow: visible;
    border: none;
  }
}

@page {
  size: landscape;
  margin: 10mm;
}
