:root {
    --primary-color: #7000ff;
    --secondary-color: #00c6ff;
    --accent-color: #ff007f;
    --text-primary: #ffffff;
    --text-secondary: rgba(255, 255, 255, 0.8);
    --glass-bg: rgba(255, 255, 255, 0.05);
    --glass-border: rgba(255, 255, 255, 0.1);
    --glass-highlight: rgba(255, 255, 255, 0.3);
    --font-main: 'Outfit', sans-serif;
}

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

body {
    font-family: var(--font-main);
    background-color: #05050a;
    color: var(--text-primary);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    line-height: 1.6;
    overflow: hidden;
    position: relative;
    perspective: 1000px;
}

#canvas3d {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    pointer-events: none;
}

/* --- Container Motion Ready --- */
.main-ui {
    /* Set starting position for GSAP but keep visible for safety */
    transform-style: preserve-3d;
}

/* --- Glassmorphism Container --- */
.container {
    background: rgba(255, 255, 255, 0.04);
    backdrop-filter: blur(25px) saturate(200%);
    -webkit-backdrop-filter: blur(25px) saturate(200%);
    border: 1px solid var(--glass-border);
    border-top: 1px solid var(--glass-highlight);
    border-left: 1px solid var(--glass-highlight);
    box-shadow:
        0 15px 35px 0 rgba(0, 0, 0, 0.4),
        inset 0 0 0 1px rgba(255, 255, 255, 0.05);
    border-radius: 28px;
    padding: 0;
    width: 95%;
    /* Use percentage to be responsive */
    max-width: 600px;
    /* Increased from 500px to fit contents better */
    position: relative;
    z-index: 10;
    overflow: visible;
    transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.container:hover {
    box-shadow: 0 20px 50px 0 rgba(0, 0, 0, 0.6);
}

/* --- Header --- */
.header {
    padding: 40px 40px 25px;
    text-align: center;
    border-bottom: 1px solid var(--glass-border);
    background: rgba(255, 255, 255, 0.02);
    border-radius: 28px 28px 0 0;
}

.header h1 {
    font-size: 1.85rem;
    font-weight: 700;
    margin-bottom: 10px;
    background: linear-gradient(135deg, #fff 0%, #c0c0c0 100%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
    letter-spacing: -0.5px;
}

.emoji {
    font-size: 3rem;
    filter: drop-shadow(0 0 10px rgba(255, 255, 255, 0.3));
    animation: bounce 2s infinite ease-in-out;
}

@keyframes bounce {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-10px);
    }
}

/* --- Main Content --- */
.main-content {
    padding: 30px 40px 40px;
}

/* Joke Display Area */
.joke-container {
    margin-bottom: 30px;
}

.joke-display {
    background: rgba(0, 0, 0, 0.25);
    border-radius: 20px;
    padding: 30px;
    min-height: 160px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    position: relative;
    border: 1px solid rgba(255, 255, 255, 0.05);
    box-shadow: inset 0 2px 15px rgba(0, 0, 0, 0.3);
    margin-bottom: 25px;
    transition: all 0.3s ease;
}

.joke-tools {
    position: absolute;
    top: 10px;
    right: 15px;
    display: flex;
    gap: 8px;
    opacity: 0;
    /* Hidden by default until hover or joke loaded */
    transition: opacity 0.3s ease;
}

.joke-display:hover .joke-tools,
.joke-display.has-joke .joke-tools {
    opacity: 1;
}

.tool-btn {
    background: rgba(255, 255, 255, 0.1);
    border: none;
    color: var(--text-secondary);
    width: 32px;
    height: 32px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    font-size: 1rem;
}

.tool-btn:hover {
    background: rgba(255, 255, 255, 0.25);
    color: white;
    transform: scale(1.1);
}

.active-tool {
    color: var(--secondary-color);
    background: rgba(0, 198, 255, 0.15);
    animation: toolPulse 1s infinite;
}

@keyframes toolPulse {
    50% {
        transform: scale(1.2);
    }
}

#joke-text {
    font-size: 1.3rem;
    font-weight: 500;
    margin-bottom: 15px;
    color: var(--text-primary);
    line-height: 1.6;
}

#punchline {
    font-size: 1.35rem;
    font-weight: 700;
    color: var(--secondary-color);
    text-shadow: 0 0 15px rgba(0, 198, 255, 0.5);
}

/* Explanation Panel */
.explanation {
    background: rgba(40, 40, 60, 0.6);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    border: 1px solid var(--glass-border);
    border-radius: 16px;
    margin-top: 20px;
    overflow: hidden;
    animation: slideDown 0.4s ease-out;
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.explanation-header {
    background: rgba(255, 255, 255, 0.05);
    padding: 12px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--glass-border);
}

.explanation-title {
    font-weight: 600;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.close-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 1.5rem;
    cursor: pointer;
    line-height: 1;
    transition: color 0.2s;
}

.close-btn:hover {
    color: var(--accent-color);
}

.explanation-content {
    padding: 20px;
    font-size: 0.95rem;
    color: var(--text-secondary);
    line-height: 1.6;
    max-height: 350px;
    overflow-y: auto;
}

/* Custom Scrollbar for Explanation Content */
.explanation-content::-webkit-scrollbar {
    width: 6px;
}

.explanation-content::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05);
    border-radius: 10px;
}

.explanation-content::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 10px;
}

.explanation-content::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.3);
}

.explanation-content strong {
    color: var(--text-primary);
}

/* --- Buttons --- */
.button-group {
    display: flex;
    flex-direction: column;
    gap: 18px;
    margin-bottom: 30px;
}

.joke-btn {
    font-family: var(--font-main);
    cursor: pointer;
    border: 1px solid rgba(255, 255, 255, 0.15);
    color: white;
    transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    position: relative;
    overflow: hidden;
}

.primary-btn {
    background: linear-gradient(135deg, var(--primary-color) 0%, #8e2de2 100%);
    padding: 18px 30px;
    border-radius: 18px;
    font-size: 1.1rem;
    font-weight: 700;
    box-shadow: 0 10px 25px rgba(112, 0, 255, 0.3);
    border: none;
    animation: pulseGlow 3s infinite;
}

@keyframes pulseGlow {

    0%,
    100% {
        box-shadow: 0 10px 25px rgba(112, 0, 255, 0.3);
    }

    50% {
        box-shadow: 0 15px 35px rgba(132, 45, 226, 0.6);
    }
}

.primary-btn:hover {
    transform: scale(1.02) translateY(-3px);
    box-shadow: 0 15px 35px rgba(112, 0, 255, 0.5);
    filter: brightness(1.1);
}

.secondary-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.secondary-btn.full-width {
    grid-column: span 2;
}

.secondary-btn {
    background: rgba(255, 255, 255, 0.05);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    padding: 14px 20px;
    border-radius: 14px;
    font-size: 0.95rem;
    font-weight: 600;
    width: 100%;
    /* Ensure full width in grid cell */
    justify-content: center;
    /* Center content explicitly */
}

.secondary-btn:hover {
    background: rgba(255, 255, 255, 0.15);
    transform: translateY(-2px);
    border-color: rgba(255, 255, 255, 0.4);
}

.joke-btn:active {
    transform: scale(0.96) translateY(0);
}

/* --- Settings Group --- */
.settings-group {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-top: 10px;
    padding: 20px 0 10px;
    border-top: 1px solid var(--glass-border);
}

.model-selector,
.bg-selector {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 15px;
    flex-wrap: wrap;
    /* Allow wrapping on very small screens */
}

.model-selector label,
.bg-selector label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1px;
    flex-shrink: 0;
    /* Prevent label from squishing */
}

.model-dropdown {
    background: rgba(0, 0, 0, 0.4);
    color: white;
    border: 1px solid var(--glass-border);
    padding: 8px 14px;
    border-radius: 10px;
    font-family: var(--font-main);
    font-size: 0.85rem;
    cursor: pointer;
    min-width: 150px;
    /* Reduced slightly */
    flex: 1;
    /* Allow it to take available space */
    transition: all 0.3s;
}

.model-dropdown:hover,
.model-dropdown:focus {
    border-color: var(--secondary-color);
    background: rgba(0, 0, 0, 0.6);
}

/* --- Loader --- */
.loader {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    border-top-color: var(--secondary-color);
    animation: spin 1s ease-in-out infinite;
    margin: 20px auto 0;
    display: none;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* --- Touch Optimization --- */
@media (hover: none) and (pointer: coarse) {
    .joke-btn:hover {
        transform: none;
        filter: none;
    }
    
    .primary-btn:hover {
        transform: none;
        box-shadow: 0 10px 25px rgba(112, 0, 255, 0.3);
    }
    
    .secondary-btn:hover {
        transform: none;
        background: rgba(255, 255, 255, 0.05);
    }
    
    .joke-btn:active {
        transform: scale(0.95);
        opacity: 0.8;
    }
    
    .model-dropdown {
        font-size: 16px; /* Prevent zoom on iOS */
    }
}

/* --- Responsive --- */
@media (max-width: 600px) {
    body {
        padding: 10px;
        align-items: center;
        justify-content: center;
        min-height: 100vh;
        min-height: 100dvh; /* Dynamic viewport height for mobile */
        padding-top: max(env(safe-area-inset-top), 10px);
        padding-bottom: max(env(safe-area-inset-bottom), 10px);
        overflow-y: auto;
    }

    .container {
        margin: auto 0;
        max-width: 100%;
        border-radius: 20px;
    }

    .header {
        padding: 20px 15px 15px;
    }

    .header h1 {
        font-size: 1.35rem;
        letter-spacing: -0.3px;
    }
    
    .emoji {
        font-size: 2.5rem;
    }

    .main-content {
        padding: 20px 15px;
    }
    
    /* Joke Display - Better for mobile reading */
    .joke-display {
        padding: 20px;
        min-height: 120px;
        margin-bottom: 20px;
        border-radius: 16px;
    }
    
    #joke-text {
        font-size: 1.15rem;
        line-height: 1.7;
        margin-bottom: 12px;
    }
    
    #punchline {
        font-size: 1.2rem;
    }

    /* Buttons - Larger touch targets */
    .button-group {
        gap: 12px;
        margin-bottom: 20px;
    }
    
    .primary-btn {
        padding: 16px 20px;
        font-size: 1.05rem;
        border-radius: 16px;
        min-height: 56px; /* Minimum 56px touch target */
    }

    .secondary-buttons {
        flex-direction: column;
        gap: 10px;
    }
    
    .secondary-btn {
        padding: 14px 16px;
        font-size: 0.95rem;
        min-height: 52px;
        border-radius: 12px;
    }
    
    /* Settings - Stack vertically on small screens */
    .settings-group {
        padding: 15px 0 5px;
        gap: 10px;
    }
    
    .model-selector,
    .bg-selector {
        flex-direction: column;
        align-items: flex-start;
        gap: 6px;
    }
    
    .model-selector label,
    .bg-selector label {
        font-size: 0.7rem;
    }
    
    .model-dropdown {
        width: 100%;
        min-width: unset;
        padding: 10px 12px;
        font-size: 16px; /* Prevent iOS zoom */
    }
    
    /* Explanation Panel - Better for mobile */
    .explanation {
        margin-top: 15px;
        border-radius: 12px;
    }
    
    .explanation-header {
        padding: 10px 15px;
    }
    
    .explanation-title {
        font-size: 0.8rem;
    }
    
    .explanation-content {
        padding: 15px;
        font-size: 0.9rem;
        max-height: 40vh; /* Use viewport height instead of fixed px */
        -webkit-overflow-scrolling: touch; /* Smooth scroll on iOS */
    }
    
    .close-btn {
        padding: 8px;
        min-width: 44px;
        min-height: 44px;
    }
    
    /* Reduce animation intensity on mobile */
    @keyframes pulseGlow {
        0%, 100% {
            box-shadow: 0 8px 20px rgba(112, 0, 255, 0.3);
        }
        50% {
            box-shadow: 0 12px 25px rgba(132, 45, 226, 0.5);
        }
    }
    
    .emoji {
        animation-duration: 2.5s; /* Slower animation for battery saving */
    }
}

/* Extra small screens */
@media (max-width: 360px) {
    body {
        padding: 8px;
    }
    
    .header h1 {
        font-size: 1.2rem;
    }
    
    .emoji {
        font-size: 2rem;
    }
    
    .main-content {
        padding: 15px 12px;
    }
    
    .joke-display {
        padding: 15px;
    }
    
    #joke-text {
        font-size: 1.05rem;
    }
    
    #punchline {
        font-size: 1.1rem;
    }
    
    .primary-btn {
        font-size: 0.95rem;
        padding: 14px 16px;
    }
    
    .secondary-btn {
        font-size: 0.9rem;
        padding: 12px 14px;
    }
}

/* Landscape mode on small screens */
@media (max-height: 500px) and (orientation: landscape) {
    body {
        padding: 10px;
        align-items: center;
        justify-content: center;
    }
    
    .header {
        padding: 15px 20px 10px;
    }
    
    .header h1 {
        font-size: 1.25rem;
        margin-bottom: 5px;
    }
    
    .emoji {
        font-size: 2rem;
        animation: none; /* Disable animation to save battery */
    }
    
    .main-content {
        padding: 15px 20px;
    }
    
    .joke-display {
        min-height: 80px;
        padding: 15px;
        margin-bottom: 15px;
    }
    
    .button-group {
        flex-direction: row;
        flex-wrap: wrap;
        gap: 10px;
        margin-bottom: 15px;
    }
    
    .primary-btn {
        flex: 1 1 100%;
        padding: 12px 20px;
        min-height: 48px;
    }
    
    .secondary-buttons {
        flex-direction: row;
        flex: 1 1 100%;
    }
    
    .settings-group {
        flex-direction: row;
        padding: 10px 0 5px;
    }
    
    .model-selector,
    .bg-selector {
        flex: 1;
    }
    
    .explanation-content {
        max-height: 35vh;
    }
}

/* --- Saved Jokes Modal --- */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(5px);
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s ease;
}

.modal.active {
    opacity: 1;
    visibility: visible;
}

.modal-content {
    background: #1a1a2e;
    background: linear-gradient(135deg, rgba(20, 20, 35, 0.95), rgba(30, 30, 50, 0.9));
    border: 1px solid var(--glass-border);
    padding: 2rem;
    border-radius: 20px;
    width: 90%;
    max-width: 600px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
    transform: scale(0.9);
    transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.modal.active .modal-content {
    transform: scale(1);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--glass-border);
}

.modal-header h2 {
    margin: 0;
    color: var(--primary-color);
    font-size: 1.5rem;
}

.saved-list {
    flex: 1;
    overflow-y: auto;
    padding-right: 0.5rem;
}

/* Scrollbar for saved list */
.saved-list::-webkit-scrollbar {
    width: 6px;
}

.saved-list::-webkit-scrollbar-thumb {
    background: var(--glass-border);
    border-radius: 10px;
}

.saved-item {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--glass-border);
    border-radius: 12px;
    padding: 1rem;
    margin-bottom: 1rem;
    position: relative;
    transition: all 0.3s ease;
}

.saved-item:hover {
    background: rgba(255, 255, 255, 0.08);
    transform: translateX(5px);
}

.saved-item p {
    margin: 0;
    color: var(--text-primary);
    font-size: 1rem;
    line-height: 1.5;
}

.saved-item .saved-setup {
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: var(--accent-color);
}

.delete-joke-btn {
    position: absolute;
    top: 10px;
    right: 10px;
    background: transparent;
    border: none;
    color: #ff4757;
    cursor: pointer;
    font-size: 1.2rem;
    opacity: 0.7;
    transition: opacity 0.2s;
}

.delete-joke-btn:hover {
    opacity: 1;
    transform: scale(1.1);
}

.empty-state {
    text-align: center;
    color: rgba(255, 255, 255, 0.5);
    margin-top: 2rem;
    font-style: italic;
}