:root {
  color-scheme: light;
  --bg: #f6f8f9;
  --surface: #ffffff;
  --surface-strong: #fdfefe;
  --text: #182127;
  --muted: #60717d;
  --line: #d8e0e5;
  --line-strong: #b8c6cf;
  --teal: #087f8c;
  --teal-dark: #055c67;
  --amber: #b56b00;
  --amber-soft: #fff3dd;
  --shadow: 0 14px 35px rgba(24, 33, 39, 0.08);
  --radius: 8px;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  width: min(1420px, calc(100% - 40px));
  margin: 0 auto;
  padding: 28px 0 40px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  min-height: 92px;
  border-bottom: 1px solid var(--line);
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(2rem, 4vw, 3.7rem);
  line-height: 1;
  font-weight: 760;
  letter-spacing: 0;
}

.topbar p {
  margin-top: 10px;
  color: var(--muted);
  font-size: 1rem;
}

.persistence-status {
  width: min(340px, 100%);
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  text-align: right;
}

.persistence-status span {
  display: block;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 760;
  text-transform: uppercase;
}

.persistence-status strong {
  display: block;
  margin-top: 4px;
  color: var(--teal-dark);
  font-size: 0.92rem;
  line-height: 1.35;
}

.persistence-status.is-warning {
  border-color: #efc470;
  background: var(--amber-soft);
}

.persistence-status.is-warning strong {
  color: #7b4700;
}

.persistence-status.is-error {
  border-color: #e0a2a2;
  background: #fff1f1;
}

.persistence-status.is-error strong {
  color: #8f1f1f;
}

.app-state {
  display: grid;
  grid-template-columns: minmax(220px, 1.15fr) minmax(220px, 1fr) minmax(150px, 0.65fr) auto;
  align-items: center;
  gap: 1px;
  margin: 16px 0 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--line);
  overflow: hidden;
}

.app-state div {
  min-height: 70px;
  padding: 14px 16px;
  background: var(--surface);
}

.app-state div > span,
.agreement-status small {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 760;
  text-transform: uppercase;
}

.app-state strong {
  display: block;
  margin-top: 6px;
  font-size: 1rem;
  line-height: 1.25;
}

.compact-button {
  width: auto;
  margin: 0 14px;
  white-space: nowrap;
}

.uf-source {
  min-width: 190px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  text-align: right;
}

.uf-source span,
.uf-source small,
.card-label,
dt,
th,
label span,
legend,
.source-note {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 720;
  letter-spacing: 0;
  text-transform: uppercase;
}

.uf-source strong {
  display: block;
  margin: 2px 0;
  color: var(--teal-dark);
  font-size: 1.45rem;
}

.tabs {
  display: flex;
  gap: 8px;
  margin: 20px 0;
  border-bottom: 1px solid var(--line);
}

.tab-button {
  height: 42px;
  border: 1px solid transparent;
  border-bottom: 0;
  border-radius: 7px 7px 0 0;
  background: transparent;
  color: var(--muted);
  padding: 0 14px;
  font-size: 0.9rem;
  font-weight: 760;
}

.tab-button:hover {
  background: #edf6f7;
  color: var(--teal-dark);
}

.tab-button.is-active {
  border-color: var(--line);
  background: var(--surface);
  color: var(--text);
}

.tab-panel[hidden] {
  display: none !important;
}

.summary-grid {
  display: grid;
  grid-template-columns: minmax(190px, 0.7fr) minmax(320px, 1.35fr) minmax(240px, 0.95fr);
  gap: 16px;
  margin: 24px 0;
}

.due-card {
  min-height: 172px;
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.due-card.primary {
  border-color: #efc470;
  background: linear-gradient(180deg, #fffaf0 0%, var(--amber-soft) 100%);
  box-shadow: var(--shadow);
}

.card-label {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
}

.icon {
  display: inline-grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 6px;
  background: #e7f3f5;
  color: var(--teal-dark);
  font-size: 0.9rem;
}

output {
  display: block;
  color: #7b4700;
  font-size: clamp(2.1rem, 4vw, 4.2rem);
  line-height: 0.98;
  font-weight: 800;
  letter-spacing: 0;
}

.due-card strong {
  display: block;
  color: var(--text);
  font-size: 1.65rem;
  line-height: 1.08;
}

.due-card p {
  margin-top: 14px;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.45;
}

dl {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin: 20px 0 0;
}

dt {
  margin-bottom: 4px;
}

dd {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 760;
}

.controls,
.projection-panel,
.contract-panel,
.payment-tracker,
.method-note {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.controls {
  padding: 18px;
}

.controls fieldset {
  grid-template-columns: repeat(4, minmax(180px, 1fr));
}

.controls fieldset + fieldset {
  grid-template-columns: repeat(5, minmax(160px, 1fr));
}

.controls legend {
  grid-column: 1 / -1;
}

.config-lock {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-top: 20px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfcfd;
}

.config-lock strong {
  display: block;
  font-size: 1rem;
}

.config-lock p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.4;
}

.config-lock button {
  flex: 0 0 auto;
}

.config-lock.is-locked {
  border-color: #8db99c;
  background: #edf8f0;
}

.controls.is-locked input,
.controls.is-locked select {
  background: #eef3f5;
  color: #5d6d76;
}

fieldset {
  display: grid;
  gap: 14px;
  margin: 0;
  padding: 0;
  border: 0;
}

fieldset + fieldset {
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid var(--line);
}

legend {
  margin-bottom: 14px;
  color: var(--text);
}

label {
  display: grid;
  gap: 7px;
}

.field-hint {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 650;
}

input,
select {
  width: 100%;
  height: 42px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: var(--surface-strong);
  color: var(--text);
  padding: 0 11px;
  outline: none;
}

input:focus,
select:focus,
button:focus-visible {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(8, 127, 140, 0.15);
}

.projection-panel {
  overflow: hidden;
}

.payment-tracker {
  display: grid;
  grid-template-columns: repeat(4, minmax(170px, 1fr));
  gap: 1px;
  margin-bottom: 18px;
  overflow: hidden;
  background: var(--line);
}

.payment-tracker article {
  min-height: 88px;
  padding: 18px;
  background: var(--surface);
}

.payment-tracker span {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 760;
  text-transform: uppercase;
}

.payment-tracker strong {
  display: block;
  margin-top: 8px;
  color: var(--text);
  font-size: 1.35rem;
  line-height: 1.15;
}

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

h2 {
  font-size: 1.3rem;
  line-height: 1.2;
}

.panel-heading p {
  margin-top: 5px;
  color: var(--muted);
  font-size: 0.94rem;
}

button {
  height: 40px;
  border: 0;
  border-radius: 7px;
  background: var(--teal);
  color: white;
  padding: 0 14px;
  font-size: 0.9rem;
  font-weight: 760;
  cursor: pointer;
}

button:hover,
.button-link:hover {
  background: var(--teal-dark);
}

.button-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  border-radius: 7px;
  background: var(--teal);
  color: white;
  padding: 0 14px;
  font-size: 0.9rem;
  font-weight: 760;
  text-decoration: none;
}

.whatsapp-button {
  background: #128c45;
}

.whatsapp-button:hover {
  background: #0d6d35;
}

.secondary-button {
  width: 100%;
  border: 1px solid var(--line-strong);
  background: #edf6f7;
  color: var(--teal-dark);
}

.secondary-button:hover {
  background: #dff0f2;
}

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  align-items: center;
  padding: 14px 20px 0;
  background: #fbfcfd;
}

.chart-legend span {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 720;
}

.legend-swatch {
  display: inline-block;
  width: 18px;
  height: 10px;
  border-radius: 4px;
}

.legend-projected {
  background: var(--teal);
}

.legend-real {
  background: #285fbd;
}

.legend-paid {
  background:
    repeating-linear-gradient(135deg, rgba(255, 255, 255, 0.32) 0 4px, transparent 4px 8px),
    #2f8a4f;
}

.chart {
  display: grid;
  grid-template-columns: repeat(var(--bar-count, 16), minmax(24px, 1fr));
  align-items: end;
  gap: 7px;
  height: 180px;
  padding: 22px 20px 18px;
  border-bottom: 1px solid var(--line);
  background:
    linear-gradient(to top, rgba(216, 224, 229, 0.58) 1px, transparent 1px) 0 0 / 100% 44px,
    #fbfcfd;
}

.bar {
  position: relative;
  min-height: 10px;
  border-radius: 5px 5px 0 0;
  background: var(--teal);
}

.bar--projected {
  background: var(--teal);
}

.bar--real {
  background: #285fbd;
}

.bar--paid {
  background:
    repeating-linear-gradient(135deg, rgba(255, 255, 255, 0.28) 0 4px, transparent 4px 8px),
    #2f8a4f;
  box-shadow: inset 0 0 0 1px rgba(21, 87, 45, 0.35);
}

.bar--next {
  outline: 2px solid var(--amber);
  outline-offset: 2px;
}

.bar span {
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 700;
  white-space: nowrap;
}

.bar mark {
  position: absolute;
  top: 7px;
  left: 50%;
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border-radius: 999px;
  background: white;
  color: #2f8a4f;
  font-size: 0.72rem;
  font-weight: 900;
  transform: translateX(-50%);
}

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  min-width: 1040px;
  border-collapse: collapse;
}

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

th:first-child,
td:first-child,
th:nth-child(2),
td:nth-child(2) {
  text-align: left;
}

tbody tr:first-child {
  background: #fff8eb;
}

tbody tr.is-paid {
  background: #edf8f0;
  color: #385247;
}

tbody tr:hover {
  background: #f2f8f9;
}

td {
  font-size: 0.94rem;
  font-variant-numeric: tabular-nums;
}

td strong {
  color: var(--amber);
}

.paid-cell {
  text-align: center;
}

.paid-cell input {
  width: 20px;
  height: 20px;
  accent-color: var(--teal);
}

.paid-date {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 650;
}

.method-note {
  margin-top: 18px;
  padding: 20px;
}

.workflow-panel {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.workflow-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 1px;
  background: var(--line);
}

.workflow-grid article {
  min-height: 214px;
  padding: 20px;
  background: var(--surface);
}

.workflow-grid span {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 7px;
  background: #e7f3f5;
  color: var(--teal-dark);
  font-weight: 800;
}

.workflow-grid strong {
  display: block;
  margin-top: 14px;
  font-size: 1.05rem;
}

.workflow-grid p {
  margin-top: 9px;
  color: var(--muted);
  font-size: 0.93rem;
  line-height: 1.48;
}

.agreement-status {
  display: grid;
  grid-template-columns: minmax(240px, 0.8fr) minmax(0, 1.2fr);
  gap: 18px;
  padding: 20px;
  border-top: 1px solid var(--line);
  background: #fbfcfd;
}

.agreement-status span {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 760;
  text-transform: uppercase;
}

.agreement-status strong {
  display: block;
  margin-top: 7px;
  font-size: 1.15rem;
  line-height: 1.3;
}

.agreement-status small {
  margin-top: 8px;
  text-transform: none;
}

.agreement-status p {
  margin-top: 12px;
  color: #334650;
  font-size: 0.94rem;
  line-height: 1.52;
  white-space: pre-line;
}

.agreement-status button {
  width: 100%;
}

.contract-panel {
  margin-top: 18px;
  overflow: hidden;
}

.contract-body {
  display: grid;
  gap: 14px;
  padding: 22px 24px;
  border-bottom: 1px solid var(--line);
}

.contract-body h3 {
  margin: 0 0 2px;
  color: var(--text);
  font-size: 1.05rem;
  line-height: 1.3;
}

.contract-body p,
.contract-body li {
  margin: 0;
  color: #334650;
  font-size: 0.98rem;
  line-height: 1.6;
}

.contract-body ol {
  display: grid;
  gap: 10px;
  margin: 0;
  padding-left: 22px;
}

.contract-meta {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.contract-meta div {
  min-height: 72px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fbfcfd;
}

.contract-meta span {
  display: block;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 760;
  text-transform: uppercase;
}

.contract-meta strong {
  display: block;
  margin-top: 6px;
  font-size: 1rem;
  line-height: 1.25;
}

.signature-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
  padding: 24px;
}

.signature-box {
  min-height: 236px;
  padding: 20px;
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  background: #fbfcfd;
}

.signature-instruction {
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

.signature-pad {
  height: 118px;
  margin-bottom: 10px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background:
    linear-gradient(to bottom, transparent 74%, rgba(117, 134, 145, 0.65) 75%, transparent 76%),
    #ffffff;
  overflow: hidden;
}

.signature-pad canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

.signature-clear,
.signature-fullscreen-button {
  height: 34px;
  margin: 0 8px 16px 0;
  border: 1px solid var(--line-strong);
  background: #edf6f7;
  color: var(--teal-dark);
  padding: 0 12px;
  font-size: 0.82rem;
}

.signature-clear:hover,
.signature-fullscreen-button:hover {
  background: #dff0f2;
}

.signature-fullscreen-button {
  background: var(--teal);
  color: white;
}

.signature-fullscreen-button:hover {
  background: var(--teal-dark);
}

.signature-box strong,
.signature-box span,
.signature-box small {
  display: block;
}

.signature-box strong {
  font-size: 1.05rem;
}

.signature-box span {
  margin-top: 3px;
  color: var(--muted);
}

.rut-field {
  margin-top: 14px;
}

.rut-field span {
  margin: 0;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 760;
  text-transform: uppercase;
}

.rut-field input {
  height: 38px;
  background: white;
}

body.signature-mode {
  overflow: hidden;
}

body.actor-dialog-open {
  overflow: hidden;
}

.actor-dialog[hidden] {
  display: none !important;
}

.actor-dialog {
  position: fixed;
  inset: 0;
  z-index: 1200;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(24, 33, 39, 0.52);
}

.actor-dialog-box {
  width: min(420px, 100%);
  padding: 24px;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.actor-dialog-box span {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 760;
  text-transform: uppercase;
}

.actor-dialog-box h2 {
  margin-top: 8px;
  font-size: 1.65rem;
}

.actor-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 20px;
}

.actor-actions button {
  height: 48px;
}

.fullscreen-signature[hidden] {
  display: none !important;
}

.fullscreen-signature {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: #f6f8f9;
  color: var(--text);
}

.fullscreen-signature-shell {
  display: flex;
  flex-direction: column;
  width: 100dvw;
  height: 100dvh;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}

.fullscreen-signature-header,
.fullscreen-signature-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex: 0 0 auto;
}

.fullscreen-signature-header span {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 760;
  text-transform: uppercase;
}

.fullscreen-signature-header strong {
  display: block;
  margin-top: 2px;
  font-size: 1.25rem;
}

.fullscreen-signature-header button {
  background: #31434d;
}

.landscape-hint {
  display: none;
  margin: 10px 0;
  color: var(--amber);
  font-size: 0.92rem;
  font-weight: 750;
}

.fullscreen-signature-pad {
  flex: 1 1 auto;
  min-height: 220px;
  margin: 12px 0;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background:
    linear-gradient(to bottom, transparent 72%, rgba(117, 134, 145, 0.72) 73%, transparent 74%),
    white;
  overflow: hidden;
}

.fullscreen-signature-pad canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
}

.fullscreen-signature-actions button {
  min-width: 140px;
}

.signature-box small {
  margin-top: 12px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 650;
}

.method-note p {
  margin-top: 10px;
  max-width: 980px;
  color: var(--muted);
  line-height: 1.55;
}

@media (max-width: 980px) {
  .app-shell {
    width: min(100% - 28px, 760px);
    padding-top: 18px;
  }

  .topbar,
  .panel-heading {
    align-items: stretch;
    flex-direction: column;
  }

  .uf-source {
    width: 100%;
    text-align: left;
  }

  .persistence-status {
    width: 100%;
    text-align: left;
  }

  .summary-grid,
  .payment-tracker,
  .workflow-grid,
  .agreement-status,
  .app-state,
  .controls fieldset,
  .controls fieldset + fieldset,
  .config-lock {
    grid-template-columns: 1fr;
  }

  .app-state div {
    min-height: 62px;
  }

  .compact-button {
    width: calc(100% - 28px);
    margin: 14px;
  }

  .config-lock {
    align-items: stretch;
    flex-direction: column;
  }

  .config-lock button {
    width: 100%;
  }

  .tabs {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    border-bottom: 0;
    gap: 8px;
  }

  .tab-button {
    min-height: 44px;
    height: auto;
    border: 1px solid var(--line);
    border-radius: 7px;
    padding: 9px 10px;
    white-space: normal;
  }

  .contract-meta,
  .signature-grid {
    grid-template-columns: 1fr;
  }

  .controls {
    position: static;
  }

  .chart {
    grid-template-columns: repeat(var(--bar-count, 16), 26px);
    overflow-x: auto;
  }
}

@media (orientation: portrait) and (max-width: 980px) {
  .fullscreen-signature-shell {
    padding-top: max(10px, env(safe-area-inset-top));
  }

  .landscape-hint {
    display: block;
  }

  .fullscreen-signature-pad {
    min-height: 58dvh;
  }
}

@media (max-width: 560px) {
  .app-shell {
    width: min(100% - 20px, 520px);
    padding-top: 12px;
  }

  h1 {
    font-size: 2rem;
  }

  .summary-grid {
    gap: 12px;
  }

  .due-card,
  .controls,
  .method-note,
  .workflow-grid article,
  .contract-body,
  .signature-box {
    padding: 16px;
  }

  .topbar {
    min-height: auto;
    gap: 14px;
  }

  .panel-heading {
    padding: 16px;
  }

  .payment-tracker article {
    min-height: 72px;
    padding: 14px 16px;
  }

  .payment-tracker strong {
    font-size: 1.15rem;
  }

  .workflow-grid article {
    min-height: auto;
  }

  .signature-grid {
    padding: 16px;
  }

  .actor-actions {
    grid-template-columns: 1fr;
  }

  dl {
    grid-template-columns: 1fr;
  }

  output {
    font-size: 2.25rem;
  }

  th,
  td {
    padding: 12px;
  }
}

@media print {
  body {
    background: white;
  }

  .app-shell {
    width: auto;
    padding: 0;
  }

  .controls,
  .chart,
  .signature-clear,
  .signature-fullscreen-button,
  .fullscreen-signature,
  .signature-instruction,
  .panel-heading button,
  #downloadCsv {
    display: none !important;
  }

  .rut-field input {
    border: 0;
    border-bottom: 1px solid #758691;
    border-radius: 0;
    padding-left: 0;
  }

  .topbar,
  .summary-grid,
  .workspace,
  .contract-panel,
  .method-note {
    break-inside: avoid;
  }

  .contract-panel,
  .payment-tracker,
  .projection-panel,
  .method-note,
  .due-card,
  .uf-source {
    box-shadow: none;
  }
}
