/* ============================================
   RESET & BASE STYLES
   ============================================ */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: #f8fafc;
    color: #1e293b;
    overflow-x: hidden;
    line-height: 1.6;
}

/* ============================================
   MAIN CONTAINER - Responsive height
   ============================================ */
#main-container {
    width: 100%;
    /* Auto-detect: if in iframe use 450px, else use 90vh */
    height: 450px;
    display: flex;
    flex-direction: column;
    background: #ffffff;
    position: relative;
}

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

/* ============================================
   TITLE BAR - Collapsible to save space
   ============================================ */
#title-bar {
    background: linear-gradient(135deg, #0284c7 0%, #0369a1 100%);
    color: white;
    display: flex;
    align-items: center;
    padding: 8px 12px;
    transition: all 0.3s ease;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

#title-bar.collapsed {
    padding: 4px 12px;
}

#title-toggle {
    background: rgba(255,255,255,0.2);
    border: none;
    color: white;
    width: 44px;
    height: 44px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

#title-toggle:hover {
    background: rgba(255,255,255,0.3);
}

#title-toggle:active {
    background: rgba(255,255,255,0.4);
    transform: scale(0.95);
}

#title-icon {
    transition: transform 0.3s ease;
}

#title-bar.collapsed #title-icon {
    transform: rotate(-90deg);
}

#title-content {
    margin-left: 12px;
    font-size: 18px;
    font-weight: 600;
    flex: 1;
    transition: all 0.3s ease;
}

#title-bar.collapsed #title-content {
    font-size: 0;
    opacity: 0;
    margin: 0;
}

/* ============================================
   INTERACTIVE AREA - Two-column layout
   ============================================ */
#interactive-area {
    flex: 1;
    display: flex;
    gap: 16px;
    padding: 16px;
    overflow: hidden;
}

/* Left panel: Step-by-step solution */
#left-panel {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding-right: 8px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* Custom scrollbar for left panel */
#left-panel::-webkit-scrollbar {
    width: 8px;
}

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

#left-panel::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 4px;
}

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

/* Right panel: Graph and controls */
#right-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 12px;
    overflow-y: auto;
}

/* ============================================
   STEP CARDS - Each step in the solution
   ============================================ */
.step-card {
    background: #ffffff;
    border: 2px solid #e2e8f0;
    border-radius: 12px;
    padding: 12px;
    transition: all 0.3s ease;
    position: relative;
}

.step-card:hover {
    border-color: #0284c7;
    box-shadow: 0 4px 12px rgba(2, 132, 199, 0.15);
}

.step-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}

.step-number {
    background: #0284c7;
    color: white;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 16px;
    flex-shrink: 0;
}

.step-title {
    font-weight: 600;
    font-size: 15px;
    color: #334155;
    flex: 1;
}

.hint-btn {
    background: #f1f5f9;
    border: 2px solid #cbd5e1;
    color: #0284c7;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 20px;
    font-weight: 700;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.hint-btn:hover {
    background: #e0f2fe;
    border-color: #0284c7;
}

.hint-btn:active {
    background: #bae6fd;
    transform: scale(0.95);
}

/* Equation display */
.equation-display {
    background: #f8fafc;
    border-left: 4px solid #0284c7;
    padding: 12px;
    border-radius: 6px;
    font-size: 16px;
    font-family: "Courier New", monospace;
    line-height: 1.8;
}

.equation-display > div {
    margin: 4px 0;
}

.sub-explanation {
    font-size: 13px;
    color: #64748b;
    font-style: italic;
    margin-top: 8px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.balance-step {
    background: #dbeafe;
    padding: 6px;
    border-radius: 4px;
    font-weight: 600;
    margin: 6px 0;
}

.final-answer {
    background: #dcfce7;
    color: #166534;
    padding: 10px;
    border-radius: 6px;
    font-weight: 700;
    font-size: 17px;
    text-align: center;
}

/* Hint tooltips */
.hint-tooltip {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: #fffbeb;
    border: 2px solid #fbbf24;
    border-radius: 8px;
    padding: 12px;
    margin-top: 8px;
    z-index: 100;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.hint-tooltip.active {
    display: block;
}

.close-hint {
    position: absolute;
    top: 8px;
    right: 8px;
    background: #fbbf24;
    border: none;
    color: white;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 20px;
    font-weight: 700;
    line-height: 1;
    transition: all 0.2s ease;
}

.close-hint:hover {
    background: #f59e0b;
}

.close-hint:active {
    transform: scale(0.9);
}

.hint-tooltip p {
    margin: 6px 0;
    font-size: 14px;
    color: #78350f;
}

.hint-tooltip strong {
    color: #92400e;
}

/* ============================================
   GRAPH CONTAINER
   ============================================ */
#graph-container {
    background: #ffffff;
    border: 2px solid #e2e8f0;
    border-radius: 12px;
    padding: 12px;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
}

#graph-canvas {
    display: block;
    max-width: 100%;
    height: auto;
}

#graph-tooltip {
    position: absolute;
    background: rgba(0,0,0,0.85);
    color: white;
    padding: 6px 10px;
    border-radius: 6px;
    font-size: 13px;
    pointer-events: none;
    display: none;
    z-index: 10;
    white-space: nowrap;
}

#graph-tooltip.active {
    display: block;
}

/* ============================================
   CONTROLS PANEL
   ============================================ */
#controls-panel {
    background: #f8fafc;
    border: 2px solid #e2e8f0;
    border-radius: 12px;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.control-group label {
    font-weight: 600;
    font-size: 14px;
    color: #334155;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

#x-value-display {
    color: #0284c7;
    font-size: 16px;
    font-weight: 700;
}

/* ============================================
   SLIDER STYLING - Native with custom accent
   ============================================ */
input[type="range"] {
    width: 100%;
    min-height: 44px;
    accent-color: #0284c7;
    background: linear-gradient(#cbd5e1, #cbd5e1) center/100% 8px no-repeat;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
}

/* WebKit (Chrome, Safari, Edge) - runnable-track */
input[type="range"]::-webkit-slider-runnable-track {
    height: 8px;
    background: #cbd5e1;
    border: 1px solid #94a3b8;
    border-radius: 999px;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 24px;
    height: 24px;
    background: #0284c7;
    border: 3px solid #ffffff;
    border-radius: 50%;
    cursor: pointer;
    margin-top: -9px;
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
    transition: all 0.2s ease;
}

input[type="range"]::-webkit-slider-thumb:hover {
    background: #0369a1;
    transform: scale(1.1);
}

input[type="range"]::-webkit-slider-thumb:active {
    background: #075985;
    transform: scale(1.05);
}

/* Firefox */
input[type="range"]::-moz-range-track {
    height: 8px;
    background: #cbd5e1;
    border: 1px solid #94a3b8;
    border-radius: 999px;
}

input[type="range"]::-moz-range-thumb {
    width: 24px;
    height: 24px;
    background: #0284c7;
    border: 3px solid #ffffff;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
    transition: all 0.2s ease;
}

input[type="range"]::-moz-range-thumb:hover {
    background: #0369a1;
    transform: scale(1.1);
}

input[type="range"]::-moz-range-thumb:active {
    background: #075985;
}

/* Focus state for accessibility */
input[type="range"]:focus-visible {
    outline: 3px solid #bae6fd;
    outline-offset: 2px;
}

.slider-labels {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    color: #64748b;
    margin-top: -4px;
}

/* ============================================
   VALUE DISPLAY
   ============================================ */
.value-display {
    background: #ffffff;
    border: 2px solid #e2e8f0;
    border-radius: 8px;
    padding: 12px;
    font-size: 15px;
}

#y-value-display {
    color: #dc2626;
    font-weight: 700;
    font-size: 16px;
}

/* ============================================
   CHECKBOX STYLING
   ============================================ */
.checkbox-label {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    padding: 8px;
    border-radius: 6px;
    transition: background 0.2s ease;
    min-height: 44px;
}

.checkbox-label:hover {
    background: #f1f5f9;
}

.checkbox-label input[type="checkbox"] {
    width: 24px;
    height: 24px;
    cursor: pointer;
    accent-color: #0284c7;
}

.checkbox-label span {
    font-size: 14px;
    color: #334155;
}

/* ============================================
   ACTION BUTTON
   ============================================ */
.action-btn {
    background: #0284c7;
    color: white;
    border: none;
    padding: 12px 24px;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    min-height: 48px;
    box-shadow: 0 2px 4px rgba(2, 132, 199, 0.3);
}

.action-btn:hover {
    background: #0369a1;
    box-shadow: 0 4px 8px rgba(2, 132, 199, 0.4);
}

.action-btn:active {
    background: #075985;
    transform: scale(0.98);
}

/* ============================================
   ANALYTICS PANEL
   ============================================ */
#analytics-container {
    border-top: 2px solid #e2e8f0;
    background: #f8fafc;
}

#analytics-toggle {
    width: 100%;
    background: #f1f5f9;
    border: none;
    padding: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    color: #334155;
    transition: all 0.2s ease;
    min-height: 48px;
}

#analytics-toggle:hover {
    background: #e2e8f0;
}

#analytics-toggle:active {
    background: #cbd5e1;
}

#analytics-icon {
    transition: transform 0.3s ease;
}

#analytics-panel.collapsed ~ #analytics-toggle #analytics-icon {
    transform: rotate(180deg);
}

#analytics-panel {
    max-height: 200px;
    overflow-y: auto;
    transition: all 0.3s ease;
}

#analytics-panel.collapsed {
    max-height: 0;
    overflow: hidden;
}

#analytics-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background: #ffffff;
    border-bottom: 1px solid #e2e8f0;
}

#analytics-header h3 {
    font-size: 15px;
    color: #334155;
    margin: 0;
}

#clear-log-btn {
    background: #ef4444;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    min-height: 44px;
}

#clear-log-btn:hover {
    background: #dc2626;
}

#clear-log-btn:active {
    background: #b91c1c;
    transform: scale(0.95);
}

#analytics-log {
    padding: 12px 16px;
    font-size: 13px;
    font-family: "Courier New", monospace;
    max-height: 140px;
    overflow-y: auto;
}

.log-entry {
    padding: 6px 0;
    border-bottom: 1px solid #f1f5f9;
    color: #475569;
}

.log-entry:last-child {
    border-bottom: none;
}

.log-time {
    color: #94a3b8;
    margin-right: 8px;
}

.log-action {
    color: #0284c7;
    font-weight: 600;
}

/* ============================================
   RESPONSIVE DESIGN
   ============================================ */

/* ============================================
   DIAGNOSTIC LEARNING CHECK
   ============================================ */

[hidden] {
    display: none !important;
}

#diagnostic-section {
    margin: 18px;
    padding: 22px;
    border: 1px solid #cbd5e1;
    border-radius: 14px;
    background: #ffffff;
    box-shadow: 0 8px 22px rgba(15, 23, 42, 0.08);
}

.diagnostic-heading {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 20px;
    align-items: center;
    padding-bottom: 18px;
    border-bottom: 1px solid #e2e8f0;
}

.diagnostic-kicker {
    margin: 0 0 4px;
    color: #0369a1;
    font-size: 0.76rem;
    font-weight: 800;
    letter-spacing: 0.09em;
    text-transform: uppercase;
}

.diagnostic-heading h2 {
    margin: 0;
    color: #0f172a;
    font-size: clamp(1.25rem, 3vw, 1.7rem);
}

.diagnostic-heading p:last-child {
    margin: 7px 0 0;
    color: #475569;
}

#diagnostic-summary {
    min-width: 116px;
    padding: 13px 16px;
    border-radius: 12px;
    text-align: center;
    background: #e0f2fe;
}

#diagnostic-summary strong,
#diagnostic-summary span {
    display: block;
}

#diagnostic-summary strong {
    color: #075985;
    font-size: 1.55rem;
}

#diagnostic-summary span {
    color: #475569;
    font-size: 0.73rem;
}

.confidence-row {
    display: grid;
    grid-template-columns: minmax(210px, 1fr) auto;
    gap: 14px;
    align-items: center;
    margin: 18px 0;
    padding: 13px 15px;
    border-left: 4px solid #0284c7;
    border-radius: 9px;
    background: #f0f9ff;
}

.confidence-row > span {
    color: #0f172a;
    font-weight: 700;
}

.confidence-buttons {
    display: flex;
    gap: 7px;
    flex-wrap: wrap;
}

.confidence-buttons button {
    min-width: 62px;
    min-height: 44px;
    padding: 6px 10px;
    border: 1px solid #94a3b8;
    border-radius: 9px;
    color: #0f172a;
    background: #ffffff;
    font-weight: 800;
}

.confidence-buttons button small {
    display: block;
    color: #64748b;
    font-size: 0.62rem;
    font-weight: 600;
}

.confidence-buttons button.selected {
    color: #ffffff;
    border-color: #0369a1;
    background: #0369a1;
}

.confidence-buttons button.selected small {
    color: #e0f2fe;
}

#diagnostic-items {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
}

.diagnostic-card {
    padding: 16px;
    border: 1px solid #d8e1e8;
    border-radius: 12px;
    background: #f8fafc;
}

.diagnostic-card[data-status="correct"] {
    border-color: #86efac;
    background: #f0fdf4;
}

.diagnostic-card[data-status="incorrect"] {
    border-color: #fca5a5;
    background: #fff7f7;
}

.diagnostic-card-head {
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: 10px;
    align-items: start;
}

.diagnostic-number {
    display: grid;
    place-items: center;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    color: #ffffff;
    background: #0369a1;
    font-weight: 800;
}

.diagnostic-card h3 {
    margin: 2px 0 0;
    color: #0f172a;
    font-size: 1rem;
    line-height: 1.4;
}

.math-expression {
    white-space: nowrap;
    font-family: Georgia, "Times New Roman", serif;
}

.diagnostic-hint {
    min-height: 34px;
    padding: 6px 10px;
    border: 1px solid #d6a331;
    border-radius: 999px;
    color: #6b4700;
    background: #fffbeb;
    font-size: 0.78rem;
    font-weight: 800;
}

.diagnostic-options {
    display: grid;
    gap: 8px;
    margin-top: 14px;
}

.diagnostic-options button {
    width: 100%;
    min-height: 45px;
    padding: 9px 11px;
    border: 1px solid #cbd5e1;
    border-radius: 9px;
    color: #1e293b;
    text-align: left;
    background: #ffffff;
}

.diagnostic-options button:hover {
    border-color: #0284c7;
    background: #f0f9ff;
}

.diagnostic-options button.selected {
    border-color: #0284c7;
    box-shadow: inset 0 0 0 1px #0284c7;
}

.diagnostic-options button.correct {
    border-color: #16a34a;
    background: #dcfce7;
}

.diagnostic-options button.incorrect {
    border-color: #dc2626;
    background: #fee2e2;
}

.diagnostic-feedback {
    margin-top: 11px;
    padding: 11px 12px;
    border-radius: 9px;
    font-size: 0.9rem;
    line-height: 1.45;
}

.diagnostic-feedback.correct {
    color: #14532d;
    border: 1px solid #86efac;
    background: #dcfce7;
}

.diagnostic-feedback.incorrect {
    color: #7f1d1d;
    border: 1px solid #fca5a5;
    background: #fee2e2;
}

.diagnostic-feedback.hint {
    color: #713f12;
    border: 1px solid #fcd34d;
    background: #fffbeb;
}

.diagnostic-complete-row {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 16px;
    align-items: center;
    margin-top: 18px;
    padding-top: 18px;
    border-top: 1px solid #e2e8f0;
}

.diagnostic-complete-row p {
    margin: 0;
    color: #475569;
}

#complete-learning-btn {
    min-height: 46px;
    padding: 10px 18px;
    border: none;
    border-radius: 10px;
    color: #ffffff;
    background: #0369a1;
    font-weight: 800;
}

#complete-learning-btn:disabled {
    cursor: not-allowed;
    opacity: 0.45;
}

#completion-feedback {
    margin-top: 14px;
    padding: 14px 16px;
    border-left: 5px solid #16a34a;
    border-radius: 9px;
    color: #14532d;
    background: #f0fdf4;
    line-height: 1.5;
}

@media (max-width: 800px) {
    .diagnostic-heading,
    .confidence-row,
    .diagnostic-complete-row {
        grid-template-columns: 1fr;
    }

    #diagnostic-summary {
        justify-self: start;
    }

    #diagnostic-items {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 480px) {
    #diagnostic-section {
        margin: 9px;
        padding: 14px;
    }

    .diagnostic-card-head {
        grid-template-columns: auto 1fr;
    }

    .diagnostic-hint {
        grid-column: 1 / -1;
        width: 100%;
    }

    .confidence-buttons {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* Tablet and below: stack panels vertically */
@media (max-width: 768px) {
    #interactive-area {
        flex-direction: column;
    }
    
    #left-panel {
        max-height: 300px;
    }
    
    #graph-canvas {
        width: 100%;
        height: auto;
    }
}

/* Small mobile */
@media (max-width: 480px) {
    #interactive-area {
        padding: 8px;
        gap: 8px;
    }
    
    .step-card {
        padding: 10px;
    }
    
    .equation-display {
        font-size: 14px;
        padding: 10px;
    }
    
    #controls-panel {
        padding: 12px;
    }
}

/* Touch device optimization */
@media (hover: none) and (pointer: coarse) {
    /* Increase touch targets */
    .hint-btn,
    #title-toggle,
    .close-hint {
        min-width: 48px;
        min-height: 48px;
    }
    
    .action-btn,
    #analytics-toggle,
    #clear-log-btn {
        min-height: 52px;
    }
    
    /* Add more spacing between interactive elements */
    .control-group {
        gap: 12px;
    }
    
    #controls-panel {
        gap: 20px;
    }
}

/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .step-card {
        border-width: 3px;
    }
    
    input[type="range"]::-webkit-slider-thumb,
    input[type="range"]::-moz-range-thumb {
        border-width: 4px;
    }
}
