#modal {
  width: min(100% - 40px, 560px);
  max-height: min(90vh, 780px);
  max-height: min(90dvh, 780px);
  padding: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scroll-padding-top: 88px;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: var(--white);
  color: var(--ink);
  box-shadow: 0 26px 90px #192e2042;
}

#modal::backdrop {
  background: #14251b75;
  backdrop-filter: blur(5px);
}

body:has(#modal[open]) {
  overflow: hidden;
}

.modal-heading {
  position: sticky;
  z-index: 1;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 17px 26px;
  border-bottom: 1px solid var(--line);
  background: var(--white);
}

.modal-heading h2 {
  margin: 0;
  font-size: clamp(22px, 4vw, 27px);
  font-weight: 800;
  line-height: 1.13;
  letter-spacing: -.05em;
}

.modal-heading .icon-button {
  display: grid;
  flex: none;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--paper);
  color: var(--ink);
}

.modal-heading .icon-button:hover {
  border-color: transparent;
  background: var(--accent-soft);
  color: var(--accent-deep);
}

.modal-heading .icon-button svg {
  width: 18px;
  height: 18px;
}

.modal-body {
  padding: 23px 26px 26px;
}

.modal-body > p:first-child,
.modal-copy {
  margin: 0 0 15px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.5;
}

.modal-copy {
  margin: 0;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--paper);
}

#modal .stack,
.discussion-content .stack {
  display: grid;
  gap: 15px;
}

#modal .field,
.discussion-content .field {
  display: grid;
  min-width: 0;
  gap: 6px;
  font-size: 13px;
  font-weight: 750;
}

#modal .field :is(input:not([type='file']), textarea, select),
.discussion-content .field :is(textarea, select) {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--paper);
  color: var(--ink);
  font-size: 15px;
  font-weight: 500;
}

#modal .field :is(input, textarea)::placeholder,
.discussion-content .field textarea::placeholder {
  color: var(--muted);
}

#modal .field :is(input:not([type='file']), textarea, select):focus,
.discussion-content .field :is(textarea, select):focus {
  border-color: var(--accent);
  background: var(--white);
}

#modal .field textarea,
.discussion-content .field textarea {
  min-height: 88px;
  line-height: 1.45;
  resize: vertical;
}

#modal .field small,
#modal .check-field small {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
  line-height: 1.4;
  overflow-wrap: anywhere;
}

.form-row {
  display: grid;
  grid-template-columns: minmax(0, 1.8fr) minmax(0, 1fr);
  gap: 12px;
}

.check-field {
  display: flex;
  align-items: flex-start;
  gap: 11px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 13px;
  font-weight: 750;
  cursor: pointer;
}

.check-field:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.check-field:has(input:disabled) {
  background: var(--paper);
  cursor: default;
}

.check-field input {
  flex: none;
  width: 18px;
  height: 18px;
  margin: 1px 0 0;
  accent-color: var(--accent);
}

.check-field small {
  margin-top: 3px;
}

#modal .file-field {
  position: relative;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 14px;
  padding: 13px;
  border: 1px dashed var(--accent);
  border-radius: 12px;
  background: var(--paper);
  cursor: pointer;
}

#modal .file-field:hover {
  background: var(--accent-soft);
}

#modal .file-field:focus-within {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.file-field > input {
  position: absolute;
  inset: 0;
  width: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.file-field > .upload-icon {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent-deep);
}

.file-preview {
  width: 48px;
  height: 48px;
  border-radius: 10px;
  object-fit: cover;
}

.form-message {
  padding: 9px 11px;
  border-radius: 8px;
  background: #fff0ee;
  color: #8f302b;
  font-size: 13px;
  line-height: 1.4;
  overflow-wrap: anywhere;
}

.form-message:empty {
  display: none;
}

.form-message.good {
  background: #e9f5ef;
  color: #196245;
}

#modal .button {
  min-height: 44px;
  max-width: 100%;
}

.auth-buttons {
  display: grid;
  gap: 8px;
}

.auth-buttons .button {
  width: 100%;
}

.auth-sep {
  display: flex;
  align-items: center;
  gap: 11px;
  color: var(--muted);
  font-size: 12px;
}

.auth-sep::before,
.auth-sep::after {
  flex: 1;
  border-top: 1px solid var(--line);
  content: '';
}

.form-secondary,
.profile-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.form-secondary {
  justify-content: flex-end;
}

.profile-actions {
  justify-content: space-between;
}

.modal-post-title {
  margin: 0 0 17px;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--paper);
  font-size: 16px;
  font-weight: 800;
  letter-spacing: -.025em;
  overflow-wrap: anywhere;
}

.comment-thread:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.comment-post-description {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 13px;
  overflow-wrap: anywhere;
}

.comment-post-description summary {
  width: fit-content;
  min-height: 32px;
  padding: 6px 0;
  cursor: pointer;
  color: var(--accent-deep);
  font-weight: 750;
}

.comment-post-description p {
  margin: 3px 0 0;
  white-space: pre-wrap;
}

.comments {
  display: grid;
  gap: 13px;
}

.comments:not(:empty) {
  margin-bottom: 16px;
}

.comment {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.comment .avatar {
  width: 34px;
  height: 34px;
  font-size: 11px;
}

.comment > div:last-child {
  min-width: 0;
}

.comment strong {
  font-size: 13px;
  overflow-wrap: anywhere;
}

.comment time {
  display: inline-block;
  margin-left: 7px;
  color: var(--muted);
  font-size: 11px;
}

.comment p {
  margin: 2px 0 0;
  font-size: 14px;
  overflow-wrap: anywhere;
  white-space: pre-line;
}

.comment-status {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 13px;
}

.comment-status:empty {
  margin: 0;
}

.comment-more:not([hidden]) {
  margin-bottom: 14px;
}

.toast {
  position: fixed;
  z-index: 10;
  pointer-events: none;
  right: 24px;
  bottom: 24px;
  max-width: min(400px, calc(100% - 48px));
  padding: 12px 16px;
  border: 1px solid #345140;
  border-radius: 12px;
  background: var(--ink);
  color: var(--white);
  box-shadow: 0 9px 28px #142d1c30;
  font-size: 14px;
  font-weight: 600;
  overflow-wrap: anywhere;
}

@media (max-width: 660px) {
  #modal {
    inset: auto 8px max(8px, env(safe-area-inset-bottom));
    width: calc(100% - 16px);
    max-height: 90vh;
    max-height: 90dvh;
    margin: 0 auto;
    border-radius: 19px;
    scroll-padding-top: 76px;
  }

  .modal-heading {
    padding: 12px 16px;
  }

  .modal-body {
    padding: 16px;
  }

  #modal .field :is(input:not([type='file']), textarea, select) {
    font-size: 16px;
  }

  #modal[open]:has(.comment-thread) {
    position: fixed;
    inset: auto 0 var(--comment-viewport-offset, 0px);
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    width: 100%;
    max-width: none;
    height: min(80vh, 620px);
    height: min(80dvh, 620px, calc(var(--comment-viewport-height, 100dvh) - 8px));
    max-height: none;
    overflow: hidden;
    border-radius: 22px 22px 0 0;
    scroll-padding-top: 0;
  }

  #modal:has(.comment-thread)::backdrop {
    background: #14251b8c;
    backdrop-filter: none;
  }

  #modal:has(.comment-thread) .modal-heading {
    position: static;
    padding: 6px 14px 6px 16px;
  }

  #modal:has(.comment-thread) .modal-heading h2 {
    font-size: 21px;
  }

  #modal:has(.comment-thread) .modal-body {
    display: grid;
    grid-template-rows: minmax(0, 1fr) auto;
    min-height: 0;
    padding: 0;
  }

  #modal .comment-thread {
    min-height: 0;
    padding: 12px 16px 18px;
    overflow-y: auto;
    overscroll-behavior-y: contain;
    touch-action: pan-y pinch-zoom;
    scrollbar-width: thin;
  }

  .comment-thread .modal-post-title {
    margin-bottom: 12px;
    padding: 9px 11px;
    border-radius: 10px;
    font-size: 14px;
  }

  .comment-thread .comment-post-description summary {
    min-height: 36px;
    padding-block: 8px;
  }

  .comment-thread .comment p {
    line-height: 1.5;
  }

  .comment-composer {
    padding: 9px 12px max(9px, calc(env(safe-area-inset-bottom) - var(--comment-viewport-offset, 0px)));
    border-top: 1px solid var(--line);
    background: var(--white);
  }

  .comment-composer > .button {
    width: 100%;
  }

  #modal #comment-form {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end;
    gap: 7px;
  }

  #comment-form .field {
    grid-area: 1 / 1;
  }

  #comment-form .comment-input-label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  #modal #comment-form textarea {
    min-height: 44px;
    max-height: 92px;
    padding: 10px 12px;
    border-radius: 15px;
    resize: none;
    field-sizing: content;
  }

  #comment-form .form-secondary {
    grid-area: 1 / 2;
  }

  #comment-form .form-secondary .button {
    padding-inline: 10px;
    white-space: nowrap;
  }

  #comment-form .form-message {
    grid-area: 2 / 1 / 3 / -1;
  }

  .toast {
    right: 14px;
    bottom: calc(76px + env(safe-area-inset-bottom));
    max-width: calc(100% - 28px);
  }
}
