:root {
      --ink: #111;
      --muted: #666;
      --line: #c8c8c8;
      --field: #f4f8ff;
      --field-border: #8aa4d6;
      --page-w: 210mm;
      --page-h: 297mm;
      --pad: 12mm 14mm 12mm;
      --side-rail: 148px;
      --chrome-x: calc(24px + 24px + 2 * var(--side-rail));
      /* Только визуальный масштаб: вёрстка страницы всегда A4 */
      --page-scale: min(1, calc((100vw - var(--chrome-x)) / var(--page-w)));
      --page-slot-w: calc(var(--page-w) * var(--page-scale));
      --font: "Times New Roman", Times, "Liberation Serif", serif;
    }

    * { box-sizing: border-box; }

    body {
      margin: 0;
      background: #e8e8e8;
      color: var(--ink);
      font-family: var(--font);
      font-size: 11pt;
      line-height: 1.2;
    }

    .workspace {
      display: grid;
      grid-template-columns:
        minmax(var(--side-rail), 1fr)
        var(--page-slot-w)
        minmax(var(--side-rail), 1fr);
      column-gap: 12px;
      align-items: start;
      width: 100%;
      max-width: 100%;
      margin: 0;
      padding: 24px 12px 48px;
      box-sizing: border-box;
    }

    .workspace-side {
      position: sticky;
      top: 24px;
      display: flex;
      align-items: flex-start;
      gap: 12px;
      min-width: 0;
    }

    .workspace-side-start {
      grid-column: 1;
      justify-self: end;
      align-items: flex-end;
    }

    .workspace-side-end {
      grid-column: 3;
      justify-self: start;
      align-items: flex-start;
      flex-direction: column;
      justify-content: space-between;
      align-self: start;
      box-sizing: border-box;
      height: calc(100dvh - 36px);
      max-height: calc(100dvh - 36px);
    }

    .actions-feedback {
      margin-top: auto;
    }

    .actions-feedback .icon-btn img {
      opacity: 0.3;
    }

    .actions a.icon-btn {
      color: inherit;
      text-decoration: none;
      background: #fff;
      border: 1px solid transparent;
      border-radius: 8px;
      box-sizing: border-box;
      box-shadow: 0 2px 10px rgba(0,0,0,.18);
    }

    .actions a.icon-btn:hover {
      background: #f3f5f8;
    }

    .tpl-panel {
      position: fixed;
      left: 0;
      top: 0;
      bottom: 0;
      z-index: 5;
      width: 132px;
      max-height: none;
      display: flex;
      flex-direction: column;
      gap: 4px;
      padding: 20px 14px;
      margin: 0;
      border: 0;
      border-radius: 0;
      background: #dcdcdc;
      box-shadow: none;
      font-family: system-ui, -apple-system, sans-serif;
      font-size: 13px;
      color: #1f2430;
      overflow: auto;
      transform: translateX(0);
      transition: transform 0.32s cubic-bezier(0.4, 0, 0.2, 1);
      will-change: transform;
    }

    .tpl-panel.is-collapsed {
      transform: translateX(-100%);
      pointer-events: none;
    }

    .tpl-add {
      margin: 0;
      padding: 0;
      border: 0;
      border-radius: 8px;
      background: rgba(255, 255, 255, 0.45);
      color: #1f2430;
      box-shadow: none;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
    }

    .tpl-add svg {
      display: block;
    }

    .tpl-add:hover {
      background: rgba(255, 255, 255, 0.72);
    }

    .tpl-add-inpanel {
      flex: 0 0 auto;
      align-self: stretch;
      width: 100%;
      height: 40px;
    }

    .tpl-add-float {
      position: fixed;
      z-index: 6;
      top: 24px;
      left: 24px;
      width: 40px;
      height: 40px;
      background: #fff;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.14);
      transition: background 0.15s ease;
    }

    .tpl-add-float:hover {
      background: #f3f4f6;
    }

    .tpl-add[hidden] {
      display: none !important;
    }

    .actions button {
      border: 0;
      border-radius: 8px;
      padding: 8px 12px;
      font: inherit;
      font-weight: 600;
      cursor: pointer;
      background: #fff;
      color: #1f2430;
      box-shadow: 0 1px 4px rgba(0,0,0,.1);
      white-space: nowrap;
    }

    .actions button.primary {
      background: #3b82f6;
      color: #fff;
    }

    .actions button:disabled {
      opacity: 0.45;
      cursor: default;
    }

    .tpl-list {
      list-style: none;
      margin: 0;
      padding: 0;
      overflow: visible;
      display: flex;
      flex-direction: column;
      gap: 4px;
      min-height: 0;
      user-select: none;
    }

    .tpl-list li button {
      width: 100%;
      margin: 0;
      padding: 8px 10px;
      border: 0;
      border-radius: 8px;
      background: transparent;
      box-shadow: none;
      display: flex;
      flex-direction: column;
      align-items: stretch;
      justify-content: flex-start;
      gap: 2px;
      text-align: left;
      font-weight: 500;
      white-space: normal;
      cursor: pointer;
      color: #1f2430;
    }

    .tpl-list li button:hover {
      background: rgba(255, 255, 255, 0.45);
    }

    .tpl-list li button.active,
    .tpl-list li button.selected {
      background: rgba(255, 255, 255, 0.72);
      color: #1f2430;
      box-shadow: none;
    }

    .tpl-list li button.active:hover,
    .tpl-list li button.selected:hover {
      background: rgba(255, 255, 255, 0.85);
    }

    .tpl-list .tpl-title {
      font-family: system-ui, -apple-system, sans-serif;
      font-size: 13px;
      font-weight: 600;
      line-height: 1.25;
    }

    .tpl-list .tpl-meta {
      font-size: 11px;
      color: #6b7280;
      font-weight: 500;
    }

    .tpl-list li button.active .tpl-meta,
    .tpl-list li button.selected .tpl-meta {
      color: #64748b;
    }

    .tpl-dirty::after {
      content: " •";
      color: #e11d48;
    }

    .tpl-ctx {
      position: fixed;
      z-index: 1000;
      min-width: 140px;
      margin: 0;
      padding: 4px;
      border: 0;
      border-radius: 8px;
      background: #fff;
      box-shadow: 0 8px 28px rgba(0, 0, 0, 0.18);
      font-family: system-ui, -apple-system, sans-serif;
      font-size: 13px;
    }

    .tpl-ctx[hidden] {
      display: none !important;
    }

    .tpl-ctx button {
      display: block;
      width: 100%;
      margin: 0;
      padding: 8px 12px;
      border: 0;
      border-radius: 6px;
      background: transparent;
      color: #1f2430;
      font: inherit;
      font-weight: 500;
      text-align: left;
      cursor: pointer;
    }

    .tpl-ctx button:hover {
      background: #f2f3f5;
    }

    .tpl-ctx button.danger {
      color: #c00;
    }

    .tpl-ctx button.danger:hover {
      background: #fdecec;
    }

    .tpl-ctx button:disabled {
      opacity: 0.4;
      cursor: default;
    }

    .tpl-ctx button:disabled:hover {
      background: transparent;
    }

    .actions {
      flex: 0 0 auto;
      display: flex;
      flex-direction: column;
      align-items: stretch;
      gap: 8px;
      font-family: system-ui, -apple-system, sans-serif;
      font-size: 13px;
    }

    .actions button {
      box-shadow: 0 2px 10px rgba(0,0,0,.18);
    }

    .actions.actions-end,
    .actions.actions-start {
      align-items: flex-end;
    }

    .workspace-side-end .actions {
      align-items: flex-start;
    }

    .actions .icon-btn {
      position: relative;
      width: 40px;
      height: 40px;
      padding: 0;
      display: inline-flex;
      align-items: center;
      justify-content: center;
    }

    .actions .icon-btn[data-tip]::after {
      --tip-tongue: 11px;
      content: attr(data-tip);
      position: absolute;
      right: calc(100% + 10px);
      top: 50%;
      transform: translateY(-50%);
      z-index: 20;
      padding: 5px 8px;
      padding-right: calc(8px + var(--tip-tongue));
      border-radius: 8px 0 0 8px;
      background: #fff;
      color: #3a4050;
      font-family: system-ui, -apple-system, sans-serif;
      font-size: 12px;
      font-weight: 500;
      line-height: 1.2;
      white-space: nowrap;
      pointer-events: none;
      opacity: 0;
      visibility: hidden;
      -webkit-mask-image:
        linear-gradient(#000 0 0),
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 24' preserveAspectRatio='none'%3E%3Cpath fill='%23000' d='M0 0c4.8 1.4 9.2 5.4 11.3 11.1.25.5.25 1.3 0 1.8C9.2 18.6 4.8 22.6 0 24z'/%3E%3C/svg%3E");
      mask-image:
        linear-gradient(#000 0 0),
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 24' preserveAspectRatio='none'%3E%3Cpath fill='%23000' d='M0 0c4.8 1.4 9.2 5.4 11.3 11.1.25.5.25 1.3 0 1.8C9.2 18.6 4.8 22.6 0 24z'/%3E%3C/svg%3E");
      -webkit-mask-size: calc(100% - var(--tip-tongue) + 1px) 100%, var(--tip-tongue) 100%;
      mask-size: calc(100% - var(--tip-tongue) + 1px) 100%, var(--tip-tongue) 100%;
      -webkit-mask-position: 0 0, 100% 0;
      mask-position: 0 0, 100% 0;
      -webkit-mask-repeat: no-repeat;
      mask-repeat: no-repeat;
      -webkit-mask-composite: source-over;
      mask-composite: add;
      filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.14));
    }

    .actions .icon-btn[data-tip]:hover::after,
    .actions .icon-btn[data-tip]:focus-visible::after {
      opacity: 1;
      visibility: visible;
    }

    .workspace-side-end .actions .icon-btn[data-tip]::after {
      right: auto;
      left: calc(100% + 10px);
      padding-right: 8px;
      padding-left: calc(8px + var(--tip-tongue));
      border-radius: 0 8px 8px 0;
      -webkit-mask-image:
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 24' preserveAspectRatio='none'%3E%3Cpath fill='%23000' d='M12 0C7.2 1.4 2.8 5.4.7 11.1.45 11.6.45 12.4.7 12.9 2.8 18.6 7.2 22.6 12 24z'/%3E%3C/svg%3E"),
        linear-gradient(#000 0 0);
      mask-image:
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 24' preserveAspectRatio='none'%3E%3Cpath fill='%23000' d='M12 0C7.2 1.4 2.8 5.4.7 11.1.45 11.6.45 12.4.7 12.9 2.8 18.6 7.2 22.6 12 24z'/%3E%3C/svg%3E"),
        linear-gradient(#000 0 0);
      -webkit-mask-size: var(--tip-tongue) 100%, calc(100% - var(--tip-tongue) + 1px) 100%;
      mask-size: var(--tip-tongue) 100%, calc(100% - var(--tip-tongue) + 1px) 100%;
      -webkit-mask-position: 0 0, var(--tip-tongue) 0;
      mask-position: 0 0, var(--tip-tongue) 0;
    }

    .actions .icon-btn svg,
    .actions .icon-btn img {
      display: block;
      width: 18px;
      height: 18px;
    }

    .actions .icon-btn .icon-edit,
    .actions .icon-btn .icon-save {
      display: none;
    }

    .actions .icon-btn[data-mode="edit"] .icon-edit,
    .actions .icon-btn[data-mode="save"] .icon-save {
      display: block;
    }

    .actions .icon-btn[hidden] {
      display: none;
    }

    .actions .icon-btn.btn-cancel {
      background: #fce8ec;
      color: #c43b55;
    }

    .actions .icon-btn.btn-cancel:hover {
      background: #f8d0d8;
      color: #a82f46;
    }

    .block[contenteditable="true"] {
      outline: none;
    }

    /* Режим правки шаблона: пункты работ — обычный текст («- …») */
    .sheets.tpl-editing .add-work,
    .sheets.tpl-editing .btn-icon {
      display: none !important;
    }

    .sheet-wrap {
      grid-column: 2;
      width: var(--page-slot-w);
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      gap: 0;
      overflow: hidden;
    }

    .page {
      width: var(--page-w);
      height: var(--page-h);
      max-height: var(--page-h);
      margin: 0;
      margin-bottom: calc(var(--page-h) * (var(--page-scale) - 1) + 20px * var(--page-scale));
      padding: var(--pad);
      background: #fff;
      box-shadow: 0 8px 28px rgba(0,0,0,.18);
      position: relative;
      overflow: hidden;
      font-size: 11pt;
      transform: scale(var(--page-scale));
      transform-origin: top left;
    }

    .page:last-child {
      margin-bottom: calc(var(--page-h) * (var(--page-scale) - 1));
    }

    .page-number {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 7mm;
      text-align: center;
      font-size: 10pt;
      line-height: 1;
      color: var(--muted);
      pointer-events: none;
      user-select: none;
    }

    h1,
    h1.flow {
      margin: 0 0 6px;
      text-align: center;
      font-size: 14pt;
      font-weight: 700;
      line-height: 1.15;
    }

    p,
    .flow {
      margin: 0 0 4px;
      text-align: left;
    }

    .works {
      margin: 0 0 4px 0;
      padding: 0;
      list-style: none;
    }

    .works li {
      display: flex;
      gap: 6px;
      align-items: flex-start;
      margin: 0 0 1px;
    }

    .works li::before {
      content: "•";
      flex: 0 0 auto;
      margin-top: .05em;
    }

    .works .item-row {
      flex: 1;
      display: flex;
      gap: 4px;
      align-items: flex-start;
    }

    .works .work-body {
      flex: 1;
      min-width: 0;
    }

    .parties {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 50px;
      margin-top: 22px;
    }

    .party h2 {
      margin: 0 0 3px;
      font-size: 11pt;
    }

    .party-text {
      margin: 0 0 4px;
      text-align: left;
      line-height: inherit;
    }

    .party-text textarea {
      display: block;
      width: 100%;
      min-width: 0;
    }

    .sign {
      margin-top: 10px;
      display: flex;
      align-items: flex-end;
      gap: 4px;
    }

    .sign .line {
      flex: 1;
      border-bottom: 1px solid #111;
      min-height: 1.1em;
    }

    input[type="text"],
    input[type="email"],
    input[type="tel"],
    textarea {
      width: 100%;
      box-sizing: border-box;
      margin: 0;
      padding: 0 0 1px;
      border: none;
      border-radius: 0;
      background: color-mix(in srgb, var(--field) 55%, transparent);
      box-shadow: inset 0 -1px 0 var(--field-border);
      color: inherit;
      font: inherit;
      font-weight: inherit;
      line-height: inherit;
      resize: vertical;
      outline: none;
    }

    input:focus,
    textarea:focus {
      background: #eaf1ff;
      box-shadow: inset 0 -1px 0 #3b82f6;
    }

    textarea {
      min-height: 1.15em;
      field-sizing: content;
      resize: none;
      overflow: hidden;
    }

    .party textarea {
      width: 100%;
      min-width: 0;
    }

    .party input {
      width: auto;
      max-width: 100%;
      min-width: 1ch;
    }

    .party input.inline {
      display: inline-block;
      width: max-content;
      max-width: none;
      box-sizing: border-box;
      field-sizing: content;
      padding: 0;
      vertical-align: baseline;
    }

    input[type="text"].inline,
    input[type="email"].inline,
    input[type="tel"].inline {
      display: inline-block;
      width: max-content;
      min-width: 1ch;
      max-width: none;
      box-sizing: border-box;
      field-sizing: content;
      padding: 0;
      vertical-align: baseline;
    }

    .keep-line {
      white-space: nowrap;
    }

    .fit-mirror {
      position: absolute;
      visibility: hidden;
      white-space: pre;
      height: 0;
      overflow: hidden;
      pointer-events: none;
    }

    input::placeholder,
    textarea::placeholder {
      color: #9aa3b2;
      opacity: 1;
    }

    .grow {
      width: 100%;
      min-height: 1.35em;
    }

    /* До края строки — только визуально, на лету (не в markdown) */
    input.field-line-end {
      max-width: 100%;
      box-sizing: border-box;
    }

    .btn-icon {
      flex: 0 0 auto;
      border: 0;
      background: transparent;
      color: #888;
      cursor: pointer;
      font-size: 14px;
      line-height: 1;
      padding: 2px 4px;
      font-family: system-ui, sans-serif;
    }

    .btn-icon:hover { color: #c00; }

    .add-work {
      margin: 0 0 4px 14px;
      border: 1px dashed #999;
      background: #fafafa;
      border-radius: 4px;
      padding: 2px 8px;
      font-family: system-ui, sans-serif;
      font-size: 11px;
      cursor: pointer;
    }

    @media print {
      @page {
        size: A4;
        margin: 0;
      }

      body {
        background: #fff;
      }

      .actions,
      .tpl-panel,
      .tpl-add,
      .workspace-side,
      .add-work,
      .btn-icon {
        display: none !important;
      }

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

      .sheet-wrap {
        width: auto;
        gap: 0;
      }

      .page {
        width: 210mm;
        height: 297mm;
        max-height: 297mm;
        margin: 0;
        padding: 12mm 14mm 12mm;
        font-size: 11pt;
        transform: none;
        box-shadow: none;
        overflow: hidden;
        break-after: page;
        page-break-after: always;
      }

      .page:last-child {
        break-after: auto;
        page-break-after: auto;
      }

      input[type="text"],
      input[type="email"],
      input[type="tel"],
      textarea {
        border: none !important;
        background: transparent !important;
        box-shadow: none !important;
        padding: 0 !important;
        color: #000;
        print-color-adjust: exact;
        -webkit-print-color-adjust: exact;
      }

      input::placeholder,
      textarea::placeholder {
        color: transparent !important;
        opacity: 0 !important;
      }

      .works li::before { color: #000; }
    }
