/* ===================================
   Handwriting Generator Tool Styles
   =================================== */

/* Main Layout */
.tool-main {
    padding: var(--space-8) 0;
    min-height: calc(100vh - 400px);
}

.tool-layout {
    display: grid;
    gap: var(--space-8);
    grid-template-columns: 1fr;
}

@media (min-width: 1024px) {
    .tool-layout {
        grid-template-columns: 450px 1fr;
        gap: var(--space-12);
        align-items: flex-start;
    }
    .controls-panel {
        order: 1;
    }
    .preview-panel {
        order: 2;
    }
}

/* Desktop Layout (2-column) */
@media (min-width: 1024px) {
    .preview-container {
        min-height: 60vh !important;
        padding: var(--space-4);
        overflow: auto !important;
        max-height: none !important;
        height: auto !important;
    }
    .preview-wrapper {
        max-height: none !important;
        min-height: 0 !important;
        height: auto !important;
        width: 100% !important;
        display: block;
    }
}

.controls-panel {
    background: var(--bg-primary);
    border-radius: var(--radius-xl);
    border: 1px solid var(--gray-200);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
}

[data-theme="dark"] .controls-panel {
    border-color: var(--gray-700);
    background: var(--bg-secondary);
}

/* Input Section */
.input-section {
    padding: var(--space-6);
    border-bottom: 1px solid var(--gray-200);
}

[data-theme="dark"] .input-section {
    border-bottom-color: var(--gray-700);
}

.section-title {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-weight: var(--font-weight-semibold);
    color: var(--text-primary);
    margin-bottom: var(--space-3);
    font-size: 1rem;
}

.section-title i {
    color: var(--brand-primary);
    width: 16px;
}

#textInput {
    width: 100%;
    padding: var(--space-4);
    border: 2px solid var(--gray-200);
    border-radius: var(--radius-lg);
    font-family: var(--font-family);
    font-size: 1rem;
    line-height: 1.5;
    color: var(--text-primary);
    background: var(--bg-primary);
    resize: vertical;
    min-height: 120px;
    transition: border-color var(--transition-base);
}

#textInput:focus {
    outline: none;
    border-color: var(--brand-primary);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

[data-theme="dark"] #textInput {
    background: var(--bg-tertiary);
    border-color: var(--gray-600);
    color: var(--text-primary);
}

[data-theme="dark"] #textInput:focus {
    border-color: var(--brand-primary);
}

.text-stats {
    margin-top: var(--space-2);
    font-size: 0.875rem;
    color: var(--text-tertiary);
}

/* Control Sections */
.control-section {
    border-bottom: 1px solid var(--gray-200);
}

[data-theme="dark"] .control-section {
    border-bottom-color: var(--gray-700);
}

.control-section:last-child {
    border-bottom: none;
}

/* Font Selection - make it consistent with accordion content */
.control-section:first-of-type {
    padding: var(--space-6);
}

.control-section:first-of-type .section-title {
    margin-bottom: var(--space-3);
}

.control-section:first-of-type #fontSelect {
    margin-top: 0;
}

#fontSelect,
#pageSize,
#resolution {
    width: 100%;
    height: 40px; /* Reduced height for better proportions */
    padding: var(--space-2) var(--space-3);
    border: 2px solid var(--gray-200);
    border-radius: var(--radius-lg);
    font-family: var(--font-family);
    font-size: 0.9rem;
    line-height: 1.3;
    color: var(--text-primary);
    background: var(--bg-primary);
    cursor: pointer;
    transition: border-color var(--transition-base);
    /* Ensure clean dropdown appearance */
    background-color: var(--bg-primary);
    border-style: solid;
    text-overflow: ellipsis;
    box-sizing: border-box;
}

/* Fix for option elements */
#fontSelect option,
#pageSize option,
#resolution option {
    padding: var(--space-2);
    background: var(--bg-primary);
    color: var(--text-primary);
}

#fontSelect:focus,
#pageSize:focus,
#resolution:focus {
    outline: none;
    border-color: var(--brand-primary);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

[data-theme="dark"] #fontSelect,
[data-theme="dark"] #pageSize,
[data-theme="dark"] #resolution {
    background: var(--bg-tertiary);
    border-color: var(--gray-600);
    color: var(--text-primary);
}

[data-theme="dark"] #fontSelect option,
[data-theme="dark"] #pageSize option,
[data-theme="dark"] #resolution option {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

/* Accordion System */
.accordion-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-6);
    cursor: pointer;
    transition: background-color var(--transition-base);
}

.accordion-header:hover {
    background: var(--gray-50);
}

[data-theme="dark"] .accordion-header:hover {
    background: var(--gray-800);
}

.accordion-icon {
    color: var(--text-tertiary);
    transition: transform var(--transition-base);
}

.accordion-content {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease-out;
}

.accordion-content.active {
    max-height: 1000px;
    transition: max-height 0.4s ease-in;
}

.accordion-content .control-group {
    padding: 0 var(--space-6) var(--space-4);
}

.accordion-content .control-group:last-child {
    padding-bottom: var(--space-6);
}

.accordion-header.active .accordion-icon {
    transform: rotate(180deg);
}

/* Control Groups */
.control-group {
    margin-bottom: var(--space-4);
}

.control-group:last-child {
    margin-bottom: 0;
}

.control-group label {
    display: block;
    font-weight: var(--font-weight-medium);
    color: var(--text-primary);
    margin-bottom: var(--space-2);
    font-size: 0.9rem;
}

/* Slider Controls */
.slider-container {
    display: flex;
    align-items: center;
    gap: var(--space-3);
}

.slider-container input[type="range"] {
    flex: 1;
    height: 6px;
    border-radius: 3px;
    background: var(--gray-200);
    outline: none;
    cursor: pointer;
}

[data-theme="dark"] .slider-container input[type="range"] {
    background: var(--gray-700);
}

.slider-container input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--brand-primary);
    cursor: pointer;
    transition: transform var(--transition-base);
}

.slider-container input[type="range"]::-webkit-slider-thumb:hover {
    transform: scale(1.1);
}

.slider-container input[type="range"]::-moz-range-thumb {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--brand-primary);
    cursor: pointer;
    border: none;
    transition: transform var(--transition-base);
}

.slider-value {
    font-size: 0.875rem;
    font-weight: var(--font-weight-medium);
    color: var(--brand-primary);
    min-width: 50px;
    text-align: right;
}

/* Color Picker */
.color-picker-container {
    display: flex;
    align-items: center;
    gap: var(--space-3);
}

#inkColor {
    width: 50px;
    height: 40px;
    border: 2px solid var(--gray-200);
    border-radius: var(--radius-lg);
    cursor: pointer;
    background: none;
    padding: 0;
}

[data-theme="dark"] #inkColor {
    border-color: var(--gray-600);
}

.color-preview {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 2px solid var(--gray-300);
    background: var(--brand-primary);
}

/* Background Gallery */
.background-gallery {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-3);
    margin-top: var(--space-2);
}

.bg-option {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-3);
    border: 2px solid var(--gray-200);
    border-radius: var(--radius-lg);
    cursor: pointer;
    transition: all var(--transition-base);
    text-align: center;
}

.bg-option:hover {
    border-color: var(--brand-primary);
    transform: translateY(-2px);
}

.bg-option.active {
    border-color: var(--brand-primary);
    background: rgba(59, 130, 246, 0.05);
}

[data-theme="dark"] .bg-option {
    border-color: var(--gray-600);
}

[data-theme="dark"] .bg-option.active {
    background: rgba(59, 130, 246, 0.1);
}

.bg-preview {
    width: 60px;
    height: 40px;
    border-radius: var(--radius-base);
    border: 1px solid var(--gray-300);
}

.bg-white {
    background: #ffffff;
}

.bg-lined {
    background: 
        linear-gradient(to bottom, transparent 19px, #e5e7eb 19px, #e5e7eb 21px, transparent 21px),
        #ffffff;
    background-size: 100% 20px;
}

.bg-grid {
    background: 
        linear-gradient(to right, #e5e7eb 1px, transparent 1px),
        linear-gradient(to bottom, #e5e7eb 1px, transparent 1px),
        #ffffff;
    background-size: 20px 20px;
}

.bg-parchment {
    background: linear-gradient(45deg, #f4f1e8 25%, #f7f4eb 25%, #f7f4eb 50%, #f4f1e8 50%, #f4f1e8 75%, #f7f4eb 75%);
    background-size: 4px 4px;
}

.bg-option span {
    font-size: 0.8rem;
    font-weight: var(--font-weight-medium);
    color: var(--text-secondary);
}

/* Download Section */
.download-section {
    padding: var(--space-6);
}

#downloadBtn {
    width: 100%;
    padding: var(--space-4) var(--space-6);
    font-size: 1rem;
    gap: var(--space-2);
}

/* ===================================
   Preview Panel
   =================================== */
.preview-panel {
    background: var(--bg-primary);
    border-radius: var(--radius-xl);
    border: 1px solid var(--gray-200);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
    height: fit-content;
}

[data-theme="dark"] .preview-panel {
    border-color: var(--gray-700);
    background: var(--bg-secondary);
}

.preview-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-6);
    border-bottom: 1px solid var(--gray-200);
    background: var(--bg-secondary);
}

[data-theme="dark"] .preview-header {
    border-bottom-color: var(--gray-700);
    background: var(--bg-tertiary);
}

.preview-header h3 {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: 1.125rem;
    font-weight: var(--font-weight-semibold);
    color: var(--text-primary);
    margin: 0;
}

.preview-header h3 i {
    color: var(--brand-primary);
}

.preview-actions {
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

.preview-btn {
    width: 36px;
    height: 36px;
    border: 1px solid var(--gray-300);
    background: var(--bg-primary);
    color: var(--text-secondary);
    border-radius: var(--radius-base);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition-base);
}

.preview-btn:hover {
    border-color: var(--brand-primary);
    color: var(--brand-primary);
    transform: translateY(-1px);
}

[data-theme="dark"] .preview-btn {
    background: var(--bg-secondary);
    border-color: var(--gray-600);
}

#zoomLevel {
    font-size: 0.875rem;
    font-weight: var(--font-weight-medium);
    color: var(--text-primary);
    min-width: 40px;
    text-align: center;
}

.preview-container {
    padding: var(--space-6);
    min-height: 500px;
    display: block; /* Changed from flex to block for better scroll control */
    background: var(--gray-50);
    overflow: auto; /* Allow scrolling when zoomed */
    position: relative;
    scroll-behavior: smooth; /* Smooth scrolling */
    /* Ensure both horizontal and vertical scrolling work */
    overflow-x: auto;
    overflow-y: auto;
}

[data-theme="dark"] .preview-container {
    background: var(--gray-800);
}

.preview-wrapper {
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    transition: transform var(--transition-base), margin var(--transition-base);
    max-width: none; /* Remove max-width constraint for zoom */
    max-height: none; /* Remove max-height constraint for zoom */
    transform-origin: top left; /* Zoom from top left for consistent scroll behavior */
    display: block; /* Ensure proper block display */
    flex-shrink: 0; /* Prevent wrapper from shrinking */
}

#previewCanvas {
    display: block;
    width: 100% !important;
    min-width: 320px !important;
    max-width: none !important;
    height: auto !important;
    border-radius: var(--radius-lg);
    box-sizing: border-box;
}

/* ===================================
   Mobile Responsive Design
   =================================== */
@media (max-width: 1023px) {
    .tool-layout {
        grid-template-columns: 1fr;
    }
    
    /* Mobile: Preview first, then controls */
    .preview-panel {
        order: -1;
    }
    
    .controls-panel {
        order: 1;
    }
    
    /* Accordion always starts collapsed on mobile */
    .accordion-content {
        max-height: 0;
    }
    
    /* Smaller preview on mobile - same scrollable behavior as desktop */
    .preview-container {
        min-height: 300px;
        padding: var(--space-4);
        overflow-x: auto !important;
        overflow-y: auto !important;
        max-height: none !important;
        height: auto !important;
    }
    
    .preview-wrapper {
        max-height: none !important;
        min-width: 320px !important;
        width: 100% !important;
        height: auto !important;
        display: block;
        box-sizing: border-box;
        flex-shrink: 0;
    }
    
    #previewCanvas {
        width: 100% !important;
        min-width: 320px !important;
        max-width: none !important;
        height: auto !important;
        border-radius: var(--radius-lg);
        box-sizing: border-box;
        flex-shrink: 0;
    }
}

@media (max-width: 768px) {
    .tool-main {
        padding: var(--space-6) 0;
    }
    
    .background-gallery {
        grid-template-columns: repeat(2, 1fr);
        gap: var(--space-2);
    }
    
    .bg-preview {
        width: 50px;
        height: 30px;
    }
    
    .bg-option {
        padding: var(--space-2);
    }
    
    .bg-option span {
        font-size: 0.75rem;
    }
    
    .preview-header {
        padding: var(--space-4);
    }
    
    .input-section,
    .control-section:first-of-type,
    .accordion-header,
    .download-section {
        padding: var(--space-4);
    }
    
    .accordion-content .control-group {
        padding: 0 var(--space-4) var(--space-3);
    }
    
    .accordion-content .control-group:last-child {
        padding-bottom: var(--space-4);
    }
}

/* ===================================
   Animations & Loading States
   =================================== */
.preview-wrapper.loading {
    opacity: 0.7;
    pointer-events: none;
}

.preview-wrapper.loading::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 40px;
    height: 40px;
    margin: -20px 0 0 -20px;
    border: 3px solid var(--gray-300);
    border-top-color: var(--brand-primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Smooth transitions for theme switching */
* {
    transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

/* Custom scrollbar for preview */
.preview-wrapper::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

.preview-wrapper::-webkit-scrollbar-track {
    background: var(--gray-100);
    border-radius: 4px;
}

.preview-wrapper::-webkit-scrollbar-thumb {
    background: var(--gray-400);
    border-radius: 4px;
}

.preview-wrapper::-webkit-scrollbar-thumb:hover {
    background: var(--gray-500);
}

[data-theme="dark"] .preview-wrapper::-webkit-scrollbar-track {
    background: var(--gray-700);
}

[data-theme="dark"] .preview-wrapper::-webkit-scrollbar-thumb {
    background: var(--gray-600);
}

[data-theme="dark"] .preview-wrapper::-webkit-scrollbar-thumb:hover {
    background: var(--gray-500);
}
