/* ===================================================
    Task Manager Sidebar — Matches play-styles.css design system
    Claymorphism + Professional SaaS
    Variables mirror :root in play-styles.css
    =================================================== */

/* Base Reset for sidebar components */
.tm-sidebar,
.tm-sidebar *,
.tm-fab,
.tm-fab * {
    box-sizing: border-box;
}

/* ── FAB Toggle Button ─────────────────────────── */
.tm-fab {
    position: fixed;
    right: 24px;
    bottom: 32px;
    z-index: 1200;
    height: 48px;
    padding: 0 20px 0 16px;
    border-radius: 24px;
    background: linear-gradient(135deg, #7C3AED 0%, #6C63FF 100%);
    color: #fff;
    border: none;
    cursor: pointer;
    box-shadow:
        0 4px 20px rgba(108, 99, 255, 0.4),
        inset 0 1px 0 rgba(255,255,255,0.3);
    transition:
        transform 0.28s cubic-bezier(0.34, 1.56, 0.64, 1),
        box-shadow 0.2s ease;
    outline: none;
    -webkit-tap-highlight-color: transparent;
}

.tm-fab:hover {
    transform: scale(1.05) translateY(-2px);
    box-shadow:
        0 8px 28px rgba(108, 99, 255, 0.5),
        inset 0 1px 0 rgba(255,255,255,0.3);
}

.tm-fab:active {
    transform: scale(0.96);
}

.tm-fab-content {
    display: flex;
    align-items: center;
    gap: 8px;
}

.tm-fab .material-symbols-outlined {
    font-size: 20px;
    font-variation-settings: 'FILL' 1, 'wght' 400;
}

.tm-fab-text {
    font-size: 0.95rem;
    font-weight: 600;
    font-family: var(--font-heading, 'Fredoka', sans-serif);
    letter-spacing: 0.02em;
}

/* FAB Badge (pending count) */
.tm-fab-badge {
    position: absolute;
    top: -6px;
    right: -6px;
    background: #EF4444;
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 6px;
    border: 2px solid #fff;
    box-shadow: 0 2px 4px rgba(239, 68, 68, 0.3);
    line-height: 1;
    pointer-events: none;
}

.tm-fab-badge:not(.d-none) {
    animation: tmBadgePop 0.38s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

@keyframes tmBadgePop {
    from { transform: scale(0); opacity: 0; }
    to   { transform: scale(1); opacity: 1; }
}

/* ── Sidebar Panel (Floating Widget) ──────────────── */
.tm-sidebar {
    position: fixed;
    top: calc(var(--header-height, 64px) + 20px);
    right: 20px;
    width: min(360px, calc(100vw - 40px));
    max-width: 420px;
    height: calc(100dvh - var(--header-height, 64px) - 40px);
    border-radius: var(--radius-clay, 24px);
    z-index: 1100;
    display: flex;
    flex-direction: column;

    /* Glassmorphism card — matches claymorphism pallette */
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(24px) saturate(1.8);
    -webkit-backdrop-filter: blur(24px) saturate(1.8);
    border: 2px solid rgba(255, 255, 255, 0.7);
    box-shadow:
        0 20px 40px rgba(30, 41, 59, 0.15),
        inset 0 1px 0 rgba(255,255,255,0.6);

    /* Floating panel slide-in */
    transform: translateX(120%) scale(0.95);
    opacity: 0;
    transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.4s ease;
    will-change: transform, opacity;
    min-height: 0;
    overflow: hidden;
}

.tm-sidebar.tm-open {
    transform: translateX(0) scale(1);
    opacity: 1;
}

/* Ensure nested flex scrolling works correctly */
/* Fix for: https://stackoverflow.com/questions/36247140/nested-flexbox-and-vertical-scroll */
.tm-sidebar > * {
    min-width: 0;
    min-height: 0;
}

/* ── Sidebar Header ────────────────────────────── */
.tm-sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 20px 16px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    background: rgba(255, 255, 255, 0.5);
    border-radius: var(--radius-clay, 24px) var(--radius-clay, 24px) 0 0;
    flex-shrink: 0;
    gap: 8px;
}

.tm-sidebar-title {
    display: flex;
    align-items: center;
    gap: 9px;
    font-family: var(--font-heading, 'Fredoka', sans-serif);
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary, #333);
    letter-spacing: 0.01em;
    flex: 1;
    min-width: 0;
}

/* Gradient icon bubble */
.tm-header-icon {
    width: 30px;
    height: 30px;
    border-radius: 10px;
    background: linear-gradient(135deg, #7C3AED 0%, #6C63FF 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(108,99,255,0.35);
}

.tm-header-icon .material-symbols-outlined {
    font-size: 16px;
    font-variation-settings: 'FILL' 1;
}

/* Pending count pill */
.tm-pending-pill {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #7C3AED, #6C63FF);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    padding: 2px 7px;
    border-radius: var(--radius-full, 9999px);
    min-width: 20px;
    flex-shrink: 0;
}

/* Header icon buttons */
.tm-header-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
}

.tm-icon-btn {
    background: none;
    border: none;
    cursor: pointer;
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm, 8px);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary, #666);
    transition: background 0.15s, color 0.15s, transform 0.15s;
    outline: none;
    -webkit-tap-highlight-color: transparent;
}

.tm-icon-btn:hover {
    background: rgba(108, 99, 255, 0.1);
    color: #6C63FF;
    transform: scale(1.05);
}

.tm-icon-btn:active { transform: scale(0.9); }

.tm-icon-btn .material-symbols-outlined {
    font-size: 18px;
}

/* ── Task List ─────────────────────────────────── */
.tm-list {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: 12px 12px 90px; /* 留出足夠的 padding-bottom 避免最後一張卡片的按鈕被 tm-fab 遮擋 */
    display: flex;
    flex-direction: column;
    gap: 8px;
    scrollbar-width: thin;
    scrollbar-color: rgba(108,99,255,0.2) transparent;
}

.tm-list::-webkit-scrollbar { width: 4px; }
.tm-list::-webkit-scrollbar-track { background: transparent; }
.tm-list::-webkit-scrollbar-thumb {
    background: rgba(108, 99, 255, 0.2);
    border-radius: 2px;
}

/* ── Empty State ───────────────────────────────── */
.tm-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 48px 20px;
    gap: 12px;
    text-align: center;
    flex: 1;
    height: 100%;
}

.tm-empty .material-symbols-outlined {
    font-size: 52px;
    color: #C4B5FD;
    font-variation-settings: 'FILL' 1;
}

.tm-empty p {
    margin: 0;
    font-family: var(--font-heading, 'Fredoka', sans-serif);
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-secondary, #666);
}

.tm-empty small {
    font-size: 0.78rem;
    color: var(--text-muted, #999);
    line-height: 1.4;
}

/* ── Task Card ─────────────────────────────────── */
.tm-card {
    background: rgba(255, 255, 255, 0.82);
    border: var(--border-clay-sm, 3px) solid rgba(255, 255, 255, 0.85);
    border-radius: var(--radius-clay-sm, 16px);
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 9px;
    box-shadow:
        4px 4px 10px rgba(30, 41, 59, 0.08),
        -2px -2px 6px rgba(255, 255, 255, 0.6);
    transition: transform 0.2s cubic-bezier(0.34, 1.2, 0.64, 1), box-shadow 0.2s;
    animation: tmCardIn 0.32s ease both;
    position: relative;
    overflow: hidden;
    /* CRITICAL: Prevent flex from shrinking card when list overflows */
    flex-shrink: 0;
    /* Card bounds */
    min-width: 0;
    width: 100%;
    /* Fixed max-width ensures cards don't stretch too wide on large sidebar */
    max-width: 100%;
}

.tm-card:hover {
    transform: translateY(-2px);
    box-shadow:
        6px 6px 16px rgba(30, 41, 59, 0.12),
        -2px -2px 8px rgba(255, 255, 255, 0.7);
}

@keyframes tmCardIn {
    from { opacity: 0; transform: translateY(10px) scale(0.97); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* Top shimmer for pending tasks */
.tm-card[data-status="pending"]::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 3px;
    background: linear-gradient(90deg, #7C3AED, #6C63FF, #A78BFA, #7C3AED);
    background-size: 200% 100%;
    animation: tmShimmer 1.8s linear infinite;
    border-radius: 16px 16px 0 0;
}

@keyframes tmShimmer {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* ── Card Header ───────────────────────────────── */
.tm-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
}

.tm-type-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 0.7rem;
    font-weight: 700;
    color: #7C3AED;
    background: rgba(124, 58, 237, 0.08);
    padding: 3px 8px;
    border-radius: var(--radius-full, 9999px);
    letter-spacing: 0.03em;
    text-transform: uppercase;
}

.tm-type-badge .material-symbols-outlined {
    font-size: 13px;
    font-variation-settings: 'FILL' 1;
}

/* ── Status Dot ────────────────────────────────── */
.tm-status-dot {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    flex-shrink: 0;
}

.tm-status--pending {
    background: #6C63FF;
    box-shadow: 0 0 0 3px rgba(108,99,255,0.2);
    animation: tmPulse 1.8s ease infinite;
}

.tm-status--success {
    background: #10B981;
    box-shadow: 0 0 0 3px rgba(16,185,129,0.15);
}

.tm-status--failed {
    background: #EF4444;
    box-shadow: 0 0 0 3px rgba(239,68,68,0.15);
}

@keyframes tmPulse {
    0%, 100% { box-shadow: 0 0 0 3px rgba(108,99,255,0.2); }
    50%       { box-shadow: 0 0 0 7px rgba(108,99,255,0.05); }
}

/* ── Preview Area ──────────────────────────────── */
.tm-preview {
    width: 100%;
    aspect-ratio: 16 / 9;
    /* CRITICAL: Safari aspect-ratio + flex-shrink protection */
    flex-shrink: 0;
    min-height: 180px;
    max-height: 260px;
    border-radius: 10px;
    overflow: hidden;
    background: rgba(245, 247, 250, 0.8);
    border: 2px solid rgba(255,255,255,0.7);
    cursor: pointer;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.2s;
}

.tm-preview:hover { opacity: 0.92; }

.tm-preview img,
.tm-preview video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.tm-preview--video { cursor: pointer; }

.tm-play-icon {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.22);
    opacity: 0;
    transition: opacity 0.2s;
}

.tm-preview--video:hover .tm-play-icon { opacity: 1; }

.tm-play-icon .material-symbols-outlined {
    font-size: 38px;
    color: #fff;
    font-variation-settings: 'FILL' 1;
    filter: drop-shadow(0 2px 8px rgba(0,0,0,0.4));
}

.tm-preview--3d {
    background: linear-gradient(135deg, #EDE9FE 0%, #DDD6FE 100%);
    cursor: default;
}

.tm-preview--3d .material-symbols-outlined {
    font-size: 38px;
    color: #7C3AED;
    font-variation-settings: 'FILL' 1;
    opacity: 0.75;
}

/* ── Progress Bar ──────────────────────────────── */
.tm-progress-wrap {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 2px 0;
}

.tm-progress-bar {
    flex: 1;
    height: 6px;
    background: rgba(0, 0, 0, 0.06);
    border-radius: 3px;
    overflow: hidden;
}

.tm-progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #7C3AED 0%, #6C63FF 50%, #A78BFA 100%);
    background-size: 200% 100%;
    border-radius: 3px;
    transition: width 1s ease;
    animation: tmProgressShimmer 2s linear infinite;
}

@keyframes tmProgressShimmer {
    0%   { background-position: 0% 0; }
    100% { background-position: 200% 0; }
}

.tm-progress-pct {
    font-size: 0.7rem;
    font-weight: 700;
    color: #7C3AED;
    min-width: 32px;
    text-align: right;
}

/* ── Error Message ─────────────────────────────── */
.tm-error-msg {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.76rem;
    color: #DC2626;
    padding: 6px 8px;
    background: rgba(239, 68, 68, 0.07);
    border-radius: 8px;
    border: 1px solid rgba(239, 68, 68, 0.15);
    word-break: break-word;
    line-height: 1.4;
}

.tm-error-msg .material-symbols-outlined {
    font-size: 15px;
    flex-shrink: 0;
    font-variation-settings: 'FILL' 1;
}

/* ── Card Prompt Text ──────────────────────────── */
.tm-card-prompt {
    font-size: 0.79rem;
    color: var(--text-primary, #333);
    line-height: 1.45;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.tm-card-prompt--expanded {
    display: block;
    -webkit-line-clamp: unset;
    white-space: pre-wrap;
    word-break: break-word;
}

.tm-prompt-wrap {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.tm-prompt-toggle {
    border: 1px solid rgba(108, 99, 255, 0.25);
    background: rgba(108, 99, 255, 0.08);
    color: #5b52e0;
    font-size: 0.72rem;
    font-weight: 600;
    border-radius: 8px;
    padding: 4px 8px;
    width: fit-content;
    cursor: pointer;
}

/* ── Card Meta ─────────────────────────────────── */
.tm-card-meta {
    font-size: 0.72rem;
    line-height: 1.45;
    color: var(--text-secondary, #666);
    background: rgba(0, 0, 0, 0.03);
    border-radius: 8px;
    padding: 6px 8px;
    /* Handle long IDs and hashes properly */
    overflow-wrap: anywhere;
    word-break: break-word;
    hyphens: auto;
    min-width: 0;
}

.tm-card-meta > div {
    /* Each meta row should also break properly */
    overflow-wrap: anywhere;
    word-break: break-word;
}

.tm-card-meta strong {
    color: var(--text-primary, #333);
    font-weight: 600;
}

/* ── Card Footer ───────────────────────────────── */
.tm-card-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
}

.tm-card-time {
    font-size: 0.7rem;
    color: var(--text-muted, #999);
}

.tm-card-actions {
    display: flex;
    align-items: center;
    gap: 6px;
}

.tm-action-btn {
    background: none;
    border: none;
    cursor: pointer;
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary, #666);
    text-decoration: none;
    transition: background 0.15s, color 0.15s, transform 0.15s;
    outline: none;
    -webkit-tap-highlight-color: transparent;
}

.tm-action-btn:hover {
    background: rgba(108, 99, 255, 0.1);
    color: #6C63FF;
    transform: scale(1.1);
}

.tm-action-btn:active { transform: scale(0.9); }

.tm-action-btn--primary {
    color: #6C63FF;
}

.tm-action-btn--primary:hover {
    background: rgba(108, 99, 255, 0.12);
    color: #5b52e0;
}

.tm-action-btn--danger:hover {
    background: rgba(239, 68, 68, 0.1);
    color: #DC2626;
}

.tm-action-btn--warn {
    color: #B45309;
}

.tm-action-btn--warn:hover {
    background: rgba(245, 158, 11, 0.14);
    color: #92400E;
}

.tm-cancel-btn {
    border: 1px solid rgba(239, 68, 68, 0.35);
    background: rgba(239, 68, 68, 0.08);
    color: #B91C1C;
    font-size: 0.72rem;
    font-weight: 700;
    border-radius: 8px;
    padding: 4px 8px;
    cursor: pointer;
    white-space: nowrap;
}

.tm-cancel-btn:hover {
    background: rgba(239, 68, 68, 0.15);
}

.tm-action-btn .material-symbols-outlined {
    font-size: 16px;
    font-variation-settings: 'FILL' 1;
}

.tm-action-text-btn {
    width: auto;
    padding: 0 10px;
    gap: 4px;
    font-size: 0.75rem;
    font-weight: 600;
    background: rgba(0, 0, 0, 0.03);
    border: 1px solid rgba(0, 0, 0, 0.05);
}

.tm-action-text-btn:hover {
    background: rgba(0, 0, 0, 0.06);
}

.tm-action-text-btn.tm-action-btn--primary {
    background: rgba(108, 99, 255, 0.08);
    border-color: rgba(108, 99, 255, 0.15);
}

.tm-action-text-btn.tm-action-btn--primary:hover {
    background: rgba(108, 99, 255, 0.15);
}

[data-theme="dark"] .tm-action-text-btn {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.1);
}

[data-theme="dark"] .tm-action-text-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

[data-theme="dark"] .tm-action-text-btn.tm-action-btn--primary {
    background: rgba(108, 99, 255, 0.15);
    border-color: rgba(108, 99, 255, 0.25);
}

[data-theme="dark"] .tm-action-text-btn.tm-action-btn--primary:hover {
    background: rgba(108, 99, 255, 0.25);
}

/* ── Responsive: Tablet / Small Desktop ────────── */
@media (max-width: 1024px) {
    .tm-sidebar {
        width: min(340px, calc(100vw - 40px));
        right: 16px;
        top: calc(var(--header-height, 64px) + 16px);
        height: calc(100dvh - var(--header-height, 64px) - 32px);
    }
}

/* ── Mobile: Bottom Sheet Behavior ─────────────── */
@media (max-width: 640px) {
    .tm-sidebar {
        top: auto;
        bottom: 0;
        right: 0;
        left: 0;
        width: 100%;
        max-width: 100%;
        /* dvh with vh fallback for broader compatibility */
        height: 85vh;
        height: 85dvh;
        max-height: 85vh;
        max-height: 85dvh;
        border-radius: var(--radius-clay, 24px) var(--radius-clay, 24px) 0 0;
        transform: translateY(120%);
    }

    .tm-sidebar.tm-open {
        transform: translateY(0);
    }

    .tm-sidebar.tm-open + .tm-fab {
        opacity: 0;
        pointer-events: none;
        transform: translateY(20px);
    }

    .tm-list {
        padding: 10px 10px calc(110px + env(safe-area-inset-bottom));
    }

    .tm-fab {
        right: 16px;
        bottom: max(80px, calc(80px + env(safe-area-inset-bottom)));
        padding: 0;
        width: 48px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .tm-fab-text {
        display: none;
    }
    .tm-fab-content {
        gap: 0;
        justify-content: center;
    }

    /* Mobile: Reduce card padding slightly */
    .tm-card {
        padding: 10px;
        gap: 8px;
    }

    .tm-sidebar-header {
        padding: 16px 16px 12px;
    }

    /* RWD Apple Devices: Hide text labels on small screens to prevent overflow */
    @media (max-width: 400px) {
        .tm-action-text-btn .tm-btn-label {
            display: none;
        }
        .tm-action-text-btn {
            width: 32px;
            padding: 0;
        }
        .tm-card-actions {
            gap: 4px;
        }
    }
}

/* ── Additional Scroll & Overflow Safety ───────── */
.tm-list {
    /* Prevent any horizontal scroll within list */
    overflow-x: hidden;
    /* Ensure vertical scroll is always available when content exceeds */
    overflow-y: auto;
}

/* Card content safety - prevent flex children from overflowing */
.tm-card-header,
.tm-card-footer,
.tm-prompt-wrap,
.tm-progress-wrap {
    min-width: 0;
    width: 100%;
}

/* Ensure preview area respects card bounds */
.tm-preview {
    max-width: 100%;
    min-width: 0;
}

/* Custom Confirm Modal for Sidebar */
.tm-confirm-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(2px);
    border-radius: 12px;
}
.tm-confirm-box {
    background: var(--bg-surface, #ffffff);
    padding: 20px;
    border-radius: 12px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.2);
    max-width: 90%;
    width: 320px;
    text-align: center;
    border: 1px solid var(--border-color, #e5e7eb);
}
[data-theme="dark"] .tm-confirm-box {
    background: #1e293b;
    border-color: #334155;
    color: #f1f5f9;
}
.tm-confirm-msg {
    font-size: 14px;
    margin-bottom: 20px;
    line-height: 1.5;
    color: var(--text-primary, #1f2937);
}
[data-theme="dark"] .tm-confirm-msg {
    color: #f8fafc;
}
.tm-confirm-actions {
    display: flex;
    justify-content: center;
    gap: 12px;
}
.tm-confirm-btn {
    padding: 8px 16px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    border: none;
}
.tm-confirm-btn--cancel {
    background: var(--bg-secondary, #f1f5f9);
    color: var(--text-secondary, #64748b);
}
.tm-confirm-btn--cancel:hover {
    background: #e2e8f0;
}
[data-theme="dark"] .tm-confirm-btn--cancel {
    background: #334155;
    color: #cbd5e1;
}
[data-theme="dark"] .tm-confirm-btn--cancel:hover {
    background: #475569;
}
.tm-confirm-btn--danger {
    background: #ef4444;
    color: white;
}
.tm-confirm-btn--danger:hover {
    background: #dc2626;
}
