.template-page {
  display: grid;
  gap: 36px;
}

.template-page [data-template-panel][hidden] {
  display: none !important;
}

.template-cards {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-4);
}

.template-card {
  display: flex;
  min-height: 220px;
  justify-content: space-between;
  align-items: flex-start;
  flex-direction: column;
}

.template-card-active {
  border-color: color-mix(in srgb, var(--color-primary) 35%, var(--color-border));
  box-shadow: 0 0 0 3px var(--color-primary-soft), var(--shadow-md);
}

.document-template-list {
  display: grid;
  gap: 16px;
  margin-top: 20px;
}

.document-template-card {
  padding: 0;
  overflow: clip;
  transition: background-color .18s ease, border-color .18s ease, box-shadow .18s ease;
}

.document-template-card:not([open]):hover,
.document-template-card:not([open]):focus-within {
  border-color: var(--color-border-strong);
  background: var(--color-surface-2);
  box-shadow: var(--shadow-md);
}

.document-template-card[open] {
  border-color: var(--color-border-strong);
  background: var(--color-surface);
  box-shadow: var(--shadow-md);
}

.document-template-card__summary {
  display: flex;
  min-height: 92px;
  align-items: center;
  justify-content: space-between;
  gap: 22px;
  padding: 18px 20px;
  cursor: pointer;
  list-style: none;
  transition: background-color .18s ease;
}

.document-template-card__summary::-webkit-details-marker {
  display: none;
}

.document-template-card__summary::marker {
  content: "";
}

.document-template-card:not([open]) > .document-template-card__summary:hover,
.document-template-card:not([open]) > .document-template-card__summary:focus-visible {
  background: transparent;
}

.document-template-card__summary:focus {
  outline: none;
}

.document-template-card__identity {
  display: grid;
  min-width: 0;
  gap: 7px;
}

.document-template-card__identity strong {
  color: var(--color-text);
  font-family: var(--font-display);
  font-size: clamp(1.2rem, 2vw, 1.45rem);
  line-height: 1.2;
}

.document-template-card__identity small {
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  line-height: 1.5;
}

.document-template-card__content {
  padding: clamp(20px, 3vw, 28px);
  border-top: 1px solid var(--color-divider);
}

.document-builder-form {
  display: grid;
  gap: 22px;
}

.document-builder-fields {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}

.document-builder-fields .field strong {
  margin-left: 3px;
}

.document-calculation-picker {
  min-width: 0;
  padding: 18px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-surface-2);
}

.document-calculation-picker__head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 14px;
}

.document-calculation-picker__head h3 {
  margin: 0 0 6px;
  font-size: var(--text-lg);
}

.document-calculation-picker__head p {
  margin: 0;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.document-calculation-picker__head > span {
  flex: 0 0 auto;
  color: var(--color-primary);
  font-size: var(--text-sm);
  font-weight: 700;
}

.document-calculation-options {
  display: grid;
  gap: 8px;
}

.document-calculation-option {
  display: grid;
  grid-template-columns: 18px 20px minmax(0, 1fr) 28px;
  align-items: center;
  gap: 11px;
  min-height: 54px;
  padding: 9px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  cursor: pointer;
  transition: background-color .18s ease, border-color .18s ease, box-shadow .18s ease, opacity .18s ease;
}

.document-report-section-option {
  grid-template-columns: 18px 20px minmax(0, 1fr) minmax(62px, auto);
  background: var(--color-primary-soft);
}

.document-calculation-option__fixed {
  display: grid;
  width: 18px;
  height: 18px;
  place-items: center;
  border-radius: 50%;
  background: var(--color-primary);
  color: var(--color-surface);
  font-size: .7rem;
  font-weight: 800;
}

.document-report-section-option__position {
  color: var(--color-primary);
  font-size: .72rem;
  font-weight: 800;
  text-align: right;
}

.document-calculation-option[hidden] {
  display: none;
}

.document-calculation-option:hover,
.document-calculation-option:focus-within {
  border-color: var(--color-border-strong);
  background: var(--color-primary-soft);
  box-shadow: var(--shadow-sm);
}

.document-calculation-option.is-dragging {
  opacity: .42;
  border-style: dashed;
  box-shadow: none;
}

.document-calculation-options.is-reordering .document-calculation-option:not(.is-dragging) {
  transition: transform .12s ease, background-color .18s ease, border-color .18s ease;
}

.document-calculation-option__drag {
  display: grid;
  width: 18px;
  gap: 3px;
  padding: 7px 2px;
  cursor: grab;
}

.document-calculation-option__drag:active {
  cursor: grabbing;
}

.document-calculation-option__drag i {
  display: block;
  height: 2px;
  border-radius: 999px;
  background: var(--color-text-muted);
}

.document-calculation-option input {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--color-primary);
}

.document-calculation-option > span {
  display: grid;
  min-width: 0;
  gap: 4px;
}

.document-calculation-option strong {
  overflow-wrap: anywhere;
}

.document-calculation-option small {
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.document-calculation-option__order {
  display: grid;
  width: 26px;
  height: 26px;
  place-items: center;
  border-radius: 50%;
  background: var(--color-primary-soft);
  color: var(--color-primary);
  font-size: .75rem;
  font-weight: 800;
}

.document-calculation-option__order[hidden] {
  visibility: hidden;
}

.document-calculation-order-status {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

.document-calculations-empty {
  margin: 0;
  padding: 16px;
  border: 1px dashed var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  text-align: center;
}

.document-calculations-empty[hidden] {
  display: none;
}

.document-builder-note {
  padding: 13px 15px;
  border-left: 3px solid var(--color-primary);
  background: var(--color-primary-soft);
  color: var(--color-text-muted);
  line-height: 1.6;
}

.document-template-placeholder {
  margin: 0;
}

.template-back-link {
  width: fit-content;
  color: var(--color-primary);
  font-size: var(--text-sm);
  font-weight: 700;
  text-decoration: none;
}

.template-back-link:hover {
  text-decoration: underline;
}

.template-panel {
  padding: clamp(var(--space-6), 4vw, var(--space-10));
}

.template-form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-4);
}

.template-binding-panel {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: minmax(220px, 0.65fr) minmax(0, 1fr);
  gap: 24px;
  margin-bottom: 8px;
  padding: 20px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-surface-2);
}

.template-binding-panel h3 {
  margin: 10px 0 7px;
}

.template-binding-panel p {
  margin: 0;
  color: var(--color-text-muted);
}

.template-binding-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

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

.field span {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--color-text);
}

.field strong {
  color: var(--color-primary);
}

.field input,
.field textarea,
.field select {
  min-height: 48px;
  width: 100%;
  border-radius: var(--radius-md);
  border: 1px solid var(--color-border);
  background: var(--color-surface-2);
  color: var(--color-text);
  padding: 0 14px;
  outline: none;
}

.field textarea {
  min-height: 116px;
  padding-block: 12px;
  resize: vertical;
}

.field input[readonly] {
  color: var(--color-text-muted);
  background: color-mix(in srgb, var(--color-surface-2) 80%, var(--color-border));
}

.field input:focus,
.field textarea:focus,
.field select:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-soft);
}

.template-form-actions {
  grid-column: 1 / -1;
  display: flex;
  gap: var(--space-3);
  flex-wrap: wrap;
  margin-top: var(--space-2);
}

.message-box {
  margin-bottom: var(--space-5);
  padding: var(--space-4) var(--space-5);
  border-radius: var(--radius-lg);
  border: 1px solid var(--color-border);
  line-height: 1.6;
}

.message-box a {
  color: var(--color-primary);
  text-decoration: underline;
}

.message-error {
  background: color-mix(in srgb, #b42318 8%, var(--color-surface));
  border-color: color-mix(in srgb, #b42318 24%, var(--color-border));
}

.message-success {
  background: color-mix(in srgb, #18794e 9%, var(--color-surface));
  border-color: color-mix(in srgb, #18794e 24%, var(--color-border));
}

.empty-template-state {
  padding: var(--space-8);
  border-radius: var(--radius-lg);
  border: 1px dashed var(--color-border);
  background: var(--color-surface-2);
}

.empty-template-state h3 {
  margin: 0 0 var(--space-3);
}

.empty-template-state p {
  margin: 0 0 var(--space-5);
  color: var(--color-text-muted);
}

.template-notes-section {
  display: grid;
  gap: 20px;
  scroll-margin-top: 110px;
}

.template-page .template-section-header {
  display: grid;
  width: 100%;
  max-width: none;
  gap: 0;
  align-items: stretch;
}

.template-page .template-section-header > div {
  width: 100%;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--color-divider);
}

.template-page .template-section-header .eyebrow + h2 {
  margin-top: 14px;
}

.template-page .template-section-header > p {
  width: 100%;
  max-width: none;
  margin: 8.4px 0 0;
  color: var(--color-text-muted);
}

.notes-builder-shell {
  display: grid;
  gap: 28px;
  padding: clamp(var(--space-6), 4vw, var(--space-10));
}

.notes-example-warning {
  padding: 14px 18px;
  border: 1px solid color-mix(in srgb, #b7791f 28%, var(--color-border));
  border-radius: calc(var(--radius-lg) / 1.5);
  background: color-mix(in srgb, #f6c453 10%, var(--color-surface));
  color: var(--color-text-muted);
  line-height: 1.6;
}

.notes-example-warning strong {
  color: var(--color-text);
}

.notes-tags-section,
.notes-preview-section {
  width: 100%;
  min-width: 0;
}

.notes-preview-section {
  padding-top: 28px;
  border-top: 1px solid var(--color-divider);
}

.result-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-4);
}

.result-header h3 {
  margin: 0;
  font-size: var(--text-lg);
}

.result-meta {
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.tag-panel {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
}

.tag-chip {
  min-height: 46px;
  padding: 0.8rem 1rem;
  border-radius: var(--radius-full);
  border: 1px solid var(--color-border);
  background: var(--color-surface-2);
  color: var(--color-text);
  font-size: var(--text-sm);
  font-weight: 700;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition:
    transform var(--transition),
    box-shadow var(--transition),
    background var(--transition),
    border-color var(--transition);
}

.tag-chip:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.tag-chip:focus-visible {
  outline: 3px solid var(--color-primary-soft);
  outline-offset: 2px;
}

.tag-chip.active {
  background: var(--color-primary);
  color: white;
  border-color: var(--color-primary);
}

.tag-chip-personal {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.tag-chip-personal span {
  padding: 2px 6px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--color-primary) 12%, transparent);
  color: var(--color-primary);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.tag-chip-personal.active span {
  background: rgba(255, 255, 255, 0.18);
  color: white;
}

.notes-manage-link {
  flex: 0 0 auto;
  color: var(--color-primary);
  font-size: var(--text-sm);
  font-weight: 750;
  text-decoration: none;
}

.notes-manage-link:hover {
  text-decoration: underline;
}

.result-box {
  border: 1px solid var(--color-border);
  background: var(--color-surface-2);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  color: var(--color-text);
  line-height: 1.7;
}

.notes-result-box {
  display: block;
  width: 100%;
  max-width: none;
  min-height: 420px;
  resize: vertical;
  font: inherit;
}

.notes-result-box:focus {
  border-color: var(--color-primary);
  outline: none;
  box-shadow: 0 0 0 3px var(--color-primary-soft);
}

.notes-builder-actions {
  display: flex;
  gap: var(--space-3);
  flex-wrap: wrap;
  margin-top: var(--space-4);
}

.personal-note-panel {
  display: grid;
  grid-template-columns: minmax(220px, 0.65fr) minmax(0, 1.35fr);
  gap: 28px;
  align-items: start;
  padding: clamp(var(--space-6), 4vw, var(--space-9));
}

.personal-note-intro h3 {
  margin: 12px 0 9px;
}

.personal-note-intro p {
  margin: 0;
  color: var(--color-text-muted);
  line-height: 1.6;
}

.personal-note-form {
  display: grid;
  grid-template-columns: minmax(180px, 0.42fr) minmax(0, 1fr);
  gap: 16px;
  align-items: end;
}

.personal-note-form input,
.personal-note-form textarea {
  background: var(--color-surface);
}

.personal-note-form .btn {
  width: fit-content;
}

@media (max-width: 920px) {
  .template-cards,
  .template-form-grid,
  .template-binding-panel,
  .template-binding-fields,
  .document-builder-fields,
  .personal-note-panel,
  .personal-note-form {
    grid-template-columns: 1fr;
  }

  .result-header {
    align-items: flex-start;
    flex-direction: column;
  }

  .document-calculation-picker__head {
    flex-direction: column;
  }
}

@media (max-width: 560px) {
  .document-template-card__summary {
    min-height: 82px;
    padding: 15px;
  }

  .document-template-card__identity small {
    display: none;
  }

}
