/* Template Store — V3 redesign.
   Warmer dark palette, Space Grotesk display titles, uniform card grid
   with a darker footer band. Scopes its own color/font tokens so the
   page stands apart from the Remote Control dashboard that shares
   /style.css. */

.templates-body {
    --tpl-bg: #111319;
    --tpl-card: #1c1f27;
    --tpl-border: rgba(255, 255, 255, 0.05);
    --tpl-border-hover: rgba(255, 255, 255, 0.12);
    --tpl-control-active-bg: rgba(255, 255, 255, 0.08);
    --tpl-control-active-border: rgba(255, 255, 255, 0.1);
    --tpl-footer-band: rgba(0, 0, 0, 0.25);
    --tpl-fg: #f4f3ee;
    --tpl-fg-dim: #a8a9ae;
    --tpl-fg-faint: #6e6f76;
    --tpl-accent: #6BA292;
    --tpl-accent-bg: rgba(107, 162, 146, 0.12);

    --tpl-font-sans: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
    --tpl-font-display: 'Space Grotesk', 'Inter', sans-serif;
    --tpl-font-mono: 'JetBrains Mono', ui-monospace, monospace;

    background: var(--tpl-bg);
    color: var(--tpl-fg);
    font-family: var(--tpl-font-sans);
}

.templates-main {
    max-width: 1200px;
    margin: 0 auto;
    padding: 28px 32px 56px;
}

/* ---------- Page header (title / count / nav buttons) ---------- */

.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 26px;
    flex-wrap: wrap;
}

.page-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
    flex-wrap: wrap;
}

.brand-link {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
    color: var(--tpl-fg);
    min-width: 0;
    transition: opacity 0.15s;
}

.brand-link:hover {
    opacity: 0.85;
}

.brand-icon {
    width: 36px;
    height: 36px;
    display: block;
    flex-shrink: 0;
}

.brand-name {
    font-family: var(--tpl-font-display);
    font-size: 24px;
    font-weight: 700;
    letter-spacing: -0.7px;
    color: var(--tpl-fg);
    line-height: 1;
}

.brand-separator {
    font-family: var(--tpl-font-display);
    font-size: 24px;
    font-weight: 400;
    color: var(--tpl-fg-faint);
    line-height: 1;
    margin: 0 -2px;
}

.page-title {
    font-family: var(--tpl-font-display);
    font-size: 24px;
    font-weight: 700;
    letter-spacing: -0.7px;
    color: var(--tpl-fg);
    margin: 0;
    line-height: 1;
}

.page-count {
    font-family: var(--tpl-font-mono);
    font-size: 12px;
    color: var(--tpl-fg-faint);
}

@media (max-width: 520px) {
    .brand-name,
    .brand-separator {
        display: none;
    }
}

.page-header-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.auth-indicator {
    font-size: 12px;
    color: var(--tpl-fg-faint);
    font-family: var(--tpl-font-sans);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 240px;
}

/* Small pill button shown next to the auth indicator; hidden when
   signed out. Uses the same surface as .ghost-button but smaller, so it
   reads as a quieter companion action to Remote Control. */
.sign-out-button {
    background: transparent;
    border: 1px solid var(--tpl-border);
    color: var(--tpl-fg-dim);
    padding: 6px 12px;
    border-radius: 999px;
    font-size: 12px;
    font-family: var(--tpl-font-sans);
    font-weight: 500;
    cursor: pointer;
    transition: border-color 0.15s, color 0.15s;
}

.sign-out-button:hover {
    border-color: var(--tpl-border-hover);
    color: var(--tpl-fg);
}

/* Mirror of .sign-out-button shown when the user is signed out. Tinted
   with the accent so anonymous visitors can tell sign-in is available
   (required to install templates) without making the pill shout. */
.sign-in-button {
    background: var(--tpl-accent-bg);
    border: 1px solid transparent;
    color: var(--tpl-accent);
    padding: 6px 14px;
    border-radius: 999px;
    font-size: 12px;
    font-family: var(--tpl-font-sans);
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

.sign-in-button:hover {
    background: rgba(107, 162, 146, 0.2);
    border-color: rgba(107, 162, 146, 0.35);
}

.ghost-button {
    background: transparent;
    border: 1px solid var(--tpl-border);
    color: var(--tpl-fg-dim);
    padding: 9px 16px;
    border-radius: 999px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    font-family: var(--tpl-font-sans);
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    transition: border-color 0.15s, color 0.15s, background 0.15s;
}

.ghost-button:hover {
    border-color: var(--tpl-border-hover);
    color: var(--tpl-fg);
}

/* ---------- Browse controls (tabs / category / search) ---------- */

.browse-controls {
    display: flex;
    gap: 10px;
    margin-bottom: 22px;
    flex-wrap: wrap;
    align-items: center;
}

.sort-tabs {
    display: flex;
    gap: 4px;
    height: 40px;
    align-items: center;
}

.sort-tab {
    height: 40px;
    padding: 0 14px;
    border: 1px solid transparent;
    background: transparent;
    color: var(--tpl-fg-dim);
    cursor: pointer;
    border-radius: 10px;
    font-size: 13px;
    font-weight: 500;
    font-family: var(--tpl-font-sans);
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.sort-tab.active {
    background: var(--tpl-control-active-bg);
    border-color: var(--tpl-control-active-border);
    color: var(--tpl-fg);
}

.sort-tab:hover:not(.active) {
    color: var(--tpl-fg);
}

.control-divider {
    width: 1px;
    height: 24px;
    background: var(--tpl-border);
    flex-shrink: 0;
}

.category-select {
    height: 40px;
    background: var(--tpl-card);
    color: var(--tpl-fg);
    border: 1px solid var(--tpl-border);
    border-radius: 10px;
    padding: 0 14px;
    font-size: 13px;
    font-family: var(--tpl-font-sans);
    min-width: 170px;
    cursor: pointer;
    appearance: none;
    -webkit-appearance: none;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6' fill='none'><path d='M1 1l4 4 4-4' stroke='%236e6f76' stroke-width='1.5' stroke-linecap='round'/></svg>");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 32px;
}

.search-input-wrapper {
    position: relative;
    flex: 1;
    min-width: 200px;
    height: 40px;
}

.search-input-wrapper .search-icon {
    position: absolute;
    left: 14px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--tpl-fg-faint);
    pointer-events: none;
}

.search-input {
    height: 40px;
    width: 100%;
    background: var(--tpl-card);
    color: var(--tpl-fg);
    border: 1px solid var(--tpl-border);
    border-radius: 10px;
    padding: 0 14px 0 36px;
    font-size: 13px;
    font-family: var(--tpl-font-sans);
    outline: none;
}

.search-input::placeholder {
    color: var(--tpl-fg-faint);
}

.search-input:focus {
    border-color: var(--tpl-border-hover);
}

/* ---------- Card grid ---------- */

.card-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
}

.template-card {
    background: var(--tpl-card);
    border: 1px solid var(--tpl-border);
    border-radius: 14px;
    transition: border-color 0.15s, transform 0.15s;
    display: flex;
    flex-direction: column;
    min-height: 230px;
    overflow: hidden;
}

.template-card:hover {
    border-color: var(--tpl-border-hover);
    transform: translateY(-2px);
}

/* Splits the card into two independently-clickable zones: body → template
   detail, footer-author → user page. Can't nest anchors, so each zone has
   its own <a>. */
.card-body-link {
    text-decoration: none;
    color: inherit;
    display: flex;
    flex-direction: column;
    flex: 1;
    padding: 20px;
    cursor: pointer;
}

.card-body-link:focus-visible {
    outline: 2px solid var(--tpl-border-hover);
    outline-offset: -2px;
}

.template-card .title {
    font-family: var(--tpl-font-display);
    font-size: 19px;
    font-weight: 600;
    line-height: 1.2;
    letter-spacing: -0.4px;
    color: var(--tpl-fg);
    margin: 0 0 10px 0;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.template-card .desc {
    font-family: var(--tpl-font-sans);
    font-size: 13px;
    line-height: 1.55;
    color: var(--tpl-fg-dim);
    margin: 0;
    display: -webkit-box;
    -webkit-line-clamp: 5;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.template-card .card-footer {
    background: var(--tpl-footer-band);
    border-top: 1px solid var(--tpl-border);
    padding: 12px 20px;
    display: flex;
    align-items: center;
    gap: 10px;
}

/* Author avatar: colored initial circle, or actual profile image if present */
.author-avatar {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    flex-shrink: 0;
    display: inline-grid;
    place-items: center;
    font-size: 12px;
    font-weight: 700;
    color: #111319;
    object-fit: cover;
    overflow: hidden;
    font-family: var(--tpl-font-sans);
}

/* Author group (avatar + name). The anchor variant navigates to the user's
   page; a plain span is rendered when userId is missing so we never ship a
   dead link. Sized to shrink gracefully when the card is narrow, so the
   category chip and star count never get pushed off the right edge. */
.author-link {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    flex-shrink: 1;
    overflow: hidden;
    color: inherit;
    border-radius: 6px;
}

a.author-link {
    text-decoration: none;
    cursor: pointer;
    transition: color 0.15s;
}

a.author-link:hover .author-name {
    color: var(--tpl-accent);
}

a.author-link:focus-visible {
    outline: 2px solid var(--tpl-border-hover);
    outline-offset: 2px;
}

.author-name {
    font-size: 12px;
    color: var(--tpl-fg);
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-family: var(--tpl-font-sans);
    min-width: 0;
    transition: color 0.15s;
}

.category-chip {
    font-family: var(--tpl-font-sans);
    font-size: 10px;
    color: var(--tpl-accent);
    background: var(--tpl-accent-bg);
    padding: 2px 7px;
    border-radius: 4px;
    font-weight: 600;
    letter-spacing: 0.3px;
    text-transform: uppercase;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 120px;
    flex-shrink: 0;
}

.template-card .star-count {
    margin-left: auto;
    font-family: var(--tpl-font-mono);
    font-size: 12px;
    color: var(--tpl-fg-faint);
    font-variant-numeric: tabular-nums;
    flex-shrink: 0;
    white-space: nowrap;
}

/* ---------- Detail page ---------- */

.detail-page {
    background: var(--tpl-card);
    border: 1px solid var(--tpl-border);
    border-radius: 14px;
    padding: 32px;
    max-width: 860px;
}

.back-link {
    color: var(--tpl-fg-dim);
    font-size: 13px;
    text-decoration: none;
    display: inline-block;
    margin-bottom: 20px;
    font-family: var(--tpl-font-sans);
    transition: color 0.15s;
}

.back-link:hover {
    color: var(--tpl-fg);
}

.detail-page h1 {
    font-family: var(--tpl-font-display);
    font-size: 30px;
    font-weight: 700;
    letter-spacing: -0.6px;
    line-height: 1.2;
    color: var(--tpl-fg);
    margin: 0 0 18px 0;
}

.detail-page .detail-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 14px;
    font-size: 13px;
    margin-bottom: 24px;
    align-items: center;
    font-family: var(--tpl-font-sans);
    color: var(--tpl-fg-dim);
}

.detail-page .detail-meta strong {
    color: var(--tpl-fg);
    font-weight: 500;
}

.detail-page .detail-meta .star-count {
    font-family: var(--tpl-font-mono);
    font-size: 12px;
    color: var(--tpl-fg-faint);
    font-variant-numeric: tabular-nums;
}

.detail-page .detail-meta .detail-date {
    font-family: var(--tpl-font-mono);
    font-size: 12px;
    color: var(--tpl-fg-faint);
}

.detail-page .detail-desc {
    color: var(--tpl-fg-dim);
    font-size: 14px;
    line-height: 1.65;
    white-space: pre-wrap;
    margin-bottom: 28px;
    font-family: var(--tpl-font-sans);
}

.detail-page .send-button {
    padding: 12px 22px;
    font-size: 14px;
    font-weight: 600;
    background: var(--tpl-accent);
    color: #111319;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    font-family: var(--tpl-font-sans);
    transition: filter 0.15s;
}

.detail-page .send-button:hover:not(:disabled) {
    filter: brightness(1.08);
}

.detail-page .send-button:disabled {
    opacity: 0.6;
    cursor: wait;
}

.spinner {
    display: inline-block;
    width: 12px;
    height: 12px;
    border: 2px solid rgba(17, 19, 25, 0.3);
    border-top-color: #111319;
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
    margin-right: 6px;
    vertical-align: middle;
}

/* ---------- Centered page-level loader ---------- */

.loader {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    gap: 12px;
    color: var(--tpl-fg-faint);
    font-size: 13px;
    font-family: var(--tpl-font-sans);
}

.loader::before {
    content: "";
    width: 36px;
    height: 36px;
    border: 3px solid var(--tpl-border);
    border-top-color: var(--tpl-accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.card-grid .loader {
    grid-column: 1 / -1;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ---------- User templates header ---------- */

.user-templates-header {
    display: flex;
    align-items: flex-start;
    gap: 14px;
    margin-bottom: 22px;
}

.user-header-text {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 0;
    flex: 1;
}

.user-templates-header h2 {
    font-family: var(--tpl-font-display);
    font-size: 22px;
    font-weight: 700;
    letter-spacing: -0.5px;
    color: var(--tpl-fg);
    margin: 0;
    line-height: 1.1;
}

.user-header-desc {
    font-family: var(--tpl-font-sans);
    font-size: 13px;
    line-height: 1.5;
    color: var(--tpl-fg-dim);
    white-space: pre-wrap;
    margin: 0;
    max-width: 720px;
}

/* ---------- Top users leaderboard ---------- */

.user-row-link {
    text-decoration: none;
    color: inherit;
    display: block;
}

.user-row {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 16px 20px;
    background: var(--tpl-card);
    border: 1px solid var(--tpl-border);
    border-radius: 14px;
    margin-bottom: 10px;
    cursor: pointer;
    transition: border-color 0.15s, transform 0.15s;
}

.user-row:hover {
    border-color: var(--tpl-border-hover);
    transform: translateY(-2px);
}

.user-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    flex-shrink: 0;
    display: inline-grid;
    place-items: center;
    font-size: 16px;
    font-weight: 700;
    color: #111319;
    object-fit: cover;
    overflow: hidden;
    font-family: var(--tpl-font-sans);
}

.user-row .user-body {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.user-row .user-name {
    font-family: var(--tpl-font-display);
    font-size: 15px;
    font-weight: 600;
    letter-spacing: -0.2px;
    color: var(--tpl-fg);
}

.user-row .user-desc {
    font-size: 12px;
    color: var(--tpl-fg-dim);
    line-height: 1.45;
    white-space: pre-wrap;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.user-row .user-stats {
    font-family: var(--tpl-font-mono);
    font-size: 12px;
    color: var(--tpl-fg-faint);
    text-align: right;
    white-space: nowrap;
    flex-shrink: 0;
    font-variant-numeric: tabular-nums;
}

/* ---------- Modal ---------- */

.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

.modal {
    background: var(--tpl-card);
    border: 1px solid var(--tpl-border);
    border-radius: 14px;
    padding: 24px;
    max-width: 440px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
    font-family: var(--tpl-font-sans);
}

.modal h3 {
    font-family: var(--tpl-font-display);
    font-size: 18px;
    font-weight: 600;
    letter-spacing: -0.3px;
    color: var(--tpl-fg);
    margin: 0 0 14px 0;
}

.modal .modal-device-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 16px;
}

.modal .device-item {
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid var(--tpl-border);
    border-radius: 10px;
    padding: 12px 14px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}

.modal .device-item .device-name {
    font-size: 14px;
    font-weight: 500;
    color: var(--tpl-fg);
}

.modal .device-item .device-last-seen {
    font-size: 12px;
    color: var(--tpl-fg-faint);
    font-family: var(--tpl-font-mono);
    margin-top: 2px;
}

.modal .modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

/* ---------- Toast ---------- */

.toast-root {
    position: fixed;
    bottom: 20px;
    right: 20px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 200;
}

.toast {
    background: var(--tpl-card);
    border: 1px solid var(--tpl-border);
    border-left: 4px solid var(--tpl-accent);
    padding: 12px 16px;
    border-radius: 10px;
    max-width: 360px;
    font-size: 13px;
    font-family: var(--tpl-font-sans);
    color: var(--tpl-fg);
    animation: toast-in 0.2s ease-out;
}

.toast.success { border-left-color: var(--tpl-accent); }
.toast.error { border-left-color: #d97777; }

@keyframes toast-in {
    from { opacity: 0; transform: translateX(20px); }
    to { opacity: 1; transform: translateX(0); }
}

/* ---------- Empty state ---------- */

.empty-state {
    text-align: center;
    padding: 56px 20px;
    color: var(--tpl-fg-faint);
    font-family: var(--tpl-font-sans);
    font-size: 13px;
    grid-column: 1 / -1;
}

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
    .card-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 600px) {
    .templates-main {
        padding: 20px 16px 40px;
    }

    .page-header {
        margin-bottom: 20px;
    }

    .browse-controls {
        flex-direction: column;
        align-items: stretch;
    }

    .sort-tabs {
        overflow-x: auto;
        width: 100%;
    }

    .control-divider {
        display: none;
    }

    .category-select {
        min-width: 0;
        width: 100%;
    }

    .card-grid {
        grid-template-columns: 1fr;
    }

    .detail-page {
        padding: 20px;
    }

    .detail-page h1 {
        font-size: 24px;
    }
}

.hidden {
    display: none !important;
}
