/* ============================================================================
 * app.css — Sticker-Hub 页面特有样式
 * 依赖 md2.css 的主题变量。DOM 契约见 SPEC.md §15。
 * ==========================================================================*/

/* ---------- AppBar ---------- */
.appbar {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--appbar-gap, 8px);
  height: var(--md2-appbar-h);
  padding: 0 var(--appbar-pad-x, 16px);
  /* 超宽屏 padding 与内容区对齐（由 md2.css 大屏断点覆盖） */
  background: var(--md2-primary);
  color: var(--md2-on-primary);
  box-shadow: var(--md2-e2);
}

.appbar-title {
  margin: 0;
  font-size: 20px;
  font-weight: 500;
  letter-spacing: .2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  flex: 0 1 auto;
}

.appbar-actions {
  display: flex;
  align-items: center;
  gap: var(--appbar-gap, 4px);
  flex-shrink: 1;    /* 允许收缩，避免窄屏下右侧区域把页面撑出横向滚动 */
  min-width: 0;
}

.search-input {
  width: var(--search-w, 200px);
  max-width: var(--search-w, 200px);
  min-width: 0;
  flex: 0 1 auto;    /* 桌面保持固定宽，窄屏空间不足时自动收缩 */
  height: 36px;
  padding: 0 14px;
  border: none;
  border-radius: 18px;
  background: rgba(255, 255, 255, .2);
  color: var(--md2-on-primary);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: background .2s ease, box-shadow .2s ease, color .2s ease;
}
.search-input::placeholder { color: rgba(255, 255, 255, .7); }
.search-input:focus {
  background: #fff;
  color: var(--md2-on-surface);
  box-shadow: var(--md2-e1);
}

.icon-btn {
  width: var(--icon-btn-s, 40px);
  height: var(--icon-btn-s, 40px);
  border: none;
  border-radius: 50%;
  background: transparent;
  color: inherit;
  font-size: 20px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background .2s ease, transform .1s ease;
}
.icon-btn:hover { background: rgba(255, 255, 255, .18); }
.icon-btn:active { transform: scale(.92); }

/* ---------- Tabs（分类行，MD2 scrollable tabs） ---------- */
.tabs {
  position: sticky;
  top: var(--md2-appbar-h);
  z-index: 90;
  display: flex;
  align-items: stretch;
  min-height: var(--md2-tabs-h);
  background: var(--md2-surface);
  box-shadow: var(--md2-e1);
  overflow-x: auto;
  overscroll-behavior-x: contain;
  border-bottom: 1px solid var(--md2-outline);
  /* 隐藏滚动条，靠拖拽/滚轮横向滚动，更接近 MD2 tabs */
  scrollbar-width: none;
  /* 超宽屏：与内容区对齐 */
  padding-left: var(--appbar-pad-x, 0);
  padding-right: var(--appbar-pad-x, 0);
}
.tabs::-webkit-scrollbar { display: none; }

.tab {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0 16px;
  min-width: 56px;
  flex-shrink: 0;   /* 折叠测量依赖稳定宽度，禁止 flex 收缩 */
  border: none;
  border-radius: 0;
  background: transparent;
  color: var(--md2-on-surface-muted);
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  transition: color .2s ease, background .2s ease;
}
.tab:hover { background: var(--md2-surface-variant); color: var(--md2-on-surface); }
.tab.active { color: var(--md2-primary); }

/* MD2 选中指示条：底部 2px 主色下划线 */
.tab::after {
  content: "";
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 0;
  height: 2px;
  border-radius: 2px 2px 0 0;
  background: var(--md2-primary);
  transform: scaleX(0);
  transition: transform .2s ease;
}
.tab.active::after { transform: scaleX(1); }

.tab-icon {
  width: 20px;
  height: 20px;
  object-fit: contain;
  border-radius: 4px;
}

.tab-badge {
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--md2-surface-variant);
  color: var(--md2-on-surface-muted);
  font-size: 11px;
  line-height: 18px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.tab.active .tab-badge {
  background: var(--md2-primary);
  color: var(--md2-on-primary);
}

/* ---------- Tab 可折叠「更多 ▾」（R1-CONTRACT §5） ---------- */
/* 「更多 ▾」按钮：始终保留，贴在 Tab 行右端 */
.tab-more {
  position: sticky;
  right: 0;
  margin-left: auto;
  padding: 0 14px;
  color: var(--md2-primary);
  background: var(--md2-surface);
  box-shadow: -6px 0 8px -6px rgba(0, 0, 0, .18);
}
.tab-more::after { display: none; }               /* 不需要选中指示条 */
.tab-more:hover { background: var(--md2-surface-variant); }
.tab-more.disabled { opacity: .45; cursor: default; color: var(--md2-on-surface-muted); }
.tab-more.disabled:hover { background: var(--md2-surface); }

/* 折叠下拉菜单 */
.tab-more-menu {
  position: fixed;
  z-index: 320;
  min-width: 168px;
  max-width: 260px;
  max-height: 60vh;
  overflow-y: auto;
  padding: 6px 0;
  background: var(--md2-surface);
  color: var(--md2-on-surface);
  border-radius: var(--md2-radius);
  box-shadow: var(--md2-e4);
  opacity: 0;
  transform: translateY(-6px) scale(.98);
  pointer-events: none;
  transition: opacity .16s ease, transform .16s ease;
}
.tab-more-menu.open { opacity: 1; transform: none; pointer-events: auto; }

.tab-more-item {
  display: block;
  width: 100%;
  padding: 10px 16px;
  border: none;
  background: transparent;
  color: var(--md2-on-surface);
  font-size: 14px;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background .15s ease;
}
.tab-more-item:hover { background: var(--md2-surface-variant); }
.tab-more-item.active { color: var(--md2-primary); font-weight: 500; }

/* ---------- 内容网格（对齐式网格：行高固定、格宽 = 行高 × 宽高比） ---------- */
.grid {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: var(--grid-gap);
  padding: var(--grid-pad);
  min-height: 200px;
  align-content: flex-start;
  /* 超宽屏限宽居中（2K/4K/8K 下不铺满全屏） */
  width: 100%;
  max-width: var(--content-max-w, none);
  margin-left: auto;
  margin-right: auto;
}

.grid-cell {
  position: relative;
  height: var(--grid-row-h);
  width: var(--grid-row-h);   /* 加载前方形占位；加载后由 JS 按图片宽高比设精确宽度 */
  max-width: 100%;
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--md2-radius);
  overflow: hidden;
  background: var(--md2-surface);
  box-shadow: var(--md2-e1);
  transition: box-shadow .2s ease, transform .15s ease;
}

/* 加载中占位：让“空白格子”看起来明显是“加载中”，而不是一片空白却能点。
   图片 load 后 JS 会移除 .loading 并设精确宽度。 */
.grid-cell.loading {
  background:
    linear-gradient(100deg,
      var(--md2-surface) 40%,
      var(--md2-surface-variant) 50%,
      var(--md2-surface) 60%);
  background-size: 200% 100%;
  animation: grid-skeleton 1.4s ease-in-out infinite;
}
@keyframes grid-skeleton {
  from { background-position: 120% 0; }
  to   { background-position: -80% 0; }
}

.grid-cell img {
  height: 100%;
  width: auto;
  max-width: none;
  object-fit: contain;
  display: block;
}

/* 图片加载失败兜底（内联 SVG 灰块 + 名称，SPEC §9.2） */
.grid-cell.img-error {
  width: var(--grid-row-h);
  background: var(--md2-surface-variant);
  color: var(--md2-on-surface-muted);
  font-size: 10px;
  text-align: center;
  padding: 2px;
  overflow: hidden;
}

.grid-cell:hover {
  box-shadow: var(--md2-e2);
  transform: translateY(-1px);
  z-index: 2;
}

/* 卡片右上角操作按钮（收藏等），hover 显示 */
.grid-cell .cell-btn {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 22px;
  height: 22px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, .35);
  color: #fff;
  font-size: 12px;
  line-height: 1;
  opacity: 0;
  transition: opacity .2s ease, background .2s ease;
  z-index: 3;
}
.grid-cell:hover .cell-btn { opacity: 1; }
.grid-cell .cell-btn:hover { background: rgba(0, 0, 0, .6); }
.grid-cell .cell-btn.faved { background: var(--md2-accent); opacity: 1; }

.grid-empty {
  width: 100%;
  padding: 48px 16px;
  text-align: center;
  color: var(--md2-on-surface-muted);
}
.grid-empty .grid-empty-icon { font-size: 40px; display: block; margin-bottom: 8px; }

/* ---------- Footer（版权 / 免责声明） ---------- */
.site-footer {
  margin-top: 24px;
  padding: 20px 16px 28px;
  border-top: 1px solid var(--md2-outline);
  background: var(--md2-surface);
  color: var(--md2-on-surface-muted);
  font-size: 12px;
  line-height: 1.7;
  text-align: center;
}
.site-footer .footer-inner {
  max-width: var(--content-max-w, none);
  margin-left: auto;
  margin-right: auto;
  padding: 0 8px;
}
.site-footer p { margin: 4px 0; }
.site-footer a {
  color: var(--md2-primary);
  text-decoration: none;
}
.site-footer a:hover { text-decoration: underline; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 300;
  transform: translateX(-50%) translateY(24px);
  padding: 12px 20px;
  max-width: 80vw;
  background: var(--md2-on-surface);
  color: var(--md2-bg);
  border-radius: 24px;
  box-shadow: var(--md2-e4);
  font-size: 14px;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease, transform .25s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Modal ---------- */
.modal-root {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-root.hidden { display: none; }

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, .5);
}

.modal {
  position: relative;
  width: 100%;
  max-width: 480px;
  max-height: 84vh;
  overflow-y: auto;
  background: var(--md2-surface);
  color: var(--md2-on-surface);
  border-radius: var(--md2-radius-lg);
  box-shadow: var(--md2-e4);
  padding: 24px;
  animation: modal-in .2s ease;
}
@keyframes modal-in {
  from { opacity: 0; transform: translateY(12px) scale(.98); }
  to   { opacity: 1; transform: none; }
}

.modal h2 {
  margin: 0 0 16px;
  font-size: 20px;
  font-weight: 500;
}
.modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
}

/* ---------- 按钮（MD2） ---------- */
.md2-btn {
  height: 36px;
  padding: 0 16px;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 500;
  letter-spacing: .4px;
  text-transform: uppercase;
  background: transparent;
  color: var(--md2-primary);
  transition: background .2s ease, box-shadow .2s ease;
}
.md2-btn:hover { background: var(--md2-primary); color: var(--md2-on-primary); box-shadow: var(--md2-e2); }
.md2-btn.contained { background: var(--md2-primary); color: var(--md2-on-primary); }
.md2-btn.contained:hover { box-shadow: var(--md2-e4); }
.md2-btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- FAB ---------- */
.fab {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 150;
  width: 56px;
  height: 56px;
  border: none;
  border-radius: 50%;
  background: var(--md2-primary);
  color: var(--md2-on-primary);
  box-shadow: var(--md2-e4);
  font-size: 24px;
  line-height: 1;
  transition: transform .2s ease, box-shadow .2s ease;
}
.fab:hover { transform: translateY(-2px); box-shadow: var(--md2-e4); }
.fab:active { transform: translateY(0) scale(.94); }

/* ---------- 表单（导入 / 设置面板） ---------- */
.field { margin-bottom: 16px; }
.field label {
  display: block;
  font-size: 13px;
  color: var(--md2-on-surface-muted);
  margin-bottom: 6px;
}
.field input,
.field select,
.field textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--md2-outline);
  border-radius: 4px;
  background: var(--md2-surface);
  color: var(--md2-on-surface);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color .2s ease;
}
.field input:focus,
.field select:focus,
.field textarea:focus { border-color: var(--md2-primary); }
.field textarea {
  min-height: 120px;
  font-family: var(--md2-mono);
  font-size: 13px;
  resize: vertical;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--md2-outline);
}
.setting-row:last-child { border-bottom: none; }
.setting-row .setting-label { font-size: 14px; }
.setting-row .setting-desc { font-size: 12px; color: var(--md2-on-surface-muted); }

/* ---------- CDN 切换 pill ---------- */
.cdn-bar {
  width: 100%;
  max-width: var(--content-max-w, none);
  margin-left: auto;
  margin-right: auto;
  padding: 8px var(--grid-pad) 0;
}
.pills { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: 12px;
  background: var(--md2-surface-variant);
  color: var(--md2-on-surface);
  font-size: 12px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background .2s ease, color .2s ease;
}
.pill:hover { border-color: var(--md2-primary); }
.pill.active { background: var(--md2-primary); color: var(--md2-on-primary); }
.pill.disabled { opacity: .4; cursor: not-allowed; }

/* ---------- 预设 / 源 列表 ---------- */
.source-list { margin: 8px 0; }
.source-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 4px;
  border-bottom: 1px solid var(--md2-outline);
}
.source-item:last-child { border-bottom: none; }
.source-item img { width: 28px; height: 28px; object-fit: contain; }
.source-item .source-name { flex: 1; font-size: 14px; }
.source-item .source-meta { font-size: 12px; color: var(--md2-on-surface-muted); }

/* ---------- 设置后台：section 分组 ---------- */
.setting-section {
  padding: 4px 0 12px;
  margin-bottom: 12px;
  border-bottom: 1px solid var(--md2-outline);
}
.setting-section:last-child { border-bottom: none; margin-bottom: 0; }
.setting-section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 500;
  letter-spacing: .3px;
  text-transform: uppercase;
  color: var(--md2-primary);
  margin: 8px 0 6px;
}

/* ---------- 设置后台：分类管理列表 ---------- */
.manage-list { margin: 8px 0 4px; }
.manage-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 4px;
  border-bottom: 1px solid var(--md2-outline);
  border-radius: 4px;
}
.manage-item:last-child { border-bottom: none; }
.manage-item img {
  width: 32px;
  height: 32px;
  object-fit: contain;
  border-radius: 6px;
  flex: 0 0 auto;
  background: var(--md2-surface-variant);
}
.manage-info {
  flex: 1;
  min-width: 0;
}
.manage-name {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.manage-meta {
  font-size: 12px;
  color: var(--md2-on-surface-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.manage-actions {
  display: flex;
  gap: 4px;
  flex: 0 0 auto;
}
.manage-actions .md2-btn {
  height: 30px;
  padding: 0 10px;
  font-size: 12px;
  border-radius: 16px;
}
.manage-actions .md2-btn.danger { color: var(--md2-error, #b3261e); }
.manage-actions .md2-btn.danger:hover {
  background: var(--md2-error, #b3261e);
  color: #fff;
  box-shadow: var(--md2-e2);
}

/* ---------- 设置后台：排序按钮（上移/下移） ---------- */
.manage-sort {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 0 0 auto;
}
.manage-sort .sort-btn {
  width: 24px;
  height: 22px;
  padding: 0;
  line-height: 1;
  font-size: 12px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.manage-sort .sort-btn:disabled { opacity: .3; cursor: default; }

/* ---------- 设置后台：数据源配置（CDN base 可增删） ---------- */
.cdn-list { margin: 4px 0; }
.cdn-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 4px;
  border-bottom: 1px solid var(--md2-outline);
  flex-wrap: wrap;
}
.cdn-row:last-child { border-bottom: none; }
.cdn-key-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--md2-primary);
  background: var(--md2-surface-variant);
  border-radius: 10px;
  padding: 2px 8px;
  flex: 0 0 auto;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.cdn-key-label.cdn-key-active {
  background: var(--md2-primary);
  color: var(--md2-on-primary);
}
.cdn-url-label {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  color: var(--md2-on-surface-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  word-break: break-all;
}
.cdn-active-mark {
  font-size: 11px;
  color: var(--md2-primary);
  flex: 0 0 auto;
}
.cdn-row .md2-btn {
  height: 28px;
  padding: 0 10px;
  font-size: 12px;
  border-radius: 14px;
}
.cdn-row-new {
  background: var(--md2-surface-variant, rgba(0,0,0,.04));
  border-radius: 8px;
  padding: 8px;
  border: none;
}
.cdn-row-new .cdn-key {
  width: 130px;
}
.cdn-row-new .cdn-url {
  flex: 1;
  min-width: 200px;
}
.cdn-row-new input {
  padding: 6px 8px;
  border: 1px solid var(--md2-outline);
  border-radius: 6px;
  background: var(--md2-surface);
  color: var(--md2-on-surface);
  font-size: 13px;
}

/* ---------- 设置后台：启用/禁用开关（MD2 switch） ---------- */
.manage-switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  flex: 0 0 auto;
}
.manage-switch input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 2;   /* 盖在 .manage-slider 之上，确保点击命中 checkbox */
}
.manage-slider {
  position: absolute;
  inset: 0;
  border-radius: 12px;
  background: var(--md2-surface-variant);
  transition: background .2s ease;
}
.manage-slider::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--md2-on-surface);
  box-shadow: var(--md2-e1);
  transition: transform .2s ease, background .2s ease;
}
.manage-switch input:checked + .manage-slider {
  background: var(--md2-primary);
}
.manage-switch input:checked + .manage-slider::before {
  transform: translateX(20px);
  background: var(--md2-on-primary);
}
.manage-switch input:focus-visible + .manage-slider {
  outline: 2px solid var(--md2-primary);
  outline-offset: 2px;
}
.manage-switch input:disabled + .manage-slider { opacity: .5; cursor: not-allowed; }
