/* ========================================
   CLAVIER MATHÉMATIQUE KHAN ACADEMY
   École Préparatoire ضفاف البحيرة
   Professeur Faouzi Gharbi
   ======================================== */

/* ========================================
   VARIABLES GLOBALES
   ======================================== */
:root {
    --mkk-primary: #1865f2;
    --mkk-primary-hover: #1557d0;
    --mkk-bg-light: #f9fafb;
    --mkk-bg-header: #f3f4f6;
    --mkk-border: #e5e7eb;
    --mkk-text-dark: #1c1d1f;
    --mkk-text-gray: #6b7280;
    --mkk-text-light: #9ca3af;
    --mkk-btn-bg: #ffffff;
    --mkk-btn-hover: #f3f4f6;
    --mkk-btn-active: #e5e7eb;
    --mkk-backspace-bg: #f3f4f6;
    --mkk-clear-bg: #fee2e2;
    --mkk-clear-text: #dc2626;
    --mkk-clear-hover: #fecaca;
    --mkk-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    --mkk-transition: all 0.12s ease;
}

/* ========================================
   INPUT MATHÉMATIQUE
   ======================================== */
.math-input-wrapper {
    position: relative;
    margin: 25px 0;
}

.math-input {
    width: 100%;
    min-height: 70px;
    padding: 16px 55px 16px 20px;
    font-size: 24px;
    border: 3px solid var(--mkk-border);
    border-radius: 12px;
    outline: none;
    transition: var(--mkk-transition);
    font-family: 'Times New Roman', serif;
    text-align: center;
    background: white;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: auto;
    direction: ltr !important; /* IMPORTANT : Maths toujours en LTR */
}

.math-input:focus {
    border-color: var(--mkk-primary);
    box-shadow: 0 0 0 4px rgba(24, 101, 242, 0.1);
}

.math-input:empty:before {
    content: attr(data-placeholder);
    color: var(--mkk-text-light);
}

/* ========================================
   FRACTIONS VISUELLES DANS L'INPUT
   ======================================== */
.math-input .fraction {
    display: inline-flex;
    flex-direction: column;
    align-items: stretch;
    margin: 0 6px;
    vertical-align: middle;
    min-width: 40px;
}

.math-input .fraction .numerator {
    border-bottom: 2px solid var(--mkk-text-dark);
    padding: 4px 8px;
    font-size: 22px;
    font-weight: 500;
    text-align: center;
    outline: none;
    min-width: 30px;
    background: transparent;
}

.math-input .fraction .numerator:focus {
    background: #f0f9ff;
}

.math-input .fraction .numerator:empty:before {
    content: '';
    display: inline-block;
    width: 0;
}

.math-input .fraction .denominator {
    padding: 4px 8px;
    font-size: 22px;
    font-weight: 500;
    text-align: center;
    outline: none;
    min-width: 30px;
    background: transparent;
}

.math-input .fraction .denominator:focus {
    background: #f0f9ff;
}

.math-input .fraction .denominator:empty:before {
    content: '';
    display: inline-block;
    width: 0;
}

/* ========================================
   BOUTON DÉCLENCHEUR DU CLAVIER
   ======================================== */
.keyboard-trigger {
    position: absolute;
    right: 10px; /* À DROITE car input LTR */
    top: 50%;
    transform: translateY(-50%);
    width: 42px;
    height: 42px;
    background: var(--mkk-primary);
    border: none;
    border-radius: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    transition: var(--mkk-transition);
    box-shadow: 0 2px 8px rgba(24, 101, 242, 0.3);
    z-index: 1;
}

.keyboard-trigger:hover {
    background: var(--mkk-primary-hover);
    transform: translateY(-50%) scale(1.05);
}

.keyboard-trigger:active {
    transform: translateY(-50%) scale(0.95);
}

/* ========================================
   POPUP DU CLAVIER
   ======================================== */
.math-keyboard-popup {
    position: fixed;
    width: 420px;
    min-width: 320px;
    max-width: 90vw;
    background: white;
    border-radius: 12px;
    box-shadow: var(--mkk-shadow);
    z-index: 9999;
    display: none;
}

.math-keyboard-popup.show {
    display: block;
}

/* ========================================
   HEADER (DRAGGABLE)
   ======================================== */
.mkk-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: var(--mkk-bg-header);
    border-bottom: 2px solid var(--mkk-border);
    cursor: move;
    user-select: none;
    border-radius: 12px 12px 0 0;
}

.mkk-header:active {
    cursor: grabbing;
}

/* Indicateur de drag (3 lignes) */
.mkk-drag-indicator {
    position: absolute;
    top: 50%;
    left: 8px;
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    gap: 2px;
    pointer-events: none;
}

.mkk-drag-indicator span {
    width: 20px;
    height: 2px;
    background: #d1d5db;
    border-radius: 1px;
}

/* ========================================
   ONGLETS
   ======================================== */
.mkk-tabs {
    display: flex;
    gap: 4px;
}

.mkk-tab {
    padding: 6px 12px;
    background: transparent;
    border: none;
    border-bottom: 3px solid transparent;
    cursor: pointer;
    font-size: 16px;
    font-weight: 600;
    color: var(--mkk-text-gray);
    transition: var(--mkk-transition);
}

.mkk-tab:hover {
    color: var(--mkk-text-dark);
}

.mkk-tab.active {
    color: var(--mkk-primary);
    border-bottom-color: var(--mkk-primary);
}

/* ========================================
   BOUTON FERMER
   ======================================== */
.mkk-close-btn {
    width: 28px;
    height: 28px;
    background: transparent;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    color: var(--mkk-text-light);
    transition: var(--mkk-transition);
}

.mkk-close-btn:hover {
    color: var(--mkk-text-dark);
}

/* ========================================
   BODY DU CLAVIER
   ======================================== */
.mkk-body {
    padding: 16px;
    background: var(--mkk-bg-light);
    max-height: 400px;
    overflow-y: auto;
    border-radius: 0 0 12px 12px;
}

.mkk-body::-webkit-scrollbar {
    width: 6px;
}

.mkk-body::-webkit-scrollbar-track {
    background: var(--mkk-bg-light);
}

.mkk-body::-webkit-scrollbar-thumb {
    background: var(--mkk-border);
    border-radius: 3px;
}

/* ========================================
   GRILLE DE TOUCHES
   ======================================== */
.mkk-grid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 6px;
}

/* ========================================
   TOUCHES STANDARD
   ======================================== */
.mkk-key {
    height: 48px;
    background: var(--mkk-btn-bg);
    border: 1px solid var(--mkk-border);
    border-radius: 8px;
    cursor: pointer;
    font-size: 20px;
    font-weight: 700;
    color: var(--mkk-text-dark);
    transition: var(--mkk-transition);
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
}

.mkk-key:hover {
    background: var(--mkk-btn-hover);
    transform: scale(1.05);
}

.mkk-key:active {
    transform: scale(0.95);
    background: var(--mkk-btn-active);
}

/* ========================================
   TOUCHE FRACTION
   ======================================== */
.mkk-key.fraction {
    font-size: 16px;
    padding: 4px;
    flex-direction: column;
    gap: 0;
}

.mkk-key.fraction .frac-box {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.mkk-key.fraction .frac-num {
    width: 14px;
    height: 14px;
    border: 2px solid var(--mkk-text-dark);
    border-radius: 2px;
    margin-bottom: 1px;
}

.mkk-key.fraction .frac-line {
    width: 20px;
    height: 2px;
    background: var(--mkk-text-dark);
    margin: 1px 0;
}

.mkk-key.fraction .frac-den {
    width: 14px;
    height: 14px;
    border: 2px solid var(--mkk-text-dark);
    border-radius: 2px;
    margin-top: 1px;
}

/* ========================================
   TOUCHE PUISSANCE
   ======================================== */
.mkk-key.power {
    font-size: 18px;
    gap: 2px;
}

.mkk-key.power .power-base {
    width: 16px;
    height: 16px;
    border: 2px solid var(--mkk-text-dark);
    border-radius: 2px;
    display: inline-block;
}

.mkk-key.power .power-exp {
    font-size: 11px;
    align-self: flex-start;
    margin-left: -2px;
}

/* ========================================
   TOUCHE RACINE
   ======================================== */
.mkk-key.root {
    font-size: 24px;
    gap: 2px;
}

.mkk-key.root .root-symbol {
    font-size: 24px;
}

.mkk-key.root .root-box {
    width: 14px;
    height: 14px;
    border: 2px solid var(--mkk-text-dark);
    border-radius: 2px;
    border-left: none;
    margin-left: -4px;
}

/* ========================================
   TOUCHE BACKSPACE
   ======================================== */
.mkk-key.backspace {
    background: var(--mkk-backspace-bg);
    font-size: 18px;
}

.mkk-key.backspace:hover {
    background: var(--mkk-btn-active);
}

/* ========================================
   TOUCHE EFFACER TOUT
   ======================================== */
.mkk-key.clear {
    background: var(--mkk-clear-bg);
    color: var(--mkk-clear-text);
    font-size: 18px;
}

.mkk-key.clear:hover {
    background: var(--mkk-clear-hover);
}

.mkk-key.clear:active {
    background: #fca5a5;
}

/* ========================================
   CONTENU DES ONGLETS
   ======================================== */
.mkk-tab-content {
    display: none;
}

.mkk-tab-content.active {
    display: block;
}

/* ========================================
   POIGNÉE DE REDIMENSIONNEMENT
   ======================================== */
.mkk-resize-handle {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 20px;
    height: 20px;
    background: linear-gradient(135deg, transparent 50%, #9ca3af 50%);
    cursor: nwse-resize;
    border-bottom-right-radius: 12px;
}

/* ========================================
   RESPONSIVE - MOBILE
   ======================================== */
@media (max-width: 480px) {
    .math-keyboard-popup {
        width: 95% !important;
    }

    .mkk-grid {
        gap: 4px;
    }

    .mkk-key {
        height: 44px;
        font-size: 18px;
    }

    .math-input {
        font-size: 20px;
        min-height: 60px;
        padding: 12px 50px 12px 16px;
    }

    .keyboard-trigger {
        width: 38px;
        height: 38px;
        font-size: 18px;
    }
}

/* ========================================
   ANIMATIONS
   ======================================== */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.math-keyboard-popup.show {
    animation: fadeIn 0.2s ease-out;
}

/* ========================================
   UTILITAIRES
   ======================================== */
.mkk-hidden {
    display: none !important;
}

.mkk-no-select {
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

/* ========================================
   PRINT - Masquer le clavier à l'impression
   ======================================== */
@media print {
    .math-keyboard-popup,
    .keyboard-trigger {
        display: none !important;
    }
}
/* ========================================
   EXPOSANTS DANS L'INPUT
   ======================================== */
.math-input .superscript {
    font-size: 0.65em;
    vertical-align: super;
    line-height: 0;
    display: inline-block;
    min-width: 8px;
}

.math-input .superscript:focus {
    background: #f0f9ff;
    outline: none;
}

/* ========================================
   BOUTON PUISSANCE FLOTTANT (STYLE KHAN)
   ======================================== */
.power-trigger-btn {
    position: fixed;
    width: 36px;
    height: 36px;
    background: #1865f2;
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 18px;
    cursor: pointer;
    display: none;
    z-index: 10000;
    box-shadow: 0 2px 12px rgba(24, 101, 242, 0.5);
    transition: all 0.2s ease;
}

.power-trigger-btn.show {
    display: flex;
    align-items: center;
    justify-content: center;
}

.power-trigger-btn:hover {
    background: #1557d0;
    transform: scale(1.15);
}

.power-trigger-btn:active {
    transform: scale(0.95);
}