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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    color: #333;
    line-height: 1.6;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 15px;
}

header {
    text-align: center;
    margin-bottom: 20px;
    color: white;
}

header h1 {
    font-size: 2.2rem;
    margin-bottom: 5px;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}

header p {
    font-size: 1rem;
    opacity: 0.9;
    margin-bottom: 10px;
}

.status-indicator {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 15px;
    margin-top: 10px;
}

#current-status {
    font-size: 1.1rem;
    font-weight: 600;
}

.mode-badge {
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 0.9rem;
    font-weight: bold;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.mode-badge.setup {
    background: rgba(147, 51, 234, 0.2);
    color: white;
    border: 2px solid rgba(147, 51, 234, 0.5);
}

.mode-badge.generating {
    background: rgba(22, 163, 74, 0.2);
    color: white;
    border: 2px solid rgba(22, 163, 74, 0.5);
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

.main-content {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: 20px;
    margin-bottom: 20px;
}

@media (max-width: 1024px) {
    .main-content {
        grid-template-columns: 300px 1fr;
        gap: 15px;
    }
    
    .container {
        padding: 10px;
    }
}

@media (max-width: 768px) {
    .main-content {
        grid-template-columns: 1fr;
        gap: 15px;
    }
    
    header h1 {
        font-size: 1.8rem;
    }
}

.controls-panel {
    background: rgba(255, 255, 255, 0.98);
    border-radius: 16px;
    padding: 20px;
    backdrop-filter: blur(10px);
    box-shadow: 0 8px 32px rgba(0,0,0,0.12);
    height: fit-content;
    max-height: 85vh;
    overflow-y: auto;
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.controls-panel.expanded {
    /* Always expanded for immediate access */
}

.triangle-controls,
.generation-controls {
    margin-bottom: 25px;
    border-bottom: 2px solid #f1f5f9;
    padding-bottom: 20px;
}

.triangle-controls:last-child,
.generation-controls:last-child {
    border-bottom: none;
    margin-bottom: 15px;
}

.triangle-controls h3,
.generation-controls h3 {
    color: #475569;
    margin-bottom: 15px;
    font-size: 1.1rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 8px;
}

.preset-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-bottom: 15px;
}

.preset-btn {
    padding: 10px 12px;
    border: 2px solid #e2e8f0;
    border-radius: 8px;
    background: white;
    color: #64748b;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    min-height: 40px;
}

.preset-btn:hover {
    border-color: #9333ea;
    color: #9333ea;
    transform: translateY(-1px);
}

.preset-btn.active {
    background: #9333ea;
    border-color: #9333ea;
    color: white;
}

.vertex-coordinates {
    background: #f8fafc;
    border-radius: 8px;
    padding: 12px;
    margin: 10px 0;
    font-size: 0.85rem;
}

.coord-header {
    font-weight: 600;
    color: #475569;
    margin-bottom: 8px;
}

.vertex-coord,
.start-point-coord {
    display: flex;
    justify-content: space-between;
    margin: 6px 0;
    padding: 4px 8px;
    background: white;
    border-radius: 4px;
}

.vertex-label {
    color: #64748b;
    font-weight: 500;
}

.coord-values {
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    color: #1e293b;
    font-weight: 600;
}

.start-point-coord {
    border-left: 3px solid #ef4444;
}

.triangle-info {
    background: #f1f5f9;
    border-radius: 8px;
    padding: 12px;
    margin: 10px 0;
    font-size: 0.85rem;
}

.triangle-props {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.prop {
    display: flex;
    justify-content: space-between;
    color: #475569;
}

.prop strong {
    color: #1e293b;
}

.control-group {
    margin-bottom: 18px;
}

.control-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
    color: #475569;
    font-size: 0.9rem;
}

.primary-btn, .secondary-btn, .tertiary-btn {
    padding: 12px 18px;
    border: none;
    border-radius: 10px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    margin-right: 8px;
    margin-bottom: 8px;
    min-height: 48px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.primary-btn {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    font-size: 1.1rem;
    min-width: 160px;
}

.primary-btn:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.primary-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
}

.primary-btn.pulse {
    animation: buttonPulse 2s infinite;
}

@keyframes buttonPulse {
    0%, 100% { box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3); }
    50% { box-shadow: 0 6px 25px rgba(102, 126, 234, 0.6); }
}

.secondary-btn {
    background: #e2e8f0;
    color: #475569;
}

.secondary-btn:hover:not(:disabled) {
    background: #cbd5e0;
    transform: translateY(-1px);
}

.tertiary-btn {
    background: #fef3c7;
    color: #92400e;
    border: 2px solid #fbbf24;
}

.tertiary-btn:hover {
    background: #fde68a;
    transform: translateY(-1px);
}

.secondary-btn:disabled,
.tertiary-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

select {
    width: 100%;
    padding: 12px 14px;
    border: 2px solid #e2e8f0;
    border-radius: 8px;
    background: white;
    font-size: 1rem;
    cursor: pointer;
    min-height: 48px;
    appearance: none;
    background-image: url('data:image/svg+xml;utf8,<svg fill="%23475569" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M7 10l5 5 5-5z"/></svg>');
    background-repeat: no-repeat;
    background-position: right 12px center;
    background-size: 20px;
}

select:focus {
    border-color: #667eea;
    outline: none;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

input[type="range"] {
    width: 100%;
    margin: 12px 0 8px 0;
    -webkit-appearance: none;
    height: 8px;
    border-radius: 4px;
    background: #e2e8f0;
    outline: none;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: #667eea;
    cursor: pointer;
    border: 3px solid white;
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}

input[type="range"]::-moz-range-thumb {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: #667eea;
    cursor: pointer;
    border: 3px solid white;
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}

.ratio-labels {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    color: #6b7280;
    margin-top: 4px;
}

input[type="checkbox"] {
    margin-right: 10px;
    transform: scale(1.4);
    cursor: pointer;
    accent-color: #667eea;
}

.action-buttons {
    margin: 20px 0;
    text-align: center;
}

.configuration-status {
    background: #fef2f2;
    border: 2px solid #fecaca;
    border-radius: 10px;
    padding: 15px;
    margin: 15px 0;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.9rem;
    font-weight: 500;
}

.configuration-status.ready {
    background: #f0fdf4;
    border-color: #bbf7d0;
    color: #166534;
}

.configuration-status.pending {
    background: #fffbeb;
    border-color: #fde68a;
    color: #92400e;
}

.status-icon {
    font-size: 1.2rem;
    flex-shrink: 0;
}

.status-text {
    flex: 1;
}

.stats {
    background: #f8fafc;
    padding: 15px;
    border-radius: 10px;
    margin: 15px 0;
    border: 2px solid #e2e8f0;
}

.stat-item {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    font-weight: 600;
    font-size: 0.9rem;
    color: #475569;
}

.stat-item:last-child {
    margin-bottom: 0;
}

.stat-item span:last-child {
    color: #1e293b;
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
}

.canvas-area {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.canvas-container {
    position: relative;
    background: rgba(255, 255, 255, 0.98);
    border-radius: 16px;
    padding: 20px;
    backdrop-filter: blur(10px);
    box-shadow: 0 8px 32px rgba(0,0,0,0.12);
    text-align: center;
    border: 1px solid rgba(255, 255, 255, 0.2);
}

#sierpinski-canvas {
    border: 3px dashed #9333ea;
    border-radius: 12px;
    cursor: grab;
    touch-action: none;
    max-width: 100%;
    height: auto;
    transition: border-color 0.3s ease;
}

#sierpinski-canvas:active {
    cursor: grabbing;
}

.canvas-instructions {
    position: absolute;
    top: 25px;
    left: 25px;
    right: 25px;
    background: rgba(0, 0, 0, 0.9);
    color: white;
    padding: 15px;
    border-radius: 12px;
    pointer-events: none;
    z-index: 10;
    transition: opacity 0.3s ease;
}

.instruction-step {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 8px 0;
    padding: 8px 12px;
    border-radius: 8px;
    transition: all 0.3s ease;
    opacity: 0.5;
    font-size: 0.9rem;
}

.instruction-step.active {
    opacity: 1;
    background: rgba(255, 255, 255, 0.1);
}

.instruction-step.completed {
    opacity: 0.7;
    text-decoration: line-through;
}

.step-number {
    background: #667eea;
    color: white;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 0.8rem;
    flex-shrink: 0;
}

.instruction-step.completed .step-number {
    background: #16a34a;
}

.step-text {
    flex: 1;
}

.canvas-grid-toggle {
    position: absolute;
    bottom: 15px;
    right: 15px;
    background: rgba(255, 255, 255, 0.9);
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 0.85rem;
    border: 1px solid #e2e8f0;
}

.performance-info {
    background: rgba(255, 255, 255, 0.95);
    padding: 10px 15px;
    border-radius: 10px;
    font-size: 0.8rem;
    color: #475569;
    text-align: center;
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    border: 1px solid rgba(255, 255, 255, 0.2);
}

footer {
    text-align: center;
    color: white;
    opacity: 0.95;
    margin-top: 20px;
    font-size: 0.9rem;
}

footer a {
    color: #fbbf24;
    text-decoration: none;
    font-weight: 600;
}

footer a:hover {
    text-decoration: underline;
}

footer p {
    margin-bottom: 6px;
}

/* High contrast mode for better visibility */
@media (prefers-contrast: high) {
    .controls-panel {
        background: rgba(255, 255, 255, 1);
        border: 3px solid #000;
    }
    
    .canvas-container {
        background: rgba(255, 255, 255, 1);
        border: 3px solid #000;
    }
    
    #sierpinski-canvas {
        border: 3px solid #000;
    }
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
    .controls-panel {
        background: rgba(31, 41, 55, 0.98);
        color: #f3f4f6;
        border-color: rgba(55, 65, 81, 0.8);
    }
    
    .triangle-controls h3,
    .generation-controls h3 {
        color: #d1d5db;
    }
    
    .vertex-coordinates,
    .triangle-info {
        background: #374151;
        color: #f3f4f6;
    }
    
    .vertex-coord,
    .start-point-coord {
        background: #4b5563;
    }
    
    .vertex-label {
        color: #9ca3af;
    }
    
    .coord-values {
        color: #f3f4f6;
    }
    
    .stats {
        background: #374151;
        color: #f3f4f6;
        border-color: #4b5563;
    }
    
    .canvas-container {
        background: rgba(31, 41, 55, 0.98);
        border-color: rgba(55, 65, 81, 0.8);
    }
    
    .performance-info {
        background: rgba(31, 41, 55, 0.95);
        color: #d1d5db;
        border-color: rgba(55, 65, 81, 0.8);
    }
    
    select {
        background: #374151;
        color: #f3f4f6;
        border-color: #4b5563;
    }
    
    .preset-btn {
        background: #374151;
        color: #d1d5db;
        border-color: #4b5563;
    }
    
    .preset-btn:hover {
        border-color: #9333ea;
        color: #9333ea;
    }
    
    .canvas-grid-toggle {
        background: rgba(31, 41, 55, 0.9);
        color: #f3f4f6;
        border-color: #4b5563;
    }
}

/* Ensure touch targets are large enough */
@media (max-width: 480px) {
    .primary-btn, .secondary-btn, .tertiary-btn {
        width: 100%;
        margin-right: 0;
        font-size: 1.1rem;
        padding: 14px 20px;
        min-height: 52px;
    }
    
    .preset-buttons {
        grid-template-columns: 1fr;
    }
    
    .preset-btn {
        min-height: 48px;
        font-size: 1rem;
    }
    
    .canvas-container {
        padding: 15px;
    }
    
    .controls-panel {
        padding: 15px;
        max-height: none;
    }
    
    .canvas-instructions {
        position: relative;
        top: auto;
        left: auto;
        right: auto;
        margin-bottom: 15px;
    }
    
    input[type="range"]::-webkit-slider-thumb,
    input[type="range"]::-moz-range-thumb {
        width: 28px;
        height: 28px;
    }
}

/* Accessibility improvements */
:focus {
    outline: 3px solid #667eea;
    outline-offset: 2px;
}

button:focus,
select:focus,
input:focus {
    outline: 3px solid #667eea;
    outline-offset: 2px;
}

/* Loading and transition states */
.controls-panel {
    transition: all 0.3s ease;
}

.canvas-container {
    transition: all 0.3s ease;
}

/* Responsive text sizing */
@media (max-width: 480px) {
    .instruction-step {
        font-size: 0.85rem;
    }
    
    .vertex-coordinates,
    .triangle-info {
        font-size: 0.8rem;
    }
    
    .performance-info {
        font-size: 0.75rem;
    }
}

/* Enhanced visual feedback */
.primary-btn:active {
    transform: translateY(0);
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.preset-btn:active {
    transform: translateY(0);
}

/* Smooth animations for state changes */
.mode-badge {
    transition: all 0.3s ease;
}

.configuration-status {
    transition: all 0.3s ease;
}

/* Better scrollbar styling */
.controls-panel::-webkit-scrollbar {
    width: 8px;
}

.controls-panel::-webkit-scrollbar-track {
    background: #f1f5f9;
    border-radius: 4px;
}

.controls-panel::-webkit-scrollbar-thumb {
    background: #cbd5e0;
    border-radius: 4px;
}

.controls-panel::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}