/**
 * Tone Theme System - CSS Custom Properties for Different Tones
 * Provides visual styling variations for each tone personality
 * Includes fallbacks for browsers without CSS custom property support
 */

/* Default/Mild Theme Variables */
:root {
    --tone-primary: #4338ca;
    --tone-primary-hover: #3730a3;
    --tone-accent: #0891b2;
    --tone-accent-hover: #0e7490;
    --tone-bg: #f9fafb;
    --tone-bg-secondary: #f3f4f6;
    --tone-text: #1f2937;
    --tone-text-secondary: #6b7280;
    --tone-border: #d1d5db;
    --tone-shadow: rgba(0, 0, 0, 0.1);
}

/* Fallback styles for browsers without CSS custom property support */
/* These will be overridden by custom properties in supporting browsers */
.tone-button,
.quiz-button,
.result-button,
.nav-button,
.btn,
.option-btn,
.quiz-start-btn {
    background-color: #4338ca; /* Fallback for --tone-primary */
    border-color: #4338ca;
}

.tone-button:hover,
.quiz-button:hover,
.result-button:hover,
.nav-button:hover,
.btn:hover,
.option-btn:hover,
.quiz-start-btn:hover {
    background-color: #3730a3; /* Fallback for --tone-primary-hover */
    border-color: #3730a3;
}

.accent-element,
.score-bar,
.progress-indicator {
    background-color: #0891b2; /* Fallback for --tone-accent */
}

.tone-bg {
    background-color: #f9fafb; /* Fallback for --tone-bg */
}

.tone-bg-secondary {
    background-color: #f3f4f6; /* Fallback for --tone-bg-secondary */
}

.tone-text {
    color: #1f2937; /* Fallback for --tone-text */
}

.tone-text-secondary {
    color: #6b7280; /* Fallback for --tone-text-secondary */
}

.tone-border {
    border-color: #d1d5db; /* Fallback for --tone-border */
}

/* Spicy Theme (Red) */
[data-tone="spicy"] {
    --tone-primary: #dc2626;
    --tone-primary-hover: #b91c1c;
    --tone-accent: #ea580c;
    --tone-accent-hover: #c2410c;
    --tone-bg: #fef2f2;
    --tone-bg-secondary: #fee2e2;
    --tone-text: #7f1d1d;
    --tone-text-secondary: #991b1b;
    --tone-border: #fca5a5;
    --tone-shadow: rgba(220, 38, 38, 0.15);
}

/* Twisted Theme (Purple) */
[data-tone="twisted"] {
    --tone-primary: #7c3aed;
    --tone-primary-hover: #6d28d9;
    --tone-accent: #a855f7;
    --tone-accent-hover: #9333ea;
    --tone-bg: #faf5ff;
    --tone-bg-secondary: #f3e8ff;
    --tone-text: #581c87;
    --tone-text-secondary: #6b21a8;
    --tone-border: #c4b5fd;
    --tone-shadow: rgba(124, 58, 237, 0.15);
}

/* Kyoto Theme (Dark Blue) */
[data-tone="kyoto"] {
    --tone-primary: #1e40af;
    --tone-primary-hover: #1d4ed8;
    --tone-accent: #2563eb;
    --tone-accent-hover: #1d4ed8;
    --tone-bg: #eff6ff;
    --tone-bg-secondary: #dbeafe;
    --tone-text: #1e3a8a;
    --tone-text-secondary: #1e40af;
    --tone-border: #93c5fd;
    --tone-shadow: rgba(30, 64, 175, 0.15);
}

/* Apply tone colors to common elements with CSS custom properties */
/* Fallback values are already defined above */
.tone-button,
.quiz-button,
.result-button,
.nav-button,
.btn,
.option-btn,
.quiz-start-btn {
    background-color: var(--tone-primary, #4338ca);
    color: white;
    border: 2px solid var(--tone-primary, #4338ca);
    transition: all 0.3s ease;
}

.tone-button:hover,
.quiz-button:hover,
.result-button:hover,
.nav-button:hover,
.btn:hover,
.option-btn:hover,
.quiz-start-btn:hover {
    background-color: var(--tone-primary-hover, #3730a3);
    border-color: var(--tone-primary-hover, #3730a3);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px var(--tone-shadow, rgba(0, 0, 0, 0.1));
}

/* Accent elements */
.accent-element,
.score-bar,
.progress-indicator {
    background-color: var(--tone-accent, #0891b2);
    color: white;
}

.accent-element:hover,
.score-bar:hover {
    background-color: var(--tone-accent-hover, #0e7490);
}

/* Background elements */
.tone-bg {
    background-color: var(--tone-bg, #f9fafb);
}

.tone-bg-secondary {
    background-color: var(--tone-bg-secondary, #f3f4f6);
}

/* Text elements */
.tone-text {
    color: var(--tone-text, #1f2937);
}

.tone-text-secondary {
    color: var(--tone-text-secondary, #6b7280);
}

/* Border elements */
.tone-border {
    border-color: var(--tone-border, #d1d5db);
}

/* Smooth transitions for theme changes */
* {
    transition: background-color 0.6s ease, 
                color 0.6s ease, 
                border-color 0.6s ease,
                box-shadow 0.6s ease;
}

/* Slower transitions specifically for mobile to prevent dizziness */
@media (max-width: 768px) {
    * {
        transition: background-color 0.8s ease, 
                    color 0.8s ease, 
                    border-color 0.8s ease,
                    box-shadow 0.8s ease,
                    transform 0.6s ease !important;
    }
}

/* Tone Selector Container */
.tone-selector-container {
    text-align: center;
}

/* Tone-specific button styling */
.tone-selector-button {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 12px 20px;
    border-radius: 12px;
    font-weight: 600;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.3s ease;
    border: 2px solid transparent;
    position: relative;
    overflow: hidden;
    backdrop-filter: blur(10px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    min-width: 110px;
    justify-content: center;
}

.tone-selector-button:focus {
    outline: none;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.3);
}

.tone-selector-button.active {
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.4), 0 4px 16px rgba(0, 0, 0, 0.15);
    transform: scale(1.05);
    border-color: rgba(255, 255, 255, 0.3);
}

.tone-selector-button .tone-emoji {
    font-size: 1.2rem;
    display: inline-block;
}

.tone-selector-button .tone-name {
    font-weight: 600;
    letter-spacing: -0.025em;
}

/* Individual tone button styles */
.tone-selector-button.tone-mild {
    background: linear-gradient(135deg, #4338ca 0%, #6366f1 100%);
    color: white;
}

.tone-selector-button.tone-mild:hover {
    background: linear-gradient(135deg, #3730a3 0%, #4f46e5 100%);
}

.tone-selector-button.tone-spicy {
    background: linear-gradient(135deg, #dc2626 0%, #ef4444 100%);
    color: white;
}

.tone-selector-button.tone-spicy:hover {
    background: linear-gradient(135deg, #b91c1c 0%, #dc2626 100%);
}

.tone-selector-button.tone-twisted {
    background: linear-gradient(135deg, #7c3aed 0%, #a855f7 100%);
    color: white;
}

.tone-selector-button.tone-twisted:hover {
    background: linear-gradient(135deg, #6d28d9 0%, #9333ea 100%);
}

.tone-selector-button.tone-kyoto {
    background: linear-gradient(135deg, #1e40af 0%, #3b82f6 100%);
    color: white;
}

.tone-selector-button.tone-kyoto:hover {
    background: linear-gradient(135deg, #1d4ed8 0%, #2563eb 100%);
}

/* Hover effects for tone buttons */
.tone-selector-button:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

.tone-selector-button:active {
    transform: translateY(-1px);
    transition: all 0.1s ease;
}

/* Active state enhancements */
.tone-selector-button.active.tone-mild {
    box-shadow: 0 0 0 3px rgba(67, 56, 202, 0.4), 0 4px 16px rgba(67, 56, 202, 0.3);
}

.tone-selector-button.active.tone-spicy {
    box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.4), 0 4px 16px rgba(220, 38, 38, 0.3);
}

.tone-selector-button.active.tone-twisted {
    box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.4), 0 4px 16px rgba(124, 58, 237, 0.3);
}

.tone-selector-button.active.tone-kyoto {
    box-shadow: 0 0 0 3px rgba(30, 64, 175, 0.4), 0 4px 16px rgba(30, 64, 175, 0.3);
}

/* Mobile responsiveness */
@media (max-width: 768px) {
    .tone-selector-button {
        padding: 10px 16px;
        font-size: 0.85rem;
        min-width: 100px;
        gap: 6px;
        transition: all 0.6s ease !important; /* Slower transition for mobile */
    }
    
    .tone-selector-button .tone-emoji {
        font-size: 1.1rem;
    }
    
    .tone-selector-button.active {
        transform: scale(1.03);
        transition: all 0.6s ease !important; /* Slower transition for mobile */
    }
    
    .tone-selector-button:hover {
        transform: translateY(-2px);
        transition: all 0.6s ease !important; /* Slower transition for mobile */
    }
}

@media (max-width: 640px) {
    .tone-selector-container .flex {
        gap: 8px;
    }
    
    .tone-selector-button {
        padding: 8px 12px;
        font-size: 0.8rem;
        min-width: 85px;
        gap: 4px;
    }
    
    .tone-selector-button .tone-emoji {
        font-size: 1rem;
    }
    
    .tone-selector-button.active {
        transform: scale(1.02);
    }
    
    .tone-selector-button:hover {
        transform: translateY(-1px);
    }
}

@media (max-width: 480px) {
    .tone-selector-container .flex {
        flex-direction: column;
        align-items: center;
        gap: 10px;
    }
    
    .tone-selector-button {
        width: 200px;
        max-width: 90%;
    }
}

/* Additional responsive improvements */
@media (max-width: 360px) {
    .tone-selector-button {
        padding: 6px 10px;
        font-size: 0.75rem;
        min-width: 75px;
        width: 180px;
    }
    
    .tone-selector-button .tone-emoji {
        font-size: 0.9rem;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .tone-selector-button {
        border: 2px solid currentColor;
    }
    
    .tone-selector-button.active {
        border-width: 3px;
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    .tone-selector-button {
        transition: none;
    }
    
    .tone-selector-button:hover {
        transform: none;
    }
    
    .tone-selector-button.active {
        transform: none;
    }
}

/* Integration with existing Tailwind classes */
[data-tone] .bg-primary-500,
[data-tone] .bg-blue-600,
[data-tone] .bg-indigo-600 {
    background-color: var(--tone-primary) !important;
}

[data-tone] .hover\\:bg-primary-600:hover,
[data-tone] .hover\\:bg-blue-700:hover,
[data-tone] .hover\\:bg-indigo-700:hover {
    background-color: var(--tone-primary-hover) !important;
}

[data-tone] .text-primary-600,
[data-tone] .text-blue-600,
[data-tone] .text-indigo-600 {
    color: var(--tone-primary) !important;
}

[data-tone] .border-primary-500,
[data-tone] .border-blue-500,
[data-tone] .border-indigo-500 {
    border-color: var(--tone-primary) !important;
}

/* Theme application for quiz cards and containers */
[data-tone] .quiz-card {
    border-color: var(--tone-border);
    background-color: var(--tone-bg);
}

[data-tone] .quiz-card:hover {
    box-shadow: 0 4px 12px var(--tone-shadow);
    border-color: var(--tone-primary);
}

/* Theme application for result containers */
[data-tone] .result-container {
    background-color: var(--tone-bg);
    border-color: var(--tone-border);
}

[data-tone] .score-display {
    color: var(--tone-primary);
    border-color: var(--tone-primary);
}

/* Theme application for navigation elements */
[data-tone] .nav-link {
    color: var(--tone-text);
}

[data-tone] .nav-link:hover {
    color: var(--tone-primary);
}

/* Animation for theme transitions */
@keyframes toneTransition {
    0% {
        opacity: 0.8;
    }
    50% {
        opacity: 0.9;
    }
    100% {
        opacity: 1;
    }
}

[data-tone] {
    animation: toneTransition 0.8s ease-in-out;
}

/* Slower tone transitions for mobile to prevent dizziness */
@media (max-width: 768px) {
    @keyframes toneTransitionMobile {
        0% {
            opacity: 0.9;
        }
        50% {
            opacity: 0.95;
        }
        100% {
            opacity: 1;
        }
    }
    
    [data-tone] {
        animation: toneTransitionMobile 1.2s ease-in-out;
    }
}

/* Ensure theme changes affect body background gradients */
[data-tone="spicy"] body {
    background: linear-gradient(135deg, #fef2f2 0%, #fee2e2 50%, #fecaca 100%) !important;
}

[data-tone="twisted"] body {
    background: linear-gradient(135deg, #faf5ff 0%, #f3e8ff 50%, #e9d5ff 100%) !important;
}

[data-tone="kyoto"] body {
    background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 50%, #bfdbfe 100%) !important;
}

/* Fallback styles for browsers without CSS custom property support */
/* These styles will be applied when CSS custom properties are not available */

/* Spicy theme fallbacks */
[data-tone="spicy"] .tone-button,
[data-tone="spicy"] .quiz-button,
[data-tone="spicy"] .result-button,
[data-tone="spicy"] .nav-button,
[data-tone="spicy"] .btn,
[data-tone="spicy"] .option-btn,
[data-tone="spicy"] .quiz-start-btn {
    background-color: #dc2626 !important;
    border-color: #dc2626 !important;
}

[data-tone="spicy"] .tone-button:hover,
[data-tone="spicy"] .quiz-button:hover,
[data-tone="spicy"] .result-button:hover,
[data-tone="spicy"] .nav-button:hover,
[data-tone="spicy"] .btn:hover,
[data-tone="spicy"] .option-btn:hover,
[data-tone="spicy"] .quiz-start-btn:hover {
    background-color: #b91c1c !important;
    border-color: #b91c1c !important;
}

[data-tone="spicy"] .accent-element,
[data-tone="spicy"] .score-bar,
[data-tone="spicy"] .progress-indicator {
    background-color: #ea580c !important;
}

[data-tone="spicy"] .tone-bg {
    background-color: #fef2f2 !important;
}

[data-tone="spicy"] .tone-text {
    color: #7f1d1d !important;
}

/* Twisted theme fallbacks */
[data-tone="twisted"] .tone-button,
[data-tone="twisted"] .quiz-button,
[data-tone="twisted"] .result-button,
[data-tone="twisted"] .nav-button,
[data-tone="twisted"] .btn,
[data-tone="twisted"] .option-btn,
[data-tone="twisted"] .quiz-start-btn {
    background-color: #7c3aed !important;
    border-color: #7c3aed !important;
}

[data-tone="twisted"] .tone-button:hover,
[data-tone="twisted"] .quiz-button:hover,
[data-tone="twisted"] .result-button:hover,
[data-tone="twisted"] .nav-button:hover,
[data-tone="twisted"] .btn:hover,
[data-tone="twisted"] .option-btn:hover,
[data-tone="twisted"] .quiz-start-btn:hover {
    background-color: #6d28d9 !important;
    border-color: #6d28d9 !important;
}

[data-tone="twisted"] .accent-element,
[data-tone="twisted"] .score-bar,
[data-tone="twisted"] .progress-indicator {
    background-color: #a855f7 !important;
}

[data-tone="twisted"] .tone-bg {
    background-color: #faf5ff !important;
}

[data-tone="twisted"] .tone-text {
    color: #581c87 !important;
}

/* Kyoto theme fallbacks */
[data-tone="kyoto"] .tone-button,
[data-tone="kyoto"] .quiz-button,
[data-tone="kyoto"] .result-button,
[data-tone="kyoto"] .nav-button,
[data-tone="kyoto"] .btn,
[data-tone="kyoto"] .option-btn,
[data-tone="kyoto"] .quiz-start-btn {
    background-color: #1e40af !important;
    border-color: #1e40af !important;
}

[data-tone="kyoto"] .tone-button:hover,
[data-tone="kyoto"] .quiz-button:hover,
[data-tone="kyoto"] .result-button:hover,
[data-tone="kyoto"] .nav-button:hover,
[data-tone="kyoto"] .btn:hover,
[data-tone="kyoto"] .option-btn:hover,
[data-tone="kyoto"] .quiz-start-btn:hover {
    background-color: #1d4ed8 !important;
    border-color: #1d4ed8 !important;
}

[data-tone="kyoto"] .accent-element,
[data-tone="kyoto"] .score-bar,
[data-tone="kyoto"] .progress-indicator {
    background-color: #2563eb !important;
}

[data-tone="kyoto"] .tone-bg {
    background-color: #eff6ff !important;
}

[data-tone="kyoto"] .tone-text {
    color: #1e3a8a !important;
}

/* Error handling styles */
.tone-error-message {
    background-color: #fef2f2;
    border: 1px solid #fca5a5;
    color: #7f1d1d;
    padding: 12px;
    border-radius: 6px;
    margin: 10px 0;
    font-size: 0.875rem;
}

.tone-fallback-notice {
    background-color: #fffbeb;
    border: 1px solid #fcd34d;
    color: #92400e;
    padding: 8px 12px;
    border-radius: 4px;
    font-size: 0.75rem;
    text-align: center;
    margin: 5px 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: 0;
}

/* Focus management improvements */
.tone-selector-button:focus-visible {
    outline: 3px solid #2563eb;
    outline-offset: 2px;
    box-shadow: 0 0 0 6px rgba(37, 99, 235, 0.2);
}

/* Enhanced focus indicators for high contrast mode */
@media (prefers-contrast: high) {
    .tone-selector-button:focus-visible {
        outline: 4px solid currentColor;
        outline-offset: 3px;
    }
}

/* Keyboard navigation indicators */
.tone-selector-button[data-focused="true"] {
    position: relative;
}

.tone-selector-button[data-focused="true"]::before {
    content: '';
    position: absolute;
    top: -3px;
    left: -3px;
    right: -3px;
    bottom: -3px;
    border: 2px dashed rgba(37, 99, 235, 0.6);
    border-radius: 15px;
    pointer-events: none;
}

/* Live region styling */
#tone-selector-announcements {
    position: absolute;
    left: -10000px;
    width: 1px;
    height: 1px;
    overflow: hidden;
}

/* Keyboard shortcut indicators */
.tone-selector-button[data-shortcut]::after {
    content: attr(data-shortcut);
    position: absolute;
    top: -8px;
    right: -8px;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    font-size: 0.6rem;
    padding: 2px 4px;
    border-radius: 3px;
    opacity: 0;
    transition: opacity 0.2s ease;
    pointer-events: none;
    z-index: 10;
}

.tone-selector-button:focus[data-shortcut]::after,
.tone-selector-button:hover[data-shortcut]::after {
    opacity: 1;
}

/* Improved button states for accessibility */
.tone-selector-button[aria-pressed="true"] {
    position: relative;
}

.tone-selector-button[aria-pressed="true"]::before {
    content: '✓';
    position: absolute;
    top: 2px;
    right: 2px;
    font-size: 0.8rem;
    color: rgba(255, 255, 255, 0.9);
    font-weight: bold;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

/* Loading states for lazy loading */
.tone-selector-button.loading {
    position: relative;
    pointer-events: none;
    opacity: 0.7;
}

.tone-selector-button.loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 16px;
    height: 16px;
    margin: -8px 0 0 -8px;
    border: 2px solid transparent;
    border-top: 2px solid currentColor;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Error states */
.tone-selector-button.error {
    border-color: #dc2626 !important;
    background-color: #fef2f2 !important;
    color: #dc2626 !important;
}

.tone-selector-button.error::before {
    content: '⚠';
    position: absolute;
    top: 2px;
    right: 2px;
    font-size: 0.8rem;
    color: #dc2626;
}

/* Performance optimization: will-change for animated elements */
.tone-selector-button {
    will-change: transform, box-shadow;
}

.tone-selector-button:hover,
.tone-selector-button:focus,
.tone-selector-button.active {
    will-change: auto;
}

/* Accessibility improvements for fallback mode */
@media (prefers-reduced-motion: reduce) {
    * {
        transition: none !important;
        animation: none !important;
    }
    
    .tone-selector-button.loading::after {
        animation: none;
        content: '⏳';
        border: none;
        font-size: 1rem;
        margin: -0.5em 0 0 -0.5em;
    }
}

/* High contrast mode improvements */
@media (prefers-contrast: high) {
    .tone-button,
    .quiz-button,
    .result-button,
    .nav-button,
    .btn,
    .option-btn,
    .quiz-start-btn {
        border-width: 3px !important;
    }
    
    .tone-error-message {
        border-width: 2px !important;
    }
    
    .tone-selector-button {
        border-width: 3px !important;
    }
    
    .tone-selector-button:focus-visible {
        outline-width: 4px !important;
    }
    
    .tone-selector-button[aria-pressed="true"]::before {
        text-shadow: none;
        background: currentColor;
        color: white;
        border-radius: 50%;
        width: 16px;
        height: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 0.7rem;
    }
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
    .tone-selector-button[data-shortcut]::after {
        background: rgba(255, 255, 255, 0.9);
        color: black;
    }
    
    .tone-error-message {
        background-color: #450a0a;
        border-color: #dc2626;
        color: #fca5a5;
    }
    
    .tone-fallback-notice {
        background-color: #451a03;
        border-color: #d97706;
        color: #fbbf24;
    }
}

/* Print styles */
@media print {
    .tone-selector-container {
        display: none !important;
    }
    
    #tone-selector-announcements {
        display: none !important;
    }
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {
    .tone-selector-button {
        min-height: 44px; /* Minimum touch target size */
        min-width: 44px;
        padding: 12px 16px;
    }
    
    .tone-selector-button:hover {
        transform: none; /* Disable hover transforms on touch devices */
    }
    
    .tone-selector-button[data-shortcut]::after {
        display: none; /* Hide keyboard shortcuts on touch devices */
    }
}

/* Large text support */
@media (min-resolution: 2dppx) {
    .tone-selector-button .tone-emoji {
        font-size: 1.3rem;
    }
}

/* Windows High Contrast Mode */
@media (-ms-high-contrast: active) {
    .tone-selector-button {
        border: 2px solid ButtonText !important;
        background: ButtonFace !important;
        color: ButtonText !important;
    }
    
    .tone-selector-button:focus {
        border: 2px solid Highlight !important;
        background: Highlight !important;
        color: HighlightText !important;
    }
    
    .tone-selector-button.active {
        background: Highlight !important;
        color: HighlightText !important;
    }
}