/* ===========================
   GLOBAL STYLES
   =========================== */
* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: #f0f4f8;
    color: #333;
    overflow-x: hidden;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* ===========================
   MAIN APP CONTAINER
   =========================== */
#app {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

body.in-iframe #app { height: 450px; }

/* ===========================
   TITLE SECTION
   =========================== */
.title-section {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 8px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: all 0.3s ease;
    flex-shrink: 0;
}

.title-section.hidden { padding: 0; height: 0; overflow: hidden; }
.title-section h1 { font-size: 1.2rem; font-weight: 600; margin: 0; }

.toggle-btn {
    background: rgba(255,255,255,0.2);
    border: 1px solid rgba(255,255,255,0.4);
    color: white;
    padding: 6px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.85rem;
    min-height: 32px;
    transition: all 0.2s ease;
}
.toggle-btn:hover  { background: rgba(255,255,255,0.3); }
.toggle-btn:active { transform: scale(0.95); }

/* ===========================
   MAIN CONTENT LAYOUT
   =========================== */
.main-content {
    flex: 1;
    display: flex;
    gap: 10px;
    padding: 8px;
    overflow: hidden;
}

/* ===========================
   DIAGRAM CONTAINER
   =========================== */
.diagram-container {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    padding: 6px;
    min-width: 0;
    overflow: hidden;
}

#diagram {
    width: 100%;
    height: 100%;
    /* No max-width/max-height — fills available space */
}

/* SVG polygon — pointer cursor so students know they're clickable */
.triangle-poly { cursor: pointer; }
.triangle-poly:hover { filter: brightness(1.12); }

.area-label {
    font-size: 32px;
    font-weight: bold;
    fill: #555;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

.side-label {
    font-size: 28px;
    font-weight: bold;
    fill: #000;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

.right-angle-marker {
    fill: none;
    stroke: #333;
    stroke-width: 1.8;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

/* ===========================
   CONTROL PANEL
   =========================== */
.control-panel {
    width: 300px;
    display: flex;
    flex-direction: column;
    gap: 7px;
    overflow-y: auto;
}

.instruction-panel {
    background: #fff3cd;
    border: 2px solid #ffc107;
    border-radius: 8px;
    padding: 8px 12px;
}
.instruction-panel.hidden { display: none; }

.instruction-text { font-size: 0.88rem; line-height: 1.4; color: #856404; margin: 0; }

/* Slider container */
.slider-container {
    background: white;
    border-radius: 8px;
    padding: 8px 12px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.slider-label {
    display: block;
    font-size: 0.9rem;
    font-weight: 600;
    color: #2d5016;
    margin-bottom: 4px;
    text-align: center;
}

.slider {
    --fill:    #667eea;   /* overridden per-slider below */
    --val-pct: 50%;
    width: 100%;
    height: 48px;
    -webkit-appearance: none;
    appearance: none;
    background: transparent;
    outline: none;
    cursor: pointer;
}

/* Per-slider accent colours */
#rotationSlider { --fill: #f59e0b; }
#aSlider        { --fill: #667eea; }
#bSlider        { --fill: #059669; }

/* Filled track: coloured left of thumb, grey right */
.slider::-webkit-slider-runnable-track {
    height: 14px;
    border-radius: 7px;
    background: linear-gradient(to right,
        var(--fill) var(--val-pct),
        #d1d5db    var(--val-pct));
    box-shadow: inset 0 1px 3px rgba(0,0,0,0.15);
}
.slider::-moz-range-track {
    height: 14px;
    border-radius: 7px;
    background: #d1d5db;
    box-shadow: inset 0 1px 3px rgba(0,0,0,0.15);
}
.slider::-moz-range-progress {
    height: 14px;
    border-radius: 7px;
    background: var(--fill);
}

/* Thumb — colour matches each slider's fill */
.slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 32px; height: 32px;
    background: var(--fill);
    border: 3px solid white;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(0,0,0,0.3);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    margin-top: -9px;  /* vertically centre thumb on taller track */
}
.slider::-moz-range-thumb {
    width: 32px; height: 32px;
    background: var(--fill);
    border: 3px solid white;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(0,0,0,0.3);
}
.slider::-webkit-slider-thumb:hover { transform: scale(1.15); box-shadow: 0 0 0 8px color-mix(in srgb, var(--fill) 20%, transparent); }
.slider::-moz-range-thumb:hover     { transform: scale(1.15); }

/* ===========================
   LIVE EQUATION DISPLAY
   =========================== */
.equation-display {
    background: #eef2ff;
    border: 2px solid #667eea;
    border-radius: 8px;
    padding: 7px 12px;
    text-align: center;
    line-height: 1.6;
}

.eq-values  { font-size: 0.85rem; color: #444; }
.eq-values strong { color: #1e3a8a; }
.eq-formula { font-size: 0.88rem; font-weight: 600; color: #1e3a8a; }
.eq-formula span { color: #7c3aed; }

/* ===========================
   CONSERVATION OF AREA BREAKDOWN
   =========================== */
.area-breakdown {
    background: #f0fdf4;
    border: 2px solid #22c55e;
    border-radius: 8px;
    padding: 7px 12px;
    font-size: 0.82rem;
    line-height: 1.65;
}

.breakdown-title {
    font-weight: 700;
    color: #15803d;
    font-size: 0.82rem;
    margin-bottom: 2px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.breakdown-row { color: #374151; font-family: monospace; }
.breakdown-row span { font-weight: 700; color: #15803d; }

.breakdown-result {
    border-top: 1px solid #86efac;
    margin-top: 3px;
    padding-top: 3px;
    color: #166534;
    font-weight: 600;
}

/* ===========================
   BUTTON GROUP
   =========================== */
.button-group {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.control-btn {
    flex: 1;
    min-width: 110px;
    padding: 8px 10px;
    background: #667eea;
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    min-height: 38px;
    white-space: nowrap;
}
.control-btn:hover  { background: #5568d3; box-shadow: 0 4px 8px rgba(0,0,0,0.15); }
.control-btn:active { transform: scale(0.97); }

/* Play/Pause */
.play-btn           { background: #059669; }
.play-btn:hover     { background: #047857; }
.play-btn.playing   { background: #d97706; }
.play-btn.playing:hover { background: #b45309; }

/* Try Another Triangle */
.try-btn       { background: #7c3aed; }
.try-btn:hover { background: #6d28d9; }

/* Patterns toggle */
.pattern-btn         { background: #0891b2; }
.pattern-btn:hover   { background: #0e7490; }
.pattern-btn.pattern-active       { background: #059669; }
.pattern-btn.pattern-active:hover { background: #047857; }

.small-btn {
    min-width: 80px;
    padding: 8px 12px;
    font-size: 0.85rem;
    min-height: 36px;
}

/* Explanation section */
.explanation-container {
    background: white;
    border-radius: 8px;
    padding: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.explanation-btn       { width: 100%; background: #28a745; }
.explanation-btn:hover { background: #218838; }

.explanation-text {
    margin-top: 12px;
    padding: 12px;
    background: #d4edda;
    border: 2px solid #28a745;
    border-radius: 6px;
    font-size: 0.95rem;
    line-height: 1.6;
    color: #155724;
}
.explanation-text.hidden { display: none; }

/* ===========================
   CHECKPOINT PANELS
   =========================== */
.checkpoint-panel {
    background: white;
    border-radius: 8px;
    padding: 16px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.checkpoint-panel.hidden { display: none; }

.checkpoint-question {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 12px;
    color: #333;
    line-height: 1.4;
}

.checkpoint-inputs {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
    flex-wrap: wrap;
}

.checkpoint-input {
    width: 60px;
    padding: 8px;
    font-size: 1rem;
    text-align: center;
    border: 2px solid #ddd;
    border-radius: 4px;
    transition: border-color 0.2s ease;
}
.checkpoint-input:focus { outline: none; border-color: #667eea; }

.stage-input { width: auto; flex: 1; min-width: 130px; text-align: left; }

.checkpoint-feedback {
    font-size: 0.9rem;
    margin-top: 8px;
    padding: 8px;
    border-radius: 4px;
    text-align: center;
    line-height: 1.4;
}
.checkpoint-feedback.correct   { background: #d4edda; color: #155724; border: 1px solid #28a745; }
.checkpoint-feedback.incorrect { background: #f8d7da; color: #721c24; border: 1px solid #dc3545; }

/* ===========================
   ANALYTICS SECTION
   =========================== */
.analytics-section {
    background: white;
    border-top: 2px solid #ddd;
    flex-shrink: 0;
}

.analytics-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 16px;
    background: #f8f9fa;
    border-bottom: 1px solid #ddd;
}
.analytics-header h3 { font-size: 1rem; margin: 0; }

.analytics-controls { display: flex; gap: 8px; align-items: center; }

.analytics-content {
    max-height: 150px;
    overflow-y: auto;
    padding: 8px;
    transition: all 0.3s ease;
}
.analytics-content.collapsed { max-height: 0; padding: 0; overflow: hidden; }

.log-entries { font-size: 0.85rem; line-height: 1.6; }

.log-entry {
    padding: 4px 8px;
    margin-bottom: 4px;
    background: #f8f9fa;
    border-left: 3px solid #667eea;
    border-radius: 3px;
}
.log-entry.interaction { border-left-color: #28a745; }
.log-entry.quiz        { border-left-color: #ffc107; }

/* ===========================
   TOOLTIP
   =========================== */
.tooltip {
    position: fixed;
    background: white;
    border: 2px solid #667eea;
    border-radius: 8px;
    padding: 14px 16px 12px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.18);
    max-width: 280px;
    z-index: 1000;
    pointer-events: auto;
}
.tooltip.hidden { display: none; }

.tooltip-close {
    position: absolute;
    top: 4px; right: 4px;
    background: transparent;
    border: none;
    font-size: 1.4rem;
    color: #999;
    cursor: pointer;
    width: 28px; height: 28px;
    display: flex; align-items: center; justify-content: center;
    border-radius: 4px;
    transition: all 0.2s ease;
}
.tooltip-close:hover { background: #f0f0f0; color: #333; }

#tooltipText {
    margin: 0;
    font-size: 0.92rem;
    line-height: 1.65;
    -webkit-user-select: text;
    user-select: text;
}

/* ===========================
   RESPONSIVE DESIGN
   =========================== */
@media (max-width: 768px) {
    .main-content    { flex-direction: column; overflow: auto; }
    .diagram-container { min-height: 280px; flex: 0 0 auto; }
    .control-panel   { width: 100%; overflow: visible; }
    .title-section h1 { font-size: 1rem; }
    .button-group    { flex-direction: row; }
    .control-btn     { min-width: calc(50% - 4px); }
    .analytics-controls { flex-wrap: wrap; }
    .analytics-height-slider { width: 80px; }
}

@media (max-width: 480px) {
    .title-section h1 { font-size: 0.9rem; }
    .main-content     { padding: 8px; gap: 8px; }
    .area-label       { font-size: 24px; }
    .side-label       { font-size: 20px; }
    .slider-label     { font-size: 0.9rem; }
    .control-btn      { font-size: 0.88rem; padding: 10px 8px; }
    .eq-formula       { font-size: 0.85rem; }
    .area-breakdown   { font-size: 0.8rem; }
}

@media (min-width: 1200px) {
    .main-content { max-width: 1400px; margin: 0 auto; }
}

/* ===========================
   ACCESSIBILITY
   =========================== */
button:focus, input:focus { outline: 3px solid #667eea; outline-offset: 2px; }

@media (prefers-contrast: high) {
    .control-btn { border: 2px solid #000; }
    .slider::-webkit-slider-thumb { border: 3px solid #000; }
}

@media (prefers-reduced-motion: reduce) {
    * { animation: none !important; transition: none !important; }
}