/**
 * HTML 格式化 / 美化 / 压缩 — yicool.com.cn /tools/htmlfmt
 * 复刻 tools.ai225.com/tools/html-formatter：
 *   工具栏「格式化 · 缩进选择 · 压缩 · 示例 · 清空」+ 错误提示条 + 输入/输出双栏（字符计数 + 复制结果）
 * 设计沿用原站工具卡视觉（卡片 + 圆角 12px + 等宽代码框 + 红色错误条），并适配本站整屏布局与明暗主题。
 * 变量统一定义在 :root（避免 fixed 元素取不到容器变量，见 goods 工具同类修复）。
 */

/* ---------- 主题变量（暗色默认 + 亮色覆盖） ---------- */
:root {
    --hf-card: #16181c;
    --hf-card-head: #1c1f24;
    --hf-border: #2f3336;
    --hf-border-soft: #23262b;
    --hf-text: #e7e9ea;
    --hf-sub: #8b98a5;
    --hf-code-bg: #0f1115;
    --hf-code-bg-2: #0a0c0f;
    --hf-shadow: 0 4px 20px rgba(0, 0, 0, .28);
    --hf-err-bg: rgba(244, 33, 46, .10);
    --hf-err-border: rgba(244, 33, 46, .38);
    --hf-err-text: #ff8b93;
    --hf-ok: #00ba7c;
}

[data-theme="light"] {
    --hf-card: #ffffff;
    --hf-card-head: #f7f9f9;
    --hf-border: #e3e8ea;
    --hf-border-soft: #eef2f4;
    --hf-text: #0f1419;
    --hf-sub: #65737e;
    --hf-code-bg: #f7f9fa;
    --hf-code-bg-2: #eef2f4;
    --hf-shadow: 0 4px 18px rgba(15, 20, 25, .08);
    --hf-err-bg: rgba(244, 33, 46, .07);
    --hf-err-border: rgba(244, 33, 46, .30);
    --hf-err-text: #cf1420;
}

/* ---------- 整屏：解除全站 .app 限宽（工具页规范） ---------- */
body[data-page="tools"] .app,
body.page-htmlfmt-full .app { max-width: 100%; }

body[data-page="tools"] .main-content,
body.page-htmlfmt-full .main-content {
    max-width: 100%;
    width: 100%;
    border-right: 0;
}

/* ==================== 页面容器 ==================== */
.hf-page {
    max-width: none;
    width: 100%;
    margin: 0;
    box-sizing: border-box;
    padding: 18px 20px 40px;
    color: var(--hf-text);
}

/* ==================== 标题区 ==================== */
.hf-hero {
    display: flex;
    align-items: center;
    gap: 14px;
    margin-bottom: 14px;
    min-width: 0;
}

.hf-hero-icon {
    flex: 0 0 auto;
    width: 50px;
    height: 50px;
    border-radius: 13px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    background: linear-gradient(135deg, #f6812b 0%, #e0562f 55%, #d0342c 100%);
    box-shadow: 0 4px 14px rgba(224, 86, 47, .32);
}

.hf-hero-icon svg { width: 27px; height: 27px; }

.hf-title {
    font-size: 21px;
    font-weight: 700;
    line-height: 1.3;
    color: var(--hf-text);
    margin: 0 0 3px;
}

.hf-sub {
    font-size: 13px;
    line-height: 1.55;
    color: var(--hf-sub);
    margin: 0;
}

/* ==================== 工具卡 ==================== */
.hf-card {
    background: var(--hf-card);
    border: 1px solid var(--hf-border);
    border-radius: 14px;
    padding: 16px;
    box-shadow: var(--hf-shadow);
}

/* ---------- 工具栏（原站顺序：格式化 · 缩进 · 压缩 · 示例 · 清空） ---------- */
.hf-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin-bottom: 12px;
}

.hf-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 34px;
    padding: 0 14px;
    border-radius: 9px;
    border: 1px solid transparent;
    background: transparent;
    color: var(--hf-text);
    font-size: 13.5px;
    font-family: inherit;
    font-weight: 600;
    line-height: 1;
    cursor: pointer;
    white-space: nowrap;
    transition: background .15s, border-color .15s, color .15s, opacity .15s;
}

.hf-btn svg { width: 15px; height: 15px; flex: 0 0 auto; }

.hf-btn-primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

.hf-btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

.hf-btn-secondary {
    background: rgba(29, 155, 240, .12);
    border-color: rgba(29, 155, 240, .34);
    color: var(--accent);
}

.hf-btn-secondary:hover { background: rgba(29, 155, 240, .2); }

.hf-btn-outline {
    border-color: var(--hf-border);
    background: var(--hf-card-head);
    color: var(--hf-text);
}

.hf-btn-outline:hover { border-color: var(--accent); color: var(--accent); }

.hf-btn:disabled { opacity: .45; cursor: not-allowed; }

.hf-btn-xs { height: 26px; padding: 0 10px; font-size: 12px; border-radius: 7px; }
.hf-btn-xs svg { width: 12px; height: 12px; }

/* 缩进选择器（原站：灰底胶囊 + 小号 select） */
.hf-indent {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    height: 34px;
    padding: 0 8px 0 12px;
    background: var(--hf-card-head);
    border: 1px solid var(--hf-border-soft);
    border-radius: 9px;
}

.hf-indent-label {
    font-size: 12px;
    font-weight: 500;
    color: var(--hf-sub);
    white-space: nowrap;
}

.hf-indent select {
    height: 26px;
    border: 0;
    background: transparent;
    color: var(--hf-text);
    font-size: 13px;
    font-family: inherit;
    font-weight: 600;
    cursor: pointer;
    outline: none;
}

.hf-indent select option { background: var(--hf-card); color: var(--hf-text); }

/* 增强操作区（导入 / 下载 / 撤销 / 重做 / 预览开关 / 自动格式化） */
.hf-actions-right {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-left: auto;
    flex-wrap: wrap;
}

.hf-switch {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    height: 30px;
    padding: 0 10px;
    border: 1px solid var(--hf-border-soft);
    border-radius: 999px;
    background: var(--hf-card-head);
    font-size: 12.5px;
    font-weight: 500;
    color: var(--hf-sub);
    cursor: pointer;
    user-select: none;
}

.hf-switch input {
    width: 14px;
    height: 14px;
    margin: 0;
    accent-color: var(--accent);
    cursor: pointer;
}

.hf-switch:hover { color: var(--hf-text); }

/* ---------- 错误提示条（原站红底红字） ---------- */
.hf-error {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 12px;
    padding: 8px 10px;
    border: 1px solid var(--hf-err-border);
    border-radius: 8px;
    background: var(--hf-err-bg);
    color: var(--hf-err-text);
    font-size: 13px;
    line-height: 1.4;
    word-break: break-word;
}

.hf-error[hidden] { display: none !important; }

.hf-error svg { width: 17px; height: 17px; flex: 0 0 auto; }

/* ==================== 输入 / 输出 双栏 ==================== */
.hf-io {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
}

.hf-io-section {
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-width: 0;
}

.hf-io-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    min-height: 30px;
}

.hf-io-label {
    font-size: 14px;
    font-weight: 500;
    color: var(--hf-text);
}

.hf-io-header-right {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.hf-io-count {
    font-size: 12px;
    color: var(--hf-sub);
    white-space: nowrap;
}

.hf-io-count b { color: var(--hf-text); font-weight: 600; }

.hf-code {
    width: 100%;
    min-height: 460px;
    padding: 12px;
    border: 1px solid var(--hf-border);
    border-radius: 10px;
    background: var(--hf-code-bg);
    color: var(--hf-text);
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 13px;
    line-height: 1.65;
    resize: vertical;
    tab-size: 4;
    outline: none;
    overflow: auto;   /* wrap="off"：超长单行走横向滚动，避免浏览器做昂贵的软换行计算 */
    transition: border-color .15s, box-shadow .15s;
}

.hf-code:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(29, 155, 240, .16);
}

.hf-code[readonly] {
    background: var(--hf-code-bg-2);
    color: var(--hf-sub);
}

/* 拖拽文件到输入框时的高亮反馈 */
.hf-code.is-drag {
    border-color: var(--accent);
    border-style: dashed;
    box-shadow: 0 0 0 3px rgba(29, 155, 240, .16);
}

.hf-io-foot {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    min-height: 22px;
    font-size: 12px;
    color: var(--hf-sub);
}

.hf-io-foot .hf-tag {
    display: inline-flex;
    align-items: center;
    padding: 2px 8px;
    border-radius: 999px;
    background: rgba(29, 155, 240, .10);
    color: var(--accent);
    font-weight: 600;
}

.hf-io-foot .hf-tag.hf-tag-good {
    background: rgba(0, 186, 124, .12);
    color: var(--hf-ok);
}

/* 预览框（沙盒 iframe，禁止脚本） */
.hf-preview {
    display: none;
    width: 100%;
    height: 460px;
    border: 1px solid var(--hf-border);
    border-radius: 10px;
    background: #fff;
}

.hf-preview.is-on { display: block; }
.hf-code.is-off { display: none; }

/* ---------- 兜底提示（window.toast 不可用时） ---------- */
.hf-toast {
    display: none;
    position: fixed;
    left: 50%;
    bottom: 42px;
    transform: translateX(-50%);
    z-index: 120000;
    max-width: 86vw;
    padding: 9px 16px;
    border-radius: 999px;
    background: rgba(23, 26, 31, .94);
    color: #fff;
    font-size: 13px;
    line-height: 1.4;
    box-shadow: 0 6px 24px rgba(0, 0, 0, .3);
}

.hf-toast-err { background: rgba(200, 30, 40, .95); }

/* ==================== 使用说明 ==================== */
.hf-guide {
    margin-top: 16px;
    padding: 18px 20px;
    background: var(--hf-card);
    border: 1px solid var(--hf-border);
    border-radius: 14px;
    color: var(--hf-text);
}

.hf-guide h3 {
    font-size: 16px;
    font-weight: 700;
    margin: 0 0 8px;
}

.hf-guide h4 {
    font-size: 14px;
    font-weight: 700;
    margin: 14px 0 6px;
}

.hf-guide p {
    font-size: 13.5px;
    line-height: 1.8;
    color: var(--hf-sub);
    margin: 0 0 6px;
}

.hf-guide ul { margin: 0; padding-left: 20px; }

.hf-guide li {
    font-size: 13.5px;
    line-height: 1.9;
    color: var(--hf-sub);
}

.hf-guide code {
    padding: 1px 5px;
    border-radius: 5px;
    background: var(--hf-code-bg);
    font-size: 12.5px;
    color: var(--accent);
}

.hf-guide .hf-privacy {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px dashed var(--hf-border);
}

kbd {
    display: inline-block;
    padding: 1px 6px;
    border: 1px solid var(--hf-border);
    border-bottom-width: 2px;
    border-radius: 5px;
    background: var(--hf-card-head);
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: 12px;
    color: var(--hf-text);
}

/* ==================== 响应式 ==================== */
@media (max-width: 1099px) {
    .hf-io { grid-template-columns: minmax(0, 1fr); }
    .hf-code, .hf-preview { min-height: 340px; height: 340px; }
    .hf-actions-right { margin-left: 0; }
}

@media (max-width: 767px) {
    .hf-page { padding: 12px 12px 30px; }
    .hf-hero { gap: 10px; margin-bottom: 12px; }
    .hf-hero-icon { width: 42px; height: 42px; border-radius: 11px; }
    .hf-hero-icon svg { width: 23px; height: 23px; }
    .hf-title { font-size: 18px; }
    .hf-sub { font-size: 12.5px; }
    .hf-card { padding: 12px; border-radius: 12px; }
    .hf-actions { gap: 6px; }
    .hf-btn { flex: 1 1 auto; justify-content: center; height: 36px; padding: 0 10px; }
    .hf-indent { flex: 1 1 100%; justify-content: space-between; }
    .hf-indent select { flex: 1; text-align: right; }
    .hf-actions-right { flex: 1 1 100%; }
    .hf-switch, .hf-actions-right .hf-btn { flex: 1 1 auto; justify-content: center; }
    .hf-code, .hf-preview { min-height: 260px; height: 260px; }
    .hf-code { font-size: 16px; line-height: 1.6; }  /* 16px 防 iOS 聚焦缩放 */
    .hf-io-header { flex-wrap: wrap; }
    .hf-guide { padding: 14px; }
}

@media (max-width: 400px) {
    .hf-btn { font-size: 12.5px; }
    .hf-btn svg { display: none; }
}

@media print {
    .hf-actions, .hf-actions-right, .hf-guide, .hf-preview { display: none !important; }
    .hf-io { grid-template-columns: 1fr; }
    .hf-code { min-height: auto; height: auto; border: 0; }
}
