/* V7.5.0: full UI audit, stable settings, message history, dirty-form guard and mobile fixes. */
:root{--v17-control-height:44px}

/* Toasts must remain visible even while a native <dialog> is in the top layer. */
.v17-dialog-toast-host{position:fixed;top:max(10px,env(safe-area-inset-top));right:50%;transform:translateX(50%);width:min(92vw,500px);display:grid;gap:8px;z-index:2147483647;pointer-events:none}
.v17-dialog-toast-host .toast{pointer-events:auto}

/* New-contract review: prevent financial labels and long amounts from crossing cards. */
.review-grid{align-items:stretch}
.review-card{min-width:0;overflow:hidden;display:flex;flex-direction:column}
.review-card>*{min-width:0;max-width:100%}
.review-card:nth-child(3),.review-card:nth-child(4){grid-column:1/-1}
.review-card .activity{min-width:0}
.review-card .activity>div{min-width:0}
.review-card .activity b,.review-card .activity small{display:block;max-width:100%;overflow-wrap:anywhere}
.financial-review-grid{grid-template-columns:repeat(3,minmax(0,1fr))!important;width:100%;max-width:100%}
.financial-review-grid .detail-item{min-width:0;overflow:hidden;padding:10px}
.financial-review-grid .detail-item span{min-height:32px;display:flex;align-items:flex-start;overflow-wrap:anywhere;line-height:1.55}
.financial-review-grid .detail-item strong{display:block;min-width:0;max-width:100%;font-size:clamp(.77rem,.82vw,.95rem);line-height:1.55;white-space:normal;overflow-wrap:anywhere;word-break:break-word;font-variant-numeric:tabular-nums}
.detail-grid,.detail-item{min-width:0;max-width:100%}
.detail-item strong,.detail-item b{min-width:0;max-width:100%;white-space:normal;overflow-wrap:anywhere;word-break:break-word}
.contract-summary-grid>div,.totals-bar>*{min-width:0;overflow:hidden}
.contract-summary-grid strong,.total-box strong{overflow-wrap:anywhere;word-break:break-word;font-variant-numeric:tabular-nums}

/* Every settings tab occupies a stable cell; page content cannot change tab dimensions. */
.settings-tabs{display:grid!important;grid-template-columns:repeat(4,minmax(0,1fr))!important;grid-auto-rows:54px;gap:8px!important;overflow:visible!important;width:100%;align-items:stretch}
.settings-tabs button{display:flex;align-items:center;justify-content:center;width:100%!important;height:54px!important;min-height:54px!important;max-height:54px!important;padding:7px 8px!important;white-space:normal!important;line-height:1.35!important;text-align:center;overflow:hidden}
.settings-grid{align-items:start}
.settings-grid>.panel{height:100%;min-width:0}

/* Long text and form controls. */
.field,.field-label,.setting-switch,.switch-field{min-width:0;max-width:100%}
.field input,.field textarea,.field select,.field-label input{width:100%;min-width:0;max-width:100%}
.field textarea{resize:vertical;overflow-wrap:anywhere}
.network-url{white-space:normal!important;overflow-wrap:anywhere;word-break:break-all}
.v17-network-list{display:grid;gap:10px;margin-top:12px}
.v17-network-row{display:grid;grid-template-columns:minmax(0,1fr) auto auto;gap:8px;align-items:center;border:1px solid var(--line);border-radius:13px;padding:9px;background:var(--surface)}
.v17-network-row code{direction:ltr;text-align:left;min-width:0;overflow-wrap:anywhere;word-break:break-all}
.v17-network-state{display:inline-flex;align-items:center;justify-content:center;border-radius:999px;padding:4px 9px;background:var(--surface-2);font-size:.76rem;white-space:nowrap}
.v17-network-state.ok{background:var(--success-soft);color:var(--success)}
.v17-network-state.bad{background:var(--danger-soft);color:var(--danger)}

/* SMS analytics and per-customer history. */
.v17-sms-dashboard{margin:15px 0}
.v17-sms-type-grid{display:grid;grid-template-columns:repeat(5,minmax(0,1fr));gap:9px}
.v17-sms-type-card{border:1px solid var(--line);border-radius:14px;padding:11px;background:var(--surface);min-width:0;display:grid;gap:4px}
.v17-sms-type-card span{font-size:.75rem;color:var(--muted);overflow-wrap:anywhere}
.v17-sms-type-card b{font-size:1.15rem;overflow-wrap:anywhere}
.v17-sms-filters{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:9px;margin:12px 0}
.v17-sms-message{max-width:520px;white-space:normal!important;overflow-wrap:anywhere;word-break:break-word;line-height:1.7}
.v17-sms-customer-head{display:flex;align-items:center;justify-content:space-between;gap:10px;flex-wrap:wrap;margin-bottom:12px}

/* Development/AI handoff guide inside settings. */
.v17-guide-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:14px}
.v17-guide-card{border:1px solid var(--line);border-radius:16px;background:var(--surface);padding:15px;min-width:0}
.v17-guide-card h3{margin:0 0 8px}
.v17-guide-card p,.v17-guide-card li,.v17-guide-card code{overflow-wrap:anywhere;word-break:break-word;line-height:1.85}
.v17-guide-card code{direction:ltr;display:inline-block;background:var(--surface-2);border-radius:7px;padding:1px 6px}
.v17-guide-card.full{grid-column:1/-1}

/* Unsaved changes dialog. */
.v17-unsaved-card{width:min(94vw,560px)}
.v17-unsaved-card .v17-unsaved-icon{width:48px;height:48px;border-radius:16px;display:grid;place-items:center;background:var(--warning-soft);color:var(--warning);font-size:24px;margin-bottom:10px}
.v17-unsaved-card p{line-height:1.9;color:var(--muted)}
.v17-dirty-indicator{display:inline-flex;align-items:center;gap:5px;color:var(--warning);font-size:.78rem;margin-inline-start:6px}

/* Better dark mode: every common white card must use theme surfaces. */
body[data-theme="dark-ledger"]{background:var(--bg);color:var(--text)}
body[data-theme="dark-ledger"] :where(.panel,.customer-card,.contract-card,.review-card,.detail-item,.activity,.stat-card,.quick-card,.choice,.step,.schedule-row,.selected-person,.contract-alert,.setting-switch,.switch-field,.template-card,.receipt-review-card,.promise-card,.installment-manager-card,.backup-card,.document-card,.search-item,.v10-customer-card,.v10-contract-card,.v17-guide-card,.v17-sms-type-card,.v17-network-row){background:var(--surface)!important;color:var(--text)!important;border-color:var(--line)!important}
body[data-theme="dark-ledger"] :where(.surface,.surface-2,.item-row,.total-box,.meta,.analysis-card,.decision-card,.barcode-box,.network-card,.success-box,.warning-box,.danger-box){background:var(--surface-2)!important;color:var(--text)!important;border-color:var(--line)!important}
body[data-theme="dark-ledger"] :where(input,select,textarea,.search-box,.btn.outline,.tabs button,.settings-tabs button,.view-switch button){background:#1a212b!important;color:var(--text)!important;border-color:#465363!important}
body[data-theme="dark-ledger"] :where(.tabs button.active,.settings-tabs button.active,.view-switch button.active,.choice.active,.step.active){background:var(--primary)!important;color:#fff!important}
body[data-theme="dark-ledger"] .muted,body[data-theme="dark-ledger"] small,body[data-theme="dark-ledger"] .detail-item span{color:#bdc7d3!important}
body[data-theme="dark-ledger"] .topbar{border-bottom-color:#313d4b}
body[data-theme="dark-ledger"] .wizard-footer,body[data-theme="dark-ledger"] .sticky-actions,body[data-theme="dark-ledger"] .p15-savebar{background:rgba(24,31,40,.95)!important;border-color:var(--line)!important}
body[data-theme="dark-ledger"] .data-table th{background:#293441;color:#f3f7fb}
body[data-theme="dark-ledger"] .data-table td{background:#202832;color:var(--text);border-color:#3a4654}

/* Width safety across all audited pages. */
.page-host>*{min-width:0;max-width:100%}
.page-head,.toolbar,.dashboard-grid,.stats-grid,.quick-grid,.analysis-grid,.form-grid,.settings-grid,.payment-layout,.sms-layout,.receipt-review-grid,.feature-grid,.v8-grid{min-width:0;max-width:100%}
.panel,.customer-card,.contract-card,.review-card,.item-row,.stat-card,.quick-card{overflow-wrap:anywhere}
.panel-head>*{min-width:0}
.panel-head h2,.panel-head h3,.panel-head h4{overflow-wrap:anywhere}

@media(max-width:1180px){
  .settings-tabs{grid-template-columns:repeat(3,minmax(0,1fr))!important}
  .v17-sms-type-grid{grid-template-columns:repeat(3,minmax(0,1fr))}
  .v17-sms-filters{grid-template-columns:repeat(2,minmax(0,1fr))}
}
@media(max-width:900px){
  .financial-review-grid{grid-template-columns:repeat(2,minmax(0,1fr))!important}
  .review-card:nth-child(1),.review-card:nth-child(2),.review-card:nth-child(3),.review-card:nth-child(4){grid-column:1/-1}
  .v17-guide-grid{grid-template-columns:1fr}
  .v17-guide-card.full{grid-column:auto}
}
@media(max-width:760px){
  .settings-tabs{grid-template-columns:repeat(2,minmax(0,1fr))!important;grid-auto-rows:50px}
  .settings-tabs button{height:50px!important;min-height:50px!important;max-height:50px!important;font-size:12px!important}
  .financial-review-grid{grid-template-columns:1fr!important}
  .financial-review-grid .detail-item span{min-height:0}
  .v17-sms-type-grid{grid-template-columns:repeat(2,minmax(0,1fr))}
  .v17-sms-filters{grid-template-columns:1fr}
  .v17-network-row{grid-template-columns:minmax(0,1fr) auto}
  .v17-network-row .v17-network-state{grid-column:1/-1;justify-self:start}
  .wizard-content{width:100%;max-width:100%;padding:0!important}
  #wizardPanel{padding:12px!important}
}
@media(max-width:390px){
  .settings-tabs{grid-template-columns:1fr 1fr!important}
  .v17-sms-type-grid{grid-template-columns:1fr}
}

/* Final audit fixes: every card owns its width and review totals use the full desktop canvas. */
.toast-host{z-index:2147483000!important;position:fixed!important;inset-block-start:max(12px,env(safe-area-inset-top))!important}
.review-card .detail-grid{display:block!important;width:100%!important}
.financial-review-grid{display:grid!important;grid-template-columns:repeat(4,minmax(0,1fr))!important;gap:10px!important}
.document-card{min-width:0!important;max-width:100%!important;overflow:hidden!important}
.document-card b,.document-card small{max-width:100%;overflow-wrap:anywhere;word-break:break-word}
.item-row,.item-row .field,.item-row input,.item-row select{min-width:0!important;max-width:100%!important;box-sizing:border-box!important}
.template-card .card-top{min-width:0;max-width:100%;overflow:hidden;gap:8px;flex-wrap:wrap}
.template-card .card-top>*{min-width:0}
.template-card .card-top label{box-sizing:border-box;white-space:nowrap}
.switch-field,.setting-switch{box-sizing:border-box!important;width:100%!important;max-width:100%!important}
.compact-list,.v10-compact-contract,.compact-customer{min-width:0!important;max-width:100%!important;width:100%!important;box-sizing:border-box!important}
.topbar-title{min-width:0}.topbar-title>div{min-width:0}.topbar-title h1,.topbar-title p{max-width:100%;overflow-wrap:anywhere;word-break:break-word}
.page-actions,.row-actions,.modal-actions{min-width:0;flex-wrap:wrap}

/* The print engine removes hidden blocks instead of keeping blank placeholders. */
.sp15-four-grid[data-count="1"]{grid-template-columns:1fr!important;grid-template-rows:1fr!important}
.sp15-four-grid[data-count="2"]{grid-template-columns:1fr 1fr!important;grid-template-rows:1fr!important}
.sp15-four-grid[data-count="3"]{grid-template-columns:1fr 1fr!important;grid-template-rows:1fr 1fr!important}
.sp15-four-grid[data-count="3"]>.sp15-panel:last-child{grid-column:1/-1}
.sp15-four-grid[data-count="4"]{grid-template-columns:1fr 1fr!important;grid-template-rows:1fr 1fr!important}

@media(max-width:1180px){.financial-review-grid{grid-template-columns:repeat(3,minmax(0,1fr))!important}}
@media(max-width:900px){.financial-review-grid{grid-template-columns:repeat(2,minmax(0,1fr))!important}}
@media(max-width:760px){
  .financial-review-grid{grid-template-columns:1fr!important}
  .compact-list,.v10-compact-contract,.compact-customer{width:100%!important;max-width:100%!important}
  .steps{scroll-padding-inline:12px}
  .step{min-width:150px!important;max-width:170px!important}
  .topbar-title h1{font-size:clamp(1rem,5.3vw,1.28rem)!important}
}

/* Scanner-clean refinements from the second desktop/mobile pass. */
.document-card>*{min-width:0!important;max-width:100%!important;overflow:hidden!important}
.template-card .card-top label{max-width:100%!important;overflow:hidden!important;white-space:normal!important}
.switch-field,.setting-switch{overflow:hidden!important}
.compact-code{min-width:0!important;max-width:100%!important;overflow:hidden!important;text-overflow:ellipsis!important}
.payment-layout b,.payment-layout strong,.receipt-review-card b{max-width:100%;white-space:normal;overflow-wrap:anywhere;word-break:break-word}
.topbar-title h1{white-space:normal!important;line-height:1.25!important;overflow:visible!important}
@media(max-width:760px){
  .steps{display:grid!important;grid-template-columns:1fr 1fr!important;gap:8px!important;overflow:visible!important;scroll-snap-type:none!important}
  .steps .step{min-width:0!important;max-width:none!important;width:100%!important}
  .steps .step:last-child:nth-child(odd){grid-column:1/-1}
}
