/* Global reset and base styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    overflow: hidden;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* Container - responsive height based on context */
.container {
    width: 100%;
    height: 450px;
    background: white;
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden;
}

/* If opened in new tab, use 90vh */
@media (min-height: 600px) {
    body.standalone .container {
        height: 90vh;
    }
}

/* Header tooltip - appears on hover/tap */
.header-tooltip {
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.85);
    color: white;
    padding: 8px 16px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
    z-index: 1000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
    display: flex;
    align-items: center;
    gap: 12px;
    white-space: nowrap;
}

.header-tooltip.visible {
    opacity: 1;
    pointer-events: all;
}

.close-tooltip {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 18px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.close-tooltip:hover,
.close-tooltip:active {
    background: rgba(255, 255, 255, 0.4);
}

/* Main content layout */
.main-content {
    display: flex;
    flex: 1;
    gap: 12px;
    padding: 12px;
    overflow: hidden;
}

/* Problem panel - left side */
.problem-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 10px;
    overflow-y: auto;
    padding-right: 8px;
}

/* Problem display */
.problem-display {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 16px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    min-height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.problem-text {
    font-size: 24px;
    font-weight: 600;
    text-align: center;
    line-height: 1.4;
}

/* Visual container for fraction bars/pies */
.visual-container {
    background: #f8f9fa;
    border: 2px solid #dee2e6;
    border-radius: 8px;
    padding: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 180px;
}

#visualCanvas {
    max-width: 100%;
    height: auto;
}

/* Controls section */
.controls {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

/* Button styles - optimized for touch */
.btn {
    flex: 1;
    min-width: 100px;
    min-height: 48px;
    padding: 12px 16px;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
    white-space: nowrap;
    -webkit-tap-highlight-color: transparent;
}

.btn:active {
    transform: scale(0.97);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.btn-primary {
    background: #667eea;
    color: white;
}

.btn-primary:hover,
.btn-primary:active {
    background: #5568d3;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.btn-secondary {
    background: #6c757d;
    color: white;
}

.btn-secondary:hover,
.btn-secondary:active {
    background: #5a6268;
    box-shadow: 0 4px 12px rgba(108, 117, 125, 0.4);
}

.btn-success {
    background: #28a745;
    color: white;
}

.btn-success:hover,
.btn-success:active {
    background: #218838;
    box-shadow: 0 4px 12px rgba(40, 167, 69, 0.4);
}

.btn-small {
    min-height: 32px;
    padding: 6px 12px;
    font-size: 12px;
    min-width: auto;
}

/* Difficulty selector */
.difficulty-selector {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    background: #f8f9fa;
    border-radius: 6px;
}

.difficulty-selector label {
    font-weight: 600;
    font-size: 14px;
}

.difficulty-selector select {
    flex: 1;
    padding: 8px 12px;
    border: 2px solid #dee2e6;
    border-radius: 6px;
    font-size: 14px;
    background: white;
    cursor: pointer;
    min-height: 44px;
}

/* Answer input section */
.answer-input-section {
    background: #f8f9fa;
    padding: 12px;
    border-radius: 8px;
    border: 2px solid #dee2e6;
}

.input-label {
    font-weight: 600;
    margin-bottom: 8px;
    font-size: 14px;
}

.fraction-input {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    margin-bottom: 8px;
}

.fraction-input input {
    width: 80px;
    height: 48px;
    text-align: center;
    font-size: 18px;
    font-weight: 600;
    border: 2px solid #ced4da;
    border-radius: 6px;
    padding: 8px;
    transition: border-color 0.2s;
}

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

.fraction-bar {
    width: 40px;
    height: 2px;
    background: #333;
}

.feedback-message {
    min-height: 24px;
    padding: 6px;
    border-radius: 4px;
    font-size: 13px;
    font-weight: 600;
    text-align: center;
}

.feedback-message.correct {
    background: #d4edda;
    color: #155724;
}

.feedback-message.incorrect {
    background: #f8d7da;
    color: #721c24;
}

/* Hint panel */
.hint-panel,
.steps-panel {
    background: #fff3cd;
    border: 2px solid #ffc107;
    border-radius: 8px;
    padding: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.steps-panel {
    background: #d1ecf1;
    border-color: #17a2b8;
}

.hint-header,
.steps-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
    font-weight: 600;
    font-size: 14px;
}

.close-btn {
    background: rgba(0, 0, 0, 0.1);
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 20px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.close-btn:hover,
.close-btn:active {
    background: rgba(0, 0, 0, 0.2);
}

.hint-content,
.steps-content {
    font-size: 13px;
    line-height: 1.6;
}

.step-item {
    margin-bottom: 8px;
    padding: 8px;
    background: rgba(255, 255, 255, 0.5);
    border-radius: 4px;
}

.step-number {
    font-weight: 700;
    color: #17a2b8;
}

.hidden {
    display: none !important;
}

/* Analytics panel - right side */
.analytics-panel {
    width: 280px;
    background: #f8f9fa;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.analytics-header {
    background: #343a40;
    color: white;
    padding: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 600;
    font-size: 14px;
}

.toggle-analytics {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    width: 32px;
    height: 32px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 18px;
    font-weight: bold;
    transition: background 0.2s;
}

.toggle-analytics:hover,
.toggle-analytics:active {
    background: rgba(255, 255, 255, 0.3);
}

.analytics-content {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.analytics-content.collapsed {
    display: none;
}

.analytics-stats {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.stat-item {
    display: flex;
    justify-content: space-between;
    padding: 8px;
    background: white;
    border-radius: 4px;
    font-size: 12px;
}

.stat-label {
    font-weight: 600;
}

.stat-value {
    color: #667eea;
    font-weight: 700;
}

.action-log-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 600;
    font-size: 13px;
    padding-bottom: 6px;
    border-bottom: 2px solid #dee2e6;
}

.action-log {
    flex: 1;
    overflow-y: auto;
    font-size: 11px;
    line-height: 1.5;
}

.log-entry {
    padding: 6px;
    margin-bottom: 4px;
    background: white;
    border-radius: 4px;
    border-left: 3px solid #667eea;
}

.log-time {
    color: #6c757d;
    font-weight: 600;
}

.log-action {
    color: #333;
}

.log-correct {
    border-left-color: #28a745;
    background: #d4edda;
}

.log-incorrect {
    border-left-color: #dc3545;
    background: #f8d7da;
}

/* Touch feedback */
.btn[data-touch-active="true"],
.close-btn[data-touch-active="true"],
.toggle-analytics[data-touch-active="true"] {
    box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.3);
}

/* Responsive adjustments for mobile */
@media (max-width: 768px) {
    .main-content {
        flex-direction: column;
    }
    
    .analytics-panel {
        width: 100%;
        max-height: 200px;
    }
    
    .controls {
        flex-direction: column;
    }
    
    .btn {
        width: 100%;
    }
}

/* Touch device detection */
@media (hover: none) and (pointer: coarse) {
    .btn,
    .close-btn,
    .toggle-analytics,
    .close-tooltip {
        min-width: 48px;
        min-height: 48px;
    }
}

/* Scrollbar styling */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #555;
}