/* Component <x-rich-editor>: lớp giao diện đặt lên theme "snow" của Quill.
   Dùng chung cho main.css và admin.css nên chỉ đọc các token có ở cả hai:
   --red --blue --gold --ink --muted --line --soft --white --shadow. */

.rich-editor {
  --rich-editor-radius: 8px;
  --rich-editor-min-height: 220px;
  --rich-editor-gap: 6px;
  position: relative;
  display: grid;
  gap: var(--rich-editor-gap);
  min-width: 0;
  position: relative;
}

/* @mention trong editor và hộp gợi ý thành viên. */
.ql-editor .chat-mention {
  background: var(--soft);
  border-radius: 4px;
  color: var(--blue);
  font-weight: 600;
  padding: 0 2px;
  white-space: nowrap;
}

.chat-mention-menu {
  background: var(--white);
  border: 1px solid var(--line);
  border-radius: var(--rich-editor-radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  left: 0;
  max-height: 264px;
  min-width: 0;
  overflow-y: auto;
  padding: 4px;
  position: absolute;
  right: 0;
  z-index: 30;
}

.chat-mention-menu[hidden] {
  display: none;
}

.chat-mention-option {
  align-items: center;
  background: none;
  border: 0;
  border-radius: 6px;
  color: var(--ink);
  cursor: pointer;
  display: flex;
  gap: 8px;
  min-height: 44px;
  padding: 6px 8px;
  text-align: left;
  width: 100%;
}

.chat-mention-option.is-active,
.chat-mention-option:hover {
  background: var(--soft);
}

.chat-mention-avatar {
  align-items: center;
  background: var(--soft);
  border-radius: 50%;
  color: var(--muted);
  display: inline-flex;
  flex: 0 0 28px;
  font-size: .8rem;
  font-weight: 700;
  height: 28px;
  justify-content: center;
  overflow: hidden;
  width: 28px;
}

.chat-mention-avatar img {
  height: 100%;
  object-fit: cover;
  width: 100%;
}

.chat-mention-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rich-editor:not(.is-enhanced) .rich-editor-toolbar,
.rich-editor:not(.is-enhanced) .rich-editor-canvas {
  display: none;
}

.rich-editor.is-enhanced .rich-editor-source {
  display: none;
}

/* Chế độ HTML thô: <textarea> thay chỗ vùng soạn, các nút định dạng mờ đi. */
.rich-editor.is-enhanced.is-source .rich-editor-source {
  display: block;
  font-family: Consolas, "Courier New", monospace;
  font-size: .85rem;
}

.rich-editor.is-source .ql-container.ql-snow {
  display: none;
}

.rich-editor.is-source .ql-toolbar.ql-snow button:not(.ql-source) {
  opacity: .35;
  pointer-events: none;
}

.rich-editor-source {
  width: 100%;
  min-height: var(--rich-editor-min-height);
  resize: vertical;
}

.rich-editor .ql-toolbar.ql-snow {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 6px;
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: var(--rich-editor-radius) var(--rich-editor-radius) 0 0;
  /* Toolbar và khung soạn thảo là hai ô liền của một khung viền: bù lại khoảng
     gap của grid, nếu không viền hai bên bị đứt một đoạn ở chỗ nối. */
  margin-bottom: calc(-1 * var(--rich-editor-gap));
  background: var(--soft);
  font-family: inherit;
}

/* Ô soạn gọn (`collapsible`, kiểu Google Chat): thanh công cụ ẩn cho tới khi
   rich-editor.js thêm `is-toolbar-open`; lúc đó khung soạn tự bo đủ bốn góc. */
.rich-editor.is-enhanced[data-collapsible]:not(.is-toolbar-open) .ql-toolbar.ql-snow {
  display: none;
}

.rich-editor.is-enhanced[data-collapsible]:not(.is-toolbar-open) .ql-container.ql-snow {
  border-radius: var(--rich-editor-radius);
}

.rich-editor .ql-toolbar.ql-snow .ql-formats {
  display: inline-flex;
  gap: 2px;
  margin: 0;
  padding-right: 4px;
  border-right: 1px solid var(--line);
}

.rich-editor .ql-toolbar.ql-snow .ql-formats:last-of-type {
  border-right: 0;
}

.rich-editor .ql-toolbar.ql-snow button {
  width: 34px;
  height: 34px;
  padding: 7px;
  border-radius: 6px;
}

.rich-editor .ql-snow.ql-toolbar button .ql-stroke,
.rich-editor .ql-snow .ql-toolbar button .ql-stroke {
  stroke: var(--muted);
}

.rich-editor .ql-snow.ql-toolbar button .ql-fill,
.rich-editor .ql-snow .ql-toolbar button .ql-fill {
  fill: var(--muted);
}

.rich-editor .ql-snow.ql-toolbar button:hover,
.rich-editor .ql-snow.ql-toolbar button:focus-visible {
  background: var(--white);
}

.rich-editor .ql-snow.ql-toolbar button.ql-active {
  background: var(--white);
  box-shadow: inset 0 0 0 1px var(--line);
}

.rich-editor .ql-snow.ql-toolbar button:hover .ql-stroke,
.rich-editor .ql-snow.ql-toolbar button:focus-visible .ql-stroke,
.rich-editor .ql-snow.ql-toolbar button.ql-active .ql-stroke {
  stroke: var(--blue);
}

.rich-editor .ql-snow.ql-toolbar button:hover .ql-fill,
.rich-editor .ql-snow.ql-toolbar button:focus-visible .ql-fill,
.rich-editor .ql-snow.ql-toolbar button.ql-active .ql-fill {
  fill: var(--blue);
}

.rich-editor .ql-container.ql-snow {
  border: 1px solid var(--line);
  border-radius: 0 0 var(--rich-editor-radius) var(--rich-editor-radius);
  background: var(--white);
  color: var(--ink);
  font-family: inherit;
  font-size: 1rem;
}

.rich-editor .ql-container.ql-snow:focus-within {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px var(--soft);
}

.rich-editor.is-invalid .ql-container.ql-snow,
.rich-editor.is-invalid .ql-toolbar.ql-snow {
  border-color: var(--red);
}

/* `anywhere` thay cho `break-word` của Quill: break-word không làm nhỏ độ rộng
   tối thiểu, nên một chuỗi dài không dấu cách đẩy cả cột grid phình ra thay vì
   xuống dòng. `resize` trả lại tay kéo giãn chiều cao như <textarea> cũ; đặt ở
   .ql-editor (vốn đã overflow-y: auto) để khung link của Quill không bị cắt. */
.rich-editor .ql-editor {
  min-height: var(--rich-editor-min-height);
  padding: 14px 16px;
  line-height: 1.65;
  overflow-wrap: anywhere;
  resize: vertical;
}

.rich-editor .ql-editor.ql-blank::before {
  left: 16px;
  right: 16px;
  color: var(--muted);
  font-style: normal;
}

.rich-editor .ql-editor h2,
.rich-editor .ql-editor h3 {
  margin: .6em 0 .3em;
  line-height: 1.3;
}

.rich-editor .ql-editor blockquote {
  margin: .5em 0;
  padding: 4px 14px;
  border-left: 4px solid var(--gold);
  color: var(--muted);
}

.rich-editor .ql-editor a {
  color: var(--blue);
}

.rich-editor .ql-editor img {
  max-width: 100%;
  height: auto;
  border-radius: 6px;
}

/* Ảnh có thanh chỉnh (imageTools): cỡ lưu bằng class, căn bằng margin của
   khối ảnh — không dùng float để đoạn chữ phía sau không tràn ra ngoài cột. */
.rich-editor .ql-editor img.img-size-small {
  width: 33%;
}

.rich-editor .ql-editor img.img-size-medium {
  width: 50%;
}

.rich-editor .ql-editor img.img-size-large {
  width: 75%;
}

.rich-editor .ql-editor img.img-align-left {
  display: block;
  margin-right: auto;
}

.rich-editor .ql-editor img.img-align-center {
  display: block;
  margin-right: auto;
  margin-left: auto;
}

.rich-editor .ql-editor img.img-align-right {
  display: block;
  margin-left: auto;
}

/* Thanh công cụ nổi đặt dưới ảnh đang chọn. */
.rich-editor-image-tools {
  position: absolute;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--white);
  box-shadow: 0 4px 12px rgba(0, 0, 0, .12);
}

.rich-editor-image-tools[hidden] {
  display: none;
}

.rich-editor-image-group {
  display: flex;
  gap: 2px;
}

.rich-editor-image-group + .rich-editor-image-group {
  padding-left: 4px;
  border-left: 1px solid var(--line);
}

.rich-editor-image-tools button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 26px;
  height: 26px;
  padding: 0 4px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: .78rem;
  cursor: pointer;
}

.rich-editor-image-tools button:hover,
.rich-editor-image-tools button:focus-visible {
  background: var(--soft);
}

.rich-editor-image-tools button.is-active {
  color: var(--blue);
  background: var(--soft);
}

.rich-editor-image-tools svg {
  width: 16px;
  height: 16px;
}

/* Quill chỉ tô màu nét vẽ bên trong .ql-snow, còn thanh này nằm ngoài
   thanh công cụ của nó, nên phải tự khai báo lại. */
.rich-editor-image-tools .ql-stroke {
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

/* Khối của trang nội dung: ảnh/YouTube trong <figure>, gallery, đường kẻ. */
.rich-editor .ql-editor figure {
  margin: 12px 0;
}

.rich-editor .ql-editor figure iframe {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  border: 0;
  border-radius: 6px;
  background: var(--soft);
  pointer-events: none;
}

.rich-editor .ql-editor figcaption {
  margin-top: 4px;
  color: var(--muted);
  font-size: .85rem;
}

.rich-editor .ql-editor .article-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 8px;
  margin: 12px 0;
}

.rich-editor .ql-editor .article-gallery figure {
  margin: 0;
}

.rich-editor .ql-editor hr {
  margin: 16px 0;
  border: 0;
  border-top: 1px solid var(--line);
}

.rich-editor .ql-snow .ql-tooltip {
  z-index: 5;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: var(--rich-editor-radius);
  color: var(--ink);
  box-shadow: var(--shadow);
}

.rich-editor .ql-snow .ql-tooltip input[type=text] {
  width: 190px;
  height: 32px;
  min-height: 0;
  padding: 3px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.rich-editor .ql-snow .ql-tooltip::before {
  content: attr(data-visit);
}

.rich-editor .ql-snow .ql-tooltip[data-mode=link]::before {
  content: attr(data-enter);
}

.rich-editor .ql-snow .ql-tooltip a.ql-action::after {
  content: attr(data-edit);
}

.rich-editor .ql-snow .ql-tooltip.ql-editing a.ql-action::after {
  content: attr(data-save);
}

.rich-editor .ql-snow .ql-tooltip a.ql-remove::before {
  content: attr(data-remove);
}

.rich-editor .ql-snow .ql-tooltip a.ql-preview {
  color: var(--blue);
}

/* Nhãn cho một editor: component không được nằm trong <label> (nhãn sẽ
   chuyển cú click sang nút đầu tiên của thanh công cụ), nên dùng khối này. */
.rich-editor-field {
  display: grid;
  gap: 7px;
  min-width: 0;
}

.rich-editor-field > span {
  font-weight: 750;
}

.rich-editor-hint {
  color: var(--muted);
}

.rich-editor-message {
  margin: 0;
  color: var(--red);
  font-size: .85rem;
}

.rich-editor-message.is-info {
  color: var(--muted);
}

.rich-editor-message:empty {
  display: none;
}

@media (max-width: 700px) {
  .rich-editor {
    --rich-editor-min-height: 160px;
  }

  .rich-editor .ql-snow .ql-tooltip {
    left: 8px !important;
    max-width: calc(100% - 16px);
  }
}
