/* =========================================================
 * 手机端启动页：4 列网格 + 文件夹 + 可编辑 + 自动轮换壁纸
 * ======================================================= */
:root {
  --cols: 4;
  --icon: clamp(46px, 14.2vw, 58px);
  --gap-x: clamp(6px, 2.6vw, 16px);
  --row-gap: clamp(14px, 3.6vw, 20px);
  --label: #eef2f7;
  --label-dim: rgba(238, 242, 247, 0.72);
  --tile-bg: rgba(255, 255, 255, 0.16);
  --sheet-bg: rgba(30, 34, 42, 0.93);
  --sheet-line: rgba(255, 255, 255, 0.12);
  --input-bg: rgba(255, 255, 255, 0.1);
  --bar-bg: rgba(16, 20, 28, 0.42);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", "Noto Sans SC", system-ui, sans-serif;
  color: var(--label);
  background: #0b1220;
  background-image:
    radial-gradient(120% 80% at 50% -10%, #2b4a63 0%, rgba(43, 74, 99, 0) 60%),
    radial-gradient(90% 60% at 100% 100%, #1d3550 0%, rgba(29, 53, 80, 0) 65%),
    linear-gradient(168deg, #16202f 0%, #0b1220 58%, #070b12 100%);
  background-attachment: fixed;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior-y: none;
}

/* ---------- 壁纸层 ---------- */
.wallpaper {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  overflow: hidden;
}
.wallpaper .layer {
  position: absolute;
  inset: 0;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  opacity: 0;
  transition: opacity 900ms ease;
  will-change: opacity;
}
.wallpaper .layer.on { opacity: 1; }

/* 壁纸上的压暗层，保证图标名字看得清 */
.scrim {
  position: fixed;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  opacity: 0;
  transition: opacity 600ms ease;
  background:
    linear-gradient(180deg, rgba(6, 10, 18, 0.46) 0%, rgba(6, 10, 18, 0.16) 30%, rgba(6, 10, 18, 0.30) 62%, rgba(6, 10, 18, 0.62) 100%);
}
body.has-wall .scrim { opacity: 1; }
body.has-wall .tile .name {
  color: #fff;
  padding: 1px 6px;
  border-radius: 8px;
  background: rgba(4, 8, 14, 0.34);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.55);
}
body.has-wall .home-title { color: rgba(255, 255, 255, 0.8); }

/* ---------- 顶栏 ---------- */
.topbar {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 10px);
  right: calc(env(safe-area-inset-right, 0px) + 12px);
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 8px;
}
.iconbtn {
  -webkit-appearance: none;
  appearance: none;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 50%;
  background: var(--bar-bg);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  color: #fff;
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.iconbtn:active { transform: scale(0.92); }
.iconbtn.text { width: auto; padding: 0 12px; border-radius: 18px; font-size: 13px; }
.iconbtn[hidden] { display: none; }

.editbar {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 10px);
  left: calc(env(safe-area-inset-left, 0px) + 12px);
  z-index: 40;
  padding: 8px 12px;
  border-radius: 16px;
  background: rgba(47, 111, 237, 0.82);
  color: #fff;
  font-size: 12px;
  letter-spacing: 0.02em;
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}
.editbar[hidden] { display: none; }

/* ---------- 页面骨架 ---------- */
.home {
  position: relative;
  z-index: 2;
  max-width: 560px;
  margin: 0 auto;
  padding:
    calc(env(safe-area-inset-top, 0px) + 58px)
    calc(env(safe-area-inset-right, 0px) + 16px)
    calc(env(safe-area-inset-bottom, 0px) + 96px)
    calc(env(safe-area-inset-left, 0px) + 16px);
}

.home-title {
  margin: 0 0 18px;
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 0.04em;
  color: var(--label-dim);
  text-align: center;
}
.home-title:empty { display: none; }

/* ---------- 图标网格 ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  column-gap: var(--gap-x);
  row-gap: var(--row-gap);
  align-items: start;
  min-width: 0;
}

.tile {
  -webkit-appearance: none;
  appearance: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 7px;
  width: 100%;
  min-width: 0;
  color: inherit;
  font: inherit;
  text-decoration: none;
  cursor: pointer;
  position: relative;
  transition: transform 120ms ease;
}
.tile:active { transform: scale(0.92); }
.tile:focus-visible { outline: 2px solid #7cc4ff; outline-offset: 4px; border-radius: 18px; }

.tile .icon {
  position: relative;
  display: grid;
  place-items: center;
  width: var(--icon);
  height: var(--icon);
  border-radius: 26%;
  background: var(--tile-bg);
  box-shadow:
    0 6px 16px rgba(0, 0, 0, 0.28),
    inset 0 1px 0 rgba(255, 255, 255, 0.22);
  overflow: hidden;
  font-size: calc(var(--icon) * 0.46);
  line-height: 1;
  user-select: none;
}
.tile .icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.tile .icon .glyph {
  font-weight: 600;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
}

.tile .name {
  max-width: 100%;
  font-size: 11px;
  line-height: 1.25;
  color: var(--label);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
}

/* ---------- 文件夹磁贴 ---------- */
.tile.folder .icon {
  padding: 6px;
  background: var(--folder-bg, rgba(127, 140, 160, 0.28));
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}
.tile.folder .mini {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 3px;
  width: 100%;
  height: 100%;
}
.tile.folder .mini .cell {
  display: grid;
  place-items: center;
  border-radius: 26%;
  overflow: hidden;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25);
  font-size: calc(var(--icon) * 0.17);
  color: #fff;
}
.tile.folder .mini .cell img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.tile.folder .badge {
  position: absolute;
  right: 3px;
  bottom: 3px;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 8px;
  background: rgba(10, 14, 20, 0.72);
  color: #fff;
  font-size: 10px;
  line-height: 16px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* ---------- 编辑模式 ---------- */
.editing .tile { animation: wiggle 520ms ease-in-out infinite alternate; }
.editing .tile:nth-child(2n) { animation-delay: -260ms; }
@keyframes wiggle {
  from { transform: rotate(-1.1deg); }
  to   { transform: rotate(1.1deg); }
}
@media (prefers-reduced-motion: reduce) {
  .editing .tile { animation: none; }
  .wallpaper .layer { transition: none; }
}
.tile .del {
  position: absolute;
  top: calc(var(--icon) * -0.28);
  left: calc(50% - var(--icon) / 2);
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 0;
  background: rgba(38, 42, 50, 0.92);
  color: #fff;
  font-size: 12px;
  line-height: 20px;
  padding: 0;
  display: none;
  z-index: 3;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
}
.editing .tile .del { display: block; }

/* ---------- 弹层通用 ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(env(safe-area-inset-top, 0px) + 16px) 16px
    calc(env(safe-area-inset-bottom, 0px) + 16px);
  background: rgba(6, 9, 14, 0.52);
  -webkit-backdrop-filter: blur(16px) saturate(120%);
  backdrop-filter: blur(16px) saturate(120%);
  animation: fade 160ms ease-out;
}
.overlay[hidden] { display: none; }

.sheet {
  width: min(88vw, 430px);
  max-height: calc(100vh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - 32px);
  max-height: calc(100dvh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - 32px);
  display: flex;
  flex-direction: column;
  padding: 18px 16px 18px;
  border-radius: 28px;
  border: 1px solid var(--sheet-line);
  background: var(--sheet-bg);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
  animation: pop 200ms cubic-bezier(0.2, 0.9, 0.3, 1.15);
}
.sheet.wide { width: min(94vw, 480px); }

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
  flex: none;
}
.sheet-title {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.02em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sheet-close {
  flex: none;
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.14);
  color: var(--label);
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
}
.sheet-close:active { background: rgba(255, 255, 255, 0.26); }

.sheet-grid {
  flex: 1 1 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 22px 0 6px;
  min-height: 0;
}

.sheet-body {
  flex: 1 1 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
  padding-right: 2px;
}

/* ---------- 表单 ---------- */
.field { margin-bottom: 12px; }
.field > label {
  display: block;
  margin-bottom: 6px;
  font-size: 12px;
  color: var(--label-dim);
}
.row { display: flex; gap: 8px; align-items: center; }
.row > .grow { flex: 1 1 auto; min-width: 0; }

input[type="text"], input[type="url"], select, textarea {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--sheet-line);
  border-radius: 12px;
  background: var(--input-bg);
  color: var(--label);
  font-size: 14px;
  font-family: inherit;
}
textarea { min-height: 70px; resize: vertical; line-height: 1.5; }
input::placeholder, textarea::placeholder { color: rgba(238, 242, 247, 0.38); }
select { padding-right: 30px; background-image: none; }

.btn {
  -webkit-appearance: none;
  appearance: none;
  padding: 10px 14px;
  border: 1px solid var(--sheet-line);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.08);
  color: var(--label);
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
}
.btn:active { background: rgba(255, 255, 255, 0.16); }
.btn.primary { background: #2f6fed; border-color: #2f6fed; color: #fff; }
.btn.danger { background: rgba(239, 68, 68, 0.16); border-color: rgba(239, 68, 68, 0.4); color: #ff8a8a; }
.btn.block { width: 100%; }
.btn[hidden] { display: none; }

.actions { display: flex; gap: 8px; margin-top: 14px; flex: none; }
.actions .btn { flex: 1 1 0; }

/* 图标预览 + 选择器 */
.preview-row { display: flex; align-items: center; gap: 12px; }
.preview {
  width: 52px;
  height: 52px;
  flex: none;
  border-radius: 26%;
  display: grid;
  place-items: center;
  font-size: 24px;
  color: #fff;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.25);
  overflow: hidden;
}
.preview img { width: 100%; height: 100%; object-fit: cover; }

.emoji-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
  max-height: 132px;
  overflow-y: auto;
  padding: 6px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.05);
  overscroll-behavior: contain;
}
.emoji-grid button {
  -webkit-appearance: none;
  appearance: none;
  border: 0;
  background: none;
  font-size: 19px;
  line-height: 1;
  padding: 5px 0;
  border-radius: 8px;
  cursor: pointer;
}
.emoji-grid button:active { background: rgba(255, 255, 255, 0.16); }

.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatches button {
  -webkit-appearance: none;
  appearance: none;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
}
.swatches button.sel { border-color: #fff; box-shadow: 0 0 0 2px rgba(47, 111, 237, 0.9); }

.seg {
  display: flex;
  gap: 4px;
  padding: 3px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.07);
}
.seg button {
  -webkit-appearance: none;
  appearance: none;
  flex: 1 1 0;
  padding: 8px 4px;
  border: 0;
  border-radius: 9px;
  background: none;
  color: var(--label-dim);
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
}
.seg button.sel { background: rgba(255, 255, 255, 0.16); color: #fff; }

.hint { font-size: 11px; line-height: 1.5; color: rgba(238, 242, 247, 0.5); margin: 6px 0 0; }

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop {
  from { opacity: 0; transform: scale(0.86); }
  to   { opacity: 1; transform: scale(1); }
}

@media (prefers-reduced-motion: reduce) {
  .overlay, .sheet { animation: none; }
  .tile, .wallpaper .layer { transition: none; }
  .home.editing .tile { animation: none; }
}

/* 超窄屏降成 3 列 */
@media (max-width: 340px) {
  :root { --cols: 3; }
}
/* ---------- 补充：添加磁贴 / 文件夹头 / 预览行 ---------- */
.tile.add .icon {
  background: rgba(255, 255, 255, 0.06) !important;
  border: 2px dashed rgba(160, 174, 192, 0.55);
  box-shadow: none !important;
}
.tile.add .icon .glyph { font-size: calc(var(--icon) * 0.4); color: rgba(238, 242, 247, 0.75); }
}

.sheet-head .row { gap: 8px; }
.preview-row > .grow { min-width: 0; }
.preview-row input[type="url"] { font-size: 13px; }

#edPreview:empty::after { content: "图"; opacity: 0.35; }

.sheet-grid .grid { row-gap: clamp(12px, 3vw, 18px); }

/* ---------- 搜索栏 ---------- */
.searchwrap {
  width: 100%;
  max-width: 520px;
  margin: 0 auto clamp(14px, 4vw, 22px);
}
.searchbar {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 46px;
  padding: 0 6px 0 14px;
  border-radius: 23px;
  background: rgba(14, 18, 26, 0.60);
  border: 1px solid rgba(255, 255, 255, 0.16);
  -webkit-backdrop-filter: blur(14px) saturate(1.2);
  backdrop-filter: blur(14px) saturate(1.2);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.26);
  transition: border-color 160ms ease, box-shadow 160ms ease;
}
.searchbar:focus-within {
  border-color: rgba(120, 170, 255, 0.62);
  box-shadow: 0 6px 22px rgba(0, 0, 0, 0.34), 0 0 0 3px rgba(60, 120, 255, 0.16);
}
.s-ico { flex: none; font-size: 15px; opacity: 0.72; }
#searchInput {
  flex: 1 1 auto;
  min-width: 0;
  height: 100%;
  border: 0;
  background: transparent;
  color: #fff;
  font-size: 16px;
  font-family: inherit;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}
#searchInput::placeholder { color: rgba(255, 255, 255, 0.52); }
#searchInput::-webkit-search-cancel-button { -webkit-appearance: none; }
.s-engine {
  flex: none;
  height: 34px;
  max-width: 104px;
  padding: 0 12px;
  border: 0;
  border-radius: 17px;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  font-size: 12.5px;
  font-family: inherit;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}
.s-engine:active { background: rgba(255, 255, 255, 0.28); }
.home.editing .searchwrap { display: none; }
