:root {
  --ink: #17151a;
  --muted: #6f6874;
  --line: #ded9e3;
  --panel: #ffffff;
  --surface: #f7f5f8;
  --purple: #56105f;
  --purple-2: #7a2286;
  --teal: #0f8d8b;
  --danger: #a13b4b;
  --shadow: 0 18px 50px rgba(34, 18, 40, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--ink);
  background: var(--surface);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  min-height: 82px;
  padding: 14px 28px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.brand img {
  width: 138px;
  height: 54px;
  object-fit: contain;
  border: 2px solid var(--purple);
}

.brand strong,
.brand span {
  display: block;
}

.brand strong {
  font-size: 15px;
}

.brand span {
  color: var(--muted);
  font-size: 13px;
  margin-top: 3px;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.primary-button,
.ghost-button,
.small-button,
.icon-button {
  border: 1px solid transparent;
  border-radius: 8px;
  min-height: 38px;
  padding: 0 14px;
  font-weight: 700;
}

.primary-button {
  background: var(--purple);
  color: #fff;
}

.ghost-button,
.small-button,
.icon-button {
  background: #fff;
  border-color: var(--line);
  color: var(--ink);
}

.small-button {
  min-height: 32px;
  padding: 0 10px;
  font-size: 13px;
}

.icon-button {
  width: 36px;
  padding: 0;
  font-size: 24px;
  line-height: 1;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(420px, 0.95fr) minmax(520px, 1.05fr);
  gap: 22px;
  padding: 22px;
}

.editor,
.preview-zone {
  min-width: 0;
}

.editor {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 20px;
  box-shadow: var(--shadow);
}

.editor-heading,
.preview-toolbar,
.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--purple);
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: 26px;
}

h2 {
  font-size: 18px;
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 4px;
  background: #f0edf3;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.segment {
  border: 0;
  background: transparent;
  border-radius: 6px;
  min-height: 34px;
  padding: 0 12px;
  color: var(--muted);
  font-weight: 800;
}

.segment.active {
  background: #fff;
  color: var(--purple);
  box-shadow: 0 2px 10px rgba(86, 16, 95, 0.12);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 13px;
  margin-top: 20px;
}

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

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

.span-2 {
  grid-column: span 2;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  min-height: 39px;
  padding: 9px 11px;
  outline: none;
}

textarea {
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--purple-2);
  box-shadow: 0 0 0 3px rgba(86, 16, 95, 0.12);
}

.import-hint {
  margin-top: 14px;
  border: 1px dashed rgba(86, 16, 95, 0.34);
  background: rgba(86, 16, 95, 0.05);
  color: #4b2552;
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 13px;
}

.client-input-wrap {
  align-items: center;
  display: grid;
  gap: 10px;
  grid-template-columns: minmax(0, 1fr) auto;
  position: relative;
}

.client-picker {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 18px 40px rgba(32, 28, 36, 0.14);
  display: none;
  left: 0;
  max-height: 260px;
  overflow: auto;
  position: absolute;
  right: 116px;
  top: calc(100% + 6px);
  z-index: 200;
}

.client-input-wrap:focus-within .client-picker:not(:empty) {
  display: grid;
}

.client-picker button {
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  display: grid;
  gap: 3px;
  min-width: 0;
  padding: 10px 12px;
  text-align: left;
  width: 100%;
}

.client-picker button:hover,
.client-picker button:focus {
  background: #f4f1f6;
  outline: none;
}

.client-picker button:last-child {
  border-bottom: 0;
}

.client-picker strong {
  color: var(--ink);
  font-size: 13px;
}

.client-picker span {
  color: var(--muted);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-loader {
  display: none;
  margin-top: 14px;
}

.history-loader.active {
  display: block;
}

.client-database {
  border-top: 1px solid var(--line);
  margin-top: 18px;
  padding-top: 18px;
}

.client-import {
  border-top: 1px solid var(--line);
  margin-top: 18px;
  padding-top: 18px;
}

.client-toolbar {
  align-items: end;
  display: grid;
  gap: 12px;
  grid-template-columns: minmax(0, 1fr) auto;
  margin-top: 12px;
}

.client-form {
  background: #faf9fb;
  border: 1px solid var(--line);
  border-radius: 8px;
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  margin-top: 12px;
  padding: 14px;
}

.client-form[hidden] {
  display: none;
}

.client-form-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

.client-table {
  border: 1px solid var(--line);
  border-radius: 8px;
  display: grid;
  margin-top: 12px;
  max-height: 260px;
  overflow: auto;
}

.client-row {
  align-items: center;
  border-bottom: 1px solid var(--line);
  display: grid;
  gap: 8px;
  grid-template-columns: minmax(130px, 1.2fr) 92px minmax(150px, 1.4fr) minmax(120px, 1fr) 250px;
  padding: 9px 10px;
}

.client-row:last-child {
  border-bottom: 0;
}

.client-row.header {
  background: #f4f1f6;
  color: var(--purple);
  font-size: 12px;
  font-weight: 900;
  position: sticky;
  text-transform: uppercase;
  top: 0;
  z-index: 1;
}

.client-row span {
  font-size: 12px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.client-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: flex-end;
}

.client-actions button {
  min-height: 30px;
  padding: 0 8px;
}

.import-actions {
  align-items: center;
  display: grid;
  gap: 12px;
  grid-template-columns: minmax(0, 1fr) auto;
  margin-top: 8px;
}

.import-actions p {
  color: var(--muted);
  font-size: 13px;
  margin: 0;
}

.import-table {
  border: 1px solid var(--line);
  border-radius: 8px;
  display: grid;
  margin-top: 12px;
  max-height: 330px;
  overflow: auto;
}

.import-row {
  align-items: center;
  border-bottom: 1px solid var(--line);
  display: grid;
  gap: 8px;
  grid-template-columns: 34px minmax(150px, 1.2fr) 95px minmax(160px, 1.4fr) minmax(120px, 1fr) 92px 90px;
  padding: 9px 10px;
}

.import-row:last-child {
  border-bottom: 0;
}

.import-row.header {
  background: #f4f1f6;
  color: var(--purple);
  font-size: 12px;
  font-weight: 900;
  position: sticky;
  text-transform: uppercase;
  top: 0;
  z-index: 1;
}

.import-row input {
  font-size: 12px;
  min-height: 32px;
  padding: 6px 8px;
}

.import-row span {
  color: var(--muted);
  font-size: 12px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.line-section,
.conditions,
.annex-editor,
.totals-panel {
  margin-top: 18px;
}

.items-table {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.item-row {
  display: grid;
  grid-template-columns: minmax(150px, 1fr) 68px 96px 90px 34px;
  gap: 8px;
  align-items: center;
}

.item-row.header {
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
}

.item-row input {
  min-height: 36px;
}

.remove-line {
  width: 34px;
  min-height: 34px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--danger);
  font-size: 20px;
}

.conditions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 13px;
}

.annex-control {
  display: none;
  margin-top: 14px;
}

.annex-control.active {
  display: block;
}

.checkbox-field {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  font-size: 14px;
  font-weight: 800;
}

.checkbox-field input {
  width: 18px;
  height: 18px;
  min-height: 18px;
  accent-color: var(--purple);
}

.annex-editor {
  display: none;
  border-top: 1px solid var(--line);
  padding-top: 18px;
}

.annex-editor.active {
  display: grid;
  gap: 13px;
}

.annex-editor .section-title span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.annex-signatures {
  margin-top: 0;
}

.totals-panel {
  border-top: 1px solid var(--line);
  padding-top: 12px;
  display: grid;
  gap: 9px;
  max-width: 360px;
  margin-left: auto;
}

.totals-panel div {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  color: var(--muted);
}

.totals-panel strong {
  color: var(--ink);
}

.totals-panel .grand-total {
  color: var(--ink);
  font-size: 20px;
  font-weight: 900;
}

.totals-panel.budget-total {
  max-width: 360px;
}

.totals-panel.budget-total div:not(.grand-total) {
  display: none;
}

.totals-panel.budget-total .grand-total span {
  color: var(--purple);
}

.preview-zone {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.preview-toolbar {
  min-height: 58px;
}

.preview-toolbar span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.paper {
  position: relative;
  width: min(100%, 760px);
  min-height: 980px;
  margin: 0 auto;
  padding: 44px 44px 76px;
  background: #fff;
  border: 4px solid var(--purple);
  box-shadow: var(--shadow);
  color: #141116;
}

.paper-header {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 26px;
  align-items: start;
}

.paper-header img {
  width: 220px;
  border: 2px solid var(--purple);
}

.company-block {
  display: grid;
  gap: 4px;
  text-align: right;
  font-size: 12px;
  text-transform: uppercase;
}

.company-block strong {
  font-size: 14px;
}

.paper-meta {
  display: grid;
  grid-template-columns: 1fr 1.1fr;
  gap: 30px;
  margin-top: 34px;
}

.paper h3 {
  margin: 0 0 16px;
  color: var(--purple);
  font-size: 30px;
  letter-spacing: 0;
}

.paper h4 {
  margin: 0 0 8px;
  font-size: 12px;
  letter-spacing: 0;
}

.paper p {
  margin: 0 0 7px;
}

.paper-concept {
  margin-top: 28px;
  padding: 13px 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}

.paper-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 22px;
  font-size: 12px;
}

.paper-table th {
  color: var(--purple);
  text-align: left;
  border-bottom: 2px solid var(--purple);
  padding: 8px 6px;
  text-transform: uppercase;
}

.paper-table td {
  border-bottom: 1px solid var(--line);
  padding: 10px 6px;
  vertical-align: top;
}

.paper-table th:not(:first-child),
.paper-table td:not(:first-child) {
  text-align: right;
}

.paper-bottom {
  display: grid;
  grid-template-columns: 1fr 250px;
  gap: 30px;
  margin-top: 28px;
}

.paper-payment {
  font-size: 12px;
}

.paper-payment.budget-summary {
  border-left: 3px solid var(--purple);
  padding-left: 13px;
}

.paper-payment.budget-summary h4 {
  color: var(--purple);
}

.paper-payment.budget-summary p {
  line-height: 1.55;
}

.paper-totals {
  display: grid;
  gap: 9px;
  align-content: start;
}

.paper-totals p {
  display: flex;
  justify-content: space-between;
  gap: 16px;
}

.paper-grand {
  padding-top: 10px;
  border-top: 2px solid var(--purple);
  color: var(--purple);
  font-size: 21px;
  font-weight: 900;
}

.paper-footer {
  position: absolute;
  right: 44px;
  bottom: 28px;
  left: 44px;
  border-top: 1px solid var(--line);
  padding-top: 10px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 800;
  text-align: center;
  text-transform: uppercase;
  white-space: nowrap;
}

.presupuesto-paper .paper-meta {
  grid-template-columns: 1fr 1fr;
}

.presupuesto-paper .paper-concept {
  display: none;
}

.presupuesto-paper .paper-table {
  margin-top: 34px;
}

.presupuesto-paper .paper-bottom {
  grid-template-columns: 1fr 310px;
  align-items: start;
}

.presupuesto-paper .paper-totals {
  max-width: 310px;
  margin-left: auto;
}

.presupuesto-paper .paper-grand {
  align-items: baseline;
  display: grid;
  gap: 18px;
  grid-template-columns: minmax(0, 1fr) auto;
}

.presupuesto-paper .paper-totals p:not(.paper-grand) {
  display: none;
}

.presupuesto-paper .paper-grand span {
  font-size: 15px;
  line-height: 1.15;
}

.presupuesto-paper .paper-grand strong {
  white-space: nowrap;
}

.presupuesto-paper .paper-footer {
  display: none;
}

.annex-paper {
  display: none;
}

.annex-paper.active {
  display: block;
}

.annex-inner {
  min-height: 888px;
  border: 1px solid #7e7a84;
  padding: 62px 54px;
  color: #123d68;
}

.annex-inner h2 {
  margin: 0 0 86px;
  text-align: center;
  color: #123d68;
  font-size: 18px;
  font-weight: 900;
}

.annex-text {
  white-space: pre-wrap;
  font-size: 12px;
  font-weight: 750;
  line-height: 1.55;
}

.annex-signature-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 70px;
  margin-top: 70px;
  padding: 0 38px;
  font-size: 10px;
  text-align: center;
}

.annex-signature-row div {
  display: grid;
  gap: 4px;
}

.annex-signature-row strong {
  margin-bottom: 12px;
}

.email-dialog {
  width: min(620px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 0;
}

.email-dialog::backdrop {
  background: rgba(18, 12, 24, 0.38);
}

.email-dialog form {
  display: grid;
  gap: 14px;
  padding: 20px;
}

.email-dialog header,
.email-dialog footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.email-dialog footer {
  justify-content: flex-end;
}

@media (max-width: 1080px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .paper {
    width: 100%;
  }
}

@media (max-width: 720px) {
  .topbar,
  .editor-heading,
  .preview-toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .top-actions {
    flex-wrap: wrap;
  }

  .workspace {
    padding: 12px;
  }

  .form-grid,
  .conditions,
  .annex-signature-row,
  .paper-header,
  .paper-meta,
  .paper-bottom {
    grid-template-columns: 1fr;
  }

  .span-2 {
    grid-column: span 1;
  }

  .item-row {
    grid-template-columns: 1fr 64px 84px 34px;
  }

  .item-row.header span:nth-child(4),
  .item-row input.line-total {
    display: none;
  }

  .paper {
    min-height: auto;
    padding: 24px;
  }

  .company-block {
    text-align: left;
  }
}

@media print {
  body {
    background: #fff;
  }

  .topbar,
  .editor,
  .preview-toolbar {
    display: none;
  }

  .workspace {
    display: block;
    padding: 0;
  }

  .paper {
    width: 100%;
    min-height: auto;
    border: 0;
    box-shadow: none;
  }
}
