/* ==========================================================================
   html-viewer.css —— 在线 HTML 代码运行与预览（页面级布局）
   --------------------------------------------------------------------------
   只负责本工具排版；颜色 / 圆角 / 间距 / 动效复用 ui-kit 令牌（--ui-*），
   随颜色模板与明暗模式自动换肤。代码高亮的 token 颜色在文件末尾按主题覆写。
   ========================================================================== */

.hv-tool {
  --r: 14px;
  --gap: 14px;
  --hv-font: 12.5px;
  --hv-lh: 1.7;
  /* 代码高亮配色（浅色） */
  --tk-com: #6a737d;
  --tk-str: #0a7d33;
  --tk-num: #b35900;
  --tk-kw: #7b1fa2;
  --tk-tag: #1565c0;
  --tk-attr: #0f766e;
  --tk-punt: #5f6368;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  color: var(--ui-text);
}
.hv-tool [hidden] { display: none !important; }
.hv-tool > .ui-alert { align-items: flex-start; }
.hv-tool > .ui-alert .body .t { font-weight: 600; }

html[data-theme="dark"] .hv-tool {
  --tk-com: #8b949e;
  --tk-str: #7ee787;
  --tk-num: #ffa657;
  --tk-kw: #d2a8ff;
  --tk-tag: #79c0ff;
  --tk-attr: #7dd3fc;
  --tk-punt: #8b949e;
}

/* --------------------------------------------------------------------------
   1. 工具栏
   -------------------------------------------------------------------------- */
.hv-bar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 14px;
  border: 1px solid var(--ui-line-soft);
  border-radius: var(--r);
  background: var(--ui-bg-sub);
}
.hv-bar .sp { flex: 1; }
.hv-sep { width: 1px; height: 22px; background: var(--ui-line-soft); flex-shrink: 0; }
.hv-bar-group { display: flex; align-items: center; gap: 8px; }
.hv-bar-label { font-size: 12px; color: var(--ui-text-mute); white-space: nowrap; }
.hv-select {
  height: var(--ui-h-sm); font-size: var(--ui-font-xs);
  padding: 0 26px 0 10px; max-width: 168px;
}
.hv-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ui-success); flex-shrink: 0; }
.hv-dot.off { background: var(--ui-line); }

/* --------------------------------------------------------------------------
   2. 主区（编辑器 / 分割条 / 预览）
   -------------------------------------------------------------------------- */
.hv-main {
  --split-x: 52%;
  --split-y: 56%;
  display: grid;
  height: min(74vh, 760px);
  min-height: 420px;
  border: 1px solid var(--ui-line-soft);
  border-radius: var(--r);
  background: var(--ui-bg);
  overflow: hidden;
}
.hv-main.layout-split { grid-template-columns: var(--split-x) 9px minmax(0, 1fr); }
.hv-main.layout-vertical { grid-template-columns: 1fr; grid-template-rows: var(--split-y) 9px minmax(0, 1fr); }
.hv-main.layout-code, .hv-main.layout-preview { grid-template-columns: 1fr; }
.hv-main.layout-code .hv-preview, .hv-main.layout-code .hv-splitter { display: none; }
.hv-main.layout-preview .hv-code, .hv-main.layout-preview .hv-splitter { display: none; }
@media (max-width: 900px) {
  .hv-main { height: min(78vh, 720px); }
  .hv-main.layout-split { grid-template-columns: 1fr; grid-template-rows: 1fr 9px minmax(0, 0.95fr); }
  .hv-main.layout-split .hv-splitter { cursor: row-resize; }
}

/* 2.1 编辑器 */
.hv-code { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--ui-bg); }
.hv-code-head {
  display: flex; align-items: center; gap: 8px; flex-shrink: 0;
  padding: 0 10px;
  border-bottom: 1px solid var(--ui-line-soft);
  background: var(--ui-bg-sub);
}
.hv-code-head .ui-tabs { border-bottom: none; flex: 1; min-width: 0; overflow-x: auto; scrollbar-width: none; }
.hv-code-head .ui-tabs::-webkit-scrollbar { display: none; }
.hv-code-head .ui-tabs .ink { bottom: 0; }
.hv-code-tools { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
.hv-code-tools button {
  padding: 5px; border: 0; border-radius: 7px; line-height: 0; background: none;
  color: var(--ui-text-mute); cursor: pointer; transition: var(--transition);
}
.hv-code-tools button:hover { background: var(--ui-bg-sunken); color: var(--ui-primary); }

.hv-editors { position: relative; flex: 1; min-height: 0; }
.hv-editor { position: absolute; inset: 0; display: flex; }
.hv-gutter {
  flex-shrink: 0; width: 46px; padding: 10px 8px 10px 0;
  text-align: right; user-select: none; overflow: hidden; white-space: pre;
  font: var(--hv-font) / var(--hv-lh) var(--font-mono);
  color: var(--ui-text-mute);
  background: var(--ui-bg-sub);
  border-right: 1px solid var(--ui-line-soft);
}
.hv-area { position: relative; flex: 1; min-width: 0; }
.hv-hl, .hv-ta {
  margin: 0; padding: 10px 14px; border: 0; border-radius: 0;
  font: var(--hv-font) / var(--hv-lh) var(--font-mono);
  tab-size: 4; white-space: pre; overflow: auto;
}
.hv-hl { position: absolute; inset: 0; overflow: hidden; pointer-events: none; color: var(--ui-text); }
.hv-ta {
  position: absolute; inset: 0; z-index: 1;
  background: transparent; color: transparent;
  caret-color: var(--ui-text); resize: none; outline: none;
}
.hv-ta::selection { background: rgba(0, 0, 0, 0.075); }
.hv-editor.wrap .hv-hl, .hv-editor.wrap .hv-ta { white-space: pre-wrap; word-break: break-word; }

/* 代码高亮 token */
.hv-hl .tk-com { color: var(--tk-com); font-style: italic; }
.hv-hl .tk-str { color: var(--tk-str); }
.hv-hl .tk-num { color: var(--tk-num); }
.hv-hl .tk-kw { color: var(--tk-kw); }
.hv-hl .tk-tag { color: var(--tk-tag); }
.hv-hl .tk-attr { color: var(--tk-attr); }
.hv-hl .tk-punt { color: var(--tk-punt); }

/* 2.2 分割条 */
.hv-splitter {
  position: relative; background: var(--ui-bg-sub);
  cursor: col-resize; user-select: none; touch-action: none;
}
.hv-main.layout-vertical .hv-splitter { cursor: row-resize; }
.hv-splitter::after {
  content: ''; position: absolute; inset: 0; margin: auto;
  background: var(--ui-line); border-radius: 2px;
  transition: background .18s var(--ui-ease);
}
.hv-main.layout-split .hv-splitter::after { width: 3px; height: 34px; }
.hv-main.layout-vertical .hv-splitter::after { width: 34px; height: 3px; }
.hv-splitter:hover::after, .hv-splitter.dragging::after { background: var(--ui-primary); }
/* 拖动分割条时：预览 iframe 不能吃掉指针事件，否则指针一进入就断拖 */
.hv-main.dragging { user-select: none; }
.hv-main.dragging .hv-frame { pointer-events: none; }
.hv-main.layout-split.dragging, .hv-main.layout-split.dragging .hv-splitter { cursor: col-resize; }
.hv-main.layout-vertical.dragging, .hv-main.layout-vertical.dragging .hv-splitter { cursor: row-resize; }

/* 2.3 预览 */
.hv-preview { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--ui-bg-sub); }
.hv-preview-head {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap; flex-shrink: 0;
  padding: 8px 10px;
  border-bottom: 1px solid var(--ui-line-soft);
  background: var(--ui-bg-sub);
}
.hv-url {
  flex: 1; min-width: 120px; display: flex; align-items: center; gap: 6px;
  height: var(--ui-h-sm); padding: 0 10px;
  border: 1px solid var(--ui-line-soft); border-radius: 8px;
  background: var(--ui-bg); font-size: 11.5px; color: var(--ui-text-mute);
  overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
}
.hv-url .ui-i { flex-shrink: 0; }
.hv-stage {
  flex: 1; min-height: 0; display: flex; align-items: stretch; justify-content: center;
  overflow: auto; padding: 0;
}
.hv-frame {
  width: 100%; height: 100%; border: 0; background: var(--surface); display: block;
  transition: width .2s var(--ui-ease);
}
.hv-stage.device .hv-frame { box-shadow: var(--shadow-md); }
.hv-stage.device { padding: 12px; }

/* 全屏预览 */
.hv-preview:fullscreen { background: var(--ui-bg); }
.hv-preview:fullscreen .hv-preview-head { background: var(--ui-bg-sub); }

/* --------------------------------------------------------------------------
   3. 控制台
   -------------------------------------------------------------------------- */
.hv-console {
  border: 1px solid var(--ui-line-soft);
  border-radius: var(--r);
  background: var(--ui-bg);
  overflow: hidden;
}
.hv-console-head {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 14px;
  background: var(--ui-bg-sub);
  cursor: pointer;
}
.hv-console-title { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 600; }
.hv-console-title > .ui-i { color: var(--ui-primary); }
.hv-console-head .sp { flex: 1; }
.hv-console-head .caret { color: var(--ui-text-mute); transition: transform .2s var(--ui-ease); }
.hv-console.open .hv-console-head .caret { transform: rotate(180deg); }
.hv-console-body { max-height: 240px; overflow: auto; border-top: 1px solid var(--ui-line-soft); }
.hv-console:not(.open) .hv-console-body { display: none; }
.hv-log {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 6px 14px;
  border-bottom: 1px solid var(--ui-line-soft);
  font: 12px / 1.7 var(--font-mono);
}
.hv-log:last-child { border-bottom: 0; }
.hv-log .lv {
  flex-shrink: 0; width: 42px; text-align: right;
  font-size: 10.5px; font-weight: 700; letter-spacing: .3px;
  text-transform: uppercase; padding-top: 1px;
}
.hv-log .msg { flex: 1; min-width: 0; white-space: pre-wrap; word-break: break-word; }
.hv-log.info .lv { color: var(--ui-info-text); }
.hv-log.log .lv { color: var(--ui-text-mute); }
.hv-log.warn { background: var(--ui-warning-soft); }
.hv-log.warn .lv { color: var(--ui-warning-text); }
.hv-log.error { background: var(--ui-danger-soft); }
.hv-log.error .lv { color: var(--ui-danger); }
.hv-log.sys .lv { color: var(--ui-success); }
.hv-console-empty { padding: 18px 14px; text-align: center; font-size: 12px; color: var(--ui-text-mute); }

/* --------------------------------------------------------------------------
   4. 资源 / 模板 / 设置弹窗内容
   -------------------------------------------------------------------------- */
.hv-modal-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 14px; }
@media (max-width: 560px) { .hv-modal-grid { grid-template-columns: 1fr; } }
.hv-lib {
  display: flex; align-items: center; gap: 9px;
  padding: 8px 10px; border-radius: 9px; cursor: pointer;
  transition: var(--transition);
}
.hv-lib:hover { background: var(--ui-bg-sub); }
.hv-lib .meta { min-width: 0; }
.hv-lib .nm { display: block; font-size: 12.5px; }
.hv-lib .ds { display: block; font-size: 11px; color: var(--ui-text-mute); }
.hv-custom { display: flex; gap: 8px; margin-top: 10px; }
.hv-custom .ui-input { flex: 1; }
.hv-tpl { display: flex; flex-direction: column; gap: 6px; }
.hv-tpl-item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 9px 12px; border: 1px solid var(--ui-line-soft); border-radius: 10px;
  background: var(--ui-bg); cursor: pointer; text-align: left;
  transition: var(--transition);
}
.hv-tpl-item:hover { border-color: var(--ui-primary); background: var(--ui-primary-soft); }
.hv-tpl-item .ui-i { color: var(--ui-primary); flex-shrink: 0; }
.hv-tpl-item .nm { font-size: 13px; font-weight: 600; }
.hv-tpl-item .ds { font-size: 11.5px; color: var(--ui-text-mute); }
.hv-code-box {
  padding: 10px 12px; border-radius: 8px;
  font: 12px / 1.6 var(--font-mono);
  background: var(--ui-bg-sunken); color: var(--ui-text-sub);
  word-break: break-all; white-space: pre-wrap; max-height: 200px; overflow: auto;
}

/* 底部说明 */
.hv-foot {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 12px 16px;
  border: 1px solid var(--ui-line-soft);
  border-radius: var(--r);
  background: var(--ui-bg-sub);
}
.hv-tool .ui-btn[disabled] { opacity: .52; cursor: not-allowed; pointer-events: none; }
