/* Light business workspace. All tokens are screen-only; paper keeps its layout. */
@media screen {
  /* Explicit missing-data state; normal zero and workflow states do not opt in. */
  .status-uncertain { color: var(--danger) !important; }
  :root {
    --surface-neutral: #fff;
    --bg: #f5f6f7;
    --surface-subtle: #f5f6f7;
    --panel: #fff;
    --surface-input: #fff;
    --surface-navigation: #f5f6f7;
    --line: #dfe3e6;
    --line-strong: #b8c2bc;
    --text: #26332d;
    --muted: #67736d;
    --accent: #167548;
    --accent-dark: #105b37;
    --accent-soft: #edf7f0;
    --row-hover: #f5f9f6;
    --table-heading: #4d5953;
    --radius: 6px;
    --control-height: 28px;
    --control-height-sm: 28px;
    --control-height-mobile: 44px;
    --ui-control-current-height: 28px;
    --ui-detail-control-height: 28px;
    --ui-table-padding-y: 6px;
    --ui-table-padding-x: 10px;
    --compact-gap: 8px;
    --compact-panel-padding: 12px;
  }

  /* Neutral surfaces share the palette; warnings and business states keep their colors. */
  .topbar { background-color: var(--panel); }
  :is(.authenticated, .guest) :is(dialog, [role="dialog"]):not(:where(.dispatch-sheet, .template-preview-sheet)) {
    background-color: var(--panel);
    color: var(--text);
  }
  :is(.authenticated, .guest) :is(.app-main, .guest-main, dialog, [role="dialog"], .topbar) :is(input, select, textarea):not(:where(
    [type="hidden"], [type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="image"],
    [type="button"], [type="submit"], [type="reset"], .select-combobox__source,
    .dispatch-sheet *, .template-preview-sheet *, .contract-body-editor *, [contenteditable] *
  )) { background-color: var(--surface-input); }
  .app-main :is(.stat, .fh-kpi, .user-card, .workflow-card, .signature-review-form,
    .cost-summary-strip, .cost-batch-row, .fulfillment-expense-row, .finance-chart-frame,
    .native-contract-head, .clause-card, .warehouse-row, .purchase-file-row, .ops-metrics,
    .demand-workbench-actions, .product-settings-actions, .production-summary,
    .workflow-node-row:not(.disabled-node), .module-statistic:not(.active)):not(:hover):not(:where(
      .dispatch-sheet *, .template-preview-sheet *, .contract-body-editor *
    )) { background-color: var(--panel); }
  .app-main .recognition-ledger article { background-color: var(--panel); }
  .app-main :is(.bom-summary, .purchase-summary, .production-summary) > div { background-color: var(--panel); }

  /* Legacy neutral surfaces opt in explicitly; semantic states and paper do not. */
  :is(.app-main, .guest-main, dialog, [role="dialog"]) :is(
    .panel, .card, .filters, .compact-form, .toolbar-form, .document-rail, .detail-nav,
    .source-builder, .source-selector, .source-choice, .rule-section, .cost-category-form,
    .recognition-ledger-head, .ledger-tabs, .workflow-stage-summary, .workflow-choice,
    .user-management-details summary, .user-role-option:not(:has(input:checked)),
    .signature-upload-form, .fulfillment-node-row, .editor-actions, .sop-message,
    .sop-dialog > .sop-heading, .sop .sop-tag, .finance-secondary-tab-list a:not(.active),
    .owner-detail-table th, .counterparty-search-list, .fulfillment-title-suggestion-list,
    .fulfillment-title-suggestion:not(.active), .row-action-menu[open] > div,
    .sales-collaborator-option:not(:has(input:checked)), .choice-row label:not(:has(input:checked)),
    .rollout-ledger, .rollout-download, .inventory-entry, .inventory-source-action,
    .ops-ledger-item, .contract-candidate-head, .delivery-summary > div,
    .inventory-preview-grid > div, .delivery-contract-details > td
  ):not(:where(
    .active, .selected, [aria-selected="true"], [aria-pressed="true"], [aria-current], .warning, .danger, .success,
    .dispatch-sheet, .dispatch-sheet *, .template-preview-sheet, .template-preview-sheet *,
    .contract-body-editor, .contract-body-editor *, [contenteditable], [contenteditable] *,
    .finance-chart-frame *, .owner-chart *, .fh-chart *
  )) { background-color: var(--surface-neutral); }
  :is(.authenticated, .guest) :is(.app-main, .guest-main, dialog, [role="dialog"]) :is(thead, thead th, tfoot th):not(:where(
    .active, .selected, [aria-selected="true"], [aria-current],
    .dispatch-sheet *, .template-preview-sheet *, .contract-body-editor *, [contenteditable] *
  )) { background-color: var(--surface-subtle); }
  .app-main :is(
    .batch-table, .sop table, .contract-material-view table, .shipment-cost-view table,
    .history-cost-page table, .fh table, .material-confirmation-table,
    .bom-ledger, .bom-import-table, .procurement-ledger, .quotation-ledger,
    .execution-ledger, .shortage-history, .shortage-detail, .workflow-ledger
  ) > tbody > tr:not(:hover):not(:where(
    [data-edited], .active, .selected, [aria-selected="true"],
    .inventory-row-error, .tv-row-blocked, .tv-row-overdue
  )) { background-color: var(--surface-neutral); }
  .app-main .contract-material-view tbody tr:nth-child(even):not(:hover),
  .app-main .shipment-cost-view tbody tr:nth-child(even):not(:hover),
  .app-main .fh tbody tr:nth-child(even):not(:hover) { background-color: var(--surface-neutral); }

  :root[data-ui-controls="compact"] {
    --control-height: 28px;
    --control-height-sm: 28px;
    --ui-control-current-height: 28px;
    --ui-detail-control-height: 28px;
  }

  /* Forms, table editors and dialogs use the same label-sized controls. */
  :is(.app-main, .guest-main, dialog, [role="dialog"], .topbar) :is(input, select, textarea, button, .button, .btn):not(:where(
    [type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="image"],
    .select-combobox__source, .dispatch-sheet *, .template-preview-sheet *, .contract-body-editor *, [contenteditable] *,
    .select-combobox__menu *, .text-combobox__menu *, [role="listbox"] *
  )) {
    font-size: var(--ui-font-small, 12px) !important;
    line-height: 1.35;
  }
  :is(.app-main, .guest-main, dialog, [role="dialog"], .topbar) :is(input, select):not(:where(
    [type="hidden"], [type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="image"],
    select[multiple], select[size]:not([size="1"]), .select-combobox__source,
    .dispatch-sheet *, .template-preview-sheet *, .contract-body-editor *, [contenteditable] *
  )) {
    box-sizing: border-box;
    height: var(--ui-control-current-height) !important;
    min-height: var(--ui-control-current-height) !important;
    padding-block: 4px !important;
  }
  :is(.app-main, .guest-main, dialog, [role="dialog"], .topbar) :is(button, .button, .btn):not(:where(
    .dispatch-sheet *, .template-preview-sheet *, .contract-body-editor *, [contenteditable] *,
    .select-combobox__menu *, .text-combobox__menu *, [role="listbox"] *, .contract-step-rail *, .nav-open
  )) {
    box-sizing: border-box;
    min-height: var(--ui-control-current-height) !important;
    height: auto !important;
    padding-block: 4px !important;
  }
  .app-main :is(.page-head, .page-header, .section-head, .cr-heading, .production-actions, .dispatch-actions,
    .ledger-actions, .trace-actions, .quotation-head-actions, .shortage-actions, .task-row-actions, .fh-actions, .dn-toolbar)
    > a:not(:where(.button, .btn, .dispatch-sheet *, .template-preview-sheet *)) {
    font-size: var(--ui-font-small, 12px);
    min-height: var(--ui-control-current-height);
    padding-block: 4px;
  }
  .topbar .ledger-switcher label { font-size: var(--ui-font-small, 12px); }

  .topbar { gap: var(--compact-gap); }
  .topbar .user, .topbar .ledger-switcher { gap: var(--compact-gap); }
  .topbar .ledger-switcher { gap: 10px; }
  .sidebar { background: var(--surface-navigation); }
  .sidebar .nav-link.active { background: var(--accent-soft); color: var(--accent); box-shadow: inset 3px 0 var(--accent); }

  .app-main, .guest-main { margin-top: 12px; }
  .app-main { width: calc(100% - 32px); }
  .app-main :is(h1, h2, h3) { color: var(--text); }
  .app-main :is(.page-head, .page-header, .page-heading) { align-items: center; flex-wrap: wrap; }
  .app-main :is(.page-head, .page-header, .page-heading) p { margin: 4px 0 0; }
  .app-main :is(.filters, .toolbar, .toolbar-form) { justify-content: flex-start; }
  .app-main :is(.filters, .toolbar, .toolbar-form) > .ui-field { flex-grow: 0; }
  .ui-form-section > legend { color: var(--accent); font-weight: 600; }
  .login-panel { padding: 24px; margin: 64px auto; }
  dialog:not(.ui-preferences) { max-width: calc(100vw - 24px); max-height: calc(100dvh - 32px); overflow: auto; }
  :is(.app-main, .guest-main, dialog) :is(input, select, textarea, button, summary, [tabindex]) { scroll-margin-block: 72px; }

  /* Native disclosures keep keyboard behavior and expose their state as text buttons. */
  :is(.app-main, .guest-main, dialog, [role="dialog"]) details > summary {
    list-style: none;
    cursor: pointer;
  }
  :is(.app-main, .guest-main, dialog, [role="dialog"]) details > summary::marker { content: ""; }
  :is(.app-main, .guest-main, dialog, [role="dialog"]) details > summary::-webkit-details-marker { display: none; }
  :is(.app-main, .guest-main, dialog, [role="dialog"]) details > summary:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 3px;
  }
  /* Action menus and existing button summaries keep their own labels. */
  :is(.app-main, .guest-main, dialog, [role="dialog"]) details:not(:where(
    .row-action-menu, .ui-more-actions, .ledger-void-action
  )) > summary:not(:where(.button, .btn))::before {
    content: "展开";
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    height: var(--ui-control-current-height);
    padding-inline: 12px;
    flex-shrink: 0;
    margin-inline-end: 8px;
    vertical-align: middle;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius);
    background: var(--panel);
    color: var(--text);
    font-size: var(--ui-font-small, 12px);
    font-weight: 600;
    line-height: 1.35;
    white-space: nowrap;
  }
  :is(.app-main, .guest-main, dialog, [role="dialog"]) details[open]:not(:where(
    .row-action-menu, .ui-more-actions, .ledger-void-action
  )) > summary:not(:where(.button, .btn))::before {
    content: "关闭";
  }
  .select-combobox__menu, .text-combobox__menu { min-width: min(280px, calc(100vw - 32px)); max-width: calc(100vw - 32px); }
  .workflow-node-row.ui-field-row { align-items: center; }
  .workflow-node-row.ui-field-row > strong { grid-row: auto; padding-top: 0; }
  .workflow-node-row.ui-field-row > :is(.workflow-node-enabled, .workflow-node-order) { grid-row: auto; grid-column: auto; }
  .cost-batch-editor { min-width: 0; grid-template-columns: minmax(0, 1fr); }
  .cost-batch-rows { min-width: 0; }
  .cost-batch-labels { display: none; }
  .cost-batch-row .ui-field-caption { display: inline; margin: 0; }
  .cost-batch-row > strong { padding: 0; text-align: left; }
  .cost-batch-row > button { justify-self: start; }
  .aftersales-billing-form { display: grid; grid-template-columns: minmax(0, 1fr); }
  .workflow-tracker-filters { flex: 1 0 100%; width: 100%; }
  .app-main .production-candidate-search { display: flex !important; flex-wrap: wrap; align-items: center; }
  .app-main [data-batch-search] { --field-search: 260px; }
  /* A toolbar groups related actions; its nested controls are not grid columns. */
  :is(.app-main, dialog) .batch-toolbar.ui-field-row:not([hidden]) {
    display: flex !important;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px !important;
  }
  .app-main .batch-toolbar > .batch-bulk-toolbar:not([hidden]) {
    flex: 0 1 auto;
    width: max-content;
    align-items: center;
  }
  .app-main .signature-review-form label.ui-field { flex-wrap: nowrap; }
  .app-main .signature-review-form label.ui-field > .ui-sized-control { flex-shrink: 1; }
  .app-main .signature-review-form > .signature-review-actions { grid-column: 1 / -1 !important; align-items: center; }
  .app-main label[data-aftersales-reserve-serial]:not([hidden]) {
    display: flex; flex-direction: row; align-items: center; flex-wrap: wrap;
    gap: 10px; width: max-content; max-width: 100%;
  }
  .app-main :is(.page-head, .page-header, .page-heading, .section-head, .section-heading, .cr-heading, .finance-section-head, .owner-heading, .fh-heading) {
    gap: var(--compact-gap);
    margin-bottom: var(--compact-gap);
  }
  .app-main :is(.owner-heading, .fh-heading) h1 { font-size: var(--ui-font-title); }
  /* Page actions follow the title; section and dialog headings keep their layout. */
  .app-main :is(
    .page-head:has(h1), .page-header:has(h1), .page-heading:has(h1),
    .owner-page > .owner-heading, .fh > .fh-heading, :where(#sales-sop) > .sop-heading,
    .trace-head:has(h1), .cr-heading:has(> h1), .section-head:has(h1)
  ):not(:where(dialog *, [role="dialog"] *, .dispatch-sheet *, .template-preview-sheet *)) {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: 12px 16px;
  }
  .app-main :is(
    .page-head:has(h1), .page-header:has(h1), .page-heading:has(h1),
    .owner-page > .owner-heading, .fh > .fh-heading, :where(#sales-sop) > .sop-heading,
    .trace-head:has(h1), .cr-heading:has(> h1), .section-head:has(h1)
  ):not(:where(dialog *, [role="dialog"] *, .dispatch-sheet *, .template-preview-sheet *)) > * {
    flex: 0 1 auto;
    width: auto;
    min-width: 0;
    max-width: 100%;
    margin-inline-start: 0;
    overflow-wrap: anywhere;
  }
  .app-main :is(
    .page-head:has(h1), .page-header:has(h1), .page-heading:has(h1),
    .owner-page > .owner-heading, .fh > .fh-heading, :where(#sales-sop) > .sop-heading,
    .trace-head:has(h1), .cr-heading:has(> h1), .section-head:has(h1)
  ):not(:where(dialog *, [role="dialog"] *, .dispatch-sheet *, .template-preview-sheet *))
    > :not(:first-child):has(> :is(a, button)) {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: 12px;
  }
  .app-main :is(.panel, .card, .ui-form-section):not(:where(.ui-ledger-card, .dispatch-sheet *, .template-preview-sheet *, [data-detail-layout="off"] *)) {
    padding: var(--compact-panel-padding);
    margin-bottom: var(--compact-gap);
    box-shadow: none;
  }
  .app-main :is(.card-header, .card-footer) { padding: 10px 12px; gap: var(--compact-gap); }
  .app-main .card-body { padding: var(--compact-panel-padding); }
  .app-main .stat { padding: 10px 12px; }
  .app-main a.stat:hover { box-shadow: none; transform: none; }

  /* Alert summaries use light surfaces except when a selected item is hovered. */
  .app-main .alert-summary .alert-stat:not(:hover),
  .app-main .alert-summary .alert-stat:not(.active):hover { color: var(--text); }
  .app-main .alert-summary .alert-stat :is(strong, span) { color: inherit; }

  .app-main :is(.form, .form-grid, .ui-form-grid, .grid, .detail-grid, .toolbar-form, .rule-fields,
    .filters, [class*="-filters"], [class*="-form"], .toolbar, .actions, .inline, .inline-form,
    .btn-list, .batch-toolbar, .batch-selection-line, .batch-bulk-toolbar, .batch-fields,
    .finance-source-grid, .finance-figures, .finance-totals, .owner-grid, .owner-numbers,
    .owner-toolbar, .owner-reminders, .owner-reconcile, .owner-detail-summary, .owner-topic-stats,
    .fh-filter, .fh-kpis, .fh-grid, .bookkeeping-charts, .bookkeeping-totals,
    .quotation-head-actions, .production-actions, .dispatch-actions, .trace-actions,
    .ledger-actions, .shortage-actions, .task-row-actions, .fh-actions) {
    gap: var(--compact-gap);
  }
  .app-main form.form.wide, .app-main .customer-editor, .app-main .material-editor form.form.wide[data-material-editor] { gap: var(--compact-gap); }
  .app-main :is(.filters, .compact-form, .active-filter):not(:where(.dispatch-sheet *, .template-preview-sheet *)) {
    padding: 8px;
    margin-bottom: var(--compact-gap);
  }
  .app-main .inventory-search { padding-block: 8px; }
  .app-main :is(.inventory-balances, .inventory-documents) { padding-block: 12px; }
  .app-main :is(.detail, .signature-upload-form, .signature-review-form) { gap: var(--compact-gap); }
  .app-main :is(.actions, .inline-form, .toolbar, .section-head) { flex-wrap: wrap; }
  .app-main :is(.table-scroll, .table-wrap, .ui-input-scroll, .ui-detail-scroll, .table-responsive) { max-width: 100%; min-width: 0; }

  :is(.app-main, .guest-main, dialog) :is(input, select, textarea):focus-visible:not(:where(.dispatch-sheet *, .template-preview-sheet *, .contract-body-editor *)) {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
  }
  :is(.app-main, .guest-main, dialog) :is(button, .button, .btn):not(:where(.dispatch-sheet *, .template-preview-sheet *, .contract-body-editor *)) { border-radius: 4px; }
  /* Text actions keep their content width, including inside grids and mobile forms. */
  :is(.app-main, .guest-main, dialog, [role="dialog"]) :is(button, .button, .btn, input[type="submit"], input[type="button"], input[type="reset"]):not(:where(
    .dispatch-sheet *, .template-preview-sheet *, .contract-body-editor *, .ui-preferences *,
    .select-combobox__menu *, .text-combobox__menu *, [role="listbox"] *,
    .fulfillment-line-buttons *, .quotation-icon-button, .procurement-remove, .batch-icon,
    .owner-pagination .button, .dn-table button, .history-materials [data-remove]
  )), .ui-preferences button {
    width: fit-content !important;
    min-width: var(--ui-control-current-height, 36px) !important;
    max-width: 100%;
    flex: 0 0 auto !important;
    justify-self: start;
    padding-inline: 12px !important;
  }
  .app-main :is(.module-statistics, .bom-status-filters, .production-view-tabs, .procurement-queue, .finance-totals:has([data-installation-state])) {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-start;
    gap: 8px;
  }
  .app-main .procurement-queue { width: fit-content; max-width: 100%; border: 0; background: transparent; }
  .app-main :is(.module-statistics, .bom-status-filters, .production-view-tabs, .procurement-queue, .finance-totals:has([data-installation-state])) > a {
    display: inline-flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: flex-start;
    gap: 10px;
    width: fit-content;
    max-width: 100%;
    min-height: var(--ui-control-current-height, 36px);
    padding: 6px 12px;
    margin-bottom: 0;
  }
  .app-main :is(.procurement-queue, .finance-totals:has([data-installation-state])) > a { border: 1px solid var(--line); border-radius: 4px; }
  .app-main .finance-totals > a[data-installation-state] strong { margin-top: 0; }
  :is(.app-main, dialog) :is(button:disabled, .button[aria-disabled="true"], .btn[aria-disabled="true"]):not(:where(.dispatch-sheet *, .template-preview-sheet *)),
  [data-selection-group] :is(button:disabled, [aria-disabled="true"]) {
    cursor: not-allowed;
  }

  /* Legacy modules share the same spacing without changing business states. */
  .app-main :is(.owner-block, .owner-formula, .owner-topic-rules, .operating-maintenance section, .bookkeeping-section) { padding-block: 12px; }
  .app-main :is(.owner-toolbar, .owner-stock-strip, .owner-topic-nav, .finance-totals, .finance-sources, .finance-secondary-content) { padding-block: 8px; }
  .app-main :is(.owner-block-heading, .owner-detail-summary, .owner-pagination, .finance-workbench section, .finance-secondary-tabs, .fh-tabs, .fh-filter, .fh-kpis, .batch-section) { margin-block: 8px; }
  .app-main .fh-kpi { padding: 10px 12px; }
  .app-main .bookkeeping-charts { padding-block: 12px; }
  .app-main .finance-source-grid { margin-top: 8px; }
  .app-main :is(.finance-expense-shortcuts, .finance-expense-totals, .toolbar-actions, .owner-actions) { gap: var(--compact-gap); }
  .app-main .payroll-page .panel { margin-bottom: 8px; }
  .app-main .batch-table tbody tr[data-edited] { box-shadow: inset 2px 0 var(--accent); }
  .app-main .procurement-queue a.active { box-shadow: inset 3px 0 var(--accent); }
  .app-main .shipment-cost-view { padding-block: 12px; margin-block: 8px; }
  .app-main :is(.bom-material-confirmation, .exchange-dialog, .bookkeeping-workbench dialog, .finance-workbench dialog, .operating-maintenance dialog) {
    padding: 12px;
  }
  .app-main :is(#dn-picker, #dn-confirm, #dn-import, #cr-contract-dialog, #cr-preview-dialog, #aftersales-contact-dialog) { padding: 12px; }
}

@media screen and (min-width: 821px) {
  .app-shell { grid-template-columns: 220px minmax(0, 1fr); }
  .sidebar .nav-group-toggle, .sidebar .nav-link { white-space: nowrap; }
  .sidebar .nav-group-toggle > :is(.ui-module-icon, .nav-group-badge, i) { flex-shrink: 0; }
}

@media screen and (max-width: 820px) {
  .dispatch-actions { flex-wrap: wrap; }
  .app-main .dispatch-actions > form { flex: 1 0 100%; }
  :root, :root[data-ui-controls] {
    --control-height: 44px;
    --control-height-sm: 44px;
    --ui-control-current-height: 44px;
    --ui-detail-control-height: 44px;
  }
  .topbar { gap: 8px; min-height: 56px; padding-block: 5px; }
  .app-shell { min-height: calc(100dvh - var(--app-header-height, 56px)); }
  .sidebar { top: var(--app-header-height, 56px); }
  .nav-scrim { top: var(--app-header-height, 56px); }
  .topbar .nav-open { width: 44px; min-height: 44px; }
  .sidebar .nav-link, .sidebar .nav-group-toggle { min-height: 44px; }
  .app-main, .guest-main { margin-top: 8px; }
  .app-main :is(.owner-filter input, .owner-filter button, .owner-period a, .fh-filter input, .fh-filter select, .batch-icon, .batch-selection-line .button) { min-height: 44px; height: auto; }
  .app-main .owner-filter input { height: 44px; }
  .app-main .batch-table .batch-icon { min-width: 44px; width: 44px; }
  .app-main :is(.owner-pagination .button, .finance-workbench .button) { min-height: 44px; height: auto; }
}
