:root {
  --bg: #f3f3f0;
  --paper: #ffffff;
  --paper-soft: #fbfbf8;
  --ink: #161718;
  --ink-soft: #303438;
  --muted: #6f7376;
  --line: #d8d9d4;
  --line-strong: #b8bab2;
  --accent: #182636;
  --accent-2: #22364b;
  --accent-soft: #edf0f2;
  --danger: #8e1919;
  --good: #28653a;
  --event: #f4f0e6;
  --shadow: 0 18px 55px rgba(22, 23, 24, 0.075);
  font-family: "Segoe UI", Arial, Helvetica, sans-serif;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(255,255,255,0.78), rgba(255,255,255,0) 220px),
    var(--bg);
  font-size: 14px;
  line-height: 1.45;
}

[hidden] { display: none !important; }

.app-shell {
  width: min(1720px, calc(100vw - 48px));
  margin: 0 auto;
  padding: 30px 0 42px;
}

.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 24px;
  border-bottom: 2px solid var(--ink);
  padding-bottom: 16px;
  margin-bottom: 26px;
}

.eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.22em;
  font-size: 10px;
  color: var(--muted);
  margin-bottom: 6px;
  font-weight: 700;
}

h1 {
  font-size: clamp(26px, 2vw, 34px);
  line-height: 1.08;
  margin: 0;
  font-weight: 750;
  letter-spacing: -0.035em;
}

h2 {
  font-size: 15px;
  line-height: 1.25;
  margin: 0;
  font-weight: 750;
  letter-spacing: -0.012em;
}

.layout {
  display: grid;
  grid-template-columns: minmax(410px, 470px) minmax(820px, 1fr);
  gap: 26px;
  align-items: start;
}

.panel,
.result-panel {
  background: var(--paper);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.input-panel {
  position: sticky;
  top: 18px;
  padding: 18px;
}

.result-panel {
  padding: 24px;
  min-height: 620px;
  min-width: 0;
}

.field-group,
.field { margin-bottom: 14px; }

label,
.rate-label {
  display: block;
  min-height: 15px;
  font-size: 11.5px;
  line-height: 1.25;
  font-weight: 750;
  letter-spacing: -0.005em;
  margin-bottom: 6px;
  color: var(--ink-soft);
}

input[type="text"],
input[type="date"],
input:not([type]) {
  width: 100%;
  height: 40px;
  border: 1px solid var(--line-strong);
  background: #fff;
  color: var(--ink);
  border-radius: 0;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
  font-variant-numeric: tabular-nums;
}

.date-input {
  letter-spacing: 0.02em;
}

input:disabled {
  background: #eeeeea;
  color: #565b5f;
  -webkit-text-fill-color: #565b5f;
}

input::placeholder { color: #a0a39e; }

input:focus,
button:focus {
  outline: 2px solid rgba(24, 38, 54, 0.34);
  outline-offset: 1px;
}

.dates-grid,
.two-col,
.date-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 10px;
}

.payment-fields {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr);
  gap: 10px;
  align-items: end;
  margin-bottom: 0;
}

.notice {
  background: #f6f6f1;
  border-left: 4px solid var(--accent);
  color: #373737;
  padding: 12px;
  line-height: 1.45;
  margin-bottom: 16px;
}
.notice.compact { font-size: 13px; }

.procedures {
  display: grid;
  gap: 12px;
}

.procedure {
  border: 1px solid var(--line);
  padding: 14px;
  background: var(--paper-soft);
}

.procedure h3 {
  margin: 0 0 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--line);
  font-size: 11px;
  line-height: 1.2;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--accent);
}

.rate-line {
  border: 1px solid var(--line);
  padding: 12px;
  margin-top: 12px;
  background: #fff;
}

.rate-value {
  min-height: 40px;
  border: 1px solid var(--line-strong);
  background: #eeeeea;
  color: #42484d;
  display: flex;
  align-items: center;
  padding: 8px 10px;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
}

.rate-box {
  border: 1px solid var(--line);
  padding: 12px;
  margin-bottom: 18px;
}
.rate-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
  margin-bottom: 10px;
}
.hint { color: var(--muted); font-size: 12px; line-height: 1.35; }
.checkline {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  white-space: nowrap;
  font-size: 12px;
  font-weight: 400;
}

hr {
  border: 0;
  border-top: 1px solid var(--line);
  margin: 20px -18px;
}

.subsection-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 12px;
}
.subsection-head p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.payments,
.partials-list { display: grid; gap: 10px; }
.payment,
.partial-row {
  border: 1px solid var(--line);
  background: var(--paper-soft);
  padding: 10px;
}
.partial-title {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 7px;
}
.partial-fields {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 38px;
  gap: 8px;
  align-items: center;
}
.delete-partial,
.remove-payment {
  height: 38px;
  border: 1px solid var(--line-strong);
  color: var(--danger);
  background: #fff;
}

button {
  border-radius: 0;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}
.primary {
  width: 100%;
  height: 48px;
  margin-top: 18px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  font-weight: 750;
  font-size: 15px;
}
.primary:hover { background: var(--accent-2); }
.ghost,
.small,
.plain,
.secondary {
  border: 1px solid var(--line-strong);
  background: #fff;
  color: var(--ink);
}
.ghost { height: 38px; padding: 0 14px; font-size: 13px; }
.small { min-height: 34px; padding: 0 10px; font-weight: 750; font-size: 12.5px; }
.add-partial-wide,
.add-procedure-wide { width: 100%; height: 40px; margin-top: 12px; }
.plain {
  width: 100%;
  height: 38px;
  margin-top: 10px;
  color: var(--muted);
}
.danger {
  width: 100%;
  min-height: 36px;
  margin-top: 12px;
  color: var(--danger);
  border-color: var(--line-strong);
  background: #fff;
}
button:hover { border-color: #8d938e; }
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.errors {
  margin-top: 14px;
  color: var(--danger);
  font-size: 13px;
  line-height: 1.45;
}
.errors ul { margin: 0; padding-left: 18px; }

.summary-cards {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}
.summary-card {
  border: 1px solid var(--line);
  padding: 16px 16px 14px;
  background: linear-gradient(180deg, #fff 0%, #fbfbf8 100%);
  min-height: 116px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.card-label {
  color: var(--muted);
  font-size: 11.5px;
  line-height: 1.25;
  margin-bottom: 10px;
  max-width: 95%;
}
.card-value {
  font-size: clamp(22px, 1.8vw, 32px);
  font-weight: 800;
  letter-spacing: -0.045em;
  line-height: 1.08;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}
.card-sub {
  margin-top: 8px;
  color: var(--muted);
  font-size: 12px;
}

.result-intro {
  line-height: 1.48;
  margin-bottom: 18px;
  font-size: 13.5px;
  color: #2f3336;
}
.result-intro p { margin: 0 0 7px; }
.result-intro strong { font-weight: 750; }

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  background: #fff;
}
table { border-collapse: collapse; width: 100%; }
.calc-table {
  min-width: 1120px;
  table-layout: fixed;
}
.calc-table th,
.calc-table td {
  border-bottom: 1px solid var(--line);
  padding: 8px 9px;
  vertical-align: top;
}
.calc-table th {
  background: #eeeeea;
  text-align: left;
  font-size: 10.5px;
  line-height: 1.25;
  text-transform: uppercase;
  letter-spacing: 0.055em;
  color: #34383a;
  white-space: nowrap;
}
.calc-table td {
  font-size: 12.5px;
  line-height: 1.32;
}
.calc-table td:first-child { width: 26%; }
.calc-table td:nth-child(2) { width: 14%; }
.num { text-align: right !important; white-space: nowrap; font-variant-numeric: tabular-nums; }
.empty { color: var(--muted); text-align: center; padding: 24px !important; }
.procedure-row td,
tr.procedure td {
  background: var(--event);
  color: #272727;
  font-weight: 700;
}
.payment-row td,
tr.payment td { background: #fbfbf8; }
.event-row td,
tr.interest td {
  background: #fff;
  color: #272727;
}
.event-title { font-weight: 700; }
.event-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.money-negative { color: var(--good); font-weight: 700; }
.debt-zero { color: var(--muted); }
.total-row td,
.total-row th,
tfoot td,
tfoot th {
  border-top: 2px solid var(--ink);
  font-weight: 750;
  background: #fafafa;
}

.formula-block {
  margin-top: 28px;
  border-top: 1px solid var(--line);
  padding-top: 18px;
  color: #333;
  line-height: 1.55;
  font-size: 13.5px;
}
.formula-block h2 { margin-bottom: 8px; }
.formula-block p { margin: 0 0 10px; }

@media (max-width: 1380px) {
  .app-shell { width: min(1280px, calc(100vw - 40px)); }
  .layout { grid-template-columns: 400px minmax(0, 1fr); }
  .summary-cards { grid-template-columns: 1fr; }
  .summary-card { min-height: 92px; }
}

@media (max-width: 1040px) {
  .layout { grid-template-columns: 1fr; }
  .input-panel { position: static; }
  .summary-cards { grid-template-columns: 1fr; }
}

@media (max-width: 620px) {
  .app-shell { width: calc(100vw - 28px); padding: 20px 0 32px; }
  .topbar { align-items: flex-start; flex-direction: column; gap: 14px; }
  .top-actions { width: 100%; }
  .top-actions .ghost { flex: 1; }
  .input-panel,
  .result-panel { padding: 16px; }
  .dates-grid,
  .two-col,
  .date-grid,
  .payment-fields { grid-template-columns: 1fr; }
  .card-value { font-size: 24px; }
  .calc-table { min-width: 980px; }
}

@media print {
  body { background: #fff; }
  .app-shell { width: auto; max-width: none; padding: 0; }
  .input-panel,
  .top-actions,
  .formula-block { display: none !important; }
  .topbar { border-bottom: 1px solid #000; margin-bottom: 12px; }
  .layout { display: block; }
  .result-panel { box-shadow: none; border: 0; padding: 0; }
  .summary-cards { grid-template-columns: 1fr 1fr 1fr; }
  .summary-card { break-inside: avoid; min-height: 82px; }
  .table-wrap { overflow: visible; border: 0; }
  .calc-table { min-width: 0; }
  .calc-table th, .calc-table td { font-size: 10px; padding: 5px 6px; }
  .card-value { font-size: 20px; }
}

/* v5 refined */
.date-control {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 40px;
  align-items: stretch;
}
.date-control .date-input {
  grid-column: 1 / 3;
  padding-right: 48px;
}
.native-date-picker {
  position: absolute;
  z-index: 3;
  right: 0;
  top: 0;
  width: 40px !important;
  height: 40px !important;
  opacity: 0;
  cursor: pointer;
  padding: 0 !important;
  border: 0 !important;
}
.calendar-button {
  position: absolute;
  right: 0;
  top: 0;
  z-index: 2;
  width: 40px;
  height: 40px;
  border: 1px solid var(--line-strong);
  background: #f7f7f4;
  color: var(--accent);
  font-size: 18px;
  font-weight: 700;
  pointer-events: none;
}
.date-control:focus-within .calendar-button {
  border-color: #8d938e;
  background: #fff;
}

.summary-card {
  display: grid;
  grid-template-rows: 40px minmax(34px, auto) 18px;
  align-content: start;
  justify-content: stretch;
  gap: 8px;
}
.summary-card .card-label {
  min-height: 34px;
  margin-bottom: 0;
  display: flex;
  align-items: flex-start;
}
.summary-card .card-value {
  align-self: start;
}
.summary-card .card-sub {
  margin-top: 0;
}
.summary-card:not(.wide)::after {
  content: '';
  display: block;
}

.calc-table col.col-event { width: 25%; }
.calc-table col.col-procedure { width: 16%; }
.calc-table col.col-days { width: 7%; }
.calc-table col.col-year-days { width: 9%; }
.calc-table col.col-rate { width: 8%; }
.calc-table col.col-interest { width: 17%; }
.calc-table col.col-balance { width: 18%; }

.total-row td:first-child {
  text-align: left;
}
.total-row td:nth-child(6),
.total-row td:nth-child(7) {
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.danger-plain {
  color: var(--danger);
  border-color: var(--line-strong);
}

@media (max-width: 1380px) {
  .summary-card { grid-template-rows: auto auto 18px; }
  .summary-card .card-label { min-height: 0; }
}


/* v5 word export + footer alignment */
.calc-table td:first-child,
.calc-table td:nth-child(2) {
  width: auto;
}
.calc-table th:nth-child(3),
.calc-table th:nth-child(4),
.calc-table th:nth-child(5),
.calc-table th:nth-child(6),
.calc-table th:nth-child(7),
.calc-table td:nth-child(3),
.calc-table td:nth-child(4),
.calc-table td:nth-child(5),
.calc-table td:nth-child(6),
.calc-table td:nth-child(7) {
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.total-row td {
  vertical-align: middle;
}
.total-row td:first-child {
  text-align: left;
  white-space: nowrap;
}
.total-row td:nth-child(6),
.total-row td:nth-child(7) {
  text-align: right !important;
  white-space: nowrap;
}
.top-actions .ghost {
  white-space: nowrap;
}


/* v5 printfix */
.principal-label {
  font-size: 15px;
  line-height: 1.25;
  font-weight: 750;
  letter-spacing: -0.012em;
  margin-bottom: 10px;
  color: var(--ink);
}
.rate-value {
  font-size: 13px;
  line-height: 1.25;
  white-space: normal;
}
.payment-card {
  border: 1px solid var(--line);
  padding: 12px;
  background: #fff;
}
.payment-row td {
  background: #fbfbf8;
}

@page {
  size: A4 landscape;
  margin: 10mm;
}
@media print {
  html, body {
    background: #fff !important;
  }
  body.print-clean {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  .topbar {
    margin-top: 0;
  }
}

/* v5 tablefit: таблица без горизонтального скроллинга */
.app-shell {
  width: min(1960px, calc(100vw - 48px));
}

.layout {
  grid-template-columns: minmax(360px, 420px) minmax(0, 1fr);
  gap: 26px;
}

.result-panel {
  padding: 22px;
  overflow: hidden;
}

.table-wrap {
  overflow-x: visible;
  width: 100%;
}

.calc-table {
  width: 100%;
  min-width: 0;
  table-layout: fixed;
}

.calc-table th,
.calc-table td {
  padding: 7px 7px;
  overflow-wrap: anywhere;
  word-break: normal;
}

.calc-table th {
  font-size: 9.2px;
  line-height: 1.18;
  white-space: normal;
  letter-spacing: 0.035em;
  vertical-align: middle;
}

.calc-table td {
  font-size: 11.2px;
  line-height: 1.25;
}

.calc-table col.col-event { width: 22%; }
.calc-table col.col-procedure { width: 14%; }
.calc-table col.col-days { width: 6%; }
.calc-table col.col-year-days { width: 7%; }
.calc-table col.col-rate { width: 7%; }
.calc-table col.col-interest { width: 20%; }
.calc-table col.col-balance { width: 24%; }

.calc-table th:nth-child(3),
.calc-table th:nth-child(4),
.calc-table th:nth-child(5),
.calc-table th:nth-child(6),
.calc-table th:nth-child(7),
.calc-table td:nth-child(3),
.calc-table td:nth-child(4),
.calc-table td:nth-child(5),
.calc-table td:nth-child(6),
.calc-table td:nth-child(7) {
  text-align: right;
}

.calc-table td:nth-child(6),
.calc-table td:nth-child(7),
.total-row td:nth-child(6),
.total-row td:nth-child(7) {
  white-space: nowrap;
  overflow-wrap: normal;
}

.event-title,
.event-sub {
  overflow-wrap: anywhere;
}

.total-row td {
  padding-top: 9px;
  padding-bottom: 9px;
}

@media (max-width: 1540px) {
  .app-shell { width: calc(100vw - 36px); }
  .layout { grid-template-columns: minmax(340px, 390px) minmax(0, 1fr); gap: 22px; }
  .result-panel { padding: 18px; }
  .calc-table th { font-size: 8.7px; padding: 6px 5px; }
  .calc-table td { font-size: 10.6px; padding: 6px 5px; }
  .calc-table col.col-event { width: 22%; }
  .calc-table col.col-procedure { width: 13%; }
  .calc-table col.col-days { width: 6%; }
  .calc-table col.col-year-days { width: 7%; }
  .calc-table col.col-rate { width: 7%; }
  .calc-table col.col-interest { width: 20%; }
  .calc-table col.col-balance { width: 25%; }
}

@media (max-width: 1240px) {
  .layout { grid-template-columns: 1fr; }
  .input-panel { position: static; }
  .summary-cards { grid-template-columns: 1fr 1fr 1fr; }
  .calc-table th { font-size: 8.5px; }
  .calc-table td { font-size: 10.4px; }
}

@media (max-width: 760px) {
  .app-shell { width: calc(100vw - 20px); }
  .result-panel { padding: 12px; }
  .summary-cards { grid-template-columns: 1fr; }
  .calc-table th { font-size: 7.5px; padding: 5px 3px; letter-spacing: 0.01em; }
  .calc-table td { font-size: 9.2px; padding: 5px 3px; }
  .calc-table col.col-event { width: 22%; }
  .calc-table col.col-procedure { width: 12%; }
  .calc-table col.col-days { width: 6%; }
  .calc-table col.col-year-days { width: 7%; }
  .calc-table col.col-rate { width: 7%; }
  .calc-table col.col-interest { width: 21%; }
  .calc-table col.col-balance { width: 25%; }
  .calc-table td:nth-child(6),
  .calc-table td:nth-child(7),
  .total-row td:nth-child(6),
  .total-row td:nth-child(7) { white-space: normal; }
}
