/* ============================================================
   MathInput Component — compact keyboard panel
   ============================================================ */

/* --- Hide ALL MathLive built-in UI chrome --- */
math-field::part(virtual-keyboard-toggle),
math-field::part(menu-toggle),
math-field::part(menu-button) {
    display: none !important;
}
.ML__keyboard-toggle,
.ML__menu-toggle,
.ML__virtual-keyboard-toggle {
    display: none !important;
}

/* --- Base math-field --- */
math-field {
    display: block;
    width: 100%;
    min-height: 40px;
    font-size: 17px;
    padding: 5px 8px;
    border: 1px solid #8c8f94;
    border-radius: 4px;
    background: #fff;
    box-sizing: border-box;
    cursor: text;
    outline: none;
}

math-field:focus-within {
    border-color: #2271b1;
    box-shadow: 0 0 0 1px #2271b1;
    outline: none;
}

/* --- Wrapper --- */
.ama-math-input-wrapper {
    display: inline-block;
    width: 100%;
}

.ama-math-single {
    max-width: 540px;
}

/* --- Field row: [math-field] [toggle-btn] --- */
.ama-mf-row {
    display: flex;
    align-items: stretch;
    gap: 3px;
}

.ama-mf-row math-field {
    flex: 1;
    min-width: 0;
    width: auto;
}

/* --- Keyboard toggle button --- */
.ama-kb-toggle-btn {
    flex-shrink: 0;
    width: 34px;
    background: #f6f7f7;
    border: 1px solid #8c8f94;
    border-radius: 4px;
    cursor: pointer;
    font-size: 15px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #555;
    padding: 0;
    line-height: 1;
    user-select: none;
    transition: background 0.12s, color 0.12s;
}

.ama-kb-toggle-btn:hover {
    background: #e0e8f5;
    color: #2271b1;
    border-color: #2271b1;
}

.ama-kb-toggle-btn.active {
    background: #2271b1;
    color: #fff;
    border-color: #2271b1;
}

/* --- Compact keyboard panel --- */
.ama-compact-kb {
    display: none;
    background: #f0f0f1;
    border: 1px solid #8c8f94;
    border-radius: 4px;
    padding: 0;
    margin-top: 3px;
    overflow: hidden;
}

.ama-math-input-wrapper.kb-open .ama-compact-kb,
.ama-math-option-wrap.kb-open .ama-compact-kb {
    display: block;
}

/* --- Tab bar --- */
.ama-kb-tabs {
    display: flex;
    background: #e2e2e3;
    border-bottom: 1px solid #c3c4c7;
}

.ama-kb-tab {
    flex: 1;
    padding: 5px 0;
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    cursor: pointer;
    font-size: 12px;
    font-weight: 600;
    color: #666;
    user-select: none;
    transition: background 0.1s, color 0.1s;
    letter-spacing: 0.5px;
}

.ama-kb-tab:hover {
    background: #d4d5d8;
    color: #333;
}

.ama-kb-tab.active {
    background: #f0f0f1;
    color: #2271b1;
    border-bottom-color: #2271b1;
}

/* --- Panels (math / letters) --- */
.ama-kb-panel {
    display: none;
    padding: 5px;
}

.ama-kb-panel.active {
    display: block;
}

/* --- Keyboard rows --- */
.ama-kb-row {
    display: flex;
    gap: 3px;
    margin-bottom: 3px;
    justify-content: center;
}

.ama-kb-row:last-child {
    margin-bottom: 0;
}

/* Letter rows: slightly smaller buttons, full-width spread */
.ama-kb-row-letters {
    justify-content: center;
}

/* --- Keyboard buttons --- */
.ama-kb-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 34px;
    height: 36px;
    padding: 0 6px;
    background: #fff;
    border: 1px solid #c3c4c7;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    color: #1d2327;
    line-height: 1;
    user-select: none;
    transition: background 0.1s, border-color 0.1s;
    font-family: inherit;
    box-shadow: 0 1px 2px rgba(0,0,0,0.08);
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.ama-kb-btn:hover {
    background: #e0e8f5;
    border-color: #2271b1;
    color: #135e96;
}

.ama-kb-btn:active {
    background: #c8d8f0;
    transform: scale(0.95);
    box-shadow: none;
}

/* Letter buttons: slightly narrower */
.ama-kb-letter {
    min-width: 30px;
    font-size: 14px;
    font-weight: 500;
    flex: 1;
}

/* Shift button */
.ama-kb-shift {
    min-width: 46px;
    background: #e8e8e8;
    color: #333;
    font-size: 16px;
}

.ama-kb-shift.active {
    background: #2271b1;
    color: #fff;
    border-color: #135e96;
}

/* Space button */
.ama-kb-space {
    flex: 3;
    min-width: 80px;
    background: #f0f0f1;
    color: #555;
    font-size: 12px;
    letter-spacing: 0.5px;
}

.ama-kb-del {
    color: #b32d2e;
    border-color: #d63638;
    background: #fff5f5;
    min-width: 44px;
}

.ama-kb-del:hover {
    background: #fcdcdc;
    border-color: #b32d2e;
}

.ama-kb-nav {
    background: #f0f0f1;
    color: #555;
    min-width: 44px;
}

/* Fraction icon */
.ama-kb-frac-icon {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    line-height: 1;
    gap: 1px;
}

.ama-kb-frac-icon span:first-child {
    font-size: 9px;
    border-bottom: 1px solid currentColor;
    padding: 0 3px;
}

.ama-kb-frac-icon span:last-child {
    font-size: 9px;
    padding: 0 3px;
}

/* --- Multi-mode (option rows) --- */
.ama-math-multi {
    max-width: 660px;
}

.ama-math-rows {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 8px;
}

.ama-math-row {
    display: flex;
    align-items: flex-start;
    gap: 6px;
}

.ama-math-row-label {
    font-weight: 600;
    color: #555;
    min-width: 20px;
    font-size: 14px;
    padding-top: 10px;
    flex-shrink: 0;
}

.ama-math-option-wrap {
    flex: 1;
    min-width: 0;
}

.ama-math-option-wrap math-field {
    font-size: 15px;
    min-height: 34px;
}

.ama-math-remove-row {
    flex-shrink: 0;
    margin-top: 6px;
    background: #fecaca;
    border: 1px solid #ef4444;
    border-radius: 4px;
    width: 26px;
    height: 26px;
    cursor: pointer;
    font-size: 15px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #dc2626;
    transition: background 0.12s;
    padding: 0;
}

.ama-math-remove-row:hover {
    background: #fca5a5;
}

.ama-math-add-row {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 5px 14px;
    background: #f0f9ff;
    border: 1px dashed #93c5fd;
    border-radius: 4px;
    cursor: pointer;
    color: #2563eb;
    font-size: 13px;
    transition: background 0.12s;
}

.ama-math-add-row:hover {
    background: #dbeafe;
}

/* --- Frontend: student numerical input --- */
.ama-numerical-input {
    text-align: center;
}

.ama-numerical-input .ama-math-input-wrapper {
    display: inline-block;
    max-width: 420px;
    width: 100%;
    text-align: left;
}

.ama-numerical-input math-field {
    font-size: 20px;
    min-height: 46px;
    border: 1px solid var(--ama-border, #ddd);
    border-radius: 4px;
}

.ama-numerical-input math-field:focus-within {
    border-color: var(--ama-primary, #2e68a1);
    box-shadow: 0 0 0 2px rgba(46, 104, 161, 0.12);
}

/* --- Responsive --- */
@media (max-width: 480px) {
    .ama-kb-btn {
        min-width: 26px;
        height: 38px;
        font-size: 13px;
        padding: 0 3px;
    }

    .ama-kb-letter {
        min-width: 24px;
        height: 40px;
        font-size: 15px;
    }

    .ama-kb-shift { min-width: 36px; }
    .ama-kb-del   { min-width: 36px; }

    .ama-numerical-input math-field {
        font-size: 17px;
        min-height: 40px;
    }

    .ama-numerical-input .ama-math-input-wrapper {
        max-width: 100%;
    }
}
