.page-invoice-settings {
  padding: 16px;
}
.invoice-settings-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  align-items: start;
}
.invoice-profile-panel,
.invoice-template-panel {
  width: 100%;
}
.panel.invoice-profile-panel h2 {
    margin-bottom: 12px;
}
.invoice-template-panel > h2 {
  margin-bottom: 12px;
}
.invoice-profile-form {
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 12px;
  align-items: end;
}
.invoice-profile-form .profile-span-1 {
  grid-column: span 1;
}
.invoice-profile-form .profile-span-2 {
  grid-column: span 2;
}
.invoice-profile-form .profile-span-4 {
  grid-column: span 4;
}
.invoice-profile-form .profile-span-5 {
  grid-column: span 5;
}
.invoice-profile-form .profile-normal-textarea textarea {
  min-height: 40px;
  height: 40px;
  resize: vertical;
}
.invoice-profile-form .profile-large-textarea textarea {
  min-height: 104px;
  height: 104px;
  resize: vertical;
}
.invoice-profile-list {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin-top: 14px;
}
.invoice-profile-card,
.invoice-template-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgb(255 255 255 / .74);
  padding: 12px;
  margin-bottom: 10px;
}
.invoice-profile-card {
  margin-bottom: 0;
}
.template-family {
  display: grid;
  gap: 10px;
}
.template-family + .template-family {
  margin-top: 18px;
  padding-top: 18px;
  border-top: 1px solid color-mix(in srgb, var(--line), transparent 30%);
}
.template-family-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.template-family-head > h3,
.template-family > h3 {
  margin: 0;
  color: var(--ink);
  font-size: 16px;
  font-weight: 850;
}
.template-family-head .button {
  min-height: 38px;
  gap: 6px;
}
.template-family-head .icon {
  width: 16px;
  height: 16px;
}
.invoice-template-card {
  padding: 0;
  overflow: hidden;
}
.invoice-template-card.is-disabled {
  opacity: .72;
}
.invoice-template-card summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 54px;
  padding: 12px 14px;
  cursor: pointer;
}
.template-card-title {
  display: grid;
  gap: 2px;
  min-width: 0;
}
.template-card-title strong {
  color: var(--ink);
  font-size: 16px;
  line-height: 1.2;
}
.template-card-title small {
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}
.template-card-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.template-settings-stack {
  display: grid;
  gap: 12px;
  padding: 0 14px 14px;
}
.template-settings-group {
  border: 1px solid color-mix(in srgb, var(--line), transparent 26%);
  border-radius: 8px;
  background: rgb(247 252 255 / .52);
  padding: 12px;
}
.template-settings-group h3 {
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: .04em;
  text-transform: uppercase;
}
.template-disable-field {
  justify-content: start;
  min-height: 40px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgb(255 255 255 / .68);
}
.template-disable-field span {
  font-size: 13px;
}
.template-settings-grid-tight {
  grid-template-columns: minmax(180px, 240px) minmax(170px, auto);
  justify-content: start;
}
.invoice-template-card > .row-actions {
  padding: 0 14px 14px;
}
.invoice-profile-preview {
  border: 1px dashed var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #f7fcff;
  margin-top: 14px;
}
.profile-logo-field {
  min-height: 68px;
}
.profile-logo-field input[type="file"] {
  height: auto;
  min-height: 38px;
  padding: 8px;
}
.profile-logo-field small {
  font-size: 11px;
  font-weight: 650;
}
.profile-logo-preview {
  grid-column: span 2;
  min-height: 68px;
  display: grid;
  place-items: center;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: rgb(247 252 255 / .72);
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
  padding: 8px;
}
.profile-logo-preview img {
  max-width: 100%;
  max-height: 54px;
  object-fit: contain;
}
.paper-size-manager {
  display: grid;
  gap: 12px;
  border: 1px solid color-mix(in srgb, var(--line), transparent 20%);
  border-radius: 8px;
  background: rgb(247 252 255 / .56);
  padding: 12px;
  margin-bottom: 16px;
}
.paper-size-manager h3 {
  margin: 0;
  color: var(--ink);
  font-size: 16px;
  font-weight: 850;
}
.paper-size-manager p {
  margin: 3px 0 0;
}
.paper-size-form {
  display: grid;
  grid-template-columns: minmax(190px, 1.2fr) minmax(110px, .7fr) minmax(110px, .7fr) auto;
  gap: 10px;
  align-items: end;
}
.paper-size-form .button {
  min-height: 40px;
  white-space: nowrap;
}
.paper-size-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.paper-size-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgb(255 255 255 / .76);
  padding: 6px 8px 6px 12px;
}
.paper-size-chip strong {
  font-size: 13px;
  min-width: 0;
}
.paper-size-chip small {
  color: var(--muted);
  font-size: 11px;
  font-weight: 750;
  min-width: 0;
}
.paper-size-chip .icon-button.tiny {
  flex: 0 0 28px;
  width: 28px;
  height: 28px;
  min-height: 28px;
  border-radius: 999px;
  color: var(--danger);
}
.paper-size-chip .icon {
  width: 14px;
  height: 14px;
}
.invoice-settings-grid textarea {
  min-height: 78px;
}
:root[data-mode="night"] .invoice-profile-card,
:root[data-mode="night"] .invoice-template-card,
:root[data-mode="night"] .paper-size-manager,
:root[data-mode="night"] .template-settings-group,
:root[data-mode="night"] .template-disable-field,
:root[data-mode="night"] .invoice-profile-preview,
:root[data-mode="night"] .profile-logo-preview,
:root[data-mode="night"] .paper-size-chip {
  background: rgb(18 16 15 / .9);
  border-color: rgb(255 136 74 / .24);
}
:root[data-mode="night"] .paper-size-manager,
:root[data-mode="night"] .invoice-profile-preview {
  color: #f5eee8;
}
@media (max-width: 1180px) {
  .invoice-profile-form {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }
  .invoice-profile-form .profile-span-1,
  .invoice-profile-form .profile-span-2 {
    grid-column: span 2;
  }
  .invoice-profile-form .profile-span-5,
  .invoice-profile-form .profile-span-4 {
    grid-column: span 3;
  }
  .profile-logo-preview {
    grid-column: span 3;
  }
  .invoice-profile-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .paper-size-form {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  .paper-size-form .button {
    grid-column: span 3;
  }
}
@media (max-width: 720px) {
  .invoice-profile-form,
  .invoice-profile-list,
  .template-settings-grid,
  .template-settings-grid-tight,
  .paper-size-form {
    grid-template-columns: 1fr;
  }
  .invoice-profile-form .profile-span-1,
  .invoice-profile-form .profile-span-2,
  .invoice-profile-form .profile-span-4,
  .invoice-profile-form .profile-span-5,
  .profile-logo-preview,
  .paper-size-form .button {
    grid-column: span 1;
  }
  .template-family-head {
    align-items: stretch;
    flex-direction: column;
  }
  .invoice-template-card summary {
    align-items: flex-start;
    flex-direction: column;
  }
}

.invoice-profile-form {
  grid-template-columns: repeat(10, minmax(0, 1fr));
}

.invoice-profile-card {
  position: relative;
  padding: 14px;
}

.profile-card-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 10px;
}

.profile-default-actions,
.profile-icon-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.invoice-profile-card .button.compact {
  font-size: 13px;
  font-weight: 720;
  line-height: 1.05;
}

.profile-icon-actions .icon-button {
  width: 38px;
  height: 38px;
  min-height: 38px;
}

.profile-default-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  border-radius: 999px;
  background: #dcfce7;
  color: #15803d;
  padding: 4px 9px;
  font-size: 11px;
  font-weight: 850;
}

.invoice-profile-card.is-default {
  border-color: #86efac;
  box-shadow: 0 10px 22px rgb(22 163 74 / .08);
}

/* Final mobile invoice profile form rule: two compact columns, no label/input overlap. */
@media (max-width: 720px) {
  .page-invoice-settings {
    padding: 12px;
  }

  .invoice-profile-form {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 10px !important;
    align-items: end !important;
  }

  .invoice-profile-form .profile-span-1,
  .invoice-profile-form .profile-span-2,
  .invoice-profile-form .profile-span-4,
  .invoice-profile-form .profile-span-5,
  .profile-logo-preview {
    grid-column: span 1 !important;
    min-width: 0 !important;
  }

  .invoice-profile-form label,
  .invoice-profile-form input,
  .invoice-profile-form select,
  .invoice-profile-form textarea {
    min-width: 0 !important;
    width: 100% !important;
  }

  .invoice-profile-form label > span:first-child {
    display: block;
    min-height: 32px;
    line-height: 1.15;
    overflow-wrap: anywhere;
  }
}

.default-profile-button.active {
  background: #dcfce7;
  border-color: #86efac;
  color: #15803d;
}

.profile-logo-preview {
  min-height: 40px;
  height: 40px;
  align-self: end;
}

.profile-logo-preview img {
  max-height: 84px;
}

.profile-logo-preview:has(img) {
  min-height: 96px;
  height: auto;
}

.invoice-profile-preview {
  display: none;
}

.invoice-profile-save-action {
  min-height: 40px;
  border-color: #1f2937;
  background: #172331;
  color: #fff;
}

.invoice-profile-save-action:disabled {
  opacity: .55;
  cursor: not-allowed;
}

.invoice-profile-save-action:not(:disabled) {
  border-color: #2f6fdd;
  background: #2f6fdd;
  box-shadow: 0 12px 28px rgb(47 111 221 / .18);
}

.invoice-profile-form input,
.invoice-profile-form textarea,
.invoice-profile-form .document-picker-button {
  min-height: 44px;
}

.invoice-profile-form .document-picker-button {
  height: 44px;
  padding-block: 0;
}

.template-title-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.template-title-row strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.template-card-meta {
  justify-content: flex-start;
}

.template-card-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.template-card-actions .icon-button {
  width: 40px;
  height: 40px;
  min-height: 40px;
}

.template-card-actions .icon-button.primary {
  background: #2f6fdd;
  border-color: #2f6fdd;
  color: #fff;
}

.template-card-actions .icon-button.primary:disabled {
  opacity: .42;
  cursor: not-allowed;
}

.template-card-actions .icon-button.danger {
  background: #fee2e2;
  border-color: #fecaca;
  color: #b91c1c;
}

.badge.badge-paper {
  background: #fef3c7;
  color: #92400e;
}

.badge.badge-type-invoice {
  background: #dbeafe;
  color: #1d4ed8;
}

.badge.badge-type-chalan {
  background: #e0f2fe;
  color: #0369a1;
}

.template-details-row {
  grid-template-columns: minmax(0, 3fr) minmax(0, 3fr) minmax(0, 3fr) minmax(120px, 1fr);
}

.template-settings-grid.template-details-row > * {
  min-width: 0;
}

.document-toggle-field {
  align-items: center;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 7px;
  min-height: 78px;
  position: relative;
}

.document-toggle-field input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.document-toggle-field i {
  width: 46px;
  height: 24px;
  border-radius: 999px;
  background: #dcfce7;
  border: 1px solid #86efac;
  position: relative;
}

.document-toggle-field i::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 999px;
  background: #16a34a;
  transition: transform .16s ease, background .16s ease;
}

.document-toggle-field input:checked + i {
  background: #fee2e2;
  border-color: #fecaca;
}

.document-toggle-field input:checked + i::after {
  background: #dc2626;
  transform: translateX(21px);
}

.invoice-settings-grid textarea[name="templateHtml"],
.invoice-settings-grid textarea[name="templateCss"] {
  min-height: 190px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.paper-size-manager {
  position: relative;
  padding: 10px;
  border: 1px solid color-mix(in srgb, var(--line), transparent 15%);
  background: rgb(247 252 255 / .5);
}

.paper-size-manager::after {
  content: none;
}

.paper-size-summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  list-style: none;
  cursor: pointer;
}

.paper-size-summary::-webkit-details-marker {
  display: none;
}

.paper-size-summary > span {
  display: grid;
  gap: 2px;
}

.paper-size-summary strong {
  color: var(--ink);
  font-size: 16px;
  font-weight: 850;
}

.paper-size-summary small {
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

.paper-size-summary .icon-button {
  pointer-events: none;
  width: 38px;
  height: 38px;
  min-height: 38px;
  border-radius: 8px;
}

.paper-size-manager:not([open]) .paper-size-body,
.paper-size-manager:not([open]) .paper-open,
.paper-size-manager[open] .paper-closed {
  display: none;
}

.paper-size-body {
  display: grid;
  gap: 12px;
}

.paper-size-chip .icon-button.tiny:not(.danger) {
  color: var(--ink);
}

:root[data-mode="night"] .template-card-actions .icon-button.primary {
  background: #ff6a2a;
  border-color: #ff8a3d;
}

:root[data-mode="night"] .template-card-actions .icon-button.danger,
:root[data-mode="night"] .default-profile-button.active,
:root[data-mode="night"] .profile-default-badge {
  box-shadow: none;
}

:root[data-mode="night"] .document-toggle-field i {
  background: rgb(22 101 52 / .45);
  border-color: rgb(74 222 128 / .42);
}

:root[data-mode="night"] .document-toggle-field input:checked + i {
  background: rgb(127 29 29 / .5);
  border-color: rgb(248 113 113 / .48);
}

@media (max-width: 1180px) {
  .template-details-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 720px) {
  .template-title-row,
  .invoice-template-card summary {
    align-items: flex-start;
  }
  .template-title-row {
    flex-direction: column;
  }
  .template-card-actions {
    margin-left: 0;
  }
  .template-details-row {
    grid-template-columns: 1fr;
  }
}
