:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-2: #f0f1f4;
  --text: #1d2129;
  --muted: #6b7280;
  --line: #e3e5ea;
  --accent: #3b6cf6;
  --accent-soft: #e8eeff;
  --lan: #16a34a;
  --ts: #7c3aed;
  --wan: #2563eb;
  --danger: #dc2626;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, .18);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --panel: #1a1d23;
    --panel-2: #23272f;
    --text: #e7e9ee;
    --muted: #9aa1ad;
    --line: #2c313a;
    --accent: #6d8fff;
    --accent-soft: #232c47;
    --lan: #22c55e;
    --ts: #a78bfa;
    --wan: #60a5fa;
    --danger: #f87171;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-lg: 0 12px 32px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: inherit; text-decoration: none; }

/* ---------- top bar */
.top {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px;
  padding: 10px max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(1.6) blur(12px);
  -webkit-backdrop-filter: saturate(1.6) blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 17px; letter-spacing: .5px; white-space: nowrap; }
.search {
  flex: 1; max-width: 560px; margin: 0 auto;
  display: flex; align-items: center; gap: 8px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
  padding: 0 14px; height: 38px; box-shadow: var(--shadow);
}
.search svg { flex: none; opacity: .55; }
.search input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; height: 100%; }
.search kbd {
  font: 11px ui-monospace, monospace; color: var(--muted);
  border: 1px solid var(--line); border-radius: 5px; padding: 0 5px;
}
.btn {
  height: 36px; padding: 0 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--panel); box-shadow: var(--shadow);
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.btn:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.btn.icon { width: 36px; padding: 0; justify-content: center; font-size: 18px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.mode .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--wan); }
.mode[data-m="lan"] .dot { background: var(--lan); }
.mode[data-m="ts"] .dot { background: var(--ts); }
.mode small { color: var(--muted); font-size: 11px; }

/* ---------- layout */
.layout { display: flex; gap: 20px; padding: 16px max(16px, env(safe-area-inset-left)) 80px; max-width: 1500px; margin: 0 auto; }
.side {
  flex: none; width: 168px; position: sticky; top: 74px; align-self: flex-start;
  max-height: calc(100vh - 90px); overflow: auto; padding-right: 4px;
}
.side a {
  display: flex; justify-content: space-between; gap: 6px;
  padding: 6px 10px; border-radius: 8px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.side a span:first-child { overflow: hidden; text-overflow: ellipsis; }
.side a:hover, .side a.active { background: var(--panel); color: var(--text); }
.side a small { opacity: .6; }
main { flex: 1; min-width: 0; }

.group { margin-bottom: 22px; }
.g-head { display: flex; align-items: center; gap: 8px; margin: 0 2px 10px; }
.g-head h2 { font-size: 14px; font-weight: 650; margin: 0; letter-spacing: .3px; }
.g-head .count { color: var(--muted); font-size: 12px; }
.g-head .sp { flex: 1; }
.g-head .fold { border: 0; background: none; color: var(--muted); padding: 2px 6px; border-radius: 6px; }
.g-head .fold:hover { background: var(--panel-2); }
.group.folded .grid { display: none; }
.group.folded .fold { transform: rotate(-90deg); }
.g-handle, .g-edit { display: none; }
.editing .g-handle { display: inline-flex; cursor: grab; color: var(--muted); padding: 2px; }
.editing .g-edit { display: inline-flex; }
.g-edit button { border: 0; background: none; color: var(--muted); padding: 2px 6px; border-radius: 6px; font-size: 12px; }
.g-edit button:hover { background: var(--panel-2); color: var(--text); }

.grid {
  display: grid; gap: 10px;
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
  min-height: 20px;
}
.editing .grid { min-height: 72px; border: 1px dashed transparent; border-radius: 12px; }
.editing .grid:empty { border-color: var(--line); }
.tile {
  position: relative;
  display: flex; flex-direction: column; align-items: center; gap: 7px;
  padding: 14px 6px 10px; border-radius: var(--radius);
  background: var(--panel); border: 1px solid transparent; box-shadow: var(--shadow);
  transition: transform .12s ease, border-color .12s, box-shadow .12s;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: default;
}
.tile:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.tile:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tile.first { border-color: var(--accent); }
.tile.unreachable { opacity: .45; }
.ico {
  width: 44px; height: 44px; border-radius: 11px; flex: none;
  display: grid; place-items: center; overflow: hidden;
  background: var(--panel-2);
}
.ico img { width: 100%; height: 100%; object-fit: contain; }
.ico { position: relative; }
.ico img.fav { position: absolute; inset: 0; padding: 8px; opacity: 0; background: var(--panel-2); }
.ico img.fav.ok { opacity: 1; }
.letter {
  width: 100%; height: 100%; display: grid; place-items: center;
  color: #fff; font-weight: 700; font-size: 19px;
  background: hsl(var(--h) 55% 50%);
}
.name {
  font-size: 12.5px; line-height: 1.3; text-align: center; width: 100%;
  overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  word-break: break-all;
}
.badges { position: absolute; top: 7px; right: 8px; display: flex; gap: 3px; }
.badges i { width: 6px; height: 6px; border-radius: 50%; }
.badges .b-lan { background: var(--lan); }
.badges .b-ts { background: var(--ts); }
.sel {
  display: none; position: absolute; top: 6px; left: 6px;
  width: 18px; height: 18px; border-radius: 5px; border: 1.5px solid var(--muted);
  background: var(--panel);
}
.editing .sel { display: block; }
.tile.selected { border-color: var(--accent); background: var(--accent-soft); }
.tile.selected .sel { background: var(--accent); border-color: var(--accent); }
.tile.selected .sel::after { content: ""; position: absolute; left: 5px; top: 1px; width: 5px; height: 10px; border: solid #fff; border-width: 0 2px 2px 0; transform: rotate(45deg); }
.editing .tile { cursor: grab; }
.sortable-ghost { opacity: .35; }
.add-tile { border: 1.5px dashed var(--line); background: transparent; box-shadow: none; color: var(--muted); justify-content: center; }
.add-tile .ico { background: transparent; font-size: 24px; }

.empty { text-align: center; color: var(--muted); padding: 60px 0; }
.empty .btn { margin-top: 12px; }

/* ---------- bulk bar */
.bulk {
  position: fixed; left: 50%; bottom: max(16px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 30;
  display: flex; align-items: center; gap: 8px; padding: 8px 10px 8px 16px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow-lg);
  max-width: calc(100vw - 32px); overflow-x: auto;
}
.bulk span { white-space: nowrap; }

/* ---------- menus & dialogs */
.menu {
  position: fixed; z-index: 50; min-width: 180px; padding: 6px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow-lg);
}
.menu button, .menu a, .menu label {
  display: flex; align-items: center; gap: 8px; width: 100%; text-align: left;
  border: 0; background: none; padding: 8px 10px; border-radius: 8px;
}
.menu button:hover, .menu a:hover, .menu label:hover { background: var(--panel-2); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 5px 4px; }
.menu .hint { color: var(--muted); font-size: 12px; padding: 4px 10px 6px; }
.menu .chk { margin-left: auto; color: var(--accent); }
.menu .danger { color: var(--danger); }

dialog {
  border: 1px solid var(--line); border-radius: 16px; padding: 0; width: min(520px, calc(100vw - 32px));
  background: var(--panel); color: var(--text); box-shadow: var(--shadow-lg);
}
dialog::backdrop { background: rgba(10, 12, 16, .45); }
.dlg { padding: 18px 20px 16px; }
.dlg h3 { margin: 0 0 14px; font-size: 16px; }
.field { display: grid; gap: 5px; margin-bottom: 12px; }
.field > span { font-size: 12.5px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.field > span i { width: 7px; height: 7px; border-radius: 50%; display: inline-block; }
.field input, .field select, .field textarea {
  width: 100%; height: 36px; padding: 0 10px; border-radius: 9px;
  border: 1px solid var(--line); background: var(--bg); outline: 0;
}
.field textarea { height: 60px; padding: 8px 10px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); }
.row { display: flex; gap: 8px; align-items: center; }
.row > input { flex: 1; min-width: 0; }
.row .btn { height: 36px; box-shadow: none; }
.icon-row { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.icon-row .ico { width: 52px; height: 52px; }
.icon-row .btn { height: 32px; font-size: 13px; box-shadow: none; }
.actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 6px; }
.actions .left { margin-right: auto; }
.err { color: var(--danger); font-size: 13px; min-height: 18px; }

.toast {
  position: fixed; left: 50%; top: 70px; transform: translateX(-50%); z-index: 60;
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 10px;
  box-shadow: var(--shadow-lg); font-size: 13px; opacity: 0; pointer-events: none; transition: opacity .2s;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; }

/* ---------- login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { width: min(340px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 28px 24px; box-shadow: var(--shadow-lg); }
.login h1 { margin: 0 0 4px; font-size: 20px; }
.login p { margin: 0 0 18px; color: var(--muted); font-size: 13px; }
.login input { width: 100%; height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); padding: 0 12px; outline: 0; margin-bottom: 10px; }
.login input:focus { border-color: var(--accent); }
.login .btn { width: 100%; justify-content: center; height: 40px; }

/* ---------- mobile */
@media (max-width: 860px) {
  .layout { display: block; padding-top: 0; }
  .side {
    position: sticky; top: 58px; z-index: 10; width: auto; max-height: none;
    display: flex; gap: 6px; overflow-x: auto; padding: 10px 0; margin: 0 -16px 6px; padding-left: 16px; padding-right: 16px;
    background: var(--bg); scrollbar-width: none;
  }
  .side::-webkit-scrollbar { display: none; }
  .side a { flex: none; background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px; }
  .side a small { display: none; }
}
@media (max-width: 600px) {
  .top { gap: 6px; flex-wrap: wrap; }
  .brand { display: none; }
  .search { order: 5; flex-basis: 100%; max-width: none; }
  .search kbd { display: none; }
  .top .sp { flex: 1; }
  .side { top: 104px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(76px, 1fr)); gap: 8px; }
  .tile { padding: 11px 4px 8px; }
  .ico { width: 40px; height: 40px; }
  .name { font-size: 12px; }
  .mode small { display: none; }
}
@media (min-width: 601px) { .top .sp { display: none; } }
.group { scroll-margin-top: 76px; }
@media (max-width: 860px) { .group { scroll-margin-top: 112px; } }
@media (max-width: 600px) { .group { scroll-margin-top: 158px; } }
