/* ==========================================================================
   HTML 查看器 —— 界面样式

   取向：这是一个工具，不是产品落地页。所以
     · 只有一个主色（朱红），其余全是暖调中性灰
     · 层次靠"更轻的描边 + 更深的阴影"，不靠卡片套卡片
     · 动效只服务于状态变化，时长压在 300ms 以内，全部用自定义缓动
     · 触屏基线：无点击灰闪、按下去立刻有反馈、刘海与 Home 指示条留白

   移动端正确性相关的声明都标了 why，改之前先读注释。
   ========================================================================== */

:root {
  /* 暖调纸面，而不是通用的冷灰蓝 */
  --bg: #faf9f7;
  --surface: #ffffff;
  --surface-2: #f2f0ee;
  --text: #191817;
  --text-2: #6b6660;
  --text-3: #9a938c;
  --line: #e8e4e0;
  --line-strong: #d8d3ce;

  /* 单一主色：朱红 */
  --accent: #d9481c;
  --accent-press: #c03d14;
  --accent-ink: #ffffff;
  --accent-wash: #fdf1ec;

  --danger: #c0392b;

  --r-sm: 8px;
  --r: 11px;
  --r-lg: 16px;
  --r-full: 999px;

  /* 分层阴影：近距离一层薄的定结构，远距离一层大的定浮起高度 */
  --shadow-1: 0 1px 2px rgb(24 20 16 / 0.06);
  --shadow-2: 0 1px 2px rgb(24 20 16 / 0.05), 0 6px 16px -6px rgb(24 20 16 / 0.12);
  --shadow-3: 0 1px 3px rgb(24 20 16 / 0.06), 0 18px 40px -12px rgb(24 20 16 / 0.2);

  /* 自定义缓动：CSS 内置的 ease-out 太弱，起手没有"立刻响应"的劲 */
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  --ease-in-out: cubic-bezier(0.77, 0, 0.175, 1);
  --ease-drawer: cubic-bezier(0.32, 0.72, 0, 1);

  --topbar-h: 52px;
}

@media (prefers-color-scheme: dark) {
  :root {
    /* 暖黑，不是纯黑的冷灰 */
    --bg: #131211;
    --surface: #1c1a18;
    --surface-2: #262421;
    --text: #f2efec;
    --text-2: #a39c95;
    --text-3: #766f68;
    --line: #2c2926;
    --line-strong: #3b3733;

    --accent: #ff6a38;
    --accent-press: #ef5a28;
    --accent-ink: #1a1005;
    --accent-wash: #2a1c14;

    --shadow-1: 0 1px 2px rgb(0 0 0 / 0.4);
    --shadow-2: 0 1px 2px rgb(0 0 0 / 0.4), 0 6px 16px -6px rgb(0 0 0 / 0.5);
    --shadow-3: 0 1px 3px rgb(0 0 0 / 0.4), 0 18px 40px -12px rgb(0 0 0 / 0.6);
  }
}

* { box-sizing: border-box; }

/* --------------------------------------------------------------------------
   hidden 必须能压过组件自身的 display。
   下面 .empty{display:grid} / .paste{display:flex} 会覆盖浏览器默认的
   [hidden]{display:none}，导致 hidden 形同虚设（踩过：空状态赖着不走、
   粘贴面板一直挂在屏幕下方）。这里统一兜住。
   -------------------------------------------------------------------------- */
[hidden] { display: none !important; }

html {
  /* iOS 点任何带事件的元素都会盖一层半透明灰——"网页感"最大的来源 */
  -webkit-tap-highlight-color: transparent;
  /* 横屏时 iOS 会自作主张放大字号 */
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  /* 整页不参与下拉刷新/橡皮筋，滚动交给内部各自的容器 */
  overscroll-behavior: none;
}

body {
  margin: 0;
  height: 100dvh; /* dvh 随地址栏收放变化；100vh 在加载瞬间会超出屏幕 */
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "SF Pro SC", "PingFang SC",
        "Helvetica Neue", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  /* 告诉浏览器这里不会双击缩放，省掉 300ms 点击延迟 */
  touch-action: manipulation;
  /* 控件文字不该被长按选中；正文才需要 */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

h1, h2, p, ul, ol, dl, dd, dt { margin: 0; }
b, strong { font-weight: 600; }

code, kbd {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9em;
  background: var(--surface-2);
  padding: 0.12em 0.36em;
  border-radius: 5px;
}

/* ============================== 顶栏 ============================== */

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: var(--topbar-h);
  padding: 6px 10px 6px 14px;
  /* 背景铺到状态栏底下，再把内容压回安全区 */
  padding-top: calc(6px + env(safe-area-inset-top, 0px));
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: saturate(160%) blur(14px);
  -webkit-backdrop-filter: saturate(160%) blur(14px);
  border-bottom: 1px solid var(--line);
  position: relative;
  z-index: 5;
}

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

/* 用文字做标，不用彩色渐变方块 + 白色图标那种模板套路 */
.stamp {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: var(--r-sm);
  background: var(--text);
  color: var(--bg);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: -0.06em;
}

.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
  line-height: 1.25;
}

.brand-text b {
  font-size: 14.5px;
  font-weight: 600;
  letter-spacing: -0.012em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-text i {
  font-style: normal;
  font-size: 11.5px;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 4px;
}

.badge {
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  color: var(--text-2);
  background: var(--surface-2);
  padding: 3px 8px;
  border-radius: var(--r-full);
}

/* 次级按钮：文字型，不带边框，靠底色变化表达按下 */
.ghost-btn {
  min-height: 32px;
  padding: 0 10px;
  border-radius: var(--r-sm);
  font-size: 13px;
  font-weight: 500;
  color: var(--text-2);
  transition: background 130ms var(--ease-out), color 130ms var(--ease-out),
              transform 130ms var(--ease-out);
}

.ghost-btn:active {
  background: var(--surface-2);
  color: var(--text);
  transform: scale(0.97);
}

.ghost-btn.wide { width: 100%; min-height: 44px; }

.icon-btn {
  display: grid;
  place-items: center;
  /* 44px 是苹果规定的最小可点区域 */
  width: 44px;
  height: 44px;
  border-radius: var(--r-sm);
  color: var(--text-2);
  transition: background 130ms var(--ease-out), transform 130ms var(--ease-out);
}

.icon-btn:active { background: var(--surface-2); transform: scale(0.94); }

/* ------------------------------ 溢出菜单 ------------------------------ */

.menu-wrap { position: relative; }

.menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 20;
  min-width: 168px;
  padding: 5px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r);
  box-shadow: var(--shadow-3);
  /* 从触发按钮长出，而不是从中心冒出来 */
  transform-origin: top right;
  opacity: 1;
  transform: scale(1) translateY(0);
  transition: opacity 150ms var(--ease-out), transform 150ms var(--ease-out);

  @starting-style {
    opacity: 0;
    transform: scale(0.96) translateY(-4px);
  }
}

.menu button {
  display: block;
  width: 100%;
  min-height: 38px;
  padding: 0 11px;
  border-radius: var(--r-sm);
  font-size: 14px;
  text-align: left;
  color: var(--text);
  transition: background 110ms var(--ease-out);
}

.menu button:active { background: var(--surface-2); }

.menu hr {
  margin: 5px 8px;
  border: 0;
  border-top: 1px solid var(--line);
}

/* ============================== 空状态 ============================== */

.empty {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 18px 22px calc(14px + env(safe-area-inset-bottom, 0px));
  overflow-y: auto;
  overscroll-behavior: contain;
}

.empty-inner {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 100%;
  max-width: 380px;
  margin: 0 auto;
  text-align: center;
}

/* 页脚：给这块竖长的空状态一个收口，也顺便声明隐私边界 */
.foot {
  flex: 0 0 auto;
  padding-top: 24px;
  font-size: 11.5px;
  color: var(--text-3);
  text-align: center;
}

.figure {
  color: var(--text);
  margin-bottom: 20px;
}

.figure svg { display: block; margin: 0 auto; }

.empty h1 {
  font-size: 22px;
  font-weight: 640;
  /* 大字号要收紧字距，否则会显得松散 */
  letter-spacing: -0.022em;
  line-height: 1.3;
  margin-bottom: 10px;
}

.lede {
  font-size: 14.5px;
  line-height: 1.6;
  color: var(--text-2);
  margin-bottom: 20px;
  text-wrap: pretty;
}

/* 主按钮：实心朱红 + 极轻的内高光，按下时下沉 */
.primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 48px;
  padding: 0 20px;
  border-radius: var(--r);
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 15.5px;
  font-weight: 600;
  letter-spacing: 0.005em;
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.16), var(--shadow-1);
  transition: background 140ms var(--ease-out), transform 140ms var(--ease-out),
              box-shadow 140ms var(--ease-out);
}

.primary:active {
  background: var(--accent-press);
  transform: scale(0.985);
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.1), none;
}

.hint {
  margin-top: 16px;
  font-size: 12.5px;
  line-height: 1.65;
  color: var(--text-3);
  text-wrap: pretty;
}

.hint b { color: var(--text-2); font-weight: 600; }

.alt {
  margin-top: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-size: 13px;
  color: var(--text-3);
}

.alt a, .link-btn {
  color: var(--text-2);
  text-decoration: none;
  border-bottom: 1px solid var(--line-strong);
  padding-bottom: 1px;
  font-size: 13px;
  transition: color 130ms var(--ease-out), border-color 130ms var(--ease-out);
}

.alt a:active, .link-btn:active {
  color: var(--accent);
  border-color: var(--accent);
}

/* ============================== 预览 ============================== */

.stage {
  flex: 1 1 auto;
  min-height: 0;
  position: relative;
  display: flex;
  background: var(--surface);
}

#frame {
  flex: 1 1 auto;
  width: 100%;
  height: 100%;
  border: 0;
  background: #fff;
  /* 就绪前先隐形，避免闪一下白 */
  opacity: 0;
  transition: opacity 200ms var(--ease-out);
}

#frame.ready { opacity: 1; }

/* 只在渲染中/出错时出现；成功后就退场，不常驻遮挡内容 */
.status {
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  max-width: calc(100% - 24px);
  padding: 5px 12px;
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text-2);
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid var(--line);
  border-radius: var(--r-full);
  box-shadow: var(--shadow-1);
  pointer-events: none;
  opacity: 1;
  transition: opacity 260ms var(--ease-out), transform 260ms var(--ease-out);

  @starting-style {
    opacity: 0;
    transform: translate(-50%, 6px);
  }
}

.status.out {
  opacity: 0;
  transform: translate(-50%, 6px);
}

/* ============================== 源码 ============================== */

.source {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
}

.source-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 8px 5px 14px;
  border-bottom: 1px solid var(--line);
}

.source-label {
  font-size: 11.5px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-3);
}

.source-actions { display: flex; gap: 2px; }

.source pre {
  flex: 1 1 auto;
  margin: 0;
  padding: 14px 14px calc(16px + env(safe-area-inset-bottom, 0px));
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.62;
  white-space: pre;
  tab-size: 2;
  /* 源码是"内容"，必须能被选中复制 */
  -webkit-user-select: text;
  user-select: text;
}

/* 底部：档位用分段控件（最常用，常驻），视图切换与工具收成图标按钮 */
.toolbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 7px 8px calc(7px + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  border-top: 1px solid var(--line);
}

.seg {
  display: flex;
  gap: 2px;
  padding: 3px;
  background: var(--surface-2);
  border-radius: 10px;
}

.seg button {
  min-height: 34px;
  padding: 0 11px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-2);
  transition: background 140ms var(--ease-out), color 140ms var(--ease-out),
              box-shadow 140ms var(--ease-out);
}

.seg button:active { transform: scale(0.97); }

.seg button.on {
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  box-shadow: var(--shadow-1);
}

@media (prefers-color-scheme: dark) {
  .seg button.on { background: var(--line-strong); }
}

.tools { display: flex; align-items: center; gap: 2px; }

/* 图标按钮在选中态用淡色底而不是描边，避免和分段控件抢注意力 */
.tool-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 38px;
  border-radius: var(--r-sm);
  color: var(--text-3);
  transition: background 140ms var(--ease-out), color 140ms var(--ease-out),
              transform 140ms var(--ease-out);
}

.tool-btn:active { background: var(--surface-2); transform: scale(0.94); }

.tool-btn.on {
  background: var(--accent-wash);
  color: var(--accent);
}

/* 同一个按钮承担"进源码 / 回预览"两个方向：两个图标叠在一起按状态切换 */
.tool-btn .ic-a, .tool-btn .ic-b { display: grid; place-items: center; grid-area: 1 / 1; }
.tool-btn .ic-b { display: none; }
.tool-btn.on .ic-a { display: none; }
.tool-btn.on .ic-b { display: grid; }

/* ============================== 粘贴模式 ============================== */

.paste {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
}

.paste-inner {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom, 0px));
}

.paste-inner label {
  font-size: 12.5px;
  color: var(--text-3);
}

#paste-text {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  padding: 12px;
  border: 1px solid var(--line-strong);
  border-radius: var(--r);
  background: var(--surface);
  color: var(--text);
  resize: none;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  /* 16px 是硬要求：小于它 iOS 聚焦时会把整页放大且不缩回来 */
  font-size: 16px;
  line-height: 1.55;
  transition: border-color 140ms var(--ease-out), box-shadow 140ms var(--ease-out);
}

#paste-text:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-wash);
}

.paste-actions { display: flex; align-items: center; gap: 10px; }
.paste-actions .primary { flex: 1 1 auto; width: auto; }
.paste-actions .ghost-btn {
  min-height: 48px;
  padding: 0 20px;
  background: var(--surface-2);
}

/* ============================== 提示 ============================== */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(86px + env(safe-area-inset-bottom, 0px));
  z-index: 40;
  max-width: min(92vw, 420px);
  padding: 9px 14px;
  border-radius: var(--r);
  background: var(--text);
  color: var(--bg);
  font-size: 13.5px;
  line-height: 1.5;
  text-align: center;
  box-shadow: var(--shadow-2);
  opacity: 0;
  transform: translate(-50%, 10px);
  transition: opacity 200ms var(--ease-out), transform 200ms var(--ease-out);
  pointer-events: none;
}

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

/* ============================== 说明弹层 ============================== */

.sheet {
  width: min(92vw, 440px);
  padding: 22px 20px 16px;
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  box-shadow: var(--shadow-3);
  /* 弹层不是从某个触发点长出来的，保持从中心缩放 */
  opacity: 1;
  transform: scale(1);
  transition: opacity 200ms var(--ease-out), transform 200ms var(--ease-out);

  @starting-style {
    opacity: 0;
    transform: scale(0.96);
  }
}

.sheet::backdrop {
  background: rgb(20 16 12 / 0.4);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  opacity: 1;
  transition: opacity 200ms var(--ease-out);

  @starting-style { opacity: 0; }
}

.sheet h2 {
  font-size: 16.5px;
  font-weight: 640;
  letter-spacing: -0.014em;
  margin-bottom: 14px;
}

.sheet-body { font-size: 14px; }

.kv {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 9px 16px;
}

.kv dt { color: var(--text-3); font-size: 13px; }
.kv dd { color: var(--text); word-break: break-all; font-size: 13px; }

.steps { padding-left: 19px; }
.steps li { margin: 9px 0; line-height: 1.6; color: var(--text-2); }
.steps li b { color: var(--text); }

.note {
  margin-top: 14px;
  padding-left: 11px;
  border-left: 2px solid var(--line-strong);
  font-size: 12.5px;
  line-height: 1.65;
  color: var(--text-3);
}

.note b { color: var(--text-2); }

/* ============================== 杂项 ============================== */

/* 拖拽（桌面顺手用） */
body.dragging::after {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 50;
  border: 2px dashed var(--accent);
  background: var(--accent-wash);
  pointer-events: none;
}

@media (max-width: 390px) {
  .seg button { padding: 0 9px; font-size: 12.5px; }
  .empty h1 { font-size: 20px; }
  .figure svg { width: 172px; height: 100px; }
}

/* 触屏没有真悬停：所有 hover 效果必须按能力查询，否则点完会粘住 */
@media (hover: hover) and (pointer: fine) {
  .primary:hover { background: var(--accent-press); }
  .ghost-btn:hover { background: var(--surface-2); }
  .icon-btn:hover { background: var(--surface-2); }
  .menu button:hover { background: var(--surface-2); }
  .alt a:hover, .link-btn:hover { color: var(--accent); border-color: var(--accent); }
}

/* 尊重系统的"减弱动态效果"：去掉位移，保留透明度变化（透明度有助于理解状态变化） */
@media (prefers-reduced-motion: reduce) {
  * {
    transition-duration: 120ms !important;
    animation-duration: 120ms !important;
  }
  .toast, .sheet, .menu, .status { transform: none !important; }
  button:active { transform: none !important; }
}
