/* Toast Notification System - Tiny Story Mill (tsm) Namespace */
.tsm-toast-container {
    position: fixed;
    top: var(--space-6);
    right: var(--space-6);
    z-index: 9999;
    pointer-events: none;
    max-width: 420px;
}

/* Base toast styles */
.tsm-toast {
    background: rgba(245, 241, 234, 0.95);
    backdrop-filter: blur(20px) saturate(1.2);
    -webkit-backdrop-filter: blur(20px) saturate(1.2);
    border-radius: var(--radius-lg);
    padding: var(--space-4) var(--space-5);
    padding-right: var(--space-12); /* Extra padding for close button */
    margin-bottom: var(--space-3);
    box-shadow: 
        0 24px 48px -12px rgba(0, 0, 0, 0.12),
        0 12px 24px -6px rgba(0, 0, 0, 0.08),
        inset 0 1px 0 rgba(255, 255, 255, 0.5);
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    min-width: 300px;
    pointer-events: auto;
    animation: toastSlideIn 0.3s ease-out;
    position: relative;
    border: 1px solid rgba(232, 226, 213, 0.5);
    overflow: visible; /* Changed from hidden to prevent cutoff */
}

/* Toast variants */
.tsm-toast--error {
    border-left: 4px solid var(--color-coral);
    background: linear-gradient(135deg, 
        rgba(212, 165, 165, 0.15) 0%, 
        rgba(245, 241, 234, 0.95) 100%);
}

.tsm-toast--success {
    border-left: 4px solid var(--color-sage);
    background: linear-gradient(135deg, 
        rgba(168, 181, 160, 0.15) 0%, 
        rgba(245, 241, 234, 0.95) 100%);
}

.tsm-toast--warning {
    border-left: 4px solid var(--color-sunshine);
    background: linear-gradient(135deg, 
        rgba(212, 184, 150, 0.15) 0%, 
        rgba(245, 241, 234, 0.95) 100%);
}

.tsm-toast--info {
    border-left: 4px solid var(--color-ocean);
    background: linear-gradient(135deg, 
        rgba(165, 196, 212, 0.15) 0%, 
        rgba(245, 241, 234, 0.95) 100%);
}

/* Toast elements */
.tsm-toast__icon {
    width: 28px;
    height: 28px;
    border-radius: var(--radius-full);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    position: relative;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}

.tsm-toast__icon::before {
    content: '';
    width: 12px;
    height: 12px;
    border-radius: var(--radius-full);
    position: absolute;
}

/* Icon variants */
.tsm-toast--error .tsm-toast__icon {
    background: rgba(212, 165, 165, 0.15);
    border: 1px solid rgba(212, 165, 165, 0.3);
}

.tsm-toast--error .tsm-toast__icon::before {
    background: var(--color-rose);
    box-shadow: 0 0 0 2px rgba(212, 165, 165, 0.2);
}

.tsm-toast--success .tsm-toast__icon {
    background: rgba(168, 181, 160, 0.15);
    border: 1px solid rgba(168, 181, 160, 0.3);
}

.tsm-toast--success .tsm-toast__icon::before {
    background: var(--color-sage);
    box-shadow: 0 0 0 2px rgba(168, 181, 160, 0.2);
}

.tsm-toast--warning .tsm-toast__icon {
    background: rgba(212, 184, 150, 0.15);
    border: 1px solid rgba(212, 184, 150, 0.3);
}

.tsm-toast--warning .tsm-toast__icon::before {
    background: var(--color-ochre);
    box-shadow: 0 0 0 2px rgba(212, 184, 150, 0.2);
}

.tsm-toast--info .tsm-toast__icon {
    background: rgba(165, 196, 212, 0.15);
    border: 1px solid rgba(165, 196, 212, 0.3);
}

.tsm-toast--info .tsm-toast__icon::before {
    background: var(--color-sky);
    box-shadow: 0 0 0 2px rgba(165, 196, 212, 0.2);
}

.tsm-toast--rate-limit .tsm-toast__icon {
    background: rgba(212, 184, 150, 0.15);
    border: 1px solid rgba(212, 184, 150, 0.3);
}

.tsm-toast--rate-limit .tsm-toast__icon::before {
    background: var(--color-ochre);
    box-shadow: 0 0 0 2px rgba(212, 184, 150, 0.2);
}

.tsm-toast__content {
    flex: 1;
    color: var(--color-charcoal);
}

.tsm-toast__content h4 {
    margin: 0 0 var(--space-1) 0;
    font-size: var(--text-base);
    font-weight: 600;
}

.tsm-toast__content p {
    margin: 0;
    font-size: var(--text-sm);
    line-height: 1.5;
}

.tsm-toast__content .tsm-toast__detail {
    margin-top: var(--space-2);
    font-size: var(--text-xs);
    opacity: 0.9;
}

.tsm-toast__close {
    position: absolute;
    top: var(--space-3);
    right: var(--space-3);
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid var(--color-sage);
    font-size: var(--text-lg);
    color: var(--color-gray-warm);
    cursor: pointer;
    padding: 0;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-full);
    transition: all 0.2s ease;
    flex-shrink: 0;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    line-height: 1;
    font-family: Arial, sans-serif;
}

.tsm-toast__close:hover {
    background: white;
    color: var(--color-charcoal);
    transform: scale(1.05);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    border-color: var(--color-sage);
}

.tsm-toast__close:focus {
    outline: 2px solid var(--color-sage);
    outline-offset: 2px;
}

.tsm-toast__close:active {
    transform: scale(0.95);
}

/* Progress bar for auto-dismiss */
.tsm-toast__progress {
    position: absolute;
    bottom: 0;
    left: 0;
    height: 3px;
    background: currentColor;
    opacity: 0.3;
    animation: toastProgress var(--toast-duration, 5s) linear;
    border-radius: 0 0 0 var(--radius-lg);
    overflow: hidden;
}

/* Rate limit toast specific styles */
.tsm-toast--rate-limit {
    min-width: 360px;
    border-left: 4px solid var(--color-sunshine);
    background: linear-gradient(135deg, 
        rgba(212, 184, 150, 0.15) 0%, 
        rgba(245, 241, 234, 0.95) 100%);
}

.tsm-toast__rate-limit-content {
    width: 100%;
}

.tsm-toast__countdown-timer {
    background: rgba(0, 0, 0, 0.05);
    padding: var(--space-3);
    border-radius: var(--radius);
    margin: var(--space-3) 0;
    text-align: center;
    font-weight: 500;
}

.tsm-toast__countdown-timer.ready {
    background: rgba(168, 181, 160, 0.1);
    color: var(--color-sage);
}

.tsm-toast__countdown-display {
    font-family: monospace;
    font-size: var(--text-lg);
    font-weight: 600;
}

.tsm-toast__rate-limit-help {
    margin-top: var(--space-3);
    font-size: var(--text-xs);
}

.tsm-toast__rate-limit-help ul {
    margin: var(--space-2) 0 0 var(--space-4);
    padding: 0;
}

.tsm-toast__rate-limit-help li {
    margin-bottom: var(--space-1);
}

/* Action buttons in toasts */
.tsm-toast__actions {
    display: flex;
    gap: var(--space-2);
    margin-top: var(--space-3);
}

.tsm-toast__actions .btn {
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-sm);
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid var(--color-sage);
    color: var(--color-charcoal);
    font-weight: 500;
    transition: all 0.2s ease;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.tsm-toast__actions .btn:hover {
    background: white;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    border-color: var(--color-sage);
}

.tsm-toast__actions .btn:focus {
    outline: 2px solid var(--color-sage);
    outline-offset: 2px;
}

.tsm-toast__actions .btn:active {
    transform: translateY(0);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.tsm-toast__actions .btn-primary {
    background: rgba(168, 181, 160, 0.85);
    color: white;
    border-color: var(--color-sage);
    font-weight: 600;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.tsm-toast__actions .btn-primary:hover {
    background: var(--color-sage);
    border-color: var(--color-sage);
    color: white;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.tsm-toast__actions .btn-primary:focus {
    outline: 2px solid var(--color-sage);
    outline-offset: 2px;
}

.tsm-toast__actions .btn-primary:active {
    transform: translateY(0);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

/* Retry button for network errors */
.tsm-toast__retry-btn {
    background: var(--color-sage);
    color: white;
    border: none;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius);
    font-size: var(--text-sm);
    cursor: pointer;
    transition: all 0.2s ease;
}

.tsm-toast__retry-btn:hover {
    background: #98a890;
    transform: translateY(-1px);
}

/* Animations */
@keyframes toastSlideIn {
    from {
        transform: translateX(110%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

@keyframes toastSlideOut {
    from {
        transform: translateX(0);
        opacity: 1;
    }
    to {
        transform: translateX(110%);
        opacity: 0;
    }
}

@keyframes toastProgress {
    from {
        width: 100%;
    }
    to {
        width: 0;
    }
}

/* Stacking behavior */
.tsm-toast:not(:last-child) {
    margin-bottom: var(--space-3);
}

/* Mobile adjustments */
@media (max-width: 480px) {
    .tsm-toast-container {
        top: var(--space-4);
        left: var(--space-4);
        right: var(--space-4);
        max-width: none;
    }
    
    .tsm-toast {
        min-width: auto;
        width: 100%;
    }
    
    .tsm-toast--rate-limit {
        min-width: auto;
    }
}

/* Toast action buttons styling */
.tsm-toast__actions .btn-sm {
    padding: var(--space-1) var(--space-3);
    font-size: var(--text-xs);
    min-height: 32px;
    border-radius: var(--radius-md);
}

.tsm-toast__actions .btn-icon {
    font-size: var(--text-sm);
    margin-right: var(--space-1);
}

/* Confetti animation for celebrations */
.tsm-toast__confetti {
    position: absolute;
    width: 10px;
    height: 10px;
    background: var(--confetti-color, #FFD700);
    top: -10px;
    animation: confettiFall var(--duration, 3s) ease-out forwards;
    z-index: 10000;
}

@keyframes confettiFall {
    0% {
        transform: translateY(0) rotate(0deg);
        opacity: 1;
    }
    100% {
        transform: translateY(100vh) rotate(720deg);
        opacity: 0;
    }
}

/* Screen reader only content */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

/* Icon animation on toast appearance */
.tsm-toast__icon {
    animation: iconAppear 0.4s ease-out 0.2s both;
}

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

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    .tsm-toast {
        animation: none;
    }
    
    .tsm-toast__progress {
        animation: none;
        display: none;
    }
    
    .tsm-toast__confetti {
        animation: none;
        display: none;
    }
}

/* Dark mode support - maintain transparency and glassmorphism */
@media (prefers-color-scheme: dark) {
    .tsm-toast {
        /* Use the same stone-light background for consistency */
        background: rgba(245, 241, 234, 0.95);
        border-color: rgba(232, 226, 213, 0.5);
        /* Enhanced shadows for dark mode */
        box-shadow: 
            0 24px 48px -12px rgba(0, 0, 0, 0.18),
            0 12px 24px -6px rgba(0, 0, 0, 0.12),
            inset 0 1px 0 rgba(255, 255, 255, 0.5);
    }
    
    .tsm-toast__content {
        /* Keep the original charcoal color for good contrast on light background */
        color: var(--color-charcoal);
    }
    
    .tsm-toast__close {
        color: var(--color-gray-warm);
        background: rgba(250, 247, 242, 0.6);
    }
    
    .tsm-toast__close:hover {
        background: rgba(250, 247, 242, 0.9);
        color: var(--color-charcoal);
    }
}