/* 代码编辑器外观。DOM 是 .kxed-code-editor-wrap 网格：第一列可选 .kxed-code-editor-gutter 行号，第二列把 <pre class="kxed-code-editor-highlight"> 与 <textarea class="kxed-input kxed-code-editor"> 叠在同一个网格区域；两者都是定位元素，textarea 在 DOM 中靠后因此画在上层接收输入。 */

.kxed-code-editor-wrap {
  position: relative;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  overflow: hidden;
  border: 1px solid var(--kxed-border);
  border-radius: var(--kxed-radius);
  background: var(--kxed-surface);
  transition: border-color var(--kxed-duration-fast) var(--kxed-ease-out);
}

.kxed-code-editor-wrap:focus-within {
  border-color: var(--kxed-primary);
}

.kxed-field-invalid .kxed-code-editor-wrap,
.kxed-field-invalid .kxed-code-editor-wrap:focus-within {
  border-color: var(--kxed-danger);
}

.kxed-code-editor-wrap:has(> .kxed-code-editor:disabled) {
  border-color: var(--kxed-disabled-border);
  background: var(--kxed-disabled);
}

.kxed-code-editor-wrap:has(> .kxed-code-editor[readonly]:not(:disabled)) {
  background: var(--kxed-readonly);
}

/* 字体、字号、行高与制表宽度必须一次性写给全部叠加层：任何一项不同都会让光标与高亮逐字符错位。 */
.kxed-code-editor-wrap,
.kxed-code-editor,
.kxed-code-editor-highlight,
.kxed-code-editor-highlight code,
.kxed-code-editor-gutter {
  font-family: var(--kxed-font-mono);
  font-size: var(--kxed-font-size-xs);
  line-height: var(--kxed-line-height-xs);
  tab-size: 2;
  font-variant-ligatures: none;
}

/* 同理，两层的 padding 只写一次，行号列单独对齐纵向 padding。 */
.kxed-code-editor,
.kxed-code-editor-highlight {
  box-sizing: border-box;
  margin: 0;
  padding: var(--kxed-space-2) var(--kxed-space-3);
  border: 0;
  white-space: pre;
  word-break: normal;
  overflow-wrap: normal;
}

.kxed-code-editor {
  grid-column: 2;
  grid-row: 1;
  position: relative;
  width: 100%;
  min-width: 0;
  min-height: var(--kxed-editor-min-height);
  max-height: var(--kxed-editor-max-height);
  overflow: auto;
  resize: vertical;
  background: transparent;
  color: transparent;
  caret-color: var(--kxed-text);
  outline: none;
}

.kxed-code-editor::selection {
  background: var(--kxed-primary-light-hover);
}

.kxed-code-editor-highlight {
  grid-column: 2;
  grid-row: 1;
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  color: var(--kxed-text);
  background: transparent;
}

.kxed-code-editor-highlight code {
  display: block;
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  white-space: inherit;
}

.kxed-code-editor-gutter {
  grid-column: 1;
  grid-row: 1;
  padding: var(--kxed-space-2);
  border-right: 1px solid var(--kxed-border);
  background: var(--kxed-subtle);
  color: var(--kxed-muted);
  text-align: right;
  white-space: pre;
  overflow: hidden;
  user-select: none;
  pointer-events: none;
}

/* Prism token 配色不抄默认主题：语义色取 --kxed-*-text，其余两类取 violet 与 orange 色阶，light-dark() 在暗色方案下换到浅一档的色阶，深浅模式一起跟随。 */
.kxed-code-editor-highlight .token.comment,
.kxed-code-editor-highlight .token.prolog,
.kxed-code-editor-highlight .token.doctype,
.kxed-code-editor-highlight .token.cdata {
  color: var(--kxed-muted);
  font-style: italic;
}

.kxed-code-editor-highlight .token.punctuation,
.kxed-code-editor-highlight .token.operator {
  color: var(--kxed-muted);
}

.kxed-code-editor-highlight .token.namespace {
  opacity: 0.75;
}

.kxed-code-editor-highlight .token.keyword,
.kxed-code-editor-highlight .token.atrule,
.kxed-code-editor-highlight .token.rule,
.kxed-code-editor-highlight .token.important {
  color: var(--kxed-primary-text);
  font-weight: var(--kxed-font-weight-semibold);
}

.kxed-code-editor-highlight .token.function,
.kxed-code-editor-highlight .token.class-name {
  color: var(--kxed-violet-7);
  color: light-dark(var(--kxed-violet-7), var(--kxed-violet-3));
}

.kxed-code-editor-highlight .token.string,
.kxed-code-editor-highlight .token.char,
.kxed-code-editor-highlight .token.attr-value,
.kxed-code-editor-highlight .token.inserted {
  color: var(--kxed-success-text);
}

.kxed-code-editor-highlight .token.number,
.kxed-code-editor-highlight .token.boolean,
.kxed-code-editor-highlight .token.constant,
.kxed-code-editor-highlight .token.symbol,
.kxed-code-editor-highlight .token.regex {
  color: var(--kxed-warning-text);
}

.kxed-code-editor-highlight .token.attr-name,
.kxed-code-editor-highlight .token.property,
.kxed-code-editor-highlight .token.variable,
.kxed-code-editor-highlight .token.builtin {
  color: var(--kxed-orange-9);
  color: light-dark(var(--kxed-orange-9), var(--kxed-orange-3));
}

.kxed-code-editor-highlight .token.tag,
.kxed-code-editor-highlight .token.selector,
.kxed-code-editor-highlight .token.deleted {
  color: var(--kxed-danger-text);
}

.kxed-code-editor-highlight .token.entity,
.kxed-code-editor-highlight .token.url {
  color: var(--kxed-primary-text);
  text-decoration: underline;
}

.kxed-code-editor-highlight .token.bold {
  font-weight: var(--kxed-font-weight-semibold);
}

.kxed-code-editor-highlight .token.italic {
  font-style: italic;
}
