:root {
  color-scheme: light;
  font: 14px/1.5 var(--mcm-font-ui, "Onest", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif);
  color: var(--mcm-text-primary, #0e1512);
  background: var(--mcm-bg-canvas, #f7f8f5);
  --ink: var(--mcm-text-primary, #0e1512);
  --ink-soft: var(--mcm-text-secondary, #3e4a44);
  --muted: var(--mcm-text-muted, #5c6962);
  --canvas: var(--mcm-bg-canvas, #f7f8f5);
  --band: var(--mcm-bg-band, #eef1ec);
  --surface: var(--mcm-bg-surface, #ffffff);
  --surface-subtle: var(--mcm-bg-subtle, #f2f4f0);
  --line: var(--mcm-border-subtle, #dde3dc);
  --line-strong: var(--mcm-border-strong, #c5cfc4);
  --border: var(--mcm-border-subtle, #dde3dc);
  --primary: var(--mcm-accent, #0b5d3f);
  --primary-hover: var(--mcm-accent-hover, #084730);
  --primary-soft: var(--mcm-accent-soft, rgba(11, 93, 63, 0.08));
  --primary-border: var(--mcm-accent-border, rgba(11, 93, 63, 0.24));
  --focus-ring: rgba(11, 93, 63, 0.16);
  --escrow-ink: var(--mcm-status-escrow, #1d5685);
  --escrow-bg: var(--mcm-status-escrow-bg, rgba(29, 86, 133, 0.09));
  --escrow-border: rgba(29, 86, 133, 0.24);
  --danger: var(--mcm-status-dispute, #b4232e);
  --danger-hover: #911a23;
  --danger-soft: var(--mcm-status-dispute-bg, rgba(180, 35, 46, 0.08));
  --success-ink: var(--mcm-status-verified, #0b6644);
  --success-bg: var(--mcm-status-verified-bg, rgba(11, 102, 68, 0.09));
  --success-border: rgba(11, 102, 68, 0.24);
  --warning-ink: var(--mcm-status-warning, #9a5b0b);
  --warning-bg: var(--mcm-status-warning-bg, rgba(154, 91, 11, 0.09));
  --warning-border: rgba(154, 91, 11, 0.24);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--canvas);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

/* Header */
.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding: 12px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: inherit;
  min-width: 0;
}

.mark {
  width: 28px;
  height: 28px;
  background: transparent;
  color: var(--ink);
  display: grid;
  place-items: center;
  flex: none;
}

.mark svg {
  width: 28px;
  height: 28px;
  display: block;
}

.brand-text {
  font-weight: 700;
  font-size: 16px;
  letter-spacing: -0.02em;
}

.org-pill {
  font-size: 12px;
  font-weight: 600;
  padding: 3px 9px;
  background: var(--primary-soft);
  border: 1px solid var(--primary-border);
  border-radius: 6px;
  color: var(--primary);
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.header-user {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-login {
  font-size: 13px;
  color: var(--ink-soft);
  font-weight: 500;
}

/* Navigation */
.nav-tabs {
  background: var(--band);
  border-bottom: 1px solid var(--line);
  padding: 0 24px;
  display: flex;
  gap: 6px;
  overflow-x: auto;
}

.tab-btn {
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  padding: 12px 14px;
  font-size: 14px;
  font-weight: 550;
  color: var(--ink-soft);
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 6px;
}

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

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

.badge {
  display: inline-flex;
  align-items: center;
  font-size: 11.5px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 6px;
  background: var(--surface-subtle);
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}

.tab-btn .badge {
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid var(--line);
  border-radius: 999px;
}

.tab-btn.active .badge {
  background: var(--surface);
  border-color: var(--primary-border);
  color: var(--primary);
}

.badge-role-seller {
  background: var(--success-bg);
  color: var(--success-ink);
  border: 1px solid var(--success-border);
}

.badge-role-buyer {
  background: var(--escrow-bg);
  color: var(--escrow-ink);
  border: 1px solid var(--escrow-border);
}

.badge-stage-disputed {
  background: var(--warning-bg);
  color: var(--warning-ink);
  border: 1px solid var(--warning-border);
}

.badge-stage-done {
  background: var(--success-bg);
  color: var(--success-ink);
  border: 1px solid var(--success-border);
}

.badge-stage-active {
  background: var(--surface-subtle);
  color: var(--ink-soft);
  border: 1px solid var(--line);
}

/* Main Layout */
.app-main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 20px 44px;
}

.view-section {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 20px;
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 22px;
  box-shadow: 0 1px 2px rgba(14, 21, 18, 0.03);
  min-width: 0;
  max-width: 100%;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}

.card-title {
  font-size: 18px;
  font-weight: 650;
  letter-spacing: -0.015em;
  margin: 0;
  color: var(--ink);
}

.card-subtitle {
  font-size: 13px;
  color: var(--muted);
  margin: 4px 0 0;
}

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

.search-row {
  margin-bottom: 16px;
}

.field-stack {
  display: grid;
  gap: 6px;
}

.field-stack-md {
  gap: 8px;
}

.field-label {
  font-weight: 550;
  font-size: 13px;
  color: var(--ink-soft);
}

.field-label-lg {
  font-weight: 550;
  font-size: 13.5px;
  color: var(--ink-soft);
}

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

.code-hint {
  margin: 10px 0 0;
}

.form-footer-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

/* Form inputs & buttons */
.input-field,
.textarea-field,
.select-field {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  background: var(--surface);
  color: var(--ink);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.input-field:focus,
.textarea-field:focus,
.select-field:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

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

.primary-btn {
  background: var(--primary);
  color: #ffffff;
  border: 1px solid var(--primary);
  padding: 8px 16px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 13.5px;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

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

.ghost-button {
  background: var(--surface);
  color: var(--ink-soft);
  border: 1px solid var(--line-strong);
  padding: 7px 14px;
  border-radius: 8px;
  font-weight: 550;
  font-size: 13px;
  cursor: pointer;
  text-decoration: none;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.ghost-button:hover:not(:disabled) {
  background: var(--surface-subtle);
  color: var(--ink);
}

.danger-btn {
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid rgba(180, 35, 46, 0.24);
  padding: 7px 12px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 12.5px;
  cursor: pointer;
  transition: background-color 0.15s ease;
}

.danger-btn:hover:not(:disabled) {
  background: rgba(180, 35, 46, 0.14);
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

/* Segmented Filter Bar (Deals) */
.deals-filter-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}

.filter-group {
  display: inline-flex;
  gap: 4px;
  flex-wrap: wrap;
  padding: 4px;
  border-radius: 9px;
  background: var(--surface-subtle);
  border: 1px solid var(--line);
}

.filter-pill {
  background: transparent;
  color: var(--ink-soft);
  border: 1px solid transparent;
  padding: 5px 12px;
  border-radius: 6px;
  font-weight: 550;
  font-size: 13px;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.filter-pill:hover {
  background: rgba(255, 255, 255, 0.72);
  color: var(--ink);
}

.filter-pill.active-pill {
  background: var(--surface);
  border-color: var(--line);
  color: var(--primary);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(14, 21, 18, 0.05);
}

.deals-search-box {
  min-width: 240px;
  flex: 0 1 320px;
}

/* Tables */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
}

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

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

.data-table th {
  background: var(--surface-subtle);
  font-weight: 600;
  color: var(--muted);
  font-size: 12.5px;
  white-space: nowrap;
}

.data-table tbody tr:hover {
  background: var(--canvas);
}

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

.col-right {
  text-align: right;
  white-space: nowrap;
}

.col-num-strong {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

#products-tbody td:nth-child(2),
#products-tbody td:nth-child(3),
#products-tbody td:last-child,
#wallets-tbody td:last-child {
  white-space: nowrap;
}

.empty-block,
.empty-state {
  text-align: center;
  padding: 32px 16px;
  color: var(--muted);
}

.empty-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--ink);
  margin: 0;
}

.empty-subtitle {
  font-size: 13px;
  color: var(--muted);
  margin: 4px 0 0;
}

.product-name-btn {
  background: transparent;
  border: 0;
  padding: 0;
  color: var(--primary);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  text-align: left;
}

.product-name-btn:hover {
  text-decoration: underline;
}

.product-subterms {
  font-size: 12px;
  color: var(--muted);
  margin-top: 3px;
  line-height: 1.35;
}

/* Status & Policy Badges */
.status-badge {
  display: inline-flex;
  align-items: center;
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 6px;
}

.status-published {
  background: var(--success-bg);
  color: var(--success-ink);
  border: 1px solid var(--success-border);
}

.status-draft {
  background: var(--warning-bg);
  color: var(--warning-ink);
  border: 1px solid var(--warning-border);
}

.status-archived {
  background: var(--surface-subtle);
  color: var(--muted);
  border: 1px solid var(--line);
}

.policy-badge {
  display: inline-flex;
  align-items: center;
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 6px;
}

.policy-direct {
  background: var(--success-bg);
  color: var(--success-ink);
  border: 1px solid var(--success-border);
}

.policy-escrow {
  background: var(--escrow-bg);
  color: var(--escrow-ink);
  border: 1px solid var(--escrow-border);
}

.cert-link {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12.5px;
  color: var(--primary);
  text-decoration: none;
  padding: 3px 9px;
  background: var(--surface-subtle);
  border: 1px solid var(--line);
  border-radius: 6px;
}

.cert-link:hover {
  background: var(--primary-soft);
  border-color: var(--primary-border);
}

/* Access Code Card */
.code-box {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface-subtle);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  padding: 12px 16px;
  margin-top: 10px;
}

.code-display {
  font-family: var(--mcm-font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.05em;
  color: var(--ink);
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
  word-break: break-all;
}

/* File picker & Modal helpers */
.file-picker-row {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.file-input-hidden {
  display: none;
}

.chosen-file-name {
  font-size: 13px;
  color: var(--ink-soft);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 240px;
}

.modal-lead {
  font-size: 13.5px;
  line-height: 1.5;
  color: var(--ink-soft);
  margin: 0;
}

/* Modal Dialog */
.modal-dialog {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0;
  box-shadow: 0 24px 56px rgba(14, 21, 18, 0.16), 0 4px 14px rgba(14, 21, 18, 0.06);
  max-width: 600px;
  width: calc(100% - 28px);
  background: var(--surface);
}

.modal-dialog-wide {
  max-width: 820px;
  width: min(820px, calc(100% - 24px));
}

.modal-dialog::backdrop {
  background: rgba(14, 21, 18, 0.45);
  backdrop-filter: blur(2px);
}

.modal-header {
  padding: 18px 24px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.modal-body {
  padding: 22px 24px;
  display: grid;
  gap: 16px;
  max-height: 75vh;
  overflow-y: auto;
}

.modal-footer {
  padding: 15px 24px;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  background: var(--surface-subtle);
  border-radius: 0 0 12px 12px;
}

/* Deal Room Modal */
.deal-timeline {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
}

.timeline-step {
  text-align: center;
  padding: 8px 6px;
  border-radius: 7px;
  background: var(--surface-subtle);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.timeline-step.is-done {
  background: var(--success-bg);
  border-color: var(--success-border);
  color: var(--success-ink);
}

.timeline-step.is-disputed {
  background: var(--warning-bg);
  border-color: var(--warning-border);
  color: var(--warning-ink);
}

.room-actions-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 12px;
  background: var(--surface-subtle);
  border-radius: 8px;
  border: 1px solid var(--line);
}

.room-actions-bar:not(:has(button:not([hidden]))) {
  display: none;
}

.room-chat-title {
  font-size: 13.5px;
  font-weight: 600;
  margin: 0;
  color: var(--ink);
}

.room-chat-messages {
  max-height: 280px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border-radius: 8px;
  background: var(--canvas);
  border: 1px solid var(--line);
}

.room-chat-empty {
  font-size: 13px;
  color: var(--muted);
  margin: 0;
}

.room-msg {
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
  line-height: 1.45;
  border: 1px solid var(--line);
}

.room-msg.is-system {
  background: var(--surface-subtle);
  border-left: 3px solid var(--muted);
}

.room-msg.is-operator {
  background: var(--warning-bg);
  border-color: var(--warning-border);
  border-left: 3px solid var(--warning-ink);
}

.room-msg.is-me {
  background: var(--primary-soft);
  border-color: var(--primary-border);
  align-self: flex-end;
  max-width: 80%;
}

.room-msg.is-other {
  background: var(--surface);
  align-self: flex-start;
  max-width: 80%;
}

.room-msg-meta {
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 3px;
  font-variant-numeric: tabular-nums;
}

.room-msg-att {
  display: inline-flex;
  margin-top: 6px;
  font-size: 12px;
  padding: 4px 10px;
}

.room-chat-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.room-chat-textarea {
  min-height: 68px;
}

.room-chat-controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.room-attach-group {
  display: flex;
  gap: 8px;
  align-items: center;
  min-width: 0;
}

.room-chosen-file {
  font-size: 12px;
  color: var(--muted);
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.error-banner {
  padding: 10px 14px;
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid rgba(180, 35, 46, 0.24);
  border-radius: 8px;
  font-size: 13px;
}

.toast-notice {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: var(--ink);
  color: #ffffff;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 13.5px;
  font-weight: 500;
  box-shadow: 0 8px 24px rgba(14, 21, 18, 0.2);
  z-index: 100;
}

@media (max-width: 680px) {
  .app-header {
    padding: 10px 16px;
    flex-wrap: wrap;
  }
  .org-pill {
    max-width: 120px;
  }
  .user-login {
    max-width: 140px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .nav-tabs {
    padding: 0 16px;
  }
  .app-main {
    padding: 16px 12px;
  }
  .grid-2 {
    grid-template-columns: 1fr;
  }
  .code-box {
    flex-direction: column;
    align-items: stretch;
  }
  .deals-search-box {
    width: 100%;
    min-width: 0;
    flex: 1 1 100%;
  }
  .deal-timeline {
    grid-template-columns: 1fr;
  }
}
