@layer components {
  /* Shared Geometric Brand Mark */
  .brand-mark {
    width: 24px;
    height: 24px;
    flex-shrink: 0;
    display: block;
  }

  .brand-mark.is-sm {
    width: 20px;
    height: 20px;
  }

  .brand-mark.is-lg {
    width: 28px;
    height: 28px;
  }

  .brand-mark.is-light .brand-mark-bg {
    fill: var(--brand-soft);
    stroke: var(--brand-border);
    stroke-width: 1;
  }

  .brand-mark.is-light .brand-mark-pane {
    fill: rgba(29, 78, 216, 0.14);
  }

  .brand-mark.is-light .brand-mark-frame,
  .brand-mark.is-light .brand-mark-lines {
    stroke: var(--brand);
  }

  .brand-mark.is-dark .brand-mark-bg {
    fill: rgba(255, 255, 255, 0.08);
    stroke: rgba(191, 219, 254, 0.28);
    stroke-width: 1;
  }

  .brand-mark.is-dark .brand-mark-pane {
    fill: rgba(96, 165, 250, 0.22);
  }

  .brand-mark.is-dark .brand-mark-frame,
  .brand-mark.is-dark .brand-mark-lines {
    stroke: #e2e8f0;
  }

  /* Page heading */
  .page-heading {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--space-4);
    margin-bottom: var(--space-5);
  }

  .page-heading-text p {
    color: var(--ink-secondary);
    font-size: var(--text-base);
    margin: var(--space-1) 0 0;
    line-height: 1.65;
  }

  .page-kicker {
    font-size: var(--text-xs);
    letter-spacing: 0.08em;
    font-weight: 600;
    color: var(--brand);
    text-transform: uppercase;
    margin-bottom: var(--space-1);
  }

  .page-heading-actions {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    flex-wrap: wrap;
    flex-shrink: 0;
  }

  /* Buttons */
  .btn-primary,
  .btn-secondary,
  .btn-ghost,
  .btn-danger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    border-radius: var(--radius-sm);
    padding: 9px 16px;
    min-height: 40px;
    font-size: var(--text-base);
    font-weight: 600;
    border: 1px solid transparent;
    transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
    white-space: nowrap;
  }

  .btn-primary {
    background: var(--brand);
    color: #ffffff;
    box-shadow: 0 1px 2px rgba(29, 78, 216, 0.18);
  }

  .btn-primary:hover:not(:disabled) {
    background: var(--brand-hover);
  }

  .btn-secondary {
    background: var(--surface);
    border-color: var(--line-strong);
    color: var(--ink);
    box-shadow: var(--shadow-sm);
  }

  .btn-secondary:hover:not(:disabled) {
    background: var(--surface-subtle);
    border-color: var(--muted-light);
  }

  .btn-ghost {
    background: transparent;
    color: var(--ink-secondary);
    padding: 8px 12px;
    min-height: 36px;
    font-size: var(--text-sm);
  }

  .btn-ghost:hover:not(:disabled) {
    background: var(--surface-hover);
    color: var(--brand);
  }

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

  .btn-danger:hover:not(:disabled) {
    background: var(--red-hover);
  }

  .btn-danger-ghost {
    background: transparent;
    border: 1px solid transparent;
    color: var(--red);
    padding: 8px 12px;
    min-height: 36px;
    border-radius: var(--radius-sm);
    font-size: var(--text-sm);
    font-weight: 600;
  }

  .btn-danger-ghost:hover:not(:disabled) {
    background: var(--red-bg);
    border-color: var(--red-border);
  }

  .btn-primary:disabled,
  .btn-secondary:disabled,
  .btn-ghost:disabled,
  .btn-danger:disabled,
  .btn-danger-ghost:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }

  .btn-wide {
    width: 100%;
  }

  .btn-sm {
    min-height: 34px;
    padding: 6px 12px;
    font-size: var(--text-xs);
  }

  .btn-icon {
    border: 0;
    background: transparent;
    color: var(--muted);
    width: 32px;
    height: 32px;
    border-radius: var(--radius-xs);
    display: inline-grid;
    place-items: center;
    font-size: 16px;
    line-height: 1;
    transition: background-color 0.15s ease, color 0.15s ease;
  }

  .btn-icon:hover:not(:disabled) {
    background: var(--surface-hover);
    color: var(--ink);
  }

  .btn-icon svg {
    width: 16px;
    height: 16px;
    display: block;
  }

  /* Cards */
  .card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
  }

  .card-pad {
    padding: var(--space-5);
  }

  .card + .card {
    margin-top: var(--space-4);
  }

  .section-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    margin-bottom: var(--space-4);
    flex-wrap: wrap;
  }

  .section-heading h2,
  .section-heading h3 {
    margin-bottom: 0;
  }

  /* Form controls & NumberField */
  .field {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--ink);
  }

  .field-label {
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--ink);
  }

  .field-hint {
    font-size: var(--text-xs);
    font-weight: 400;
    color: var(--muted);
    margin: 0;
    line-height: 1.6;
  }

  .field-error {
    font-size: var(--text-xs);
    font-weight: 500;
    color: var(--red-ink);
    margin: 0;
  }

  .input,
  .select,
  .textarea {
    width: 100%;
    min-height: 40px;
    padding: 8px 12px;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--ink);
    font-size: var(--text-base);
    font-variant-numeric: tabular-nums;
    outline: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease, background-color 0.15s ease;
  }

  .input:hover:not(:disabled):not(:focus),
  .select:hover:not(:disabled):not(:focus),
  .textarea:hover:not(:disabled):not(:focus) {
    border-color: var(--muted-light);
  }

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

  .input:focus,
  .select:focus,
  .textarea:focus {
    border-color: var(--brand);
    box-shadow: var(--focus-ring);
  }

  .input[readonly] {
    background: var(--surface-subtle);
    color: var(--muted);
  }

  .input::placeholder,
  .textarea::placeholder {
    color: var(--muted-light);
  }

  .input-invalid {
    border-color: var(--red);
    background: var(--red-bg);
  }

  .input-invalid:focus {
    border-color: var(--red);
    box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.16);
  }

  .input-unit-wrap {
    display: flex;
    align-items: center;
    gap: var(--space-2);
  }

  .input-unit-wrap .input {
    flex: 1;
    min-width: 96px;
  }

  .input-unit-label {
    font-size: var(--text-xs);
    color: var(--muted);
    white-space: nowrap;
  }

  .number-field-compact .input {
    min-height: 36px;
    padding: 6px 10px;
    min-width: 96px;
  }

  .check-label {
    display: flex;
    align-items: flex-start;
    gap: var(--space-2);
    font-size: var(--text-sm);
    line-height: 1.6;
    font-weight: 400;
    color: var(--ink-secondary);
    cursor: pointer;
  }

  .check-label input[type="checkbox"] {
    flex: 0 0 auto;
    width: 17px;
    height: 17px;
    margin-top: 3px;
    accent-color: var(--brand);
    cursor: pointer;
  }

  /* Tables */
  .table-wrap {
    overflow-x: auto;
    max-width: 100%;
  }

  .data-table {
    width: 100%;
    border-collapse: collapse;
    text-align: left;
    font-variant-numeric: tabular-nums;
  }

  .data-table th {
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--ink-secondary);
    background: var(--surface-subtle);
    padding: 11px 14px;
    border-bottom: 1px solid var(--line);
    white-space: nowrap;
  }

  .data-table td {
    padding: 12px 14px;
    border-bottom: 1px solid var(--line);
    font-size: var(--text-sm);
    vertical-align: middle;
  }

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

  .cell-secondary {
    font-size: var(--text-xs);
    color: var(--muted);
    margin-top: 4px;
  }

  /* Pills & Badges */
  .pill {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: var(--text-xs);
    font-weight: 600;
    line-height: 1.45;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    background: var(--surface-subtle);
    color: var(--ink-secondary);
    border: 1px solid var(--line);
  }

  .pill-green {
    color: var(--green-ink);
    background: var(--green-bg);
    border-color: var(--green-border);
  }

  .pill-amber {
    color: var(--amber-ink);
    background: var(--amber-bg);
    border-color: var(--amber-border);
  }

  .pill-red {
    color: var(--red-ink);
    background: var(--red-bg);
    border-color: var(--red-border);
  }

  .pill-info {
    color: var(--brand);
    background: var(--info-bg);
    border-color: var(--info-border);
  }

  .test-badge {
    display: inline-flex;
    align-items: center;
    padding: 4px 10px;
    border-radius: 999px;
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--amber-ink);
    background: var(--amber-bg);
    border: 1px solid var(--amber-border);
    white-space: nowrap;
  }

  .count-chip {
    font-size: var(--text-xs);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    color: var(--ink-secondary);
    background: var(--surface-subtle);
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 3px 10px;
  }

  /* Banners & Notices */
  .banner {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    border: 1px solid var(--info-border);
    background: var(--info-bg);
    color: var(--brand-hover);
    font-size: var(--text-sm);
    line-height: 1.65;
    margin-bottom: var(--space-4);
  }

  .banner-error {
    background: var(--red-bg);
    border-color: var(--red-border);
    color: var(--red-ink);
  }

  .banner-warning {
    background: var(--amber-bg);
    border-color: var(--amber-border);
    color: var(--amber-ink);
  }

  .banner-success {
    background: var(--green-bg);
    border-color: var(--green-border);
    color: var(--green-ink);
  }

  .banner-body {
    flex: 1;
    min-width: 0;
  }

  .banner-title {
    font-weight: 650;
    margin-bottom: 2px;
  }

  .banner-actions {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    flex-shrink: 0;
  }

  .banner a {
    text-decoration: underline;
    text-underline-offset: 2px;
    font-weight: 600;
  }

  /* Toast */
  .toast-region {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 300;
    max-width: calc(100vw - 32px);
    pointer-events: none;
  }

  .toast-card {
    pointer-events: auto;
    display: inline-flex;
    align-items: center;
    gap: var(--space-3);
    background: var(--blueprint-deep);
    color: #ffffff;
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: var(--radius-md);
    padding: 11px 16px;
    font-size: var(--text-sm);
    font-weight: 500;
    box-shadow: var(--shadow-md);
  }

  .toast-card .btn-icon {
    color: rgba(255, 255, 255, 0.72);
  }

  .toast-card .btn-icon:hover:not(:disabled) {
    color: #ffffff;
    background: rgba(255, 255, 255, 0.12);
  }

  .toast-success {
    background: #064e3b;
    border-color: rgba(167, 243, 208, 0.25);
  }

  @media (max-width: 760px) {
    .toast-region {
      bottom: calc(var(--mobile-nav-h) + 14px);
    }
  }

  /* Light Clean Sidebar (Linear / Feishu Architectural Minimalism) */
  .sidebar {
    width: var(--sidebar-w);
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    background: var(--surface);
    border-right: 1px solid var(--line);
    color: var(--ink);
    padding: var(--space-5) var(--space-3) var(--space-4);
    display: flex;
    flex-direction: column;
    z-index: 30;
    overflow-y: auto;
  }

  .sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 18px;
    font-weight: 700;
    letter-spacing: 0.04em;
    padding: 2px var(--space-2);
    color: var(--ink);
    border-radius: var(--radius-sm);
  }

  .sidebar-caption {
    font-size: var(--text-xs);
    color: var(--muted);
    margin: 4px 0 var(--space-5) var(--space-2);
    line-height: 1.4;
  }

  .sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 3px;
  }

  .nav-item {
    position: relative;
    width: 100%;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--ink-secondary);
    padding: 9px 12px;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: var(--text-base);
    font-weight: 500;
    text-align: left;
    transition: background-color 0.15s ease, color 0.15s ease;
  }

  .nav-item:hover {
    color: var(--ink);
    background: var(--surface-hover);
  }

  .nav-item.active {
    color: var(--brand);
    background: var(--brand-soft);
    font-weight: 600;
  }

  .nav-item.active::before {
    content: "";
    position: absolute;
    left: 0;
    top: 7px;
    bottom: 7px;
    width: 3px;
    border-radius: 0 3px 3px 0;
    background: var(--brand);
  }

  .nav-icon {
    width: 18px;
    height: 18px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    color: inherit;
  }

  .nav-icon svg {
    width: 18px;
    height: 18px;
    display: block;
  }

  .nav-label {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .nav-label-short {
    display: none;
  }

  .nav-badge {
    background: var(--amber);
    color: #ffffff;
    font-size: var(--text-xs);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    padding: 1px 7px;
    border-radius: 999px;
    line-height: 1.4;
  }

  .sidebar-bottom {
    margin-top: auto;
    padding-top: var(--space-4);
  }

  .runtime-status {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: var(--text-xs);
    font-variant-numeric: tabular-nums;
    color: var(--muted);
    padding: 6px var(--space-2) var(--space-3);
  }

  .runtime-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--green);
    flex-shrink: 0;
  }

  .user-block {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: var(--space-3) var(--space-1) 0;
    border-top: 1px solid var(--line);
  }

  .user-avatar {
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    background: var(--brand-soft);
    border: 1px solid var(--brand-border);
    color: var(--brand);
    display: grid;
    place-items: center;
    font-size: var(--text-sm);
    font-weight: 700;
    flex-shrink: 0;
  }

  .user-meta {
    flex: 1;
    min-width: 0;
  }

  .user-meta strong {
    display: block;
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--ink);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .user-meta small {
    display: block;
    font-size: var(--text-xs);
    font-variant-numeric: tabular-nums;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .user-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
  }

  @media (max-width: 760px) {
    .sidebar {
      top: auto;
      right: 0;
      bottom: 0;
      left: 0;
      width: 100%;
      height: calc(var(--mobile-nav-h) + env(safe-area-inset-bottom, 0px));
      padding: 4px 6px env(safe-area-inset-bottom, 0px);
      flex-direction: row;
      align-items: stretch;
      border-right: 0;
      border-top: 1px solid var(--line);
      box-shadow: 0 -2px 10px rgba(16, 24, 40, 0.05);
      overflow-x: auto;
      overflow-y: hidden;
    }

    .sidebar-brand,
    .sidebar-caption,
    .sidebar-bottom {
      display: none;
    }

    .sidebar-nav {
      flex: 1;
      flex-direction: row;
      align-items: stretch;
      justify-content: space-around;
      gap: 2px;
      min-width: 0;
    }

    .nav-item {
      flex: 1 0 auto;
      min-width: 56px;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      gap: 3px;
      padding: 6px 4px;
      font-size: var(--text-xs);
      position: relative;
      text-align: center;
    }

    .nav-item.active::before {
      left: 24%;
      right: 24%;
      top: 0;
      bottom: auto;
      width: auto;
      height: 2px;
      border-radius: 0 0 2px 2px;
    }

    .nav-label-full {
      display: none;
    }

    .nav-label-short {
      display: block;
      font-size: var(--text-xs);
      line-height: 1.2;
      flex: 0 0 auto;
    }

    .nav-badge {
      position: absolute;
      top: 4px;
      right: 6px;
      padding: 0 5px;
    }
  }

  /* StepBar */
  .step-bar {
    list-style: none;
    margin: 0 0 var(--space-5);
    padding: var(--space-4) var(--space-5);
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    display: flex;
    align-items: center;
    gap: var(--space-4);
  }

  .step-item {
    flex: 1;
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-width: 0;
    font-size: var(--text-sm);
    color: var(--muted);
  }

  .step-item:not(:last-child)::after {
    content: "";
    flex: 1;
    height: 1px;
    background: var(--line);
    margin-left: var(--space-2);
  }

  .step-num {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    font-size: var(--text-xs);
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    background: var(--surface-subtle);
    border: 1px solid var(--line-strong);
    color: var(--muted);
    flex-shrink: 0;
  }

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

  .step-title {
    font-weight: 600;
    color: var(--ink);
  }

  .step-desc {
    font-size: var(--text-xs);
    color: var(--muted);
  }

  .step-done .step-num {
    background: var(--green-bg);
    border-color: var(--green-border);
    color: var(--green-ink);
  }

  .step-current .step-num {
    background: var(--brand-soft);
    border-color: var(--brand-border);
    color: var(--brand);
  }

  .step-warning .step-num {
    background: var(--red-bg);
    border-color: var(--red-border);
    color: var(--red-ink);
  }

  .step-warning .step-desc {
    color: var(--red-ink);
    font-weight: 600;
  }

  .step-action-link {
    border: 0;
    background: transparent;
    padding: 0;
    text-align: left;
    display: inline-flex;
    align-items: center;
    gap: var(--space-3);
    color: inherit;
  }

  .step-action-link:hover .step-title {
    color: var(--brand);
    text-decoration: underline;
    text-underline-offset: 2px;
  }

  @media (max-width: 760px) {
    .step-bar {
      flex-direction: column;
      align-items: stretch;
      gap: var(--space-3);
      padding: var(--space-4);
    }

    .step-item:not(:last-child)::after {
      display: none;
    }
  }

  /* Dialog */
  .cs-dialog {
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    padding: 0;
    width: min(480px, calc(100vw - 24px));
    max-height: min(88vh, 840px);
    box-shadow: var(--shadow-lg);
    background: var(--surface);
    color: var(--ink);
  }

  .cs-dialog-lg {
    width: min(680px, calc(100vw - 24px));
  }

  .cs-dialog::backdrop {
    background: rgba(15, 23, 42, 0.45);
    backdrop-filter: blur(3px);
  }

  .cs-dialog-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-5) var(--space-6) var(--space-4);
    border-bottom: 1px solid var(--line);
  }

  .cs-dialog-head h2 {
    font-size: var(--text-lg);
    margin: 0;
  }

  .cs-dialog-subtitle {
    font-size: var(--text-xs);
    color: var(--muted);
    margin: 4px 0 0;
  }

  .cs-dialog-body {
    padding: var(--space-5) var(--space-6) var(--space-6);
    overflow-y: auto;
    max-height: calc(88vh - 84px);
  }

  .confirm-actions {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-3);
    margin-top: var(--space-5);
    flex-wrap: wrap;
  }

  .confirm-list {
    margin: var(--space-3) 0 0;
    padding-left: 20px;
    max-height: 220px;
    overflow-y: auto;
    font-size: var(--text-sm);
    line-height: 1.75;
    color: var(--ink);
  }

  /* Progress element */
  .progress-track {
    width: 100%;
    height: 8px;
    border-radius: 999px;
    border: 0;
    background: #e5e7eb;
    overflow: hidden;
    display: block;
    appearance: none;
  }

  .progress-track::-webkit-progress-bar {
    background: #e5e7eb;
    border-radius: 999px;
  }

  .progress-track::-webkit-progress-value {
    background: var(--brand);
    border-radius: 999px;
    transition: width 0.2s ease;
  }

  .progress-track::-moz-progress-bar {
    background: var(--brand);
    border-radius: 999px;
  }

  /* Empty state */
  .empty-state {
    text-align: center;
    padding: var(--space-8) var(--space-4);
    color: var(--muted);
    font-size: var(--text-base);
  }

  .spinner {
    display: inline-block;
    width: 13px;
    height: 13px;
    border: 2px solid var(--brand-border);
    border-top-color: var(--brand);
    border-radius: 50%;
    animation: cs-spin 0.9s linear infinite;
  }

  @keyframes cs-spin {
    to {
      transform: rotate(360deg);
    }
  }
}
