/*!
 * 直播源工具箱 · 统一界面增强层
 * 所有页面在自身样式之后引入本文件。只做"增强"，不改变各页面原有布局和配色：
 *   - 键盘焦点、跳转链接、减少动效、打印样式（可访问性 / 企业规范）
 *   - 电脑端：超宽屏利用、滚动条、悬停反馈
 *   - 移动端：安全区（刘海屏）、触控尺寸、禁止 iOS 输入框自动放大、表格横向滚动、长链接换行
 * 绝大部分选择器使用 :where()（优先级为 0），页面自身样式始终优先。
 */

:root {
  --ui-primary: var(--primary-color, #2196f3);
  --ui-radius: 10px;
  --ui-focus: 0 0 0 3px color-mix(in srgb, var(--ui-primary) 35%, transparent);
  --ui-font: system-ui, -apple-system, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  --ui-mono: ui-monospace, "SFMono-Regular", Consolas, "Cascadia Mono", Menlo, monospace;
}

/* ---------- 基础 ---------- */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  touch-action: manipulation;          /* 去掉移动端 300ms 点击延迟与双击放大，保留双指缩放 */
  scroll-padding-top: 16px;
}
:where(body) { font-family: var(--ui-font); }
body { -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-rendering: optimizeLegibility; }
:where(img, svg, canvas) { max-width: 100%; }
:where(pre, code, kbd, samp) { font-family: var(--ui-mono); }
:where(pre) { overflow-wrap: anywhere; }
::selection { background: color-mix(in srgb, var(--ui-primary) 25%, transparent); }
[hidden] { display: none !important; }

/* ---------- 键盘焦点（鼠标点击不显示，键盘 Tab 时显示） ---------- */
:where(a, button, input, select, textarea, summary, [tabindex], [role="button"]):focus-visible {
  outline: 2px solid var(--ui-primary) !important;
  outline-offset: 2px !important;
  box-shadow: var(--ui-focus);
}
:where(button, [role="button"], a.btn, .btn):disabled,
:where(button, .btn)[aria-disabled="true"] { cursor: not-allowed; opacity: .55; }

#main-content:focus, #main-content:focus-visible { outline: none !important; box-shadow: none !important; }

/* ---------- 跳转到主要内容（仅键盘可见） ---------- */
.ui-skip {
  position: fixed; left: 12px; top: -60px; z-index: 100000;
  padding: 10px 16px; border-radius: 8px;
  background: var(--ui-primary); color: #fff !important; font-weight: 600; text-decoration: none;
  transition: top .15s ease;
}
.ui-skip:focus { top: 12px; }

/* ---------- 返回工具箱 ---------- */
.ui-home {
  display: inline-flex; align-items: center; gap: 6px;
  margin: 0 0 10px; padding: 5px 12px; border-radius: 999px;
  font-size: 13px; line-height: 1.4; font-weight: 500; text-decoration: none;
  color: var(--text-secondary, #666) !important;
  background: color-mix(in srgb, var(--ui-primary) 8%, transparent);
  border: 1px solid color-mix(in srgb, var(--ui-primary) 18%, transparent);
  transition: background .2s, color .2s;
}
.ui-home:hover { color: var(--ui-primary) !important; background: color-mix(in srgb, var(--ui-primary) 14%, transparent); }
.ui-home-row { display: block; text-align: inherit; }

/* ---------- 表格横向滚动容器（JS 自动包裹） ---------- */
.ui-table-scroll {
  width: 100%; max-width: 100%;
  overflow-x: auto; -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
}

/* ---------- 回到顶部 ---------- */
.ui-top {
  position: fixed; z-index: 9990;
  right: max(16px, env(safe-area-inset-right)); bottom: max(20px, env(safe-area-inset-bottom));
  width: 44px; height: 44px; border-radius: 50%; border: 0;
  display: grid; place-items: center; cursor: pointer;
  background: var(--card-bg, #fff); color: var(--ui-primary);
  box-shadow: 0 6px 20px rgba(0,0,0,.18);
  opacity: 0; transform: translateY(12px); pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.ui-top.show { opacity: 1; transform: none; pointer-events: auto; }
.ui-top svg { width: 18px; height: 18px; }

/* ---------- 轻提示（替代阻塞式 alert） ---------- */
.ui-toast-wrap {
  position: fixed; z-index: 100001; left: 50%; transform: translateX(-50%);
  top: max(16px, env(safe-area-inset-top));
  display: flex; flex-direction: column; gap: 8px; align-items: center;
  width: min(92vw, 420px); pointer-events: none;
}
.ui-toast {
  --tc: #2563eb;
  pointer-events: auto; width: 100%;
  display: flex; gap: 10px; align-items: flex-start;
  padding: 12px 12px 12px 14px; border-radius: 10px;
  background: #fff; color: #101828; font-size: 14px; line-height: 1.5;
  border: 1px solid #e4e7ec; border-left: 3px solid var(--tc);
  box-shadow: 0 16px 32px -10px rgba(16,24,40,.22), 0 2px 6px rgba(16,24,40,.06);
  white-space: pre-line; overflow-wrap: anywhere;
  animation: ui-toast-in .22s cubic-bezier(.2,.7,.3,1);
}
.ui-toast-success { --tc: #16a34a; }
.ui-toast-warning { --tc: #f59e0b; }
.ui-toast-error   { --tc: #dc2626; }
.ui-toast-icon { flex: none; width: 20px; height: 20px; color: var(--tc); margin-top: 1px; }
.ui-toast-icon svg { width: 20px; height: 20px; display: block; }
.ui-toast-text { flex: 1; min-width: 0; }
.ui-toast .ui-toast-close {
  flex: none; margin: -2px -2px 0 4px; width: 24px; height: 24px; min-height: 0 !important;
  display: grid; place-items: center; border: 0; border-radius: 6px;
  background: none; color: #667085; cursor: pointer; font-size: 18px; line-height: 1; padding: 0;
}
.ui-toast .ui-toast-close:hover { background: #f2f4f7; color: #101828; }
.ui-toast.out { animation: ui-toast-out .2s ease-in forwards; }
@keyframes ui-toast-in { from { opacity: 0; transform: translateY(-8px) scale(.98); } }
@keyframes ui-toast-out { to { opacity: 0; transform: translateY(-8px); } }

/* ---------- 确认对话框（替代原生 confirm） ---------- */
html.ui-dialog-open, html.ui-dialog-open body { overflow: hidden; }
.ui-dialog-backdrop {
  position: fixed; inset: 0; z-index: 100002;
  display: grid; place-items: center; padding: 20px;
  background: rgba(15, 23, 42, .45);
  -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px);
  opacity: 0; transition: opacity .16s ease;
}
.ui-dialog-backdrop.in { opacity: 1; }
.ui-dialog-backdrop.out { opacity: 0; }
.ui-dialog {
  --dc: #2563eb; --dc-soft: #eff4ff;
  width: min(100%, 420px);
  display: grid; grid-template-columns: 44px 1fr; column-gap: 14px; row-gap: 20px;
  padding: 22px 22px 18px; border-radius: 14px;
  background: #fff; color: #101828; border: 1px solid #e4e7ec;
  box-shadow: 0 24px 48px -12px rgba(16,24,40,.30);
  font-family: var(--ui-font);
  transform: translateY(8px) scale(.98); transition: transform .18s cubic-bezier(.2,.7,.3,1);
}
.ui-dialog-backdrop.in .ui-dialog { transform: none; }
.ui-dialog.is-danger { --dc: #dc2626; --dc-soft: #fef3f2; }
.ui-dialog-icon {
  width: 44px; height: 44px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--dc-soft); color: var(--dc);
  box-shadow: 0 0 0 6px color-mix(in srgb, var(--dc-soft) 55%, transparent);
}
.ui-dialog-icon svg { width: 22px; height: 22px; }
.ui-dialog-body { min-width: 0; padding-top: 2px; }
.ui-dialog-title { margin: 0 0 6px; font-size: 16px; line-height: 1.45; font-weight: 600; color: #101828; }
.ui-dialog-msg { margin: 0; font-size: 14px; line-height: 1.6; color: #475467; white-space: pre-line; overflow-wrap: anywhere; }
.ui-dialog-actions { grid-column: 1 / -1; display: flex; justify-content: flex-end; gap: 10px; }
.ui-dialog-btn {
  min-width: 88px; height: 38px; padding: 0 16px; border-radius: 8px;
  border: 1px solid #d0d5dd; background: #fff; color: #344054;
  font: 500 14px/1 var(--ui-font); cursor: pointer;
  box-shadow: 0 1px 2px rgba(16,24,40,.05);
  transition: background-color .15s, border-color .15s, box-shadow .15s;
}
.ui-dialog-btn:hover { background: #f9fafb; }
.ui-dialog-btn.is-primary { background: #2563eb; border-color: #2563eb; color: #fff; }
.ui-dialog-btn.is-primary:hover { background: #1d4ed8; border-color: #1d4ed8; }
.ui-dialog-btn.is-danger { background: #dc2626; border-color: #dc2626; color: #fff; }
.ui-dialog-btn.is-danger:hover { background: #b91c1c; border-color: #b91c1c; }
@media (max-width: 480px) {
  .ui-dialog { grid-template-columns: 1fr; justify-items: center; text-align: center; padding: 22px 18px 16px; }
  .ui-dialog-actions { width: 100%; flex-direction: column-reverse; }
  .ui-dialog-btn { width: 100%; height: 44px; }
}
@media (prefers-color-scheme: dark) {
  .ui-toast, .ui-dialog { background: #111827; color: #e5e7eb; border-color: #243045; }
  .ui-toast .ui-toast-close { color: #8a94a6; }
  .ui-toast .ui-toast-close:hover { background: #1e293b; color: #e5e7eb; }
  .ui-dialog { --dc-soft: rgba(59,130,246,.16); }
  .ui-dialog.is-danger { --dc-soft: rgba(239,68,68,.16); }
  .ui-dialog-title { color: #e5e7eb; }
  .ui-dialog-msg { color: #a7b0be; }
  .ui-dialog-btn { background: #161f2e; border-color: #334155; color: #e5e7eb; }
  .ui-dialog-btn:hover { background: #1e293b; }
}
/* 播放器使用 body.dark-mode 切换深浅色 */
body.dark-mode .ui-toast, body.dark-mode .ui-dialog { background: #1a1d24; color: #e5e7eb; border-color: #2c313c; }
body.dark-mode .ui-dialog-title { color: #e5e7eb; }
body.dark-mode .ui-dialog-msg { color: #a7b0be; }
body.dark-mode .ui-dialog-btn:not(.is-primary):not(.is-danger) { background: #232833; border-color: #353b48; color: #e5e7eb; }

/* ---------- 无 JS 提示 ---------- */
.ui-noscript { margin: 12px; padding: 12px 16px; border-radius: 8px; background: #fff3cd; color: #664d03; border: 1px solid #ffe69c; font-size: 14px; }

/* ---------- 电脑端 ---------- */
@media (min-width: 1680px) {
  .container { max-width: 1560px; }                 /* 超宽屏多利用一些空间 */
}
@media (hover: hover) and (pointer: fine) {
  ::-webkit-scrollbar { width: 10px; height: 10px; }
  ::-webkit-scrollbar-thumb { background: rgba(127,127,127,.35); border-radius: 8px; border: 2px solid transparent; background-clip: padding-box; }
  ::-webkit-scrollbar-thumb:hover { background-color: rgba(127,127,127,.55); }
  ::-webkit-scrollbar-track { background: transparent; }
}

/* ---------- 移动端 ---------- */
@media (max-width: 768px) {
  /* 输入框字号 < 16px 时 iOS 会自动放大页面 */
  :where(input:not([type="checkbox"]):not([type="radio"]):not([type="range"]), select, textarea) { font-size: 16px !important; }
  /* 刘海屏 / 横屏安全区 */
  .container {
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
  /* 长链接、长频道名不撑破布局 */
  :where(td, th, li, p, .result-url, .stream-url, .history-url, .url-cell, .log-message, .log-item) { overflow-wrap: anywhere; }
  .ui-toast-wrap { width: calc(100vw - 24px); }
}
@media (pointer: coarse) {
  /* 触控目标不小于 40px（WCAG 2.5.8）。播放器页的视频内控件尺寸由播放器自己控制，不受影响 */
  :where(body:not(.ui-player)) :where(button, .btn, select, input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"])) { min-height: 40px; }
  :where(body:not(.ui-player)) :where(input[type="checkbox"], input[type="radio"]) { min-width: 18px; min-height: 18px; }
  :where(body:not(.ui-player)) :where(input[type="range"]) { min-height: 28px; }
  :where(body.ui-player) :where(.header button, .sidebar button, .panel button, .modal button) { min-height: 40px; }
}

/* ---------- 减少动效（系统设置了"减弱动态效果"时） ---------- */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto !important; }
  *, *::before, *::after {
    animation-duration: .01ms !important; animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}

/* ---------- 打印 ---------- */
@media print {
  .ui-top, .ui-toast-wrap, .ui-dialog-backdrop, .ui-skip, .ui-home, button, .btn { display: none !important; }
  body { background: #fff !important; color: #000 !important; }
  a[href^="http"]::after { content: " (" attr(href) ")"; font-size: 85%; color: #555; }
}
