.msx-editor {
    --msx-editor-surface: var(--color-surface-card, var(--color-surface, #fff));
    --msx-editor-text: var(--color-ink, #1c1917);
    --msx-editor-muted: var(--color-muted, #777169);
    --msx-editor-line: var(--color-hairline-strong, var(--color-hairline, rgba(0, 0, 0, 0.12)));
    --msx-editor-primary: var(--color-primary, #1c1917);
    --msx-editor-success: var(--color-semantic-success, #16a34a);
    --msx-editor-on-primary: var(--color-on-primary, #fff);
    --msx-editor-radius: var(--radius-md, 8px);
    --msx-editor-content-image-inline-gap: 6px;
    --msx-editor-topic-image-default-width: 25%;
    --msx-editor-reply-image-default-width: clamp(72px, 11vw, 88px);

    display: grid;
    gap: 0;
    color: var(--msx-editor-text);
}

.msx-editor-source,
.msx-editor-source-container,
[data-content-image-uploader].msx-editor-source-container,
[data-content-image-uploader][hidden] {
    display: none !important;
}

.msx-editor [hidden] {
    display: none !important;
}

.msx-editor-frame {
    border: 1px solid var(--msx-editor-line);
    border-radius: var(--msx-editor-radius);
    background: var(--msx-editor-surface);
    box-shadow: none;
}

.msx-editor-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    min-height: 36px;
    padding: 8px 10px 0;
    gap: 4px;
}

.msx-editor-button {
    appearance: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    min-width: 28px;
    height: 28px;
    border: 0;
    border-radius: 999px;
    background: transparent;
    color: var(--msx-editor-muted);
    font: inherit;
    font-size: 13px;
    line-height: 1;
    cursor: pointer;
}

.msx-editor-button {
    padding: 0;
}

.msx-editor-icon {
    display: block;
    width: 17px;
    height: 17px;
    flex: 0 0 auto;
    pointer-events: none;
}

.msx-editor-button:hover {
    background: color-mix(in srgb, var(--msx-editor-primary) 5%, transparent);
    color: var(--msx-editor-text);
}

.msx-editor-button:focus-visible {
    outline: 2px solid var(--msx-editor-primary);
    outline-offset: 2px;
}

.msx-editor-attachment-button {
    position: relative;
}

.msx-editor-attachment-button.has-files::after {
    content: attr(data-msx-editor-attachment-count);
    position: absolute;
    top: 1px;
    right: 1px;
    min-width: 13px;
    height: 13px;
    padding: 0 3px;
    border-radius: 9999px;
    background: var(--msx-editor-primary);
    color: var(--msx-editor-on-primary);
    font-size: 9px;
    font-weight: 600;
    line-height: 13px;
    text-align: center;
}

.msx-editor-popover-wrap {
    position: relative;
    display: inline-flex;
}

.msx-editor-popover {
    position: absolute;
    z-index: 20;
    top: calc(100% + 8px);
    left: 0;
    width: max-content;
    max-width: min(320px, calc(100vw - 32px));
    padding: 10px;
    border: 1px solid var(--msx-editor-line);
    border-radius: var(--msx-editor-radius);
    background: var(--msx-editor-surface);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}

.msx-editor-shortcuts-wrap {
    margin-left: auto;
}

.msx-editor-shortcuts-wrap .msx-editor-popover {
    right: 0;
    left: auto;
}

.msx-editor-shortcut-trigger {
    width: 28px;
    min-width: 28px;
    height: 28px;
    padding: 0;
    border-radius: 9999px;
    color: var(--msx-editor-muted);
    font-size: 12px;
}

.msx-editor-shortcuts {
    display: grid;
    gap: 6px;
    min-width: 170px;
    margin: 0;
}

.msx-editor-shortcut-row {
    display: grid;
    grid-template-columns: minmax(68px, auto) 1fr;
    align-items: center;
    gap: 12px;
    color: var(--msx-editor-muted);
    font-size: 12px;
    line-height: 1.3;
}

.msx-editor-shortcut-row dt,
.msx-editor-shortcut-row dd {
    margin: 0;
}

.msx-editor-shortcut-row dt {
    color: var(--msx-editor-text);
    font-weight: 500;
    white-space: nowrap;
}

.msx-editor-palette {
    display: grid;
    grid-template-columns: repeat(3, 28px);
    gap: 8px;
}

.msx-editor-swatch {
    appearance: none;
    width: 28px;
    height: 28px;
    border: 1px solid var(--msx-editor-line);
    border-radius: 9999px;
    background: var(--msx-editor-swatch);
    cursor: pointer;
}

.msx-editor-swatch:hover,
.msx-editor-swatch:focus-visible {
    outline: 2px solid var(--msx-editor-primary);
    outline-offset: 2px;
}

.msx-editor-table-picker {
    display: grid;
    gap: 8px;
}

.msx-editor-table-picker-grid {
    display: grid;
    grid-template-columns: repeat(var(--msx-editor-table-picker-columns), 22px);
    gap: 4px;
}

.msx-editor-table-picker-cell {
    appearance: none;
    width: 22px;
    height: 22px;
    border: 1px solid var(--msx-editor-line);
    border-radius: 6px;
    background: transparent;
    cursor: pointer;
}

.msx-editor-table-picker-cell:hover {
    border-color: var(--msx-editor-success);
}

.msx-editor-table-picker-cell.is-selected {
    border-color: var(--msx-editor-success);
    border-style: dashed;
    background: color-mix(in srgb, var(--msx-editor-success) 12%, var(--msx-editor-surface));
}

.msx-editor-table-picker-status {
    color: var(--msx-editor-muted);
    font-size: 12px;
    line-height: 1.2;
    text-align: center;
}

.msx-editor-surface {
    min-height: 180px;
    padding: 10px 16px 14px;
    overflow-x: hidden;
    border: 0;
    border-radius: 0 0 var(--msx-editor-radius) var(--msx-editor-radius);
    background: transparent;
    box-shadow: none;
}

.msx-editor-surface:focus-visible {
    outline: 0;
}

.msx-editor-surface:empty::before {
    content: attr(data-placeholder);
    color: var(--msx-editor-muted);
}

.msx-editor-attachments {
    display: grid;
    gap: 6px;
    margin-top: 6px;
}

.msx-editor-attachments-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 18px;
}

.msx-editor-attachments-summary {
    color: var(--msx-editor-muted);
    font-size: 12px;
    line-height: 1.3;
}

.msx-editor-attachment-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.msx-editor-attachment-item {
    display: inline-flex;
    align-items: center;
    max-width: min(100%, 360px);
    min-height: 30px;
    gap: 8px;
    padding: 5px 8px 5px 10px;
    border-radius: 9999px;
    background: color-mix(in srgb, var(--msx-editor-primary) 5%, var(--msx-editor-surface));
    color: var(--msx-editor-text);
    font-size: 12px;
    line-height: 1.2;
}

.msx-editor-attachment-name {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.msx-editor-attachment-size {
    flex: 0 0 auto;
    color: var(--msx-editor-muted);
    font-size: 11px;
}

.msx-editor-attachment-remove {
    appearance: none;
    flex: 0 0 auto;
    border: 0;
    background: transparent;
    color: var(--msx-editor-muted);
    cursor: pointer;
    font: inherit;
    font-size: 12px;
    line-height: 1;
    padding: 0;
}

.msx-editor-attachment-remove:hover,
.msx-editor-attachment-remove:focus-visible {
    color: var(--msx-editor-primary);
}

.msx-editor-code code:empty::before {
    content: attr(data-placeholder);
    color: var(--msx-editor-muted);
}

.msx-editor-table-wrap {
    position: relative;
}

.msx-editor-block-delete {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 2;
    height: 26px;
    border: 0;
    border-radius: 9999px;
    background: var(--msx-editor-primary);
    color: var(--msx-editor-on-primary);
    cursor: pointer;
    font: inherit;
    font-size: 12px;
    line-height: 1;
    opacity: 0;
    padding: 0 10px;
    transition: opacity 0.15s ease;
}

.msx-editor-table-wrap:hover .msx-editor-block-delete,
.msx-editor-table-wrap:focus-within .msx-editor-block-delete {
    opacity: 0.92;
}

.msx-editor-paid-gate {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 42px;
    margin: 10px 0;
    border: 1px dashed var(--msx-editor-line);
    border-radius: var(--msx-editor-radius);
    background: color-mix(in srgb, var(--msx-editor-primary) 4%, var(--msx-editor-surface));
    color: var(--msx-editor-muted);
    font-size: 13px;
    line-height: 1.4;
    user-select: none;
}

.msx-editor-paid-gate:focus-visible {
    outline: 2px solid var(--msx-editor-primary);
    outline-offset: 2px;
}

.msx-editor-paid-gate span {
    pointer-events: none;
}

.msx-editor-paid-gate-delete {
    position: absolute;
    top: 50%;
    right: 10px;
    height: 26px;
    transform: translateY(-50%);
    border: 0;
    background: transparent;
    color: var(--msx-editor-muted);
    cursor: pointer;
    font: inherit;
    font-size: 12px;
    line-height: 1;
    padding: 0;
}

.msx-editor-paid-gate-delete:hover {
    color: var(--msx-editor-primary);
}

.msx-editor-table td:focus {
    outline: 2px solid var(--msx-editor-primary);
    outline-offset: -2px;
    background: color-mix(in srgb, var(--msx-editor-primary) 8%, var(--msx-editor-surface));
}

.msx-editor-media {
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
}

.msx-editor-media[data-msx-type="image"] {
    position: relative;
    display: inline-block;
    box-sizing: border-box;
    max-width: 100%;
    margin: 6px 0;
    padding-right: var(--msx-editor-content-image-inline-gap);
    line-height: 0;
    user-select: none;
    vertical-align: top;
    cursor: grab;
}

.msx-editor-media[data-msx-type="image"].is-dragging {
    cursor: grabbing;
    opacity: 0.38;
}

.msx-editor-is-dragging {
    user-select: none;
}

.msx-editor-media-ghost {
    position: fixed !important;
    z-index: 9999;
    top: 0;
    left: 0;
    margin: 0 !important;
    opacity: 0.88;
    pointer-events: none;
    transform-origin: 0 0;
    will-change: transform;
}

.msx-editor-media-ghost .msx-editor-media-resize {
    display: none;
}

.msx-editor-media-ghost .content-image {
    border-radius: var(--msx-editor-radius, 8px);
}

.msx-editor-media-drop-indicator {
    position: fixed;
    z-index: 9998;
    border-radius: 9999px;
    background: var(--msx-editor-primary, var(--color-primary, #1c1917));
    box-shadow: 0 0 0 2px color-mix(in srgb, var(--msx-editor-primary, var(--color-primary, #1c1917)) 18%, transparent);
    pointer-events: none;
}

.msx-editor-media-frame {
    position: relative;
    display: block;
    max-width: 100%;
    vertical-align: top;
    touch-action: none;
}

.msx-editor-media .content-image {
    margin: 0;
    border: 0;
    border-radius: var(--msx-editor-radius);
}

.msx-editor-media[data-msx-type="image"]:not([data-width]) {
    width: var(--msx-editor-topic-image-default-width);
}

.msx-editor-media:not([data-width]) .content-image {
    width: 100%;
    aspect-ratio: 1 / 1;
    height: auto;
    max-width: 100%;
    max-height: none;
    object-fit: cover;
    object-position: center;
}

.msx-editor[data-msx-editor-context="reply"] .msx-editor-media[data-msx-type="image"],
.msx-editor[data-msx-editor-context="reply"] .msx-editor-media[data-msx-type="image"][data-width] {
    display: inline-block;
    width: var(--msx-editor-reply-image-default-width);
    max-width: var(--msx-editor-reply-image-default-width);
    margin: 6px 0;
    padding-right: var(--msx-editor-content-image-inline-gap);
}

.msx-editor[data-msx-editor-context="reply"] .msx-editor-media[data-msx-type="image"] .content-image {
    width: 100% !important;
    aspect-ratio: 1 / 1;
    height: auto !important;
    max-width: 100%;
    max-height: none;
    object-fit: cover;
    object-position: center;
}

.msx-editor[data-msx-editor-context="reply"] .msx-editor-media[data-msx-type="image"] .msx-editor-media-resize {
    display: none;
}

.msx-editor-media-frame::after {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: var(--msx-editor-radius);
    pointer-events: none;
}

.msx-editor-media.is-selected .msx-editor-media-frame::after,
.msx-editor-media.is-resizing .msx-editor-media-frame::after {
    outline: 2px solid var(--msx-editor-primary);
    outline-offset: 2px;
}

.msx-editor-media[data-msx-type="image"][data-width] {
    display: inline-block;
    width: var(--msx-editor-media-width, 100%);
    max-width: 100%;
    vertical-align: top;
}

.msx-editor-media[data-width] .msx-editor-media-frame {
    width: 100%;
}

.msx-editor-media[data-width] .content-image {
    width: 100%;
    height: auto;
    max-height: none;
}

.msx-editor-media-resize {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 32px;
    height: 32px;
    background: transparent;
    color: var(--msx-editor-on-primary);
    cursor: nwse-resize;
    opacity: 0;
    transition: opacity 0.15s ease;
    touch-action: none;
}

.msx-editor-media-resize::before {
    content: "";
    position: absolute;
    right: 6px;
    bottom: 6px;
    width: 16px;
    height: 16px;
    box-sizing: border-box;
    border: 2px solid var(--msx-editor-surface);
    border-radius: 9999px;
    background: var(--msx-editor-primary);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.24);
}

.msx-editor-media-frame:hover .msx-editor-media-resize,
.msx-editor-media.is-selected .msx-editor-media-resize,
.msx-editor-media.is-resizing .msx-editor-media-resize {
    opacity: 0.95;
}

@media (max-width: 720px) {
    .msx-editor[data-msx-editor-context="topic"] {
        --msx-editor-topic-image-default-width: 50%;
    }
}

.msx-editor-help {
    color: var(--msx-editor-muted);
    font-size: 12px;
}
