/* ================= 主题变量 ================= */
:root {
    color-scheme: light;
    --bg1: #eef1f6;
    --bg2: #e6eaf2;
    --card: rgba(255, 255, 255, .72);
    --card-solid: #fff;
    --text: #23272f;
    --muted: #8a919c;
    --line: rgba(20, 24, 34, .08);
    --accent: #4f7cff;
    --accent-soft: rgba(79, 124, 255, .18);
    --glow1: rgba(79, 124, 255, .14);
    --glow2: rgba(56, 189, 248, .12);
    --hover: rgba(20, 24, 34, .05);
    --shadow: 0 8px 24px rgba(31, 35, 41, .10);
    --radius: 14px;
}

html[data-theme="dark"] {
    color-scheme: dark;
    --bg1: #16181d;
    --bg2: #1b1e24;
    --card: rgba(36, 40, 48, .72);
    --card-solid: #262a32;
    --text: #e8eaee;
    --muted: #9aa1ab;
    --line: rgba(255, 255, 255, .08);
    --accent: #6c8cff;
    --accent-soft: rgba(108, 140, 255, .22);
    --glow1: rgba(108, 140, 255, .10);
    --glow2: rgba(45, 212, 191, .07);
    --hover: rgba(255, 255, 255, .07);
    --shadow: 0 8px 24px rgba(0, 0, 0, .4);
}

* {
    box-sizing: border-box;
    margin: 0;
}

html[data-theme="dark"] .i-moon {
    display: none;
}

html:not([data-theme="dark"]) .i-sun {
    display: none;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI",
        "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
    color: var(--text);
    min-height: 100vh;
    overflow-x: hidden;
    background:
        radial-gradient(720px 480px at 88% -12%, var(--glow1), transparent 70%),
        radial-gradient(640px 420px at -8% 108%, var(--glow2), transparent 70%),
        linear-gradient(160deg, var(--bg1), var(--bg2)) fixed;
}

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* ================= 顶栏 ================= */
.top-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 18px 28px;
}

.clock {
    font-variant-numeric: tabular-nums;
    font-size: 14px;
    color: var(--muted);
    letter-spacing: .5px;
}

.top-actions {
    display: flex;
    gap: 8px;
}

.icon-btn {
    width: 34px;
    height: 34px;
    border: none;
    border-radius: 10px;
    background: var(--card);
    color: var(--muted);
    cursor: pointer;
    display: grid;
    place-items: center;
    backdrop-filter: blur(10px);
    transition: color .15s, transform .15s, background .15s;
}

.icon-btn:hover {
    color: var(--accent);
    transform: translateY(-1px);
}

.icon-btn.active {
    color: #fff;
    background: var(--accent);
}

.icon-btn svg {
    width: 17px;
    height: 17px;
}

/* ================= 主体 ================= */
.container {
    max-width: 1080px;
    margin: 0 auto;
    padding: 12px 24px 48px;
}

.greeting {
    text-align: center;
    font-size: 30px;
    font-weight: 600;
    letter-spacing: 1px;
    margin-top: 14px;
}

.date {
    text-align: center;
    color: var(--muted);
    font-size: 13px;
    margin: 6px 0 22px;
}

/* 搜索框（z-index 保证下拉菜单浮于分类/卡片之上） */
.search-box {
    position: relative;
    z-index: 20;
    display: flex;
    align-items: center;
    gap: 8px;
    max-width: 640px;
    margin: 0 auto 26px;
    background: var(--card);
    backdrop-filter: blur(14px);
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 6px 8px 6px 6px;
    box-shadow: var(--shadow);
    transition: box-shadow .2s, border-color .2s;
}

.search-box:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 4px var(--accent-soft), var(--shadow);
}

.engine-chip {
    flex: none;
    padding: 8px 14px;
    border: none;
    border-radius: 999px;
    background: transparent;
    color: var(--text);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    user-select: none;
    transition: color .15s;
}

.engine-chip:hover {
    color: var(--accent);
}

.engine-chip::after {
    content: '▾';
    font-size: 10px;
    margin-left: 4px;
    color: var(--muted);
}

.engine-menu {
    position: absolute;
    top: calc(100% + 8px);
    left: 0;
    z-index: 5;
    min-width: 132px;
    padding: 6px;
    display: none;
    isolation: isolate;
    background: var(--card-solid);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: var(--shadow);
}

.engine-menu.open {
    display: block;
    animation: pop .15s ease;
}

.engine-menu button {
    display: block;
    width: 100%;
    text-align: left;
    padding: 8px 12px;
    border: none;
    background: none;
    border-radius: 8px;
    cursor: pointer;
    color: var(--text);
    font-size: 13px;
    transition: background .12s;
}

.engine-menu button:hover {
    background: var(--hover);
}

.engine-menu button.on {
    color: var(--accent);
    font-weight: 600;
}

#searchInput {
    flex: 1;
    min-width: 0;
    border: none;
    outline: none;
    background: transparent;
    color: var(--text);
    font-size: 15px;
    padding: 8px 4px;
}

#searchInput::placeholder {
    color: var(--muted);
}

.search-go {
    flex: none;
    width: 38px;
    height: 38px;
    border: none;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    cursor: pointer;
    display: grid;
    place-items: center;
    transition: transform .15s, filter .15s;
}

.search-go svg {
    width: 18px;
    height: 18px;
}

.search-go:hover {
    filter: brightness(1.08);
    transform: scale(1.05);
}

/* 分类 */
.cats {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
    margin-bottom: 18px;
}

.pill {
    position: relative;
    padding: 7px 16px;
    border-radius: 999px;
    border: 1px solid var(--line);
    background: var(--card);
    color: var(--muted);
    font-size: 13px;
    cursor: pointer;
    backdrop-filter: blur(8px);
    transition: .15s;
}

.pill:hover {
    color: var(--text);
}

.pill.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    font-weight: 600;
    box-shadow: 0 4px 14px rgba(79, 124, 255, .3);
}

/* 新建分组入口（仅编辑模式显示） */
.pill.add-cat {
    border-style: dashed;
    font-weight: 600;
    display: none;
}

body.editing .pill.add-cat {
    display: inline-block;
}

.pill.add-cat:hover {
    color: var(--accent);
    border-color: var(--accent);
    background: transparent;
}

/* 空分组删除角标（仅编辑模式显示） */
.cat-del {
    display: none;
    position: absolute;
    top: -7px;
    right: -7px;
    width: 17px;
    height: 17px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: #e5484d;
    color: #fff;
    font-size: 12px;
    line-height: 17px;
    text-align: center;
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(0, 0, 0, .25);
    transition: transform .12s;
}

.cat-del:hover {
    transform: scale(1.18);
}

body.editing .pill.empty-cat {
    border-style: dashed;
}

body.editing .pill.empty-cat .cat-del {
    display: block;
}

/* 卡片网格 */
.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
    gap: 10px;
    margin-bottom: 14px;
}

.card {
    position: relative;
    contain: content;
    border-radius: var(--radius);
    background: var(--card);
    border: 1px solid var(--line);
    backdrop-filter: blur(8px);
    animation: fadeIn .25s both;
    animation-delay: calc(var(--i) * 12ms);
    transition: transform .15s ease, box-shadow .15s ease, border-color .15s;
}

.card:hover {
    transform: translateY(-3px);
    box-shadow: var(--shadow);
    border-color: transparent;
}

.card-main {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 9px;
    width: 100%;
    padding: 16px 6px 12px;
    border-radius: inherit;
    text-decoration: none;
    color: var(--text);
}

.fav {
    width: 34px;
    height: 34px;
    border-radius: 9px;
    object-fit: contain;
    background: var(--card-solid);
    padding: 2px;
}

.fav.letter {
    display: grid;
    place-items: center;
    color: #fff;
    font-weight: 700;
    font-size: 16px;
}

.name {
    font-size: 12px;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    opacity: .85;
}

.del {
    position: absolute;
    top: 5px;
    right: 6px;
    width: 20px;
    height: 20px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: rgba(0, 0, 0, .5);
    color: #fff;
    font-size: 13px;
    line-height: 1;
    cursor: pointer;
    display: none;
    place-items: center;
}

body.editing .del {
    display: grid;
}

.empty {
    grid-column: 1/-1;
    text-align: center;
    color: var(--muted);
    padding: 40px 0;
    font-size: 14px;
}

.empty .only-edit {
    display: none;
}

body.editing .empty .only-edit {
    display: inline;
}

body.editing .empty .no-edit {
    display: none;
}

/* 添加网址按钮（仅编辑模式显示） */
.add-card {
    display: none;
    width: 100%;
    padding: 13px;
    border-radius: var(--radius);
    border: 1.5px dashed var(--line);
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    font-size: 13px;
    transition: .15s;
}

body.editing .add-card {
    display: block;
}

.add-card:hover {
    color: var(--accent);
    border-color: var(--accent);
}

/* ================= 对话框 ================= */
dialog {
    border: none;
    border-radius: 16px;
    padding: 22px;
    width: min(360px, 90vw);
    background: var(--card-solid);
    color: var(--text);
    box-shadow: var(--shadow);
}

dialog::backdrop {
    background: rgba(0, 0, 0, .35);
    backdrop-filter: blur(2px);
}

dialog h3 {
    font-size: 16px;
    margin-bottom: 14px;
}

dialog label {
    display: block;
    font-size: 12px;
    color: var(--muted);
    margin-bottom: 12px;
}

dialog input {
    display: block;
    width: 100%;
    margin-top: 5px;
    padding: 9px 12px;
    border-radius: 10px;
    border: 1px solid var(--line);
    background: var(--bg1);
    color: var(--text);
    font-size: 14px;
    outline: none;
}

dialog input[hidden] {
    display: none;
}

dialog input:focus {
    border-color: var(--accent);
}

dialog menu {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 0;
    margin-top: 8px;
}

dialog menu button {
    padding: 8px 16px;
    border-radius: 10px;
    border: 1px solid var(--line);
    background: transparent;
    color: var(--text);
    cursor: pointer;
    font-size: 13px;
}

dialog .primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    font-weight: 600;
}

/* 配置编辑器 */
#dlgCfg {
    width: min(680px, 94vw);
}

.cfg-tip {
    display: block;
    font-size: 12px;
    color: var(--muted);
    margin: -8px 0 10px;
}

.cfg-tip code,
.cfg-msg code {
    background: var(--hover);
    padding: 1px 5px;
    border-radius: 5px;
}

#cfgText {
    width: 100%;
    height: 46vh;
    min-height: 260px;
    resize: vertical;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 12px;
    background: var(--bg1);
    color: var(--text);
    outline: none;
    font: 12.5px/1.55 ui-monospace, SFMono-Regular, Consolas, "Courier New", monospace;
    tab-size: 2;
    white-space: pre;
    overflow: auto;
}

#cfgText:focus {
    border-color: var(--accent);
}

.cfg-msg {
    min-height: 18px;
    margin: 8px 2px 0;
    font-size: 12px;
}

.cfg-msg.ok {
    color: #2da44e;
}

.cfg-msg.err {
    color: #e5484d;
}

.cfg-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 10px;
}

.cfg-actions>span {
    display: flex;
    gap: 8px;
}

.cfg-actions button {
    padding: 8px 14px;
    border-radius: 10px;
    border: 1px solid var(--line);
    background: transparent;
    color: var(--text);
    cursor: pointer;
    font-size: 13px;
    transition: .15s;
}

.cfg-actions button:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.cfg-actions .primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    font-weight: 600;
}

.cfg-actions .primary:hover {
    color: #fff;
    filter: brightness(1.08);
}

/* 提示 */
.toast {
    position: fixed;
    left: 50%;
    bottom: 28px;
    transform: translate(-50%, 12px);
    background: #2b2f37;
    color: #fff;
    padding: 9px 18px;
    border-radius: 999px;
    font-size: 13px;
    opacity: 0;
    pointer-events: none;
    z-index: 99;
    box-shadow: var(--shadow);
    transition: .25s;
}

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

.toast.err {
    background: #c0392b;
}

@keyframes pop {
    from {
        opacity: 0;
        transform: translateY(-4px);
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
}

/* 无障碍：用户偏好减少动效时全部关闭 */
@media (prefers-reduced-motion: reduce) {

    *,
    *::before,
    *::after {
        animation: none !important;
        transition: none !important;
    }
}

@media (max-width: 640px) {
    .top-bar {
        padding: 12px 16px;
    }

    .container {
        padding: 8px 16px 40px;
    }

    .greeting {
        font-size: 24px;
    }

    .grid {
        grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
    }
}