:root {
    --bg: #f5f5f7;
    --panel: #ffffff;
    --border: #e0e0e6;
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --text: #1f1f23;
    --muted: #6b7280;
    --highlight: #fef3c7;
    --success: #16a34a;
    --sidebar-w: 260px;
  }
  * { box-sizing: border-box; }
  body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
  }
  header {
    padding: 16px 24px;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  header h1 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
  }
  header .sub {
    color: var(--muted);
    font-size: 12px;
  }
  .hamburger {
    display: none;
    background: white;
    color: var(--text);
    border: 1px solid var(--border);
    padding: 6px 10px;
    border-radius: 6px;
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    margin-right: 8px;
  }
  .sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 99;
  }
  .layout {
    display: grid;
    grid-template-columns: var(--sidebar-w) 6px 1fr;
    gap: 8px;
    padding: 16px 24px;
    height: calc(100vh - 65px);
  }
  .splitter {
    cursor: col-resize;
    position: relative;
    background: transparent;
    user-select: none;
  }
  .splitter::before {
    content: '';
    position: absolute;
    top: 0; bottom: 0; left: 2px; right: 2px;
    background: var(--border);
    border-radius: 2px;
    transition: background 0.15s;
  }
  .splitter:hover::before,
  .splitter.dragging::before { background: var(--accent); }
  body.resizing,
  body.resizing * { cursor: col-resize !important; user-select: none !important; }
  .sidebar {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 16px;
    overflow-y: auto;
  }
  .sidebar h2 {
    margin: 0 0 12px;
    font-size: 13px;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
  }
  .rule-group { margin-bottom: 18px; }
  .rule {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    padding: 6px 0;
    cursor: pointer;
    font-size: 13px;
    user-select: none;
  }
  .rule input { margin-top: 3px; }
  .rule-label { flex: 1; line-height: 1.5; }
  .rule-desc {
    color: var(--muted);
    font-size: 11px;
    margin-top: 2px;
  }
  .rule-count {
    color: var(--success);
    font-size: 11px;
    font-weight: 600;
    min-width: 24px;
    text-align: right;
    padding: 1px 6px;
    border-radius: 4px;
    user-select: none;
    cursor: pointer;
    transition: background 0.1s;
  }
  .rule-count:empty { cursor: default; }
  .rule-count:not(:empty):hover { background: rgba(0,0,0,0.06); }
  .rule-count.selected {
    background: var(--accent);
    color: white;
  }
  .rule-count.selected:hover { background: var(--accent-hover); }
  .editor {
    display: grid;
    grid-template-rows: auto 1fr auto 1fr;
    gap: 8px;
    min-height: 0;
  }
  .toolbar {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
  }
  button {
    background: var(--accent);
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: background 0.15s;
  }
  button:hover { background: var(--accent-hover); }
  button.secondary {
    background: white;
    color: var(--text);
    border: 1px solid var(--border);
  }
  button.secondary:hover { background: #f9fafb; }
  button.toggle[aria-pressed="true"] {
    background: #dbeafe;
    color: var(--accent);
    border-color: var(--accent);
  }
  button.toggle[aria-pressed="true"]::before {
    content: '● ';
    font-size: 10px;
    vertical-align: 1px;
  }
  button.toggle[aria-pressed="false"]::before {
    content: '○ ';
    font-size: 10px;
    color: var(--muted);
    vertical-align: 1px;
  }
  /* 简繁转换按钮 */
  button.convert {
    background: #f3e8ff;
    color: #7c3aed;
    border: 1px solid #c4b5fd;
  }
  button.convert:hover { background: #ede9fe; border-color: #7c3aed; }
  /* 工具栏竖向分隔符 */
  .toolbar-sep {
    width: 1px;
    height: 22px;
    background: var(--border);
    flex-shrink: 0;
    align-self: center;
    margin: 0 2px;
  }
  .stats {
    margin-left: auto;
    font-size: 12px;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    user-select: none;
  }
  .stats b {
    color: var(--text);
    font-weight: 600;
    margin: 0 2px;
  }
  .status {
    color: var(--muted);
    font-size: 12px;
  }
  .status.success { color: var(--success); font-weight: 500; }
  .pane {
    position: relative;
    min-height: 0;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--panel);
    overflow: hidden;
  }
  .pane:focus-within { border-color: var(--accent); }
  .pane > textarea,
  .pane > .rendered {
    position: absolute;
    inset: 0;
    margin: 0;
    padding: 14px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", "PingFang SC", "Hiragino Sans GB", "Noto Sans CJK SC", sans-serif;
    font-size: 13px;
    line-height: 1.7;
    letter-spacing: 0;
    word-spacing: 0;
    white-space: pre-wrap;
    overflow-wrap: break-word;
    word-break: normal;
    tab-size: 4;
    -moz-tab-size: 4;
    color: var(--text);
  }
  .pane > textarea {
    width: 100%;
    height: 100%;
    border: 0;
    border-radius: 0;
    resize: none;
    background: transparent;
    outline: none;
    caret-color: var(--text);
    overflow-y: auto;
  }
  .pane > .rendered {
    overflow-y: auto;
    overflow-x: hidden;
    cursor: text;
    background: var(--panel);
  }
  .pane > .rendered:empty { display: none; }
  .rendered mark {
    border-radius: 2px;
    padding: 0;
    color: inherit;
  }
  .rendered mark.del { background: #fecaca; }
  .rendered mark.ins { background: #bbf7d0; }
  .rendered mark.current {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
    box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.15);
  }
  .traversal {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 0 4px;
    border-left: 1px solid var(--border);
    margin-left: 4px;
  }
  .traversal[hidden] { display: none; }
  .traversal button {
    padding: 4px 10px;
    font-size: 14px;
    font-weight: 700;
    line-height: 1;
    min-width: 30px;
    color: var(--accent);
    border-color: var(--accent);
  }
  .traversal button:hover {
    background: var(--accent);
    color: white;
  }
  .traversal-counter {
    font-size: 12px;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
    min-width: 44px;
    text-align: center;
    font-weight: 500;
  }
  /* View mode: rendered (with highlights) is the visible layer;
     textarea sits transparently on top so clicks/keys/wheels go to it natively
     (caret placement, selection, scroll). Its content shows through to rendered. */
  .pane:not(.editing) > textarea {
    color: transparent;
    background: transparent;
    /* caret stays visible via caret-color from the base rule */
  }
  /* In input pane's view mode, hide rendered's scrollbar (textarea's scrollbar is the visible one).
     Output pane has no overlapping textarea, so it keeps its native scrollbar. */
  #input-pane:not(.editing) > .rendered { scrollbar-width: none; }
  #input-pane:not(.editing) > .rendered::-webkit-scrollbar { display: none; }
  .pane.editing > .rendered { display: none; }
  .panel-label {
    font-size: 12px;
    color: var(--muted);
    font-weight: 500;
    padding: 0 4px;
  }
  .toggle-all {
    font-size: 11px;
    color: var(--accent);
    cursor: pointer;
    background: none;
    border: none;
    padding: 0;
    margin-left: auto;
  }
  .group-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
  }
  .group-header h2 { margin: 0; }

  /* === Mobile (<=768px): sidebar becomes a drawer, layout single-column === */
  @media (max-width: 768px) {
    header {
      padding: 10px 12px;
    }
    header h1 { font-size: 16px; }
    header .sub { font-size: 11px; }
    header > div.sub:last-child { display: none; }   /* hide right-side subtitle */
    .hamburger { display: inline-block; }

    .layout {
      grid-template-columns: 1fr;       /* single column */
      gap: 8px;
      padding: 8px;
      height: calc(100dvh - 56px);      /* dvh handles mobile browser chrome */
      height: calc(100vh - 56px);       /* fallback */
    }
    .splitter { display: none; }

    .sidebar {
      position: fixed;
      top: 0; left: 0; bottom: 0;
      width: 280px;
      max-width: 85vw;
      z-index: 100;
      border-radius: 0;
      transform: translateX(-100%);
      transition: transform 0.2s ease;
      box-shadow: 2px 0 12px rgba(0,0,0,0.15);
    }
    body.sidebar-open .sidebar { transform: translateX(0); }
    body.sidebar-open .sidebar-backdrop { display: block; }

    .toolbar { gap: 6px; }
    .toolbar button {
      padding: 7px 10px;
      font-size: 12px;
      min-height: 32px;
    }
    .traversal button {
      padding: 4px 8px;
      font-size: 13px;
      min-width: 32px;
    }
    .stats {
      font-size: 11px;
      flex-basis: 100%;                  /* wrap to its own line */
      order: 99;
      text-align: right;
      margin-left: 0;
    }
    .status {
      flex-basis: 100%;
      order: 100;
    }

    /* slightly more breathing room for the editor panes */
    .editor { gap: 6px; }
    .pane > textarea,
    .pane > .rendered {
      padding: 10px;
      font-size: 14px;                   /* easier to read on phones */
      line-height: 1.65;
    }
    .panel-label { font-size: 11px; }

    /* Touch-friendly rule rows */
    .rule { padding: 8px 0; }
    .rule input[type="checkbox"],
    .rule input[type="radio"] { transform: scale(1.15); }
  }