/**
 * 工具箱 /tools — 桌面 + 平板 + 窄屏布局
 * 窄屏例外覆盖 viewport-shell（<640px 自然纵向滚动）
 */

.th-main {
    background: #f4f6f9;
    flex: 1 1 auto;
    min-height: 0;
    min-width: 0;
    max-width: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.th-wrap {
    flex: 1 1 auto;
    min-height: 0;
    min-width: 0;
    max-width: 1280px;
    width: 100%;
    margin: 0 auto;
    padding: 0 1rem 1.5rem;
    overflow-y: auto;
    overflow-x: clip;
}

.th-hero {
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.95) 0%, #f4f6f9 100%);
    border-bottom: 1px solid rgba(15, 23, 42, 0.06);
    padding: 2.5rem 1rem 1.5rem;
    flex-shrink: 0;
}

.th-page-title {
    font-size: clamp(1.75rem, 4vw, 2.5rem);
    font-weight: 800;
    color: #0f172a;
    letter-spacing: -0.02em;
}

.th-page-sub {
    color: #64748b;
    margin-top: 0.35rem;
    font-size: 1rem;
}

.th-btn-settings {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    min-height: 44px;
    padding: 5px 10px;
    border-radius: 8px;
    border: 1px solid rgba(15, 23, 42, 0.08);
    background: #fff;
    color: #0f172a;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(15, 23, 42, 0.05);
    flex-shrink: 0;
}

.th-btn-settings .fa {
    font-size: 11px;
}

.th-btn-settings:hover {
    border-color: var(--tools-primary);
    color: var(--tools-primary);
}

.th-fav-section {
    margin-bottom: 2rem;
}

.th-fav-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 0.75rem;
}

.th-fav-title {
    font-size: 1.05rem;
    font-weight: 700;
    color: #0f172a;
}

.th-fav-scroll-wrap {
    position: relative;
    margin-inline: -1rem;
    padding-inline: 1rem;
}

.th-fav-scroll-wrap::before,
.th-fav-scroll-wrap::after {
    content: '';
    position: absolute;
    top: 0;
    bottom: 10px;
    width: 1.75rem;
    pointer-events: none;
    z-index: 1;
    transition: opacity 0.2s ease;
}

.th-fav-scroll-wrap::before {
    left: 0;
    background: linear-gradient(to right, #f4f6f9 30%, transparent);
    opacity: 0;
}

.th-fav-scroll-wrap::after {
    right: 0;
    background: linear-gradient(to left, #f4f6f9 30%, transparent);
}

.th-fav-scroll-wrap--start::before {
    opacity: 0;
}

.th-fav-scroll-wrap:not(.th-fav-scroll-wrap--start)::before {
    opacity: 1;
}

.th-fav-scroll-wrap--end::after {
    opacity: 0;
}

.th-fav-strip {
    display: flex;
    gap: 12px;
    overflow-x: auto;
    overflow-y: hidden;
    padding-bottom: 10px;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x mandatory;
    scrollbar-width: thin;
    scrollbar-color: #94a3b8 #e2e8f0;
}

.th-fav-strip::-webkit-scrollbar {
    height: 8px;
}

.th-fav-strip::-webkit-scrollbar-track {
    background: #e2e8f0;
    border-radius: 4px;
}

.th-fav-strip::-webkit-scrollbar-thumb {
    background: #94a3b8;
    border-radius: 4px;
}

.th-fav-strip::-webkit-scrollbar-thumb:hover {
    background: #64748b;
}

.th-fav-tile {
    flex: 0 0 auto;
    scroll-snap-align: start;
    width: 108px;
    background: #fff;
    border-radius: 14px;
    border: 1px solid rgba(15, 23, 42, 0.06);
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
    padding: 12px 10px;
    text-align: center;
}

.th-fav-tile a {
    text-decoration: none;
    color: inherit;
    display: block;
    min-height: 44px;
}

.th-fav-empty {
    background: #fff;
    border: 1px dashed rgba(15, 23, 42, 0.12);
    border-radius: 14px;
    padding: 1.25rem 1rem;
    color: #64748b;
    text-align: center;
}

.th-section {
    margin-bottom: 2.25rem;
}

.th-section-head {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 1rem;
}

.th-section-head h2 {
    font-size: 1.15rem;
    font-weight: 800;
    color: #0f172a;
    margin: 0;
}

.th-badge {
    font-size: 12px;
    font-weight: 600;
    color: #64748b;
    background: #e2e8f0;
    border-radius: 999px;
    padding: 2px 10px;
}

.th-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 260px), 1fr));
    gap: 1.25rem;
}

.th-grid > * {
    min-width: 0;
}

.th-card {
    background: #fff;
    border: 1px solid rgba(15, 23, 42, 0.06);
    border-radius: 18px;
    padding: 1.35rem 1.25rem 1.25rem;
    box-shadow: 0 10px 30px rgba(15, 23, 42, 0.05);
    position: relative;
    transition: box-shadow 0.2s ease, transform 0.2s ease;
}

.th-card:hover {
    box-shadow: 0 16px 40px rgba(15, 23, 42, 0.09);
    transform: translateY(-2px);
}

.th-card-disabled {
    opacity: 0.55;
}

.th-agent-btn {
    position: absolute;
    right: 12px;
    top: 12px;
    width: 34px;
    height: 34px;
    border-radius: 999px;
    border: 1px solid rgba(15, 23, 42, 0.1);
    background: rgba(255, 255, 255, 0.95);
    color: #334155;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    z-index: 2;
}

.th-agent-btn:hover {
    color: var(--tools-primary);
    border-color: rgba(0, 102, 255, 0.25);
}

.th-tool-icon-wrap {
    width: 72px;
    height: 72px;
    margin: 0 auto 12px;
    border-radius: 16px;
    background: #f1f5f9;
    display: flex;
    align-items: center;
    justify-content: center;
}

.th-tool-icon-img {
    width: 56px;
    height: 56px;
    object-fit: contain;
    border-radius: 12px;
}

.th-tool-icon-fa {
    font-size: 2rem;
    color: #64748b;
}

.th-card h3 {
    font-size: 1.1rem;
    font-weight: 700;
    color: #0f172a;
    margin: 0 0 6px;
    text-align: center;
}

.th-card p {
    font-size: 0.875rem;
    color: #64748b;
    line-height: 1.45;
    text-align: center;
    min-height: 2.6em;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.th-primary-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    margin-top: 14px;
    padding: 10px 14px;
    border-radius: 12px;
    font-weight: 700;
    text-decoration: none;
    color: #fff;
    background: var(--tools-primary);
    border: 0;
    cursor: pointer;
}

.th-primary-btn:disabled,
.th-primary-btn.th-muted {
    background: #cbd5e1;
    color: #64748b;
    cursor: not-allowed;
}

.th-modal-mask {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.45);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 1400;
    padding: 16px;
}

.th-modal-mask.show {
    display: flex;
}

.th-modal {
    width: min(640px, 96vw);
    max-height: 88vh;
    background: #fff;
    border-radius: 16px;
    border: 1px solid #e2e8f0;
    box-shadow: 0 24px 80px rgba(15, 23, 42, 0.25);
    overflow: hidden;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
}

.th-modal-head {
    padding: 14px 16px;
    border-bottom: 1px solid #eef2f7;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.th-modal-head h3 {
    margin: 0;
    font-size: 18px;
    color: #0f172a;
}

.th-modal-body {
    padding: 14px 16px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
}

#toolsHubSettingsHint {
    white-space: pre-line;
    line-height: 1.55;
}

.th-modal-foot {
    padding: 12px 14px;
    border-top: 1px solid #eef2f7;
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    flex-wrap: wrap;
}

.th-btn {
    border: 0;
    border-radius: 10px;
    padding: 9px 14px;
    font-size: 13px;
    font-weight: 700;
    cursor: pointer;
}

.th-btn-ghost {
    background: #e2e8f0;
    color: #334155;
}

.th-btn-primary {
    background: var(--tools-primary);
    color: #fff;
}

.th-sort-list {
    list-style: none;
    margin: 0;
    padding: 0;
    min-height: 36px;
}

.th-sort-list li {
    padding: 10px 12px;
    margin-bottom: 8px;
    border-radius: 10px;
    border: 1px solid #e2e8f0;
    background: #f8fafc;
    cursor: grab;
    font-size: 14px;
    color: #0f172a;
    font-weight: 600;
    touch-action: pan-y;
    user-select: none;
    -webkit-user-select: none;
}

.th-sortable-hint {
    display: none;
    font-size: 12px;
    color: #64748b;
    margin: 0 0 10px;
    line-height: 1.45;
}

.th-sortable-ghost {
    opacity: 0.45;
    background: #eff6ff !important;
    border-color: #93c5fd !important;
}

.th-sortable-chosen {
    background: #eef2ff;
    border-color: #818cf8;
    box-shadow: inset 0 0 0 1px rgba(99, 102, 241, 0.2);
}

.th-sortable-drag {
    opacity: 1 !important;
    box-shadow: 0 10px 28px rgba(15, 23, 42, 0.16);
    border-color: #93c5fd;
    cursor: grabbing;
}

.th-sort-list li.sortable-fallback {
    opacity: 1 !important;
    box-shadow: 0 12px 32px rgba(15, 23, 42, 0.18);
    border-color: #93c5fd;
    cursor: grabbing;
    z-index: 10000;
}

.th-settings-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}

.th-settings-table th,
.th-settings-table td {
    border-bottom: 1px solid #eef2f7;
    padding: 10px 8px;
    text-align: left;
    vertical-align: middle;
}

.th-settings-table th {
    color: #64748b;
    font-weight: 700;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.agent-picker-mask {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.42);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 1500;
    padding: 16px;
}

.agent-picker-mask.show {
    display: flex;
}

.agent-picker-modal {
    width: min(460px, 96vw);
    max-height: min(82vh, 680px);
    border-radius: 16px;
    background: #fff;
    border: 1px solid #dbe5f0;
    box-shadow: 0 24px 80px rgba(15, 23, 42, 0.28);
    overflow: hidden;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
}

.agent-picker-head {
    padding: 14px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #eef2f7;
}

.agent-picker-head h3 {
    margin: 0;
    font-size: 18px;
    color: #0f172a;
}

.agent-picker-close {
    width: 30px;
    height: 30px;
    border-radius: 999px;
    border: 0;
    background: #e2e8f0;
    color: #334155;
    cursor: pointer;
    font-weight: 700;
}

.agent-picker-list {
    padding: 10px;
    overflow: auto;
    max-height: 52vh;
}

.agent-picker-item {
    width: 100%;
    text-align: left;
    padding: 10px 12px;
    border-radius: 10px;
    border: 1px solid #e2e8f0;
    margin-bottom: 8px;
    cursor: pointer;
    background: #fff;
    color: #0f172a;
    font-size: 14px;
}

.agent-picker-item.active {
    border-color: #2563eb;
    background: #eff6ff;
    color: #1d4ed8;
    font-weight: 700;
}

.agent-picker-foot {
    border-top: 1px solid #eef2f7;
    padding: 12px 14px;
    display: flex;
    justify-content: space-between;
    gap: 12px;
}

.agent-picker-btn {
    border: 0;
    border-radius: 10px;
    padding: 9px 14px;
    font-size: 13px;
    font-weight: 700;
    cursor: pointer;
}

.agent-picker-btn-secondary {
    background: #e2e8f0;
    color: #334155;
}

.agent-picker-btn-primary {
    background: #2563eb;
    color: #fff;
}

/* ---------- 平板 640–1023px ---------- */
@media (min-width: 640px) and (max-width: 1023px) {
    .th-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 1rem;
    }

    .th-card {
        padding: 1.1rem 1rem;
    }
}

/* ---------- 窄屏 <640px ---------- */
@media (max-width: 639px) {
    #main-content:has(> .th-main) {
        height: auto;
        max-height: none;
        overflow: visible;
    }

    #main-content:has(> .th-main) > .th-main {
        max-height: none;
        overflow: visible;
        flex: none;
    }

    .th-wrap {
        overflow-y: visible;
        flex: none;
    }

    .th-hero {
        padding: 1.25rem 1rem 0.75rem;
    }

    .th-page-title {
        font-size: clamp(1.35rem, 6vw, 1.75rem);
    }

    .th-btn-settings span {
        display: none;
    }

    .th-fav-tile {
        width: 84px;
        padding: 10px 8px;
    }

    .th-fav-tile .th-tool-icon-wrap {
        width: 48px !important;
        height: 48px !important;
        margin-bottom: 6px !important;
    }

    .th-fav-tile .th-tool-icon-img {
        width: 32px;
        height: 32px;
    }

    .th-fav-tile .th-tool-icon-fa {
        font-size: 1.25rem;
    }

    .th-grid {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
    }

    .th-card {
        display: grid;
        grid-template-columns: 52px 1fr auto;
        grid-template-rows: auto auto;
        align-items: center;
        gap: 0 0.75rem;
        padding: 0.85rem 1rem;
        text-align: left;
        border-radius: 14px;
    }

    .th-card:hover {
        transform: none;
    }

    .th-tool-icon-wrap {
        width: 48px;
        height: 48px;
        margin: 0;
        grid-row: 1 / span 2;
        border-radius: 12px;
    }

    .th-tool-icon-img {
        width: 36px;
        height: 36px;
    }

    .th-tool-icon-fa {
        font-size: 1.35rem;
    }

    .th-card h3 {
        text-align: left;
        margin: 0;
        font-size: 0.95rem;
        align-self: end;
    }

    .th-card p {
        text-align: left;
        -webkit-line-clamp: 1;
        line-clamp: 1;
        min-height: 0;
        font-size: 0.8rem;
        align-self: start;
    }

    .th-card .th-primary-btn {
        display: none;
    }

    .th-card:not(.th-card-disabled):has(.th-primary-btn:not(.th-muted)) {
        cursor: pointer;
    }

    .th-card:not(.th-card-disabled):has(.th-primary-btn:not(.th-muted))::after {
        content: '\f105';
        font-family: FontAwesome;
        color: #94a3b8;
        grid-column: 3;
        grid-row: 1 / span 2;
        font-size: 1.1rem;
        line-height: 1;
    }

    .th-modal-mask {
        padding: 0;
    }

    .th-modal-mask .th-modal {
        width: 100%;
        max-width: 100%;
        height: 100%;
        max-height: 100%;
        border-radius: 0;
        border: 0;
    }

    .th-modal-foot .th-btn {
        flex: 1 1 auto;
        min-width: 0;
    }

    .th-settings-table {
        font-size: 13px;
    }

    .th-settings-table th,
    .th-settings-table td {
        padding: 8px 6px;
    }

    .th-sortable-hint {
        display: block;
    }

    .th-sort-list li {
        touch-action: pan-y;
    }

    .th-sort-list li.th-sortable-drag,
    .th-sort-list li.sortable-fallback {
        touch-action: none;
    }

    .agent-picker-mask {
        padding: 0;
    }

    .agent-picker-modal {
        width: 100%;
        max-width: 100%;
        height: 100%;
        max-height: 100%;
        border-radius: 0;
        border: 0;
    }

    .agent-picker-list {
        max-height: none;
    }

    .agent-picker-foot {
        flex-wrap: wrap;
    }

    .agent-picker-foot .agent-picker-btn {
        flex: 1 1 auto;
        min-width: 0;
    }
}

@media (prefers-reduced-motion: reduce) {
    .th-card {
        transition: none;
    }

    .th-fav-scroll-wrap::before,
    .th-fav-scroll-wrap::after {
        transition: none;
    }
}
