:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1a1d21;
  --muted: #6b7280;
  --border: #e5e7eb;
  --brand: #dc2626;
  --brand-dark: #b91c1c;
  --topbar: #14161a;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px -12px rgba(16, 24, 40, .18);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d0f12;
    --card: #161a1f;
    --text: #e8eaed;
    --muted: #9aa2ad;
    --border: #262c34;
    --brand: #ef4444;
    --brand-dark: #dc2626;
    --topbar: #0a0b0d;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px -12px rgba(0, 0, 0, .6);
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

/* ---------- 顶栏 ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--topbar);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px clamp(14px, 3vw, 28px);
  border-bottom: 1px solid rgba(255, 255, 255, .08);
}

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

.brand h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: .5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--brand);
  box-shadow: 0 0 0 4px rgba(220, 38, 38, .18);
  flex: none;
}

.actions {
  display: flex;
  gap: 10px;
  flex: none;
}

/* ---------- 按钮 ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 38px;
  padding: 0 18px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, .16);
  background: rgba(255, 255, 255, .08);
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: background .15s ease, transform .1s ease, opacity .15s ease;
  -webkit-tap-highlight-color: transparent;
  white-space: nowrap;
}

.btn svg {
  width: 16px;
  height: 16px;
  fill: currentColor;
  flex: none;
}

.btn:hover { background: rgba(255, 255, 255, .16); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.btn.primary {
  background: var(--brand);
  border-color: var(--brand);
  box-shadow: 0 4px 14px -4px rgba(220, 38, 38, .6);
}
.btn.primary:hover { background: var(--brand-dark); border-color: var(--brand-dark); }

.btn.ghost {
  border-color: var(--border);
  background: transparent;
  color: var(--muted);
}
.btn.ghost:hover { background: rgba(127, 127, 127, .1); color: var(--text); }

/* ---------- 布局 ---------- */
.wrap {
  width: 100%;
  max-width: 980px;
  margin: 0 auto;
  padding: clamp(14px, 3vw, 28px);
  flex: 1;
  display: flex;
  flex-direction: column;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.hidden { display: none !important; }

#editorPanel { flex: 1; }

/* ---------- 输入框 ---------- */
#input {
  flex: 1;
  width: 100%;
  min-height: clamp(320px, 58vh, 640px);
  resize: none;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--text);
  padding: 18px 20px;
  font-family: inherit;
  font-size: 16px;
  line-height: 1.9;
  letter-spacing: .2px;
  tab-size: 4;
}

#input::placeholder { color: var(--muted); opacity: .8; }

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.foot {
  padding: 10px 20px;
  border-top: 1px solid var(--border);
  background: rgba(127, 127, 127, .04);
}

.meta {
  font-size: 12.5px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.state.ready { color: #16a34a; }
.state.error { color: var(--brand); }

/* ---------- 结果 ---------- */
#resultPanel { flex: 0 1 auto; }

.result-head {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: rgba(127, 127, 127, .04);
}

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  font-size: 13px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.stats b { color: var(--text); font-weight: 600; }
.stats .hit b { color: var(--brand); }

.head-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.switch {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}

.switch input { accent-color: var(--brand); width: 14px; height: 14px; cursor: pointer; }

.progress {
  height: 3px;
  background: rgba(127, 127, 127, .15);
  overflow: hidden;
  display: none;
}
.progress.active { display: block; }
.progress span {
  display: block;
  height: 100%;
  width: 0;
  background: var(--brand);
  transition: width .18s linear;
}

.output {
  flex: 1;
  min-height: 35vh;
  padding: 18px 20px 32px;
  font-size: 16px;
  line-height: 2;
  letter-spacing: .2px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  user-select: text;
}

/* 红底白字 —— CSS Custom Highlight API（无额外 DOM 节点，超大文本也不卡） */
::highlight(illegal) {
  background-color: #dc2626;
  color: #ffffff;
}

/* 红底白字 —— 旧浏览器回退方案 */
.hl {
  background: var(--brand);
  color: #fff;
  border-radius: 3px;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
  padding: 0 .12em;
  margin: 0 -.06em;
}

.hl::selection { background: #fff; color: var(--brand); }

/* ---------- 提示 ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translate(-50%, 20px);
  background: rgba(20, 22, 26, .94);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13.5px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
  z-index: 50;
  max-width: calc(100vw - 40px);
  text-align: center;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- 移动端 ---------- */
@media (max-width: 640px) {
  .topbar { padding: 10px 12px; gap: 10px; }
  .brand h1 { font-size: 15px; }
  .btn { height: 36px; padding: 0 14px; font-size: 13.5px; }
  .btn svg { width: 15px; height: 15px; }
  #input { padding: 14px; font-size: 16px; }
  .foot { padding: 8px 14px; }
  .result-head { padding: 12px 14px; }
  .output { padding: 14px 14px 28px; font-size: 16px; line-height: 1.95; }
  .stats { font-size: 12.5px; }
  .head-actions { width: 100%; justify-content: space-between; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}